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