From a72ee0f2f1fca3dedbafcc97efde98f8d4bcc81d Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Thu, 10 Sep 2026 10:21:59 -0500 Subject: [PATCH] Default to the scalar check digit implementation -Dsimd defaulted to true on the assumption that vectorizing a weighted sum would beat the loop. `zig build bench` says otherwise: on this machine the ISBN-10 checksum measures 2.5 ns/op vectorized against 2.1 scalar, and the ISBN-13 one is level within noise at about 2.4. Nine or twelve lanes of multiply-and-add is not enough work to earn back widening the digits to u16, padding to sixteen lanes, and assembling the vector -- and the scalar loop is nine iterations that a compiler already unrolls. So the default is now false, and the faster path is the one a consumer gets without knowing the option exists. The option itself stays: it is how the two implementations are compared, and a target where the answer is different can turn it back on. Nothing about the SIMD code changes, and both settings keep their test run. The benchmark is unaffected either way, since it calls computeCheckDigitWith directly rather than following the build option. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01NJz47vhFE5XcSQUDbfP1N3 --- README.md | 5 ++++- build.zig | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index efb843f..ff11898 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,10 @@ std.debug.print("{f}\n", .{ten}); // 0-306-40615-2 ## Build options - `-Dsimd=[bool]` — use SIMD vector operations for computing check - digits (default: `true`). + digits (default: `false`). It is off because it does not pay: `zig + build bench` measures both implementations, and nine or twelve lanes + of multiply-and-add do not earn back the cost of widening the digits + and assembling the vector. ## Standards diff --git a/build.zig b/build.zig index a837ece..8245245 100644 --- a/build.zig +++ b/build.zig @@ -7,7 +7,13 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const simd = b.option(bool, "simd", "Use SIMD for computing check digits.") orelse true; + // Off by default, because it does not pay. `zig build bench` measures + // both: the ISBN-13 checksum comes out about level and the ISBN-10 one + // comes out slower, since nine lanes of multiply-and-add do not earn + // back the cost of widening the digits and assembling the vector. The + // option stays so the two implementations can be compared, and so + // anyone whose target says otherwise can turn it on. + const simd = b.option(bool, "simd", "Use SIMD for computing check digits.") orelse false; const options = b.addOptions(); options.addOption(bool, "simd", simd); -- 2.51.2