[6/6] Add a generic scalb implementation

Message ID 938d4a14c41385b4a5c8144b029e3caa84353115.1580757509.git.murphyp@linux.vnet.ibm.com
State Superseded
Delegated to: Joseph Myers
Headers

Commit Message

Paul E. Murphy Feb. 3, 2020, 9:10 p.m. UTC
  From: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>

Create templates for e_scabl and w_scalb.
---
 math/Makefile                              |  7 +--
 math/{e_scalb.c => e_scalb_template.c}     | 33 ++++++------
 math/e_scalbf.c                            | 54 --------------------
 math/e_scalbl.c                            | 54 --------------------
 math/w_scalb_compat.c                      |  6 +--
 math/w_scalb_template.c                    | 59 ++++++++++++++++++++++
 math/w_scalbf_compat.c                     |  4 +-
 math/w_scalbl_compat.c                     |  4 +-
 sysdeps/ieee754/float128/w_scalbf128.c     |  1 +
 sysdeps/ieee754/ldbl-opt/w_scalb_compat.c  |  4 +-
 sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c |  2 +
 11 files changed, 91 insertions(+), 137 deletions(-)
 rename math/{e_scalb.c => e_scalb_template.c} (64%)
 delete mode 100644 math/e_scalbf.c
 delete mode 100644 math/e_scalbl.c
 create mode 100644 math/w_scalb_template.c
 create mode 100644 sysdeps/ieee754/float128/w_scalbf128.c
  

Comments

Paul E Murphy Feb. 11, 2020, 8:20 p.m. UTC | #1
Ping? If no objections/feedback, I will commit with patch 1 in the 
series next week.

Tested on x86_64 and ppc64le.  I've also built/check-abi csky to verify 
the LIBM_SVID_COMPAT changes still generate the appropriate symbols for 
those arches not requiring the compat versions.

On 2/3/20 3:10 PM, Paul E. Murphy wrote:
> From: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>
> 
> Create templates for e_scabl and w_scalb.
> ---
>   math/Makefile                              |  7 +--
>   math/{e_scalb.c => e_scalb_template.c}     | 33 ++++++------
>   math/e_scalbf.c                            | 54 --------------------
>   math/e_scalbl.c                            | 54 --------------------
>   math/w_scalb_compat.c                      |  6 +--
>   math/w_scalb_template.c                    | 59 ++++++++++++++++++++++
>   math/w_scalbf_compat.c                     |  4 +-
>   math/w_scalbl_compat.c                     |  4 +-
>   sysdeps/ieee754/float128/w_scalbf128.c     |  1 +
>   sysdeps/ieee754/ldbl-opt/w_scalb_compat.c  |  4 +-
>   sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c |  2 +
>   11 files changed, 91 insertions(+), 137 deletions(-)
>   rename math/{e_scalb.c => e_scalb_template.c} (64%)
>   delete mode 100644 math/e_scalbf.c
>   delete mode 100644 math/e_scalbl.c
>   create mode 100644 math/w_scalb_template.c
>   create mode 100644 sysdeps/ieee754/float128/w_scalbf128.c
> 
> diff --git a/math/Makefile b/math/Makefile
> index 5985b6744b..1d203e7ad5 100644
> --- a/math/Makefile
> +++ b/math/Makefile
> @@ -48,7 +48,7 @@ libm-support = s_lib_version s_matherr s_signgam			\
> 
>   # Wrappers for these functions generated per type using a file named
>   # <func>_template.c and the appropriate math-type-macros-<TYPE>.h.
> -gen-libm-calls = cargF conjF cimagF crealF cabsF s_cacosF		  \
> +gen-libm-calls = cargF conjF cimagF crealF cabsF e_scalbF s_cacosF	  \
>   	         s_cacoshF s_ccosF s_ccoshF s_casinF s_csinF s_casinhF	  \
>   		 k_casinhF s_csinhF k_casinhF s_csinhF s_catanhF s_catanF \
>   		 s_ctanF s_ctanhF s_cexpF s_clogF s_cprojF s_csqrtF	  \
> @@ -58,13 +58,14 @@ gen-libm-calls = cargF conjF cimagF crealF cabsF s_cacosF		  \
>   		 w_log1pF w_scalblnF s_fmaxmagF s_fminmagF w_acosF	  \
>   		 w_acoshF w_asinF w_atan2F w_atanhF w_coshF w_exp10F	  \
>   		 w_exp2F w_fmodF w_hypotF w_j0F w_j1F w_jnF w_logF	  \
> -		 w_log10F w_log2F w_powF w_remainderF w_sinhF w_sqrtF	  \
> +		 w_log10F w_log2F w_powF w_remainderF w_scalbF		  \
> +		 w_sinhF w_sqrtF					  \
>   		 w_tgammaF w_lgammaF w_lgammaF_r w_expF e_exp2F
> 
>   libm-calls =								  \
>   	e_acosF e_acoshF e_asinF e_atan2F e_atanhF e_coshF e_expF e_fmodF \
>   	e_hypotF e_j0F e_j1F e_jnF e_lgammaF_r e_logF e_log10F e_powF	  \
> -	e_remainderF e_scalbF e_sinhF e_sqrtF e_gammaF_r	  \
> +	e_remainderF e_sinhF e_sqrtF e_gammaF_r				  \
>   	e_ilogbF							  \
>   	k_tanF s_asinhF s_atanF s_cbrtF					  \
>   	s_ceilF s_cosF s_erfF s_expm1F s_fabsF				  \
> diff --git a/math/e_scalb.c b/math/e_scalb_template.c
> similarity index 64%
> rename from math/e_scalb.c
> rename to math/e_scalb_template.c
> index dbe3b51d6a..ed0508c7bf 100644
> --- a/math/e_scalb.c
> +++ b/math/e_scalb_template.c
> @@ -1,6 +1,8 @@
> -/* Copyright (C) 2011-2020 Free Software Foundation, Inc.
> +/* Multiply by integral power of radix.
> +
> +   Copyright (C) 2011-2020 Free Software Foundation, Inc.
> +
>      This file is part of the GNU C Library.
> -   Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
> 
>      The GNU C Library is free software; you can redistribute it and/or
>      modify it under the terms of the GNU Lesser General Public
> @@ -20,35 +22,36 @@
>   #include <math_private.h>
>   #include <libm-alias-finite.h>
> 
> -static double
> +static FLOAT
>   __attribute__ ((noinline))
> -invalid_fn (double x, double fn)
> +invalid_fn (FLOAT x, FLOAT fn)
>   {
> -  if (rint (fn) != fn)
> +  if (M_SUF (rint) (fn) != fn)
>       return (fn - fn) / (fn - fn);
> -  else if (fn > 65000.0)
> -    return __scalbn (x, 65000);
> +  else if (fn > M_LIT (65000.0))
> +    return M_SUF (__scalbn) (x, 65000);
>     else
> -    return __scalbn (x,-65000);
> +    return M_SUF (__scalbn) (x,-65000);
>   }
> 
> 
> -double
> -__ieee754_scalb (double x, double fn)
> +FLOAT
> +M_DECL_FUNC (__ieee754_scalb) (FLOAT x, FLOAT fn)
>   {
>     if (__glibc_unlikely (isnan (x)))
>       return x * fn;
>     if (__glibc_unlikely (!isfinite (fn)))
>       {
> -      if (isnan (fn) || fn > 0.0)
> +      if (isnan (fn) || fn > M_LIT (0.0))
>   	return x * fn;
> -      if (x == 0.0)
> +      if (x == M_LIT (0.0))
>   	return x;
>         return x / -fn;
>       }
> -  if (__glibc_unlikely (fabs (fn) >= 0x1p31 || (double) (int) fn != fn))
> +  if (__glibc_unlikely (M_FABS (fn) >= M_LIT (0x1p31)
> +			|| (FLOAT) (int) fn != fn))
>       return invalid_fn (x, fn);
> 
> -  return __scalbn (x, (int) fn);
> +  return M_SCALBN (x, (int) fn);
>   }
> -libm_alias_finite (__ieee754_scalb, __scalb)
> +declare_mgen_finite_alias (__ieee754_scalb, __scalb)
> diff --git a/math/e_scalbf.c b/math/e_scalbf.c
> deleted file mode 100644
> index 944cfbefc9..0000000000
> --- a/math/e_scalbf.c
> +++ /dev/null
> @@ -1,54 +0,0 @@
> -/* Copyright (C) 2011-2020 Free Software Foundation, Inc.
> -   This file is part of the GNU C Library.
> -   Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
> -
> -   The GNU C Library is free software; you can redistribute it and/or
> -   modify it under the terms of the GNU Lesser General Public
> -   License as published by the Free Software Foundation; either
> -   version 2.1 of the License, or (at your option) any later version.
> -
> -   The GNU C Library is distributed in the hope that it will be useful,
> -   but WITHOUT ANY WARRANTY; without even the implied warranty of
> -   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> -   Lesser General Public License for more details.
> -
> -   You should have received a copy of the GNU Lesser General Public
> -   License along with the GNU C Library; if not, see
> -   <https://www.gnu.org/licenses/>.  */
> -
> -#include <math.h>
> -#include <math_private.h>
> -#include <libm-alias-finite.h>
> -
> -static float
> -__attribute__ ((noinline))
> -invalid_fn (float x, float fn)
> -{
> -  if (rintf (fn) != fn)
> -    return (fn - fn) / (fn - fn);
> -  else if (fn > 65000.0f)
> -    return __scalbnf (x, 65000);
> -  else
> -    return __scalbnf (x,-65000);
> -}
> -
> -
> -float
> -__ieee754_scalbf (float x, float fn)
> -{
> -  if (__glibc_unlikely (isnan (x)))
> -    return x * fn;
> -  if (__glibc_unlikely (!isfinite (fn)))
> -    {
> -      if (isnan (fn) || fn > 0.0f)
> -	return x * fn;
> -      if (x == 0.0f)
> -	return x;
> -      return x / -fn;
> -    }
> -  if (__glibc_unlikely (fabsf (fn) >= 0x1p31f || (float) (int) fn != fn))
> -    return invalid_fn (x, fn);
> -
> -  return __scalbnf (x, (int) fn);
> -}
> -libm_alias_finite (__ieee754_scalbf, __scalbf)
> diff --git a/math/e_scalbl.c b/math/e_scalbl.c
> deleted file mode 100644
> index 6595ec6c27..0000000000
> --- a/math/e_scalbl.c
> +++ /dev/null
> @@ -1,54 +0,0 @@
> -/* Copyright (C) 2011-2020 Free Software Foundation, Inc.
> -   This file is part of the GNU C Library.
> -   Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
> -
> -   The GNU C Library is free software; you can redistribute it and/or
> -   modify it under the terms of the GNU Lesser General Public
> -   License as published by the Free Software Foundation; either
> -   version 2.1 of the License, or (at your option) any later version.
> -
> -   The GNU C Library is distributed in the hope that it will be useful,
> -   but WITHOUT ANY WARRANTY; without even the implied warranty of
> -   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> -   Lesser General Public License for more details.
> -
> -   You should have received a copy of the GNU Lesser General Public
> -   License along with the GNU C Library; if not, see
> -   <https://www.gnu.org/licenses/>.  */
> -
> -#include <math.h>
> -#include <math_private.h>
> -#include <libm-alias-finite.h>
> -
> -static long double
> -__attribute__ ((noinline))
> -invalid_fn (long double x, long double fn)
> -{
> -  if (rintl (fn) != fn)
> -    return (fn - fn) / (fn - fn);
> -  else if (fn > 65000.0L)
> -    return __scalbnl (x, 65000);
> -  else
> -    return __scalbnl (x,-65000);
> -}
> -
> -
> -long double
> -__ieee754_scalbl (long double x, long double fn)
> -{
> -  if (__glibc_unlikely (isnan (x)))
> -    return x * fn;
> -  if (__glibc_unlikely (!isfinite (fn)))
> -    {
> -      if (isnan (fn) || fn > 0.0L)
> -	return x * fn;
> -      if (x == 0.0L)
> -	return x;
> -      return x / -fn;
> -    }
> -  if (__glibc_unlikely (fabsl (fn) >= 0x1p31L || (long double) (int) fn != fn))
> -    return invalid_fn (x, fn);
> -
> -  return __scalbnl (x, (int) fn);
> -}
> -libm_alias_finite (__ieee754_scalbl, __scalbl)
> diff --git a/math/w_scalb_compat.c b/math/w_scalb_compat.c
> index c3dd3db4f7..664c0474a7 100644
> --- a/math/w_scalb_compat.c
> +++ b/math/w_scalb_compat.c
> @@ -41,18 +41,15 @@ sysv_scalb (double x, double fn)
> 
>     return z;
>   }
> -#endif
> 
> 
>   /* Wrapper scalb */
>   double
>   __scalb (double x, double fn)
>   {
> -#if LIBM_SVID_COMPAT
>     if (__glibc_unlikely (_LIB_VERSION == _SVID_))
>       return sysv_scalb (x, fn);
>     else
> -#endif
>       {
>         double z = __ieee754_scalb (x, fn);
> 
> @@ -79,7 +76,8 @@ __scalb (double x, double fn)
>       }
>   }
>   weak_alias (__scalb, scalb)
> -#ifdef NO_LONG_DOUBLE
> +# ifdef NO_LONG_DOUBLE
>   strong_alias (__scalb, __scalbl)
>   weak_alias (__scalb, scalbl)
> +# endif
>   #endif
> diff --git a/math/w_scalb_template.c b/math/w_scalb_template.c
> new file mode 100644
> index 0000000000..b2aafcdf36
> --- /dev/null
> +++ b/math/w_scalb_template.c
> @@ -0,0 +1,59 @@
> +/* Wrapper to set errno for scalb.
> +   Copyright (C) 2018 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <http://www.gnu.org/licenses/>.  */
> +
> +/* Only build wrappers from the templates for the types that define the macro
> +   below.  This macro is set in math-type-macros-<type>.h in sysdeps/generic
> +   for each floating-point type.  */
> +#if __USE_WRAPPER_TEMPLATE
> +
> +#include <errno.h>
> +#include <math.h>
> +#include <math_private.h>
> +
> +/* Wrapper scalb */
> +FLOAT
> +M_DECL_FUNC (__scalb) (FLOAT x, FLOAT fn)
> +{
> +    {
> +      FLOAT z = M_SUF (__ieee754_scalb) (x, fn);
> +
> +      if (__glibc_unlikely (!isfinite (z) || z == M_LIT (0.0)))
> +	{
> +	  if (isnan (z))
> +	    {
> +	      if (!isnan (x) && !isnan (fn))
> +		__set_errno (EDOM);
> +	    }
> +	  else if (isinf (z))
> +	    {
> +	      if (!isinf (x) && !isinf (fn))
> +		__set_errno (ERANGE);
> +	    }
> +	  else
> +	    {
> +	      /* z == 0.  */
> +	      if (x != M_LIT (0.0) && !isinf (fn))
> +		__set_errno (ERANGE);
> +	    }
> +	}
> +      return z;
> +    }
> +}
> +declare_mgen_alias (__scalb, scalb);
> +
> +#endif /* __USE_WRAPPER_TEMPLATE.  */
> diff --git a/math/w_scalbf_compat.c b/math/w_scalbf_compat.c
> index 42bba44cc6..1b742e9b98 100644
> --- a/math/w_scalbf_compat.c
> +++ b/math/w_scalbf_compat.c
> @@ -41,18 +41,15 @@ sysv_scalbf (float x, float fn)
> 
>     return z;
>   }
> -#endif
> 
> 
>   /* Wrapper scalbf */
>   float
>   __scalbf (float x, float fn)
>   {
> -#if LIBM_SVID_COMPAT
>     if (__glibc_unlikely (_LIB_VERSION == _SVID_))
>       return sysv_scalbf (x, fn);
>     else
> -#endif
>       {
>         float z = __ieee754_scalbf (x, fn);
> 
> @@ -79,3 +76,4 @@ __scalbf (float x, float fn)
>       }
>   }
>   weak_alias (__scalbf, scalbf)
> +#endif
> diff --git a/math/w_scalbl_compat.c b/math/w_scalbl_compat.c
> index 8858c6b66d..c8ba3fddb4 100644
> --- a/math/w_scalbl_compat.c
> +++ b/math/w_scalbl_compat.c
> @@ -41,18 +41,15 @@ sysv_scalbl (long double x, long double fn)
> 
>     return z;
>   }
> -#endif
> 
> 
>   /* Wrapper scalbl */
>   long double
>   __scalbl (long double x, long double fn)
>   {
> -#if LIBM_SVID_COMPAT
>     if (__glibc_unlikely (_LIB_VERSION == _SVID_))
>       return sysv_scalbl (x, fn);
>     else
> -#endif
>       {
>         long double z = __ieee754_scalbl (x, fn);
> 
> @@ -79,3 +76,4 @@ __scalbl (long double x, long double fn)
>       }
>   }
>   weak_alias (__scalbl, scalbl)
> +#endif
> diff --git a/sysdeps/ieee754/float128/w_scalbf128.c b/sysdeps/ieee754/float128/w_scalbf128.c
> new file mode 100644
> index 0000000000..067b724164
> --- /dev/null
> +++ b/sysdeps/ieee754/float128/w_scalbf128.c
> @@ -0,0 +1 @@
> +/* Not defined for _FloatN types.  */
> diff --git a/sysdeps/ieee754/ldbl-opt/w_scalb_compat.c b/sysdeps/ieee754/ldbl-opt/w_scalb_compat.c
> index f6d53a5ba5..56735e825e 100644
> --- a/sysdeps/ieee754/ldbl-opt/w_scalb_compat.c
> +++ b/sysdeps/ieee754/ldbl-opt/w_scalb_compat.c
> @@ -1,5 +1,7 @@
>   #include <math_ldbl_opt.h>
>   #include <math/w_scalb_compat.c>
> -#if LONG_DOUBLE_COMPAT(libm, GLIBC_2_0)
> +#if LIBM_SVID_COMPAT
> +# if LONG_DOUBLE_COMPAT(libm, GLIBC_2_0)
>   compat_symbol (libm, __scalb, scalbl, GLIBC_2_0);
> +# endif
>   #endif
> diff --git a/sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c b/sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c
> index c8feb654a2..4d16f413c4 100644
> --- a/sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c
> +++ b/sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c
> @@ -2,4 +2,6 @@
>   #undef weak_alias
>   #define weak_alias(n,a)
>   #include <math/w_scalbl_compat.c>
> +#if LIBM_SVID_COMPAT
>   long_double_symbol (libm, __scalbl, scalbl);
> +#endif
>
  
Joseph Myers Feb. 12, 2020, 1:21 a.m. UTC | #2
On Tue, 11 Feb 2020, Paul E Murphy wrote:

> Tested on x86_64 and ppc64le.  I've also built/check-abi csky to verify the
> LIBM_SVID_COMPAT changes still generate the appropriate symbols for those
> arches not requiring the compat versions.

I think a complete build-many-glibcs build is appropriate here.  It's also 
important to verify that it's still the case that no scalbf128 is built - 
that on platforms with _Float128, all the *scalbf128* object files contain 
no code (via the sysdeps/ieee754/float128 versions taking precedence).

> > +/* Wrapper scalb */
> > +FLOAT
> > +M_DECL_FUNC (__scalb) (FLOAT x, FLOAT fn)
> > +{
> > +    {
> > +      FLOAT z = M_SUF (__ieee754_scalb) (x, fn);

The indentation seems wrong here, as well as having an unnecessary layer 
of braces.

Please repost the patch for review with these issues fixed and a more 
detailed commit message with some explanation of the patch and its 
motivation.
  

Patch

diff --git a/math/Makefile b/math/Makefile
index 5985b6744b..1d203e7ad5 100644
--- a/math/Makefile
+++ b/math/Makefile
@@ -48,7 +48,7 @@  libm-support = s_lib_version s_matherr s_signgam			\
 
 # Wrappers for these functions generated per type using a file named
 # <func>_template.c and the appropriate math-type-macros-<TYPE>.h.
-gen-libm-calls = cargF conjF cimagF crealF cabsF s_cacosF		  \
+gen-libm-calls = cargF conjF cimagF crealF cabsF e_scalbF s_cacosF	  \
 	         s_cacoshF s_ccosF s_ccoshF s_casinF s_csinF s_casinhF	  \
 		 k_casinhF s_csinhF k_casinhF s_csinhF s_catanhF s_catanF \
 		 s_ctanF s_ctanhF s_cexpF s_clogF s_cprojF s_csqrtF	  \
@@ -58,13 +58,14 @@  gen-libm-calls = cargF conjF cimagF crealF cabsF s_cacosF		  \
 		 w_log1pF w_scalblnF s_fmaxmagF s_fminmagF w_acosF	  \
 		 w_acoshF w_asinF w_atan2F w_atanhF w_coshF w_exp10F	  \
 		 w_exp2F w_fmodF w_hypotF w_j0F w_j1F w_jnF w_logF	  \
-		 w_log10F w_log2F w_powF w_remainderF w_sinhF w_sqrtF	  \
+		 w_log10F w_log2F w_powF w_remainderF w_scalbF		  \
+		 w_sinhF w_sqrtF					  \
 		 w_tgammaF w_lgammaF w_lgammaF_r w_expF e_exp2F
 
 libm-calls =								  \
 	e_acosF e_acoshF e_asinF e_atan2F e_atanhF e_coshF e_expF e_fmodF \
 	e_hypotF e_j0F e_j1F e_jnF e_lgammaF_r e_logF e_log10F e_powF	  \
-	e_remainderF e_scalbF e_sinhF e_sqrtF e_gammaF_r	  \
+	e_remainderF e_sinhF e_sqrtF e_gammaF_r				  \
 	e_ilogbF							  \
 	k_tanF s_asinhF s_atanF s_cbrtF					  \
 	s_ceilF s_cosF s_erfF s_expm1F s_fabsF				  \
diff --git a/math/e_scalb.c b/math/e_scalb_template.c
similarity index 64%
rename from math/e_scalb.c
rename to math/e_scalb_template.c
index dbe3b51d6a..ed0508c7bf 100644
--- a/math/e_scalb.c
+++ b/math/e_scalb_template.c
@@ -1,6 +1,8 @@ 
-/* Copyright (C) 2011-2020 Free Software Foundation, Inc.
+/* Multiply by integral power of radix.
+
+   Copyright (C) 2011-2020 Free Software Foundation, Inc.
+
    This file is part of the GNU C Library.
-   Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -20,35 +22,36 @@ 
 #include <math_private.h>
 #include <libm-alias-finite.h>
 
-static double
+static FLOAT
 __attribute__ ((noinline))
-invalid_fn (double x, double fn)
+invalid_fn (FLOAT x, FLOAT fn)
 {
-  if (rint (fn) != fn)
+  if (M_SUF (rint) (fn) != fn)
     return (fn - fn) / (fn - fn);
-  else if (fn > 65000.0)
-    return __scalbn (x, 65000);
+  else if (fn > M_LIT (65000.0))
+    return M_SUF (__scalbn) (x, 65000);
   else
-    return __scalbn (x,-65000);
+    return M_SUF (__scalbn) (x,-65000);
 }
 
 
-double
-__ieee754_scalb (double x, double fn)
+FLOAT
+M_DECL_FUNC (__ieee754_scalb) (FLOAT x, FLOAT fn)
 {
   if (__glibc_unlikely (isnan (x)))
     return x * fn;
   if (__glibc_unlikely (!isfinite (fn)))
     {
-      if (isnan (fn) || fn > 0.0)
+      if (isnan (fn) || fn > M_LIT (0.0))
 	return x * fn;
-      if (x == 0.0)
+      if (x == M_LIT (0.0))
 	return x;
       return x / -fn;
     }
-  if (__glibc_unlikely (fabs (fn) >= 0x1p31 || (double) (int) fn != fn))
+  if (__glibc_unlikely (M_FABS (fn) >= M_LIT (0x1p31)
+			|| (FLOAT) (int) fn != fn))
     return invalid_fn (x, fn);
 
-  return __scalbn (x, (int) fn);
+  return M_SCALBN (x, (int) fn);
 }
-libm_alias_finite (__ieee754_scalb, __scalb)
+declare_mgen_finite_alias (__ieee754_scalb, __scalb)
diff --git a/math/e_scalbf.c b/math/e_scalbf.c
deleted file mode 100644
index 944cfbefc9..0000000000
--- a/math/e_scalbf.c
+++ /dev/null
@@ -1,54 +0,0 @@ 
-/* Copyright (C) 2011-2020 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-   Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-#include <math.h>
-#include <math_private.h>
-#include <libm-alias-finite.h>
-
-static float
-__attribute__ ((noinline))
-invalid_fn (float x, float fn)
-{
-  if (rintf (fn) != fn)
-    return (fn - fn) / (fn - fn);
-  else if (fn > 65000.0f)
-    return __scalbnf (x, 65000);
-  else
-    return __scalbnf (x,-65000);
-}
-
-
-float
-__ieee754_scalbf (float x, float fn)
-{
-  if (__glibc_unlikely (isnan (x)))
-    return x * fn;
-  if (__glibc_unlikely (!isfinite (fn)))
-    {
-      if (isnan (fn) || fn > 0.0f)
-	return x * fn;
-      if (x == 0.0f)
-	return x;
-      return x / -fn;
-    }
-  if (__glibc_unlikely (fabsf (fn) >= 0x1p31f || (float) (int) fn != fn))
-    return invalid_fn (x, fn);
-
-  return __scalbnf (x, (int) fn);
-}
-libm_alias_finite (__ieee754_scalbf, __scalbf)
diff --git a/math/e_scalbl.c b/math/e_scalbl.c
deleted file mode 100644
index 6595ec6c27..0000000000
--- a/math/e_scalbl.c
+++ /dev/null
@@ -1,54 +0,0 @@ 
-/* Copyright (C) 2011-2020 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-   Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-#include <math.h>
-#include <math_private.h>
-#include <libm-alias-finite.h>
-
-static long double
-__attribute__ ((noinline))
-invalid_fn (long double x, long double fn)
-{
-  if (rintl (fn) != fn)
-    return (fn - fn) / (fn - fn);
-  else if (fn > 65000.0L)
-    return __scalbnl (x, 65000);
-  else
-    return __scalbnl (x,-65000);
-}
-
-
-long double
-__ieee754_scalbl (long double x, long double fn)
-{
-  if (__glibc_unlikely (isnan (x)))
-    return x * fn;
-  if (__glibc_unlikely (!isfinite (fn)))
-    {
-      if (isnan (fn) || fn > 0.0L)
-	return x * fn;
-      if (x == 0.0L)
-	return x;
-      return x / -fn;
-    }
-  if (__glibc_unlikely (fabsl (fn) >= 0x1p31L || (long double) (int) fn != fn))
-    return invalid_fn (x, fn);
-
-  return __scalbnl (x, (int) fn);
-}
-libm_alias_finite (__ieee754_scalbl, __scalbl)
diff --git a/math/w_scalb_compat.c b/math/w_scalb_compat.c
index c3dd3db4f7..664c0474a7 100644
--- a/math/w_scalb_compat.c
+++ b/math/w_scalb_compat.c
@@ -41,18 +41,15 @@  sysv_scalb (double x, double fn)
 
   return z;
 }
-#endif
 
 
 /* Wrapper scalb */
 double
 __scalb (double x, double fn)
 {
-#if LIBM_SVID_COMPAT
   if (__glibc_unlikely (_LIB_VERSION == _SVID_))
     return sysv_scalb (x, fn);
   else
-#endif
     {
       double z = __ieee754_scalb (x, fn);
 
@@ -79,7 +76,8 @@  __scalb (double x, double fn)
     }
 }
 weak_alias (__scalb, scalb)
-#ifdef NO_LONG_DOUBLE
+# ifdef NO_LONG_DOUBLE
 strong_alias (__scalb, __scalbl)
 weak_alias (__scalb, scalbl)
+# endif
 #endif
diff --git a/math/w_scalb_template.c b/math/w_scalb_template.c
new file mode 100644
index 0000000000..b2aafcdf36
--- /dev/null
+++ b/math/w_scalb_template.c
@@ -0,0 +1,59 @@ 
+/* Wrapper to set errno for scalb.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* Only build wrappers from the templates for the types that define the macro
+   below.  This macro is set in math-type-macros-<type>.h in sysdeps/generic
+   for each floating-point type.  */
+#if __USE_WRAPPER_TEMPLATE
+
+#include <errno.h>
+#include <math.h>
+#include <math_private.h>
+
+/* Wrapper scalb */
+FLOAT
+M_DECL_FUNC (__scalb) (FLOAT x, FLOAT fn)
+{
+    {
+      FLOAT z = M_SUF (__ieee754_scalb) (x, fn);
+
+      if (__glibc_unlikely (!isfinite (z) || z == M_LIT (0.0)))
+	{
+	  if (isnan (z))
+	    {
+	      if (!isnan (x) && !isnan (fn))
+		__set_errno (EDOM);
+	    }
+	  else if (isinf (z))
+	    {
+	      if (!isinf (x) && !isinf (fn))
+		__set_errno (ERANGE);
+	    }
+	  else
+	    {
+	      /* z == 0.  */
+	      if (x != M_LIT (0.0) && !isinf (fn))
+		__set_errno (ERANGE);
+	    }
+	}
+      return z;
+    }
+}
+declare_mgen_alias (__scalb, scalb);
+
+#endif /* __USE_WRAPPER_TEMPLATE.  */
diff --git a/math/w_scalbf_compat.c b/math/w_scalbf_compat.c
index 42bba44cc6..1b742e9b98 100644
--- a/math/w_scalbf_compat.c
+++ b/math/w_scalbf_compat.c
@@ -41,18 +41,15 @@  sysv_scalbf (float x, float fn)
 
   return z;
 }
-#endif
 
 
 /* Wrapper scalbf */
 float
 __scalbf (float x, float fn)
 {
-#if LIBM_SVID_COMPAT
   if (__glibc_unlikely (_LIB_VERSION == _SVID_))
     return sysv_scalbf (x, fn);
   else
-#endif
     {
       float z = __ieee754_scalbf (x, fn);
 
@@ -79,3 +76,4 @@  __scalbf (float x, float fn)
     }
 }
 weak_alias (__scalbf, scalbf)
+#endif
diff --git a/math/w_scalbl_compat.c b/math/w_scalbl_compat.c
index 8858c6b66d..c8ba3fddb4 100644
--- a/math/w_scalbl_compat.c
+++ b/math/w_scalbl_compat.c
@@ -41,18 +41,15 @@  sysv_scalbl (long double x, long double fn)
 
   return z;
 }
-#endif
 
 
 /* Wrapper scalbl */
 long double
 __scalbl (long double x, long double fn)
 {
-#if LIBM_SVID_COMPAT
   if (__glibc_unlikely (_LIB_VERSION == _SVID_))
     return sysv_scalbl (x, fn);
   else
-#endif
     {
       long double z = __ieee754_scalbl (x, fn);
 
@@ -79,3 +76,4 @@  __scalbl (long double x, long double fn)
     }
 }
 weak_alias (__scalbl, scalbl)
+#endif
diff --git a/sysdeps/ieee754/float128/w_scalbf128.c b/sysdeps/ieee754/float128/w_scalbf128.c
new file mode 100644
index 0000000000..067b724164
--- /dev/null
+++ b/sysdeps/ieee754/float128/w_scalbf128.c
@@ -0,0 +1 @@ 
+/* Not defined for _FloatN types.  */
diff --git a/sysdeps/ieee754/ldbl-opt/w_scalb_compat.c b/sysdeps/ieee754/ldbl-opt/w_scalb_compat.c
index f6d53a5ba5..56735e825e 100644
--- a/sysdeps/ieee754/ldbl-opt/w_scalb_compat.c
+++ b/sysdeps/ieee754/ldbl-opt/w_scalb_compat.c
@@ -1,5 +1,7 @@ 
 #include <math_ldbl_opt.h>
 #include <math/w_scalb_compat.c>
-#if LONG_DOUBLE_COMPAT(libm, GLIBC_2_0)
+#if LIBM_SVID_COMPAT
+# if LONG_DOUBLE_COMPAT(libm, GLIBC_2_0)
 compat_symbol (libm, __scalb, scalbl, GLIBC_2_0);
+# endif
 #endif
diff --git a/sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c b/sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c
index c8feb654a2..4d16f413c4 100644
--- a/sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c
+++ b/sysdeps/ieee754/ldbl-opt/w_scalbl_compat.c
@@ -2,4 +2,6 @@ 
 #undef weak_alias
 #define weak_alias(n,a)
 #include <math/w_scalbl_compat.c>
+#if LIBM_SVID_COMPAT
 long_double_symbol (libm, __scalbl, scalbl);
+#endif