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
, "// AUTO-GENERATED by mkfastlog2table.go")
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
:= ioutil
.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
] = math
.Log2(1.0 + float64(i
)/(1<<fastlogNumBits
))