github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/runtime/mkfastlog2table.go (about)

     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.
     4  
     5  // +build ignore
     6  
     7  // fastlog2Table contains log2 approximations for 5 binary digits.
     8  // This is used to implement fastlog2, which is used for heap sampling.
     9  
    10  package main
    11  
    12  import (
    13  	"bytes"
    14  	"fmt"
    15  	"io/ioutil"
    16  	"log"
    17  	"math"
    18  )
    19  
    20  func main() {
    21  	var buf bytes.Buffer
    22  
    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.")
    26  	fmt.Fprintln(&buf)
    27  	fmt.Fprintln(&buf, "package runtime")
    28  	fmt.Fprintln(&buf)
    29  	fmt.Fprintln(&buf, "const fastlogNumBits =", fastlogNumBits)
    30  	fmt.Fprintln(&buf)
    31  
    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)
    36  	}
    37  	fmt.Fprintln(&buf, "}")
    38  
    39  	if err := ioutil.WriteFile("fastlog2table.go", buf.Bytes(), 0644); err != nil {
    40  		log.Fatalln(err)
    41  	}
    42  }
    43  
    44  const fastlogNumBits = 5
    45  
    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))
    50  	}
    51  	return fastlog2Table
    52  }