|
| 1 | +package float8 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +// BenchmarkFromFloat32 benchmarks float32 → Float8 conversion. |
| 8 | +func BenchmarkFromFloat32(b *testing.B) { |
| 9 | + b.Run("Normal", func(b *testing.B) { |
| 10 | + f32 := float32(1.5) |
| 11 | + for i := 0; i < b.N; i++ { |
| 12 | + _ = ToFloat8(f32) |
| 13 | + } |
| 14 | + }) |
| 15 | + b.Run("Subnormal", func(b *testing.B) { |
| 16 | + f32 := float32(0.001953125) // smallest normal float8 boundary |
| 17 | + for i := 0; i < b.N; i++ { |
| 18 | + _ = ToFloat8(f32) |
| 19 | + } |
| 20 | + }) |
| 21 | + b.Run("Zero", func(b *testing.B) { |
| 22 | + f32 := float32(0.0) |
| 23 | + for i := 0; i < b.N; i++ { |
| 24 | + _ = ToFloat8(f32) |
| 25 | + } |
| 26 | + }) |
| 27 | + b.Run("Large", func(b *testing.B) { |
| 28 | + f32 := float32(448.0) // max finite float8 |
| 29 | + for i := 0; i < b.N; i++ { |
| 30 | + _ = ToFloat8(f32) |
| 31 | + } |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +// BenchmarkToFloat32_Modes benchmarks Float8 → float32 conversion with |
| 36 | +// algorithmic and lookup-table paths. |
| 37 | +func BenchmarkToFloat32_Modes(b *testing.B) { |
| 38 | + f8 := ToFloat8(1.5) |
| 39 | + |
| 40 | + b.Run("Algorithmic", func(b *testing.B) { |
| 41 | + DisableFastConversion() |
| 42 | + b.ResetTimer() |
| 43 | + for i := 0; i < b.N; i++ { |
| 44 | + _ = f8.ToFloat32() |
| 45 | + } |
| 46 | + }) |
| 47 | + b.Run("Lookup", func(b *testing.B) { |
| 48 | + EnableFastConversion() |
| 49 | + b.ResetTimer() |
| 50 | + for i := 0; i < b.N; i++ { |
| 51 | + _ = f8.ToFloat32() |
| 52 | + } |
| 53 | + b.StopTimer() |
| 54 | + DisableFastConversion() |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +// BenchmarkAddModes benchmarks addition with algorithmic and lookup-table paths. |
| 59 | +func BenchmarkAddModes(b *testing.B) { |
| 60 | + a := ToFloat8(1.5) |
| 61 | + c := ToFloat8(2.5) |
| 62 | + |
| 63 | + b.Run("Algorithmic", func(b *testing.B) { |
| 64 | + DisableFastArithmetic() |
| 65 | + b.ResetTimer() |
| 66 | + for i := 0; i < b.N; i++ { |
| 67 | + _ = Add(a, c) |
| 68 | + } |
| 69 | + }) |
| 70 | + b.Run("Lookup", func(b *testing.B) { |
| 71 | + EnableFastArithmetic() |
| 72 | + b.ResetTimer() |
| 73 | + for i := 0; i < b.N; i++ { |
| 74 | + _ = Add(a, c) |
| 75 | + } |
| 76 | + b.StopTimer() |
| 77 | + DisableFastArithmetic() |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +// BenchmarkMulModes benchmarks multiplication with algorithmic and lookup-table paths. |
| 82 | +func BenchmarkMulModes(b *testing.B) { |
| 83 | + a := ToFloat8(1.5) |
| 84 | + c := ToFloat8(2.5) |
| 85 | + |
| 86 | + b.Run("Algorithmic", func(b *testing.B) { |
| 87 | + DisableFastArithmetic() |
| 88 | + b.ResetTimer() |
| 89 | + for i := 0; i < b.N; i++ { |
| 90 | + _ = Mul(a, c) |
| 91 | + } |
| 92 | + }) |
| 93 | + b.Run("Lookup", func(b *testing.B) { |
| 94 | + EnableFastArithmetic() |
| 95 | + b.ResetTimer() |
| 96 | + for i := 0; i < b.N; i++ { |
| 97 | + _ = Mul(a, c) |
| 98 | + } |
| 99 | + b.StopTimer() |
| 100 | + DisableFastArithmetic() |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +// BenchmarkSub benchmarks subtraction. |
| 105 | +func BenchmarkSub(b *testing.B) { |
| 106 | + a := ToFloat8(3.5) |
| 107 | + c := ToFloat8(1.5) |
| 108 | + |
| 109 | + b.Run("Algorithmic", func(b *testing.B) { |
| 110 | + DisableFastArithmetic() |
| 111 | + b.ResetTimer() |
| 112 | + for i := 0; i < b.N; i++ { |
| 113 | + _ = Sub(a, c) |
| 114 | + } |
| 115 | + }) |
| 116 | + b.Run("Lookup", func(b *testing.B) { |
| 117 | + EnableFastArithmetic() |
| 118 | + b.ResetTimer() |
| 119 | + for i := 0; i < b.N; i++ { |
| 120 | + _ = Sub(a, c) |
| 121 | + } |
| 122 | + b.StopTimer() |
| 123 | + DisableFastArithmetic() |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +// BenchmarkDiv benchmarks division. |
| 128 | +func BenchmarkDiv(b *testing.B) { |
| 129 | + a := ToFloat8(3.5) |
| 130 | + c := ToFloat8(1.5) |
| 131 | + |
| 132 | + b.Run("Algorithmic", func(b *testing.B) { |
| 133 | + DisableFastArithmetic() |
| 134 | + b.ResetTimer() |
| 135 | + for i := 0; i < b.N; i++ { |
| 136 | + _ = Div(a, c) |
| 137 | + } |
| 138 | + }) |
| 139 | + b.Run("Lookup", func(b *testing.B) { |
| 140 | + EnableFastArithmetic() |
| 141 | + b.ResetTimer() |
| 142 | + for i := 0; i < b.N; i++ { |
| 143 | + _ = Div(a, c) |
| 144 | + } |
| 145 | + b.StopTimer() |
| 146 | + DisableFastArithmetic() |
| 147 | + }) |
| 148 | +} |
0 commit comments