[PATCH v2] math: Fix x86_64 build for -Os (BZ 33367)

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Sep 9 12:21:06 GMT 2025



On 09/09/25 01:11, H.J. Lu wrote:
> On Mon, Sep 8, 2025 at 1:47 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>>
>> On Mon, Sep 8, 2025 at 1:11 PM Adhemerval Zanella Netto
>> <adhemerval.zanella@linaro.org> wrote:
>>>
>>>
>>>
>>> On 08/09/25 17:09, H.J. Lu wrote:
>>>> 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.
>>>
>>> Would it make a lot of difference on modf implementation to add this extra
>>> switch?
>>
>> I think an extra
>>
>> #if GCC_COMPAT(16, 1)
>>
>> is simple enough.  But it can wait.
> 
> GCC 16 is fixed.  But -fno-trapping-math doesn't work well due to
> 
> https://sourceware.org/bugzilla/show_bug.cgi?id=33405
> 
> since -fsignaling-nans takes precedence over -fno-trapping-math.
> 
> We should add a configure test to check if the GCC bug is fixed.

I am not sure which direction to use, both your patch enable -fno-trapping-math [1]
or this one works.  Your changes adds some generic Makefile rules, and the extra
use-builtins-trunc* which eventually we will need to remove since it is already
fixed upstream; the inline version at least is more clear of what tried to
accomplish and do not mess with the math flags.


More information about the Libc-alpha mailing list