-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath_test.go
156 lines (144 loc) · 2.85 KB
/
math_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
package fastmath_test
import (
"fmt"
"math"
"testing"
"github.com/13rac1/fastmath"
)
func TestQAdd8(t *testing.T) {
if fastmath.QAdd8(255, 128) != 255 {
t.Fatal("QAdd8() is not saturating")
}
}
func TestQSub8(t *testing.T) {
if fastmath.QSub8(128, 255) != 0 {
t.Fatal("QSub8() is not saturating")
}
}
func TestQMul8(t *testing.T) {
if fastmath.QMul8(255, 255) != 255 {
t.Fatal("QMul8() is not saturating")
}
}
func TestAbs8(t *testing.T) {
if fastmath.Abs8(-100) != 100 {
t.Fatal("Abs8() absolute value not found")
}
}
func TestSqrt16(t *testing.T) {
testCases := []struct {
in uint16
expected uint8
}{{
in: 0,
expected: 0,
}, {
in: 2,
expected: 1,
}, {
in: 255,
expected: 15,
}, {
in: 128,
expected: 11,
}, {
in: 2500,
expected: 50,
}, {
in: 5000,
expected: 70,
}, {
in: 7500,
expected: 86,
}, {
in: 10000,
expected: 100,
}}
for _, test := range testCases {
name := fmt.Sprintf("Sqrt16(%d)", test.in)
t.Run(name, func(t *testing.T) {
r := fastmath.Sqrt16(test.in)
if test.expected != r {
t.Fatalf("expected: %d, found: %d", test.expected, r)
}
})
}
}
const (
mask = 0x7FF
shift = 64 - 11 - 1
bias = 1023
)
// Copied from https://golang.org/src/math/sqrt.go to avoid compiler
// optimizations into a single assembly instruction on many architectures.
// Copyright 2009 The Go Authors. All rights reserved.
func sqrt(x float64) float64 {
// special cases
switch {
case x == 0 || math.IsNaN(x) || math.IsInf(x, 1):
return x
case x < 0:
return math.NaN()
}
ix := math.Float64bits(x)
// normalize x
exp := int((ix >> shift) & mask)
if exp == 0 { // subnormal x
for ix&(1<<shift) == 0 {
ix <<= 1
exp--
}
exp++
}
exp -= bias // unbias exponent
ix &^= mask << shift
ix |= 1 << shift
if exp&1 == 1 { // odd exp, double x to make it even
ix <<= 1
}
exp >>= 1 // exp = exp/2, exponent of square root
// generate sqrt(x) bit by bit
ix <<= 1
var q, s uint64 // q = sqrt(x)
r := uint64(1 << (shift + 1)) // r = moving bit from MSB to LSB
for r != 0 {
t := s + r
if t <= ix {
s = t + r
ix -= t
q += r
}
ix <<= 1
r >>= 1
}
// final rounding
if ix != 0 { // remainder, result not exact
q += q & 1 // round according to extra bit
}
ix = q>>1 + uint64(exp-1+bias)<<shift // significand + biased exponent
return math.Float64frombits(ix)
}
func BenchmarkStdLibFallbackSqrt(b *testing.B) {
var r uint8
x := fastmath.PI16
for n := 0; n < b.N; n++ {
r = uint8(sqrt(float64(x)))
}
result8 = r
}
func BenchmarkStdLibDefaultSqrt(b *testing.B) {
var r uint8
x := fastmath.PI16
for n := 0; n < b.N; n++ {
r = uint8(math.Sqrt(float64(x)))
}
result8 = r
}
func BenchmarkSqrt16(b *testing.B) {
var r uint8
x := fastmath.PI16
for n := 0; n < b.N; n++ {
r = fastmath.Sqrt16(x)
}
result8 = r
}