libgo: update to go1.9
[official-gcc.git] / libgo / go / math / bits / make_tables.go
blobff2fe2e385ed043a92ca232a329ceffb3da18435
1 // Copyright 2017 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.
5 // +build ignore
7 // This program generates bits_tables.go.
9 package main
11 import (
12 "bytes"
13 "fmt"
14 "go/format"
15 "io"
16 "io/ioutil"
17 "log"
20 var header = []byte(`// Copyright 2017 The Go Authors. All rights reserved.
21 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
24 // Code generated by go run make_tables.go. DO NOT EDIT.
26 package bits
30 func main() {
31 buf := bytes.NewBuffer(header)
33 gen(buf, "ntz8tab", ntz8)
34 gen(buf, "pop8tab", pop8)
35 gen(buf, "rev8tab", rev8)
36 gen(buf, "len8tab", len8)
38 out, err := format.Source(buf.Bytes())
39 if err != nil {
40 log.Fatal(err)
43 err = ioutil.WriteFile("bits_tables.go", out, 0666)
44 if err != nil {
45 log.Fatal(err)
49 func gen(w io.Writer, name string, f func(uint8) uint8) {
50 fmt.Fprintf(w, "var %s = [256]uint8{", name)
51 for i := 0; i < 256; i++ {
52 if i%16 == 0 {
53 fmt.Fprint(w, "\n\t")
54 } else {
55 fmt.Fprint(w, " ")
57 fmt.Fprintf(w, "%#02x,", f(uint8(i)))
59 fmt.Fprint(w, "\n}\n\n")
62 func ntz8(x uint8) (n uint8) {
63 for x&1 == 0 && n < 8 {
64 x >>= 1
65 n++
67 return
70 func pop8(x uint8) (n uint8) {
71 for x != 0 {
72 x &= x - 1
73 n++
75 return
78 func rev8(x uint8) (r uint8) {
79 for i := 8; i > 0; i-- {
80 r = r<<1 | x&1
81 x >>= 1
83 return
86 func len8(x uint8) (n uint8) {
87 for x != 0 {
88 x >>= 1
89 n++
91 return