-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuint32_test.go
More file actions
219 lines (193 loc) · 4.33 KB
/
uint32_test.go
File metadata and controls
219 lines (193 loc) · 4.33 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package fastdiv
import (
"math"
"testing"
"testing/quick"
)
var (
sinkUint32 uint32 = math.MaxUint32
varUint32 uint32 = 123456789
)
const constUint32 uint32 = 123456789
func TestUint32Div(t *testing.T) {
checkUint32Div := func(x, y uint32) bool {
if y == 0 || y == 1 {
return true
}
d := NewUint32(y)
return (x / y) == d.Div(x)
}
if err := quick.Check(checkUint32Div, nil); err != nil {
t.Error(err)
}
}
func TestUint32Mod(t *testing.T) {
checkUint32Mod := func(x, y uint32) bool {
if y == 0 {
return true
}
d := NewUint32(y)
return (x % y) == d.Mod(x)
}
if err := quick.Check(checkUint32Mod, nil); err != nil {
t.Error(err)
}
}
func TestUint32DivMod(t *testing.T) {
checkUint32ModDiv := func(x, y uint32) bool {
if y == 0 {
return true
}
d := NewUint32(y)
q, r := d.DivMod(x)
return (x/y) == q && (x%y) == r
}
if err := quick.Check(checkUint32ModDiv, nil); err != nil {
t.Error(err)
}
}
func TestUint32Divisible(t *testing.T) {
checkUint32Divisible := func(x, y uint32) bool {
if x < y {
x, y = y, x
}
if y == 0 {
return true
}
d := NewUint32(y)
if ((x % y) == 0) != d.Divisible(x) {
return false
}
x, y = x&math.MaxUint16, y&math.MaxUint16
if x == 0 || y == 0 {
return true
}
d = NewUint32(y)
return d.Divisible(x * y)
}
if err := quick.Check(checkUint32Divisible, nil); err != nil {
t.Error(err)
}
}
func TestUint32DivSpeed(t *testing.T) {
if testing.Short() {
t.Skip("skipping speed test in short mode")
}
const samples = 100
results := testing.Benchmark(func(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < samples; j++ {
sinkUint32 = sinkUint32 / varUint32
}
}
})
baseline := float64(results.NsPerOp()) / float64(samples)
t.Logf("baseline: %2.2f ns/op", baseline)
benchmarkUint32Div := func(b *testing.B, n int) {
for i := 0; i < b.N; i++ {
d := NewUint32(varUint32)
for j := 0; j < n; j++ {
sinkUint32 = d.Div(sinkUint32)
}
}
}
sizes := []int{1, 2, 3, 5, 8, 10}
for _, s := range sizes {
results = testing.Benchmark(func(b *testing.B) {
benchmarkUint32Div(b, s)
})
nsPerOp := float64(results.NsPerOp()) / float64(s)
t.Logf(" %2d divs: %2.2f ns/op, faster: %v", s, nsPerOp, nsPerOp < baseline)
}
}
func TestUint32ModSpeed(t *testing.T) {
if testing.Short() {
t.Skip("skipping speed test in short mode")
}
const samples = 100
results := testing.Benchmark(func(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < samples; j++ {
sinkUint32 = sinkUint32 % varUint32
}
}
})
baseline := float64(results.NsPerOp()) / float64(samples)
t.Logf("baseline: %2.2f ns/op", baseline)
benchmarkUint32Mod := func(b *testing.B, n int) {
for i := 0; i < b.N; i++ {
d := NewUint32(varUint32)
for j := 0; j < n; j++ {
sinkUint32 = d.Mod(sinkUint32)
}
}
}
sizes := []int{1, 2, 3, 5, 8, 10}
for _, s := range sizes {
results = testing.Benchmark(func(b *testing.B) {
benchmarkUint32Mod(b, s)
})
nsPerOp := float64(results.NsPerOp()) / float64(s)
t.Logf(" %2d mods: %2.2f ns/op, faster: %v", s, nsPerOp, nsPerOp < baseline)
}
}
func BenchmarkUint32DivVar(b *testing.B) {
for i := 0; i < b.N; i++ {
sinkUint32 = sinkUint32 / varUint32
}
}
func BenchmarkUint32DivConst(b *testing.B) {
for i := 0; i < b.N; i++ {
sinkUint32 = sinkUint32 / constUint32
}
}
func BenchmarkUint32Div(b *testing.B) {
d := NewUint32(varUint32)
for i := 0; i < b.N; i++ {
sinkUint32 = d.Div(sinkUint32)
}
}
func BenchmarkUint32ModVar(b *testing.B) {
for i := 0; i < b.N; i++ {
sinkUint32 = sinkUint32 % varUint32
}
}
func BenchmarkUint32ModConst(b *testing.B) {
for i := 0; i < b.N; i++ {
sinkUint32 = sinkUint32 % constUint32
}
}
func BenchmarkUint32Mod(b *testing.B) {
d := NewUint32(varUint32)
for i := 0; i < b.N; i++ {
sinkUint32 = d.Mod(sinkUint32)
}
}
func BenchmarkUint32DivisibleVar(b *testing.B) {
for i := 0; i < b.N; i++ {
if (uint32(i) % varUint32) == 0 {
sinkUint32 = 2.0
} else {
sinkUint32 = 1.0
}
}
}
func BenchmarkUint32DivisibleConst(b *testing.B) {
for i := 0; i < b.N; i++ {
if (uint32(i) % constUint32) == 0 {
sinkUint32 = 2.0
} else {
sinkUint32 = 1.0
}
}
}
func BenchmarkUint32Divisible(b *testing.B) {
d := NewUint32(varUint32)
for i := 0; i < b.N; i++ {
if d.Divisible(uint32(i)) {
sinkUint32 = 2.0
} else {
sinkUint32 = 1.0
}
}
}