_u64.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.toBig = exports.shrSL = exports.shrSH = exports.rotrSL = exports.rotrSH = exports.rotrBL = exports.rotrBH = exports.rotr32L = exports.rotr32H = exports.rotlSL = exports.rotlSH = exports.rotlBL = exports.rotlBH = exports.add5L = exports.add5H = exports.add4L = exports.add4H = exports.add3L = exports.add3H = void 0;
  4. exports.add = add;
  5. exports.fromBig = fromBig;
  6. exports.split = split;
  7. /**
  8. * Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
  9. * @todo re-check https://issues.chromium.org/issues/42212588
  10. * @module
  11. */
  12. const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
  13. const _32n = /* @__PURE__ */ BigInt(32);
  14. function fromBig(n, le = false) {
  15. if (le)
  16. return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
  17. return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
  18. }
  19. function split(lst, le = false) {
  20. const len = lst.length;
  21. let Ah = new Uint32Array(len);
  22. let Al = new Uint32Array(len);
  23. for (let i = 0; i < len; i++) {
  24. const { h, l } = fromBig(lst[i], le);
  25. [Ah[i], Al[i]] = [h, l];
  26. }
  27. return [Ah, Al];
  28. }
  29. const toBig = (h, l) => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
  30. exports.toBig = toBig;
  31. // for Shift in [0, 32)
  32. const shrSH = (h, _l, s) => h >>> s;
  33. exports.shrSH = shrSH;
  34. const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
  35. exports.shrSL = shrSL;
  36. // Right rotate for Shift in [1, 32)
  37. const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
  38. exports.rotrSH = rotrSH;
  39. const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
  40. exports.rotrSL = rotrSL;
  41. // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
  42. const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
  43. exports.rotrBH = rotrBH;
  44. const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
  45. exports.rotrBL = rotrBL;
  46. // Right rotate for shift===32 (just swaps l&h)
  47. const rotr32H = (_h, l) => l;
  48. exports.rotr32H = rotr32H;
  49. const rotr32L = (h, _l) => h;
  50. exports.rotr32L = rotr32L;
  51. // Left rotate for Shift in [1, 32)
  52. const rotlSH = (h, l, s) => (h << s) | (l >>> (32 - s));
  53. exports.rotlSH = rotlSH;
  54. const rotlSL = (h, l, s) => (l << s) | (h >>> (32 - s));
  55. exports.rotlSL = rotlSL;
  56. // Left rotate for Shift in (32, 64), NOTE: 32 is special case.
  57. const rotlBH = (h, l, s) => (l << (s - 32)) | (h >>> (64 - s));
  58. exports.rotlBH = rotlBH;
  59. const rotlBL = (h, l, s) => (h << (s - 32)) | (l >>> (64 - s));
  60. exports.rotlBL = rotlBL;
  61. // JS uses 32-bit signed integers for bitwise operations which means we cannot
  62. // simple take carry out of low bit sum by shift, we need to use division.
  63. function add(Ah, Al, Bh, Bl) {
  64. const l = (Al >>> 0) + (Bl >>> 0);
  65. return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
  66. }
  67. // Addition with more than 2 elements
  68. const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
  69. exports.add3L = add3L;
  70. const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
  71. exports.add3H = add3H;
  72. const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
  73. exports.add4L = add4L;
  74. const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
  75. exports.add4H = add4H;
  76. const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
  77. exports.add5L = add5L;
  78. const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
  79. exports.add5H = add5H;
  80. // prettier-ignore
  81. const u64 = {
  82. fromBig, split, toBig,
  83. shrSH, shrSL,
  84. rotrSH, rotrSL, rotrBH, rotrBL,
  85. rotr32H, rotr32L,
  86. rotlSH, rotlSL, rotlBH, rotlBL,
  87. add, add3L, add3H, add4L, add4H, add5H, add5L,
  88. };
  89. exports.default = u64;
  90. //# sourceMappingURL=_u64.js.map