]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Fix x86_64 build for -Os (BZ 33367)
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 9 Sep 2025 20:47:21 +0000 (17:47 -0300)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 11 Sep 2025 13:23:33 +0000 (06:23 -0700)
The compiler might not inline the trunc function call for
USE_TRUNC_BUILTIN [1].

This patch adds an optimized __trunc/__truncf for x86 used
on modf ifunc variant to avoid the trunc libcall.

Checked on x86_64, x86_64-v2, x86_64-v3, and x86_64-v4. Used -O2 and
-Os options. Performed a full make check on x86_64 with both
 optimizations.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121861
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
sysdeps/x86/fpu/math_private.h
sysdeps/x86_64/fpu/multiarch/s_modf-avx.c
sysdeps/x86_64/fpu/multiarch/s_modf-sse4_1.c
sysdeps/x86_64/fpu/multiarch/s_modf.c
sysdeps/x86_64/fpu/multiarch/s_modff-avx.c
sysdeps/x86_64/fpu/multiarch/s_modff-sse4_1.c
sysdeps/x86_64/fpu/multiarch/s_modff.c

index 132f01180967509069e28c473ebc692b93da3215..d30d580ceac44e65ee04069aa39f32ada4ad898a 100644 (file)
@@ -19,6 +19,7 @@
 #ifndef X86_MATH_PRIVATE_H
 #define X86_MATH_PRIVATE_H 1
 
+#include <math.h>
 #include_next <math_private.h>
 
 __extern_always_inline long double
@@ -29,4 +30,30 @@ __NTH (__ieee754_atan2l (long double y, long double x))
   return ret;
 }
 
+__extern_always_inline double
+__trunc (double x)
+{
+#ifdef __AVX__
+  asm ("vroundsd $11, %1, %1, %0" : "=v" (x) : "v" (x));
+#elif defined __SSE4_1__
+  asm ("roundsd $11, %1, %0" : "=x" (x) : "x" (x));
+#else
+  x = trunc (x);
+#endif
+  return x;
+}
+
+__extern_always_inline float
+__truncf (float x)
+{
+#ifdef __AVX__
+  asm ("vroundss $11, %1, %1, %0" : "=v" (x) : "v" (x));
+#elif defined __SSE4_1__
+  asm ("roundss $11, %1, %0" : "=x" (x) : "x" (x));
+#else
+  x = truncf (x);
+#endif
+  return x;
+}
+
 #endif
index ab4f03db0ee19cd7249e93d4f6fc37bc645435b2..0982280d252cf2b8b9b61c73655cfeec29d3a65e 100644 (file)
@@ -1,3 +1,6 @@
+#include <math_private.h>
+
 #define __modf __modf_avx
+#define trunc __trunc
 
 #include <sysdeps/ieee754/dbl-64/s_modf.c>
index 00aa8cd7365682460eef1f9c0aa73318df2e0032..f6fb996f979d993dfb1a0365b867423417c3c96c 100644 (file)
@@ -1,3 +1,6 @@
+#include <math_private.h>
+
 #define __modf __modf_sse41
+#define trunc __trunc
 
 #include <sysdeps/ieee754/dbl-64/s_modf.c>
index e365bfcef7d3686f8d0b5b3bc450a225928bb29a..a108ae5dc6c49876daefabe9baedc6e6deb5d69d 100644 (file)
@@ -38,4 +38,6 @@ libm_alias_double (__modf, modf)
 #  define __modf __modf_sse2
 # endif
 #endif
+#include <math_private.h>
+#define trunc __trunc
 #include <sysdeps/ieee754/dbl-64/s_modf.c>
index 07cb9c10360387edc2ca975673bcafa7a0723910..b2afe1efe3c35cb85864c35881019edbdbf2238d 100644 (file)
@@ -1,3 +1,6 @@
+#include <math_private.h>
+
 #define __modff __modff_avx
+#define truncf __truncf
 
 #include <sysdeps/ieee754/flt-32/s_modff.c>
index 060c5e39796f6f4a2819fbb9ae0d55992c0a64d6..0352c3ea4ba14a2f3fbe947cc5bd944190796e28 100644 (file)
@@ -1,3 +1,6 @@
+#include <math_private.h>
+
 #define __modff __modff_sse41
+#define truncf __truncf
 
 #include <sysdeps/ieee754/flt-32/s_modff.c>
index a4b542903771577c6ee66b4722e6a01995a81823..62d7645bc76eb536943a268011c295eadb593c8a 100644 (file)
@@ -38,4 +38,6 @@ libm_alias_float (__modf, modf)
 #  define __modff __modff_sse2
 # endif
 #endif
+#include <math_private.h>
+#define truncf __truncf
 #include <sysdeps/ieee754/flt-32/s_modff.c>