1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-12-06 12:01:08 +03:00

math: Don't redirect inlined builtin math functions

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>
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
This commit is contained in:
Adhemerval Zanella
2025-11-10 10:28:56 -03:00
parent a0ce8b0779
commit 13cfd77bf5
59 changed files with 225 additions and 79 deletions

View File

@@ -139,25 +139,35 @@ 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)
/* NO_MATH_REDIRECT must be defined in the source implementing function,
FUNC, if FUNC is implemented as an alias of __FUNC or vice versa to
avoid redirecting FUNC to __FUNC. */
# include <math-use-builtins.h>
/* NB: Do not redirect math builtin functions when they are inlined. */
# ifndef NO_MATH_REDIRECT
/* Declare some functions for use within GLIBC. Compilers typically
inline those functions as a single instruction. Use an asm to
avoid use of PLTs if it doesn't. */
# define MATH_REDIRECT(FUNC, PREFIX, ARGS) \
float (FUNC ## f) (ARGS (float)) asm (PREFIX #FUNC "f"); \
double (FUNC) (ARGS (double)) asm (PREFIX #FUNC ); \
float (NO_ ## FUNC ## f ## _BUILTIN) (ARGS (float)) \
asm (PREFIX #FUNC "f"); \
double (NO_ ## FUNC ## _BUILTIN) (ARGS (double)) \
asm (PREFIX #FUNC ); \
MATH_REDIRECT_LDBL (FUNC, PREFIX, ARGS) \
MATH_REDIRECT_F128 (FUNC, PREFIX, ARGS)
# if defined __NO_LONG_DOUBLE_MATH \
|| __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
# define MATH_REDIRECT_LDBL(FUNC, PREFIX, ARGS)
# else
# define MATH_REDIRECT_LDBL(FUNC, PREFIX, ARGS) \
long double (FUNC ## l) (ARGS (long double)) asm (PREFIX #FUNC "l");
# define MATH_REDIRECT_LDBL(FUNC, PREFIX, ARGS) \
long double (NO_ ## FUNC ## l ## _BUILTIN) (ARGS (long double)) \
asm (PREFIX #FUNC "l");
# endif
# if __HAVE_DISTINCT_FLOAT128
# define MATH_REDIRECT_F128(FUNC, PREFIX, ARGS) \
_Float128 (FUNC ## f128) (ARGS (_Float128)) asm (PREFIX #FUNC "f128");
# define MATH_REDIRECT_F128(FUNC, PREFIX, ARGS) \
_Float128 (NO_ ## FUNC ## f128 ## _BUILTIN) (ARGS (_Float128)) \
asm (PREFIX #FUNC "f128");
# else
# define MATH_REDIRECT_F128(FUNC, PREFIX, ARGS)
# endif

View File

@@ -17,7 +17,6 @@
<https://www.gnu.org/licenses/>. */
#include <math.h>
#include <math-use-builtins.h>
FLOAT
M_DECL_FUNC (__fmax) (FLOAT x, FLOAT y)

View File

@@ -17,8 +17,6 @@
<https://www.gnu.org/licenses/>. */
#include <math.h>
#include <math-use-builtins.h>
FLOAT
M_DECL_FUNC (__fmin) (FLOAT x, FLOAT y)

View File

@@ -1,4 +1,6 @@
/* Generic implementations for float and double always use the builtin. */
#define USE_COPYSIGNF_BUILTIN 1
#define USE_COPYSIGN_BUILTIN 1
#define USE_COPYSIGNL_BUILTIN 1
#if __GNUC_PREREQ (7, 0)
# define USE_COPYSIGNF128_BUILTIN 1

View File

@@ -44,4 +44,200 @@
#include <math-use-builtins-lround.h>
#include <math-use-builtins-llround.h>
/* Disable internal alias optimizations done at include/math.h if the
compiler can expand the builtin for the symbol. Different than gcc, the
clang will always expand the alias before handling the builtin expansion,
which makes the builtin expansion ineffective. */
#if USE_SQRT_BUILTIN
# define NO_sqrt_BUILTIN inline_sqrt
#else
# define NO_sqrt_BUILTIN sqrt
#endif
#if USE_SQRTF_BUILTIN
# define NO_sqrtf_BUILTIN inline_sqrtf
#else
# define NO_sqrtf_BUILTIN sqrtf
#endif
#if USE_SQRTL_BUILTIN
# define NO_sqrtl_BUILTIN inline_sqrtl
#else
# define NO_sqrtl_BUILTIN sqrtl
#endif
#if USE_SQRTF128_BUILTIN
# define NO_sqrtf128_BUILTIN inline_sqrtf128
#else
# define NO_sqrtf128_BUILTIN sqrtf128
#endif
#if USE_CEIL_BUILTIN
# define NO_ceil_BUILTIN inline_ceil
#else
# define NO_ceil_BUILTIN ceil
#endif
#if USE_CEILF_BUILTIN
# define NO_ceilf_BUILTIN inline_ceilf
#else
# define NO_ceilf_BUILTIN ceilf
#endif
#if USE_CEILL_BUILTIN
# define NO_ceill_BUILTIN inline_ceill
#else
# define NO_ceill_BUILTIN ceill
#endif
#if USE_CEILF128_BUILTIN
# define NO_ceilf128_BUILTIN inline_ceilf128
#else
# define NO_ceilf128_BUILTIN ceilf128
#endif
#if USE_FLOOR_BUILTIN
# define NO_floor_BUILTIN inline_floor
#else
# define NO_floor_BUILTIN floor
#endif
#if USE_FLOORF_BUILTIN
# define NO_floorf_BUILTIN inline_floorf
#else
# define NO_floorf_BUILTIN floorf
#endif
#if USE_FLOORL_BUILTIN
# define NO_floorl_BUILTIN inline_floorl
#else
# define NO_floorl_BUILTIN floorl
#endif
#if USE_FLOORF128_BUILTIN
# define NO_floorf128_BUILTIN inline_floorf128
#else
# define NO_floorf128_BUILTIN floorf128
#endif
#if USE_ROUNDEVEN_BUILTIN
# define NO_roundeven_BUILTIN inline_roundeven
#else
# define NO_roundeven_BUILTIN roundeven
#endif
#if USE_ROUNDEVENF_BUILTIN
# define NO_roundevenf_BUILTIN inline_roundevenf
#else
# define NO_roundevenf_BUILTIN roundevenf
#endif
#if USE_ROUNDEVENL_BUILTIN
# define NO_roundevenl_BUILTIN inline_roundevenl
#else
# define NO_roundevenl_BUILTIN roundevenl
#endif
#if USE_ROUNDEVENF128_BUILTIN
# define NO_roundevenf128_BUILTIN inline_roundevenf128
#else
# define NO_roundevenf128_BUILTIN roundevenf128
#endif
#if USE_RINT_BUILTIN
# define NO_rint_BUILTIN inline_rint
#else
# define NO_rint_BUILTIN rint
#endif
#if USE_RINTF_BUILTIN
# define NO_rintf_BUILTIN inline_rintf
#else
# define NO_rintf_BUILTIN rintf
#endif
#if USE_RINTL_BUILTIN
# define NO_rintl_BUILTIN inline_rintl
#else
# define NO_rintl_BUILTIN rintl
#endif
#if USE_RINTF128_BUILTIN
# define NO_rintf128_BUILTIN inline_rintf128
#else
# define NO_rintf128_BUILTIN rintf128
#endif
#if USE_TRUNC_BUILTIN
# define NO_trunc_BUILTIN inline_trunc
#else
# define NO_trunc_BUILTIN trunc
#endif
#if USE_TRUNCF_BUILTIN
# define NO_truncf_BUILTIN inline_truncf
#else
# define NO_truncf_BUILTIN truncf
#endif
#if USE_TRUNCL_BUILTIN
# define NO_truncl_BUILTIN inline_truncl
#else
# define NO_truncl_BUILTIN truncl
#endif
#if USE_TRUNCF128_BUILTIN
# define NO_truncf128_BUILTIN inline_truncf128
#else
# define NO_truncf128_BUILTIN truncf128
#endif
#if USE_ROUND_BUILTIN
# define NO_round_BUILTIN inline_round
#else
# define NO_round_BUILTIN round
#endif
#if USE_ROUNDF_BUILTIN
# define NO_roundf_BUILTIN inline_roundf
#else
# define NO_roundf_BUILTIN roundf
#endif
#if USE_ROUNDL_BUILTIN
# define NO_roundl_BUILTIN inline_roundl
#else
# define NO_roundl_BUILTIN roundl
#endif
#if USE_ROUNDF128_BUILTIN
# define NO_roundf128_BUILTIN inline_roundf128
#else
# define NO_roundf128_BUILTIN roundf128
#endif
#if USE_COPYSIGN_BUILTIN
# define NO_copysign_BUILTIN inline_copysign
#else
# define NO_copysign_BUILTIN copysign
#endif
#if USE_COPYSIGNF_BUILTIN
# define NO_copysignf_BUILTIN inline_copysignf
#else
# define NO_copysignf_BUILTIN copysignf
#endif
#if USE_COPYSIGNL_BUILTIN
# define NO_copysignl_BUILTIN inline_copysignl
#else
# define NO_copysignl_BUILTIN copysignl
#endif
#if USE_COPYSIGNF128_BUILTIN
# define NO_copysignf128_BUILTIN inline_copysignf128
#else
# define NO_copysignf128_BUILTIN copysignf128
#endif
#if USE_FMA_BUILTIN
# define NO_fma_BUILTIN inline_fma
#else
# define NO_fma_BUILTIN fma
#endif
#if USE_FMAF_BUILTIN
# define NO_fmaf_BUILTIN inline_fmaf
#else
# define NO_fmaf_BUILTIN fmaf
#endif
#if USE_FMAL_BUILTIN
# define NO_fmal_BUILTIN inline_fmal
#else
# define NO_fmal_BUILTIN fmal
#endif
#if USE_FMAF128_BUILTIN
# define NO_fmaf128_BUILTIN inline_fmaf128
#else
# define NO_fmaf128_BUILTIN fmaf128
#endif
#endif /* MATH_USE_BUILTINS_H */

View File

@@ -39,7 +39,6 @@
#include <math_private.h>
#include <math-underflow.h>
#include <math-narrow-eval.h>
#include <math-use-builtins.h>
#include <math-svid-compat.h>
#include <libm-alias-finite.h>
#include <libm-alias-double.h>

View File

@@ -40,7 +40,6 @@
#include <math_private.h>
#include <fenv_private.h>
#include <libm-alias-finite.h>
#include <math-use-builtins.h>
/*********************************************************************/
/* An ultimate sqrt routine. Given an IEEE double machine number x */

View File

@@ -21,7 +21,6 @@
#include <math.h>
#include <math_private.h>
#include <libm-alias-double.h>
#include <math-use-builtins.h>
double
__ceil (double x)

View File

@@ -34,7 +34,6 @@
#include <math_private.h>
#include <stdint.h>
#include <libm-alias-double.h>
#include <math-use-builtins.h>
/*
* floor(x)

View File

@@ -30,7 +30,6 @@
#include <libm-alias-double.h>
#include <math-narrow-alias.h>
#include <tininess.h>
#include <math-use-builtins.h>
/* This implementation uses rounding to odd to avoid problems with
double rounding. See a paper by Boldo and Melquiond:

View File

@@ -23,7 +23,6 @@
#include <math-barriers.h>
#include <fenv_private.h>
#include <libm-alias-float.h>
#include <math-use-builtins.h>
/* This implementation relies on double being more than twice as
precise as float and uses rounding to odd in order to avoid problems

View File

@@ -25,7 +25,6 @@
#include <math_private.h>
#include <libm-alias-double.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long long int

View File

@@ -27,7 +27,6 @@
#include <math_private.h>
#include <libm-alias-double.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long long int
__llround (double x)

View File

@@ -17,11 +17,9 @@
<https://www.gnu.org/licenses/>. */
#include <math.h>
#include <math_private.h>
#include <libm-alias-double.h>
#include <fix-int-fp-convert-zero.h>
#include <math-use-builtins.h>
double
__logb (double x)

View File

@@ -25,7 +25,6 @@
#include <math_private.h>
#include <libm-alias-double.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long int

View File

@@ -23,7 +23,6 @@
#include <math_private.h>
#include <libm-alias-double.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
/* For LP64, lround is an alias for llround. */
#ifndef _LP64

View File

@@ -19,7 +19,6 @@
#include <math.h>
#include <libm-alias-double.h>
#include "math_config.h"
#include <math-use-builtins-trunc.h>
double
__modf (double x, double *iptr)

View File

@@ -25,7 +25,6 @@
#include <math_private.h>
#include <fenv_private.h>
#include <libm-alias-double.h>
#include <math-use-builtins.h>
double
__nearbyint (double x)

View File

@@ -23,7 +23,6 @@
#include <math.h>
#include <math_private.h>
#include <libm-alias-double.h>
#include <math-use-builtins.h>
double
__rint (double x)

View File

@@ -22,8 +22,6 @@
#include <math_private.h>
#include <libm-alias-double.h>
#include <stdint.h>
#include <math-use-builtins.h>
double
__round (double x)

View File

@@ -21,7 +21,6 @@
#include <math_private.h>
#include <libm-alias-double.h>
#include <stdint.h>
#include <math-use-builtins.h>
#define BIAS 0x3ff
#define MANT_DIG 53

View File

@@ -21,8 +21,6 @@
#include <math_private.h>
#include <libm-alias-double.h>
#include <math-use-builtins.h>
double
__trunc (double x)

View File

@@ -142,7 +142,6 @@
#define libm_alias_ldouble_narrow(from, to) \
libm_alias_float128_narrow (from, to)
#include <math-use-builtins.h>
#undef USE_NEARBYINTL_BUILTIN
#define USE_NEARBYINTL_BUILTIN USE_NEARBYINTF128_BUILTIN
#undef USE_RINTL_BUILTIN

View File

@@ -16,7 +16,6 @@
#include <math.h>
#include <math_private.h>
#include <libm-alias-float.h>
#include <math-use-builtins.h>
float
__ceilf (float x)

View File

@@ -23,7 +23,6 @@
#include <math.h>
#include <math_private.h>
#include <libm-alias-float.h>
#include <math-use-builtins.h>
float
__floorf (float x)

View File

@@ -25,7 +25,6 @@
#include <math_private.h>
#include <libm-alias-float.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long long int

View File

@@ -23,7 +23,6 @@
#include <math_private.h>
#include <libm-alias-float.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long long int
__llroundf (float x)

View File

@@ -16,7 +16,6 @@
#include <math_private.h>
#include <libm-alias-float.h>
#include <fix-int-fp-convert-zero.h>
#include <math-use-builtins.h>
float
__logbf (float x)

View File

@@ -25,7 +25,6 @@
#include <math_private.h>
#include <libm-alias-float.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long int

View File

@@ -23,7 +23,6 @@
#include <math_private.h>
#include <libm-alias-float.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long int
__lroundf (float x)

View File

@@ -19,7 +19,6 @@
#include <math.h>
#include <libm-alias-float.h>
#include "math_config.h"
#include <math-use-builtins-trunc.h>
float
__modff (float x, float *iptr)

View File

@@ -19,7 +19,6 @@
#include <math_private.h>
#include <fenv_private.h>
#include <libm-alias-float.h>
#include <math-use-builtins.h>
float
__nearbyintf (float x)

View File

@@ -16,7 +16,6 @@
#include <math.h>
#include <math_private.h>
#include <libm-alias-float.h>
#include <math-use-builtins.h>
float
__rintf (float x)

View File

@@ -21,7 +21,6 @@
#include <math.h>
#include <math_private.h>
#include <libm-alias-float.h>
#include <math-use-builtins.h>
#include <stdint.h>
#define BIAS 0x7f

View File

@@ -21,7 +21,6 @@
#include <math_private.h>
#include <libm-alias-float.h>
#include <math-use-builtins.h>
float

View File

@@ -21,7 +21,6 @@
#include <math_private.h>
#include <libm-alias-float.h>
#include <math-use-builtins.h>
float

View File

@@ -27,7 +27,6 @@ static char rcsid[] = "$NetBSD: $";
#include <math.h>
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
_Float128
__ceill (_Float128 x)

View File

@@ -26,7 +26,6 @@ static char rcsid[] = "$NetBSD: $";
#include <math.h>
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
_Float128
__copysignl (_Float128 x, _Float128 y)

View File

@@ -27,7 +27,6 @@ static char rcsid[] = "$NetBSD: $";
#include <math.h>
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
_Float128
__floorl (_Float128 x)

View File

@@ -28,7 +28,6 @@
#include <libm-alias-ldouble.h>
#include <math-narrow-alias.h>
#include <tininess.h>
#include <math-use-builtins.h>
/* This implementation uses rounding to odd to avoid problems with
double rounding. See a paper by Boldo and Melquiond:

View File

@@ -24,7 +24,6 @@
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long long int

View File

@@ -25,7 +25,6 @@ static char rcsid[] = "$NetBSD: $";
#include <math.h>
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
_Float128
__logbl (_Float128 x)

View File

@@ -24,7 +24,6 @@
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <fix-fp-int-convert-overflow.h>
#include <math-use-builtins.h>
long int

View File

@@ -27,7 +27,6 @@
#include <math-barriers.h>
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
_Float128
__nearbyintl (_Float128 x)

View File

@@ -30,7 +30,6 @@ static char rcsid[] = "$NetBSD: $";
#include <math.h>
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
_Float128
__rintl (_Float128 x)

View File

@@ -21,7 +21,6 @@
#include <math.h>
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
#include <stdint.h>
#define BIAS 0x3fff

View File

@@ -21,7 +21,6 @@
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
_Float128

View File

@@ -21,7 +21,6 @@
#include <math_private.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
_Float128

View File

@@ -24,7 +24,6 @@ static char rcsid[] = "$NetBSD: $";
#include <math.h>
#include <math_private.h>
#include <math_ldbl_opt.h>
#include <math-use-builtins.h>
long double __fabsl(long double x)
{

View File

@@ -18,7 +18,6 @@
#include <math.h>
#include <libm-alias-ldouble.h>
#include <math-use-builtins.h>
long double
__fabsl (long double x)

View File

@@ -19,7 +19,7 @@
ISA, double support can be subsetted. Only FMAF is enabled for this
case. */
#include <sysdep.h>
#include <isarev.h>
#if __mips_isa_rev >= 6
# ifdef __mips_single_float

8
sysdeps/mips/isarev.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef _ISAREV_H
#define _ISAREV_H
#ifndef __mips_isa_rev
# define __mips_isa_rev 0
#endif
#endif

View File

@@ -1,3 +1,3 @@
#include <sysdep.h>
#include <isarev.h>
#define USE_FFS_BUILTIN (__mips_isa_rev >= 1)
#define USE_FFSLL_BUILTIN (__mips_isa_rev >= 1)

View File

@@ -20,7 +20,6 @@
#include <math_private.h>
#include <fenv_libc.h>
#include <libm-alias-finite.h>
#include <math-use-builtins.h>
double
__ieee754_sqrt (double x)

View File

@@ -15,12 +15,10 @@
License along with the GNU C Library. If not, see
<https://www.gnu.org/licenses/>. */
#include <isarev.h>
#include <sgidefs.h>
#include <sysdeps/unix/sysdep.h>
#ifndef __mips_isa_rev
# define __mips_isa_rev 0
#endif
#ifdef __ASSEMBLER__

View File

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

View File

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

View File

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

View File

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