public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: YunQiang Su <syq@gcc.gnu.org>
To: libc-alpha@sourceware.org
Subject: [PATCH 6/6] MIPS/math: Implement optimized f(max,min)imum(_mag)_num(f)
Date: Mon, 13 May 2024 16:14:29 +0800	[thread overview]
Message-ID: <20240513081429.1749898-7-syq@gcc.gnu.org> (raw)
In-Reply-To: <20240513081429.1749898-1-syq@gcc.gnu.org>

MIPSr6 instroduces min.s/min.d/max.s/max.d instructions, which have
slight different with fmaximum_num, when one operand is sNaN.
In this case, these instructions will return qNaN, while fmaximum_num
requires another operand.

For pre-r6 with hardfloat, we determine whether NAN is present, so that
we can use abs.fmt, which can boost performance.  We also use
GET_HIGH_WORD/GET_FLOAT_WORD instead of copysign for the equal cases.

	* sysdeps/mips/ieee754/s_fmaximum_num.c
	* sysdeps/mips/ieee754/s_fmaximum_numf.c
	* sysdeps/mips/ieee754/s_fminimum_num.c
	* sysdeps/mips/ieee754/s_fminimum_numf.c
	* sysdeps/mips/ieee754/s_fmaximum_mag_num.c
	* sysdeps/mips/ieee754/s_fmaximum_mag_numf.c
	* sysdeps/mips/ieee754/s_fminimum_mag_num.c
	* sysdeps/mips/ieee754/s_fminimum_mag_numf.c

Signed-off-by: YunQiang Su <syq@gcc.gnu.org>
---
 sysdeps/mips/ieee754/s_fmaximum_mag_num.c  | 65 ++++++++++++++++++++++
 sysdeps/mips/ieee754/s_fmaximum_mag_numf.c | 64 +++++++++++++++++++++
 sysdeps/mips/ieee754/s_fmaximum_num.c      | 54 ++++++++++++++++++
 sysdeps/mips/ieee754/s_fmaximum_numf.c     | 53 ++++++++++++++++++
 sysdeps/mips/ieee754/s_fminimum_mag_num.c  | 65 ++++++++++++++++++++++
 sysdeps/mips/ieee754/s_fminimum_mag_numf.c | 64 +++++++++++++++++++++
 sysdeps/mips/ieee754/s_fminimum_num.c      | 54 ++++++++++++++++++
 sysdeps/mips/ieee754/s_fminimum_numf.c     | 53 ++++++++++++++++++
 8 files changed, 472 insertions(+)
 create mode 100644 sysdeps/mips/ieee754/s_fmaximum_mag_num.c
 create mode 100644 sysdeps/mips/ieee754/s_fmaximum_mag_numf.c
 create mode 100644 sysdeps/mips/ieee754/s_fmaximum_num.c
 create mode 100644 sysdeps/mips/ieee754/s_fmaximum_numf.c
 create mode 100644 sysdeps/mips/ieee754/s_fminimum_mag_num.c
 create mode 100644 sysdeps/mips/ieee754/s_fminimum_mag_numf.c
 create mode 100644 sysdeps/mips/ieee754/s_fminimum_num.c
 create mode 100644 sysdeps/mips/ieee754/s_fminimum_numf.c

diff --git a/sysdeps/mips/ieee754/s_fmaximum_mag_num.c b/sysdeps/mips/ieee754/s_fmaximum_mag_num.c
new file mode 100644
index 0000000000..83e9a28bed
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmaximum_mag_num.c
@@ -0,0 +1,65 @@
+/* fmaximum_mag_num().  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
+__fmaximum_mag_num (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));
+  if (isnan (ret))
+    {
+      if (!__mips_issignaling (x))
+	ret = x;
+      else if (!__mips_issignaling (y))
+	ret = y;
+    }
+  return ret;
+#else
+  double ax;
+  double ay;
+  /* ABS.d signals both sNAN and qNAN on pre-R5.  */
+  if (isunordered (x, y))
+    return isnan (y) ? (isnan (x) ? x + y : x) : y;
+#  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;
+  else
+    {
+      int32_t xi;
+      GET_HIGH_WORD (xi, x);
+      return (xi < 0 ? y : x);
+    }
+#endif
+}
+
+libm_alias_double (__fmaximum_mag_num, fmaximum_mag_num)
diff --git a/sysdeps/mips/ieee754/s_fmaximum_mag_numf.c b/sysdeps/mips/ieee754/s_fmaximum_mag_numf.c
new file mode 100644
index 0000000000..c0e6589c00
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmaximum_mag_numf.c
@@ -0,0 +1,64 @@
+/* fmaximum_mag_numf().  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
+__fmaximum_mag_numf (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));
+  if (isnan (ret))
+    {
+      if (!__mips_issignalingf (x))
+	ret = x;
+      else if (!__mips_issignalingf (y))
+	ret = y;
+    }
+  return ret;
+#else
+  float ax;
+  float ay;
+  /* ABS.s signals both sNAN and qNAN on pre-R5.  */
+  if (isunordered (x, y))
+    return isnan (y) ? (isnan (x) ? x + y : x) : y;
+#  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;
+  else
+    {
+      int32_t xi;
+      GET_FLOAT_WORD (xi, x);
+      return (xi < 0 ? y : x);
+    }
+#endif
+}
+
+libm_alias_float (__fmaximum_mag_num, fmaximum_mag_num)
diff --git a/sysdeps/mips/ieee754/s_fmaximum_num.c b/sysdeps/mips/ieee754/s_fmaximum_num.c
new file mode 100644
index 0000000000..85816a12be
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmaximum_num.c
@@ -0,0 +1,54 @@
+/* fmaximum_num().  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
+__fmaximum_num (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));
+  if (isnan (ret))
+    {
+      if (!__mips_issignaling (x))
+	ret = x;
+      else if (!__mips_issignaling (y))
+	ret = y;
+    }
+  return ret;
+#else
+  if (isgreater (x, y))
+    return x;
+  else if (isless (x, y))
+    return y;
+  else if (x == y)
+    {
+      int32_t xi;
+      GET_HIGH_WORD (xi, x);
+      return (xi < 0 ? y : x);
+    }
+  else
+    return isnan (y) ? (isnan (x) ? x + y : x) : y;
+#endif
+}
+
+libm_alias_double (__fmaximum_num, fmaximum_num)
diff --git a/sysdeps/mips/ieee754/s_fmaximum_numf.c b/sysdeps/mips/ieee754/s_fmaximum_numf.c
new file mode 100644
index 0000000000..1047f354be
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fmaximum_numf.c
@@ -0,0 +1,53 @@
+/* fmaximum_numf().  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
+__fmaximum_numf (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));
+  if (isnan (ret))
+    {
+      if (!__mips_issignalingf (x))
+	ret = x;
+      if (!__mips_issignalingf (y))
+	ret = y;
+    }
+  return ret;
+#else
+  if (isgreater (x, y))
+    return x;
+  else if (isless (x, y))
+    return y;
+  else if (x == y)
+    {
+      int32_t xi;
+      GET_FLOAT_WORD (xi, x);
+      return (xi < 0 ? y : x);
+    }
+  else
+    return isnan (y) ? (isnan (x) ? x + y : x) : y;
+#endif
+}
+
+libm_alias_float (__fmaximum_num, fmaximum_num)
diff --git a/sysdeps/mips/ieee754/s_fminimum_mag_num.c b/sysdeps/mips/ieee754/s_fminimum_mag_num.c
new file mode 100644
index 0000000000..a6df931aaf
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fminimum_mag_num.c
@@ -0,0 +1,65 @@
+/* fminimum_mag_num().  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
+__fminimum_mag_num (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));
+  if (isnan (ret))
+    {
+      if (!__mips_issignaling (x))
+	ret = x;
+      else if (!__mips_issignaling (y))
+	ret = y;
+    }
+  return ret;
+#else
+  double ax;
+  double ay;
+  /* ABS.d signals both sNAN and qNAN on pre-R5.  */
+  if (isunordered (x, y))
+    return isnan (y) ? (isnan (x) ? x + y : x) : y;
+#  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;
+  else
+    {
+      int32_t xi;
+      GET_HIGH_WORD (xi, x);
+      return (xi < 0 ? x : y);
+    }
+#endif
+}
+
+libm_alias_double (__fminimum_mag_num, fminimum_mag_num)
diff --git a/sysdeps/mips/ieee754/s_fminimum_mag_numf.c b/sysdeps/mips/ieee754/s_fminimum_mag_numf.c
new file mode 100644
index 0000000000..74d189b380
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fminimum_mag_numf.c
@@ -0,0 +1,64 @@
+/* fminimum_mag_numf().  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
+__fminimum_mag_numf (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));
+  if (isnan (ret))
+    {
+      if (!__mips_issignalingf (x))
+	ret = x;
+      else if (!__mips_issignalingf (y))
+	ret = y;
+    }
+  return ret;
+#else
+  float ax;
+  float ay;
+  /* ABS.s signals both sNAN and qNAN on pre-R5.  */
+  if (isunordered (x, y))
+    return isnan (y) ? (isnan (x) ? x + y : x) : y;
+#  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;
+  else
+    {
+      int32_t xi;
+      GET_FLOAT_WORD (xi, x);
+      return (xi < 0 ? x : y);
+    }
+#endif
+}
+
+libm_alias_float (__fminimum_mag_num, fminimum_mag_num)
diff --git a/sysdeps/mips/ieee754/s_fminimum_num.c b/sysdeps/mips/ieee754/s_fminimum_num.c
new file mode 100644
index 0000000000..62fd139d63
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fminimum_num.c
@@ -0,0 +1,54 @@
+/* fminimum_num().  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
+__fminimum_num (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));
+  if (isnan (ret))
+    {
+      if (!__mips_issignaling (x))
+	ret = x;
+      if (!__mips_issignaling (y))
+	ret = y;
+    }
+  return ret;
+#else
+  if (isgreater (x, y))
+    return y;
+  else if (isless (x, y))
+    return x;
+  else if (x == y)
+    {
+      int32_t xi;
+      GET_HIGH_WORD (xi, x);
+      return (xi < 0 ? x : y);
+    }
+  else
+    return isnan (y) ? (isnan (x) ? x + y : x) : y;
+#endif
+}
+
+libm_alias_double (__fminimum_num, fminimum_num)
diff --git a/sysdeps/mips/ieee754/s_fminimum_numf.c b/sysdeps/mips/ieee754/s_fminimum_numf.c
new file mode 100644
index 0000000000..37d66ff6fa
--- /dev/null
+++ b/sysdeps/mips/ieee754/s_fminimum_numf.c
@@ -0,0 +1,53 @@
+/* fminimum_numf().  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
+__fminimum_numf (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));
+  if (isnan (ret))
+    {
+      if (!__mips_issignalingf (x))
+	ret = x;
+      if (!__mips_issignalingf (y))
+	ret = y;
+    }
+  return ret;
+#else
+  if (isgreater (x, y))
+    return y;
+  else if (isless (x, y))
+    return x;
+  else if (x == y)
+    {
+      int32_t xi;
+      GET_FLOAT_WORD (xi, x);
+      return (xi < 0 ? x : y);
+    }
+  else
+    return isnan (y) ? (isnan (x) ? x + y : x) : y;
+#endif
+}
+
+libm_alias_float (__fminimum_num, fminimum_num)
-- 
2.39.2


  parent reply	other threads:[~2024-05-13  8:14 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-13  8:14 [PATCH 0/6] MIPS: Improve math YunQiang Su
2024-05-13  8:14 ` [PATCH 1/6] MIPSr6/math: Use builtin fma and fmaf YunQiang Su
2024-05-22 13:42   ` Adhemerval Zanella Netto
2024-05-25 11:55     ` YunQiang Su
2024-05-31 15:53       ` YunQiang Su
2024-05-13  8:14 ` [PATCH 2/6] MIPS/math: Define port-specific GET_HIGH_WORD YunQiang Su
2024-05-22 18:36   ` Adhemerval Zanella Netto
2024-05-13  8:14 ` [PATCH 3/6] MIPS/math: Implement optimized issignaling(f) YunQiang Su
2024-05-23 14:37   ` Adhemerval Zanella Netto
2024-05-13  8:14 ` [PATCH 4/6] MIPS/math: Implement optimized fmaximum/fminmum(,_mag)(,f) YunQiang Su
2024-05-13  8:14 ` [PATCH 5/6] MIPS/math: Implement optimized fmax(mag)(f)/fmin(mag)(f) YunQiang Su
2024-05-13  8:14 ` YunQiang Su [this message]
2024-05-22 13:21 ` [PATCH 0/6] MIPS: Improve math YunQiang Su

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240513081429.1749898-7-syq@gcc.gnu.org \
    --to=syq@gcc.gnu.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).