[PATCH v2 09/28] math: Don't redirect inlined builtin math functions

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Thu Oct 30 14:14:34 GMT 2025



On 28/10/25 17:02, H.J. Lu wrote:
> On Wed, Oct 29, 2025 at 4:00 AM Adhemerval Zanella Netto
> <adhemerval.zanella@linaro.org> wrote:
>>
>>
>>
>> On 28/10/25 16:05, H.J. Lu wrote:
>>> On Wed, Oct 29, 2025 at 1:12 AM Adhemerval Zanella
>>> <adhemerval.zanella@linaro.org> wrote:
>>>>
>>>> When we want to inline builtin math functions, like truncf, for
>>>>
>>>>   extern float truncf (float __x) __attribute__ ((__nothrow__ )) __attribute__ ((__const__));
>>>>   extern float __truncf (float __x) __attribute__ ((__nothrow__ )) __attribute__ ((__const__));
>>>>
>>>>   float (truncf) (float) asm ("__truncf");
>>>>
>>>> compiler may redirect truncf calls to __truncf, instead of inlining it
>>>> (for instance, clang).  The USE_TRUNCF_BUILTIN is 1 to indicate that
>>>> truncf should be inlined.  In this case, we don't want the truncf
>>>> redirection:
>>>>
>>>>   1. For each math function which may be inlined, we define
>>>>
>>>>   #if USE_TRUNCF_BUILTIN
>>>>    # define NO_truncf_BUILTIN inline_truncf
>>>>    #else
>>>>    # define NO_truncf_BUILTIN truncf
>>>>    #endif
>>>>
>>>> in <math-use-builtins.h>.
>>>>
>>>>   2. Include <math-use-builtins.h> in include/math.h.
>>>>
>>>>   3. Change MATH_REDIRECT to
>>>>
>>>>    #define MATH_REDIRECT(FUNC, PREFIX, ARGS)            \
>>>>     float (NO_ ## FUNC ## f ## _BUILTIN) (ARGS (float)) \
>>>>       asm (PREFIX #FUNC "f");
>>>>
>>>> With this change If USE_TRUNCF_BUILTIN is 0, we get
>>>>
>>>>   float (truncf) (float) asm ("__truncf");
>>>>   truncf will be redirected to __truncf.
>>>>
>>>> And for USE_TRUNCF_BUILTIN 1, we get:
>>>>
>>>>   float (inline_truncf) (float) asm ("__truncf");
>>>>
>>>> In both cases either truncf will be inlined or the internal alias
>>>> (__truncf) will be called.
>>>>
>>>> It is not required for all math-use-builtin symbol, only the one
>>>> defined in math.h.  It also allows to remove all the math-use-builtin
>>>> inclusion, since it is now implicitly included by math.h.
>>>>
>>>> For MIPS, some math-use-builtin headers include sysdep.h and this
>>>> in turn includes a lot of extra headers that do not allow ldbl-128
>>>> code to override alias definition (math.h will include
>>>> some stdlib.h definition).  The math-use-builtin only requires
>>>> the __mips_isa_rev, so move the defintion to sgidefs.h.
>>>>
>>>> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
>>>> Co-authored-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
>>>> ---
>>>>  include/math.h                                |  19 +-
>>>>  math/s_fmax_template.c                        |   1 -
>>>>  math/s_fmin_template.c                        |   2 -
>>>>  sysdeps/generic/math-use-builtins-copysign.h  |   2 +
>>>>  sysdeps/generic/math-use-builtins.h           | 196 ++++++++++++++++++
>>>>  sysdeps/ieee754/dbl-64/e_hypot.c              |   1 -
>>>>  sysdeps/ieee754/dbl-64/e_sqrt.c               |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_ceil.c               |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_floor.c              |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_fma.c                |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_fmaf.c               |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_llrint.c             |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_llround.c            |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_logb.c               |   2 -
>>>>  sysdeps/ieee754/dbl-64/s_lrint.c              |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_lround.c             |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_modf.c               |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_nearbyint.c          |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_rint.c               |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_round.c              |   2 -
>>>>  sysdeps/ieee754/dbl-64/s_roundeven.c          |   1 -
>>>>  sysdeps/ieee754/dbl-64/s_trunc.c              |   2 -
>>>>  sysdeps/ieee754/float128/float128_private.h   |   1 -
>>>>  sysdeps/ieee754/flt-32/e_sqrtf.c              |   1 -
>>>>  sysdeps/ieee754/flt-32/s_ceilf.c              |   1 -
>>>>  sysdeps/ieee754/flt-32/s_floorf.c             |   1 -
>>>>  sysdeps/ieee754/flt-32/s_llrintf.c            |   1 -
>>>>  sysdeps/ieee754/flt-32/s_llroundf.c           |   1 -
>>>>  sysdeps/ieee754/flt-32/s_logbf.c              |   1 -
>>>>  sysdeps/ieee754/flt-32/s_lrintf.c             |   1 -
>>>>  sysdeps/ieee754/flt-32/s_lroundf.c            |   1 -
>>>>  sysdeps/ieee754/flt-32/s_modff.c              |   1 -
>>>>  sysdeps/ieee754/flt-32/s_nearbyintf.c         |   1 -
>>>>  sysdeps/ieee754/flt-32/s_rintf.c              |   1 -
>>>>  sysdeps/ieee754/flt-32/s_roundevenf.c         |   1 -
>>>>  sysdeps/ieee754/flt-32/s_roundf.c             |   1 -
>>>>  sysdeps/ieee754/flt-32/s_truncf.c             |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_ceill.c            |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_copysignl.c        |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_floorl.c           |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_fma.c              |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_fmal.c             |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_llrintl.c          |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_logbl.c            |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_lrintl.c           |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_nearbyintl.c       |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_rintl.c            |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_roundevenl.c       |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_roundl.c           |   1 -
>>>>  sysdeps/ieee754/ldbl-128/s_truncl.c           |   1 -
>>>>  sysdeps/ieee754/ldbl-128ibm/s_fabsl.c         |   1 -
>>>>  sysdeps/ieee754/ldbl-96/s_fabsl.c             |   1 -
>>>>  sysdeps/mips/fpu/math-use-builtins-fma.h      |   2 +-
>>>>  sysdeps/mips/math-use-builtins-ffs.h          |   2 +-
>>>>  sysdeps/mips/sgidefs.h                        |   3 +
>>>>  sysdeps/powerpc/fpu/e_sqrt.c                  |   1 -
>>>>  sysdeps/powerpc/fpu/e_sqrtf.c                 |   1 -
>>>>  sysdeps/unix/mips/sysdep.h                    |   3 -
>>>>  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_modff-avx.c    |   4 -
>>>>  sysdeps/x86_64/fpu/multiarch/s_modff-sse4_1.c |   4 -
>>>>  62 files changed, 216 insertions(+), 82 deletions(-)
>>>>
>>>> diff --git a/include/math.h b/include/math.h
>>>> index 9ba4708801..0fac2fda84 100644
>>>> --- a/include/math.h
>>>> +++ b/include/math.h
>>>> @@ -139,25 +139,32 @@ fabsf128 (_Float128 x)
>>>>  /* NB: Internal tests don't have access to internal symbols.  */
>>>>  # if !IS_IN (testsuite_internal) \
>>>>       && !(defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0)
>>>> +/* NB: Do not redirect math builtin functions when they are inlined.  */
>>>> +# include <math-use-builtins.h>
>>>>  #  ifndef NO_MATH_REDIRECT
>>>
>>> Is it possible to remove NO_MATH_REDIRECT?
>>
>> I am not sure, the NO_MATH_REDIRECT is used on some implementations for
>> symbol themselves to avoid a circular code generation.  We will need
>> to find a way to advertise that we are building the symbol itself that
>> might contain the very alias it adds.
> 
> The current comments say it is for PLT only.   Please verify your claim
> and update NO_MATH_REDIRECT comments.

As an experiment I commented out NO_MATH_REDIRECT from sysdeps/ieee754/dbl-64/s_fma.c
and for armhf I get:

$ arm-glibc-linux-gnueabihf-gcc ../sysdeps/ieee754/dbl-64/s_fma.c [...]
/tmp/cczW8fhm.s: Assembler messages:
/tmp/cczW8fhm.s:1457: Error: symbol `__fma' is already defined

The assembly does have two __fma reference:

[...]
        .type   __fma, %function
__fma:
        @ args = 0, pretend = 0, frame = 64
        @ frame_needed = 0, uses_anonymous_args = 0
[...]
        .weak   __fma
        .set    __fma,__fma
[...]

If you may I can add your extended comment [1] on this patch.

[1] https://sourceware.org/pipermail/libc-alpha/2025-October/171726.html


More information about the Libc-alpha mailing list