-
Notifications
You must be signed in to change notification settings - Fork 2
/
log10.go
36 lines (33 loc) · 968 Bytes
/
log10.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Package log10 calculates log base 10 of an integer.
// This is a simple task.
// This package focuses on performance,
// as there are non-obvious techniques for speed.
package log10
import (
"math/bits"
)
var table9 = [16]uint32{
0, 9, 99, 999, 9999, 99999, 999999,
9999999, 99999999, 999999999, 0xFFFFFFFF,
}
// Uint32 returns the number of digits required to hold the base 10 representation of x.
// As a special case, Uint32(0) == 1.
//
// Note that if you are making a byte buffer for a base 10 representation of x,
// you will often get better results by calling make with a large-enough constant.
// Be sure to benchmark both approaches.
//
// Implementation inspired by
// https://lemire.me/blog/2021/05/28/computing-the-number-of-digits-of-an-integer-quickly/#comment-585476
// and tweaked for the Go compiler.
func Uint32(x uint32) int {
x |= 1
log2 := bits.Len32(x)
n := 9 * log2
n += 32 - 9
n >>= 5
if x > table9[n&15] {
n++
}
return n
}