Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bend2/comp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
60 changes: 60 additions & 0 deletions tests/run/nat_mul_wide.bend
Original file line number Diff line number Diff line change
@@ -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<Unit>:
xs : List<String> <- 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
19 changes: 19 additions & 0 deletions tests/run/nat_overflow_wide.bend
Original file line number Diff line number Diff line change
@@ -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<Unit>:
xs : List<String> <- IO.args()
IO.print(Nat.show(wide(List.length(&1, String, xs))))

#|bend: a Nat past the largest immediate 2^48-1
#|exit 1