]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Remove the SVID error handling from remainderf
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 31 Oct 2025 16:08:50 +0000 (13:08 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 4 Nov 2025 07:14:01 +0000 (04:14 -0300)
The optimized i386 version is faster than the generic one, and gcc
implements it through the builtin.  This optimization enables us to
migrate the implementation to a C version.  The performance on a Zen3
chip is similar to the SVID one.

The m68k provided an optimized version through __m81_u(remainderf)
(mathimpl.h), and gcc does not implement it through a builtin (different
than i386).

Performance improves a bit on x86_64 (Zen3, gcc 15.2.1):

reciprocal-throughput          input   master  NO-SVID  improvement
x86_64                    subnormals  17.5349  15.6125       10.96%
x86_64                        normal  53.8134  52.5754        2.30%
x86_64                close-exponent  20.0211  18.6656        6.77%
i686                      subnormals  21.8105  20.1856        7.45%
i686                          normal  73.1945  71.2199        2.70%
i686                  close-exponent  22.2141   20.331        8.48%

Tested on x86_64-linux-gnu and i686-linux-gnu.

Reviewed-by: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
32 files changed:
math/Versions
math/w_remainderf_compat.c
sysdeps/i386/fpu/e_remainderf.S [deleted file]
sysdeps/i386/fpu/e_remainderf.c [new file with mode: 0644]
sysdeps/ieee754/flt-32/e_remainderf.c
sysdeps/ieee754/flt-32/w_remainderf.c [new file with mode: 0644]
sysdeps/m68k/m680x0/fpu/e_remainderf.c
sysdeps/mach/hurd/i386/libm.abilist
sysdeps/unix/sysv/linux/aarch64/libm.abilist
sysdeps/unix/sysv/linux/alpha/libm.abilist
sysdeps/unix/sysv/linux/arm/be/libm.abilist
sysdeps/unix/sysv/linux/arm/le/libm.abilist
sysdeps/unix/sysv/linux/hppa/libm.abilist
sysdeps/unix/sysv/linux/i386/libm.abilist
sysdeps/unix/sysv/linux/m68k/coldfire/libm.abilist
sysdeps/unix/sysv/linux/m68k/m680x0/libm.abilist
sysdeps/unix/sysv/linux/microblaze/be/libm.abilist
sysdeps/unix/sysv/linux/microblaze/le/libm.abilist
sysdeps/unix/sysv/linux/mips/mips32/libm.abilist
sysdeps/unix/sysv/linux/mips/mips64/libm.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libm.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libm.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libm.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libm.abilist
sysdeps/unix/sysv/linux/s390/s390-32/libm.abilist
sysdeps/unix/sysv/linux/s390/s390-64/libm.abilist
sysdeps/unix/sysv/linux/sh/be/libm.abilist
sysdeps/unix/sysv/linux/sh/le/libm.abilist
sysdeps/unix/sysv/linux/sparc/sparc32/libm.abilist
sysdeps/unix/sysv/linux/sparc/sparc64/libm.abilist
sysdeps/unix/sysv/linux/x86_64/64/libm.abilist
sysdeps/unix/sysv/linux/x86_64/x32/libm.abilist

index d7466238733ab3679312e9e516c471dee88b2215..18cbce4ef202c17b718c17992eb214fd5f6d72ef 100644 (file)
@@ -697,6 +697,7 @@ libm {
     j1f;
     jnf;
     log10f;
+    remainderf;
     y0f;
     y1f;
     ynf;
index c2cdd58b3d1f81a2067820025536caf7f884f00e..eec6032df5b48f0a15574bcf043ef6d6a25d1ca3 100644 (file)
 #include <math_private.h>
 #include <math-svid-compat.h>
 #include <libm-alias-float.h>
+#include <shlib-compat.h>
 
 
-#if LIBM_SVID_COMPAT
+#if LIBM_SVID_COMPAT && SHLIB_COMPAT (libm, GLIBC_2_0, GLIBC_2_43)
 /* wrapper remainderf */
 float
-__remainderf (float x, float y)
+__remainder_compatf (float x, float y)
 {
   if (((__builtin_expect (y == 0.0f, 0) && ! isnan (x))
        || (__builtin_expect (isinf (x), 0) && ! isnan (y)))
@@ -33,6 +34,6 @@ __remainderf (float x, float y)
 
   return __ieee754_remainderf (x, y);
 }
-libm_alias_float (__remainder, remainder)
-weak_alias (__remainderf, dremf)
+compat_symbol (libm, __remainder_compatf, remainderf, GLIBC_2_0);
+weak_alias (__remainder_compatf, dremf)
 #endif
diff --git a/sysdeps/i386/fpu/e_remainderf.S b/sysdeps/i386/fpu/e_remainderf.S
deleted file mode 100644 (file)
index 475b7e2..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Public domain.
- */
-
-#include <machine/asm.h>
-#include <libm-alias-finite.h>
-
-ENTRY(__ieee754_remainderf)
-       flds    8(%esp)
-       flds    4(%esp)
-1:     fprem1
-       fstsw   %ax
-       sahf
-       jp      1b
-       fstp    %st(1)
-       ret
-END (__ieee754_remainderf)
-libm_alias_finite (__ieee754_remainderf, __remainderf)
diff --git a/sysdeps/i386/fpu/e_remainderf.c b/sysdeps/i386/fpu/e_remainderf.c
new file mode 100644 (file)
index 0000000..30ca4d3
--- /dev/null
@@ -0,0 +1,41 @@
+/* Floating-point remainder function.
+   Copyright (C) 2023-2025 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-finite.h>
+#include <libm-alias-float.h>
+#include "sysdeps/ieee754/flt-32/math_config.h"
+
+float
+__remainderf (float x, float y)
+{
+  uint32_t hx = asuint (x);
+  uint32_t hy = asuint (y);
+
+  /* fmod(+-Inf,y) or fmod(x,0) */
+  if (__glibc_unlikely ((is_inf (hx) || y == 0.0f)
+                       && !is_nan (hy)
+                       && !is_nan (hx)))
+    return __math_invalidf (x);
+
+  return __builtin_remainderf (x, y);
+}
+strong_alias (__remainderf, __ieee754_remainderf)
+versioned_symbol (libm, __remainderf, remainderf, GLIBC_2_43);
+libm_alias_float_other (__remainder, remainder)
+libm_alias_finite (__ieee754_remainderf, __remainderf)
index 6e6d687263b0bfbad9c533430f2965c3ae0e43ef..f30884cbf20caa56ae8b70f031bc019026889f6a 100644 (file)
@@ -18,6 +18,8 @@
 
 #include <math.h>
 #include <libm-alias-finite.h>
+#include <libm-alias-float.h>
+#include <math-svid-compat.h>
 #include "math_config.h"
 
 float
@@ -34,12 +36,8 @@ __ieee754_remainderf(float x, float p)
   p = fabsf (p);
   if (__glibc_likely (hp < 0x7f000000))
     {
-      /* |x| not finite, |y| equal 0 is handled by fmod.  */
-      if (__glibc_unlikely (hx >= EXPONENT_MASK))
-       return (x * p) / (x * p);
-
-      x = fabs (__ieee754_fmodf (x, p + p)); /* now x < 2p */
-      if (x + x > p)
+      x = fabs (__fmodf (x, p + p)); /* now x < 2p */
+      if (isgreater (x + x, p))
        {
          x -= p;
          if (x + x >= p)
@@ -52,9 +50,9 @@ __ieee754_remainderf(float x, float p)
     }
   else
     {
-      /* |x| not finite or |y| is NaN or 0 */
-      if ((hx >= EXPONENT_MASK || (hp - 1) >= EXPONENT_MASK))
-       return (x * p) / (x * p);
+      /* |x| not finite or |y| is NaN */
+      if (__glibc_unlikely (hx >= EXPONENT_MASK || hp > EXPONENT_MASK))
+       return __math_invalidf (x * p);
 
       x = fabsf (x);
       float p_half = 0.5f * p;
@@ -64,10 +62,17 @@ __ieee754_remainderf(float x, float p)
          if (x >= p_half)
            x -= p;
          else if (x == 0.0f)
-           x = 0.0;
+           x = 0.0f;
        }
     }
 
   return sx ? -x : x;
 }
 libm_alias_finite (__ieee754_remainderf, __remainderf)
+#if LIBM_SVID_COMPAT
+versioned_symbol (libm, __ieee754_remainderf, remainderf, GLIBC_2_43);
+libm_alias_float_other (__ieee754_remainder, remainder)
+#else
+libm_alias_float (__ieee754_remainder, remainder)
+weak_alias (__ieee754_remainderf, dremf)
+#endif
diff --git a/sysdeps/ieee754/flt-32/w_remainderf.c b/sysdeps/ieee754/flt-32/w_remainderf.c
new file mode 100644 (file)
index 0000000..db3355f
--- /dev/null
@@ -0,0 +1 @@
+/* Not needed */
index 2c1e4f67746c7d002598be952805e3a7fb66d788..b581ff318356b7911fdaf9f8c7b94dcb46eb4349 100644 (file)
 
 
 #include <math.h>
+#include <libm-alias-float.h>
 #include <libm-alias-finite.h>
 #include "mathimpl.h"
+#include "sysdeps/ieee754/flt-32/math_config.h"
 
 float
-__ieee754_remainderf (float x, float y)
+__remainderf (float x, float y)
 {
+  uint32_t hx = asuint (x);
+  uint32_t hy = asuint (y);
+
+  /* fmod(+-Inf,y) or fmod(x,0) */
+  if (__glibc_unlikely ((is_inf (hx) || y == 0.0f)
+                       && !is_nan (hy)
+                       && !is_nan (hx)))
+    return __math_invalidf (x);
+
   return __m81_u(__ieee754_remainderf)(x, y);
 }
+strong_alias (__remainderf, __ieee754_remainderf)
+versioned_symbol (libm, __remainderf, remainderf, GLIBC_2_43);
+libm_alias_float_other (__remainder, remainder)
 libm_alias_finite (__ieee754_remainderf, __remainderf)
index d365787eee69c1be025051ae3a91858edcc7a282..97a955f103cae701c2c3719714186c877dce0515 100644 (file)
@@ -1328,6 +1328,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 2feed09372974c2cab85a4edd780101a80903286..b3ef9288c842c4a7baf30b7ea6e7ef0b91657d4a 100644 (file)
@@ -1294,6 +1294,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index d5aef83b05ca1d0973c2ca79288c58ea330a22c7..e05ee8fc0915cc7b15c8943827689994e62d7fcd 100644 (file)
@@ -1453,6 +1453,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 31705bf7274936dec909225e5ab0dca7f335991a..ccbc8488414f67bce3d246e2bbe57187884533ca 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 31705bf7274936dec909225e5ab0dca7f335991a..ccbc8488414f67bce3d246e2bbe57187884533ca 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 5dd8edb546155fc7e8853bb5c80f470cd6f59582..268d1589438eb6dd9ec7b22b040f49efb510fe6f 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index e7559f7c592a7f467c806bf67acf4d1a67a27ee8..cb043bc5980f04797822ef52b0e2f643b410a364 100644 (file)
@@ -1335,6 +1335,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 31705bf7274936dec909225e5ab0dca7f335991a..ccbc8488414f67bce3d246e2bbe57187884533ca 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index e12aa8f541a09519568ed8536910d24cf76196a1..b4927dbb2e81fe490b8315badf1760732668955f 100644 (file)
@@ -992,6 +992,7 @@ GLIBC_2.43 fmodf F
 GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index a950051f651d90417fe7f61c0c2bf75ff41ffea1..90089d142802b75987192529ca81f6b2977d7b3e 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index a950051f651d90417fe7f61c0c2bf75ff41ffea1..90089d142802b75987192529ca81f6b2977d7b3e 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 9430555eef08e5d7a029c52dbe1a52c5fee06e1d..666d67867d20c4873de55b7089fcdb22c41d63ac 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 7a53aeb36dafc581bca143350e609f05d35ec895..ee49433203a2d4ef442ab43878550713da9a924d 100644 (file)
@@ -1294,6 +1294,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 133e240209b090180ef11bae754781d5f7c98739..fa7d38edc6a5dd2f1c1812ce589d99d11e111207 100644 (file)
@@ -1106,6 +1106,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 7395a4b2e6fbdc96edd2e793d602b473a2d4ae3b..cb79ecc5d708fcff9bf3eeb1642ae0959afff0ed 100644 (file)
@@ -1105,6 +1105,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index c2762fe9782f3a216acaa3e79220a490e14ee6ea..e7d13a48e983a7d0dd798e0e4efe40a8e6d3142b 100644 (file)
@@ -1099,6 +1099,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 8acbad450ed8619bfac1b60033afd112880d4bf8..8362b4eb68f2bae3e0914efad661525680b40569 100644 (file)
@@ -1483,6 +1483,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index e3c406590bb29f47f4a478fdc8b94d88dc9ec606..56a38af71b263c22e58ae47ac1dbecaafef8c533 100644 (file)
@@ -1397,6 +1397,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 5f61a937bf2900debcaee2d006c1aeb6f2ed7247..457a2856d9111e669f1533cb84266878a6a6815f 100644 (file)
@@ -1397,6 +1397,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index afaf39be67ac3968758154dfd2e6c401369bae8c..8a026ba740a94ba8a59b7bf20c9351ab47d98ba1 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index afaf39be67ac3968758154dfd2e6c401369bae8c..8a026ba740a94ba8a59b7bf20c9351ab47d98ba1 100644 (file)
@@ -959,6 +959,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index b2db446b82702427d55283e1971f7163ee673a65..af62388c05569a07e08f5bb1e9a663a7c6d9b71a 100644 (file)
@@ -1404,6 +1404,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index c8db2148fb62dcd78b9d8aa0c067480c2cebf99f..61dc56a894313049e6f5863cf66639e7959f0a96 100644 (file)
@@ -1294,6 +1294,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index 1e4663f8304ef17dc2d878e831f87d896923c087..cea77965ca07c388d5aa8f1cd2027a999990a78f 100644 (file)
@@ -1327,6 +1327,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F
index f818987242604477f11c9bdde990580f41f5d587..c4d83222502041e080eef22921cb4c6698a6cba1 100644 (file)
@@ -1327,6 +1327,7 @@ GLIBC_2.43 j0f F
 GLIBC_2.43 j1f F
 GLIBC_2.43 jnf F
 GLIBC_2.43 log10f F
+GLIBC_2.43 remainderf F
 GLIBC_2.43 y0f F
 GLIBC_2.43 y1f F
 GLIBC_2.43 ynf F