[PATCH 4/4] LoongArch: Optimize f{max,min}imum_mag_num{,f}

Xi Ruoyao xry111@xry111.site
Mon Feb 24 09:50:55 GMT 2025


Like the previous commit, but use f{max,min}a.{s,d} instructions.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 sysdeps/loongarch/fpu/s_fmaximum_mag_num.c  | 23 ++++-----------------
 sysdeps/loongarch/fpu/s_fmaximum_mag_numf.c | 23 ++++-----------------
 sysdeps/loongarch/fpu/s_fminimum_mag_num.c  | 23 ++++-----------------
 sysdeps/loongarch/fpu/s_fminimum_mag_numf.c | 23 ++++-----------------
 4 files changed, 16 insertions(+), 76 deletions(-)

diff --git a/sysdeps/loongarch/fpu/s_fmaximum_mag_num.c b/sysdeps/loongarch/fpu/s_fmaximum_mag_num.c
index d33218a3c6..b81b36ad9d 100644
--- a/sysdeps/loongarch/fpu/s_fmaximum_mag_num.c
+++ b/sysdeps/loongarch/fpu/s_fmaximum_mag_num.c
@@ -24,25 +24,10 @@
 double
 __fmaximum_mag_num (double x, double y)
 {
-  int x_cond;
-  int y_cond;
-  asm volatile ("fclass.d \t%0, %1" : "=f" (x_cond) : "f" (x));
-  asm volatile ("fclass.d \t%0, %1" : "=f" (y_cond) : "f" (y));
+  double a = __builtin_isnan (y) ? x : y;
+  double b = __builtin_isnan (x) ? y : x;
 
-  if (__glibc_unlikely((x_cond & _FCLASS_NAN) && !(y_cond & _FCLASS_NAN)))
-    {
-      asm volatile ("fmaxa.d \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return y;
-    }
-  else if (__glibc_unlikely(!(x_cond & _FCLASS_NAN) && (y_cond & _FCLASS_NAN)))
-    {
-      asm volatile ("fmaxa.d \t%0, %1, %2" : "=f" (y) : "f" (x), "f" (y));
-      return x;
-    }
-  else
-    {
-      asm volatile ("fmaxa.d \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return x;
-    }
+  asm ("fmaxa.d \t%0, %1, %2" : "=f" (a) : "f" (a), "f" (b));
+  return a;
 }
 libm_alias_double (__fmaximum_mag_num, fmaximum_mag_num)
diff --git a/sysdeps/loongarch/fpu/s_fmaximum_mag_numf.c b/sysdeps/loongarch/fpu/s_fmaximum_mag_numf.c
index 62a35c5488..e9b4eb0af6 100644
--- a/sysdeps/loongarch/fpu/s_fmaximum_mag_numf.c
+++ b/sysdeps/loongarch/fpu/s_fmaximum_mag_numf.c
@@ -24,25 +24,10 @@
 float
 __fmaximum_mag_numf (float x, float y)
 {
-  int x_cond;
-  int y_cond;
-  asm volatile ("fclass.s \t%0, %1" : "=f" (x_cond) : "f" (x));
-  asm volatile ("fclass.s \t%0, %1" : "=f" (y_cond) : "f" (y));
+  float a = __builtin_isnanf (y) ? x : y;
+  float b = __builtin_isnanf (x) ? y : x;
 
-  if (__glibc_unlikely((x_cond & _FCLASS_NAN) && !(y_cond & _FCLASS_NAN)))
-    {
-      asm volatile ("fmaxa.s \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return y;
-    }
-  else if (__glibc_unlikely(!(x_cond & _FCLASS_NAN) && (y_cond & _FCLASS_NAN)))
-    {
-      asm volatile ("fmaxa.s \t%0, %1, %2" : "=f" (y) : "f" (x), "f" (y));
-      return x;
-    }
-  else
-    {
-      asm volatile ("fmaxa.s \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return x;
-    }
+  asm ("fmaxa.s \t%0, %1, %2" : "=f" (a) : "f" (a), "f" (b));
+  return a;
 }
 libm_alias_float (__fmaximum_mag_num, fmaximum_mag_num)
diff --git a/sysdeps/loongarch/fpu/s_fminimum_mag_num.c b/sysdeps/loongarch/fpu/s_fminimum_mag_num.c
index a892544804..44b0efe312 100644
--- a/sysdeps/loongarch/fpu/s_fminimum_mag_num.c
+++ b/sysdeps/loongarch/fpu/s_fminimum_mag_num.c
@@ -24,25 +24,10 @@
 double
 __fminimum_mag_num (double x, double y)
 {
-  int x_cond;
-  int y_cond;
-  asm volatile ("fclass.d \t%0, %1" : "=f" (x_cond) : "f" (x));
-  asm volatile ("fclass.d \t%0, %1" : "=f" (y_cond) : "f" (y));
+  double a = __builtin_isnan (y) ? x : y;
+  double b = __builtin_isnan (x) ? y : x;
 
-  if (__glibc_unlikely((x_cond & _FCLASS_NAN) && !(y_cond & _FCLASS_NAN)))
-    {
-      asm volatile ("fmina.d \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return y;
-    }
-  else if (__glibc_unlikely(!(x_cond & _FCLASS_NAN) && (y_cond & _FCLASS_NAN)))
-    {
-      asm volatile ("fmina.d \t%0, %1, %2" : "=f" (y) : "f" (x), "f" (y));
-      return x;
-    }
-  else
-    {
-      asm volatile ("fmina.d \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return x;
-    }
+  asm ("fmina.d \t%0, %1, %2" : "=f" (a) : "f" (a), "f" (b));
+  return a;
 }
 libm_alias_double (__fminimum_mag_num, fminimum_mag_num)
diff --git a/sysdeps/loongarch/fpu/s_fminimum_mag_numf.c b/sysdeps/loongarch/fpu/s_fminimum_mag_numf.c
index 2164816291..88d9e95bca 100644
--- a/sysdeps/loongarch/fpu/s_fminimum_mag_numf.c
+++ b/sysdeps/loongarch/fpu/s_fminimum_mag_numf.c
@@ -24,25 +24,10 @@
 float
 __fminimum_mag_numf (float x, float y)
 {
-  int x_cond;
-  int y_cond;
-  asm volatile ("fclass.s \t%0, %1" : "=f" (x_cond) : "f" (x));
-  asm volatile ("fclass.s \t%0, %1" : "=f" (y_cond) : "f" (y));
+  float a = __builtin_isnanf (y) ? x : y;
+  float b = __builtin_isnanf (x) ? y : x;
 
-  if (__glibc_unlikely((x_cond & _FCLASS_NAN) && !(y_cond & _FCLASS_NAN)))
-    {
-      asm volatile ("fmina.s \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return y;
-    }
-  else if (__glibc_unlikely(!(x_cond & _FCLASS_NAN) && (y_cond & _FCLASS_NAN)))
-    {
-      asm volatile ("fmina.s \t%0, %1, %2" : "=f" (y) : "f" (x), "f" (y));
-      return x;
-    }
-  else
-    {
-      asm volatile ("fmina.s \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return x;
-    }
+  asm ("fmina.s \t%0, %1, %2" : "=f" (a) : "f" (a), "f" (b));
+  return a;
 }
 libm_alias_float (__fminimum_mag_num, fminimum_mag_num)
-- 
2.48.1



More information about the Libc-alpha mailing list