Skip to contentSkip to conversation

softfloat: Allow runtime choice of default NaN value

RFC PATCH In review Peter Maydell 2 participants 2 messages Nov 28, 2024

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 softfloat: Allow runtime choice of default NaN value

Currently we hardcode the default NaN value in parts64_default_nan() using a compile-time ifdef ladder. This is awkward for two cases:
* for single-QEMU-binary we can't hard-code target-specifics like this
* for Arm FEAT_AFP the default NaN value depends on FPCR.AH (specifically the sign bit is different)

Add a field to float_status to specify the default NaN value; fall back to the old ifdef behaviour if these are not set.

The default NaN value is specified by setting a uint8_t to a pattern corresponding to the sign and upper fraction parts of the NaN; the lower bits of the fraction are set from bit 0 of the pattern.

This is an RFC to ask for opinions on whether this is the right way to let the target set its default NaN. I can't decide whether I think encoding it into a uint8_t like that is clever, or merely too clever :-)

The other options would be e.g. separate
bool default_nan_sign;
uint8_t default_nan_frac;
or an enum for the frac possibilities ("default_nan_frac_01__1", "default_nan_frac_010__0", etc ???). The ones we currently need are:

frac 1....1 sign 0 (m68k, sparc), sign 1 (hexagon)
(with sign 0 for m68k, sparc; sign 1 for hexagon)
frac 10...0
(with sign 0 for many targets, sign 1 for i386, microblaze)
frac 01...1
(always with sign 0; sh4, some MIPS configs)
frac 010..0
(always with sign 0; hppa)

---
 include/fpu/softfloat-types.h  | 10 ++++++
 fpu/softfloat-specialize.c.inc | 61 ++++++++++++++++++++++------------
 2 files changed, 49 insertions(+), 22 deletions(-)
include/fpu/softfloat-types.h +10 -0
index 6e237fb697d..0a0508fe2d8 100644
@@ -285,6 +285,16 @@ typedef struct float_status {
     /* should denormalised inputs go to zero and set the input_denormal flag? */
     bool flush_inputs_to_zero;
     bool default_nan_mode;
+    /*
+     * The pattern to use for the default NaN. Here the high bit specifies
+     * the default NaN's sign bit, and bits 6..0 specify the high bits of the
+     * fractional part. The low bits of the fractional part are copies of bit 0.
+     * The exponent of the default NaN is (as for any NaN) always all 1s.
+     * Note that a value of 0 here is not a valid NaN. The target must set
+     * this to the correct non-zero value, or we will assert when trying to
+     * create a default NaN.
+     */
+    uint8_t default_nan_pattern;
     /*
      * The flags below are not used on all specializations and may
      * constant fold away (see snan_bit_is_one()/no_signalling_nans() in
fpu/softfloat-specialize.c.inc +39 -22

74 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Peter Maydell

Currently we hardcode the default NaN value in parts64_default_nan() using a compile-time ifdef ladder. This is awkward for two cases:
* for single-QEMU-binary we can't hard-code target-specifics like this
* for Arm FEAT_AFP the default NaN value depends on FPCR.AH (specifically the sign bit is different)

Add a field to float_status to specify the default NaN value; fall back to the old ifdef behaviour if these are not set.

The default NaN value is specified by setting a uint8_t to a pattern corresponding to the sign and upper fraction parts of the NaN; the lower bits of the fraction are set from bit 0 of the pattern.

Quoted 4 lines

This is an RFC to ask for opinions on whether this is the right way to let the target set its default NaN. I can't decide whether I think encoding it into a uint8_t like that is clever, or merely too clever :-)

I think that's perfectly fine.

fpu/softfloat-specialize.c.inc static void parts64_default_nan(FloatParts64 *p, float_status *status) · line 156

Quoted 10 lines

#elif defined(TARGET_HEXAGON)

fpu/softfloat-specialize.c.inc +7 -2
-    sign = 1;
-    frac = ~0ULL;
+        /*
+         * Sign bit set, all frac bits set. This is an odd special case,
+         * where our value doesn't match up with the snan_bit_is_one setting.
+         * This is because for Hexagon the returned value is always -1,
+         * not a real NaN value.
+         */
+        dnan_pattern = 0b11111111;

It's a real (signalling) nan value -- the oddity is that the default nan is not quiet.

r~