1 // $G $D/$F.go && $L $F.$A &&
2 // ./$A.out >tmp.go && $G tmp.go && $L -o $A.out1 tmp.$A && ./$A.out1
3 // rm -f tmp.go $A.out1
5 // Copyright 2012 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
9 // Generate test of shift and rotate by constants.
10 // The output is compiled and run.
12 // The output takes around a minute or two to compile, link, and run
13 // but it is only done during ./run, not in normal builds using run.go.
27 b := bufio.NewWriter(os.Stdout)
30 fmt.Fprintf(b, "%s\n", prolog)
32 for logBits := uint(3); logBits <= 6; logBits++ {
33 for mode := 0; mode < 1<<2; mode++ {
34 gentest(b, 1<<logBits, mode&1 != 0, mode&2 != 0)
51 i32 int32 = 0x12345678
52 i64 int64 = 0x123456789abcdef0
55 ui32 uint32 = 0x12345678
56 ui64 uint64 = 0x123456789abcdef0
70 func check(desc string, have, want interface{}) {
73 fmt.Printf("%s = %T(%#x), want %T(%#x)\n", desc, have, have, want, want)
75 fmt.Printf("BUG: stopping after 100 failures\n")
90 uop = [2]func(x, y uint64) uint64{
91 func(x, y uint64) uint64 {
94 func(x, y uint64) uint64 {
98 iop = [2]func(x, y int64) int64{
99 func(x, y int64) int64 {
102 func(x, y int64) int64 {
106 cop = [2]byte{'|', '^'}
109 func gentest(b *bufio.Writer, bits uint, unsigned, inverted bool) {
110 fmt.Fprintf(b, "func init() {\n")
111 defer fmt.Fprintf(b, "}\n")
114 // Generate tests for left/right and right/left.
115 for l := uint(0); l <= bits; l++ {
116 for r := uint(0); r <= bits; r++ {
117 for o, op := range cop {
118 typ := fmt.Sprintf("int%d", bits)
119 v := fmt.Sprintf("i%d", bits)
124 v0 := int64(0x123456789abcdef0)
129 expr1 := fmt.Sprintf("%s<<%d %c %s>>%d", v, l, op, v, r)
130 expr2 := fmt.Sprintf("%s>>%d %c %s<<%d", v, r, op, v, l)
134 v := uint64(v0) >> (64 - bits)
135 v = uop[o](v<<l, v>>r)
138 result = fmt.Sprintf("%#x", v)
140 v := int64(v0) >> (64 - bits)
141 v = iop[o](v<<l, v>>r)
144 result = fmt.Sprintf("%#x", v)
147 fmt.Fprintf(b, "\tcheck(%q, %s, %s(%s))\n", expr1, expr1, typ, result)
148 fmt.Fprintf(b, "\tcheck(%q, %s, %s(%s))\n", expr2, expr2, typ, result)
150 // Chop test into multiple functions so that there's not one
151 // enormous function to compile/link.
152 // All the functions are named init so we don't have to do
153 // anything special to call them. ☺
155 fmt.Fprintf(b, "}\n")
156 fmt.Fprintf(b, "func init() {\n")