Skip to content

Commit 5c1e327

Browse files
committed
[js] Use naive directly from the spec asIntN and asUIntN
That avoid using BigInt and will be replaced once efficent versions are in jsbi
1 parent 82930f5 commit 5c1e327

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"eslint-config-google": "^0.8.0"
1313
},
1414
"dependencies": {
15+
"jsbi": "^2.0.2",
1516
"nqp-runtime": "file:src/vm/js/nqp-runtime"
1617
}
1718
}

src/vm/js/nqp-runtime/as-int-n.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const JSBI = require('jsbi');
22

33
module.exports.asIntN = function(bits, bigint) {
4-
return JSBI.BigInt(BigInt.asIntN(bits, BigInt(bigint.toString())).toString());
4+
const power = JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(bits));
5+
const mod = JSBI.remainder(bigint, power);
6+
return (JSBI.greaterThanOrEqual(mod, JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(bits-1))) ? JSBI.subtract(mod, power) : mod);
57
};
68

79
module.exports.asUintN = function(bits, bigint) {
8-
return JSBI.BigInt(BigInt.asUintN(bits, BigInt(bigint.toString())).toString());
10+
return JSBI.remainder(bigint, JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(bits)));
911
};
1012

0 commit comments

Comments
 (0)