gen.go 1.1 KB

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