(int x)
| 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 |
no test coverage detected