[PATCH v2] math: Fix x86_64 build for -Os (BZ 33367)
H.J. Lu
hjl.tools@gmail.com
Mon Sep 8 20:09:24 GMT 2025
On Mon, Sep 8, 2025 at 12:56 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> 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
> ---
> sysdeps/x86/fpu/math_private.h | 27 +++++++++++++++++++
> sysdeps/x86_64/fpu/multiarch/s_modf-avx.c | 4 +++
> sysdeps/x86_64/fpu/multiarch/s_modf-sse4_1.c | 4 +++
> sysdeps/x86_64/fpu/multiarch/s_modf.c | 3 ++-
> sysdeps/x86_64/fpu/multiarch/s_modff-avx.c | 4 +++
> sysdeps/x86_64/fpu/multiarch/s_modff-sse4_1.c | 4 +++
> sysdeps/x86_64/fpu/multiarch/s_modff.c | 3 ++-
> 7 files changed, 47 insertions(+), 2 deletions(-)
>
> diff --git a/sysdeps/x86/fpu/math_private.h b/sysdeps/x86/fpu/math_private.h
> index 132f011809..d30d580cea 100644
> --- a/sysdeps/x86/fpu/math_private.h
> +++ b/sysdeps/x86/fpu/math_private.h
> @@ -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;
> +}
I will fix GCC 16. Please make sure that inline asm isn't used for
GCC 16 or above.
> +__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
> diff --git a/sysdeps/x86_64/fpu/multiarch/s_modf-avx.c b/sysdeps/x86_64/fpu/multiarch/s_modf-avx.c
> index ab4f03db0e..2f84d254a9 100644
> --- a/sysdeps/x86_64/fpu/multiarch/s_modf-avx.c
> +++ b/sysdeps/x86_64/fpu/multiarch/s_modf-avx.c
> @@ -1,3 +1,7 @@
> +#include <sysdeps/x86/isa-level.h>
> +#include <math_private.h>
> +
> #define __modf __modf_avx
> +#define trunc __trunc
>
> #include <sysdeps/ieee754/dbl-64/s_modf.c>
> diff --git a/sysdeps/x86_64/fpu/multiarch/s_modf-sse4_1.c b/sysdeps/x86_64/fpu/multiarch/s_modf-sse4_1.c
> index 00aa8cd736..dcf04097f2 100644
> --- a/sysdeps/x86_64/fpu/multiarch/s_modf-sse4_1.c
> +++ b/sysdeps/x86_64/fpu/multiarch/s_modf-sse4_1.c
> @@ -1,3 +1,7 @@
> +#include <sysdeps/x86/isa-level.h>
> +#include <math_private.h>
> +
> #define __modf __modf_sse41
> +#define trunc __trunc
>
> #include <sysdeps/ieee754/dbl-64/s_modf.c>
> diff --git a/sysdeps/x86_64/fpu/multiarch/s_modf.c b/sysdeps/x86_64/fpu/multiarch/s_modf.c
> index e365bfcef7..61f77566b1 100644
> --- a/sysdeps/x86_64/fpu/multiarch/s_modf.c
> +++ b/sysdeps/x86_64/fpu/multiarch/s_modf.c
> @@ -18,7 +18,6 @@
>
> #include <sysdeps/x86/isa-level.h>
> #if MINIMUM_X86_ISA_LEVEL < AVX_X86_ISA_LEVEL
> -# define NO_MATH_REDIRECT
> # include <libm-alias-double.h>
>
> # define modf __redirect_modf
> @@ -38,4 +37,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>
> diff --git a/sysdeps/x86_64/fpu/multiarch/s_modff-avx.c b/sysdeps/x86_64/fpu/multiarch/s_modff-avx.c
> index 07cb9c1036..c121ec26e2 100644
> --- a/sysdeps/x86_64/fpu/multiarch/s_modff-avx.c
> +++ b/sysdeps/x86_64/fpu/multiarch/s_modff-avx.c
> @@ -1,3 +1,7 @@
> +#include <sysdeps/x86/isa-level.h>
> +#include <math_private.h>
> +
> #define __modff __modff_avx
> +#define truncf __truncf
>
> #include <sysdeps/ieee754/flt-32/s_modff.c>
> diff --git a/sysdeps/x86_64/fpu/multiarch/s_modff-sse4_1.c b/sysdeps/x86_64/fpu/multiarch/s_modff-sse4_1.c
> index 060c5e3979..30a785d385 100644
> --- a/sysdeps/x86_64/fpu/multiarch/s_modff-sse4_1.c
> +++ b/sysdeps/x86_64/fpu/multiarch/s_modff-sse4_1.c
> @@ -1,3 +1,7 @@
> +#include <sysdeps/x86/isa-level.h>
> +#include <math_private.h>
> +
> #define __modff __modff_sse41
> +#define truncf __truncf
>
> #include <sysdeps/ieee754/flt-32/s_modff.c>
> diff --git a/sysdeps/x86_64/fpu/multiarch/s_modff.c b/sysdeps/x86_64/fpu/multiarch/s_modff.c
> index a4b5429037..ebf16777fd 100644
> --- a/sysdeps/x86_64/fpu/multiarch/s_modff.c
> +++ b/sysdeps/x86_64/fpu/multiarch/s_modff.c
> @@ -18,7 +18,6 @@
>
> #include <sysdeps/x86/isa-level.h>
> #if MINIMUM_X86_ISA_LEVEL < AVX_X86_ISA_LEVEL
> -# define NO_MATH_REDIRECT
> # include <libm-alias-float.h>
>
> # define modff __redirect_modff
> @@ -38,4 +37,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>
> --
> 2.43.0
>
--
H.J.
More information about the Libc-alpha
mailing list