|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +// TestBFloat16_SpecialValues tests bfloat16 special values (zero, infinity, NaN). |
| 11 | +func TestBFloat16_SpecialValues(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + bf16 BFloat16 |
| 15 | + expected float32 |
| 16 | + checkInf bool // If true, check infinity instead of exact value. |
| 17 | + checkNaN bool // If true, check NaN. |
| 18 | + }{ |
| 19 | + {"Zero", BFloat16(0x0000), 0.0, false, false}, |
| 20 | + //nolint:staticcheck // SA4026: Testing negative zero representation. |
| 21 | + {"NegativeZero", BFloat16(0x8000), -0.0, false, false}, |
| 22 | + {"One", BFloat16(0x3F80), 1.0, false, false}, |
| 23 | + {"NegativeOne", BFloat16(0xBF80), -1.0, false, false}, |
| 24 | + {"PositiveInfinity", BFloat16(0x7F80), float32(math.Inf(1)), true, false}, |
| 25 | + {"NegativeInfinity", BFloat16(0xFF80), float32(math.Inf(-1)), true, false}, |
| 26 | + {"NaN", BFloat16(0x7FC0), float32(math.NaN()), false, true}, |
| 27 | + } |
| 28 | + |
| 29 | + for _, tc := range tests { |
| 30 | + t.Run(tc.name, func(t *testing.T) { |
| 31 | + result := tc.bf16.ToFloat32() |
| 32 | + switch { |
| 33 | + case tc.checkInf && math.IsInf(float64(tc.expected), 1): |
| 34 | + assert.True(t, math.IsInf(float64(result), 1), "expected +Inf") |
| 35 | + case tc.checkInf && math.IsInf(float64(tc.expected), -1): |
| 36 | + assert.True(t, math.IsInf(float64(result), -1), "expected -Inf") |
| 37 | + case tc.checkNaN: |
| 38 | + assert.True(t, math.IsNaN(float64(result)), "expected NaN") |
| 39 | + default: |
| 40 | + assert.Equal(t, tc.expected, result, "bf16=%04x", tc.bf16) |
| 41 | + } |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// TestBFloat16_RoundTrip tests bfloat16 round-trip conversion. |
| 47 | +func TestBFloat16_RoundTrip(t *testing.T) { |
| 48 | + values := []float32{0.0, 1.0, -1.0, 0.5, -0.5, 2.0, 100.0, 12345.0, 3.14159, -273.15, 0.001, 1000000.0} |
| 49 | + |
| 50 | + for _, v := range values { |
| 51 | + bf16 := Float32ToBFloat16(v) |
| 52 | + result := bf16.ToFloat32() |
| 53 | + |
| 54 | + // bfloat16 has ~2 decimal digits precision. |
| 55 | + // Allow 1% error for most values, 5% for very small values. |
| 56 | + if v != 0 { |
| 57 | + relativeError := math.Abs(float64(result-v)) / math.Abs(float64(v)) |
| 58 | + if math.Abs(float64(v)) < 0.01 { |
| 59 | + assert.Less(t, relativeError, 0.05, "value=%f, bf16=%04x, result=%f", v, bf16, result) |
| 60 | + } else { |
| 61 | + assert.Less(t, relativeError, 0.01, "value=%f, bf16=%04x, result=%f", v, bf16, result) |
| 62 | + } |
| 63 | + } else { |
| 64 | + assert.Equal(t, float32(0.0), result) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// TestBFloat16_SpecialConversions tests bfloat16 conversions of special values. |
| 70 | +func TestBFloat16_SpecialConversions(t *testing.T) { |
| 71 | + tests := []struct { |
| 72 | + name string |
| 73 | + input float32 |
| 74 | + expected BFloat16 |
| 75 | + }{ |
| 76 | + {"Zero", 0.0, BFloat16(0x0000)}, |
| 77 | + {"NegativeZero", float32(math.Copysign(0, -1)), BFloat16(0x8000)}, |
| 78 | + {"One", 1.0, BFloat16(0x3F80)}, |
| 79 | + {"NegativeOne", -1.0, BFloat16(0xBF80)}, |
| 80 | + {"PositiveInfinity", float32(math.Inf(1)), BFloat16(0x7F80)}, |
| 81 | + {"NegativeInfinity", float32(math.Inf(-1)), BFloat16(0xFF80)}, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tc := range tests { |
| 85 | + t.Run(tc.name, func(t *testing.T) { |
| 86 | + result := Float32ToBFloat16(tc.input) |
| 87 | + assert.Equal(t, tc.expected, result, "input=%f", tc.input) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// TestBFloat16_NaN tests bfloat16 NaN handling. |
| 93 | +func TestBFloat16_NaN(t *testing.T) { |
| 94 | + input := float32(math.NaN()) |
| 95 | + bf16 := Float32ToBFloat16(input) |
| 96 | + result := bf16.ToFloat32() |
| 97 | + assert.True(t, math.IsNaN(float64(result)), "expected NaN") |
| 98 | +} |
| 99 | + |
| 100 | +// TestBFloat16_Rounding tests bfloat16 rounding to nearest even. |
| 101 | +func TestBFloat16_Rounding(t *testing.T) { |
| 102 | + // Test rounding behavior. |
| 103 | + // 1.0 + epsilon (very small) should round to 1.0. |
| 104 | + input := float32(1.0) + float32(0.0001) |
| 105 | + bf16 := Float32ToBFloat16(input) |
| 106 | + result := bf16.ToFloat32() |
| 107 | + assert.InDelta(t, 1.0, result, 0.01, "expected rounding to 1.0") |
| 108 | + |
| 109 | + // 1.5 should round to 1.5 (if representable) or close. |
| 110 | + input = float32(1.5) |
| 111 | + bf16 = Float32ToBFloat16(input) |
| 112 | + result = bf16.ToFloat32() |
| 113 | + assert.InDelta(t, 1.5, result, 0.01, "expected rounding to 1.5") |
| 114 | +} |
| 115 | + |
| 116 | +// TestBFloat16_Encode tests bfloat16 encoding to bytes. |
| 117 | +func TestBFloat16_Encode(t *testing.T) { |
| 118 | + tests := []struct { |
| 119 | + name string |
| 120 | + bf16 BFloat16 |
| 121 | + expected []byte |
| 122 | + }{ |
| 123 | + {"Zero", BFloat16(0x0000), []byte{0x00, 0x00}}, |
| 124 | + {"One", BFloat16(0x3F80), []byte{0x80, 0x3F}}, // Little-endian. |
| 125 | + {"NegativeOne", BFloat16(0xBF80), []byte{0x80, 0xBF}}, |
| 126 | + {"Infinity", BFloat16(0x7F80), []byte{0x80, 0x7F}}, |
| 127 | + } |
| 128 | + |
| 129 | + for _, tc := range tests { |
| 130 | + t.Run(tc.name, func(t *testing.T) { |
| 131 | + result := tc.bf16.Encode() |
| 132 | + assert.Equal(t, tc.expected, result, "bf16=%04x", tc.bf16) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// TestBFloat16_Decode tests bfloat16 decoding from bytes. |
| 138 | +func TestBFloat16_Decode(t *testing.T) { |
| 139 | + tests := []struct { |
| 140 | + name string |
| 141 | + data []byte |
| 142 | + expected BFloat16 |
| 143 | + }{ |
| 144 | + {"Zero", []byte{0x00, 0x00}, BFloat16(0x0000)}, |
| 145 | + {"One", []byte{0x80, 0x3F}, BFloat16(0x3F80)}, // Little-endian. |
| 146 | + {"NegativeOne", []byte{0x80, 0xBF}, BFloat16(0xBF80)}, |
| 147 | + {"Infinity", []byte{0x80, 0x7F}, BFloat16(0x7F80)}, |
| 148 | + } |
| 149 | + |
| 150 | + for _, tc := range tests { |
| 151 | + t.Run(tc.name, func(t *testing.T) { |
| 152 | + result := DecodeBFloat16(tc.data) |
| 153 | + assert.Equal(t, tc.expected, result, "data=%v", tc.data) |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// TestBFloat16_Range tests bfloat16 dynamic range (same as float32). |
| 159 | +func TestBFloat16_Range(t *testing.T) { |
| 160 | + // bfloat16 has same dynamic range as float32 (8-bit exponent). |
| 161 | + // Test large values. |
| 162 | + largeValue := float32(1e20) |
| 163 | + bf16 := Float32ToBFloat16(largeValue) |
| 164 | + result := bf16.ToFloat32() |
| 165 | + assert.InDelta(t, largeValue, result, float64(largeValue)*0.01, "expected large value preserved") |
| 166 | + |
| 167 | + // Test small values. |
| 168 | + smallValue := float32(1e-20) |
| 169 | + bf16 = Float32ToBFloat16(smallValue) |
| 170 | + result = bf16.ToFloat32() |
| 171 | + assert.InDelta(t, smallValue, result, float64(smallValue)*0.05, "expected small value preserved") |
| 172 | +} |
| 173 | + |
| 174 | +// TestBFloat16_Precision tests bfloat16 precision (~2 decimal digits). |
| 175 | +func TestBFloat16_Precision(t *testing.T) { |
| 176 | + // bfloat16 has 7-bit mantissa (vs 23-bit in float32). |
| 177 | + // This gives ~2 decimal digits precision. |
| 178 | + // Test that values differing in later digits are indistinguishable. |
| 179 | + input1 := float32(1.23) |
| 180 | + input2 := float32(1.24) |
| 181 | + |
| 182 | + bf16_1 := Float32ToBFloat16(input1) |
| 183 | + bf16_2 := Float32ToBFloat16(input2) |
| 184 | + |
| 185 | + result1 := bf16_1.ToFloat32() |
| 186 | + result2 := bf16_2.ToFloat32() |
| 187 | + |
| 188 | + // Both should be close (precision limited). |
| 189 | + diff := math.Abs(float64(result1 - result2)) |
| 190 | + assert.Less(t, diff, 0.02, "expected limited precision") |
| 191 | +} |
| 192 | + |
| 193 | +// TestBFloat16_EdgeCases tests bfloat16 edge cases. |
| 194 | +func TestBFloat16_EdgeCases(t *testing.T) { |
| 195 | + // Test denormal (subnormal) numbers. |
| 196 | + // bfloat16 subnormals are very small (exp=0, mantissa≠0). |
| 197 | + denormal := BFloat16(0x0001) // exp=0, mant=1. |
| 198 | + result := denormal.ToFloat32() |
| 199 | + assert.Greater(t, result, float32(0.0), "expected non-zero denormal") |
| 200 | + assert.Less(t, result, float32(1e-30), "expected very small denormal") |
| 201 | + |
| 202 | + // Test negative denormal. |
| 203 | + negativeDenormal := BFloat16(0x8001) // sign=1, exp=0, mant=1. |
| 204 | + result = negativeDenormal.ToFloat32() |
| 205 | + assert.Less(t, result, float32(0.0), "expected negative denormal") |
| 206 | + assert.Greater(t, result, float32(-1e-30), "expected very small negative denormal") |
| 207 | +} |
0 commit comments