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

Method log2

output/java_guava/1.4.19/IntMath.java:124–150  ·  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

(int x, RoundingMode mode)

Source from the content-addressed store, hash-verified

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

Callers 3

factorialMethod · 0.95
ceilToPowerOfTwoMethod · 0.95
AlphabetMethod · 0.45

Calls 4

isPowerOfTwoMethod · 0.95
lessThanBranchFreeMethod · 0.95
checkPositiveMethod · 0.45

Tested by

no test coverage detected