softfloat: roundAndPackFloat16 should return FPInfinity and FPMaxNormal based on overflow_to_inf.
PATCH 1/1 softfloat: roundAndPackFloat16 should return FPInfinity and FPMaxNormal based on overflow_to_inf.
Flag overflow_to_inf is specified by ARM manual to decide if FPInfinity(sign) or FPMaxNormal(sign) is returned. Current codepath fails to check it.
Fixed by adding overflow_to_inf flag to return infinity when it's true and maxnormal otherwise.
--- fpu/softfloat.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
fpu/softfloat.c +11 -1
@@ -3377,6 +3377,7 @@ static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,uint32_t increment;bool rounding_bumps_exp;bool is_tiny = false; +bool overflow_to_inf = false;/* Calculate the mask of bits of the mantissa which are not * representable in half-precision and will be lost.
@@ -3398,18 +3399,23 @@ static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,if ((zSig & mask) == increment) { increment = zSig & (increment <<1 ); } + overflow_to_inf = true;break ;case float_round_ties_away: increment = (mask +1 ) >>1 ; + overflow_to_inf = true;break ;case float_round_up: increment = zSign ?0 : mask; + overflow_to_inf = (zSign ==0 );break ;case float_round_down: increment = zSign ? mask :0 ; + overflow_to_inf = (zSign ==1 );break ;default :/* round_to_zero */ increment =0 ; + overflow_to_inf = false;break ; }
@@ -3418,7 +3424,11 @@ static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,if (zExp > maxexp || (zExp == maxexp && rounding_bumps_exp)) {if (ieee) {float_raise (float_flag_overflow | float_flag_inexact, status); -return packFloat16 (zSign,0x1f ,0 ); +if (overflow_to_inf) { +return packFloat16 (zSign,0x1f ,0 ); + }else { +return packFloat16 (zSign,0x1e ,0x3ff ); + } }else {float_raise (float_flag_invalid, status);return packFloat16 (zSign,0x1f ,0x3ff );
Signature · 1 line
1.9.1
Quoted 5 lines · Xiangyu Hu
Flag
overflow_to_infis specified by ARM manual to decide if FPInfinity(sign) or FPMaxNormal(sign) is returned. Current codepath fails to check it.
Fixed by addingoverflow_to_infflag to return infinity when it's true and maxnormal otherwise.
I agree that this is wrong, but I think we want to fix it by making roundAndPackFloat16 work more similarly to the roundAndPackFloat32 and 64 functions...
Quoted 18 lines
Signed-off-by Xiangyu Hu <[email protected]>--- fpu/softfloat.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)fpu/softfloat.c +1
-0@@ -3377,6 +3377,7 @@ static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,uint32_t increment;bool rounding_bumps_exp;bool is_tiny = false; +bool overflow_to_inf = false;/* Calculate the mask of bits of the mantissa which are not * representable in half-precision and will be lost.
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3401
Quoted 3 lines
fpu/softfloat.c +0
-0@@ -3398,18 +3399,23 @@ static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,if ((zSig & mask) == increment) { increment = zSig & (increment <<1 ); }
1) we move this fiddling with increment down in the function to the point just before where we add increment to zSig
Quoted 9 lines
fpu/softfloat.c +3
-0+ overflow_to_inf = true;break ;case float_round_ties_away: increment = (mask +1 ) >>1 ; + overflow_to_inf = true;break ;case float_round_up: increment = zSign ?0 : mask; + overflow_to_inf = (zSign ==0 );
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3420
Quoted 10 lines
fpu/softfloat.c +2
-0break ;case float_round_down: increment = zSign ? mask :0 ; + overflow_to_inf = (zSign ==1 );break ;default :/* round_to_zero */ increment =0 ; + overflow_to_inf = false;break ; }
2) which means we don't need an extra flag, because now "do we overflow to inf?" is always "is increment non-zero?"
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3431
Quoted 10 lines
fpu/softfloat.c +5
-1@@ -3418,7 +3424,11 @@ static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,if (zExp > maxexp || (zExp == maxexp && rounding_bumps_exp)) {if (ieee) {float_raise (float_flag_overflow | float_flag_inexact, status); -return packFloat16 (zSign,0x1f ,0 ); +if (overflow_to_inf) { +return packFloat16 (zSign,0x1f ,0 ); + }else { +return packFloat16 (zSign,0x1e ,0x3ff ); + }
3) and so you can just say
return packFloat16(zSign, 0x1f, -(increment == 0));
This is slightly tricky (we rely on packFloat16() adding the significand argument into the value it builds up, such that passing exponent X and significand -1 results in a packed value with exponent X-1 and significand all-ones.) But it's consistent with the way we handle this case in the other roundAndPackFloat* functions, so I prefer it.
Deferring the adjustment of increment in the round_nearest_even case looks at first glance like it might affect other cases, because we calculate rounding_bumps_exp from increment. But in fact rounding_bumps_exp will always have the same value either way, if you work through the different possible zSig values.
thanks
-- PMM
Quoted 11 lines · Peter Maydell
Quoted 5 lines · Xiangyu Hu
Flag
overflow_to_infis specified by ARM manual to decide if FPInfinity(sign) or FPMaxNormal(sign) is returned. Current codepath fails to check it.
Fixed by addingoverflow_to_infflag to return infinity when it's true and maxnormal otherwise.I agree that this is wrong, but I think we want to fix it by making roundAndPackFloat16 work more similarly to the roundAndPackFloat32 and 64 functions...
Yes. Actually, a lot of single/double_to_half instructions would lead to inconsistent errors
when calling roundAndPackFloat16. This fix on FPMaxNormal/FPInfinity handling
only recover some of them. Failure to justify "round_up" flag described in the manual
exposed other bugs.
I had another implementation that completely followed manual's FPRound(), which
brought no failed cases at all.
I wonder where current algrithm of roundAndPackFloat16 comes from?
Quoted 48 lines
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign,
Quoted 11 lines
Signed-off-by Xiangyu Hu <[email protected]>--- fpu/softfloat.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)fpu/softfloat.c +0
-0@@ -3377,6 +3377,7 @@ static float32 roundAndPackFloat16(flag zSign,
int_fast16_tzExp,fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign,
Quoted 8 lines
fpu/softfloat.c +1
-0uint32_t increment;bool rounding_bumps_exp;bool is_tiny = false; +bool overflow_to_inf = false;/* Calculate the mask of bits of the mantissa which are not * representable in half-precision and will be lost. @@ -3398,18 +3399,23 @@ static float32 roundAndPackFloat16(flag zSign,
int_fast16_tzExp,fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3401
Quoted 3 lines
fpu/softfloat.c +0
-0if ((zSig & mask) == increment) { increment = zSig & (increment <<1 ); }1) we move this fiddling with increment down in the function to the point just before where we add increment to zSig
Quoted 9 lines
fpu/softfloat.c +3
-0+ overflow_to_inf = true;break ;case float_round_ties_away: increment = (mask +1 ) >>1 ; + overflow_to_inf = true;break ;case float_round_up: increment = zSign ?0 : mask; + overflow_to_inf = (zSign ==0 );fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3420
Quoted 10 lines
fpu/softfloat.c +2
-0break ;case float_round_down: increment = zSign ? mask :0 ; + overflow_to_inf = (zSign ==1 );break ;default :/* round_to_zero */ increment =0 ; + overflow_to_inf = false;break ; }
Quoted 3 lines
2) which means we don't need an extra flag, because now "do we overflow to inf?" is always "is increment non-zero?"
Yes... my original thought it was not meaningful as the manual and added the overflow_to_inf flag.
Quoted 18 lines
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign,
Quoted 1 line
fpu/softfloat.c +0
-0@@ -3418,7 +3424,11 @@ static float32 roundAndPackFloat16(flag zSign,
int_fast16_tzExp,fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,
Quoted 3 lines
fpu/softfloat.c +0
-0if (zExp > maxexp || (zExp == maxexp && rounding_bumps_exp)) {if (ieee) { float_raise(float_flag_overflow | float_flag_inexact,status);
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3431
Quoted 6 lines
fpu/softfloat.c +5
-1-return packFloat16 (zSign,0x1f ,0 ); +if (overflow_to_inf) { +return packFloat16 (zSign,0x1f ,0 ); + }else { +return packFloat16 (zSign,0x1e ,0x3ff ); + }3) and so you can just say
return packFloat16(zSign, 0x1f, -(increment == 0));
Quoted 7 lines
This is slightly tricky (we rely on
packFloat16()adding the significand argument into the value it builds up, such that passing exponent X and significand -1 results in a packed value with exponent X-1 and significand all-ones.) But it's consistent with the way we handle this case in the other roundAndPackFloat* functions, so I prefer it.
That's a nice trick!
Quoted 8 lines
Deferring the adjustment of increment in the
round_nearest_evencase looks at first glance like it might affect other cases, because we calculaterounding_bumps_expfrom increment. But in factrounding_bumps_expwill always have the same value either way, if you work through the different possible zSig values.
Again, I am not quite following the whole implemented algorithm of this roundAndPackFloat16; it cannot align with roundAndPackFloat[32|64] which originates softfloat(John Hauser).
Quoted 4 lines
thanks
-- PMM
Thanks!
- xiangyu
Quoted 15 lines · Xiangyu Hu
Quoted 11 lines · Peter Maydell
Quoted 5 lines · Xiangyu Hu
Flag
overflow_to_infis specified by ARM manual to decide if FPInfinity(sign) or FPMaxNormal(sign) is returned. Current codepath fails to check it.
Fixed by addingoverflow_to_infflag to return infinity when it's true and maxnormal otherwise.I agree that this is wrong, but I think we want to fix it by making roundAndPackFloat16 work more similarly to the roundAndPackFloat32 and 64 functions...
Quoted 7 lines
Yes. Actually, a lot of single/double_to_half instructions would lead to inconsistent errors
when calling roundAndPackFloat16. This fix on FPMaxNormal/FPInfinity handling
only recover some of them. Failure to justify "round_up" flag described in the manual
exposed other bugs.
I'm not surprised there are other bugs lurking here.
Quoted 3 lines
I had another implementation that completely followed manual's
FPRound(), which
brought no failed cases at all.
I'd prefer to fix the bugs we have rather than totally reimplement, though (especially since the ARM ARM pseudocode tends to assume things like infinite-precision floating point types; it's not always a great model to copy).
Quoted 1 line
I wonder where current algrithm of roundAndPackFloat16 comes from?
It was added ages ago as part of supporting the ARM 16-bit fp types; upstream softfloat doesn't have any 16-bit fp support.
-- PMM
Quoted 11 lines · Peter Maydell
Quoted 5 lines · Xiangyu Hu
Flag
overflow_to_infis specified by ARM manual to decide if FPInfinity(sign) or FPMaxNormal(sign) is returned. Current codepath fails to check it.
Fixed by addingoverflow_to_infflag to return infinity when it's true and maxnormal otherwise.I agree that this is wrong, but I think we want to fix it by making roundAndPackFloat16 work more similarly to the roundAndPackFloat32 and 64 functions...
Yes. Actually, a lot of single/double_to_half instructions would lead to inconsistent errors
when calling roundAndPackFloat16. This fix on FPMaxNormal/FPInfinity handling
only recover some of them. Failure to justify "round_up" flag described in the manual
exposed other bugs.
I had another implementation that completely followed manual's FPRound(), which
brought no failed cases at all.
I wonder where current algrithm of roundAndPackFloat16 comes from?
Quoted 47 lines
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign,
Quoted 10 lines
Signed-off-by Xiangyu Hu <[email protected]>--- fpu/softfloat.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)fpu/softfloat.c +0
-0@@ -3377,6 +3377,7 @@ static float32 roundAndPackFloat16(flag zSign,
int_fast16_tzExp,fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign,
Quoted 8 lines
fpu/softfloat.c +1
-0uint32_t increment;bool rounding_bumps_exp;bool is_tiny = false; +bool overflow_to_inf = false;/* Calculate the mask of bits of the mantissa which are not * representable in half-precision and will be lost. @@ -3398,18 +3399,23 @@ static float32 roundAndPackFloat16(flag zSign,
int_fast16_tzExp,fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3401
Quoted 3 lines
fpu/softfloat.c +0
-0if ((zSig & mask) == increment) { increment = zSig & (increment <<1 ); }1) we move this fiddling with increment down in the function to the point just before where we add increment to zSig
Quoted 9 lines
fpu/softfloat.c +3
-0+ overflow_to_inf = true;break ;case float_round_ties_away: increment = (mask +1 ) >>1 ; + overflow_to_inf = true;break ;case float_round_up: increment = zSign ?0 : mask; + overflow_to_inf = (zSign ==0 );fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3420
Quoted 10 lines
fpu/softfloat.c +2
-0break ;case float_round_down: increment = zSign ? mask :0 ; + overflow_to_inf = (zSign ==1 );break ;default :/* round_to_zero */ increment =0 ; + overflow_to_inf = false;break ; }
Quoted 3 lines
2) which means we don't need an extra flag, because now "do we overflow to inf?" is always "is increment non-zero?"
Yes... my original thought it was not meaningful as the manual and added the overflow_to_inf flag.
Quoted 14 lines
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign,
Quoted 2 lines
fpu/softfloat.c +0
-0@@ -3418,7 +3424,11 @@ static float32 roundAndPackFloat16(flag zSign,
int_fast16_tzExp,fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,
Quoted 3 lines
fpu/softfloat.c +0
-0if (zExp > maxexp || (zExp == maxexp && rounding_bumps_exp)) {if (ieee) { float_raise(float_flag_overflow | float_flag_inexact,status);
fpu/softfloat.c static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp, · line 3431
Quoted 6 lines
fpu/softfloat.c +5
-1-return packFloat16 (zSign,0x1f ,0 ); +if (overflow_to_inf) { +return packFloat16 (zSign,0x1f ,0 ); + }else { +return packFloat16 (zSign,0x1e ,0x3ff ); + }
Quoted 4 lines
3) and so you can just say
return packFloat16(zSign, 0x1f, -(increment == 0));
That's a nice trick!
Quoted 8 lines
This is slightly tricky (we rely on
packFloat16()adding the significand argument into the value it builds up, such that passing exponent X and significand -1 results in a packed value with exponent X-1 and significand all-ones.) But it's consistent with the way we handle this case in the other roundAndPackFloat* functions, so I prefer it.
Quoted 7 lines
Deferring the adjustment of increment in the
round_nearest_evencase looks at first glance like it might affect other cases, because we calculaterounding_bumps_expfrom increment. But in factrounding_bumps_expwill always have the same value either way, if you work through the different possible zSig values.
Again, I am not quite following the whole implemented algorithm of this roundAndPackFloat16; it cannot align with roundAndPackFloat[32|64] which originates softfloat(John Hauser).
Quoted 4 lines
thanks
-- PMM
Thanks!
- xiangyu