/
cbrt_test.go
217 lines (202 loc) · 4.3 KB
/
cbrt_test.go
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
package intmath
import (
"github.com/plumbum/intmath/i32"
"github.com/plumbum/intmath/i64"
"github.com/plumbum/intmath/intgr"
"github.com/plumbum/intmath/u32"
"github.com/plumbum/intmath/u64"
"github.com/plumbum/intmath/uintgr"
"math"
"testing"
)
func BenchmarkCbrtIntgr(b *testing.B) {
k := uint(1)
if k<<32 != 0 {
k = 32
} else {
k = 0
}
for i := 0; i < b.N; i++ {
for j := 0x10000; j < 0x4000000; j += 0x10000 {
_ = intgr.Cbrt(j << k)
}
}
}
func BenchmarkCbrtUintgr(b *testing.B) {
k := uint(1)
if k<<32 != 0 {
k = 32
} else {
k = 0
}
for i := 0; i < b.N; i++ {
for j := uint(0x10000); j < 0x4000000; j += 0x10000 {
_ = uintgr.Cbrt(j << k)
}
}
}
func BenchmarkCbrtInt32(b *testing.B) {
k := uint(0) //to prevent unfair advantage over other benchmarks
for i := 0; i < b.N; i++ {
for j := int32(0x10000); j < 0x4000000; j += 0x10000 {
_ = i32.Cbrt(j << k)
}
}
}
func BenchmarkCbrtUint32(b *testing.B) {
k := uint(0) //to prevent unfair advantage over other benchmarks
for i := 0; i < b.N; i++ {
for j := uint32(0x10000); j < 0x4000000; j += 0x10000 {
_ = u32.Cbrt(j << k)
}
}
}
func BenchmarkCbrtInt64(b *testing.B) {
k := uint(32)
for i := 0; i < b.N; i++ {
for j := int64(0x10000); j < 0x4000000; j += 0x10000 {
_ = i64.Cbrt(j << k)
}
}
}
func BenchmarkCbrtUint64(b *testing.B) {
k := uint(32)
for i := 0; i < b.N; i++ {
for j := uint64(0x10000); j < 0x4000000; j += 0x10000 {
_ = u64.Cbrt(j << k)
}
}
}
func BenchmarkCbrtFloat64ToInt(b *testing.B) {
k := uint(1)
if k<<32 != 0 {
k = 32
} else {
k = 0
}
for i := 0; i < b.N; i++ {
for j := 0x10000; j < 0x4000000; j += 0x10000 {
_ = int(math.Cbrt(float64(j << k)))
}
}
}
func BenchmarkCbrtFloat64ToUint(b *testing.B) {
k := uint(1)
if k<<32 != 0 {
k = 32
} else {
k = 0
}
for i := 0; i < b.N; i++ {
for j := uint(0x10000); j < 0x4000000; j += 0x10000 {
_ = uint(math.Cbrt(float64(j << k)))
}
}
}
func BenchmarkCbrtFloat64ToInt32(b *testing.B) {
k := uint(0)
for i := 0; i < b.N; i++ {
for j := int32(0x10000); j < 0x4000000; j += 0x10000 {
_ = int32(math.Cbrt(float64(j << k)))
}
}
}
func BenchmarkCbrtFloat64ToUint32(b *testing.B) {
k := uint(0)
for i := 0; i < b.N; i++ {
for j := uint32(0x10000); j < 0x4000000; j += 0x10000 {
_ = uint32(math.Cbrt(float64(j << k)))
}
}
}
func BenchmarkCbrtFloat64ToInt64(b *testing.B) {
k := uint(32)
for i := 0; i < b.N; i++ {
for j := int64(0x10000); j < 0x4000000; j += 0x10000 {
_ = int64(math.Cbrt(float64(j << k)))
}
}
}
func BenchmarkCbrtFloat64ToUint64(b *testing.B) {
k := uint(32)
for i := 0; i < b.N; i++ {
for j := uint64(0x10000); j < 0x4000000; j += 0x10000 {
_ = uint64(math.Cbrt(float64(j << k)))
}
}
}
func TestCrt(t *testing.T) {
t.Logf("Testing Crt\n")
// 32 bits -> 10 bits max number
// 64 bits -> 21 bits max number
i32Max := int32(1<<10 - 1)
i64Max := int64(1<<21 - 1)
u32Max := uint32(1<<10 - 1)
u64Max := uint64(1<<21 - 1)
iMax := 1
var uMax uint
if iMax<<32 == 0 {
iMax = int(i32Max)
uMax = uint(u32Max)
} else {
iMax = int(i64Max)
uMax = uint(u64Max)
}
for i := iMax; i > 0; i >>= 1 {
k := intgr.Cbrt(i * i * i)
if k != i {
t.Logf("intgr.Cbrt(%X*%X*%X) == %X\n", i, i, i, k)
t.Fail()
}
k = intgr.Cbrt(-(i * i * i))
if k != -i {
t.Logf("intgr.Cbrt(%X*%X*%X) == %X\n", -i, -i, -i, k)
t.Fail()
}
}
for i := uMax; i > 0; i >>= 1 {
k := uintgr.Cbrt(i * i * i)
if k != i {
t.Logf("uintgr.Cbrt(%X*%X*%X) == %X\n", i, i, i, k)
t.Fail()
}
}
for i := i32Max; i > 0; i >>= 1 {
k := i32.Cbrt(i * i * i)
if k != i {
t.Logf("i32.Cbrt(%X*%X*%X) == %X\n", i, i, i, k)
t.Fail()
}
k = i32.Cbrt(-(i * i * i))
if k != -i {
t.Logf("i32.Cbrt(%X*%X*%X) == %X\n", -i, -i, -i, k)
t.Fail()
}
}
for i := u32Max; i > 0; i >>= 1 {
k := u32.Cbrt(i * i * i)
if k != i {
t.Logf("u32.Cbrt(%X*%X*%X) == %X\n", i, i, i, k)
t.Fail()
}
}
for i := i64Max; i > 0; i >>= 1 {
k := i64.Cbrt(i * i * i)
if k != i {
t.Logf("i64.Cbrt(%X*%X*%X) == %X\n", i, i, i, k)
t.Fail()
}
k = i64.Cbrt(-(i * i * i))
if k != -i {
t.Logf("i64.Cbrt(%X*%X*%X) == %X\n", -i, -i, -i, k)
t.Fail()
}
}
for i := u64Max; i > 0; i >>= 1 {
k := u64.Cbrt(i * i * i)
if k != i {
t.Logf("u64.Cbrt(%X*%X*%X) == %X\n", i, i, i, k)
t.Fail()
}
}
}