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.asUintN(1, "foo");
}).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo");
});
test("large allocation", () => {
expect(() => {
BigInt.asUintN(0x4000000000000, 1n);
}).toThrowWithMessage(InternalError, "Out of memory");
});
});
describe("correct behavior", () => {
@ -80,4 +74,20 @@ describe("correct behavior", () => {
115792089237316195423570861551898784396480861208851440582668460551124006183147n
);
});
test("large bit values", () => {
const INDEX_MAX = 2 ** 53 - 1;
const LAST_8_DIGITS = 10n ** 8n;
expect(BigInt.asUintN(0x400000000, 1n)).toBe(1n);
expect(BigInt.asUintN(0x4000, -1n) % LAST_8_DIGITS).toBe(64066815n);
expect(BigInt.asUintN(0x400000000, 2n)).toBe(2n);
expect(BigInt.asUintN(0x4000, -2n) % LAST_8_DIGITS).toBe(64066814n);
expect(BigInt.asUintN(INDEX_MAX, 2n)).toBe(2n);
expect(() => {
BigInt.asUintN(INDEX_MAX, -2n);
}).toThrowWithMessage(InternalError, "Out of memory");
});
});