-
Notifications
You must be signed in to change notification settings - Fork 8
/
float16_bench_test.go
88 lines (72 loc) · 1.75 KB
/
float16_bench_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
// Copyright 2019 Montgomery Edwards⁴⁴⁸ and Faye Amacker
package float16_test
import (
"math"
"testing"
"github.com/x448/float16"
)
// prevent compiler optimizing out code by assigning to these
var resultF16 float16.Float16
var resultF32 float32
var resultStr string
var pcn float16.Precision
func BenchmarkFloat32pi(b *testing.B) {
result := float32(0)
pi32 := float32(math.Pi)
pi16 := float16.Fromfloat32(pi32)
for i := 0; i < b.N; i++ {
f16 := float16.Frombits(uint16(pi16))
result = f16.Float32()
}
resultF32 = result
}
func BenchmarkFrombits(b *testing.B) {
result := float16.Float16(0)
pi32 := float32(math.Pi)
pi16 := float16.Fromfloat32(pi32)
for i := 0; i < b.N; i++ {
result = float16.Frombits(uint16(pi16))
}
resultF16 = result
}
func BenchmarkFromFloat32pi(b *testing.B) {
result := float16.Float16(0)
pi := float32(math.Pi)
for i := 0; i < b.N; i++ {
result = float16.Fromfloat32(pi)
}
resultF16 = result
}
func BenchmarkFromFloat32nan(b *testing.B) {
result := float16.Float16(0)
nan := float32(math.NaN())
for i := 0; i < b.N; i++ {
result = float16.Fromfloat32(nan)
}
resultF16 = result
}
func BenchmarkFromFloat32subnorm(b *testing.B) {
result := float16.Float16(0)
subnorm := math.Float32frombits(0x007fffff)
for i := 0; i < b.N; i++ {
result = float16.Fromfloat32(subnorm)
}
resultF16 = result
}
func BenchmarkPrecisionFromFloat32(b *testing.B) {
var result float16.Precision
for i := 0; i < b.N; i++ {
f32 := float32(0.00001) + float32(0.00001)
result = float16.PrecisionFromfloat32(f32)
}
pcn = result
}
func BenchmarkString(b *testing.B) {
var result string
pi32 := float32(math.Pi)
pi16 := float16.Fromfloat32(pi32)
for i := 0; i < b.N; i++ {
result = pi16.String()
}
resultStr = result
}