[1/2] RISC-V: Add implementation for fmaximum and fminimum

Message ID 20240911141952.37217-1-jz531210@gmail.com
State New
Headers
Series [1/2] RISC-V: Add implementation for fmaximum and fminimum |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed

Commit Message

Julian Zhu Sept. 11, 2024, 2:19 p.m. UTC
  Implemented `fmaximum{f}` and `fminimum{f}` using hard-float `fclass.fmt` and `f{max,min}.fmt` instructions to generate more simplified code.

Signed-off-by: Julian Zhu <jz531210@gmail.com>
---
 sysdeps/riscv/rvd/s_fmaximum.c  | 36 +++++++++++++++++++++++++++++++++
 sysdeps/riscv/rvd/s_fminimum.c  | 36 +++++++++++++++++++++++++++++++++
 sysdeps/riscv/rvf/s_fmaximumf.c | 36 +++++++++++++++++++++++++++++++++
 sysdeps/riscv/rvf/s_fminimumf.c | 36 +++++++++++++++++++++++++++++++++
 4 files changed, 144 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
  

Patch

diff --git a/sysdeps/riscv/rvd/s_fmaximum.c b/sysdeps/riscv/rvd/s_fmaximum.c
new file mode 100644
index 0000000000..ef9f622b55
--- /dev/null
+++ b/sysdeps/riscv/rvd/s_fmaximum.c
@@ -0,0 +1,36 @@ 
+/* Return maximum of X and Y. RISC-V version.
+   Copyright (C) 2024 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 <fenv_private.h>
+#include <libm-alias-double.h>
+
+double
+__fmaximum (double x, double y)
+{
+  double res;
+
+  if (__glibc_unlikely((_FCLASS (x) | _FCLASS (y)) & _FCLASS_NAN))
+      return x * y;
+  else
+    {
+      asm volatile ("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..c46383af1e
--- /dev/null
+++ b/sysdeps/riscv/rvd/s_fminimum.c
@@ -0,0 +1,36 @@ 
+/* Return minimum of X and Y. RISC-V version.
+   Copyright (C) 2024 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 <fenv_private.h>
+#include <libm-alias-double.h>
+
+double
+__fminimum (double x, double y)
+{
+  double res;
+
+  if (__glibc_unlikely((_FCLASS (x) | _FCLASS (y)) & _FCLASS_NAN))
+      return x * y;
+  else
+    {
+      asm volatile ("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..61a2bd0be8
--- /dev/null
+++ b/sysdeps/riscv/rvf/s_fmaximumf.c
@@ -0,0 +1,36 @@ 
+/* Return maximum of X and Y. RISC-V version.
+   Copyright (C) 2024 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 <fenv_private.h>
+#include <libm-alias-float.h>
+
+float
+__fmaximumf (float x, float y)
+{
+  float res;
+
+  if (__glibc_unlikely((_FCLASS (x) | _FCLASS (y)) & _FCLASS_NAN))
+      return x * y;
+  else
+    {
+      asm volatile ("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..9c657ad9ac
--- /dev/null
+++ b/sysdeps/riscv/rvf/s_fminimumf.c
@@ -0,0 +1,36 @@ 
+/* Return minimum of X and Y. RISC-V version.
+   Copyright (C) 2024 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 <fenv_private.h>
+#include <libm-alias-float.h>
+
+float
+__fminimumf (float x, float y)
+{
+  float res;
+
+  if (__glibc_unlikely((_FCLASS (x) | _FCLASS (y)) & _FCLASS_NAN))
+      return x * y;
+  else
+    {
+      asm volatile ("fmin.s %0, %1, %2" : "=f" (res) : "f" (x), "f" (y));
+      return res;
+    }
+}
+libm_alias_float (__fminimum, fminimum)