1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// runoutput
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// terribly slow on wasm
//go:build !wasm
package main
import (
"fmt"
"math/big"
"unsafe"
)
var one = big.NewInt(1)
type _type struct {
name string
bits uint
signed bool
}
// testvalues returns a list of all test values for this type.
func (t *_type) testvalues() []*big.Int {
var a []*big.Int
a = append(a, big.NewInt(0))
a = append(a, big.NewInt(1))
a = append(a, big.NewInt(2))
if t.signed {
a = append(a, big.NewInt(-1))
a = append(a, big.NewInt(-2))
r := big.NewInt(1)
a = append(a, r.Lsh(r, t.bits-1).Sub(r, big.NewInt(1)))
r = big.NewInt(1)
a = append(a, r.Lsh(r, t.bits-1).Sub(r, big.NewInt(2)))
r = big.NewInt(1)
a = append(a, r.Lsh(r, t.bits-1).Neg(r))
r = big.NewInt(1)
a = append(a, r.Lsh(r, t.bits-1).Neg(r).Add(r, big.NewInt(1)))
} else {
r := big.NewInt(1)
a = append(a, r.Lsh(r, t.bits).Sub(r, big.NewInt(1)))
r = big.NewInt(1)
a = append(a, r.Lsh(r, t.bits).Sub(r, big.NewInt(2)))
}
return a
}
// trunc truncates a value to the range of the given type.
func (t *_type) trunc(x *big.Int) *big.Int {
r := new(big.Int)
m := new(big.Int)
m.Lsh(one, t.bits)
m.Sub(m, one)
r.And(x, m)
if t.signed && r.Bit(int(t.bits)-1) == 1 {
m.Neg(one)
m.Lsh(m, t.bits)
r.Or(r, m)
}
return r
}
var types = []_type{
_type{"byte", 8, false},
_type{"int8", 8, true},
_type{"uint8", 8, false},
_type{"rune", 32, true},
_type{"int16", 16, true},
_type{"uint16", 16, false},
_type{"int32", 32, true},
_type{"uint32", 32, false},
_type{"int64", 64, true},
_type{"uint64", 64, false},
_type{"int", 8 * uint(unsafe.Sizeof(int(0))), true},
_type{"uint", 8 * uint(unsafe.Sizeof(uint(0))), false},
_type{"uintptr", 8 * uint(unsafe.Sizeof((*byte)(nil))), false},
}
type binop struct {
name string
eval func(x, y *big.Int) *big.Int
}
var binops = []binop{
binop{"+", func(x, y *big.Int) *big.Int { return new(big.Int).Add(x, y) }},
binop{"-", func(x, y *big.Int) *big.Int { return new(big.Int).Sub(x, y) }},
binop{"*", func(x, y *big.Int) *big.Int { return new(big.Int).Mul(x, y) }},
binop{"/", func(x, y *big.Int) *big.Int { return new(big.Int).Quo(x, y) }},
binop{"%", func(x, y *big.Int) *big.Int { return new(big.Int).Rem(x, y) }},
binop{"&", func(x, y *big.Int) *big.Int { return new(big.Int).And(x, y) }},
binop{"|", func(x, y *big.Int) *big.Int { return new(big.Int).Or(x, y) }},
binop{"^", func(x, y *big.Int) *big.Int { return new(big.Int).Xor(x, y) }},
binop{"&^", func(x, y *big.Int) *big.Int { return new(big.Int).AndNot(x, y) }},
}
type unop struct {
name string
eval func(x *big.Int) *big.Int
}
var unops = []unop{
unop{"+", func(x *big.Int) *big.Int { return new(big.Int).Set(x) }},
unop{"-", func(x *big.Int) *big.Int { return new(big.Int).Neg(x) }},
unop{"^", func(x *big.Int) *big.Int { return new(big.Int).Not(x) }},
}
type shiftop struct {
name string
eval func(x *big.Int, i uint) *big.Int
}
var shiftops = []shiftop{
shiftop{"<<", func(x *big.Int, i uint) *big.Int { return new(big.Int).Lsh(x, i) }},
shiftop{">>", func(x *big.Int, i uint) *big.Int { return new(big.Int).Rsh(x, i) }},
}
// valname returns the name of n as can be used as part of a variable name.
func valname(n *big.Int) string {
s := fmt.Sprintf("%d", n)
if s[0] == '-' {
s = "neg" + s[1:]
}
return s
}
func main() {
fmt.Println("package main")
// We make variables to hold all the different values we'd like to use.
// We use global variables to prevent any constant folding.
for _, t := range types {
for _, n := range t.testvalues() {
fmt.Printf("var %s_%s %s = %d\n", t.name, valname(n), t.name, n)
}
}
fmt.Println("func main() {")
for _, t := range types {
// test binary ops
for _, op := range binops {
for _, x := range t.testvalues() {
for _, y := range t.testvalues() {
if (op.name == "/" || op.name == "%") && y.Sign() == 0 {
continue
}
r := t.trunc(op.eval(x, y))
eqn := fmt.Sprintf("%s_%s %s %s_%s != %d", t.name, valname(x), op.name, t.name, valname(y), r)
fmt.Printf("\tif %s { println(\"bad: %s\") }\n", eqn, eqn)
}
}
}
// test unary ops
for _, op := range unops {
for _, x := range t.testvalues() {
r := t.trunc(op.eval(x))
eqn := fmt.Sprintf("%s %s_%s != %d", op.name, t.name, valname(x), r)
fmt.Printf("\tif %s { println(\"bad: %s\") }\n", eqn, eqn)
}
}
// test shifts
for _, op := range shiftops {
for _, x := range t.testvalues() {
for _, i := range []uint{0, 1, t.bits - 2, t.bits - 1, t.bits, t.bits + 1} {
r := t.trunc(op.eval(x, i))
eqn := fmt.Sprintf("%s_%s %s %d != %d", t.name, valname(x), op.name, i, r)
fmt.Printf("\tif %s { println(\"bad: %s\") }\n", eqn, eqn)
}
}
}
}
fmt.Println("}")
}
|