[v2,2/4] iee754: provide gcc builtins based generic fma functions

Message ID 20200602003541.21005-3-vgupta@synopsys.com
State Superseded
Headers
Series Enable generic math code for more arches |

Commit Message

Vineet Gupta June 2, 2020, 12:35 a.m. UTC
  ---
 sysdeps/generic/math-use-builtins.h         | 5 +++++
 sysdeps/ieee754/dbl-64/s_fma.c              | 6 ++++++
 sysdeps/ieee754/dbl-64/s_fmaf.c             | 6 ++++++
 sysdeps/ieee754/float128/float128_private.h | 2 ++
 sysdeps/ieee754/ldbl-128/s_fmal.c           | 5 +++++
 5 files changed, 24 insertions(+)
  

Comments

Stefan Liebler June 2, 2020, 12:51 p.m. UTC | #1
On 6/2/20 2:35 AM, Vineet Gupta via Libc-alpha wrote:
> ---
>  sysdeps/generic/math-use-builtins.h         | 5 +++++
>  sysdeps/ieee754/dbl-64/s_fma.c              | 6 ++++++
>  sysdeps/ieee754/dbl-64/s_fmaf.c             | 6 ++++++
>  sysdeps/ieee754/float128/float128_private.h | 2 ++
>  sysdeps/ieee754/ldbl-128/s_fmal.c           | 5 +++++
>  5 files changed, 24 insertions(+)
> 
> diff --git a/sysdeps/generic/math-use-builtins.h b/sysdeps/generic/math-use-builtins.h
> index fc724c824a17..cf25ed8a2138 100644
> --- a/sysdeps/generic/math-use-builtins.h
> +++ b/sysdeps/generic/math-use-builtins.h
> @@ -63,4 +63,9 @@
>  #define USE_SQRT_BUILTIN 0
>  #define USE_SQRTF_BUILTIN 0
> 
> +#define USE_FMA_BUILTIN 0
> +#define USE_FMAF_BUILTIN 0
> +#define USE_FMAL_BUILTIN 0
> +#define USE_FMAF128_BUILTIN 0
> +
>  #endif /* math-use-builtins.h */
Please also update the current architecture specific math-use-builtins.h
file: sysdeps/s390/fpu/math-use-builtins.h
Otherwise it will break build on s390x.

> diff --git a/sysdeps/ieee754/dbl-64/s_fma.c b/sysdeps/ieee754/dbl-64/s_fma.c
> index 876df6e78bdc..9dc5b132b9ee 100644
> --- a/sysdeps/ieee754/dbl-64/s_fma.c
> +++ b/sysdeps/ieee754/dbl-64/s_fma.c
> @@ -25,6 +25,7 @@
>  #include <fenv_private.h>
>  #include <libm-alias-double.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:
> @@ -33,6 +34,10 @@
>  double
>  __fma (double x, double y, double z)
>  {
> +#if USE_FMA_BUILTIN
> +  return __builtin_fma (x, y, z);
Architectures which have support for ldbl-128 will use the file
sysdeps/ieee754/ldbl-128/s_fma.c instead of
sysdeps/ieee754/dbl-64/s_fma.c. Should this file also be adjusted in
order to use the builtin if USE_FMA_BUILTIN is set to one?
> +#else
> +  /* Use generic implementation.  */
>    union ieee754_double u, v, w;
>    int adjust = 0;
>    u.d = x;
> @@ -292,6 +297,7 @@ __fma (double x, double y, double z)
>        v.ieee.mantissa1 |= j;
>        return v.d * 0x1p-108;
>      }
> +#endif /* ! USE_FMA_BUILTIN  */
>  }
>  #ifndef __fma
>  libm_alias_double (__fma, fma)
> diff --git a/sysdeps/ieee754/dbl-64/s_fmaf.c b/sysdeps/ieee754/dbl-64/s_fmaf.c
> index 57329d0a87fe..93b8660d5242 100644
> --- a/sysdeps/ieee754/dbl-64/s_fmaf.c
> +++ b/sysdeps/ieee754/dbl-64/s_fmaf.c
> @@ -23,6 +23,7 @@
>  #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
> @@ -33,6 +34,10 @@
>  float
>  __fmaf (float x, float y, float z)
>  {
> +#if USE_FMAF_BUILTIN
> +  return __builtin_fmaf (x, y, z);
> +#else
> +  /* Use generic implementation.  */
>    fenv_t env;
> 
>    /* Multiplication is always exact.  */
> @@ -60,6 +65,7 @@ __fmaf (float x, float y, float z)
> 
>    /* And finally truncation with round to nearest.  */
>    return (float) u.d;
> +#endif /* ! USE_FMAF_BUILTIN  */
>  }
>  #ifndef __fmaf
>  libm_alias_float (__fma, fma)
> diff --git a/sysdeps/ieee754/float128/float128_private.h b/sysdeps/ieee754/float128/float128_private.h
> index f97463d9dc1b..ab6fc9f3c9cf 100644
> --- a/sysdeps/ieee754/float128/float128_private.h
> +++ b/sysdeps/ieee754/float128/float128_private.h
> @@ -154,6 +154,8 @@
>  #define USE_ROUNDL_BUILTIN USE_ROUNDF128_BUILTIN
>  #undef USE_COPYSIGNL_BUILTIN
>  #define USE_COPYSIGNL_BUILTIN USE_COPYSIGNF128_BUILTIN
> +#undef USE_FMAL_BUILTIN
> +#define USE_FMAL_BUILTIN USE_FMAF128_BUILTIN
> 
>  /* IEEE function renames.  */
>  #define __ieee754_acoshl __ieee754_acoshf128
> diff --git a/sysdeps/ieee754/ldbl-128/s_fmal.c b/sysdeps/ieee754/ldbl-128/s_fmal.c
> index 7475015bcec6..a610499e47c7 100644
> --- a/sysdeps/ieee754/ldbl-128/s_fmal.c
> +++ b/sysdeps/ieee754/ldbl-128/s_fmal.c
> @@ -25,6 +25,7 @@
>  #include <math_private.h>
>  #include <libm-alias-ldouble.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:
> @@ -33,6 +34,9 @@
>  _Float128
>  __fmal (_Float128 x, _Float128 y, _Float128 z)
>  {
> +#if USE_FMAL_BUILTIN
> +  return __builtin_fmal (x, y, z);
> +#else
>    union ieee854_long_double u, v, w;
>    int adjust = 0;
>    u.d = x;
> @@ -296,5 +300,6 @@ __fmal (_Float128 x, _Float128 y, _Float128 z)
>        v.ieee.mantissa3 |= j;
>        return v.d * L(0x1p-228);
>      }
> +#endif /* ! USE_FMAL_BUILTIN  */
>  }
>  libm_alias_ldouble (__fma, fma)
>
  
Vineet Gupta June 2, 2020, 5:13 p.m. UTC | #2
On 6/2/20 5:51 AM, Stefan Liebler via Libc-alpha wrote:
>  #endif /* math-use-builtins.h */
> Please also update the current architecture specific math-use-builtins.h
> file: sysdeps/s390/fpu/math-use-builtins.h
> Otherwise it will break build on s390x.

Done.

>> diff --git a/sysdeps/ieee754/dbl-64/s_fma.c b/sysdeps/ieee754/dbl-64/s_fma.c
>> index 876df6e78bdc..9dc5b132b9ee 100644
>> --- a/sysdeps/ieee754/dbl-64/s_fma.c
>> +++ b/sysdeps/ieee754/dbl-64/s_fma.c
>> @@ -25,6 +25,7 @@
>>  #include <fenv_private.h>
>>  #include <libm-alias-double.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:
>> @@ -33,6 +34,10 @@
>>  double
>>  __fma (double x, double y, double z)
>>  {
>> +#if USE_FMA_BUILTIN
>> +  return __builtin_fma (x, y, z);
>
> Architectures which have support for ldbl-128 will use the file
> sysdeps/ieee754/ldbl-128/s_fma.c instead of
> sysdeps/ieee754/dbl-64/s_fma.c. Should this file also be adjusted in
> order to use the builtin if USE_FMA_BUILTIN is set to one?

Right.

I used commit f82996f815 "Use GCC builtins for round functions if desired" as
starting point for my change. And seems it was not an ideal reference :-) as round
has far fewer instances than fma. Indeed fma is present in ldbl-128 and dbl-64 so
needs updating in both.

But just to be sure s390 is currently not using the newly introduced builtins so
I'll keep them as follows.

#define USE_SQRT_BUILTIN 0
#define USE_SQRTF_BUILTIN 0

#define USE_FMA_BUILTIN 0
#define USE_FMAF_BUILTIN 0
#define USE_FMAL_BUILTIN 0
#define USE_FMAF128_BUILTIN 0

-Vineet
  
Adhemerval Zanella Netto June 2, 2020, 5:27 p.m. UTC | #3
On 02/06/2020 14:13, Vineet Gupta via Libc-alpha wrote:
> On 6/2/20 5:51 AM, Stefan Liebler via Libc-alpha wrote:
>>  #endif /* math-use-builtins.h */
>> Please also update the current architecture specific math-use-builtins.h
>> file: sysdeps/s390/fpu/math-use-builtins.h
>> Otherwise it will break build on s390x.
> 
> Done.
> 
>>> diff --git a/sysdeps/ieee754/dbl-64/s_fma.c b/sysdeps/ieee754/dbl-64/s_fma.c
>>> index 876df6e78bdc..9dc5b132b9ee 100644
>>> --- a/sysdeps/ieee754/dbl-64/s_fma.c
>>> +++ b/sysdeps/ieee754/dbl-64/s_fma.c
>>> @@ -25,6 +25,7 @@
>>>  #include <fenv_private.h>
>>>  #include <libm-alias-double.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:
>>> @@ -33,6 +34,10 @@
>>>  double
>>>  __fma (double x, double y, double z)
>>>  {
>>> +#if USE_FMA_BUILTIN
>>> +  return __builtin_fma (x, y, z);
>>
>> Architectures which have support for ldbl-128 will use the file
>> sysdeps/ieee754/ldbl-128/s_fma.c instead of
>> sysdeps/ieee754/dbl-64/s_fma.c. Should this file also be adjusted in
>> order to use the builtin if USE_FMA_BUILTIN is set to one?
> 
> Right.
> 
> I used commit f82996f815 "Use GCC builtins for round functions if desired" as
> starting point for my change. And seems it was not an ideal reference :-) as round
> has far fewer instances than fma. Indeed fma is present in ldbl-128 and dbl-64 so
> needs updating in both.

I think after this set is upstream I will refactor to make each symbol and its
variant (i.g sqrt, sqrtf, etc.) to be define on its own file.  The default
math-use-builtins will then include each file:

  /* math-use-builtins.h  */
  #inclde <math-use-builtins-sqrt.h>
  [...]

With default USE_* begin 0. This would allow to add new builtin usage without 
require to sync with every architecture (and without breaking its build as well).

> 
> But just to be sure s390 is currently not using the newly introduced builtins so
> I'll keep them as follows.
> 
> #define USE_SQRT_BUILTIN 0
> #define USE_SQRTF_BUILTIN 0
> 
> #define USE_FMA_BUILTIN 0
> #define USE_FMAF_BUILTIN 0
> #define USE_FMAL_BUILTIN 0
> #define USE_FMAF128_BUILTIN 0
> 
> -Vineet

It is ok, we can optimize s390 in a subsequent patch (since it does provide fma
and sqrtf builtins).
  
Adhemerval Zanella Netto June 2, 2020, 5:28 p.m. UTC | #4
On 01/06/2020 21:35, Vineet Gupta wrote:
> ---
>  sysdeps/generic/math-use-builtins.h         | 5 +++++
>  sysdeps/ieee754/dbl-64/s_fma.c              | 6 ++++++
>  sysdeps/ieee754/dbl-64/s_fmaf.c             | 6 ++++++
>  sysdeps/ieee754/float128/float128_private.h | 2 ++
>  sysdeps/ieee754/ldbl-128/s_fmal.c           | 5 +++++
>  5 files changed, 24 insertions(+)


LGTM with s390 fix pointed by Stefan.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>

> 
> diff --git a/sysdeps/generic/math-use-builtins.h b/sysdeps/generic/math-use-builtins.h
> index fc724c824a17..cf25ed8a2138 100644
> --- a/sysdeps/generic/math-use-builtins.h
> +++ b/sysdeps/generic/math-use-builtins.h
> @@ -63,4 +63,9 @@
>  #define USE_SQRT_BUILTIN 0
>  #define USE_SQRTF_BUILTIN 0
>  
> +#define USE_FMA_BUILTIN 0
> +#define USE_FMAF_BUILTIN 0
> +#define USE_FMAL_BUILTIN 0
> +#define USE_FMAF128_BUILTIN 0
> +
>  #endif /* math-use-builtins.h */
> diff --git a/sysdeps/ieee754/dbl-64/s_fma.c b/sysdeps/ieee754/dbl-64/s_fma.c
> index 876df6e78bdc..9dc5b132b9ee 100644
> --- a/sysdeps/ieee754/dbl-64/s_fma.c
> +++ b/sysdeps/ieee754/dbl-64/s_fma.c
> @@ -25,6 +25,7 @@
>  #include <fenv_private.h>
>  #include <libm-alias-double.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:
> @@ -33,6 +34,10 @@
>  double
>  __fma (double x, double y, double z)
>  {
> +#if USE_FMA_BUILTIN
> +  return __builtin_fma (x, y, z);
> +#else
> +  /* Use generic implementation.  */
>    union ieee754_double u, v, w;
>    int adjust = 0;
>    u.d = x;
> @@ -292,6 +297,7 @@ __fma (double x, double y, double z)
>        v.ieee.mantissa1 |= j;
>        return v.d * 0x1p-108;
>      }
> +#endif /* ! USE_FMA_BUILTIN  */
>  }
>  #ifndef __fma
>  libm_alias_double (__fma, fma)
> diff --git a/sysdeps/ieee754/dbl-64/s_fmaf.c b/sysdeps/ieee754/dbl-64/s_fmaf.c
> index 57329d0a87fe..93b8660d5242 100644
> --- a/sysdeps/ieee754/dbl-64/s_fmaf.c
> +++ b/sysdeps/ieee754/dbl-64/s_fmaf.c
> @@ -23,6 +23,7 @@
>  #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
> @@ -33,6 +34,10 @@
>  float
>  __fmaf (float x, float y, float z)
>  {
> +#if USE_FMAF_BUILTIN
> +  return __builtin_fmaf (x, y, z);
> +#else
> +  /* Use generic implementation.  */
>    fenv_t env;
>  
>    /* Multiplication is always exact.  */
> @@ -60,6 +65,7 @@ __fmaf (float x, float y, float z)
>  
>    /* And finally truncation with round to nearest.  */
>    return (float) u.d;
> +#endif /* ! USE_FMAF_BUILTIN  */
>  }
>  #ifndef __fmaf
>  libm_alias_float (__fma, fma)
> diff --git a/sysdeps/ieee754/float128/float128_private.h b/sysdeps/ieee754/float128/float128_private.h
> index f97463d9dc1b..ab6fc9f3c9cf 100644
> --- a/sysdeps/ieee754/float128/float128_private.h
> +++ b/sysdeps/ieee754/float128/float128_private.h
> @@ -154,6 +154,8 @@
>  #define USE_ROUNDL_BUILTIN USE_ROUNDF128_BUILTIN
>  #undef USE_COPYSIGNL_BUILTIN
>  #define USE_COPYSIGNL_BUILTIN USE_COPYSIGNF128_BUILTIN
> +#undef USE_FMAL_BUILTIN
> +#define USE_FMAL_BUILTIN USE_FMAF128_BUILTIN
>  
>  /* IEEE function renames.  */
>  #define __ieee754_acoshl __ieee754_acoshf128
> diff --git a/sysdeps/ieee754/ldbl-128/s_fmal.c b/sysdeps/ieee754/ldbl-128/s_fmal.c
> index 7475015bcec6..a610499e47c7 100644
> --- a/sysdeps/ieee754/ldbl-128/s_fmal.c
> +++ b/sysdeps/ieee754/ldbl-128/s_fmal.c
> @@ -25,6 +25,7 @@
>  #include <math_private.h>
>  #include <libm-alias-ldouble.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:
> @@ -33,6 +34,9 @@
>  _Float128
>  __fmal (_Float128 x, _Float128 y, _Float128 z)
>  {
> +#if USE_FMAL_BUILTIN
> +  return __builtin_fmal (x, y, z);
> +#else
>    union ieee854_long_double u, v, w;
>    int adjust = 0;
>    u.d = x;
> @@ -296,5 +300,6 @@ __fmal (_Float128 x, _Float128 y, _Float128 z)
>        v.ieee.mantissa3 |= j;
>        return v.d * L(0x1p-228);
>      }
> +#endif /* ! USE_FMAL_BUILTIN  */
>  }
>  libm_alias_ldouble (__fma, fma)
>
  
Vineet Gupta June 3, 2020, 12:20 a.m. UTC | #5
On 6/2/20 10:27 AM, Adhemerval Zanella via Libc-alpha wrote:
>> I used commit f82996f815 "Use GCC builtins for round functions if desired" as
>> starting point for my change. And seems it was not an ideal reference :-) as round
>> has far fewer instances than fma. Indeed fma is present in ldbl-128 and dbl-64 so
>> needs updating in both.
> I think after this set is upstream I will refactor to make each symbol and its
> variant (i.g sqrt, sqrtf, etc.) to be define on its own file.  The default
> math-use-builtins will then include each file:
> 
>   /* math-use-builtins.h  */
>   #inclde <math-use-builtins-sqrt.h>
>   [...]
> 
> With default USE_* begin 0. This would allow to add new builtin usage without 
> require to sync with every architecture (and without breaking its build as well).

OK !
  
Stefan Liebler June 3, 2020, 6:29 a.m. UTC | #6
On 6/2/20 7:13 PM, Vineet Gupta wrote:
> On 6/2/20 5:51 AM, Stefan Liebler via Libc-alpha wrote:
>>  #endif /* math-use-builtins.h */
>> Please also update the current architecture specific math-use-builtins.h
>> file: sysdeps/s390/fpu/math-use-builtins.h
>> Otherwise it will break build on s390x.
> 
> Done.
> 
>>> diff --git a/sysdeps/ieee754/dbl-64/s_fma.c b/sysdeps/ieee754/dbl-64/s_fma.c
>>> index 876df6e78bdc..9dc5b132b9ee 100644
>>> --- a/sysdeps/ieee754/dbl-64/s_fma.c
>>> +++ b/sysdeps/ieee754/dbl-64/s_fma.c
>>> @@ -25,6 +25,7 @@
>>>  #include <fenv_private.h>
>>>  #include <libm-alias-double.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:
>>> @@ -33,6 +34,10 @@
>>>  double
>>>  __fma (double x, double y, double z)
>>>  {
>>> +#if USE_FMA_BUILTIN
>>> +  return __builtin_fma (x, y, z);
>>
>> Architectures which have support for ldbl-128 will use the file
>> sysdeps/ieee754/ldbl-128/s_fma.c instead of
>> sysdeps/ieee754/dbl-64/s_fma.c. Should this file also be adjusted in
>> order to use the builtin if USE_FMA_BUILTIN is set to one?
> 
> Right.
> 
> I used commit f82996f815 "Use GCC builtins for round functions if desired" as
> starting point for my change. And seems it was not an ideal reference :-) as round
> has far fewer instances than fma. Indeed fma is present in ldbl-128 and dbl-64 so
> needs updating in both.
> 
> But just to be sure s390 is currently not using the newly introduced builtins so
> I'll keep them as follows.
> 
> #define USE_SQRT_BUILTIN 0
> #define USE_SQRTF_BUILTIN 0
> 
> #define USE_FMA_BUILTIN 0
> #define USE_FMAF_BUILTIN 0
> #define USE_FMAL_BUILTIN 0
> #define USE_FMAF128_BUILTIN 0
> 
Yes. Those should be disabled for now in order to not break anything. As
soon as you've committed your patches, I'll have a look if I can
activate some of them and remove s390 specific implementations. But I
first have to check with the gcc guys, if the builtins are always
expanded correctly in various gcc versions.

Thanks.
Stefan
> -Vineet
>
  

Patch

diff --git a/sysdeps/generic/math-use-builtins.h b/sysdeps/generic/math-use-builtins.h
index fc724c824a17..cf25ed8a2138 100644
--- a/sysdeps/generic/math-use-builtins.h
+++ b/sysdeps/generic/math-use-builtins.h
@@ -63,4 +63,9 @@ 
 #define USE_SQRT_BUILTIN 0
 #define USE_SQRTF_BUILTIN 0
 
+#define USE_FMA_BUILTIN 0
+#define USE_FMAF_BUILTIN 0
+#define USE_FMAL_BUILTIN 0
+#define USE_FMAF128_BUILTIN 0
+
 #endif /* math-use-builtins.h */
diff --git a/sysdeps/ieee754/dbl-64/s_fma.c b/sysdeps/ieee754/dbl-64/s_fma.c
index 876df6e78bdc..9dc5b132b9ee 100644
--- a/sysdeps/ieee754/dbl-64/s_fma.c
+++ b/sysdeps/ieee754/dbl-64/s_fma.c
@@ -25,6 +25,7 @@ 
 #include <fenv_private.h>
 #include <libm-alias-double.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:
@@ -33,6 +34,10 @@ 
 double
 __fma (double x, double y, double z)
 {
+#if USE_FMA_BUILTIN
+  return __builtin_fma (x, y, z);
+#else
+  /* Use generic implementation.  */
   union ieee754_double u, v, w;
   int adjust = 0;
   u.d = x;
@@ -292,6 +297,7 @@  __fma (double x, double y, double z)
       v.ieee.mantissa1 |= j;
       return v.d * 0x1p-108;
     }
+#endif /* ! USE_FMA_BUILTIN  */
 }
 #ifndef __fma
 libm_alias_double (__fma, fma)
diff --git a/sysdeps/ieee754/dbl-64/s_fmaf.c b/sysdeps/ieee754/dbl-64/s_fmaf.c
index 57329d0a87fe..93b8660d5242 100644
--- a/sysdeps/ieee754/dbl-64/s_fmaf.c
+++ b/sysdeps/ieee754/dbl-64/s_fmaf.c
@@ -23,6 +23,7 @@ 
 #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
@@ -33,6 +34,10 @@ 
 float
 __fmaf (float x, float y, float z)
 {
+#if USE_FMAF_BUILTIN
+  return __builtin_fmaf (x, y, z);
+#else
+  /* Use generic implementation.  */
   fenv_t env;
 
   /* Multiplication is always exact.  */
@@ -60,6 +65,7 @@  __fmaf (float x, float y, float z)
 
   /* And finally truncation with round to nearest.  */
   return (float) u.d;
+#endif /* ! USE_FMAF_BUILTIN  */
 }
 #ifndef __fmaf
 libm_alias_float (__fma, fma)
diff --git a/sysdeps/ieee754/float128/float128_private.h b/sysdeps/ieee754/float128/float128_private.h
index f97463d9dc1b..ab6fc9f3c9cf 100644
--- a/sysdeps/ieee754/float128/float128_private.h
+++ b/sysdeps/ieee754/float128/float128_private.h
@@ -154,6 +154,8 @@ 
 #define USE_ROUNDL_BUILTIN USE_ROUNDF128_BUILTIN
 #undef USE_COPYSIGNL_BUILTIN
 #define USE_COPYSIGNL_BUILTIN USE_COPYSIGNF128_BUILTIN
+#undef USE_FMAL_BUILTIN
+#define USE_FMAL_BUILTIN USE_FMAF128_BUILTIN
 
 /* IEEE function renames.  */
 #define __ieee754_acoshl __ieee754_acoshf128
diff --git a/sysdeps/ieee754/ldbl-128/s_fmal.c b/sysdeps/ieee754/ldbl-128/s_fmal.c
index 7475015bcec6..a610499e47c7 100644
--- a/sysdeps/ieee754/ldbl-128/s_fmal.c
+++ b/sysdeps/ieee754/ldbl-128/s_fmal.c
@@ -25,6 +25,7 @@ 
 #include <math_private.h>
 #include <libm-alias-ldouble.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:
@@ -33,6 +34,9 @@ 
 _Float128
 __fmal (_Float128 x, _Float128 y, _Float128 z)
 {
+#if USE_FMAL_BUILTIN
+  return __builtin_fmal (x, y, z);
+#else
   union ieee854_long_double u, v, w;
   int adjust = 0;
   u.d = x;
@@ -296,5 +300,6 @@  __fmal (_Float128 x, _Float128 y, _Float128 z)
       v.ieee.mantissa3 |= j;
       return v.d * L(0x1p-228);
     }
+#endif /* ! USE_FMAL_BUILTIN  */
 }
 libm_alias_ldouble (__fma, fma)