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

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


Like the previous commits, but reverse the selection logic for masking
the NaN input out if another is a number.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 sysdeps/loongarch/fpu/s_fmaximum_num.c  | 23 ++++-------------------
 sysdeps/loongarch/fpu/s_fmaximum_numf.c | 23 ++++-------------------
 sysdeps/loongarch/fpu/s_fminimum_num.c  | 23 ++++-------------------
 sysdeps/loongarch/fpu/s_fminimum_numf.c | 23 ++++-------------------
 4 files changed, 16 insertions(+), 76 deletions(-)

diff --git a/sysdeps/loongarch/fpu/s_fmaximum_num.c b/sysdeps/loongarch/fpu/s_fmaximum_num.c
index a7d47f5950..74a74de9ba 100644
--- a/sysdeps/loongarch/fpu/s_fmaximum_num.c
+++ b/sysdeps/loongarch/fpu/s_fmaximum_num.c
@@ -24,25 +24,10 @@
 double
 __fmaximum_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 ("fmax.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 ("fmax.d \t%0, %1, %2" : "=f" (y) : "f" (x), "f" (y));
-      return x;
-    }
-  else
-    {
-      asm volatile ("fmax.d \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return x;
-    }
+  asm ("fmax.d \t%0, %1, %2" : "=f" (a) : "f" (a), "f" (b));
+  return a;
 }
 libm_alias_double (__fmaximum_num, fmaximum_num)
diff --git a/sysdeps/loongarch/fpu/s_fmaximum_numf.c b/sysdeps/loongarch/fpu/s_fmaximum_numf.c
index f107391ae6..22827665b2 100644
--- a/sysdeps/loongarch/fpu/s_fmaximum_numf.c
+++ b/sysdeps/loongarch/fpu/s_fmaximum_numf.c
@@ -25,25 +25,10 @@
 float
 __fmaximum_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 ("fmax.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 ("fmax.s \t%0, %1, %2" : "=f" (y) : "f" (x), "f" (y));
-      return x;
-    }
-  else
-    {
-      asm volatile ("fmax.s \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return x;
-    }
+  asm ("fmax.s \t%0, %1, %2" : "=f" (a) : "f" (a), "f" (b));
+  return a;
 }
 libm_alias_float (__fmaximum_num, fmaximum_num)
diff --git a/sysdeps/loongarch/fpu/s_fminimum_num.c b/sysdeps/loongarch/fpu/s_fminimum_num.c
index 769efc2ff4..4e154500c9 100644
--- a/sysdeps/loongarch/fpu/s_fminimum_num.c
+++ b/sysdeps/loongarch/fpu/s_fminimum_num.c
@@ -24,25 +24,10 @@
 double
 __fminimum_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 ("fmin.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 ("fmin.d \t%0, %1, %2" : "=f" (y) : "f" (x), "f" (y));
-      return x;
-    }
-  else
-    {
-      asm volatile ("fmin.d \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return x;
-    }
+  asm ("fmin.d \t%0, %1, %2" : "=f" (a) : "f" (a), "f" (b));
+  return a;
 }
 libm_alias_double (__fminimum_num, fminimum_num)
diff --git a/sysdeps/loongarch/fpu/s_fminimum_numf.c b/sysdeps/loongarch/fpu/s_fminimum_numf.c
index 6900aeaf2f..a6229f2c69 100644
--- a/sysdeps/loongarch/fpu/s_fminimum_numf.c
+++ b/sysdeps/loongarch/fpu/s_fminimum_numf.c
@@ -24,25 +24,10 @@
 float
 __fminimum_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 ("fmin.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 ("fmin.s \t%0, %1, %2" : "=f" (y) : "f" (x), "f" (y));
-      return x;
-    }
-  else
-    {
-      asm volatile ("fmin.s \t%0, %1, %2" : "=f" (x) : "f" (x), "f" (y));
-      return x;
-    }
+  asm ("fmin.s \t%0, %1, %2" : "=f" (a) : "f" (a), "f" (b));
+  return a;
 }
 libm_alias_float (__fminimum_num, fminimum_num)
-- 
2.48.1



More information about the Libc-alpha mailing list