-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuint64_test.go
More file actions
218 lines (193 loc) · 4.33 KB
/
uint64_test.go
File metadata and controls
218 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
package fastdiv
import (
"math"
"testing"
"testing/quick"
)
var (
sinkUint64 uint64 = math.MaxUint64
varUint64 uint64 = 123456789
)
const constUint64 uint64 = 123456789
func TestUint64Div(t *testing.T) {
checkUint64Div := func(x, y uint64) bool {
if y == 0 || y == 1 {
return true
}
d := NewUint64(y)
return (x / y) == d.Div(x)
}
if err := quick.Check(checkUint64Div, nil); err != nil {
t.Error(err)
}
}
func TestUint64Mod(t *testing.T) {
checkUint64Mod := func(x, y uint64) bool {
if y == 0 {
return true
}
d := NewUint64(y)
return (x % y) == d.Mod(x)
}
if err := quick.Check(checkUint64Mod, nil); err != nil {
t.Error(err)
}
}
func TestUint64DivMod(t *testing.T) {
checkUint64ModDiv := func(x, y uint64) bool {
if y == 0 {
return true
}
d := NewUint64(y)
q, r := d.DivMod(x)
return (x/y) == q && (x%y) == r
}
if err := quick.Check(checkUint64ModDiv, nil); err != nil {
t.Error(err)
}
}
func TestUint64Divisible(t *testing.T) {
checkUint64Divisible := func(x, y uint64) bool {
if x < y {
x, y = y, x
}
if y == 0 {
return true
}
d := NewUint64(y)
if ((x % y) == 0) != d.Divisible(x) {
return false
}
x, y = x&math.MaxUint32, y&math.MaxUint32
if x == 0 || y == 0 {
return true
}
d = NewUint64(y)
return d.Divisible(x * y)
}
if err := quick.Check(checkUint64Divisible, nil); err != nil {
t.Error(err)
}
}
func TestUint64DivSpeed(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++ {
sinkUint64 = sinkUint64 / varUint64
}
}
})
baseline := float64(results.NsPerOp()) / float64(samples)
t.Logf("baseline: %2.2f ns/op", baseline)
benchmarkUint64Div := func(b *testing.B, n int) {
for i := 0; i < b.N; i++ {
d := NewUint64(varUint64)
for j := 0; j < n; j++ {
sinkUint64 = d.Div(sinkUint64)
}
}
}
sizes := []int{1, 2, 3, 5, 8, 10}
for _, s := range sizes {
results = testing.Benchmark(func(b *testing.B) {
benchmarkUint64Div(b, s)
})
nsPerOp := float64(results.NsPerOp()) / float64(s)
t.Logf(" %2d divs: %2.2f ns/op, faster: %v", s, nsPerOp, nsPerOp < baseline)
}
}
func TestUint64ModSpeed(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++ {
sinkUint64 = sinkUint64 % varUint64
}
}
})
baseline := float64(results.NsPerOp()) / float64(samples)
t.Logf("baseline: %2.2f ns/op", baseline)
benchmarkUint64Mod := func(b *testing.B, n int) {
for i := 0; i < b.N; i++ {
d := NewUint64(varUint64)
for j := 0; j < n; j++ {
sinkUint64 = d.Mod(sinkUint64)
}
}
}
sizes := []int{1, 2, 3, 5, 8, 10}
for _, s := range sizes {
results = testing.Benchmark(func(b *testing.B) {
benchmarkUint64Mod(b, s)
})
nsPerOp := float64(results.NsPerOp()) / float64(s)
t.Logf(" %2d mods: %2.2f ns/op, faster: %v", s, nsPerOp, nsPerOp < baseline)
}
}
func BenchmarkUint64DivVar(b *testing.B) {
for i := 0; i < b.N; i++ {
sinkUint64 = sinkUint64 / varUint64
}
}
func BenchmarkUint64DivConst(b *testing.B) {
for i := 0; i < b.N; i++ {
sinkUint64 = sinkUint64 / constUint64
}
}
func BenchmarkUint64Div(b *testing.B) {
d := NewUint64(varUint64)
for i := 0; i < b.N; i++ {
sinkUint64 = d.Div(sinkUint64)
}
}
func BenchmarkUint64ModVar(b *testing.B) {
for i := 0; i < b.N; i++ {
sinkUint64 = sinkUint64 % varUint64
}
}
func BenchmarkUint64ModConst(b *testing.B) {
for i := 0; i < b.N; i++ {
sinkUint64 = sinkUint64 % constUint64
}
}
func BenchmarkUint64Mod(b *testing.B) {
d := NewUint64(varUint64)
for i := 0; i < b.N; i++ {
sinkUint64 = d.Mod(sinkUint64)
}
}
func BenchmarkUint64DivisibleVar(b *testing.B) {
for i := 0; i < b.N; i++ {
if (uint64(i) % varUint64) == 0 {
sinkUint64 = 2.0
} else {
sinkUint64 = 1.0
}
}
}
func BenchmarkUint64DivisibleConst(b *testing.B) {
for i := 0; i < b.N; i++ {
if (uint64(i) % constUint64) == 0 {
sinkUint64 = 2.0
} else {
sinkUint64 = 1.0
}
}
}
func BenchmarkUint64Divisible(b *testing.B) {
d := NewUint64(varUint64)
for i := 0; i < b.N; i++ {
if d.Divisible(uint64(i)) {
sinkUint64 = 2.0
} else {
sinkUint64 = 1.0
}
}
}