MCPcopy Create free account
hub / github.com/antlr/codebuff / log2

Method log2

output/java_guava/1.4.16/LongMath.java:125–151  ·  view source on GitHub ↗

Returns the base-2 logarithm of x, rounded according to the specified rounding mode. @throws IllegalArgumentException if x <= 0 @throws ArithmeticException if mode is RoundingMode#UNNECESSARY and x is not a power of two

(long x, RoundingMode mode)

Source from the content-addressed store, hash-verified

123 */
124
125 @SuppressWarnings("fallthrough")
126 // TODO(kevinb): remove after this warning is disabled globally
127 public static int log2(long x, RoundingMode mode) {
128 checkPositive("x", x);
129 switch (mode) {
130 case UNNECESSARY:
131 checkRoundingUnnecessary(isPowerOfTwo(x));
132 // fall through
133 case DOWN:
134 case FLOOR:
135 return (Long.SIZE - 1) - Long.numberOfLeadingZeros(x);
136 case UP:
137 case CEILING:
138 return Long.SIZE - Long.numberOfLeadingZeros(x - 1);
139 case HALF_DOWN:
140 case HALF_UP:
141 case HALF_EVEN:
142 // Since sqrt(2) is irrational, log2(x) - logFloor cannot be exactly 0.5
143 int leadingZeros = Long.numberOfLeadingZeros(x);
144 long cmp = MAX_POWER_OF_SQRT2_UNSIGNED >>> leadingZeros;
145 // floor(2^(logFloor + 0.5))
146 int logFloor = (Long.SIZE - 1) - leadingZeros;
147 return logFloor + lessThanBranchFree(cmp, x);
148 default:
149 throw new AssertionError("impossible");
150 }
151 }
152
153 /** The biggest half power of two that fits into an unsigned long */
154

Callers 3

factorialMethod · 0.95
binomialMethod · 0.95
binomialMethod · 0.95

Calls 4

isPowerOfTwoMethod · 0.95
lessThanBranchFreeMethod · 0.95
checkPositiveMethod · 0.45

Tested by

no test coverage detected