Created
March 29, 2023 08:11
-
-
Save Yaffle/4db2e1cd3dd9c6f91172373d9b77a847 to your computer and use it in GitHub Desktop.
jsHelper64.js
This file contains hidden or 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
const uint64 = new BigUint64Array(1); | |
const uint32 = new Uint32Array(uint64.buffer); | |
function clz64(bigint) { | |
uint64[0] = bigint; | |
let x = Math.clz32(uint32[1]); | |
let y = Math.clz32(uint32[0]); | |
return x + (x === 32 ? y : 0); | |
} | |
function jsHelper64($x, $xlo, $y, $ylo) { | |
let x = BigInt.asUintN(64, $x); | |
let xlo = BigInt.asUintN(64, $xlo); | |
let y = BigInt.asUintN(64, $y); | |
let ylo = BigInt.asUintN(64, $ylo); | |
let A = BigInt.asIntN(64, 1n); | |
let B = BigInt.asIntN(64, 0n); | |
let C = BigInt.asIntN(64, 0n); | |
let D = BigInt.asIntN(64, 1n); | |
let i = 7; | |
let q = BigInt.asUintN(64, 1n); | |
let y1 = BigInt.asUintN(64, 1n); | |
let A1 = BigInt.asIntN(64, 1n); | |
let B1 = BigInt.asIntN(64, 0n); | |
let C1 = BigInt.asIntN(64, 0n); | |
let D1 = BigInt.asIntN(64, 1n); | |
let sameQuotient = 0; | |
let bits = BigInt.asUintN(64, 0n); | |
let xlo1 = BigInt.asIntN(64, 0n); | |
let ylo1 = BigInt.asIntN(64, 0n); | |
let lobits = BigInt.asUintN(64, 64n); | |
const zero = BigInt.asIntN(64, 0n); | |
do { | |
A1 = A; | |
B1 = B; | |
C1 = C; | |
D1 = D; | |
y1 = y; | |
y = x; | |
do { | |
x = y; | |
y = y1; | |
A = A1; | |
B = B1; | |
C = C1; | |
D = D1; | |
q = BigInt.asUintN(64, x / y); | |
y1 = BigInt.asUintN(64, x - BigInt.asUintN(64, q * y)); | |
A1 = C; | |
B1 = D; | |
C1 = BigInt.asIntN(64, A - BigInt.asIntN(64, q * C)); | |
D1 = BigInt.asIntN(64, B - BigInt.asIntN(64, q * D)); | |
sameQuotient = (clz64(BigInt.asIntN(64, -D)) + clz64(D) + clz64(q) > 64) & | |
( | |
C1 < zero & BigInt.asUintN(64, -C1) < y1 & BigInt.asUintN(64, D1 - D) <= BigInt.asUintN(64, y - y1) | |
| | |
D1 < zero & BigInt.asUintN(64, -D1) < y1 & BigInt.asUintN(64, C1 - C) <= BigInt.asUintN(64, y - y1) | |
) | |
} while (sameQuotient !== 0); | |
bits = x < 0n ? 0n : BigInt(clz64(BigInt.asUintN(64, x + (B < A ? A : B)))); | |
if (bits !== 0n) { | |
bits = lobits < bits ? lobits : bits; | |
lobits = BigInt.asUintN(64, lobits - bits); | |
xlo1 = BigInt.asUintN(64, xlo >> lobits); | |
ylo1 = BigInt.asUintN(64, ylo >> lobits); | |
xlo = BigInt.asUintN(64, xlo - BigInt.asUintN(64, xlo1 << lobits)); | |
ylo = BigInt.asUintN(64, ylo - BigInt.asUintN(64, ylo1 << lobits)); | |
x = BigInt.asUintN(64, BigInt.asUintN(64, BigInt.asUintN(64, A * xlo1) + BigInt.asUintN(64, B * ylo1)) + BigInt.asUintN(64, x << bits)); | |
y = BigInt.asUintN(64, BigInt.asUintN(64, BigInt.asUintN(64, C * xlo1) + BigInt.asUintN(64, D * ylo1)) + BigInt.asUintN(64, y << bits)); | |
i -= 1; | |
} | |
} while (i !== 0 && bits !== 0n); | |
return [A, B, C, D]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment