diff mbox series

[5/6] MIPS/math: Implement optimized fmax(mag)(f)/fmin(mag)(f)

Message ID 20240513081429.1749898-6-syq@gcc.gnu.org
State New
Headers show
Series MIPS: Improve math | expand

Commit Message

YunQiang Su May 13, 2024, 8:14 a.m. UTC
MIPSr6 instroduces min/mina/max/maxa instructions, which
can be use for fmax(mag)(f)/fmin(mag) directly.

For other cases, we continue to use the generic implemention,
with our defined __mips_issignaling inline functions.

Since abs.fmt instructions will signal for NAN, so we need to
be sure that the operands is orderable first.

	* sysdeps/mips/ieee754/s_fmax.c
	* sysdeps/mips/ieee754/s_fmaxf.c
	* sysdeps/mips/ieee754/s_fmin.c
	* sysdeps/mips/ieee754/s_fminf.c
	* sysdeps/mips/ieee754/s_fmaxmag.c
	* sysdeps/mips/ieee754/s_fmaxmagf.c
	* sysdeps/mips/ieee754/s_fminmag.c
	* sysdeps/mips/ieee754/s_fminmagf.c
---
 sysdeps/mips/ieee754/s_fmax.c     | 45 ++++++++++++++++++++++
 sysdeps/mips/ieee754/s_fmaxf.c    | 43 +++++++++++++++++++++
 sysdeps/mips/ieee754/s_fmaxmag.c  | 62 +++++++++++++++++++++++++++++++
 sysdeps/mips/ieee754/s_fmaxmagf.c | 61 ++++++++++++++++++++++++++++++
 sysdeps/mips/ieee754/s_fmin.c     | 44 ++++++++++++++++++++++
 sysdeps/mips/ieee754/s_fminf.c    | 43 +++++++++++++++++++++
 sysdeps/mips/ieee754/s_fminmag.c  | 62 +++++++++++++++++++++++++++++++
 sysdeps/mips/ieee754/s_fminmagf.c | 61 ++++++++++++++++++++++++++++++
 8 files changed, 421 insertions(+)
 create mode 100644 sysdeps/mips/ieee754/s_fmax.c
 create mode 100644 sysdeps/mips/ieee754/s_fmaxf.c
 create mode 100644 sysdeps/mips/ieee754/s_fmaxmag.c
 create mode 100644 sysdeps/mips/ieee754/s_fmaxmagf.c
 create mode 100644 sysdeps/mips/ieee754/s_fmin.c
 create mode 100644 sysdeps/mips/ieee754/s_fminf.c
 create mode 100644 sysdeps/mips/ieee754/s_fminmag.c
 create mode 100644 sysdeps/mips/ieee754/s_fminmagf.c
diff mbox series

Patch

diff --git a/sysdeps/mips/ieee754/s_fmax.c b/sysdeps/mips/ieee754/s_fmax.c
new file mode 100644
index 0000000000..190ca4a885
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmax.c
@@ -0,0 +1,45 @@ 
+/* fmax().  MIPS 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 <libm-alias-double.h>
+#include <math_private.h>
+
+double
+__fmax (double x, double y)
+{
+
+#if __mips_isa_rev >= 6 && defined(__mips_hard_float)                         \
+    && !defined(__mips_single_float)
+  double ret;
+  asm volatile("max.d	%0, %1, %2" : "=f"(ret) : "f"(x), "f"(y));
+  return ret;
+#else
+  if (isgreaterequal (x, y))
+    return x;
+  else if (isless (x, y))
+    return y;
+
+  if (__mips_issignaling (x) || __mips_issignaling (y))
+    return x + y;
+  else
+    return isnan (y) ? x : y;
+#endif
+}
+
+libm_alias_double (__fmax, fmax)
diff --git a/sysdeps/mips/ieee754/s_fmaxf.c b/sysdeps/mips/ieee754/s_fmaxf.c
new file mode 100644
index 0000000000..358ddefc17
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmaxf.c
@@ -0,0 +1,43 @@ 
+/* fmaxf().  MIPS 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 <libm-alias-float.h>
+#include <math_private.h>
+
+float
+__fmaxf (float x, float y)
+{
+#if __mips_isa_rev >= 6 && defined(__mips_hard_float)
+  float ret;
+  asm volatile("max.s	%0, %1, %2" : "=f"(ret) : "f"(x), "f"(y));
+  return ret;
+#else
+  if (isgreaterequal (x, y))
+    return x;
+  else if (isless (x, y))
+    return y;
+#endif
+
+  if (__mips_issignalingf (x) || __mips_issignalingf (y))
+    return x + y;
+  else
+    return isnan (y) ? x : y;
+}
+
+libm_alias_float (__fmax, fmax)
diff --git a/sysdeps/mips/ieee754/s_fmaxmag.c b/sysdeps/mips/ieee754/s_fmaxmag.c
new file mode 100644
index 0000000000..3f1b32afe9
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmaxmag.c
@@ -0,0 +1,62 @@ 
+/* fmaxmag().  MIPS 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 <libm-alias-double.h>
+#include <math_private.h>
+#include <math-type-macros-double.h>
+
+double
+__fmaxmag (double x, double y)
+{
+#if __mips_isa_rev >= 6 && defined(__mips_hard_float)                         \
+    && !defined(__mips_single_float)
+  double ret;
+  asm volatile("maxa.d	%0, %1, %2" : "=f"(ret) : "f"(x), "f"(y));
+  return ret;
+#else
+  /* ABS.d signals both sNAN and qNAN on pre-R5.  */
+  if (!isunordered (x, y))
+    {
+      double ax;
+      double ay;
+#  if defined(__mips_hard_float) && !defined(__mips_single_float)
+      asm volatile("abs.d	%0, %1" : "=f"(ax) : "f"(x));
+      asm volatile("abs.d	%0, %1" : "=f"(ay) : "f"(y));
+#  else
+      ax = M_FABS (x);
+      ay = M_FABS (y);
+#  endif
+      if (isgreater (ax, ay))
+	return x;
+      else if (isless (ax, ay))
+	return y;
+
+      int32_t xi;
+      GET_HIGH_WORD (xi, x);
+      return (xi < 0 ? y : x);
+    }
+#endif /* __mips_isa_rev >= 6 */
+
+  if (__mips_issignaling (x) || __mips_issignaling (y))
+    return x + y;
+  else
+    return isnan (y) ? x : y;
+}
+
+libm_alias_double (__fmaxmag, fmaxmag)
diff --git a/sysdeps/mips/ieee754/s_fmaxmagf.c b/sysdeps/mips/ieee754/s_fmaxmagf.c
new file mode 100644
index 0000000000..cfa44c773d
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmaxmagf.c
@@ -0,0 +1,61 @@ 
+/* fmaxmagf().  MIPS 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 <libm-alias-float.h>
+#include <math_private.h>
+#include <math-type-macros-float.h>
+
+float
+__fmaxmagf (float x, float y)
+{
+#if __mips_isa_rev >= 6 && defined(__mips_hard_float)
+  float ret;
+  asm volatile("maxa.s	%0, %1, %2" : "=f"(ret) : "f"(x), "f"(y));
+  return ret;
+#else
+  /* ABS.s signals both sNAN and qNAN on pre-R5.  */
+  if (!isunordered (x, y))
+    {
+      float ax;
+      float ay;
+#  if defined(__mips_hard_float)
+      asm volatile("abs.s	%0, %1" : "=f"(ax) : "f"(x));
+      asm volatile("abs.s	%0, %1" : "=f"(ay) : "f"(y));
+#  else
+      ax = M_FABS (x);
+      ay = M_FABS (y);
+#  endif
+      if (isgreater (ax, ay))
+	return x;
+      else if (isless (ax, ay))
+	return y;
+
+      int32_t xi;
+      GET_FLOAT_WORD (xi, x);
+      return (xi < 0 ? y : x);
+    }
+#endif /* __mips_isa_rev >= 6 */
+
+  if (__mips_issignalingf (x) || __mips_issignalingf (y))
+    return x + y;
+  else
+    return isnan (y) ? x : y;
+}
+
+libm_alias_float (__fmaxmag, fmaxmag)
diff --git a/sysdeps/mips/ieee754/s_fmin.c b/sysdeps/mips/ieee754/s_fmin.c
new file mode 100644
index 0000000000..56ff7100c4
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmin.c
@@ -0,0 +1,44 @@ 
+/* fmin().  MIPS 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 <libm-alias-double.h>
+#include <math_private.h>
+
+double
+__fmin (double x, double y)
+{
+#if __mips_isa_rev >= 6 && defined(__mips_hard_float)                         \
+    && !defined(__mips_single_float)
+  double ret;
+  asm volatile("min.d	%0, %1, %2" : "=f"(ret) : "f"(x), "f"(y));
+  return ret;
+#else
+  if (isgreaterequal (x, y))
+    return y;
+  else if (isless (x, y))
+    return x;
+#endif
+
+  if (__mips_issignaling (x) || __mips_issignaling (y))
+    return x + y;
+  else
+    return isnan (y) ? x : y;
+}
+
+libm_alias_double (__fmin, fmin)
diff --git a/sysdeps/mips/ieee754/s_fminf.c b/sysdeps/mips/ieee754/s_fminf.c
new file mode 100644
index 0000000000..55c56183c1
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fminf.c
@@ -0,0 +1,43 @@ 
+/* fminf().  MIPS 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 <libm-alias-float.h>
+#include <math_private.h>
+
+float
+__fminf (float x, float y)
+{
+#if __mips_isa_rev >= 6 && defined(__mips_hard_float)
+  float ret;
+  asm volatile("min.s	%0, %1, %2" : "=f"(ret) : "f"(x), "f"(y));
+  return ret;
+#else
+  if (isgreaterequal (x, y))
+    return y;
+  else if (isless (x, y))
+    return x;
+#endif
+
+  if (__mips_issignalingf (x) || __mips_issignalingf (y))
+    return x + y;
+  else
+    return isnan (y) ? x : y;
+}
+
+libm_alias_float (__fmin, fmin)
diff --git a/sysdeps/mips/ieee754/s_fminmag.c b/sysdeps/mips/ieee754/s_fminmag.c
new file mode 100644
index 0000000000..bd115675f4
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fminmag.c
@@ -0,0 +1,62 @@ 
+/* fminmag().  MIPS 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-type-macros-double.h>
+#include <math.h>
+#include <libm-alias-double.h>
+#include <math_private.h>
+
+double
+__fminmag (double x, double y)
+{
+#if __mips_isa_rev >= 6 && defined(__mips_hard_float)                         \
+    && !defined(__mips_single_float)
+  double ret;
+  asm volatile("mina.d	%0, %1, %2" : "=f"(ret) : "f"(x), "f"(y));
+  return ret;
+#else
+  /* ABS.d signals both sNAN and qNAN on pre-R5.  */
+  if (!isunordered (x, y))
+    {
+      double ax;
+      double ay;
+#  if defined(__mips_hard_float) && !defined(__mips_single_float)
+      asm volatile("abs.d	%0, %1" : "=f"(ax) : "f"(x));
+      asm volatile("abs.d	%0, %1" : "=f"(ay) : "f"(y));
+#  else
+      ax = M_FABS (x);
+      ay = M_FABS (y);
+#  endif
+      if (isgreater (ax, ay))
+	return y;
+      else if (isless (ax, ay))
+	return x;
+
+      int32_t xi;
+      GET_HIGH_WORD (xi, x);
+      return (xi < 0 ? x : y);
+    }
+#endif /* __mips_isa_rev >= 6 */
+
+  if (__mips_issignaling (x) || __mips_issignaling (y))
+    return x + y;
+  else
+    return isnan (y) ? x : y;
+}
+
+libm_alias_double (__fminmag, fminmag)
diff --git a/sysdeps/mips/ieee754/s_fminmagf.c b/sysdeps/mips/ieee754/s_fminmagf.c
new file mode 100644
index 0000000000..8997ef05f7
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fminmagf.c
@@ -0,0 +1,61 @@ 
+/* fminmagf().  MIPS 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 <libm-alias-float.h>
+#include <math_private.h>
+#include <math-type-macros-float.h>
+
+float
+__fminmagf (float x, float y)
+{
+#if __mips_isa_rev >= 6 && defined(__mips_hard_float)
+  float ret;
+  asm volatile("mina.s	%0, %1, %2" : "=f"(ret) : "f"(x), "f"(y));
+  return ret;
+#else
+  /* ABS.s signals both sNAN and qNAN on pre-R5.  */
+  if (!isunordered (x, y))
+    {
+      float ax;
+      float ay;
+#  if defined(__mips_hard_float)
+      asm volatile("abs.s	%0, %1" : "=f"(ax) : "f"(x));
+      asm volatile("abs.s	%0, %1" : "=f"(ay) : "f"(y));
+#  else
+      ax = M_FABS (x);
+      ay = M_FABS (y);
+#  endif
+      if (isgreater (ax, ay))
+	return y;
+      else if (isless (ax, ay))
+	return x;
+
+      int32_t xi;
+      GET_FLOAT_WORD (xi, x);
+      return (xi < 0 ? x : y);
+    }
+#endif
+
+  if (__mips_issignalingf (x) || __mips_issignalingf (y))
+    return x + y;
+  else
+    return isnan (y) ? x : y;
+}
+
+libm_alias_float (__fminmag, fminmag)