Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/mkfastlog2table.go

Documentation: runtime

     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  //go:build ignore
     6  // +build ignore
     7  
     8  // fastlog2Table contains log2 approximations for 5 binary digits.
     9  // This is used to implement fastlog2, which is used for heap sampling.
    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