public final class Fast extends Object
This class used to contain a large number of bits hacks. Intrinsification of methods
such as Long.bitCount(long)
, Long.numberOfTrailingZeros(long)
, etc. using
SSE instructions has made such hacks obsolete.
The main highlight is now a new algorithm for selection that is twice as fast as the one previously implemented, but that will behave impredictably if there is no bit with the requested rank; the algorithm is based on the one presented by Sebastiano Vigna in “Broadword Implementation of Rank/Select Queries”, Proc. of the 7th International Workshop on Experimental Algorithms, WEA 2008, number 5038 in Lecture Notes in Computer Science, pages 154−168. Springer–Verlag, 2008, but it has been improved with ideas from Simon Gog's SDSL library.
Modifier and Type | Field and Description |
---|---|
static int[] |
BYTELSB
Precomputed least significant bits for bytes (-1 for 0 ).
|
static int[] |
BYTEMSB
Precomputed most significant bits for bytes (-1 for 0 ).
|
static long |
INCR_STEP_8
Deprecated.
|
static long |
MSBS_STEP_8 |
static long |
ONES_STEP_4 |
static long |
ONES_STEP_8 |
static byte[] |
selectInByte
A precomputed table containing in position 256i + j the position of the i-th one (0 ≤ j < 8) in the binary representation of i
(0 ≤ i < 256), or -1 if no such bit exists.
|
Modifier and Type | Method and Description |
---|---|
static int |
approximateLog2(double x)
Computes an approximate integer base-2 logarithm of the argument.
|
static int |
ceilLog2(int x)
Computes the ceiling of the base-two logarithm of the argument.
|
static int |
ceilLog2(long x)
Computes the ceiling of the base-two logarithm of the argument.
|
static int |
count(long x)
Deprecated.
Please use
Long.bitCount(long) , which is intrinsified in recent JVMs. |
static int |
int2nat(int x)
Maps integers bijectively into natural numbers.
|
static long |
int2nat(long x)
Maps longs bijectively into long natural numbers.
|
static int |
leastSignificantBit(long x)
Deprecated.
Please use
Long.numberOfTrailingZeros(long) , which is intrinsified in recent JVMs, taking
into consideration that the input 0 is treated differently. |
static int |
length(int x)
Returns the number of bits that are necessary to encode the argument.
|
static int |
length(long x)
Returns the number of bits that are necessary to encode the argument.
|
static double |
log2(double x)
Returns the base-two logarithm of the argument.
|
static void |
main(String[] a) |
static int |
mostSignificantBit(int x)
Returns the most significant bit of an integer.
|
static int |
mostSignificantBit(long x)
Returns the most significant bit of a long.
|
static int |
nat2int(int x)
Maps natural numbers bijectively into integers.
|
static long |
nat2int(long x)
Maps long natural numbers bijectively into longs.
|
static double |
pow2(int exponent)
Quickly raises 2 to the argument.
|
static long |
reverseBytes(long l)
Deprecated.
|
static int |
select(long x,
int rank)
Returns the position of a bit of given rank (starting from zero).
|
public static final long ONES_STEP_4
public static final long ONES_STEP_8
public static final long MSBS_STEP_8
@Deprecated public static final long INCR_STEP_8
public static final int[] BYTELSB
public static final int[] BYTEMSB
public static final byte[] selectInByte
public static int int2nat(int x)
This method will map a negative integer x to -2x-1 and
a nonnegative integer x to 2x. It can be used to save
integers in the range [Integer.MIN_VALUE
/2..Integer.MAX_VALUE
/2]
(i.e., [-230..230-1])
using the standard coding methods (which all work on natural numbers). Note
that no range checks are performed.
The inverse of the above map is computed by nat2int(int)
.
x
- an integer.nat2int(int)
public static int nat2int(int x)
This method computes the inverse of int2nat(int)
.
x
- a natural number.int2nat(int)
public static long int2nat(long x)
This method will map a negative long x to -2x-1 and
a nonnegative long x to 2x. It can be used to save
longs in the range [Long.MIN_VALUE
/2..Long.MAX_VALUE
/2]
(i.e., [-262..262-1])
using the standard coding methods (which all work on natural numbers). Note
that no range checks are performed.
The inverse of the above map is computed by nat2int(long)
.
x
- a long.int2nat(int)
public static long nat2int(long x)
This method computes the inverse of int2nat(long)
.
x
- a long natural number.nat2int(int)
public static double log2(double x)
x
- a double.x
.public static int ceilLog2(int x)
This method relies on mostSignificantBit(int)
, and thus is pretty fast.
x
- an integer.x
is zero.public static int ceilLog2(long x)
This method relies on mostSignificantBit(long)
, and thus is pretty fast.
x
- an integer.x
is zero.public static int approximateLog2(double x)
This method relies on Double.doubleToRawLongBits(double)
, and thus is very
fast if the former is intrinsified by the JVM.
x
- a double.public static double pow2(int exponent)
exponent
- an integer exponent between -62 ad 62.exponent
.public static int length(int x)
x
- an integer.x
.public static int length(long x)
x
- a long.x
.@Deprecated public static int count(long x)
Long.bitCount(long)
, which is intrinsified in recent JVMs.This method implements a classical broadword algorithm.
x
- a long.x
.public static int select(long x, int rank)
Warning: This method implements a new, much faster broadword algorithm, that, however, cannot handle the case in which there is no bit of requested rank.
x
- a long.rank
- an integer smaller than the number of ones in x
; impredictable
results (including exceptions) might happen if this constraint is violated.x
of the bit of given rank.public static int mostSignificantBit(long x)
This method returns 64 − Long.numberOfLeadingZeroes( x )
.
x
- a long.x
, of x
is nonzero; −1, otherwise.public static int mostSignificantBit(int x)
x
- an integer.x
, of x
is nonzero; −1, otherwise.mostSignificantBit(long)
@Deprecated public static int leastSignificantBit(long x)
Long.numberOfTrailingZeros(long)
, which is intrinsified in recent JVMs, taking
into consideration that the input 0 is treated differently.x
- a long integer.@Deprecated public static long reverseBytes(long l)
Long.reverseBytes(long)
.l
- a long integer.l
: the first byte is the last byte of l
, and so on.public static void main(String[] a)