]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
RISC-V: Change initial value for fmin/fmax autovec reduce [PR126049]
authorIcenowy Zheng <zhengxingda@iscas.ac.cn>
Tue, 30 Jun 2026 16:11:42 +0000 (00:11 +0800)
committerRobin Dapp <robin.dapp@oss.qualcomm.com>
Wed, 1 Jul 2026 18:22:06 +0000 (20:22 +0200)
Currently the auto-vectorization of C fmin()/fmax() uses
infinity/-infinity as the initial value for reduction, which introduces
bogus infinity values when iterating over an array with only NaNs.

As all C fmin()/fmax(), RV F/D fmin/fmax, RVV vfmin/vfmax and RVV
vfredmin/vfredmax are implementing the IEEE754 minimumNumber or
maximumNumber behavior, an initial value of NaN is more suitable than
infinity when reducing the vector (if the input vector is all NaN, the
result will still be NaN and if the input vector contains non-NaN
elements they will cover the initial NaN).

Change the initial value during reduction from corresponding inf to a
quiet NaN.

PR target/126049

gcc/ChangeLog:
* config/riscv/autovec.md: Change fmin/fmax reduction initial
value from inf/-inf to NaN for proper semantics of corresponding
C funtion.

gcc/testsuite/ChangeLog:
* gcc.target/riscv/rvv/autovec/pr126049.c: New.

gcc/config/riscv/autovec.md
gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126049.c [new file with mode: 0644]

index 31584f4918c45c2adc259590871a20438602449e..f6ec19d0025f80f73a0c3baca7d01fb1232ff49d 100644 (file)
   "TARGET_VECTOR && !HONOR_SNANS (<MODE>mode)"
 {
   REAL_VALUE_TYPE rv;
-  real_inf (&rv, true);
+  real_nan (&rv, "", 1, VOIDmode);
   rtx f = const_double_from_real_value (rv, <VEL>mode);
   riscv_vector::expand_reduction (UNSPEC_REDUC_MAX,
                                  UNSPEC_REDUC_MAX_VL0_SAFE,
   "TARGET_VECTOR && !HONOR_SNANS (<MODE>mode)"
 {
   REAL_VALUE_TYPE rv;
-  real_inf (&rv, false);
+  real_nan (&rv, "", 1, VOIDmode);
   rtx f = const_double_from_real_value (rv, <VEL>mode);
   riscv_vector::expand_reduction (UNSPEC_REDUC_MIN,
                                  UNSPEC_REDUC_MIN_VL0_SAFE,
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126049.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126049.c
new file mode 100644 (file)
index 0000000..1fab446
--- /dev/null
@@ -0,0 +1,26 @@
+/* { dg-do run } */
+/* { dg-require-effective-target riscv_v_ok } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O2 -ftree-vectorize" } */
+
+#define NAN (__builtin_nan(""))
+
+__attribute__ ((noipa)) float
+maxx (float *arr, unsigned int sz)
+{
+  float val = NAN;
+  unsigned int i;
+
+  for (i = 0; i < sz; i++)
+    val = __builtin_fmaxf (val, arr[i]);
+
+  return val;
+}
+
+int
+main ()
+{
+  float arr[] = { NAN, NAN, NAN, NAN };
+  float res = maxx (arr, 4);
+  if (!__builtin_isnan (res))
+    __builtin_abort ();
+}