LibJS+LibCrypto: Use a bitwise approach for BigInt's as*IntN methods

This speeds up expressions such as `BigInt.asIntN(0x4000000000000, 1n)`
(#3615). And those involving very large bigints.
This commit is contained in:
Jess 2025-03-19 10:31:39 +13:00 committed by Jelle Raaijmakers
commit 12cbefbee7
Notes: github-actions[bot] 2025-03-20 08:45:14 +00:00
9 changed files with 110 additions and 33 deletions

View file

@ -22,12 +22,6 @@ describe("errors", () => {
BigInt.asIntN(1, "foo");
}).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo");
});
test("large allocation", () => {
expect(() => {
BigInt.asIntN(0x4000000000000, 1n);
}).toThrowWithMessage(InternalError, "Out of memory");
});
});
describe("correct behavior", () => {
@ -82,4 +76,23 @@ describe("correct behavior", () => {
expect(BigInt.asIntN(128, -extremelyBigInt)).toBe(99061374399389259395070030194384019691n);
expect(BigInt.asIntN(256, -extremelyBigInt)).toBe(-extremelyBigInt);
});
test("large bit values", () => {
expect(BigInt.asIntN(0x4000000000000, 1n)).toBe(1n);
expect(BigInt.asIntN(0x8ffffffffffff, 1n)).toBe(1n);
expect(BigInt.asIntN(2 ** 53 - 1, 2n)).toBe(2n);
// These incur large intermediate values that 00M. For now, ensure they don't crash
expect(() => {
BigInt.asIntN(0x4000000000000, -1n);
}).toThrowWithMessage(InternalError, "Out of memory");
expect(() => {
BigInt.asIntN(0x8ffffffffffff, -1n);
}).toThrowWithMessage(InternalError, "Out of memory");
expect(() => {
BigInt.asIntN(2 ** 53 - 1, -2n);
}).toThrowWithMessage(InternalError, "Out of memory");
});
});