Repository navigation
Nat.mul divides to test for overflow only when an operand reaches 2^32 - #2174
Merged
Merged
Conversation
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>
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.
comp.tsis 65,980 ttok, under the 66,000 cap (main is 65,966), andgates/repo.tspasses. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run. Independent of #2173 and of every other open draft.What it does
nat_multested every product for overflow with a 64-bit division:clang folds the division only when an operand is a constant, so
Nat.mulof two runtime values cost audivon top of themul. When both operands are under 2^32 the 64-bit product cannot wrap, andnat_chkalone holds it to 2^48-1. The division is now taken only when an operand reaches 2^32:The answer is the same for every pair of 64-bit operands: under 2^32 the old test
a > NAT_IMM / bandnat_chkof 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/runtimemultiplies noNatat run time, so the programs are the 22 kernels ofkorulang/koru-benchmarks(suites/osprey-compute-kernels/bend, small integer loops overNat), plus three of them respelled.primes(d * d > neach step)primeswith #2173 tooprimes, the two tests in the reference's orderpowmod, product in two 15-bit limbscollatz(3 * n)nestedloop(i * j * k)The other 19 kernels are within 0.1% in instructions: they multiply by a constant or not at all. With #2173,
primesgoes from 1.52x of its C reference (cc -O2, start-up taken out of both) to 0.96x.nestedloopgets slower, and it is clang's loop strength reduction, not the test. Its inner loop isacc = (acc + i * j * k) % Mover a countdown ofk. On main the product sits behind the division's select, and the loop-carried chain isadd,umulh,lsr,msub. With the division gone, LSR rewritesi * j * kas two induction variables and reassociates the sum around the remainder, so the chain gains anadd(9.2 to 10.3 cycles a turn) and the loop five instructions. The same C built with-mllvm -disable-lsrruns 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) keepsnestedlooplevel (0.99x), but costs instructions on the other programs (+38% onprimes, +14% on a respelledfactorial) for a smaller gain onprimes(0.74x). A 128-bit product and__builtin_mul_overflowwere slower than this on the limb-multiplypowmod(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,collatzandnestedloopwere built at 16 positions (nops at the head ofwork_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/runtimeprograms differ in the runtime's text only: their binaries disassemble identically to main's.Checks
All local, against main
f0cf0a7c:#|lines): 883 pass on main and here, no status differs.-DBEND_POISONand a count of live heap words at exit: the same output, exit code and count as main on every one.nat_mulis the C runtime's); the JS for seven programs is byte-identical.Nat.div,Nat.modandNat.mulon runtime operands inside a!fork tree prints the value computed apart in Python with--gpu onand--gpu off, on main and here. Its operands are under 2^32; the division path on the device is main's code.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.bend file.bend, on the C lane and on the JS lane, on main and here.nat_mul(no test before the multiply, the bound inverted, the bound at 2^33, onlyatested, nonat_chkon the product) all failnat_overflow_wide;nat_overflow(2^24 * 2^24, the path without a division) fails the last.gates/repo.tspasses.Not covered
divcosts more than on this M4, so the gain there should be larger; not measured.tscwas not run.Cost
+14 ttok.
🤖 Generated with Claude Code