Skip to content

Commit 2340c9a

Browse files
committed
refactor(format): Apply formatting and incorporate developer changes
1 parent f007fc6 commit 2340c9a

10 files changed

Lines changed: 71 additions & 65 deletions

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ race:
2121
vet:
2222
GOWORK=off go vet $(PKG)
2323

24-
# Auto-format code
25-
fmt:
26-
gofmt -s -w .
24+
format:
25+
@echo "🎨 Applying code formatters..."
26+
@echo " - Standard Go formatting..."
27+
@gofmt -w .
28+
@echo " - Organizing imports..."
29+
@goimports -w .
30+
@echo " - Strict formatting with gofumpt..."
31+
@gofumpt -w . 2>/dev/null || echo " (gofumpt not available, skipping)"
32+
@echo "✅ Code formatting complete"
2733

2834
# Check formatting without modifying files; fails if formatting needed
2935
fmt-check:

arithmetic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func ScaleSlice(s []Float16, scalar Float16) []Float16 {
525525

526526
// SumSlice returns the sum of all elements in the slice
527527
func SumSlice(s []Float16) Float16 {
528-
var sum Float16 = PositiveZero
528+
sum := PositiveZero
529529
for _, v := range s {
530530
sum = Add(sum, v)
531531
}
@@ -538,7 +538,7 @@ func DotProduct(a, b []Float16) Float16 {
538538
panic("float16: slice length mismatch")
539539
}
540540

541-
var sum Float16 = PositiveZero
541+
sum := PositiveZero
542542
for i := range a {
543543
product := Mul(a[i], b[i])
544544
sum = Add(sum, product)
@@ -548,7 +548,7 @@ func DotProduct(a, b []Float16) Float16 {
548548

549549
// Norm2 computes the L2 norm (Euclidean norm) of a Float16 slice
550550
func Norm2(s []Float16) Float16 {
551-
var sumSquares Float16 = PositiveZero
551+
sumSquares := PositiveZero
552552
for _, v := range s {
553553
square := Mul(v, v)
554554
sumSquares = Add(sumSquares, square)

arithmetic_rounding_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ func modes() []RoundingMode {
1818

1919
func TestAddWithMode_RoundingMatchesConverter(t *testing.T) {
2020
cases := [][2]float32{
21-
{1.0, float32(math.Pow(2, -11))}, // halfway between 1.0 and next
22-
{1.0, 1e-3}, // general positive
23-
{-1.0, float32(math.Pow(2, -11))}, // negative with halfway increment
24-
{-0.75, 0.125}, // mixed signs, exact binary fractions
21+
{1.0, float32(math.Pow(2, -11))}, // halfway between 1.0 and next
22+
{1.0, 1e-3}, // general positive
23+
{-1.0, float32(math.Pow(2, -11))}, // negative with halfway increment
24+
{-0.75, 0.125}, // mixed signs, exact binary fractions
2525
}
2626

2727
for _, c := range cases {
@@ -47,10 +47,10 @@ func TestAddWithMode_RoundingMatchesConverter(t *testing.T) {
4747

4848
func TestMulWithMode_RoundingMatchesConverter(t *testing.T) {
4949
cases := [][2]float32{
50-
{1.25, 0.2}, // positive * positive
51-
{-1.25, 0.2}, // negative * positive
52-
{1.5, -0.75}, // positive * negative
53-
{-0.5, -0.125}, // negative * negative
50+
{1.25, 0.2}, // positive * positive
51+
{-1.25, 0.2}, // negative * positive
52+
{1.5, -0.75}, // positive * negative
53+
{-0.5, -0.125}, // negative * negative
5454
{float32(math.Pow(2, -3)), float32(math.Pow(2, -8))}, // exact powers of two
5555
}
5656

@@ -77,11 +77,11 @@ func TestMulWithMode_RoundingMatchesConverter(t *testing.T) {
7777

7878
func TestDivWithMode_RoundingMatchesConverter(t *testing.T) {
7979
cases := [][2]float32{
80-
{1.25, 0.2}, // positive / positive
81-
{-1.25, 0.2}, // negative / positive
82-
{1.5, -0.75}, // positive / negative
83-
{-0.5, -0.125}, // negative / negative
84-
{7.0, 3.0}, // non-terminating in binary
80+
{1.25, 0.2}, // positive / positive
81+
{-1.25, 0.2}, // negative / positive
82+
{1.5, -0.75}, // positive / negative
83+
{-0.5, -0.125}, // negative / negative
84+
{7.0, 3.0}, // non-terminating in binary
8585
}
8686

8787
for _, c := range cases {

bfloat16.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ func BFloat16FromFloat32WithRounding(f float32, mode RoundingMode) BFloat16 {
8686

8787
// Extract the high 16 bits (sign, exponent, and 7 MSBs of mantissa)
8888
bfloat16Bits := bits >> 16
89-
89+
9090
// Check the bit at position 15 of the original float32 bits (the first bit to be truncated)
9191
roundBit := (bits >> 15) & 0x1
92-
92+
9393
// Check if any of the bits from position 0 to 14 are non-zero
9494
stickyBits := bits & 0x7FFF
9595

@@ -133,7 +133,6 @@ func BFloat16FromFloat32WithRounding(f float32, mode RoundingMode) BFloat16 {
133133
return BFloat16(bfloat16Bits)
134134
}
135135

136-
137136
// ToFloat32 converts BFloat16 to float32
138137
func (b BFloat16) ToFloat32() float32 {
139138
// Expand back to 32 bits by shifting left 16 positions
@@ -152,7 +151,6 @@ func BFloat16FromFloat64WithRounding(f float64, mode RoundingMode) BFloat16 {
152151
return BFloat16FromFloat32WithRounding(float32(f), mode)
153152
}
154153

155-
156154
// BFloat16FromFloat32WithMode converts a float32 to BFloat16 with specified conversion and rounding modes.
157155
func BFloat16FromFloat32WithMode(f32 float32, convMode ConversionMode, roundMode RoundingMode) (BFloat16, error) {
158156
// First, perform the rounding conversion

bfloat16_conversion_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,4 @@ func TestBFloat16FromFloat32WithMode(t *testing.T) {
272272
}
273273
})
274274
}
275-
}
275+
}

convert.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -212,44 +212,44 @@ func (f Float16) ToFloat64() float64 {
212212
// shouldRound determines whether to round up during conversion
213213
// This is a helper function used in conversion algorithms
214214
func shouldRound(mantissa uint32, shift int, sign uint16) bool {
215-
if shift <= 0 {
216-
return false
217-
}
218-
219-
// Bits about to be discarded
220-
guard := (mantissa >> uint(shift-1)) & 1
221-
sticky := mantissa & ((1 << uint(shift-1)) - 1)
222-
lsb := (mantissa >> uint(shift)) & 1
223-
anyDiscarded := (guard | (boolToUint(sticky != 0))) == 1
224-
225-
switch DefaultRoundingMode {
226-
case RoundNearestEven:
227-
// Round up if guard=1 and (sticky!=0 or LSB is 1) => ties to even
228-
return guard == 1 && (sticky != 0 || lsb == 1)
229-
case RoundNearestAway:
230-
// Round up on half or more (guard=1). If less than half (guard=0), do not round.
231-
// sticky doesn't affect decision except that if sticky>0, it's strictly more than half.
232-
return guard == 1 || sticky != 0
233-
case RoundTowardZero:
234-
return false
235-
case RoundTowardPositive:
236-
// Round up for positive numbers if any discarded bits are non-zero
237-
return (sign&SignMask) == 0 && anyDiscarded
238-
case RoundTowardNegative:
239-
// Round up (i.e., toward -inf increases magnitude) for negative numbers if discarded bits
240-
return (sign&SignMask) != 0 && anyDiscarded
241-
default:
242-
// Invalid rounding mode: do not round
243-
return false
244-
}
215+
if shift <= 0 {
216+
return false
217+
}
218+
219+
// Bits about to be discarded
220+
guard := (mantissa >> uint(shift-1)) & 1
221+
sticky := mantissa & ((1 << uint(shift-1)) - 1)
222+
lsb := (mantissa >> uint(shift)) & 1
223+
anyDiscarded := (guard | (boolToUint(sticky != 0))) == 1
224+
225+
switch DefaultRoundingMode {
226+
case RoundNearestEven:
227+
// Round up if guard=1 and (sticky!=0 or LSB is 1) => ties to even
228+
return guard == 1 && (sticky != 0 || lsb == 1)
229+
case RoundNearestAway:
230+
// Round up on half or more (guard=1). If less than half (guard=0), do not round.
231+
// sticky doesn't affect decision except that if sticky>0, it's strictly more than half.
232+
return guard == 1 || sticky != 0
233+
case RoundTowardZero:
234+
return false
235+
case RoundTowardPositive:
236+
// Round up for positive numbers if any discarded bits are non-zero
237+
return (sign&SignMask) == 0 && anyDiscarded
238+
case RoundTowardNegative:
239+
// Round up (i.e., toward -inf increases magnitude) for negative numbers if discarded bits
240+
return (sign&SignMask) != 0 && anyDiscarded
241+
default:
242+
// Invalid rounding mode: do not round
243+
return false
244+
}
245245
}
246246

247247
// boolToUint converts a bool to 0/1 as uint32
248248
func boolToUint(b bool) uint32 {
249-
if b {
250-
return 1
251-
}
252-
return 0
249+
if b {
250+
return 1
251+
}
252+
return 0
253253
}
254254

255255
// Parse converts a string to a Float16 value
@@ -355,4 +355,3 @@ func ParseFloat(s string, precision int) (Float16, error) {
355355
}
356356
return FromFloat32(float32(f32)), nil
357357
}
358-

float16.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Package float16 implements the 16-bit floating point data type (IEEE 754-2008).
1+
// Package float16 implements both 16-bit floating point data types:
2+
// - Float16: IEEE 754-2008 half-precision (1 sign, 5 exponent, 10 mantissa bits)
3+
// - BFloat16: "Brain Floating Point" format (1 sign, 8 exponent, 7 mantissa bits)
24
//
3-
// This implementation provides conversion between float16 and other floating-point types
5+
// This implementation provides conversion between both types and other floating-point types
46
// (float32 and float64) with support for various rounding modes and error handling.
57
//
68
// # Special Values

math.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ func Remainder(f, divisor Float16) Float16 {
458458

459459
// Mathematical constants as Float16 values
460460
var (
461-
E = FromFloat32(float32(math.E)) // Euler's number
461+
E = FromFloat32(float32(math.E)) // Euler's number
462462
Pi = FromFloat32(float32(math.Pi)) // Pi
463463
Phi = FromFloat32(float32(math.Phi)) // Golden ratio
464464
Sqrt2 = FromFloat32(float32(math.Sqrt2)) // Square root of 2

math_extra_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestAsin(t *testing.T) {
1111
want Float16
1212
}{
1313
{"Asin(0)", PositiveZero, PositiveZero},
14-
{"Asin(1)", FromFloat32(1.0), FromBits(0x3E48)}, // Approx. Pi/2
14+
{"Asin(1)", FromFloat32(1.0), FromBits(0x3E48)}, // Approx. Pi/2
1515
{"Asin(-1)", FromFloat32(-1.0), FromBits(0xBE48)}, // Approx. -Pi/2
1616
{"Asin(NaN)", QuietNaN, QuietNaN},
1717
{"Asin(2)", FromFloat32(2.0), QuietNaN},
@@ -55,7 +55,7 @@ func TestAtan(t *testing.T) {
5555
want Float16
5656
}{
5757
{"Atan(0)", PositiveZero, PositiveZero},
58-
{"Atan(inf)", PositiveInfinity, FromBits(0x3E48)}, // Approx. Pi/2
58+
{"Atan(inf)", PositiveInfinity, FromBits(0x3E48)}, // Approx. Pi/2
5959
{"Atan(-inf)", NegativeInfinity, FromBits(0xBE48)}, // Approx. -Pi/2
6060
{"Atan(NaN)", QuietNaN, QuietNaN},
6161
}
@@ -333,4 +333,4 @@ func TestY1(t *testing.T) {
333333
})
334334
}
335335
}
336-
*/
336+
*/

types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ func (f Float16) String() string {
280280
func (f Float16) GoString() string {
281281
return fmt.Sprintf("float16.FromBits(0x%04x)", uint16(f))
282282
}
283+
283284
func (f Float16) ToInt32() int32 {
284285
return int32(f.ToFloat32())
285286
}

0 commit comments

Comments
 (0)