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 // NOTE: This test is not run by 'run.go' and so not run by all.bash.
6 // To run this test you must use the ./run shell script.
8 // Copyright 2012 The Go Authors. All rights reserved.
9 // Use of this source code is governed by a BSD-style
10 // license that can be found in the LICENSE file.
12 // Generate test of shift and rotate by constants.
13 // The output is compiled and run.
15 // The output takes around a gigabyte of memory to compile, link, and run
16 // but it is only done during ./run, not in normal builds using run.go.
31 b := bufio.NewWriter(os.Stdout)
34 fmt.Fprintf(b, "%s\n", prolog)
36 for logBits := uint(3); logBits <= 6; logBits++ {
37 typ := fmt.Sprintf("int%d", 1<<logBits)
38 fmt.Fprint(b, strings.Replace(checkFunc, "XXX", typ, -1))
39 fmt.Fprint(b, strings.Replace(checkFunc, "XXX", "u"+typ, -1))
40 for mode := 0; mode < 1<<2; mode++ {
41 gentest(b, 1<<logBits, mode&1 != 0, mode&2 != 0)
58 i32 int32 = 0x12345678
59 i64 int64 = 0x123456789abcdef0
62 ui32 uint32 = 0x12345678
63 ui64 uint64 = 0x123456789abcdef0
86 func check_XXX(desc string, have, want XXX) {
89 fmt.Printf("%s = %T(%#x), want %T(%#x)\n", desc, have, have, want, want)
91 fmt.Printf("BUG: stopping after 100 failures\n")
99 uop = [2]func(x, y uint64) uint64{
100 func(x, y uint64) uint64 {
103 func(x, y uint64) uint64 {
107 iop = [2]func(x, y int64) int64{
108 func(x, y int64) int64 {
111 func(x, y int64) int64 {
115 cop = [2]byte{'|', '^'}
118 func gentest(b *bufio.Writer, bits uint, unsigned, inverted bool) {
119 fmt.Fprintf(b, "func init() {\n")
120 defer fmt.Fprintf(b, "}\n")
123 // Generate tests for left/right and right/left.
124 for l := uint(0); l <= bits; l++ {
125 for r := uint(0); r <= bits; r++ {
126 for o, op := range cop {
127 typ := fmt.Sprintf("int%d", bits)
128 v := fmt.Sprintf("i%d", bits)
133 v0 := int64(0x123456789abcdef0)
138 expr1 := fmt.Sprintf("%s<<%d %c %s>>%d", v, l, op, v, r)
139 expr2 := fmt.Sprintf("%s>>%d %c %s<<%d", v, r, op, v, l)
143 v := uint64(v0) >> (64 - bits)
144 v = uop[o](v<<l, v>>r)
147 result = fmt.Sprintf("%#x", v)
149 v := int64(v0) >> (64 - bits)
150 v = iop[o](v<<l, v>>r)
153 result = fmt.Sprintf("%#x", v)
156 fmt.Fprintf(b, "\tcheck_%s(%q, %s, %s(%s))\n", typ, expr1, expr1, typ, result)
157 fmt.Fprintf(b, "\tcheck_%s(%q, %s, %s(%s))\n", typ, expr2, expr2, typ, result)
159 // Chop test into multiple functions so that there's not one
160 // enormous function to compile/link.
161 // All the functions are named init so we don't have to do
162 // anything special to call them. ☺
164 fmt.Fprintf(b, "}\n")
165 fmt.Fprintf(b, "func init() {\n")