Repository navigation
API review: aliases, shared code, docs and examples for every algorithm - #2
Merged
Merged
Conversation
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/.
…, zlib trailer, resume
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.
…e hash per Bloom key)
…includes what it uses
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.
…nd EOR3 in the test presets
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.
…ut decisions, reference comparison
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.
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Review of every algorithm except the CRC: API, implementation, structure, docs and examples. No hash or checksum value changes.
API
fletcher16/32/64,xxh32,xxh64,xxh3_64,xxh3_128,murmur3_32,murmur3_128, each a<name>_statealias and a<name>_compute(data[, seed])function._updateand_finalizetake the width from the state.fletcher4_computeon astd::string_vieworstd::vectornow 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 (wasxxh3_hash128) and MurmurHash3_x64_128 (wasstd::array<std::uint64_t, 2>;low= h1,high= h2). Generated code is identical on x86-64 and Cortex-M4.adler32_update(std::uint32_t, data),internet_update(std::uint16_t, data)andfletcher_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.Implementation
checksum_private/fletcher_loops.hpp, likecrc_table_loops.hpp. The twosum_loop.cppfiles shrink from 300 lines to about 60.detail::loadinbyte_range.hppreplaces the copies in xxHash and MurmurHash3. XXH3 reuses the XXH64 avalanche.accumulate/scrambleonly as kernel members, one AVX2 block in the fletcher4 header, the MurmurHash3 2-byte tail read byload.checksum_private/arch.hppis gone: each<algorithm>_arch.hppselects its architecture directory.tests/include/test_data.hpp, replaces 15 copies in tests, reference vectors and benchmarks.-Wduplicated-condand-Wlogical-opare passed to GCC only, so Clang no longer warns about unknown options.Performance (nRF52840, Cortex-M4, MB/s at 64 MHz, before → after)
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)
-O2GCC 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-O3and are re-measured at-O2.fletcher4_arch.hppincludes what it uses, NDEBUG note in the CRC guide, one hash per key in the Bloom filter example.-Og(cross-arm-portablekeeps-O0for the inline assembly), fewer Fletcher/Adler-32 offsets and smaller all-ones messages;x86_64-gcc-pclmulnow builds the VPCLMULQDQ CRC kernel andcross-aarch64-pmullthe 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):-march=armv8-a)crc32instructions. Folding constants grow from 80 to 160 B per table.adcchains on x86-64, one masked load of the last word, a two-step fold. Code size grows (Cortex-M4-Os202 → 284 B).__AVXVNNI__; tested locally only, QEMU has no AVX-VNNI), an AArch64 NEON kernel over 64-byte groups, short inputs without store-forwarding stalls.Docs and examples
CHECKSUM_EXAMPLESrow.ut_internet_checksum.cppis renamed tout_internet.cpp.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
*_update(state, Range&&)helper: each of those overloads is already two lines.