Class FastIntegerMath


  • final class FastIntegerMath
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.math.BigInteger FIVE  
      (package private) static java.math.BigInteger FIVE_POW_16  
      private static java.math.BigInteger[] SMALL_POWERS_OF_TEN  
      (package private) static java.math.BigInteger TEN_POW_16  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FastIntegerMath()
      Don't let anyone instantiate this class.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static java.math.BigInteger computePowerOfTen​(java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> powersOfTen, int n)
      Computes the n-th power of ten.
      (package private) static java.math.BigInteger computeTenRaisedByNFloor16Recursive​(java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> powersOfTen, int n)
      Computes 10n&~15.
      (package private) static java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> createPowersOfTenFloor16Map()  
      static long estimateNumBits​(long numDecimalDigits)  
      (package private) static java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> fillPowersOf10Floor16​(int from, int to)
      Fills a map with powers of 10 floor 16.
      (package private) static void fillPowersOfNFloor16Recursive​(java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> powersOfTen, int from, int to)  
      (package private) static int splitFloor16​(int from, int to)
      Finds middle of range with upper range half rounded up to multiple of 16.
      (package private) static long unsignedMultiplyHigh​(long x, long y)
      Computes uint128 product = (uint64)x * (uint64)y.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • FIVE

        public static final java.math.BigInteger FIVE
      • TEN_POW_16

        static final java.math.BigInteger TEN_POW_16
      • FIVE_POW_16

        static final java.math.BigInteger FIVE_POW_16
      • SMALL_POWERS_OF_TEN

        private static final java.math.BigInteger[] SMALL_POWERS_OF_TEN
    • Constructor Detail

      • FastIntegerMath

        private FastIntegerMath()
        Don't let anyone instantiate this class.
    • Method Detail

      • computePowerOfTen

        static java.math.BigInteger computePowerOfTen​(java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> powersOfTen,
                                                      int n)
        Computes the n-th power of ten.
        Parameters:
        powersOfTen - A map with pre-computed powers of ten
        n - the power
        Returns:
        the computed power of ten
      • computeTenRaisedByNFloor16Recursive

        static java.math.BigInteger computeTenRaisedByNFloor16Recursive​(java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> powersOfTen,
                                                                        int n)
        Computes 10n&~15.
      • createPowersOfTenFloor16Map

        static java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> createPowersOfTenFloor16Map()
      • estimateNumBits

        public static long estimateNumBits​(long numDecimalDigits)
      • fillPowersOf10Floor16

        static java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> fillPowersOf10Floor16​(int from,
                                                                                                          int to)
        Fills a map with powers of 10 floor 16.
        Parameters:
        from - the start index of the character sequence that contains the digits
        to - the end index of the character sequence that contains the digits
        Returns:
        the filled map
      • fillPowersOfNFloor16Recursive

        static void fillPowersOfNFloor16Recursive​(java.util.NavigableMap<java.lang.Integer,​java.math.BigInteger> powersOfTen,
                                                  int from,
                                                  int to)
      • unsignedMultiplyHigh

        static long unsignedMultiplyHigh​(long x,
                                         long y)
        Computes uint128 product = (uint64)x * (uint64)y.

        References:

        Getting the high part of 64 bit integer multiplication
        stackoverflow
        Parameters:
        x - uint64 factor x
        y - uint64 factor y
        Returns:
        uint128 product of x and y
      • splitFloor16

        static int splitFloor16​(int from,
                                int to)
        Finds middle of range with upper range half rounded up to multiple of 16.
        Parameters:
        from - start of range (inclusive)
        to - end of range (exclusive)
        Returns:
        middle of range with upper range half rounded up to multiple of 16