Skip to content

Nat.mul divides to test for overflow only when an operand reaches 2^32 - #2174

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:opt/nat-mul
Nov 18, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:opt/nat-mul

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

comp.ts is 65,980 ttok, under the 66,000 cap (main is 65,966), and gates/repo.ts passes. Measured on one Mac (M4), not on the cluster: the test and perf gates have not been run. Independent of #2173 and of every other open draft.

What it does

nat_mul tested every product for overflow with a 64-bit division:

return nat_chk(e, b != 0 && a > NAT_IMM / b ? NAT_IMM + 1 : a * b);

clang folds the division only when an operand is a constant, so Nat.mul of two runtime values cost a udiv on top of the mul. When both operands are under 2^32 the 64-bit product cannot wrap, and nat_chk alone holds it to 2^48-1. The division is now taken only when an operand reaches 2^32:

return nat_chk(e, (a | b) >> 32 != 0 && b != 0 && a > NAT_IMM / b
  ? NAT_IMM + 1 : a * b);

The answer is the same for every pair of 64-bit operands: under 2^32 the old test a > NAT_IMM / b and nat_chk of the exact product agree, and from 2^32 on the old test still runs. One source for the host and the devices, and no builtin.

Results

One M4, one thread, GPU off, instructions retired and cycles from /usr/bin/time -l, the least of 7 to 9 interleaved runs. bench/runtime multiplies no Nat at run time, so the programs are the 22 kernels of korulang/koru-benchmarks (suites/osprey-compute-kernels/bend, small integer loops over Nat), plus three of them respelled.

program instructions, main this cycles, main this cycles over 16 placements, this / main
primes (d * d > n each step) 112.9M 120.2M +6.5% 34.1M 26.1M -23.5% 0.749 to 0.767
primes with #2173 too 112.9M 84.5M -25.1% 34.1M 23.0M -32.6% not swept
primes, the two tests in the reference's order 165.4M 136.5M -17.5% 36.4M 29.0M -20.4% not swept
powmod, product in two 15-bit limbs 315.4M 227.2M -28.0% 247.0M 242.4M -1.8% not swept
collatz (3 * n) 289.6M 278.9M -3.7% 97.0M 95.2M -1.8% 0.938 to 1.027
nestedloop (i * j * k) 231.5M 310.6M +34.1% 147.4M 164.3M +11.5% 1.113 to 1.115

The other 19 kernels are within 0.1% in instructions: they multiply by a constant or not at all. With #2173, primes goes from 1.52x of its C reference (cc -O2, start-up taken out of both) to 0.96x.

nestedloop gets slower, and it is clang's loop strength reduction, not the test. Its inner loop is acc = (acc + i * j * k) % M over a countdown of k. On main the product sits behind the division's select, and the loop-carried chain is add, umulh, lsr, msub. With the division gone, LSR rewrites i * j * k as two induction variables and reassociates the sum around the remainder, so the chain gains an add (9.2 to 10.3 cycles a turn) and the loop five instructions. The same C built with -mllvm -disable-lsr runs 231.4M instructions in 145.9M cycles, 1% under main. It still runs under its C reference (176.6M cycles). I did not find a spelling of the test that keeps LSR away for a reason other than luck: merging the overflow flag into the product with an OR (a * b | flag << 48) keeps nestedloop level (0.99x), but costs instructions on the other programs (+38% on primes, +14% on a respelled factorial) for a smaller gain on primes (0.74x). A 128-bit product and __builtin_mul_overflow were slower than this on the limb-multiply powmod (1.38x and 1.09x its cycles) and are host-only. Those three were measured on top of #2173.

Placement. Cycles on this M4 can move with where code lands, so primes, collatz and nestedloop were built at 16 positions (nops at the head of work_loop, 4 bytes a step) with both compilers, 3 interleaved rounds, the minimum per position; the last column is the range of the per-position ratio. collatz's range overlaps main's, so it is no result.

The bench suite. The 17 bench/runtime programs differ in the runtime's text only: their binaries disassemble identically to main's.

Checks

All local, against main f0cf0a7c:

  • The two formulas compared apart in C over 200 million operand pairs: every pair of the values 2^k - 1, 2^k and 2^k + 1 for k up to 63 with 0, 2^64-1 and 2^48-1, and 200 million random pairs of every bit width (138 million of them overflow). No difference.
  • Native lane (every test built and run with the GPU off, compared with its #| lines): 883 pass on main and here, no status differs.
  • Memory: all 904 tests that pass or print to stderr (the runtime's text is in every program, so every C file differs) were built with both compilers with -DBEND_POISON and a count of live heap words at exit: the same output, exit code and count as main on every one.
  • JS lane: untouched (nat_mul is the C runtime's); the JS for seven programs is byte-identical.
  • Metal: a scratch program that calls Nat.div, Nat.mod and Nat.mul on runtime operands inside a ! fork tree prints the value computed apart in Python with --gpu on and --gpu off, on main and here. Its operands are under 2^32; the division path on the device is main's code.
  • New tests:
    • tests/run/nat_mul_wide.bend: products on both sides of 2^32 in either operand and at 2^48-1 exactly, by both paths ((2^24-1) * (2^24+1) and (2^32+2^16+1) * 65535).
    • tests/run/nat_overflow_wide.bend: 3 * 2^30 times (2^34+2)/3 is 2^64 + 2^31, which a machine word holds as 2^31, so only the test before the multiply stops it; the first operand is under 2^32 and the second between 2^32 and 2^33.
    • Every operand is scaled by the command line's length, so no lane folds a product. Both print their lines under bend file.bend, on the C lane and on the JS lane, on main and here.
    • Five deliberately wrong versions of nat_mul (no test before the multiply, the bound inverted, the bound at 2^33, only a tested, no nat_chk on the product) all fail nat_overflow_wide; nat_overflow (2^24 * 2^24, the path without a division) fails the last.
  • gates/repo.ts passes.

Not covered

  • Cluster gates, CUDA, Linux and x86 have not run. On x86 a 64-bit div costs more than on this M4, so the gain there should be larger; not measured. tsc was not run.
  • An operand of 2^32 or more still divides.

Cost

+14 ttok.

🤖 Generated with Claude Code

nat_mul tested for overflow with a 64-bit division on every call
(a > NAT_IMM / b). When both operands are under 2^32 the 64-bit product is
exact, so nat_chk alone holds it to 2^48-1; the division is kept for an
operand of 2^32 or more, where the product can wrap.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

1 participant