[PATCH 2/5] riscv: Optimize fmaximum and fminimum

Julian Zhu <[email protected]> Thu, 6 Aug 2026 18:25:28 +0800
Newsgroups gmane.comp.lib.glibc.alpha
Message-ID <[email protected]>
Add RISC-V float and double implementations for the NaN-propagating C23 maximum and minimum operations.  Handle NaN inputs explicitly, then use fmax or fmin for ordered operands.

Signed-off-by: Julian Zhu <[email protected]>
---
 sysdeps/riscv/rvd/s_fmaximum.c  | 37 +++++++++++++++++++++++++++++++++
 sysdeps/riscv/rvd/s_fminimum.c  | 37 +++++++++++++++++++++++++++++++++
 sysdeps/riscv/rvf/s_fmaximumf.c | 34 ++++++++++++++++++++++++++++++
 sysdeps/riscv/rvf/s_fminimumf.c | 34 ++++++++++++++++++++++++++++++
 4 files changed, 142 insertions(+)
 create mode 100644 sysdeps/riscv/rvd/s_fmaximum.c
 create mode 100644 sysdeps/riscv/rvd/s_fminimum.c
 create mode 100644 sysdeps/riscv/rvf/s_fmaximumf.c
 create mode 100644 sysdeps/riscv/rvf/s_fminimumf.c

diff --git a/sysdeps/riscv/rvd/s_fmaximum.c b/sysdeps/riscv/rvd/s_fmaximum.c
new file mode 100644
index 0000000000..d514fb17a6
--- /dev/null
+++ b/sysdeps/riscv/rvd/s_fmaximum.c
@@ -0,0 +1,37 @@
+/* fmaximum().  RISC-V version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library.  If not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <math.h>
+#include <libm-alias-double.h>
+
+/* fmaximum is the IEEE 754-2019 maximum operation: like fmaximum_num but
+   NaN-propagating, so a NaN operand yields a quiet NaN result (and a
+   signaling NaN also raises the invalid exception).  RISC-V's fmax.d gives
+   the correct result whenever neither operand is NaN, so only the NaN case
+   needs to be handled explicitly.  */
+
+double
+__fmaximum (double x, double y)
+{
+  double res;
+  if (__glibc_unlikely (isunordered (x, y)))
+    return x + y;
+  asm ("fmax.d %0, %1, %2" : "=f" (res) : "f" (x), "f" (y));
+  return res;
+}
+libm_alias_double (__fmaximum, fmaximum)
diff --git a/sysdeps/riscv/rvd/s_fminimum.c b/sysdeps/riscv/rvd/s_fminimum.c
new file mode 100644
index 0000000000..608b37bed5
--- /dev/null
+++ b/sysdeps/riscv/rvd/s_fminimum.c
@@ -0,0 +1,37 @@
+/* fminimum().  RISC-V version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library.  If not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <math.h>
+#include <libm-alias-double.h>
+
+/* fminimum is the IEEE 754-2019 minimum operation: like fminimum_num but
+   NaN-propagating, so a NaN operand yields a quiet NaN result (and a
+   signaling NaN also raises the invalid exception).  RISC-V's fmin.d gives
+   the correct result whenever neither operand is NaN, so only the NaN case
+   needs to be handled explicitly.  */
+
+double
+__fminimum (double x, double y)
+{
+  double res;
+  if (__glibc_unlikely (isunordered (x, y)))
+    return x + y;
+  asm ("fmin.d %0, %1, %2" : "=f" (res) : "f" (x), "f" (y));
+  return res;
+}
+libm_alias_double (__fminimum, fminimum)
diff --git a/sysdeps/riscv/rvf/s_fmaximumf.c b/sysdeps/riscv/rvf/s_fmaximumf.c
new file mode 100644
index 0000000000..daa71f0f34
--- /dev/null
+++ b/sysdeps/riscv/rvf/s_fmaximumf.c
@@ -0,0 +1,34 @@
+/* fmaximumf().  RISC-V version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library.  If not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <math.h>
+#include <libm-alias-float.h>
+
+/* See s_fmaximum.c for a description of the semantics.  RISC-V's fmax.s
+   handles the ordered case, so only NaNs need explicit handling.  */
+
+float
+__fmaximumf (float x, float y)
+{
+  float res;
+  if (__glibc_unlikely (isunordered (x, y)))
+    return x + y;
+  asm ("fmax.s %0, %1, %2" : "=f" (res) : "f" (x), "f" (y));
+  return res;
+}
+libm_alias_float (__fmaximum, fmaximum)
diff --git a/sysdeps/riscv/rvf/s_fminimumf.c b/sysdeps/riscv/rvf/s_fminimumf.c
new file mode 100644
index 0000000000..2dba5412f8
--- /dev/null
+++ b/sysdeps/riscv/rvf/s_fminimumf.c
@@ -0,0 +1,34 @@
+/* fminimumf().  RISC-V version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library.  If not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <math.h>
+#include <libm-alias-float.h>
+
+/* See s_fminimum.c for a description of the semantics.  RISC-V's fmin.s
+   handles the ordered case, so only NaNs need explicit handling.  */
+
+float
+__fminimumf (float x, float y)
+{
+  float res;
+  if (__glibc_unlikely (isunordered (x, y)))
+    return x + y;
+  asm ("fmin.s %0, %1, %2" : "=f" (res) : "f" (x), "f" (y));
+  return res;
+}
+libm_alias_float (__fminimum, fminimum)
-- 
2.53.0