Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bfloat16.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ func BFloat16FromFloat32WithMode(f32 float32, convMode ConversionMode, roundMode
// Check for special values and ranges
if math.IsNaN(float64(f32)) {
if convMode == ModeStrict {
return 0, &Float16Error{Op: "BFloat16FromFloat32", Msg: "NaN conversion in strict mode", Code: ErrNaN}
return 0, &BFloat16Error{Op: "BFloat16FromFloat32", Msg: "NaN conversion in strict mode", Code: ErrNaN}
}
return BFloat16QuietNaN, nil
}

if math.IsInf(float64(f32), 0) {
if convMode == ModeStrict {
return 0, &Float16Error{Op: "BFloat16FromFloat32", Msg: "Inf conversion in strict mode", Code: ErrInfinity}
return 0, &BFloat16Error{Op: "BFloat16FromFloat32", Msg: "Inf conversion in strict mode", Code: ErrInfinity}
}
// Already handled by BFloat16FromFloat32WithRounding, which preserves Inf
return b, nil
Expand All @@ -180,7 +180,7 @@ func BFloat16FromFloat32WithMode(f32 float32, convMode ConversionMode, roundMode

if f32 > bf16Max || f32 < bf16Min {
if convMode == ModeStrict {
return 0, &Float16Error{Op: "BFloat16FromFloat32", Msg: "overflow in strict mode", Code: ErrOverflow}
return 0, &BFloat16Error{Op: "BFloat16FromFloat32", Msg: "overflow in strict mode", Code: ErrOverflow}
}
// ModeIEEE: saturate to infinity
if f32 > 0 {
Expand All @@ -194,7 +194,7 @@ func BFloat16FromFloat32WithMode(f32 float32, convMode ConversionMode, roundMode
// and the result after rounding is zero, it's an underflow.
if f32 != 0 && math.Abs(float64(f32)) < float64(bf16SmallestNormalPos) && b.IsZero() {
if convMode == ModeStrict {
return 0, &Float16Error{Op: "BFloat16FromFloat32", Msg: "underflow in strict mode", Code: ErrUnderflow}
return 0, &BFloat16Error{Op: "BFloat16FromFloat32", Msg: "underflow in strict mode", Code: ErrUnderflow}
}
// ModeIEEE: saturate to zero (already handled by rounding to zero)
return b, nil
Expand Down
16 changes: 8 additions & 8 deletions bfloat16_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func BFloat16AddWithMode(a, b BFloat16, mode ArithmeticMode, rounding RoundingMo
// Handle NaN propagation: if either operand is NaN, propagate it
if a.IsNaN() || b.IsNaN() {
if mode == ModeExactArithmetic {
return 0, &Float16Error{Op: "bfloat16_add", Msg: "NaN operand in exact mode", Code: ErrNaN}
return 0, &BFloat16Error{Op: "bfloat16_add", Msg: "NaN operand in exact mode", Code: ErrNaN}
}
return BFloat16QuietNaN, nil
}
Expand All @@ -24,7 +24,7 @@ func BFloat16AddWithMode(a, b BFloat16, mode ArithmeticMode, rounding RoundingMo
if a.IsInf(0) || b.IsInf(0) {
if a.IsInf(1) && b.IsInf(-1) || a.IsInf(-1) && b.IsInf(1) {
if mode == ModeExactArithmetic {
return 0, &Float16Error{Op: "bfloat16_add", Msg: "infinity - infinity is undefined", Code: ErrInvalidOperation}
return 0, &BFloat16Error{Op: "bfloat16_add", Msg: "infinity - infinity is undefined", Code: ErrInvalidOperation}
}
return BFloat16QuietNaN, nil
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func BFloat16MulWithMode(a, b BFloat16, mode ArithmeticMode, rounding RoundingMo
// NaN propagation
if a.IsNaN() || b.IsNaN() {
if mode == ModeExactArithmetic {
return 0, &Float16Error{Op: "bfloat16_mul", Msg: "NaN operand in exact mode", Code: ErrNaN}
return 0, &BFloat16Error{Op: "bfloat16_mul", Msg: "NaN operand in exact mode", Code: ErrNaN}
}
return BFloat16QuietNaN, nil
}
Expand All @@ -75,7 +75,7 @@ func BFloat16MulWithMode(a, b BFloat16, mode ArithmeticMode, rounding RoundingMo
// 0 * Inf = NaN
if (aZero && b.IsInf(0)) || (a.IsInf(0) && bZero) {
if mode == ModeExactArithmetic {
return 0, &Float16Error{Op: "bfloat16_mul", Msg: "zero times infinity is undefined", Code: ErrInvalidOperation}
return 0, &BFloat16Error{Op: "bfloat16_mul", Msg: "zero times infinity is undefined", Code: ErrInvalidOperation}
}
return BFloat16QuietNaN, nil
}
Expand Down Expand Up @@ -119,23 +119,23 @@ func BFloat16DivWithMode(a, b BFloat16, mode ArithmeticMode, rounding RoundingMo
// NaN propagation
if a.IsNaN() || b.IsNaN() {
if mode == ModeExactArithmetic {
return 0, &Float16Error{Op: "bfloat16_div", Msg: "NaN operand in exact mode", Code: ErrNaN}
return 0, &BFloat16Error{Op: "bfloat16_div", Msg: "NaN operand in exact mode", Code: ErrNaN}
}
return BFloat16QuietNaN, nil
}

// 0 / 0 = NaN
if a.IsZero() && b.IsZero() {
if mode == ModeExactArithmetic {
return 0, &Float16Error{Op: "bfloat16_div", Msg: "zero divided by zero is undefined", Code: ErrInvalidOperation}
return 0, &BFloat16Error{Op: "bfloat16_div", Msg: "zero divided by zero is undefined", Code: ErrInvalidOperation}
}
return BFloat16QuietNaN, nil
}

// finite / 0 = +/-Inf
if b.IsZero() {
if mode == ModeExactArithmetic {
return 0, &Float16Error{Op: "bfloat16_div", Msg: "division by zero", Code: ErrDivisionByZero}
return 0, &BFloat16Error{Op: "bfloat16_div", Msg: "division by zero", Code: ErrDivisionByZero}
}
if a.Signbit() != b.Signbit() {
return BFloat16NegativeInfinity, nil
Expand All @@ -154,7 +154,7 @@ func BFloat16DivWithMode(a, b BFloat16, mode ArithmeticMode, rounding RoundingMo
// Inf / Inf = NaN
if a.IsInf(0) && b.IsInf(0) {
if mode == ModeExactArithmetic {
return 0, &Float16Error{Op: "bfloat16_div", Msg: "infinity divided by infinity is undefined", Code: ErrInvalidOperation}
return 0, &BFloat16Error{Op: "bfloat16_div", Msg: "infinity divided by infinity is undefined", Code: ErrInvalidOperation}
}
return BFloat16QuietNaN, nil
}
Expand Down
20 changes: 10 additions & 10 deletions bfloat16_arithmetic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func TestBFloat16AddWithMode(t *testing.T) {
if err == nil {
t.Fatalf("expected error, got nil")
}
fe, ok := err.(*Float16Error)
fe, ok := err.(*BFloat16Error)
if !ok {
t.Fatalf("expected *Float16Error, got %T", err)
t.Fatalf("expected *BFloat16Error, got %T", err)
}
if fe.Code != tt.errCode {
t.Errorf("error code = %d, want %d", fe.Code, tt.errCode)
Expand Down Expand Up @@ -83,9 +83,9 @@ func TestBFloat16SubWithMode(t *testing.T) {
if err == nil {
t.Fatalf("expected error, got nil")
}
fe, ok := err.(*Float16Error)
fe, ok := err.(*BFloat16Error)
if !ok {
t.Fatalf("expected *Float16Error, got %T", err)
t.Fatalf("expected *BFloat16Error, got %T", err)
}
if fe.Code != tt.errCode {
t.Errorf("error code = %d, want %d", fe.Code, tt.errCode)
Expand Down Expand Up @@ -133,9 +133,9 @@ func TestBFloat16MulWithMode(t *testing.T) {
if err == nil {
t.Fatalf("expected error, got nil")
}
fe, ok := err.(*Float16Error)
fe, ok := err.(*BFloat16Error)
if !ok {
t.Fatalf("expected *Float16Error, got %T", err)
t.Fatalf("expected *BFloat16Error, got %T", err)
}
if fe.Code != tt.errCode {
t.Errorf("error code = %d, want %d", fe.Code, tt.errCode)
Expand Down Expand Up @@ -193,9 +193,9 @@ func TestBFloat16DivWithMode(t *testing.T) {
if err == nil {
t.Fatalf("expected error, got nil")
}
fe, ok := err.(*Float16Error)
fe, ok := err.(*BFloat16Error)
if !ok {
t.Fatalf("expected *Float16Error, got %T", err)
t.Fatalf("expected *BFloat16Error, got %T", err)
}
if fe.Code != tt.errCode {
t.Errorf("error code = %d, want %d", fe.Code, tt.errCode)
Expand Down Expand Up @@ -291,9 +291,9 @@ func TestBFloat16NaNPropagationAllModes(t *testing.T) {
if err == nil {
t.Fatal("expected error for NaN in exact mode, got nil")
}
fe, ok := err.(*Float16Error)
fe, ok := err.(*BFloat16Error)
if !ok {
t.Fatalf("expected *Float16Error, got %T", err)
t.Fatalf("expected *BFloat16Error, got %T", err)
}
if fe.Code != ErrNaN {
t.Errorf("error code = %d, want %d (ErrNaN)", fe.Code, ErrNaN)
Expand Down
Loading