// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
export function swap32(val) {
  return (val & 0xff) << 24 | (val & 0xff00) << 8 | val >> 8 & 0xff00 | val >> 24 & 0xff;
}
function n16(n) {
  return n & 0xffff;
}
function n32(n) {
  return n >>> 0;
}
function add32WithCarry(a, b) {
  const added = n32(a) + n32(b);
  return [
    n32(added),
    added > 0xffffffff ? 1 : 0
  ];
}
function mul32WithCarry(a, b) {
  const al = n16(a);
  const ah = n16(a >>> 16);
  const bl = n16(b);
  const bh = n16(b >>> 16);
  const [t, tc] = add32WithCarry(al * bh, ah * bl);
  const [n, nc] = add32WithCarry(al * bl, n32(t << 16));
  const carry = nc + (tc << 16) + n16(t >>> 16) + ah * bh;
  return [
    n,
    carry
  ];
}
/**
 * mul32 performs 32-bit multiplication, a * b
 * @param a
 * @param b
 */ export function mul32(a, b) {
  // https://stackoverflow.com/a/28151933
  const al = n16(a);
  const ah = a - al;
  return n32(n32(ah * b) + al * b);
}
/**
 * mul64 performs 64-bit multiplication with two 32-bit words
 * @param [ah, al]
 * @param [bh, bl]
 */ export function mul64([ah, al], [bh, bl]) {
  const [n, c] = mul32WithCarry(al, bl);
  return [
    n32(mul32(al, bh) + mul32(ah, bl) + c),
    n
  ];
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImh0dHBzOi8vZGVuby5sYW5kL3N0ZEAwLjIxNy4wL2NyeXB0by9fZm52L3V0aWwudHMiXSwic291cmNlc0NvbnRlbnQiOlsiLy8gQ29weXJpZ2h0IDIwMTgtMjAyNCB0aGUgRGVubyBhdXRob3JzLiBBbGwgcmlnaHRzIHJlc2VydmVkLiBNSVQgbGljZW5zZS5cbi8vIFRoaXMgbW9kdWxlIGlzIGJyb3dzZXIgY29tcGF0aWJsZS5cblxuZXhwb3J0IGZ1bmN0aW9uIHN3YXAzMih2YWw6IG51bWJlcik6IG51bWJlciB7XG4gIHJldHVybiAoXG4gICAgKCh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