From 82448509100543e6e009db0155994cfd5f97166e Mon Sep 17 00:00:00 2001 From: Nico Abril Date: Sun, 15 Nov 2026 22:47:19 +0100 Subject: [PATCH] perf(c): Nat.mul divides only when an operand reaches 2^32 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 --- bend2/comp.ts | 3 +- tests/run/nat_mul_wide.bend | 60 ++++++++++++++++++++++++++++++++ tests/run/nat_overflow_wide.bend | 19 ++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/run/nat_mul_wide.bend create mode 100644 tests/run/nat_overflow_wide.bend diff --git a/bend2/comp.ts b/bend2/comp.ts index be82a5d8b..2c707cd89 100644 --- a/bend2/comp.ts +++ b/bend2/comp.ts @@ -3674,7 +3674,8 @@ INLINE u64 nat_chk(Env e, u64 n) { } INLINE u64 nat_mul(Env e, u64 a, u64 b) { - return nat_chk(e, b != 0 && a > NAT_IMM / b ? NAT_IMM + 1 : a * b); + return nat_chk(e, (a | b) >> 32 != 0 && b != 0 && a > NAT_IMM / b + ? NAT_IMM + 1 : a * b); } INLINE u64 map_msb_u(u64 n, u32 x) { diff --git a/tests/run/nat_mul_wide.bend b/tests/run/nat_mul_wide.bend new file mode 100644 index 000000000..0d0092770 --- /dev/null +++ b/tests/run/nat_mul_wide.bend @@ -0,0 +1,60 @@ +# Nat.mul's overflow test divides only when an operand reaches 2^32: under +# that the 64-bit product is exact, and nat_chk alone holds it to 2^48-1. +# Every operand is scaled by the command line's length (1 in the harness), +# which no compiler sees, so no lane folds a row. The rows sit on both +# sides of 2^32 in either operand, and at the largest Nat as a product: +# (2^24-1) * (2^24+1) = 2^48-1 with both operands small, (2^32+2^16+1) * +# 65535 = 2^48-1 with one operand wide, 2^32 * 65535 and 2^40 * 255. +# nat_overflow and nat_overflow_wide are the products past the wall. +import Base + +def big() -> Nat: + Nat.add(Nat.mul(16777215n, 16777216n), 16777215n) + +def row(a: Nat, b: Nat) -> String: + Nat.show(Nat.mul(a, b)) + +def rows(+one: Nat) -> String: + String.join([ + row(Nat.mul(0n, one), big()), + row(big(), Nat.mul(0n, one)), + row(one, big()), + row(big(), one), + row(Nat.mul(16777215n, one), Nat.mul(16777217n, one)), + row(Nat.mul(16777217n, one), Nat.mul(16777215n, one)), + row(Nat.mul(4294967295n, one), Nat.mul(65535n, one)), + row(Nat.mul(65535n, one), Nat.mul(4294967295n, one)), + row(Nat.add(4294967295n, one), Nat.mul(65535n, one)), + row(Nat.mul(65535n, one), Nat.add(4294967295n, one)), + row(Nat.add(4294967295n, Nat.mul(2n, one)), Nat.mul(65535n, one)), + row(Nat.mul(1048576n, Nat.mul(1048576n, one)), Nat.mul(255n, one)), + row(Nat.mul(255n, one), Nat.mul(1048576n, Nat.mul(1048576n, one))), + row(Nat.mul(16777216n, one), Nat.mul(8388608n, one)), + row(Nat.add(4294967295n, Nat.mul(65538n, one)), Nat.mul(65535n, one)), + row(Nat.mul(65535n, one), Nat.add(4294967295n, Nat.mul(65538n, one))), + row(Nat.mul(4294967295n, one), Nat.mul(0n, one)), + row(Nat.add(4294967295n, one), Nat.mul(0n, one))], "\n") + +def main() -> IO(Unit): + do IO: + xs : List <- IO.args() + IO.print(rows(List.length(&1, String, xs))) + +#|0 +#|0 +#|281474976710655 +#|281474976710655 +#|281474976710655 +#|281474976710655 +#|281470681677825 +#|281470681677825 +#|281470681743360 +#|281470681743360 +#|281470681808895 +#|280375465082880 +#|280375465082880 +#|140737488355328 +#|281474976710655 +#|281474976710655 +#|0 +#|0 diff --git a/tests/run/nat_overflow_wide.bend b/tests/run/nat_overflow_wide.bend new file mode 100644 index 000000000..96a7df8c7 --- /dev/null +++ b/tests/run/nat_overflow_wide.bend @@ -0,0 +1,19 @@ +# Nat overflow where the 64-bit product wraps to a small number: 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 sees it. The second operand is past +# 2^32 and the first is not, so this is Nat.mul's division path, taken for +# an operand on either side and from 2^32 on; nat_overflow's 2^24 * 2^24 is +# the path without a division. The operands are scaled by the command +# line's length (1 in the harness), which no compiler sees. +import Base + +def wide(+one: Nat) -> Nat: + Nat.mul(Nat.mul(3221225472n, one), Nat.add(4294967295n, Nat.mul(1431655767n, one))) + +def main() -> IO(Unit): + do IO: + xs : List <- IO.args() + IO.print(Nat.show(wide(List.length(&1, String, xs)))) + +#|bend: a Nat past the largest immediate 2^48-1 +#|exit 1