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

Method log10Floor

output/java_guava/1.4.19/IntMath.java:191–205  ·  view source on GitHub ↗
(int x)

Source from the content-addressed store, hash-verified

189 }
190
191 private static int log10Floor(int x) {
192 /*
193 * Based on Hacker's Delight Fig. 11-5, the two-table-lookup, branch-free implementation.
194 *
195 * The key idea is that based on the number of leading zeros (equivalently, floor(log2(x))), we
196 * can narrow the possible floor(log10(x)) values to two. For example, if floor(log2(x)) is 6,
197 * then 64 <= x < 128, so floor(log10(x)) is either 1 or 2.
198 */
199 int y = maxLog10ForLeadingZeros[Integer.numberOfLeadingZeros(x)];
200 /*
201 * y is the higher of the two possible values of floor(log10(x)). If x < 10^y, then we want the
202 * lower of the two possible values, or y - 1, otherwise, we want y.
203 */
204 return y - lessThanBranchFree(x, powersOf10[y]);
205 }
206
207 // maxLog10ForLeadingZeros[i] == floor(log10(2^(Long.SIZE - i)))
208

Callers 1

log10Method · 0.95

Calls 1

lessThanBranchFreeMethod · 0.95

Tested by

no test coverage detected