On 11/13/26 08:11, Philippe Mathieu-Daudé wrote:
The final negation of NMADD.fmt and NMSUB.fmt is arithmetic. With the
legacy NaN encoding (FCSR.NAN2008 = 0) QEMU returns the default NaN
(0x7ff7ffffffffffff for double precision, positive) for a NaN result,
then flipped its sign with float{32,64}_chs().

Observed on a Cavium Octeon III V0.2 (FPU V0.0, Linux 4.9.79) running
risu: "nmsub.d $f7,$f27,$f23,$f15" with $f27 = 0x7fffffffffffffff (a
signaling NaN in the legacy encoding), $f23 = 0 and
$f15 = 0x472fa6f57f408dcc gives 0x7ff7ffffffffffff on the hardware,
where QEMU gave 0xfff7ffffffffffff. The same was seen for the single
precision "nmsub.s $f1,$f18,$f22,$f21".

Do not change the sign of a NaN result when NAN2008 is 0. Non-NaN
results and the NAN2008 = 1 behavior are unchanged. NMADD.fmt, which
shares the code, was not exercised on the hardware.

The hardware (or its kernel FPU emulator) also returns a different NaN
than QEMU for "mul.d" with a signaling NaN operand and a denormal
operand: this is not addressed here.

Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
---
  target/mips/tcg/fpu_helper.c | 39 ++++++++++++++++++++++++++++--------
  1 file changed, 31 insertions(+), 8 deletions(-)

Isn't the larger problem that this should be using a fused multiply-add via float64_muladd, and thus the negation via float_muladd_negate_result (which will not affect NaN results).

Certainly MD00087 says the product uses infinite precision.


r~


diff --git a/target/mips/tcg/fpu_helper.c b/target/mips/tcg/fpu_helper.c
index d6595da6482f..e085aa5669f0 100644
--- a/target/mips/tcg/fpu_helper.c
+++ b/target/mips/tcg/fpu_helper.c
@@ -1627,12 +1627,35 @@ uint64_t HELPER(float_msub_ps)(CPUMIPSState *env, 
uint64_t fdt0,
      return ((uint64_t)fsth0 << 32) | fstl0;
  }
+/*
+ * The final negation of NMADD.fmt and NMSUB.fmt is arithmetic: with the
+ * legacy NaN encoding (FCSR.NAN2008 = 0) the sign of a NaN result is not
+ * changed.
+ */
+static inline float64 nm_negate_d(CPUMIPSState *env, float64 a)
+{
+    if (!(env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) &&
+        float64_is_any_nan(a)) {
+        return a;
+    }
+    return float64_chs(a);
+}
+
+static inline float32 nm_negate_s(CPUMIPSState *env, float32 a)
+{
+    if (!(env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) &&
+        float32_is_any_nan(a)) {
+        return a;
+    }
+    return float32_chs(a);
+}
+
  uint64_t HELPER(float_nmadd_d)(CPUMIPSState *env, uint64_t fst0,
                               uint64_t fst1, uint64_t fst2)
  {
      fst0 = float64_mul(fst0, fst1, &env->active_fpu.fp_status);
      fst0 = float64_add(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float64_chs(fst0);
+    fst0 = nm_negate_d(env, fst0);
update_fcr31(env, GETPC());
      return fst0;
@@ -1643,7 +1666,7 @@ uint32_t HELPER(float_nmadd_s)(CPUMIPSState *env, 
uint32_t fst0,
  {
      fst0 = float32_mul(fst0, fst1, &env->active_fpu.fp_status);
      fst0 = float32_add(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float32_chs(fst0);
+    fst0 = nm_negate_s(env, fst0);
update_fcr31(env, GETPC());
      return fst0;
@@ -1661,10 +1684,10 @@ uint64_t HELPER(float_nmadd_ps)(CPUMIPSState *env, 
uint64_t fdt0,
fstl0 = float32_mul(fstl0, fstl1, &env->active_fpu.fp_status);
      fstl0 = float32_add(fstl0, fstl2, &env->active_fpu.fp_status);
-    fstl0 = float32_chs(fstl0);
+    fstl0 = nm_negate_s(env, fstl0);
      fsth0 = float32_mul(fsth0, fsth1, &env->active_fpu.fp_status);
      fsth0 = float32_add(fsth0, fsth2, &env->active_fpu.fp_status);
-    fsth0 = float32_chs(fsth0);
+    fsth0 = nm_negate_s(env, fsth0);
update_fcr31(env, GETPC());
      return ((uint64_t)fsth0 << 32) | fstl0;
@@ -1675,7 +1698,7 @@ uint64_t HELPER(float_nmsub_d)(CPUMIPSState *env, 
uint64_t fst0,
  {
      fst0 = float64_mul(fst0, fst1, &env->active_fpu.fp_status);
      fst0 = float64_sub(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float64_chs(fst0);
+    fst0 = nm_negate_d(env, fst0);
update_fcr31(env, GETPC());
      return fst0;
@@ -1686,7 +1709,7 @@ uint32_t HELPER(float_nmsub_s)(CPUMIPSState *env, 
uint32_t fst0,
  {
      fst0 = float32_mul(fst0, fst1, &env->active_fpu.fp_status);
      fst0 = float32_sub(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float32_chs(fst0);
+    fst0 = nm_negate_s(env, fst0);
update_fcr31(env, GETPC());
      return fst0;
@@ -1704,10 +1727,10 @@ uint64_t HELPER(float_nmsub_ps)(CPUMIPSState *env, 
uint64_t fdt0,
fstl0 = float32_mul(fstl0, fstl1, &env->active_fpu.fp_status);
      fstl0 = float32_sub(fstl0, fstl2, &env->active_fpu.fp_status);
-    fstl0 = float32_chs(fstl0);
+    fstl0 = nm_negate_s(env, fstl0);
      fsth0 = float32_mul(fsth0, fsth1, &env->active_fpu.fp_status);
      fsth0 = float32_sub(fsth0, fsth2, &env->active_fpu.fp_status);
-    fsth0 = float32_chs(fsth0);
+    fsth0 = nm_negate_s(env, fsth0);
update_fcr31(env, GETPC());
      return ((uint64_t)fsth0 << 32) | fstl0;


Reply via email to