_u64.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
  2. const _32n = /* @__PURE__ */ BigInt(32);
  3. // We are not using BigUint64Array, because they are extremely slow as per 2022
  4. function fromBig(n, le = false) {
  5. if (le)
  6. return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
  7. return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
  8. }
  9. function split(lst, le = false) {
  10. let Ah = new Uint32Array(lst.length);
  11. let Al = new Uint32Array(lst.length);
  12. for (let i = 0; i < lst.length; i++) {
  13. const { h, l } = fromBig(lst[i], le);
  14. [Ah[i], Al[i]] = [h, l];
  15. }
  16. return [Ah, Al];
  17. }
  18. const toBig = (h, l) => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
  19. // for Shift in [0, 32)
  20. const shrSH = (h, _l, s) => h >>> s;
  21. const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
  22. // Right rotate for Shift in [1, 32)
  23. const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
  24. const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
  25. // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
  26. const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
  27. const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
  28. // Right rotate for shift===32 (just swaps l&h)
  29. const rotr32H = (_h, l) => l;
  30. const rotr32L = (h, _l) => h;
  31. // Left rotate for Shift in [1, 32)
  32. const rotlSH = (h, l, s) => (h << s) | (l >>> (32 - s));
  33. const rotlSL = (h, l, s) => (l << s) | (h >>> (32 - s));
  34. // Left rotate for Shift in (32, 64), NOTE: 32 is special case.
  35. const rotlBH = (h, l, s) => (l << (s - 32)) | (h >>> (64 - s));
  36. const rotlBL = (h, l, s) => (h << (s - 32)) | (l >>> (64 - s));
  37. // JS uses 32-bit signed integers for bitwise operations which means we cannot
  38. // simple take carry out of low bit sum by shift, we need to use division.
  39. function add(Ah, Al, Bh, Bl) {
  40. const l = (Al >>> 0) + (Bl >>> 0);
  41. return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
  42. }
  43. // Addition with more than 2 elements
  44. const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
  45. const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
  46. const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
  47. const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
  48. const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
  49. const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
  50. // prettier-ignore
  51. export { fromBig, split, toBig, shrSH, shrSL, rotrSH, rotrSL, rotrBH, rotrBL, rotr32H, rotr32L, rotlSH, rotlSL, rotlBH, rotlBL, add, add3L, add3H, add4L, add4H, add5H, add5L, };
  52. // prettier-ignore
  53. const u64 = {
  54. fromBig, split, toBig,
  55. shrSH, shrSL,
  56. rotrSH, rotrSL, rotrBH, rotrBL,
  57. rotr32H, rotr32L,
  58. rotlSH, rotlSL, rotlBH, rotlBL,
  59. add, add3L, add3H, add4L, add4H, add5H, add5L,
  60. };
  61. export default u64;
  62. //# sourceMappingURL=_u64.js.map