softfloat: Add round-to-odd rounding mode
RFC PATCH 1/1 softfloat: Add round-to-odd rounding mode
Power ISA 3.0 introduces a few quadruple precision floating point instructions that support round-to-add rounding mode. The round-to-odd mode is explained as under:
Let Z be the intermediate arithmetic result or the operand of a convert operation. If Z can be represented exactly in the target format, the result is Z. Otherwise the result is either Z1 or Z2 whichever is odd. Here Z1 and Z2 are the next larger and smaller numbers representable in the target format respectively.
---
- I am not fully sure if this the correct implementation for the above described round-to-odd rounding method. Any help is appreciated.
- Didn't bother to add round-to-odd to other floating point precision variants as round-to-odd option is currently supported only for some instructions that work on quad precision.
fpu/softfloat.c | 6 ++++++ include/fpu/softfloat.h | 1 + 2 files changed, 7 insertions(+)
fpu/softfloat.c +6 -0
@@ -1149,6 +1149,9 @@ static float128 roundAndPackFloat128(flag zSign, int32_t zExp,case float_round_down: increment = zSign && zSig2;break ; +case float_round_to_odd: + increment = !(zSig1 &0x1 ) && zSig2; +break ;default :abort (); }
@@ -1215,6 +1218,9 @@ static float128 roundAndPackFloat128(flag zSign, int32_t zExp,case float_round_down: increment = zSign && zSig2;break ; +case float_round_to_odd: + increment = !(zSig1 &0x1 ) && zSig2; +break ;default :abort (); }
include/fpu/softfloat.h +1 -0
@@ -180,6 +180,7 @@ enum { float_round_up =2 , float_round_to_zero =3 , float_round_ties_away =4 , + float_round_to_odd = 5, };/*----------------------------------------------------------------------------
Signature · 1 line
2.7.4
Quoted 10 lines · Bharata B Rao
Power ISA 3.0 introduces a few quadruple precision floating point instructions that support round-to-add rounding mode. The round-to-odd mode is explained as under:
Let Z be the intermediate arithmetic result or the operand of a convert operation. If Z can be represented exactly in the target format, the result is Z. Otherwise the result is either Z1 or Z2 whichever is odd. Here Z1 and Z2 are the next larger and smaller numbers representable in the target format respectively.
Quoted 7 lines
Signed-off-by Bharata B Rao <[email protected]>---
- I am not fully sure if this the correct implementation for the above described round-to-odd rounding method. Any help is appreciated.
- Didn't bother to add round-to-odd to other floating point precision variants as round-to-odd option is currently supported only for some instructions that work on quad precision.
ARM has a couple of instructions that also want round-to-odd, specifically for 32-bit results. So we should probably extend it to those at some point (at the moment the target/arm code just treats it like nearest-even, so gets inaccurate results).
Quoted 9 lines
fpu/softfloat.c | 6 ++++++ include/fpu/softfloat.h | 1 + 2 files changed, 7 insertions(+)fpu/softfloat.c +0
-0
fpu/softfloat.c static float128 roundAndPackFloat128(flag zSign, int32_t zExp, · line 1154
Quoted 6 lines
fpu/softfloat.c +3
-0@@ -1149,6 +1149,9 @@ static float128 roundAndPackFloat128(flag zSign, int32_t zExp,case float_round_down: increment = zSign && zSig2;break ; +case float_round_to_odd: + increment = !(zSig1 &0x1 ) && zSig2; +break ;
I think this is the right calculation, yes. The ARM spec expresses the rounding operation in terms of "force the LS bit of the mantissa to 1", but I think doing it by setting increment like this helps us get the corner cases right (like tininess detection).
The remaining part that your patch doesn't address is the handling of rounding to infinity if the precise result is too large to fit in the float128.
For ARM this is clearly defined in the spec: the rounding pseudocode doesn't permit overflow-to-infinity. This means the condition
if ( ( roundingMode == float_round_to_zero )
|| ( zSign && ( roundingMode == float_round_up ) )
|| ( ! zSign && ( roundingMode == float_round_down ) )which controls "do we return the maximum-float-value?" needs an extra (roundingMode == float_round_to_odd) clause.
Hopefully those semantics work for the PPC case too?
Quoted 18 lines
fpu/softfloat.c +3
-0default :abort (); }@@ -1215,6 +1218,9 @@ static float128 roundAndPackFloat128(flag zSign, int32_t zExp,case float_round_down: increment = zSign && zSig2;break ; +case float_round_to_odd: + increment = !(zSig1 &0x1 ) && zSig2; +break ;default :abort (); }include/fpu/softfloat.h +0
-0
include/fpu/softfloat.h enum { · line 183
Quoted 4 lines
include/fpu/softfloat.h +1
-0@@ -180,6 +180,7 @@ enum { float_round_up =2 , float_round_to_zero =3 , float_round_ties_away =4 , + float_round_to_odd = 5,
Maybe worth a comment:
/* Not an IEEE rounding mode: round to the closest odd mantissa value */
since all our other rounding modes are as defined in the IEEE spec.
include/fpu/softfloat.h enum {
Quoted 4 lines
include/fpu/softfloat.h +0
-1};/*---------------------------------------------------------------------------- --
thanks
-- PMM
Quoted 2 lines · Bharata B Rao
Power ISA 3.0 introduces a few quadruple precision floating point instructions that support round-to-add rounding mode. The
s/add/odd/
Quoted 7 lines
round-to-odd mode is explained as under:
Let Z be the intermediate arithmetic result or the operand of a convert operation. If Z can be represented exactly in the target format, the result is Z. Otherwise the result is either Z1 or Z2 whichever is odd. Here Z1 and Z2 are the next larger and smaller numbers representable in the target format respectively.
IEEE 854 and thus POSIX <float.h> specifies round-to-zero, round-to-pos-inf, round-to-neg-inf, and round-to-even. It sounds like round-to-odd is similar to round-to-even, only that the ties are broken in the opposite direction. I don't know if portable C code can even request this mode (although float.h is allowed to add additional implementation-defined values to FLT_ROUNDS), but since the hardware can do it, I guess we have to emulate it.
Quoted 5 lines
Signed-off-by Bharata B Rao <[email protected]>---
- I am not fully sure if this the correct implementation for the above described round-to-odd rounding method. Any help is appreciated.
I don't know the code either, but if round-to-even is implemented correctly, it sounds like round-to-odd is a one-off tweak from that.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 9 lines · Eric Blake
Quoted 7 lines · Bharata B Rao
round-to-odd mode is explained as under:
Let Z be the intermediate arithmetic result or the operand of a convert operation. If Z can be represented exactly in the target format, the result is Z. Otherwise the result is either Z1 or Z2 whichever is odd. Here Z1 and Z2 are the next larger and smaller numbers representable in the target format respectively.
Quoted 4 lines
IEEE 854 and thus POSIX <
float.h> specifies round-to-zero, round-to-pos-inf, round-to-neg-inf, and round-to-even. It sounds like round-to-odd is similar to round-to-even, only that the ties are broken in the opposite direction.
No, round-to-even does "round ties to even" -- the rounding is to the closest representable number, and only if the two surrounding numbers are both equally near do we pick the even one. round-to-odd is "always round to the odd number, even if that's much further from the infinitely-precise result than the even number is". (This is also sometimes called von Neumann rounding or sticky rounding -- the motivation is that you can for instance do 64bit -> 32bit -> 16bit conversions in two steps without double rounding errors if you do the first conversion with round-to-odd.)
thanks
-- PMM
Quoted 37 lines · Bharata B Rao
Power ISA 3.0 introduces a few quadruple precision floating point instructions that support round-to-add rounding mode. The round-to-odd mode is explained as under:
Let Z be the intermediate arithmetic result or the operand of a convert operation. If Z can be represented exactly in the target format, the result is Z. Otherwise the result is either Z1 or Z2 whichever is odd. Here Z1 and Z2 are the next larger and smaller numbers representable in the target format respectively.
Signed-off-by Bharata B Rao <[email protected]>---
- I am not fully sure if this the correct implementation for the above described round-to-odd rounding method. Any help is appreciated.
- Didn't bother to add round-to-odd to other floating point precision variants as round-to-odd option is currently supported only for some instructions that work on quad precision.fpu/softfloat.c | 6 ++++++ include/fpu/softfloat.h | 1 + 2 files changed, 7 insertions(+)fpu/softfloat.c +3
-0@@ -1149,6 +1149,9 @@ static float128 roundAndPackFloat128(flag zSign, int32_t zExp,case float_round_down: increment = zSign && zSig2;break ; +case float_round_to_odd: + increment = !(zSig1 &0x1 ) && zSig2; +break ;default :abort (); }
fpu/softfloat.c static float128 roundAndPackFloat128(flag zSign, int32_t zExp, · line 1226
Quoted 9 lines
fpu/softfloat.c +3
-0@@ -1215,6 +1218,9 @@ static float128 roundAndPackFloat128(flag zSign, int32_t zExp,case float_round_down: increment = zSign && zSig2;break ; +case float_round_to_odd: + increment = !(zSig1 &0x1 ) && zSig2; +break ;default :abort (); }
I believe you've missed the section in between that deals with round-to-largest or to infinity:
if ( ( roundingMode == float_round_to_zero )
|| ( zSign && ( roundingMode == float_round_up ) )
|| ( ! zSign && ( roundingMode == float_round_down ) )
) {The description in see in the manual on page 387 is more precise than what you quote above:
# If IR is exact, choose IR.
# Otherwise choose NL, and if Guard=1, Round=1 or Sticky=1,
# the least significant bit of the result is set to 1.
The "choose NL" part means round-to-zero, which means that we do not overflow to infinity. And since FLOAT128_MAX is already odd, we're done.
Otherwise this looks ok.
r~
Quoted 9 lines · Richard Henderson
The description in see in the manual on page 387 is more precise than what you quote above:
# If IR is exact, choose IR.
# Otherwise choose NL, and if Guard=1, Round=1 or Sticky=1,# the least significant bit of the result is set to 1.The "choose NL" part means round-to-zero, which means that we do not overflow to infinity. And since
FLOAT128_MAXis already odd, we're done.
...so it does have the same semantics as ARM. Good :-)
thanks
-- PMM
Quoted 51 lines · Richard Henderson
Quoted 37 lines · Bharata B Rao
Power ISA 3.0 introduces a few quadruple precision floating point instructions that support round-to-add rounding mode. The round-to-odd mode is explained as under:
Let Z be the intermediate arithmetic result or the operand of a convert operation. If Z can be represented exactly in the target format, the result is Z. Otherwise the result is either Z1 or Z2 whichever is odd. Here Z1 and Z2 are the next larger and smaller numbers representable in the target format respectively.
Signed-off-by Bharata B Rao <[email protected]>---
- I am not fully sure if this the correct implementation for the above described round-to-odd rounding method. Any help is appreciated.
- Didn't bother to add round-to-odd to other floating point precision variants as round-to-odd option is currently supported only for some instructions that work on quad precision.fpu/softfloat.c | 6 ++++++ include/fpu/softfloat.h | 1 + 2 files changed, 7 insertions(+)fpu/softfloat.c +3
-0@@ -1149,6 +1149,9 @@ static float128 roundAndPackFloat128(flag zSign, int32_t zExp,case float_round_down: increment = zSign && zSig2;break ; +case float_round_to_odd: + increment = !(zSig1 &0x1 ) && zSig2; +break ;default :abort (); }fpu/softfloat.c static float128 roundAndPackFloat128(flag zSign, int32_t zExp, · line 1226
Quoted 9 lines
fpu/softfloat.c +3
-0@@ -1215,6 +1218,9 @@ static float128 roundAndPackFloat128(flag zSign, int32_t zExp,case float_round_down: increment = zSign && zSig2;break ; +case float_round_to_odd: + increment = !(zSig1 &0x1 ) && zSig2; +break ;default :abort (); }I believe you've missed the section in between that deals with round-to-largest or to infinity:
Quoted 4 lines
if ( ( roundingMode == float_round_to_zero )
|| ( zSign && ( roundingMode == float_round_up ) )
|| ( ! zSign && ( roundingMode == float_round_down ) )
) {Addresed in v1. Thanks Richard and Peter for pointing this out.
Quoted 3 lines
The description in see in the manual on page 387 is more precise than what you quote above:
Right. I quoted from page 385 as it is more concise for patch description. However if you think the more precise definition of page 387 is appropriate in commit, I can put it.
Regards,
Bharata.