Source file
src/runtime/mkfastlog2table.go
Documentation: runtime
1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import (
14 "bytes"
15 "fmt"
16 "log"
17 "math"
18 "os"
19 )
20
21 func main() {
22 var buf bytes.Buffer
23
24 fmt.Fprintln(&buf, "// Code generated by mkfastlog2table.go; DO NOT EDIT.")
25 fmt.Fprintln(&buf, "// Run go generate from src/runtime to update.")
26 fmt.Fprintln(&buf, "// See mkfastlog2table.go for comments.")
27 fmt.Fprintln(&buf)
28 fmt.Fprintln(&buf, "package runtime")
29 fmt.Fprintln(&buf)
30 fmt.Fprintln(&buf, "const fastlogNumBits =", fastlogNumBits)
31 fmt.Fprintln(&buf)
32
33 fmt.Fprintln(&buf, "var fastlog2Table = [1<<fastlogNumBits + 1]float64{")
34 table := computeTable()
35 for _, t := range table {
36 fmt.Fprintf(&buf, "\t%v,\n", t)
37 }
38 fmt.Fprintln(&buf, "}")
39
40 if err := os.WriteFile("fastlog2table.go", buf.Bytes(), 0644); err != nil {
41 log.Fatalln(err)
42 }
43 }
44
45 const fastlogNumBits = 5
46
47 func computeTable() []float64 {
48 fastlog2Table := make([]float64, 1<<fastlogNumBits+1)
49 for i := 0; i <= (1 << fastlogNumBits); i++ {
50 fastlog2Table[i] = math.Log2(1.0 + float64(i)/(1<<fastlogNumBits))
51 }
52 return fastlog2Table
53 }
54
View as plain text