All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Subject: [PULL 08/72] softfloat: Allow runtime choice of inf * 0 + NaN result
Date: Wed, 11 Dec 2024 16:19:00 +0000	[thread overview]
Message-ID: <20241211162004.2795499-9-peter.maydell@linaro.org> (raw)
In-Reply-To: <20241211162004.2795499-1-peter.maydell@linaro.org>

IEEE 758 does not define a fixed rule for what NaN to return in
the case of a fused multiply-add of inf * 0 + NaN. Different
architectures thus do different things:
 * some return the default NaN
 * some return the input NaN
 * Arm returns the default NaN if the input NaN is quiet,
   and the input NaN if it is signalling

We want to make this logic be runtime selected rather than
hardcoded into the binary, because:
 * this will let us have multiple targets in one QEMU binary
 * the Arm FEAT_AFP architectural feature includes letting
   the guest select a NaN propagation rule at runtime

In this commit we add an enum for the propagation rule, the field in
float_status, and the corresponding getters and setters.  We change
pickNaNMulAdd to honour this, but because all targets still leave
this field at its default 0 value, the fallback logic will pick the
rule type with the old ifdef ladder.

Note that four architectures both use the muladd softfloat functions
and did not have a branch of the ifdef ladder to specify their
behaviour (and so were ending up with the "default" case, probably
wrongly): i386, HPPA, SH4 and Tricore.  SH4 and Tricore both set
default_nan_mode, and so will never get into pickNaNMulAdd().  For
HPPA and i386 we retain the same behaviour as the old default-case,
which is to not ever return the default NaN.  This might not be
correct but it is not a behaviour change.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-id: 20241202131347.498124-4-peter.maydell@linaro.org
---
 include/fpu/softfloat-helpers.h | 11 ++++
 include/fpu/softfloat-types.h   | 23 +++++++++
 fpu/softfloat-specialize.c.inc  | 91 ++++++++++++++++++++++-----------
 3 files changed, 95 insertions(+), 30 deletions(-)

diff --git a/include/fpu/softfloat-helpers.h b/include/fpu/softfloat-helpers.h
index 453188de70b..0bf44dc6087 100644
--- a/include/fpu/softfloat-helpers.h
+++ b/include/fpu/softfloat-helpers.h
@@ -81,6 +81,12 @@ static inline void set_float_2nan_prop_rule(Float2NaNPropRule rule,
     status->float_2nan_prop_rule = rule;
 }
 
+static inline void set_float_infzeronan_rule(FloatInfZeroNaNRule rule,
+                                             float_status *status)
+{
+    status->float_infzeronan_rule = rule;
+}
+
 static inline void set_flush_to_zero(bool val, float_status *status)
 {
     status->flush_to_zero = val;
@@ -137,6 +143,11 @@ static inline Float2NaNPropRule get_float_2nan_prop_rule(float_status *status)
     return status->float_2nan_prop_rule;
 }
 
+static inline FloatInfZeroNaNRule get_float_infzeronan_rule(float_status *status)
+{
+    return status->float_infzeronan_rule;
+}
+
 static inline bool get_flush_to_zero(float_status *status)
 {
     return status->flush_to_zero;
diff --git a/include/fpu/softfloat-types.h b/include/fpu/softfloat-types.h
index 8f39691dfd0..47bb22c4e25 100644
--- a/include/fpu/softfloat-types.h
+++ b/include/fpu/softfloat-types.h
@@ -207,6 +207,28 @@ typedef enum __attribute__((__packed__)) {
     float_2nan_prop_x87,
 } Float2NaNPropRule;
 
+/*
+ * Rule for result of fused multiply-add 0 * Inf + NaN.
+ * This must be a NaN, but implementations differ on whether this
+ * is the input NaN or the default NaN.
+ *
+ * You don't need to set this if default_nan_mode is enabled.
+ * When not in default-NaN mode, it is an error for the target
+ * not to set the rule in float_status if it uses muladd, and we
+ * will assert if we need to handle an input NaN and no rule was
+ * selected.
+ */
+typedef enum __attribute__((__packed__)) {
+    /* No propagation rule specified */
+    float_infzeronan_none = 0,
+    /* Result is never the default NaN (so always the input NaN) */
+    float_infzeronan_dnan_never,
+    /* Result is always the default NaN */
+    float_infzeronan_dnan_always,
+    /* Result is the default NaN if the input NaN is quiet */
+    float_infzeronan_dnan_if_qnan,
+} FloatInfZeroNaNRule;
+
 /*
  * Floating Point Status. Individual architectures may maintain
  * several versions of float_status for different functions. The
@@ -219,6 +241,7 @@ typedef struct float_status {
     FloatRoundMode float_rounding_mode;
     FloatX80RoundPrec floatx80_rounding_precision;
     Float2NaNPropRule float_2nan_prop_rule;
+    FloatInfZeroNaNRule float_infzeronan_rule;
     bool tininess_before_rounding;
     /* should denormalised results go to zero and set the inexact flag? */
     bool flush_to_zero;
diff --git a/fpu/softfloat-specialize.c.inc b/fpu/softfloat-specialize.c.inc
index 81a67eb67b5..f5b422e07b5 100644
--- a/fpu/softfloat-specialize.c.inc
+++ b/fpu/softfloat-specialize.c.inc
@@ -475,6 +475,8 @@ static int pickNaN(FloatClass a_cls, FloatClass b_cls,
 static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
                          bool infzero, float_status *status)
 {
+    FloatInfZeroNaNRule rule = status->float_infzeronan_rule;
+
     /*
      * We guarantee not to require the target to tell us how to
      * pick a NaN if we're always returning the default NaN.
@@ -482,14 +484,68 @@ static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
      * specify.
      */
     assert(!status->default_nan_mode);
+
+    if (rule == float_infzeronan_none) {
+        /*
+         * Temporarily fall back to ifdef ladder
+         */
 #if defined(TARGET_ARM)
-    /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
-     * the default NaN
-     */
-    if (infzero && is_qnan(c_cls)) {
-        return 3;
+        /*
+         * For ARM, the (inf,zero,qnan) case returns the default NaN,
+         * but (inf,zero,snan) returns the input NaN.
+         */
+        rule = float_infzeronan_dnan_if_qnan;
+#elif defined(TARGET_MIPS)
+        if (snan_bit_is_one(status)) {
+            /*
+             * For MIPS systems that conform to IEEE754-1985, the (inf,zero,nan)
+             * case sets InvalidOp and returns the default NaN
+             */
+            rule = float_infzeronan_dnan_always;
+        } else {
+            /*
+             * For MIPS systems that conform to IEEE754-2008, the (inf,zero,nan)
+             * case sets InvalidOp and returns the input value 'c'
+             */
+            rule = float_infzeronan_dnan_never;
+        }
+#elif defined(TARGET_PPC) || defined(TARGET_SPARC) || \
+    defined(TARGET_XTENSA) || defined(TARGET_HPPA) || \
+    defined(TARGET_I386) || defined(TARGET_LOONGARCH)
+        /*
+         * For LoongArch systems that conform to IEEE754-2008, the (inf,zero,nan)
+         * case sets InvalidOp and returns the input value 'c'
+         */
+        /*
+         * For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
+         * to return an input NaN if we have one (ie c) rather than generating
+         * a default NaN
+         */
+        rule = float_infzeronan_dnan_never;
+#elif defined(TARGET_S390X)
+        rule = float_infzeronan_dnan_always;
+#endif
     }
 
+    if (infzero) {
+        /*
+         * Inf * 0 + NaN -- some implementations return the default NaN here,
+         * and some return the input NaN.
+         */
+        switch (rule) {
+        case float_infzeronan_dnan_never:
+            return 2;
+        case float_infzeronan_dnan_always:
+            return 3;
+        case float_infzeronan_dnan_if_qnan:
+            return is_qnan(c_cls) ? 3 : 2;
+        default:
+            g_assert_not_reached();
+        }
+    }
+
+#if defined(TARGET_ARM)
+
     /* This looks different from the ARM ARM pseudocode, because the ARM ARM
      * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
      */
@@ -508,13 +564,6 @@ static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
     }
 #elif defined(TARGET_MIPS)
     if (snan_bit_is_one(status)) {
-        /*
-         * For MIPS systems that conform to IEEE754-1985, the (inf,zero,nan)
-         * case sets InvalidOp and returns the default NaN
-         */
-        if (infzero) {
-            return 3;
-        }
         /* Prefer sNaN over qNaN, in the a, b, c order. */
         if (is_snan(a_cls)) {
             return 0;
@@ -530,10 +579,6 @@ static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
             return 2;
         }
     } else {
-        /*
-         * For MIPS systems that conform to IEEE754-2008, the (inf,zero,nan)
-         * case sets InvalidOp and returns the input value 'c'
-         */
         /* Prefer sNaN over qNaN, in the c, a, b order. */
         if (is_snan(c_cls)) {
             return 2;
@@ -550,11 +595,6 @@ static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
         }
     }
 #elif defined(TARGET_LOONGARCH64)
-    /*
-     * For LoongArch systems that conform to IEEE754-2008, the (inf,zero,nan)
-     * case sets InvalidOp and returns the input value 'c'
-     */
-
     /* Prefer sNaN over qNaN, in the c, a, b order. */
     if (is_snan(c_cls)) {
         return 2;
@@ -570,11 +610,6 @@ static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
         return 1;
     }
 #elif defined(TARGET_PPC)
-    /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
-     * to return an input NaN if we have one (ie c) rather than generating
-     * a default NaN
-     */
-
     /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
      * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
      */
@@ -586,10 +621,6 @@ static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
         return 1;
     }
 #elif defined(TARGET_S390X)
-    if (infzero) {
-        return 3;
-    }
-
     if (is_snan(a_cls)) {
         return 0;
     } else if (is_snan(b_cls)) {
-- 
2.34.1



  parent reply	other threads:[~2024-12-11 16:27 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-11 16:18 [PULL 00/72] target-arm queue Peter Maydell
2024-12-11 16:18 ` [PULL 01/72] hw/net/lan9118: Extract lan9118_phy Peter Maydell
2024-12-11 16:18 ` [PULL 02/72] hw/net/lan9118_phy: Reuse in imx_fec and consolidate implementations Peter Maydell
2024-12-11 16:18 ` [PULL 03/72] hw/net/lan9118_phy: Fix off-by-one error in MII_ANLPAR register Peter Maydell
2024-12-11 16:18 ` [PULL 04/72] hw/net/lan9118_phy: Reuse MII constants Peter Maydell
2024-12-11 16:18 ` [PULL 05/72] hw/net/lan9118_phy: Add missing 100 mbps full duplex advertisement Peter Maydell
2024-12-11 16:18 ` [PULL 06/72] fpu: handle raising Invalid for infzero in pick_nan_muladd Peter Maydell
2024-12-11 16:18 ` [PULL 07/72] fpu: Check for default_nan_mode before calling pickNaNMulAdd Peter Maydell
2024-12-11 16:19 ` Peter Maydell [this message]
2024-12-11 16:19 ` [PULL 09/72] tests/fp: Explicitly set inf-zero-nan rule Peter Maydell
2024-12-11 16:19 ` [PULL 10/72] target/arm: Set FloatInfZeroNaNRule explicitly Peter Maydell
2024-12-11 16:19 ` [PULL 11/72] target/s390: " Peter Maydell
2024-12-11 16:19 ` [PULL 12/72] target/ppc: " Peter Maydell
2024-12-11 16:19 ` [PULL 13/72] target/mips: " Peter Maydell
2024-12-11 16:19 ` [PULL 14/72] target/sparc: " Peter Maydell
2024-12-11 16:19 ` [PULL 15/72] target/xtensa: " Peter Maydell
2024-12-11 16:19 ` [PULL 16/72] target/x86: " Peter Maydell
2024-12-11 16:19 ` [PULL 17/72] target/loongarch: " Peter Maydell
2024-12-11 16:19 ` [PULL 18/72] target/hppa: " Peter Maydell
2024-12-11 16:19 ` [PULL 19/72] softfloat: Pass have_snan to pickNaNMulAdd Peter Maydell
2024-12-11 16:19 ` [PULL 20/72] softfloat: Allow runtime choice of NaN propagation for muladd Peter Maydell
2024-12-11 16:19 ` [PULL 21/72] tests/fp: Explicitly set 3-NaN propagation rule Peter Maydell
2024-12-11 16:19 ` [PULL 22/72] target/arm: Set Float3NaNPropRule explicitly Peter Maydell
2024-12-11 16:19 ` [PULL 23/72] target/loongarch: " Peter Maydell
2024-12-11 16:19 ` [PULL 24/72] target/ppc: " Peter Maydell
2024-12-11 16:19 ` [PULL 25/72] target/s390x: " Peter Maydell
2024-12-11 16:19 ` [PULL 26/72] target/sparc: " Peter Maydell
2024-12-11 16:19 ` [PULL 27/72] target/mips: " Peter Maydell
2024-12-11 16:19 ` [PULL 28/72] target/xtensa: " Peter Maydell
2024-12-11 16:19 ` [PULL 29/72] target/i386: " Peter Maydell
2024-12-11 16:19 ` [PULL 30/72] target/hppa: " Peter Maydell
2024-12-11 16:19 ` [PULL 31/72] fpu: Remove use_first_nan field from float_status Peter Maydell
2024-12-11 16:19 ` [PULL 32/72] target/m68k: Don't pass NULL float_status to floatx80_default_nan() Peter Maydell
2024-12-11 16:19 ` [PULL 33/72] softfloat: Create floatx80 default NaN from parts64_default_nan Peter Maydell
2024-12-11 16:19 ` [PULL 34/72] target/loongarch: Use normal float_status in fclass_s and fclass_d helpers Peter Maydell
2024-12-11 16:19 ` [PULL 35/72] target/m68k: In frem helper, initialize local float_status from env->fp_status Peter Maydell
2024-12-11 16:19 ` [PULL 36/72] target/m68k: Init local float_status from env fp_status in gdb get/set reg Peter Maydell
2024-12-11 16:19 ` [PULL 37/72] target/sparc: Initialize local scratch float_status from env->fp_status Peter Maydell
2024-12-11 16:19 ` [PULL 38/72] target/ppc: Use env->fp_status in helper_compute_fprf functions Peter Maydell
2024-12-11 16:19 ` [PULL 39/72] target/arm: Copy entire float_status in is_ebf Peter Maydell
2024-12-11 16:19 ` [PULL 40/72] fpu: Allow runtime choice of default NaN value Peter Maydell
2024-12-11 16:19 ` [PULL 41/72] tests/fp: Set default NaN pattern explicitly Peter Maydell
2024-12-11 16:19 ` [PULL 42/72] target/microblaze: " Peter Maydell
2024-12-11 16:19 ` [PULL 43/72] target/i386: " Peter Maydell
2024-12-11 16:19 ` [PULL 44/72] target/hppa: " Peter Maydell
2024-12-11 16:19 ` [PULL 45/72] target/alpha: " Peter Maydell
2024-12-11 16:19 ` [PULL 46/72] target/arm: " Peter Maydell
2024-12-11 16:19 ` [PULL 47/72] target/loongarch: " Peter Maydell
2024-12-11 16:19 ` [PULL 48/72] target/m68k: " Peter Maydell
2024-12-11 16:19 ` [PULL 49/72] target/mips: " Peter Maydell
2024-12-11 16:19 ` [PULL 50/72] target/openrisc: " Peter Maydell
2024-12-11 16:19 ` [PULL 51/72] target/ppc: " Peter Maydell
2024-12-11 16:19 ` [PULL 52/72] target/sh4: " Peter Maydell
2024-12-11 16:19 ` [PULL 53/72] target/rx: " Peter Maydell
2024-12-11 16:19 ` [PULL 54/72] target/s390x: " Peter Maydell
2024-12-11 16:19 ` [PULL 55/72] target/sparc: " Peter Maydell
2024-12-11 16:19 ` [PULL 56/72] target/xtensa: " Peter Maydell
2024-12-11 16:19 ` [PULL 57/72] target/hexagon: " Peter Maydell
2024-12-11 16:19 ` [PULL 58/72] target/riscv: " Peter Maydell
2024-12-11 16:19 ` [PULL 59/72] target/tricore: " Peter Maydell
2024-12-11 16:19 ` [PULL 60/72] fpu: Remove default handling for dnan_pattern Peter Maydell
2024-12-11 16:19 ` [PULL 61/72] softfloat: Inline pickNaNMulAdd Peter Maydell
2024-12-11 16:19 ` [PULL 62/72] softfloat: Use goto for default nan case in pick_nan_muladd Peter Maydell
2024-12-11 16:19 ` [PULL 63/72] softfloat: Remove which from parts_pick_nan_muladd Peter Maydell
2024-12-11 16:19 ` [PULL 64/72] softfloat: Pad array size in pick_nan_muladd Peter Maydell
2024-12-11 16:19 ` [PULL 65/72] softfloat: Move propagateFloatx80NaN to softfloat.c Peter Maydell
2024-12-11 16:19 ` [PULL 66/72] softfloat: Use parts_pick_nan in propagateFloatx80NaN Peter Maydell
2024-12-11 16:19 ` [PULL 67/72] softfloat: Inline pickNaN Peter Maydell
2024-12-11 16:20 ` [PULL 68/72] softfloat: Share code between parts_pick_nan cases Peter Maydell
2024-12-11 16:20 ` [PULL 69/72] softfloat: Sink frac_cmp in parts_pick_nan until needed Peter Maydell
2024-12-11 16:20 ` [PULL 70/72] softfloat: Replace WHICH with RET in parts_pick_nan Peter Maydell
2024-12-11 16:20 ` [PULL 71/72] MAINTAINERS: update email address for Leif Lindholm Peter Maydell
2024-12-11 16:20 ` [PULL 72/72] MAINTAINERS: Add correct email address for Vikram Garhwal Peter Maydell
2024-12-13  1:20 ` [PULL 00/72] target-arm queue Stefan Hajnoczi
2024-12-13 10:44   ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241211162004.2795499-9-peter.maydell@linaro.org \
    --to=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.