org.apache.hadoop.hive.common.type
Class SqlMathUtil

java.lang.Object
  extended by org.apache.hadoop.hive.common.type.SqlMathUtil

public final class SqlMathUtil
extends Object

This code was originally written for Microsoft PolyBase. Misc utilities used in this package.


Field Summary
static int FULLBITS_31
          Mask to convert an int to an unsigned int.
static int FULLBITS_32
          Max unsigned integer.
static long FULLBITS_63
          Mask to convert a long to an unsigned long.
static UnsignedInt128[] INVERSE_POWER_FIVES_INT128
          1/5^x, scaled to 128bits (in other words, 2^128/5^x).
static UnsignedInt128[] INVERSE_POWER_TENS_INT128
          1/10^x, scaled to 128bits, also word-shifted for better accuracy.
static int[] INVERSE_POWER_TENS_INT128_WORD_SHIFTS
          number of words shifted up in each INVERSE_POWER_TENS_INT128.
static long LONG_MASK
          Mask to convert signed integer to unsigned long.
static int MAX_POWER_FIVE_INT128
          5^55 fits in 2^128.
static int MAX_POWER_FIVE_INT31
          5^13 fits in 2^31.
static int MAX_POWER_FIVE_INT63
          5^27 fits in 2^63.
static int MAX_POWER_TEN_INT128
          10^38 fits in UnsignedInt128.
static int MAX_POWER_TEN_INT31
          10^9 fits in 2^31.
static int NEGATIVE_INT_MASK
          Mask to convert an int to a negative int.
static long NEGATIVE_LONG_MASK
          Mask to convert a long to a negative long.
static UnsignedInt128[] POWER_FIVES_INT128
          5^x.
static int[] POWER_FIVES_INT31
          5^x.
static long[] POWER_FIVES_INT63
          5^x.
static UnsignedInt128[] POWER_TENS_INT128
          10^x.
static int[] POWER_TENS_INT31
          10^x.
static UnsignedInt128[] ROUND_POWER_TENS_INT128
          5 * 10^(x-1).
static int[] ROUND_POWER_TENS_INT31
          5 * 10^(x-1).
 
Method Summary
static short bitLength(int v0, int v1, int v2, int v3)
          Returns the minimal number of bits to represent the words.
static short bitLengthInWord(int word)
          Returns the minimal number of bits to represent the given integer value.
static long combineInts(int lo, int hi)
           
static int compareUnsignedInt(int x, int y)
          If we can assume JDK 1.8, this should use java.lang.Integer.compareUnsigned(), which will be replaced with intrinsics in JVM.
static int compareUnsignedLong(long x, long y)
          If we can assume JDK 1.8, this should use java.lang.Long.compareUnsigned(), which will be replaced with intrinsics in JVM.
static int[] divideMultiPrecision(int[] dividend, int[] divisor, int[] quotient)
          Multi-precision divide.
static long divideUnsignedLong(long dividend, long divisor)
          If we can assume JDK 1.8, this should use java.lang.Long.divideUnsigned(), which will be replaced with intrinsics in JVM.
static int extractHiInt(long val)
           
static int extractLowInt(long val)
           
static long remainderUnsignedLong(long dividend, long divisor)
          If we can assume JDK 1.8, this should use java.lang.Long.remainderUnsigned(), which will be replaced with intrinsics in JVM.
static int setSignBitInt(int val, boolean positive)
          Turn on or off the highest bit of an int value.
static long setSignBitLong(long val, boolean positive)
          Turn on or off the highest bit of a long value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEGATIVE_LONG_MASK

public static final long NEGATIVE_LONG_MASK
Mask to convert a long to a negative long.

See Also:
Constant Field Values

FULLBITS_63

public static final long FULLBITS_63
Mask to convert a long to an unsigned long.

See Also:
Constant Field Values

NEGATIVE_INT_MASK

public static final int NEGATIVE_INT_MASK
Mask to convert an int to a negative int.

See Also:
Constant Field Values

LONG_MASK

public static final long LONG_MASK
Mask to convert signed integer to unsigned long.

See Also:
Constant Field Values

FULLBITS_31

public static final int FULLBITS_31
Mask to convert an int to an unsigned int.

See Also:
Constant Field Values

FULLBITS_32

public static final int FULLBITS_32
Max unsigned integer.

See Also:
Constant Field Values

MAX_POWER_FIVE_INT31

public static final int MAX_POWER_FIVE_INT31
5^13 fits in 2^31.

See Also:
Constant Field Values

POWER_FIVES_INT31

public static final int[] POWER_FIVES_INT31
5^x. All unsigned values.


MAX_POWER_FIVE_INT63

public static final int MAX_POWER_FIVE_INT63
5^27 fits in 2^63.

See Also:
Constant Field Values

POWER_FIVES_INT63

public static final long[] POWER_FIVES_INT63
5^x. All unsigned values.


MAX_POWER_FIVE_INT128

public static final int MAX_POWER_FIVE_INT128
5^55 fits in 2^128.

See Also:
Constant Field Values

POWER_FIVES_INT128

public static final UnsignedInt128[] POWER_FIVES_INT128
5^x.


INVERSE_POWER_FIVES_INT128

public static final UnsignedInt128[] INVERSE_POWER_FIVES_INT128
1/5^x, scaled to 128bits (in other words, 2^128/5^x). Because of flooring, this is same or smaller than real value.


MAX_POWER_TEN_INT31

public static final int MAX_POWER_TEN_INT31
10^9 fits in 2^31.

See Also:
Constant Field Values

POWER_TENS_INT31

public static final int[] POWER_TENS_INT31
10^x. All unsigned values.


ROUND_POWER_TENS_INT31

public static final int[] ROUND_POWER_TENS_INT31
5 * 10^(x-1).


MAX_POWER_TEN_INT128

public static final int MAX_POWER_TEN_INT128
10^38 fits in UnsignedInt128.

See Also:
Constant Field Values

POWER_TENS_INT128

public static final UnsignedInt128[] POWER_TENS_INT128
10^x.


ROUND_POWER_TENS_INT128

public static final UnsignedInt128[] ROUND_POWER_TENS_INT128
5 * 10^(x-1).


INVERSE_POWER_TENS_INT128

public static final UnsignedInt128[] INVERSE_POWER_TENS_INT128
1/10^x, scaled to 128bits, also word-shifted for better accuracy. Because of flooring, this is same or smaller than real value.


INVERSE_POWER_TENS_INT128_WORD_SHIFTS

public static final int[] INVERSE_POWER_TENS_INT128_WORD_SHIFTS
number of words shifted up in each INVERSE_POWER_TENS_INT128.

Method Detail

setSignBitInt

public static int setSignBitInt(int val,
                                boolean positive)
Turn on or off the highest bit of an int value.

Parameters:
val - the value to modify
positive - whether to turn off (positive) or on (negative).
Returns:
unsigned int value

setSignBitLong

public static long setSignBitLong(long val,
                                  boolean positive)
Turn on or off the highest bit of a long value.

Parameters:
val - the value to modify
positive - whether to turn off (positive) or on (negative).
Returns:
unsigned long value

bitLengthInWord

public static short bitLengthInWord(int word)
Returns the minimal number of bits to represent the given integer value.

Parameters:
word - int32 value
Returns:
the minimal number of bits to represent the given integer value

bitLength

public static short bitLength(int v0,
                              int v1,
                              int v2,
                              int v3)
Returns the minimal number of bits to represent the words.

Parameters:
v0 - v0
v1 - v1
v2 - v2
v3 - v3
Returns:
the minimal number of bits to represent the words

compareUnsignedInt

public static int compareUnsignedInt(int x,
                                     int y)
If we can assume JDK 1.8, this should use java.lang.Integer.compareUnsigned(), which will be replaced with intrinsics in JVM.

Parameters:
x - the first int to compare
y - the second int to compare
Returns:
the value 0 if x == y; a value less than 0 if x < y as unsigned values; and a value greater than 0 if x > y as unsigned values
See Also:
"http://hg.openjdk.java.net/jdk8/tl/jdk/rev/71200c517524"

compareUnsignedLong

public static int compareUnsignedLong(long x,
                                      long y)
If we can assume JDK 1.8, this should use java.lang.Long.compareUnsigned(), which will be replaced with intrinsics in JVM.

Parameters:
x - the first int to compare
y - the second int to compare
Returns:
the value 0 if x == y; a value less than 0 if x < y as unsigned values; and a value greater than 0 if x > y as unsigned values
See Also:
"http://hg.openjdk.java.net/jdk8/tl/jdk/rev/71200c517524"

divideUnsignedLong

public static long divideUnsignedLong(long dividend,
                                      long divisor)
If we can assume JDK 1.8, this should use java.lang.Long.divideUnsigned(), which will be replaced with intrinsics in JVM.

Parameters:
dividend - the value to be divided
divisor - the value doing the dividing
Returns:
the unsigned quotient of the first argument divided by the second argument
See Also:
"http://hg.openjdk.java.net/jdk8/tl/jdk/rev/71200c517524"

remainderUnsignedLong

public static long remainderUnsignedLong(long dividend,
                                         long divisor)
If we can assume JDK 1.8, this should use java.lang.Long.remainderUnsigned(), which will be replaced with intrinsics in JVM.

Parameters:
dividend - the value to be divided
divisor - the value doing the dividing
Returns:
the unsigned remainder of the first argument divided by the second argument
See Also:
"http://hg.openjdk.java.net/jdk8/tl/jdk/rev/71200c517524"

combineInts

public static long combineInts(int lo,
                               int hi)
Parameters:
lo - low 32bit
hi - high 32bit
Returns:
long value that combines the two integers

extractHiInt

public static int extractHiInt(long val)
Parameters:
val - long value
Returns:
high 32bit of the given value

extractLowInt

public static int extractLowInt(long val)
Parameters:
val - long value
Returns:
low 32bit of the given value

divideMultiPrecision

public static int[] divideMultiPrecision(int[] dividend,
                                         int[] divisor,
                                         int[] quotient)
Multi-precision divide. dividend and divisor not changed. Assumes that there is enough room in quotient for results. Drawbacks of this implementation: 1) Need one extra super-digit in R 2) As it modifies D during work, then it restores it back (this is necessary because the caller doesn't expect D to change) 3) Always get Q and R - if R is unnecessary, can be slightly faster.

Parameters:
dividend - dividend. in.
divisor - divisor. in.
quotient - quotient. out.
Returns:
remainder


Copyright © 2014 The Apache Software Foundation. All rights reserved.