-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
148 lines (136 loc) · 2.96 KB
/
benchmark_test.go
File metadata and controls
148 lines (136 loc) · 2.96 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
package float8
import (
"testing"
)
// BenchmarkFromFloat32 benchmarks float32 → Float8 conversion.
func BenchmarkFromFloat32(b *testing.B) {
b.Run("Normal", func(b *testing.B) {
f32 := float32(1.5)
for i := 0; i < b.N; i++ {
_ = ToFloat8(f32)
}
})
b.Run("Subnormal", func(b *testing.B) {
f32 := float32(0.001953125) // smallest normal float8 boundary
for i := 0; i < b.N; i++ {
_ = ToFloat8(f32)
}
})
b.Run("Zero", func(b *testing.B) {
f32 := float32(0.0)
for i := 0; i < b.N; i++ {
_ = ToFloat8(f32)
}
})
b.Run("Large", func(b *testing.B) {
f32 := float32(448.0) // max finite float8
for i := 0; i < b.N; i++ {
_ = ToFloat8(f32)
}
})
}
// BenchmarkToFloat32_Modes benchmarks Float8 → float32 conversion with
// algorithmic and lookup-table paths.
func BenchmarkToFloat32_Modes(b *testing.B) {
f8 := ToFloat8(1.5)
b.Run("Algorithmic", func(b *testing.B) {
DisableFastConversion()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = f8.ToFloat32()
}
})
b.Run("Lookup", func(b *testing.B) {
EnableFastConversion()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = f8.ToFloat32()
}
b.StopTimer()
DisableFastConversion()
})
}
// BenchmarkAddModes benchmarks addition with algorithmic and lookup-table paths.
func BenchmarkAddModes(b *testing.B) {
a := ToFloat8(1.5)
c := ToFloat8(2.5)
b.Run("Algorithmic", func(b *testing.B) {
DisableFastArithmetic()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Add(a, c)
}
})
b.Run("Lookup", func(b *testing.B) {
EnableFastArithmetic()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Add(a, c)
}
b.StopTimer()
DisableFastArithmetic()
})
}
// BenchmarkMulModes benchmarks multiplication with algorithmic and lookup-table paths.
func BenchmarkMulModes(b *testing.B) {
a := ToFloat8(1.5)
c := ToFloat8(2.5)
b.Run("Algorithmic", func(b *testing.B) {
DisableFastArithmetic()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Mul(a, c)
}
})
b.Run("Lookup", func(b *testing.B) {
EnableFastArithmetic()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Mul(a, c)
}
b.StopTimer()
DisableFastArithmetic()
})
}
// BenchmarkSub benchmarks subtraction.
func BenchmarkSub(b *testing.B) {
a := ToFloat8(3.5)
c := ToFloat8(1.5)
b.Run("Algorithmic", func(b *testing.B) {
DisableFastArithmetic()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Sub(a, c)
}
})
b.Run("Lookup", func(b *testing.B) {
EnableFastArithmetic()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Sub(a, c)
}
b.StopTimer()
DisableFastArithmetic()
})
}
// BenchmarkDiv benchmarks division.
func BenchmarkDiv(b *testing.B) {
a := ToFloat8(3.5)
c := ToFloat8(1.5)
b.Run("Algorithmic", func(b *testing.B) {
DisableFastArithmetic()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Div(a, c)
}
})
b.Run("Lookup", func(b *testing.B) {
EnableFastArithmetic()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Div(a, c)
}
b.StopTimer()
DisableFastArithmetic()
})
}