1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // fastlog2Table contains log2 approximations for 5 binary digits.
8 // This is used to implement fastlog2, which is used for heap sampling.
23 fmt.Fprintln(&buf, "// Code generated by mkfastlog2table.go; DO NOT EDIT.")
24 fmt.Fprintln(&buf, "// Run go generate from src/runtime to update.")
25 fmt.Fprintln(&buf, "// See mkfastlog2table.go for comments.")
27 fmt.Fprintln(&buf, "package runtime")
29 fmt.Fprintln(&buf, "const fastlogNumBits =", fastlogNumBits)
32 fmt.Fprintln(&buf, "var fastlog2Table = [1<<fastlogNumBits + 1]float64{")
33 table := computeTable()
34 for _, t := range table {
35 fmt.Fprintf(&buf, "\t%v,\n", t)
37 fmt.Fprintln(&buf, "}")
39 if err := os.WriteFile("fastlog2table.go", buf.Bytes(), 0644); err != nil {
44 const fastlogNumBits = 5
46 func computeTable() []float64 {
47 fastlog2Table := make([]float64, 1<<fastlogNumBits+1)
48 for i := 0; i <= (1 << fastlogNumBits); i++ {
49 fastlog2Table[i] = log2(1.0 + float64(i)/(1<<fastlogNumBits))
54 // log2 is a local copy of math.Log2 with an explicit float64 conversion
55 // to disable FMA. This lets us generate the same output on all platforms.
56 func log2(x float64) float64 {
57 frac, exp := math.Frexp(x)
58 // Make sure exact powers of two give an exact answer.
59 // Don't depend on Log(0.5)*(1/Ln2)+exp being exactly exp-1.
61 return float64(exp - 1)
63 return float64(nlog(frac)*(1/math.Ln2)) + float64(exp)
66 // nlog is a local copy of math.Log with explicit float64 conversions
67 // to disable FMA. This lets us generate the same output on all platforms.
68 func nlog(x float64) float64 {
70 Ln2Hi = 6.93147180369123816490e-01 /* 3fe62e42 fee00000 */
71 Ln2Lo = 1.90821492927058770002e-10 /* 3dea39ef 35793c76 */
72 L1 = 6.666666666666735130e-01 /* 3FE55555 55555593 */
73 L2 = 3.999999999940941908e-01 /* 3FD99999 9997FA04 */
74 L3 = 2.857142874366239149e-01 /* 3FD24924 94229359 */
75 L4 = 2.222219843214978396e-01 /* 3FCC71C5 1D8E78AF */
76 L5 = 1.818357216161805012e-01 /* 3FC74664 96CB03DE */
77 L6 = 1.531383769920937332e-01 /* 3FC39A09 D078C69F */
78 L7 = 1.479819860511658591e-01 /* 3FC2F112 DF3E5244 */
83 case math.IsNaN(x) || math.IsInf(x, 1):
92 f1, ki := math.Frexp(x)
93 if f1 < math.Sqrt2/2 {
101 s := float64(f / (2 + f))
103 s4 := float64(s2 * s2)
104 t1 := s2 * float64(L1+float64(s4*float64(L3+float64(s4*float64(L5+float64(s4*L7))))))
105 t2 := s4 * float64(L2+float64(s4*float64(L4+float64(s4*L6))))
106 R := float64(t1 + t2)
107 hfsq := float64(0.5 * f * f)
108 return float64(k*Ln2Hi) - ((hfsq - (float64(s*float64(hfsq+R)) + float64(k*Ln2Lo))) - f)