Package cds.healpix.common.math
Class HackersDelight
java.lang.Object
cds.healpix.common.math.HackersDelight
Utility 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
FieldsModifier and TypeFieldDescriptionstatic final long
Bit mask to isolate all but the sign bit of a long or double.static final long
Bit mask to isolate all but the sign bit of a long or double.static final long
Bit mask to isolate the sign bit of a long or double.static final int
Bit mask to isolate the sign bit of an int or a float.static final long
Bit mask to isolate the sign bit of a long or double. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic double
abs
(double x) static int
abs
(int x) static long
abs
(long x) static int
doz
(int x, int y) Doz stands for "difference or zero" (see p.static long
doz
(long x, long y) Doz stands for "difference or zero" (see p.static int
floorInt
(double a) static int
floorIntN
(double a) static int
floorIntP
(double a) static long
floorLong
(double a) static long
floorLongN
(double a) static long
floorLongP
(double a) static long
flp2
(long x) Round down to the next power of 2 (see HD Fig.static double
fromBits
(long doubleBits) static double
halfOf
(double x) static boolean
isPowOf2
(int x) Determines if an integer is a power of two, exact solution.static boolean
isPowOf2Fast
(int x) Determines if an integer is a power of two, including 0.static void
static int
max
(int x, int y) Returns the greater of two values.static long
max
(long x, long y) Returns the greater of two values.static int
min
(int x, int y) Returns the smaller of two values.static long
min
(long x, long y) Returns the smaller of two values.static int
nlz
(int x) Branch-free version ofInteger.numberOfLeadingZeros(int)
(also taken from the HD).static long
toBits
(double x)
-
Field Details
-
SIGN_BIT_MASK_I
public static final int SIGN_BIT_MASK_IBit mask to isolate the sign bit of an int or a float.- See Also:
-
SIGN_BIT_MASK_L
public static final long SIGN_BIT_MASK_LBit mask to isolate the sign bit of a long or double.- See Also:
-
BUT_SIGN_BIT_MASK_L
public static final long BUT_SIGN_BIT_MASK_LBit mask to isolate all but the sign bit of a long or double.- See Also:
-
EXPONENT_BITS_MASK
public static final long EXPONENT_BITS_MASKBit mask to isolate the sign bit of a long or double.- See Also:
-
BUT_EXPONENT_BIT_MASK
public static final long BUT_EXPONENT_BIT_MASKBit mask to isolate all but the sign bit of a long or double.- See Also:
-
-
Constructor Details
-
HackersDelight
public HackersDelight()
-
-
Method Details
-
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 returns0
ifx > y
, else returnsx - y
, so it can be replace by(x - y) > 0 ? x - y : 0
- Parameters:
x
- an argumenty
- another argument- Returns:
0
ifx > 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 returns0
ifx > y
, else returnsx - y
, so it can be replace by(x - y) > 0 ? x - y : 0
- Parameters:
x
- an argumenty
- another argument- Returns:
0
ifx > 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
-