-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move bigint utils to
src/optimizer/util.ts
(#1468)
- Loading branch information
Showing
4 changed files
with
28 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// precondition: the divisor is not zero | ||
// rounds the division result towards negative infinity | ||
export function divFloor(a: bigint, b: bigint): bigint { | ||
const almostSameSign = a > 0n === b > 0n; | ||
if (almostSameSign) { | ||
return a / b; | ||
} | ||
return a / b + (a % b === 0n ? 0n : -1n); | ||
} | ||
|
||
export function abs(a: bigint): bigint { | ||
return a < 0n ? -a : a; | ||
} | ||
|
||
export function sign(a: bigint): bigint { | ||
if (a === 0n) return 0n; | ||
else return a < 0n ? -1n : 1n; | ||
} | ||
|
||
// precondition: the divisor is not zero | ||
// rounds the result towards negative infinity | ||
// Uses the fact that a / b * b + a % b == a, for all b != 0. | ||
export function modFloor(a: bigint, b: bigint): bigint { | ||
return a - divFloor(a, b) * b; | ||
} |