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 gigabyte of memory to compile, link, and run
13 // but it is only done during ./run, not in normal builds using run.go.
28 b := bufio.NewWriter(os.Stdout)
31 fmt.Fprintf(b, "%s\n", prolog)
33 for logBits := uint(3); logBits <= 6; logBits++ {
34 typ := fmt.Sprintf("int%d", 1<<logBits)
35 fmt.Fprint(b, strings.Replace(checkFunc, "XXX", typ, -1))
36 fmt.Fprint(b, strings.Replace(checkFunc, "XXX", "u"+typ, -1))
37 for mode := 0; mode < 1<<2; mode++ {
38 gentest(b, 1<<logBits, mode&1 != 0, mode&2 != 0)
55 i32 int32 = 0x12345678
56 i64 int64 = 0x123456789abcdef0
59 ui32 uint32 = 0x12345678
60 ui64 uint64 = 0x123456789abcdef0
83 func check_XXX(desc string, have, want XXX) {
86 fmt.Printf("%s = %T(%#x), want %T(%#x)\n", desc, have, have, want, want)
88 fmt.Printf("BUG: stopping after 100 failures\n")
96 uop = [2]func(x, y uint64) uint64{
97 func(x, y uint64) uint64 {
100 func(x, y uint64) uint64 {
104 iop = [2]func(x, y int64) int64{
105 func(x, y int64) int64 {
108 func(x, y int64) int64 {
112 cop = [2]byte{'|', '^'}
115 func gentest(b *bufio.Writer, bits uint, unsigned, inverted bool) {
116 fmt.Fprintf(b, "func init() {\n")
117 defer fmt.Fprintf(b, "}\n")
120 // Generate tests for left/right and right/left.
121 for l := uint(0); l <= bits; l++ {
122 for r := uint(0); r <= bits; r++ {
123 for o, op := range cop {
124 typ := fmt.Sprintf("int%d", bits)
125 v := fmt.Sprintf("i%d", bits)
130 v0 := int64(0x123456789abcdef0)
135 expr1 := fmt.Sprintf("%s<<%d %c %s>>%d", v, l, op, v, r)
136 expr2 := fmt.Sprintf("%s>>%d %c %s<<%d", v, r, op, v, l)
140 v := uint64(v0) >> (64 - bits)
141 v = uop[o](v<<l, v>>r)
144 result = fmt.Sprintf("%#x", v)
146 v := int64(v0) >> (64 - bits)
147 v = iop[o](v<<l, v>>r)
150 result = fmt.Sprintf("%#x", v)
153 fmt.Fprintf(b, "\tcheck_%s(%q, %s, %s(%s))\n", typ, expr1, expr1, typ, result)
154 fmt.Fprintf(b, "\tcheck_%s(%q, %s, %s(%s))\n", typ, expr2, expr2, typ, result)
156 // Chop test into multiple functions so that there's not one
157 // enormous function to compile/link.
158 // All the functions are named init so we don't have to do
159 // anything special to call them. ☺
161 fmt.Fprintf(b, "}\n")
162 fmt.Fprintf(b, "func init() {\n")