Skip to content

API review: aliases, shared code, docs and examples for every algorithm - #2

Merged
jmacheta merged 64 commits into
mainfrom
feature/api-review
Oct 4, 2026
Merged

jmacheta merged 64 commits into
mainfrom
feature/api-review

Conversation

@jmacheta

@jmacheta jmacheta commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

Review of every algorithm except the CRC: API, implementation, structure, docs and examples. No hash or checksum value changes.

API

  • Variant aliases: fletcher16/32/64, xxh32, xxh64, xxh3_64, xxh3_128, murmur3_32, murmur3_128, each a <name>_state alias and a <name>_compute(data[, seed]) function. _update and _finalize take the width from the state.
  • fletcher4_compute on a std::string_view or std::vector now takes the out-of-line run-time path, like a span. Before, it lost that path on Cortex-M4 and called memset.
  • checksum::hash128 {low, high} is the one 128-bit result type: XXH3-128 (was xxh3_hash128) and MurmurHash3_x64_128 (was std::array<std::uint64_t, 2>; low = h1, high = h2). Generated code is identical on x86-64 and Cortex-M4.
  • Continue from a stored checksum: adler32_update(std::uint32_t, data), internet_update(std::uint16_t, data) and fletcher_update(std::uint16/32/64_t, data) return the checksum of the stored message plus data. The checksum type must match exactly, and {} still means the empty state. Internet continues after even lengths only, Fletcher-32/64 after whole blocks.
  • Nothing else had to leave the public headers.

Implementation

  • Fletcher and Adler-32 share their run-time loops in checksum_private/fletcher_loops.hpp, like crc_table_loops.hpp. The two sum_loop.cpp files shrink from 300 lines to about 60.
  • One little-endian detail::load in byte_range.hpp replaces the copies in xxHash and MurmurHash3. XXH3 reuses the XXH64 avalanche.
  • Smaller cleanups: XXH3 accumulate/scramble only as kernel members, one AVX2 block in the fletcher4 header, the MurmurHash3 2-byte tail read by load.
  • checksum_private/arch.hpp is gone: each <algorithm>_arch.hpp selects its architecture directory.
  • One pseudo-random data generator, tests/include/test_data.hpp, replaces 15 copies in tests, reference vectors and benchmarks.
  • -Wduplicated-cond and -Wlogical-op are passed to GCC only, so Clang no longer warns about unknown options.

Performance (nRF52840, Cortex-M4, MB/s at 64 MHz, before → after)

20 B 64 B 256 B 4 KiB
Adler-32 4.8 → 4.7 9.1 → 10.0 17.2 → 17.9 23.8 → 23.8
Fletcher-16 4.5 → 5.0 9.0 → 9.7 17.1 → 17.7 23.7 → 23.8
Fletcher-32 4.2 → 4.9 10.6 → 12.2 21.6 → 23.1 30.2 → 30.9
Fletcher-64 5.0 → 5.2 12.3 → 12.8 24.5 → 24.9 35.5 → 35.5

The other algorithms are unchanged within ±3 %. The Adler-32 20 B difference moves with code placement in flash. On x86 with AVX2, Fletcher is 30–90 % faster at 64 and 256 B and equal at 4 KiB.

Against reference libraries (follow-up)

  • AArch64 XXH3 kernel: at -O2 GCC kept the scalar lanes on the stack; with the lanes written out it runs 1.15× xxHash v0.8.4 at 4 KiB on the A72 (was 0.69×). The guide's old A72 figures had been taken at -O3 and are re-measured at -O2.
  • One-shot XXH64 (64-bit targets) and MurmurHash3 x86_32 keep their lanes in registers: XXH64 at 64 B on x86 0.87 → 1.01× xxHash, MurmurHash3 x86_32 at 20 B on the A72 0.81 → 1.00× SMHasher.
  • Review fixes: XXH3 builds without SSE2 on x86-64, fletcher4_arch.hpp includes what it uses, NDEBUG note in the CRC guide, one hash per key in the Bloom filter example.
  • Tests: Debug test builds use -Og (cross-arm-portable keeps -O0 for the inline assembly), fewer Fletcher/Adler-32 offsets and smaller all-ones messages; x86_64-gcc-pclmul now builds the VPCLMULQDQ CRC kernel and cross-aarch64-pmull the EOR3 path. CI 4 min → 2 min 55 s.

Closing the gaps to the references

Every cell that was below 0.85× of the fastest reference is now at 0.85× or above, except Fletcher-32 at 20 B on x86 (x86 GCC 16 -O2 -march=native, Raspberry Pi 4 A72 -mcpu=cortex-a72):

Cell Before After
CRC-32 / CRC-32C / CRC-64/XZ, x86 256 B vs ISA-L 0.78 / 0.71 / 0.75× 1.01 / 0.95 / 0.96×
Internet checksum, x86 256 B vs Linux 0.84× 0.97×
Internet checksum, A72 20 B vs Linux 0.62× 1.10×
Adler-32, x86 256 B / 4 KiB / 1 MiB vs zlib-ng 0.72 / 0.74 / 0.79× 0.91 / 1.14 / 1.17×
Adler-32, A72 4 KiB / 1 MiB 0.72 / 0.77× 0.96 / 0.99×
Fletcher-16 / Fletcher-32, x86 20 B 0.68 / 0.54× 1.00 / 0.74×
Fletcher-32, A72 20 B 0.52× 0.85× (0.79× with -march=armv8-a)
fletcher4, x86 20 B / 256 B vs OpenZFS 0.81 / 0.62× 0.93 / 1.09×
fletcher4, A72 256 B 0.55× 1.18×
  • CRC: the x86 wide fold reduces its last 8 blocks in one step, the Barrett reduction stays in vector registers, CRC-32C ends with crc32 instructions. Folding constants grow from 80 to 160 B per table.
  • Internet checksum: adc chains on x86-64, one masked load of the last word, a two-step fold. Code size grows (Cortex-M4 -Os 202 → 284 B).
  • Adler-32/Fletcher: an AVX-VNNI kernel (selected by __AVXVNNI__; tested locally only, QEMU has no AVX-VNNI), an AArch64 NEON kernel over 64-byte groups, short inputs without store-forwarding stalls.
  • fletcher4: the lane combination is written out, no stack-protector check on the short path, lower lane thresholds.
  • Other sizes, Clang, AArch32 and the nRF52840 stay within 3 %; the few exceptions are listed in the guides.
  • The Fletcher, Adler-32 and Internet checksum tables in the guides are re-measured on the current code (x86 on an idle host, A72, nRF52840, STM32L4A6), with the Fletcher reference loop in the same harness. The Internet checksum kernels break even below their thresholds (x86 about 350 B, AArch64 370 B, AArch32 100-130 B, Cortex-M4 120-160 B); the thresholds stay at 512 and 192 B.

Docs and examples

  • Every guide was checked against the code.
    • Fixes: the Internet checksum quick start, Fletcher kernel chunk sizes (2 to 256 KiB), the MurmurHash3 x86_32 vs x64_128 claim on x86, the AArch64 XXH3 threshold when streaming, the CHECKSUM_EXAMPLES row.
    • The Internet checksum and MurmurHash3 guides now use the same section order as the others.
  • ut_internet_checksum.cpp is renamed to ut_internet.cpp.
  • Examples: 23 programs with one structure. Every directory has compile_time.cpp, and each program checks its result against a known value:
    • examples/internet: IPv4 header, UDP datagram, TTL decrement from the stored checksum.
    • examples/fletcher, examples/adler32: compile time, file checksum, frame check, zlib trailer, resume.
    • examples/fletcher4: ZFS block, file checksum.
    • examples/murmur3: command dispatch, Bloom filter, deduplication.
    • examples/xxhash, examples/xxh3: compile-time IDs, file hash, cache key, block dedup, seeded hash table.

Not done

  • A shared *_update(state, Range&&) helper: each of those overloads is already two lines.

fletcher4_compute(Range) now forwards contiguous ranges to the span overload at run time, as xxh3_compute does, so string views and vectors also get the out-of-line compute_loop. The x86-64 header keeps each threshold next to its kernel.
xxh32_state, xxh64_state, xxh32_compute and xxh64_compute forward to the generic templates. Also document the 2^32 length wrap of XXH32 on xxhash_finalize and spell detail types as word<Width>.
Fletcher and Adler-32 now run one portable loop, chunk loop and kernel
dispatch from checksum_private/fletcher_loops.hpp, with one reduction
bound and one division-free reduction. The public headers keep only
the constant-evaluation loop; the run-time path finishes an unfinished
Fletcher block in src/.
The constant-evaluation loop, which also finishes unfinished blocks at
run time, kept its sums in 64 bits; on Cortex-M4 this cost 150 bytes
of code in the library.
The shared loops take the checksum's own state, adler32_state or a
two-member sum_pair, so states of up to 4 bytes stay in one register
on 32-bit Arm. Callers branch on runs_kernel() themselves, so that
Adler-32 tail-calls the kernel path again, and Fletcher-32 and
Fletcher-64 run the byte loop out of line and only for bytes of an
unfinished block.

nRF52840, MB/s at 64 B: Adler-32 9.1 -> 10.0, Fletcher-16 9.0 -> 9.7,
Fletcher-64 12.3 -> 12.8.
At -O2 GCC did not unroll the scalar lane and scramble loops of the NEON kernel and kept the lanes in memory: the kernel ran 25 % slower than the portable loop on a Cortex-A72. Spelled out per vector and lane it beats the portable loop from the first stripe, so the threshold drops from 448 bytes to 0.
The one-shot XXH64 now runs the same out-of-line function as XXH32, with the lanes in registers and converge() spelled out: 0.88 to 1.0 of xxHash 0.8.4 at 64 bytes on x86-64 with GCC 16, and 1.3 at 20 bytes. A compiler barrier keeps GCC from folding the last stripe multiplication into converge(), which slowed the stripe loop. 32-bit targets keep the stripe loop: on Cortex-M4 the 64-bit lanes do not fit the registers.
The one-shot MurmurHash3_x86_32 runs one out-of-line function instead of passing its lane through memory to block_loop(): on a Cortex-A72 0.81 to 1.0 of SMHasher at 20 bytes and 0.92 to 0.98 at 64 bytes. On Cortex-M4 the loop is the same.
…om the first stripe

The Cortex-A72 tables were measured at -O3, which hid the slow AArch64 kernel; they now come from the -O2 benchmark builds. Adds one-line comparisons with xxHash v0.8.4 and SMHasher.
The VPCLMULQDQ loop folds the 8 blocks left after its 256- and 128-byte steps
onto the last one with one constant pair per distance (by_seven to by_two,
new in folding_constants), and a 64-byte remainder with one more 256-bit step,
instead of three dependent 128-bit folds. The Barrett reduction stays in
vector registers, and the wide loop is inlined into the non-inlined
long-input callers instead of a second call.
…uctions

Branches that a 256-byte input does not take are marked unlikely, so that
path runs without taken jumps. CRC-32C folds its 8 blocks onto the last two
and hands those 32 bytes and the 0..63 remaining ones to the crc32
instruction. CRC-32C at 256 bytes: 0.84 to 0.93 of ISA-L.
Re-measured accelerated sliced rows (GCC 16, Core Ultra 7 155H), engine sizes
and footprints with the larger folding constants, the decisions behind the
single-step fold, and comments that state only measured gains.
The portable loop takes 64-byte blocks in two chains of four words.
On x86-64 __builtin_addcll turns each chain into add/adc with the carry
added back at once, as Linux csum_partial does. The bytes after the
last whole word come from one load of the message's last word with the
summed bytes masked off, instead of three branches. fold() adds a value
to its rotation by half the width. Short inputs return early and the
kernel call is a tail call.
…VX2 lanes from 128 B

GCC vectorized the weight loop of combine() into emulated 64-bit vector
multiplies; explicit sums with constant weights become shifts and adds.
compute_loop() writes the long result into its own return value, so no
local array triggers a stack protector check, and the long path builds
its empty state itself.
…es from 192 B

WIP: docs, cross-check tables and the nRF52840 run still to do.
With the cheaper lane combination they overtake the word loop at 192 B on
a Cortex-A72 in AArch32 with GCC and Clang, as on AArch64.
Same code; the constants of combine() get names.
vpdpbusd weights four 32-byte blocks by 127..0 in one instruction each, so the
previous sums are added once per 128 bytes; the lane totals reduce together.
From 320 bytes, group_kernel sums four 16-byte blocks per iteration with
pairwise additions and 64 column sums weighted 64..1, as zlib-ng does;
sum_long() reaches it through a tail call so its registers do not slow the
16-byte kernel. Adler-32 on the Cortex-A72 at 4 KiB: 4 746 -> 6 270 MB/s.
AArch32 keeps the 16-byte kernel: with half the registers it spilled.
GCC returned small states by storing their members one by one and loading
them as one word. The sums are now built in one integer, Fletcher-16/-32
pass their state to sum_loop() as one integer, unfinished blocks go to a
separate function, 64-bit Fletcher sums reduce with a constant %, and
AArch64 sums 16-bit values in 32 bits. Against a plain deferred-modulo loop
at 20 bytes: Fletcher-16 0.68 -> 0.94x and Fletcher-32 0.51 -> 0.86x on
x86-64, Fletcher-32 0.52 -> 0.88x on the Cortex-A72.
x86-64 (Core Ultra 7 155H), Cortex-A72 and Cortex-M4 (nRF52840, STM32L4A6) figures come from one run of the current
code; the notes on figures that predated the portable loop changes are gone. Break-even points of the Internet kernels
are re-measured, and the Wikipedia-loop comparison at 20 bytes runs in the same harness.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The broad API changes and architecture-specific checksum kernels require final human review and the full native and cross-platform verification matrix.

Review effort: Balanced
Findings: None

What changed in this PR

Broad API and implementation review that standardizes algorithm aliases and 128-bit hashes, consolidates shared loops/test data, expands examples, and optimizes architecture-specific paths without intentionally changing results.

Changes:

  • Adds variant aliases, resumable checksum APIs, and shared hash128.
  • Refactors and accelerates Fletcher, Adler-32, Internet checksum, CRC, xxHash, XXH3, and MurmurHash3 internals.
  • Expands documentation, examples, tests, presets, and shared test infrastructure.
File Description
tests/​unit_tests/​ut_xxhash.cpp Tests aliases and shared data.
tests/​unit_tests/​ut_xxh3.cpp Tests aliases and hash128.
tests/​unit_tests/​ut_murmur3.cpp Tests aliases and hash128.
tests/​unit_tests/​ut_internet.cpp Tests continuation and loop boundaries.
tests/​unit_tests/​ut_fletcher4.cpp Uses shared test data.
tests/​unit_tests/​ut_fletcher.cpp Tests aliases, continuation, and kernels.
tests/​unit_tests/​ut_crc_engine.cpp Updates constants and test data.
tests/​unit_tests/​ut_crc_accumulator.cpp Uses shared test data.
tests/​unit_tests/​ut_crc_acceleration.cpp Uses shared test data.
tests/​unit_tests/​ut_adler32.cpp Tests continuation and kernels.
tests/​unit_tests/​include/​xxhash_reference_vectors.hpp Removes duplicated generator.
tests/​unit_tests/​include/​xxh3_reference_vectors.hpp Adopts shared hash128.
tests/​unit_tests/​include/​murmur3_reference_vectors.hpp Adopts shared hash128.
tests/​unit_tests/​include/​fletcher4_reference_vectors.hpp Removes duplicated generator.
tests/​unit_tests/​include/​crc_test_support.hpp Uses shared test data.
tests/​unit_tests/​CMakeLists.txt Renames test and links helper target.
tests/​include/​test_data.hpp Adds shared deterministic generator.
tests/​CMakeLists.txt Defines test-data interface target.
tests/​benchmarks/​CMakeLists.txt Links shared test data.
tests/​benchmarks/​bench_xxhash.cpp Uses shared data and aliases.
tests/​benchmarks/​bench_murmur3.cpp Uses shared data.
tests/​benchmarks/​bench_internet.cpp Uses shared data.
tests/​benchmarks/​bench_fletcher4.cpp Uses shared data.
tests/​benchmarks/​bench_fletcher.cpp Uses shared data.
tests/​benchmarks/​bench_crc.cpp Uses shared data.
tests/​benchmarks/​bench_adler32.cpp Uses shared data.
src/​xxhash/​stripe_loop.cpp Adds optimized one-shot hashes.
src/​xxh3/​stripe_loop.cpp Adopts shared hash128.
src/​murmur3/​block_loop.cpp Adds optimized 32-bit hash path.
src/​internet/​sum_loop.cpp Adds carry chains and masked tails.
src/​include/​checksum/​xxhash.hpp Adds aliases and shared loading.
src/​include/​checksum/​murmur3.hpp Adds aliases and hash128.
src/​include/​checksum/​internet.hpp Adds checksum continuation API.
src/​include/​checksum/​hash128.hpp Defines common 128-bit result.
src/​include/​checksum/​fletcher4.hpp Routes contiguous ranges efficiently.
src/​include/​checksum/​crc.hpp Expands folding constants.
src/​include/​checksum/​byte_range.hpp Centralizes little-endian loads.
src/​include/​checksum/​adler32.hpp Adds checksum continuation API.
src/​include_private/​checksum_private/​xxh3_arch.hpp Selects architecture directly.
src/​include_private/​checksum_private/​x86_64/​xxh3.hpp Adds no-SSE2 fallback.
src/​include_private/​checksum_private/​x86_64/​internet.hpp Updates measured threshold.
src/​include_private/​checksum_private/​x86_64/​fletcher4.hpp Lowers lane thresholds.
src/​include_private/​checksum_private/​x86_64/​fletcher.hpp Adds AVX-VNNI byte kernel.
src/​include_private/​checksum_private/​internet_arch.hpp Selects architecture directly.
src/​include_private/​checksum_private/​generic/​fletcher.hpp Adds group-kernel interface.
src/​include_private/​checksum_private/​fletcher4_arch.hpp Selects architecture directly.
src/​include_private/​checksum_private/​fletcher_arch.hpp Delegates shared loop logic.
src/​include_private/​checksum_private/​crc_arch.hpp Updates selection and wide folding.
src/​include_private/​checksum_private/​arm/​xxh3.hpp Optimizes AArch64 lane handling.
src/​include_private/​checksum_private/​arm/​internet.hpp Documents updated thresholds.
src/​include_private/​checksum_private/​arm/​fletcher4.hpp Lowers NEON threshold.
src/​include_private/​checksum_private/​arm/​fletcher.hpp Adds AArch64 group kernel.
src/​include_private/​checksum_private/​arch.hpp Removes central selector.
src/​fletcher4/​sum_loop.cpp Reworks lane combination and short path.
src/​CMakeLists.txt Exports hash128.hpp.
src/​adler32/​sum_loop.cpp Reuses shared Fletcher loops.
README.md Lists expanded examples.
examples/​xxhash/​file_hash.cpp Adds streamed file hashing.
examples/​xxhash/​compile_time.cpp Adds compile-time IDs.
examples/​xxhash/​CMakeLists.txt Builds xxHash examples.
examples/​xxhash/​cache_key.cpp Adds cache-key example.
examples/​xxh3/​hash_table.cpp Adds seeded hash table.
examples/​xxh3/​file_hash.cpp Adds streamed file hashing.
examples/​xxh3/​deduplicate.cpp Adds block deduplication.
examples/​xxh3/​compile_time.cpp Adds compile-time asset IDs.
examples/​xxh3/​CMakeLists.txt Builds XXH3 examples.
examples/​murmur3/​deduplicate.cpp Adds record deduplication.
examples/​murmur3/​compile_time.cpp Adds command dispatch.
examples/​murmur3/​CMakeLists.txt Builds MurmurHash3 examples.
examples/​murmur3/​bloom_filter.cpp Adds Bloom filter example.
examples/​internet/​udp_datagram.cpp Adds UDP checksum example.
examples/​internet/​ttl_decrement.cpp Adds incremental TTL update.
examples/​internet/​ipv4_header.cpp Adds IPv4 header example.
examples/​internet/​compile_time.cpp Adds compile-time header checksum.
examples/​internet/​CMakeLists.txt Builds Internet examples.
examples/​fletcher4/​zfs_block.cpp Adds ZFS block verification.
examples/​fletcher4/​file_checksum.cpp Adds streamed file checksum.
examples/​fletcher4/​compile_time.cpp Adds compile-time block checksum.
examples/​fletcher4/​CMakeLists.txt Builds fletcher4 examples.
examples/​fletcher/​frame_check.cpp Adds frame checksum example.
examples/​fletcher/​file_checksum.cpp Adds streamed Fletcher-32.
examples/​fletcher/​compile_time.cpp Adds compile-time checksums.
examples/​fletcher/​CMakeLists.txt Builds Fletcher examples.
examples/​CMakeLists.txt Enables all example suites.
examples/​adler32/​zlib_trailer.cpp Adds zlib trailer verification.
examples/​adler32/​resume.cpp Demonstrates checksum continuation.
examples/​adler32/​compile_time.cpp Adds compile-time checksum.
examples/​adler32/​CMakeLists.txt Builds Adler-32 examples.
doc/​murmur3.md Updates API and performance guidance.
doc/​fletcher4.md Updates thresholds and measurements.
doc/​fletcher.md Documents aliases and continuation.
doc/​design/​testing.md Updates testing guidance.
doc/​design/​principles.md Documents architecture selection.
doc/​crc.md Updates sizes, behavior, and performance.
doc/​adler32.md Documents continuation and kernels.
CMakePresets.json Adjusts sanitizer warnings.
cmake/​x86_64_presets.json Enables VPCLMULQDQ coverage.
cmake/​emulator_presets.json Expands Arm acceleration coverage.
cmake/​common_presets.json Adds debug optimization and compiler warnings.
.cspell.yml Adds new technical vocabulary.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@jmacheta
jmacheta merged commit 3a3f8cb into main Oct 4, 2026
17 checks passed
@jmacheta
jmacheta deleted the feature/api-review branch October 4, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants