Java Code Examples for com.google.common.primitives.UnsignedLongs#remainder()

The following examples show how to use com.google.common.primitives.UnsignedLongs#remainder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m), m);
}
 
Example 2
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m), m);
}
 
Example 3
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m), m);
}
 
Example 4
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m), m);
}
 
Example 5
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m), m);
}
 
Example 6
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m), m);
}
 
Example 7
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m), m);
}
 
Example 8
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m), m);
}
 
Example 9
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(
      result,
      UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m),
      m);
}
 
Example 10
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */

private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example 11
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */

private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example 12
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */

private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example 13
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */

private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example 14
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */
private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example 15
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(
      result,
      UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m),
      m);
}