Skip to content

Commit

Permalink
fix for #44 (#45)
Browse files Browse the repository at this point in the history
* fix for #44

* Update tests.mjs
  • Loading branch information
Yaffle committed Jun 4, 2020
1 parent a6e69e0 commit 0400946
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 11 additions & 6 deletions jsbi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,9 +1194,11 @@ class JSBI extends Array {
let carry = true;
for (let i = 0; i < inputLength; i++) {
let digit = x.__digit(i);
const newCarry = digit === (0xFFFFFFFF | 0);
if (carry) digit = (digit + 1) | 0;
carry = newCarry;
if (carry) {
const newCarry = digit === (0xFFFFFFFF | 0);
digit = (digit + 1) | 0;
carry = newCarry;
}
result.__setDigit(i, digit);
}
if (carry) {
Expand All @@ -1212,11 +1214,14 @@ class JSBI extends Array {
let borrow = true;
for (let i = 0; i < length; i++) {
let digit = x.__digit(i);
const newBorrow = digit === 0;
if (borrow) digit = (digit - 1) | 0;
borrow = newBorrow;
if (borrow) {
const newBorrow = digit === 0;
digit = (digit - 1) | 0;
borrow = newBorrow;
}
result.__setDigit(i, digit);
}
if (borrow) throw new Error('implementation bug');
for (let i = length; i < resultLength; i++) {
result.__setDigit(i, 0);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ const TESTS = [
b: '0x100000001',
expected: '0x1',
},
{ // https://github.com/GoogleChromeLabs/jsbi/issues/44#issue-630518844
operation: 'bitwiseAnd',
a: '0b10000010001000100010001000100010001000100010001000100010001000100',
b: '-0b10000000000000000000000000000000000000000000000000000000000000001',
expected: '0b10001000100010001000100010001000100010001000100010001000100',
},
{ // https://github.com/GoogleChromeLabs/jsbi/issues/44#issue-630518844
operation: 'bitwiseXor',
a: '0',
b: '-0b1111111111111111111111111111111111111111111111111111111111111111',
expected: '-0b1111111111111111111111111111111111111111111111111111111111111111',
},
];

// https://github.com/GoogleChromeLabs/jsbi/issues/36
Expand Down

0 comments on commit 0400946

Please sign in to comment.