Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stdlib): Correctly promote numbers to bigints when left-shifting #1354

Merged
merged 4 commits into from
Jul 21, 2022
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
5 changes: 5 additions & 0 deletions compiler/test/suites/numbers.re
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ describe("numbers", ({test, testSkip}) => {
assertRun("number_syntax8", "print(1.2e2)", "120.0\n");
assertRun("number_syntax9", "print(1l)", "1\n");
assertRun("number_syntax10", "print(1L)", "1\n");
assertRun(
"number_shift_promote",
"print(5 << 64)",
"92233720368547758080\n",
);
assertRun(
"number_syntax11",
"print(987654321987654321987654321)",
Expand Down
10 changes: 9 additions & 1 deletion stdlib/runtime/numbers.gr
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,15 @@ export let (<<) = (x: Number, y: Number) => {
} else {
let xval = coerceNumberToWasmI64(x)
let yval = coerceNumberToWasmI64(y)
WasmI32.toGrain(reducedInteger(WasmI64.shl(xval, yval))): Number
// if the number will be shifted beyond the end of the i64 range, promote to BigInt
// (note that we subtract one leading zero, since the leading bit is the sign bit)
if (WasmI64.leU(WasmI64.sub(WasmI64.clz(i64abs(xval)), 1N), yval)) {
let xbi = coerceNumberToBigInt(x)
let yval = coerceNumberToWasmI32(y)
WasmI32.toGrain(reducedBigInteger(BI.shl(xbi, yval))): Number
} else {
WasmI32.toGrain(reducedInteger(WasmI64.shl(xval, yval))): Number
}
}
}

Expand Down