Skip to contentSkip to conversation

fpu: Fix OCP FP8 E4M3 conversion issues

PATCH Superseded Max Chou 3 participants 11 messages Feb 25 – Feb 26

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Max Chou

author
#0 lore raw

cover fpu: Fix OCP FP8 E4M3 conversion issues

This series fix following issues related to OCP FP8 E4M3 conversion.

- Numeric overflow to E4M3 produces incorrect exponent values due to a missing local variable update after calling uncanon_e4m3_overflow.
- Converting infinity to E4M3 incorrectly raises OF|NX exception flags.
- The E4M3 NaN classification logic in parts_canonicalize uses an indirect method that obscures the architecture-dependent semantics.

rnax

Max Chou (3):
fpu: Fix repacking issues in the uncanonical step for E4M3 overflow
fpu: Fix unexpected exception flags when converting infinity to OCP

    E4M3

fpu: Simplify OCP FP8 E4M3 NaN classification in parts_canonicalize

 fpu/softfloat-parts.c.inc | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH 1/3 fpu: Fix repacking issues in the uncanonical step for E4M3 overflow

In the uncanonical step, the input FloatParts will be repacked to the target FloatFmt. This commit fixes following issues after calling uncanon_e4m3_overflow in the uncanon/uncanon_normal functions.

- Add the local exp update after calling uncanon_e4m3_overflow in the parts_uncanon_normal function.
- Add the fraction shift after calling uncanon_e4m3_overflow in the parts_uncanon function.

Fixes 27e989f99c ("fpu: Add conversion routines for OCP FP8 E4M3")
---
 fpu/softfloat-parts.c.inc | 2 ++
 1 file changed, 2 insertions(+)
fpu/softfloat-parts.c.inc +2 -0
index 61b07307bf..a738758aee 100644
@@ -387,6 +387,7 @@ static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s,
             case float_expmax_e4m3:
                 if (exp > exp_max || p->frac_hi > E4M3_NORMAL_FRAC_MAX) {
                     partsN(uncanon_e4m3_overflow)(p, s, fmt, overflow_norm);
+                    exp = p->exp;
                 }
                 break;
 
@@ -496,6 +497,7 @@ static void partsN(uncanon)(FloatPartsN *p, float_status *s,
                 break;
             case float_expmax_e4m3:
                 partsN(uncanon_e4m3_overflow)(p, s, fmt, saturate);
+                frac_shr(p, fmt->frac_shift);
                 break;
             case float_expmax_normal:
             default:
Signature · 1 line
2.52.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] fpu: Fix repacking issues in the uncanonical step for E4M3 overflow

Quoted 39 lines · Max Chou

In the uncanonical step, the input FloatParts will be repacked to the target FloatFmt. This commit fixes following issues after calling uncanon_e4m3_overflow in the uncanon/uncanon_normal functions.

- Add the local exp update after calling uncanon_e4m3_overflow in the parts_uncanon_normal function.
- Add the fraction shift after calling uncanon_e4m3_overflow in the parts_uncanon function.

Fixes 27e989f99c ("fpu: Add conversion routines for OCP FP8 E4M3")
---
 fpu/softfloat-parts.c.inc | 2 ++
 1 file changed, 2 insertions(+)
fpu/softfloat-parts.c.inc +2 -0
index 61b07307bf..a738758aee 100644
@@ -387,6 +387,7 @@ static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s,
             case float_expmax_e4m3:
                 if (exp > exp_max || p->frac_hi > E4M3_NORMAL_FRAC_MAX) {
                     partsN(uncanon_e4m3_overflow)(p, s, fmt, overflow_norm);
+                    exp = p->exp;
                 }
                 break;
 
@@ -496,6 +497,7 @@ static void partsN(uncanon)(FloatPartsN *p, float_status *s,
                 break;
             case float_expmax_e4m3:
                 partsN(uncanon_e4m3_overflow)(p, s, fmt, saturate);
+                frac_shr(p, fmt->frac_shift);
                 break;
             case float_expmax_normal:
             default:
Signature · 1 line
2.52.0

Thanks,
Chao

Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] fpu: Fix repacking issues in the uncanonical step for E4M3 overflow

In reply to Max Chou
Quoted 28 lines · Max Chou

In the uncanonical step, the input FloatParts will be repacked to the target FloatFmt. This commit fixes following issues after calling uncanon_e4m3_overflow in the uncanon/uncanon_normal functions.

- Add the local exp update after calling uncanon_e4m3_overflow in the parts_uncanon_normal function.
- Add the fraction shift after calling uncanon_e4m3_overflow in the parts_uncanon function.

Fixes 27e989f99c ("fpu: Add conversion routines for OCP FP8 E4M3")
---
  fpu/softfloat-parts.c.inc | 2 ++
  1 file changed, 2 insertions(+)
fpu/softfloat-parts.c.inc +1 -0
index 61b07307bf..a738758aee 100644
@@ -387,6 +387,7 @@ static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s,
              case float_expmax_e4m3:
                  if (exp > exp_max || p->frac_hi > E4M3_NORMAL_FRAC_MAX) {
                      partsN(uncanon_e4m3_overflow)(p, s, fmt, overflow_norm);
+                    exp = p->exp;
                  }
                  break;
 

fpu/softfloat-parts.c.inc static void partsN(uncanon)(FloatPartsN *p, float_status *s, · line 503

Quoted 7 lines
fpu/softfloat-parts.c.inc +1 -0
@@ -496,6 +497,7 @@ static void partsN(uncanon)(FloatPartsN *p, float_status *s,
                  break;
              case float_expmax_e4m3:
                  partsN(uncanon_e4m3_overflow)(p, s, fmt, saturate);
+                frac_shr(p, fmt->frac_shift);
                  break;
              case float_expmax_normal:
              default:

r~

author
#4 lore raw

PATCH 2/3 fpu: Fix unexpected exception flags when converting infinity to OCP E4M3

In reply to Max Chou

Infinity is a special case distinct from numeric overflow:
- Numeric overflow: finite value exceeds format's max normal -> overflow|inexact
- Infinity conversion: input is already infinite -> no flags

This commit fixes the unexpect exception flags by relocating the float exception flag update flow to be outside the uncanon_e4m3_overflow. And raising the overflow|inexact for numeric overflow in uncanon_normal.

Fixes 27e989f99c ("fpu: Add conversion routines for OCP FP8 E4M3")
---
 fpu/softfloat-parts.c.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fpu/softfloat-parts.c.inc +1 -1
index a738758aee..3c323c0cec 100644
@@ -277,7 +277,6 @@ static void partsN(uncanon_e4m3_overflow)(FloatPartsN *p, float_status *s,
                                           const FloatFmt *fmt, bool saturate)
 {
     assert(N == 64);
-    float_raise(float_flag_overflow | float_flag_inexact, s);
     if (saturate) {
         p->exp = fmt->exp_max;
         p->frac_hi = E4M3_NORMAL_FRAC_MAX;
@@ -388,6 +387,7 @@ static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s,
                 if (exp > exp_max || p->frac_hi > E4M3_NORMAL_FRAC_MAX) {
                     partsN(uncanon_e4m3_overflow)(p, s, fmt, overflow_norm);
                     exp = p->exp;
+                    flags |= (float_flag_overflow | float_flag_inexact);
                 }
                 break;
 
Signature · 1 line
2.52.0
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] fpu: Fix unexpected exception flags when converting infinity to OCP E4M3

Quoted 40 lines · Max Chou

Infinity is a special case distinct from numeric overflow:
- Numeric overflow: finite value exceeds format's max normal -> overflow|inexact
- Infinity conversion: input is already infinite -> no flags

This commit fixes the unexpect exception flags by relocating the float exception flag update flow to be outside the uncanon_e4m3_overflow. And raising the overflow|inexact for numeric overflow in uncanon_normal.

Fixes 27e989f99c ("fpu: Add conversion routines for OCP FP8 E4M3")
---
 fpu/softfloat-parts.c.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fpu/softfloat-parts.c.inc +1 -1
index a738758aee..3c323c0cec 100644
@@ -277,7 +277,6 @@ static void partsN(uncanon_e4m3_overflow)(FloatPartsN *p, float_status *s,
                                           const FloatFmt *fmt, bool saturate)
 {
     assert(N == 64);
-    float_raise(float_flag_overflow | float_flag_inexact, s);
     if (saturate) {
         p->exp = fmt->exp_max;
         p->frac_hi = E4M3_NORMAL_FRAC_MAX;
@@ -388,6 +387,7 @@ static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s,
                 if (exp > exp_max || p->frac_hi > E4M3_NORMAL_FRAC_MAX) {
                     partsN(uncanon_e4m3_overflow)(p, s, fmt, overflow_norm);
                     exp = p->exp;
+                    flags |= (float_flag_overflow | float_flag_inexact);
                 }
                 break;
 
Signature · 1 line
2.52.0

Thanks,
Chao

Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] fpu: Fix unexpected exception flags when converting infinity to OCP E4M3

In reply to Max Chou
Quoted 29 lines · Max Chou

Infinity is a special case distinct from numeric overflow:
- Numeric overflow: finite value exceeds format's max normal -> overflow|inexact
- Infinity conversion: input is already infinite -> no flags

This commit fixes the unexpect exception flags by relocating the float exception flag update flow to be outside the uncanon_e4m3_overflow. And raising the overflow|inexact for numeric overflow in uncanon_normal.

Fixes 27e989f99c ("fpu: Add conversion routines for OCP FP8 E4M3")
---
  fpu/softfloat-parts.c.inc | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fpu/softfloat-parts.c.inc +0 -1
index a738758aee..3c323c0cec 100644
@@ -277,7 +277,6 @@ static void partsN(uncanon_e4m3_overflow)(FloatPartsN *p, float_status *s,
                                            const FloatFmt *fmt, bool saturate)
  {
      assert(N == 64);
-    float_raise(float_flag_overflow | float_flag_inexact, s);
      if (saturate) {
          p->exp = fmt->exp_max;
          p->frac_hi = E4M3_NORMAL_FRAC_MAX;

fpu/softfloat-parts.c.inc static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s, · line 393

Quoted 7 lines
fpu/softfloat-parts.c.inc +1 -0
@@ -388,6 +387,7 @@ static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s,
                  if (exp > exp_max || p->frac_hi > E4M3_NORMAL_FRAC_MAX) {
                      partsN(uncanon_e4m3_overflow)(p, s, fmt, overflow_norm);
                      exp = p->exp;
+                    flags |= (float_flag_overflow | float_flag_inexact);
                  }
                  break;
 

r~

author
#7 lore raw

PATCH 3/3 fpu: Simplify OCP FP8 E4M3 NaN classification in parts_canonicalize

In reply to Max Chou

The OCP FP8 E4M3 format has only a single NaN encoding (0x7F/0xFF), Replace the indirect check via parts_is_snan_frac with a direct check of no_signaling_nans to make the intent clearer.

---
 fpu/softfloat-parts.c.inc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fpu/softfloat-parts.c.inc +2 -2
index 3c323c0cec..5ef7e2d921 100644
@@ -245,8 +245,8 @@ static void partsN(canonicalize)(FloatPartsN *p, float_status *status,
         case float_expmax_e4m3:
             if (p->frac_hi == 0b111) {
                 frac_shl(p, fmt->frac_shift);
-                p->cls = (parts_is_snan_frac(p->frac_hi, status)
-                          ? float_class_snan : float_class_qnan);
+                p->cls = no_signaling_nans(status) ? float_class_qnan :
+                                                     float_class_snan;
                 return;
             }
             /* otherwise normal */
Signature · 1 line
2.52.0
Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] fpu: Simplify OCP FP8 E4M3 NaN classification in parts_canonicalize

Quoted 28 lines · Max Chou

The OCP FP8 E4M3 format has only a single NaN encoding (0x7F/0xFF), Replace the indirect check via parts_is_snan_frac with a direct check of no_signaling_nans to make the intent clearer.

---
 fpu/softfloat-parts.c.inc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fpu/softfloat-parts.c.inc +2 -2
index 3c323c0cec..5ef7e2d921 100644
@@ -245,8 +245,8 @@ static void partsN(canonicalize)(FloatPartsN *p, float_status *status,
         case float_expmax_e4m3:
             if (p->frac_hi == 0b111) {
                 frac_shl(p, fmt->frac_shift);
-                p->cls = (parts_is_snan_frac(p->frac_hi, status)
-                          ? float_class_snan : float_class_qnan);
+                p->cls = no_signaling_nans(status) ? float_class_qnan :
+                                                     float_class_snan;
                 return;
             }
             /* otherwise normal */
Signature · 1 line
2.52.0

Thanks,
Chao

#9 lore raw

Re: [PATCH 3/3] fpu: Simplify OCP FP8 E4M3 NaN classification in parts_canonicalize

In reply to Max Chou
Quoted 14 lines · Max Chou

The OCP FP8 E4M3 format has only a single NaN encoding (0x7F/0xFF), Replace the indirect check via parts_is_snan_frac with a direct check of no_signaling_nans to make the intent clearer.

---
  fpu/softfloat-parts.c.inc | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fpu/softfloat-parts.c.inc +0 -0
index 3c323c0cec..5ef7e2d921 100644

fpu/softfloat-parts.c.inc static void partsN(canonicalize)(FloatPartsN *p, float_status *status, · line 252

Quoted 10 lines
fpu/softfloat-parts.c.inc +2 -2
@@ -245,8 +245,8 @@ static void partsN(canonicalize)(FloatPartsN *p, float_status *status,
          case float_expmax_e4m3:
              if (p->frac_hi == 0b111) {
                  frac_shl(p, fmt->frac_shift);
-                p->cls = (parts_is_snan_frac(p->frac_hi, status)
-                          ? float_class_snan : float_class_qnan);
+                p->cls = no_signaling_nans(status) ? float_class_qnan :
+                                                     float_class_snan;
                  return;
              }
              /* otherwise normal */

But parts_is_snan_frac also checks snan_bit_is_one.

r~

author
#10 lore raw

Re: [PATCH 3/3] fpu: Simplify OCP FP8 E4M3 NaN classification in parts_canonicalize

Quoted 29 lines · Richard Henderson
Quoted 14 lines · Max Chou

The OCP FP8 E4M3 format has only a single NaN encoding (0x7F/0xFF), Replace the indirect check via parts_is_snan_frac with a direct check of no_signaling_nans to make the intent clearer.

---
  fpu/softfloat-parts.c.inc | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fpu/softfloat-parts.c.inc +0 -0
index 3c323c0cec..5ef7e2d921 100644

fpu/softfloat-parts.c.inc static void partsN(canonicalize)(FloatPartsN *p, float_status *status, · line 252

Quoted 10 lines
fpu/softfloat-parts.c.inc +2 -2
@@ -245,8 +245,8 @@ static void partsN(canonicalize)(FloatPartsN *p, float_status *status,
          case float_expmax_e4m3:
              if (p->frac_hi == 0b111) {
                  frac_shl(p, fmt->frac_shift);
-                p->cls = (parts_is_snan_frac(p->frac_hi, status)
-                          ? float_class_snan : float_class_qnan);
+                p->cls = no_signaling_nans(status) ? float_class_qnan :
+                                                     float_class_snan;
                  return;
              }
              /* otherwise normal */

But parts_is_snan_frac also checks snan_bit_is_one.

Quoted 1 line

r~

Hi Richard,

You're right. I missed that parts_is_snan_frac respects both snan_bit_is_one and no_signaling_nans flags and I just assumed that user will use no_signaling_nans flag to define the E4M3 NaN.
The current implementation correctly allows targets to control E4M3 NaN classification through either mechanism, and this change would have removed the snan_bit_is_one flexibility.

I'll drop this patch and send a v2 series containing only the two bug fix patches:

Thanks,
rnax