github.com/acrespo/mobile@v0.0.0-20190107162257-dc0771356504/exp/f32/gen.go (about)

     1  // Copyright 2014 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  package main
     8  
     9  /*
    10  This program generates table.go. Invoke it as:
    11  go run gen.go -output table.go
    12  */
    13  
    14  import (
    15  	"bytes"
    16  	"flag"
    17  	"fmt"
    18  	"go/format"
    19  	"io/ioutil"
    20  	"log"
    21  	"math"
    22  )
    23  
    24  // N is the number of entries in the sin look-up table. It must be a power of 2.
    25  const N = 4096
    26  
    27  var filename = flag.String("output", "table.go", "output file name")
    28  
    29  func main() {
    30  	b := new(bytes.Buffer)
    31  	fmt.Fprintf(b, "// Code generated by go run gen.go; DO NOT EDIT\n\npackage f32\n\n")
    32  	fmt.Fprintf(b, "const sinTableLen = %d\n\n", N)
    33  	fmt.Fprintf(b, "// sinTable[i] equals sin(i * π / sinTableLen).\n")
    34  	fmt.Fprintf(b, "var sinTable = [sinTableLen]float32{\n")
    35  	for i := 0; i < N; i++ {
    36  		radians := float64(i) * (math.Pi / N)
    37  		fmt.Fprintf(b, "%v,\n", float32(math.Sin(radians)))
    38  	}
    39  	fmt.Fprintf(b, "}\n")
    40  
    41  	data, err := format.Source(b.Bytes())
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  	if err := ioutil.WriteFile(*filename, data, 0644); err != nil {
    46  		log.Fatal(err)
    47  	}
    48  }