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

Method log10Floor

output/java_guava/1.4.18/LongMath.java:193–208  ·  view source on GitHub ↗
(long x)

Source from the content-addressed store, hash-verified

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

Callers 1

log10Method · 0.95

Calls 1

lessThanBranchFreeMethod · 0.95

Tested by

no test coverage detected