softfloat: fix floatx80 emulation bugs
v2 cover softfloat: fix floatx80 emulation bugs
Attempting to run the GCC and glibc testsuites for i686 under QEMU shows up a range of bugs in the x87 floating-point emulation. This series fixes some bugs (found both through those testsuites and through subsequent code inspection) that appear to be in the softfloat code itself rather than in the target/i386 code; I intend to address such bugs in target/i386 separately.
Note that the floatx80 code is used for both i386 and m68k emulation, but the two variants of the floatx80 format are not entirely compatible. Where the code should do different things for i386 and m68k, it consistently only does the thing that is right for i386, not the thing that is right for m68k, and my patches (specifically, the second and third patches) continue this, doing the things that are right for i386 but not for m68k.
Specifically, the formats have the following differences (based on documentation; I don't have m68k hardware to test):
* For m68k, the explicit integer bit of the significand may be either 0 or 1 for infinities and NaNs, but for i386 it must be 1 and having 0 there makes it an invalid encoding.
* For i386, when the biased exponent is 0, this is interpreted the same way as a biased exponent of 0 in an IEEE format; an explicit integer bit 0 means a subnormal value while an explicit integer bit 1 means a pseudo-denormal; the integer bit has value 2^-16382, as for a biased exponent of 1. For m68k, a biased exponent of 0 results in the integer bit having value 2^-16383, so values with integer bit 1 are normal and those with integer bit 0 are subnormal. So the least subnormal value is 2^-16445 for i386 and 2^-16446 for m68k. (This means that the i386 floatx80 format meets the IEEE definition of an extended format, which requires a certain relation between the largest and smallest exponents, but the m68k floatx80 format does not meet that definition.)
Patches 2 and 3 in this series deal with pseudo-denormals in a way that is correct for i386 but not for m68k; to support the m68k format properly, the new code in patch 3 could simply be disabled for m68k, but addition / subtraction would need more complicated changes to be correct for m68k and just disabling the new code would not make it correct (likewise, various changes elsewhere in the softfloat code would be needed to handle the m68k semantics for biased exponent 0).
This second version of the patch series includes i386-specific tests for the bugs being fixed (written to be reasonably self-contained rather than depending on libm functionality). Given the previous discussion of how some existing tests for floating-point operations that are present but not enabled fail for unrelated reasons if enabled for floatx80, this does not do anything regarding enabling such tests.
Joseph Myers (4):
softfloat: silence sNaN for conversions to/from floatx80
softfloat: fix floatx80 pseudo-denormal addition / subtraction
softfloat: fix floatx80 pseudo-denormal comparisons
softfloat: fix floatx80 pseudo-denormal round to integer
fpu/softfloat.c | 37 ++++++++++--- tests/tcg/i386/test-i386-pseudo-denormal.c | 38 +++++++++++++ tests/tcg/i386/test-i386-snan-convert.c | 63 ++++++++++++++++++++++ 3 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 tests/tcg/i386/test-i386-pseudo-denormal.c create mode 100644 tests/tcg/i386/test-i386-snan-convert.c
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
PATCH v2 1/4 softfloat: silence sNaN for conversions to/from floatx80
Conversions between IEEE floating-point formats should convert signaling NaNs to quiet NaNs. Most of those in QEMU's softfloat code do so, but those for floatx80 fail to. Fix those conversions to silence signaling NaNs as well.
--- fpu/softfloat.c | 24 +++++++--- tests/tcg/i386/test-i386-snan-convert.c | 63 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 tests/tcg/i386/test-i386-snan-convert.c
fpu/softfloat.c +18 -6
@@ -4498,7 +4498,9 @@ floatx80 float32_to_floatx80(float32 a, float_status *status) aSign =extractFloat32Sign ( a );if ( aExp ==0xFF ) {if (aSig) { -return commonNaNToFloatx80 (float32ToCommonNaN (a, status), status); +floatx80 res =commonNaNToFloatx80 (float32ToCommonNaN (a, status), + status); +return floatx80_silence_nan (res, status); } return packFloatx80(aSign, floatx80_infinity_high,
@@ -5016,7 +5018,9 @@ floatx80 float64_to_floatx80(float64 a, float_status *status) aSign =extractFloat64Sign ( a );if ( aExp ==0x7FF ) {if (aSig) { -return commonNaNToFloatx80 (float64ToCommonNaN (a, status), status); +floatx80 res =commonNaNToFloatx80 (float64ToCommonNaN (a, status), + status); +return floatx80_silence_nan (res, status); } return packFloatx80(aSign, floatx80_infinity_high,
@@ -5618,7 +5622,9 @@ float32 floatx80_to_float32(floatx80 a, float_status *status) aSign =extractFloatx80Sign ( a );if ( aExp ==0x7FFF ) {if ( (uint64_t ) ( aSig<<1 ) ) { -return commonNaNToFloat32 (floatx80ToCommonNaN (a, status), status); +float32 res =commonNaNToFloat32 (floatx80ToCommonNaN (a, status), + status); +return float32_silence_nan (res, status); }return packFloat32 ( aSign,0xFF ,0 ); }
@@ -5650,7 +5656,9 @@ float64 floatx80_to_float64(floatx80 a, float_status *status) aSign =extractFloatx80Sign ( a );if ( aExp ==0x7FFF ) {if ( (uint64_t ) ( aSig<<1 ) ) { -return commonNaNToFloat64 (floatx80ToCommonNaN (a, status), status); +float64 res =commonNaNToFloat64 (floatx80ToCommonNaN (a, status), + status); +return float64_silence_nan (res, status); }return packFloat64 ( aSign,0x7FF ,0 ); }
@@ -5681,7 +5689,9 @@ float128 floatx80_to_float128(floatx80 a, float_status *status) aExp =extractFloatx80Exp ( a ); aSign =extractFloatx80Sign ( a );if ( ( aExp ==0x7FFF ) && (uint64_t ) ( aSig<<1 ) ) { -return commonNaNToFloat128 (floatx80ToCommonNaN (a, status), status); +float128 res =commonNaNToFloat128 (floatx80ToCommonNaN (a, status), + status); +return float128_silence_nan (res, status); }shift128Right ( aSig<<1 ,0 ,16 , &zSig0, &zSig1 );return packFloat128 ( aSign, aExp, zSig0, zSig1 );
@@ -6959,7 +6969,9 @@ floatx80 float128_to_floatx80(float128 a, float_status *status) aSign =extractFloat128Sign ( a );if ( aExp ==0x7FFF ) {if ( aSig0 | aSig1 ) { -return commonNaNToFloatx80 (float128ToCommonNaN (a, status), status); +floatx80 res =commonNaNToFloatx80 (float128ToCommonNaN (a, status), + status); +return floatx80_silence_nan (res, status); }return packFloatx80 (aSign, floatx80_infinity_high, floatx80_infinity_low);
tests/tcg/i386/test-i386-snan-convert.c new file +63 -0
63 lines not on this page · raw message
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
PATCH v2 2/4 softfloat: fix floatx80 pseudo-denormal addition / subtraction
In reply to Joseph MyersThe softfloat function addFloatx80Sigs, used for addition of values with the same sign and subtraction of values with opposite sign, fails to handle the case where the two values both have biased exponent zero and there is a carry resulting from adding the significands, which can occur if one or both values are pseudo-denormals (biased exponent zero, explicit integer bit 1). Add a check for that case, so making the results match those seen on x86 hardware for pseudo-denormals.
--- fpu/softfloat.c | 6 ++++++ tests/tcg/i386/test-i386-pseudo-denormal.c | 24 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/tcg/i386/test-i386-pseudo-denormal.c
fpu/softfloat.c +6 -0
@@ -5866,6 +5866,12 @@ static floatx80 addFloatx80Sigs(floatx80 a, floatx80 b, flag zSign, zSig1 =0 ; zSig0 = aSig + bSig;if ( aExp ==0 ) { +if ((aSig | bSig) &UINT64_C (0x8000000000000000 ) && zSig0 < aSig) { +/* At least one of the values is a pseudo-denormal, +* and there is a carry out of the result. */ + zExp =1 ; + goto shiftRight1; + }if (zSig0 ==0 ) {return packFloatx80 (zSign,0 ,0 ); }
tests/tcg/i386/test-i386-pseudo-denormal.c new file +24 -0
@@ -0,0 +1,24 @@ +/* Test pseudo-denormal operations. */ + +#include <stdint.h> +#include <stdio.h> + +union u { +struct {uint64_t sig;uint16_t sign_exp; } s; +long double ld; +}; + +volatile union u ld_pseudo_m16382 = { .s = {UINT64_C (1 ) <<63 ,0 } }; + +volatile long double ld_res; + +int main (void ) +{ +int ret =0 ; + ld_res = ld_pseudo_m16382.ld + ld_pseudo_m16382.ld; +if (ld_res !=0x1p-16381L ) { +printf ("FAIL: pseudo-denormal add\n" ); + ret =1 ; + } +return ret; +}
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
PATCH v2 3/4 softfloat: fix floatx80 pseudo-denormal comparisons
In reply to Joseph MyersThe softfloat floatx80 comparisons fail to allow for pseudo-denormals, which should compare equal to corresponding values with biased exponent 1 rather than 0. Add an adjustment for that case when comparing numbers with the same sign.
Note that this fix only changes floatx80_compare_internal, not the other more specific comparison operations. That is the only comparison function for floatx80 used in the i386 port, which is the only supported port with these pseudo-denormal semantics.
--- fpu/softfloat.c | 5 +++++ tests/tcg/i386/test-i386-pseudo-denormal.c | 4 ++++ 2 files changed, 9 insertions(+)
fpu/softfloat.c +5 -0
@@ -7966,6 +7966,11 @@ static inline int floatx80_compare_internal(floatx80 a, floatx80 b,return 1 - (2 * aSign); } }else { +/* Normalize pseudo-denormals before comparison. */ +if ((a.high &0x7fff ) ==0 && a.low &UINT64_C (0x8000000000000000 )) + ++a.high; +if ((b.high &0x7fff ) ==0 && b.low &UINT64_C (0x8000000000000000 )) + ++b.high;if (a.low == b.low && a.high == b.high) {return float_relation_equal; }else {
tests/tcg/i386/test-i386-pseudo-denormal.c +4 -0
@@ -20,5 +20,9 @@ int main(void)printf ("FAIL: pseudo-denormal add\n" ); ret =1 ; } +if (ld_pseudo_m16382.ld !=0x1p-16382L ) { +printf ("FAIL: pseudo-denormal compare\n" ); + ret =1 ; + }return ret; }
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
PATCH v2 4/4 softfloat: fix floatx80 pseudo-denormal round to integer
In reply to Joseph MyersThe softfloat function floatx80_round_to_int incorrectly handles the case of a pseudo-denormal where only the high bit of the significand is set, ignoring that bit (treating the number as an exact zero) rather than treating the number as an alternative representation of +/- 2^-16382 (which may round to +/- 1 depending on the rounding mode) as hardware does. Fix this check (simplifying the code in the process).
--- fpu/softfloat.c | 2 +- tests/tcg/i386/test-i386-pseudo-denormal.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
fpu/softfloat.c +1 -1
@@ -5741,7 +5741,7 @@ floatx80 floatx80_round_to_int(floatx80 a, float_status *status) }if ( aExp <0x3FFF ) {if ( ( aExp ==0 ) - && ( (uint64_t ) (extractFloatx80Frac ( a )<<1 ) ==0 ) ) { + && ( (uint64_t ) (extractFloatx80Frac ( a ) ) ==0 ) ) {return a; } status->float_exception_flags |= float_flag_inexact;
tests/tcg/i386/test-i386-pseudo-denormal.c +10 -0
@@ -14,6 +14,7 @@ volatile long double ld_res;int main (void ) { +short cw;int ret =0 ; ld_res = ld_pseudo_m16382.ld + ld_pseudo_m16382.ld;if (ld_res !=0x1p-16381L ) {
@@ -24,5 +25,14 @@ int main(void)printf ("FAIL: pseudo-denormal compare\n" ); ret =1 ; } +/* Set round-upward. */ + __asm__volatile ("fnstcw %0" :"=m" (cw)); + cw = (cw & ~0xc00 ) |0x800 ; + __asm__volatile ("fldcw %0" : :"m" (cw)); + __asm__ ("frndint" :"=t" (ld_res) :"0" (ld_pseudo_m16382.ld)); +if (ld_res !=1.0L ) { +printf ("FAIL: pseudo-denormal round-to-integer\n" ); + ret =1 ; + }return ret; }
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
Quoted 5 lines · Joseph Myers
Joseph Myers (4):
softfloat: silence sNaN for conversions to/from floatx80
softfloat: fix floatx80 pseudo-denormal addition / subtraction
softfloat: fix floatx80 pseudo-denormal comparisons
softfloat: fix floatx80 pseudo-denormal round to integer
Thanks. Queued to a local branch with some more fpu cleanups.
r~