gen.go 1.1 KB

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