Package cds.healpix.common.math
Class HackersDelight
- java.lang.Object
-
- cds.healpix.common.math.HackersDelight
-
public final class HackersDelight extends java.lang.ObjectUtility class which takes its name from the book "Hacker's Delight" by Henry S. Warren. Jr. We also uses the "Bit Twiddling Hacks" page by Sean Eron Anderson from Standford.- Author:
- F.-X Pineau
-
-
Field Summary
Fields Modifier and Type Field Description static longBUT_EXPONENT_BIT_MASKBit mask to isolate all but the sign bit of a long or double.static longBUT_SIGN_BIT_MASK_LBit mask to isolate all but the sign bit of a long or double.static longEXPONENT_BITS_MASKBit mask to isolate the sign bit of a long or double.static intSIGN_BIT_MASK_IBit mask to isolate the sign bit of an int or a float.static longSIGN_BIT_MASK_LBit mask to isolate the sign bit of a long or double.
-
Constructor Summary
Constructors Constructor Description HackersDelight()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doubleabs(double x)static intabs(int x)static longabs(long x)static intdoz(int x, int y)Doz stands for "difference or zero" (see p.static longdoz(long x, long y)Doz stands for "difference or zero" (see p.static intfloorInt(double a)static intfloorIntN(double a)static intfloorIntP(double a)static longfloorLong(double a)static longfloorLongN(double a)static longfloorLongP(double a)static longflp2(long x)Round down to the next power of 2 (see HD Fig.static doublefromBits(long doubleBits)static doublehalfOf(double x)static booleanisPowOf2(int x)Determines if an integer is a power of two, exact solution.static booleanisPowOf2Fast(int x)Determines if an integer is a power of two, including 0.static voidmain(java.lang.String[] args)static intmax(int x, int y)Returns the greater of two values.static longmax(long x, long y)Returns the greater of two values.static intmin(int x, int y)Returns the smaller of two values.static longmin(long x, long y)Returns the smaller of two values.static intnlz(int x)Branch-free version ofInteger.numberOfLeadingZeros(int)(also taken from the HD).static longtoBits(double x)
-
-
-
Field Detail
-
SIGN_BIT_MASK_I
public static final int SIGN_BIT_MASK_I
Bit mask to isolate the sign bit of an int or a float.- See Also:
- Constant Field Values
-
SIGN_BIT_MASK_L
public static final long SIGN_BIT_MASK_L
Bit mask to isolate the sign bit of a long or double.- See Also:
- Constant Field Values
-
BUT_SIGN_BIT_MASK_L
public static final long BUT_SIGN_BIT_MASK_L
Bit mask to isolate all but the sign bit of a long or double.- See Also:
- Constant Field Values
-
EXPONENT_BITS_MASK
public static final long EXPONENT_BITS_MASK
Bit mask to isolate the sign bit of a long or double.- See Also:
- Constant Field Values
-
BUT_EXPONENT_BIT_MASK
public static final long BUT_EXPONENT_BIT_MASK
Bit mask to isolate all but the sign bit of a long or double.- See Also:
- Constant Field Values
-
-
Method Detail
-
isPowOf2
public static boolean isPowOf2(int x)
Determines if an integer is a power of two, exact solution. From the "Bit Twiddling Hacks" web page of Sean Eron Anderson, slightly modified.- Parameters:
x- the argument- Returns:
- true if the argument is a power of 2 and is different from 0.
-
isPowOf2Fast
public static boolean isPowOf2Fast(int x)
Determines if an integer is a power of two, including 0. From the "Bit Twiddling Hacks" web page of Sean Eron Anderson, slightly modified.- Parameters:
x- the argument- Returns:
- if the argument is a power of 2 OR if it is 0 (which is not a power of 2).
For an exact solution (excluding 0),
isPowOf2(int).
-
flp2
public static long flp2(long x)
Round down to the next power of 2 (see HD Fig. 3-1).- Parameters:
x- the argument- Returns:
- round down to the next power of 2
-
halfOf
public static double halfOf(double x)
-
toBits
public static long toBits(double x)
-
fromBits
public static double fromBits(long doubleBits)
-
abs
public static int abs(int x)
-
abs
public static long abs(long x)
-
abs
public static double abs(double x)
-
floorIntP
public static int floorIntP(double a)
-
floorIntN
public static int floorIntN(double a)
-
floorInt
public static int floorInt(double a)
-
floorLongP
public static long floorLongP(double a)
-
floorLongN
public static long floorLongN(double a)
-
floorLong
public static long floorLong(double a)
-
doz
public static int doz(int x, int y)Doz stands for "difference or zero" (see p. 37 of HD). It returns0ifx > y, else returnsx - y, so it can be replace by(x - y) > 0 ? x - y : 0- Parameters:
x- an argumenty- another argument- Returns:
0ifx > y, else returnsx - y.
-
doz
public static long doz(long x, long y)Doz stands for "difference or zero" (see p. 37 of HD). It returns0ifx > y, else returnsx - y, so it can be replace by(x - y) > 0 ? x - y : 0- Parameters:
x- an argumenty- another argument- Returns:
0ifx > y, else returnsx - y.
-
min
public static int min(int x, int y)Returns the smaller of two values.- Parameters:
x- an argumenty- another argument- Returns:
- the smaller of two values.
-
min
public static long min(long x, long y)Returns the smaller of two values.- Parameters:
x- an argumenty- another argument- Returns:
- the smaller of two values.
-
max
public static int max(int x, int y)Returns the greater of two values.- Parameters:
x- an argumenty- another argument- Returns:
- the greater of two values.
-
max
public static long max(long x, long y)Returns the greater of two values.- Parameters:
x- an argumenty- another argument- Returns:
- the greater of two values.
-
nlz
public static int nlz(int x)
Branch-free version ofInteger.numberOfLeadingZeros(int)(also taken from the HD). See "Number of leading zeros, branch-free binary search" in HD.- Parameters:
x- the argument- Returns:
- the number of leading zeros in the given argument.
-
main
public static void main(java.lang.String[] args)
-
-