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

Max Chou posted 3 patches 1 month, 2 weeks ago
Maintainers: Aurelien Jarno <aurelien@aurel32.net>, Peter Maydell <peter.maydell@linaro.org>, "Alex Bennée" <alex.bennee@linaro.org>
There is a newer version of this series
[PATCH 2/3] fpu: Fix unexpected exception flags when converting infinity to OCP E4M3
Posted by Max Chou 1 month, 2 weeks ago
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")
Signed-off-by: Max Chou <max.chou@sifive.com>
---
 fpu/softfloat-parts.c.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fpu/softfloat-parts.c.inc b/fpu/softfloat-parts.c.inc
index a738758aee..3c323c0cec 100644
--- a/fpu/softfloat-parts.c.inc
+++ b/fpu/softfloat-parts.c.inc
@@ -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;
 
-- 
2.52.0
Re: [PATCH 2/3] fpu: Fix unexpected exception flags when converting infinity to OCP E4M3
Posted by Richard Henderson 1 month, 2 weeks ago
On 2/25/26 22:08, Max Chou wrote:
> 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")
> Signed-off-by: Max Chou <max.chou@sifive.com>
> ---
>   fpu/softfloat-parts.c.inc | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fpu/softfloat-parts.c.inc b/fpu/softfloat-parts.c.inc
> index a738758aee..3c323c0cec 100644
> --- a/fpu/softfloat-parts.c.inc
> +++ b/fpu/softfloat-parts.c.inc
> @@ -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;
>   

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~
Re: [PATCH 2/3] fpu: Fix unexpected exception flags when converting infinity to OCP E4M3
Posted by Chao Liu 1 month, 2 weeks ago
On Wed, Feb 25, 2026 at 07:08:01PM +0800, Max Chou wrote:
> 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")
> Signed-off-by: Max Chou <max.chou@sifive.com>
> ---
>  fpu/softfloat-parts.c.inc | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fpu/softfloat-parts.c.inc b/fpu/softfloat-parts.c.inc
> index a738758aee..3c323c0cec 100644
> --- a/fpu/softfloat-parts.c.inc
> +++ b/fpu/softfloat-parts.c.inc
> @@ -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;
>  
> -- 
> 2.52.0
> 
> 

Reviewed-by: Chao Liu <chao.liu.zevorn@gmail.com>

Thanks,
Chao