math: Fix x86_64 build for -Os (BZ 33367)

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>
This commit is contained in:
Adhemerval Zanella 2025-09-09 17:47:21 -03:00 committed by H.J. Lu
parent f0b88eb784
commit b5d88fa6c3
7 changed files with 43 additions and 0 deletions

View 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

View File

@ -1,3 +1,6 @@
#include <math_private.h>
#define __modf __modf_avx
#define trunc __trunc
#include <sysdeps/ieee754/dbl-64/s_modf.c>

View File

@ -1,3 +1,6 @@
#include <math_private.h>
#define __modf __modf_sse41
#define trunc __trunc
#include <sysdeps/ieee754/dbl-64/s_modf.c>

View 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>

View File

@ -1,3 +1,6 @@
#include <math_private.h>
#define __modff __modff_avx
#define truncf __truncf
#include <sysdeps/ieee754/flt-32/s_modff.c>

View File

@ -1,3 +1,6 @@
#include <math_private.h>
#define __modff __modff_sse41
#define truncf __truncf
#include <sysdeps/ieee754/flt-32/s_modff.c>

View 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>