[PATCH v2 03/28] Replace count_leading_zeros with stdc_leading_zeros
H.J. Lu
hjl.tools@gmail.com
Tue Oct 28 20:30:50 GMT 2025
On Wed, Oct 29, 2025 at 1:12 AM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> Checked on x86_64-linux-gnu and aarch64-linux-gnu.
> ---
> stdlib/divmod_1.c | 9 +++------
> stdlib/mod_1.c | 9 +++------
> stdlib/strtod_l.c | 12 +++++-------
> sysdeps/ieee754/dbl-64/dbl2mpn.c | 5 +++--
> sysdeps/ieee754/ldbl-128/ldbl2mpn.c | 7 ++++---
> sysdeps/ieee754/ldbl-128ibm/ldbl2mpn.c | 7 ++++---
> sysdeps/ieee754/ldbl-96/ldbl2mpn.c | 5 +++--
> sysdeps/wordsize-32/divdi3.c | 7 ++++---
> sysdeps/x86/ldbl2mpn.c | 5 +++--
> 9 files changed, 32 insertions(+), 34 deletions(-)
>
> diff --git a/stdlib/divmod_1.c b/stdlib/divmod_1.c
> index f5030420fc..b91ab9e593 100644
> --- a/stdlib/divmod_1.c
> +++ b/stdlib/divmod_1.c
> @@ -25,6 +25,7 @@ along with the GNU MP Library; see the file COPYING.LIB. If not, see
> <https://www.gnu.org/licenses/>. */
>
> #include <gmp.h>
> +#include <stdbit.h>
> #include "gmp-impl.h"
> #include "longlong.h"
>
> @@ -62,9 +63,7 @@ mpn_divmod_1 (mp_ptr quot_ptr,
> if (UDIV_TIME > (2 * UMUL_TIME + 6)
> && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME)
> {
> - int normalization_steps;
> -
> - count_leading_zeros (normalization_steps, divisor_limb);
> + int normalization_steps = stdc_leading_zeros (divisor_limb);
> if (normalization_steps != 0)
> {
> mp_limb_t divisor_limb_inverted;
> @@ -144,9 +143,7 @@ mpn_divmod_1 (mp_ptr quot_ptr,
> {
> if (UDIV_NEEDS_NORMALIZATION)
> {
> - int normalization_steps;
> -
> - count_leading_zeros (normalization_steps, divisor_limb);
> + int normalization_steps = stdc_leading_zeros (divisor_limb);
> if (normalization_steps != 0)
> {
> divisor_limb <<= normalization_steps;
> diff --git a/stdlib/mod_1.c b/stdlib/mod_1.c
> index a4d81d4db0..74c1f6a521 100644
> --- a/stdlib/mod_1.c
> +++ b/stdlib/mod_1.c
> @@ -22,6 +22,7 @@ along with the GNU MP Library; see the file COPYING.LIB. If not, see
> <https://www.gnu.org/licenses/>. */
>
> #include <gmp.h>
> +#include <stdbit.h>
> #include "gmp-impl.h"
> #include "longlong.h"
>
> @@ -58,9 +59,7 @@ mpn_mod_1 (mp_srcptr dividend_ptr, mp_size_t dividend_size,
> if (UDIV_TIME > (2 * UMUL_TIME + 6)
> && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME)
> {
> - int normalization_steps;
> -
> - count_leading_zeros (normalization_steps, divisor_limb);
> + int normalization_steps = stdc_leading_zeros (divisor_limb);
> if (normalization_steps != 0)
> {
> mp_limb_t divisor_limb_inverted;
> @@ -137,9 +136,7 @@ mpn_mod_1 (mp_srcptr dividend_ptr, mp_size_t dividend_size,
> {
> if (UDIV_NEEDS_NORMALIZATION)
> {
> - int normalization_steps;
> -
> - count_leading_zeros (normalization_steps, divisor_limb);
> + int normalization_steps = stdc_leading_zeros (divisor_limb);
> if (normalization_steps != 0)
> {
> divisor_limb <<= normalization_steps;
> diff --git a/stdlib/strtod_l.c b/stdlib/strtod_l.c
> index f9572b9ebf..5814475e40 100644
> --- a/stdlib/strtod_l.c
> +++ b/stdlib/strtod_l.c
> @@ -72,6 +72,7 @@ extern double ____strtod_l_internal (const char *, char **, int, locale_t);
> #include <stdint.h>
> #include <rounding-mode.h>
> #include <tininess.h>
> +#include <stdbit.h>
>
> /* The gmp headers need some configuration frobs. */
> #define HAVE_ALLOCA 1
> @@ -1247,7 +1248,7 @@ ____STRTOF_INTERNAL (const STRING_TYPE *nptr, STRING_TYPE **endptr, int group,
> }
>
> /* Determine how many bits of the result we already have. */
> - count_leading_zeros (bits, num[numsize - 1]);
> + bits = stdc_leading_zeros (num[numsize - 1]);
> bits = numsize * BITS_PER_MP_LIMB - bits;
>
> /* Now we know the exponent of the number in base two.
> @@ -1465,7 +1466,8 @@ ____STRTOF_INTERNAL (const STRING_TYPE *nptr, STRING_TYPE **endptr, int group,
> |--- n ---|
> */
>
> - count_leading_zeros (cnt, den[densize - 1]);
> + cnt = stdc_leading_zeros (den[densize - 1]);
> +
>
> if (cnt > 0)
> {
> @@ -1504,11 +1506,7 @@ ____STRTOF_INTERNAL (const STRING_TYPE *nptr, STRING_TYPE **endptr, int group,
> #define got_limb \
> if (bits == 0) \
> { \
> - int cnt; \
> - if (quot == 0) \
> - cnt = BITS_PER_MP_LIMB; \
> - else \
> - count_leading_zeros (cnt, quot); \
> + int cnt = stdc_leading_zeros (quot); \
> exponent -= cnt; \
> if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
> { \
> diff --git a/sysdeps/ieee754/dbl-64/dbl2mpn.c b/sysdeps/ieee754/dbl-64/dbl2mpn.c
> index af369cd38a..d69973419b 100644
> --- a/sysdeps/ieee754/dbl-64/dbl2mpn.c
> +++ b/sysdeps/ieee754/dbl-64/dbl2mpn.c
> @@ -21,6 +21,7 @@
> #include <ieee754.h>
> #include <float.h>
> #include <stdlib.h>
> +#include <stdbit.h>
>
> /* Convert a `double' in IEEE754 standard double-precision format to a
> multi-precision integer representing the significand scaled up by its
> @@ -68,7 +69,7 @@ __mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
>
> if (res_ptr[N - 1] != 0)
> {
> - count_leading_zeros (cnt, res_ptr[N - 1]);
> + cnt = stdc_leading_zeros (res_ptr[N - 1]);
> cnt -= NUM_LEADING_ZEROS;
> #if N == 2
> res_ptr[N - 1] = res_ptr[1] << cnt
> @@ -82,7 +83,7 @@ __mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
> }
> else
> {
> - count_leading_zeros (cnt, res_ptr[0]);
> + cnt = stdc_leading_zeros (res_ptr[0]);
> if (cnt >= NUM_LEADING_ZEROS)
> {
> res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
> diff --git a/sysdeps/ieee754/ldbl-128/ldbl2mpn.c b/sysdeps/ieee754/ldbl-128/ldbl2mpn.c
> index 1179b1cd23..d3f3476d56 100644
> --- a/sysdeps/ieee754/ldbl-128/ldbl2mpn.c
> +++ b/sysdeps/ieee754/ldbl-128/ldbl2mpn.c
> @@ -23,6 +23,7 @@
> #include <math.h>
> #include <math_private.h>
> #include <stdlib.h>
> +#include <stdbit.h>
>
> /* Convert a `long double' in IEEE854 quad-precision format to a
> multi-precision integer representing the significand scaled up by its
> @@ -76,7 +77,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
> #if N == 2
> if (res_ptr[N - 1] != 0)
> {
> - count_leading_zeros (cnt, res_ptr[N - 1]);
> + cnt = stdc_leading_zeros (res_ptr[N - 1]);
> cnt -= NUM_LEADING_ZEROS;
> res_ptr[N - 1] = res_ptr[N - 1] << cnt
> | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
> @@ -85,7 +86,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
> }
> else
> {
> - count_leading_zeros (cnt, res_ptr[0]);
> + cnt = stdc_leading_zeros (res_ptr[0]);
> if (cnt >= NUM_LEADING_ZEROS)
> {
> res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
> @@ -106,7 +107,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
> if (res_ptr[j] != 0)
> break;
>
> - count_leading_zeros (cnt, res_ptr[j]);
> + cnt = stdc_leading_zeros (res_ptr[j]);
> cnt -= NUM_LEADING_ZEROS;
> l = N - 1 - j;
> if (cnt < 0)
> diff --git a/sysdeps/ieee754/ldbl-128ibm/ldbl2mpn.c b/sysdeps/ieee754/ldbl-128ibm/ldbl2mpn.c
> index 795d7b4598..d91699c3d0 100644
> --- a/sysdeps/ieee754/ldbl-128ibm/ldbl2mpn.c
> +++ b/sysdeps/ieee754/ldbl-128ibm/ldbl2mpn.c
> @@ -22,6 +22,7 @@
> #include <float.h>
> #include <math.h>
> #include <stdlib.h>
> +#include <stdbit.h>
>
> /* Convert a `long double' in IBM extended format to a multi-precision
> integer representing the significand scaled up by its number of
> @@ -133,7 +134,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
> #if N == 2
> if (res_ptr[N - 1] != 0)
> {
> - count_leading_zeros (cnt, res_ptr[N - 1]);
> + cnt = stdc_leading_zeros (res_ptr[N - 1]);
> cnt -= NUM_LEADING_ZEROS;
> res_ptr[N - 1] = res_ptr[N - 1] << cnt
> | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
> @@ -142,7 +143,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
> }
> else
> {
> - count_leading_zeros (cnt, res_ptr[0]);
> + cnt = stdc_leading_zeros (res_ptr[0]);
> if (cnt >= NUM_LEADING_ZEROS)
> {
> res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
> @@ -163,7 +164,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
> if (res_ptr[j] != 0)
> break;
>
> - count_leading_zeros (cnt, res_ptr[j]);
> + cnt = stdc_leading_zeros (res_ptr[j]);
> cnt -= NUM_LEADING_ZEROS;
> l = N - 1 - j;
> if (cnt < 0)
> diff --git a/sysdeps/ieee754/ldbl-96/ldbl2mpn.c b/sysdeps/ieee754/ldbl-96/ldbl2mpn.c
> index 07af9791f9..982f427380 100644
> --- a/sysdeps/ieee754/ldbl-96/ldbl2mpn.c
> +++ b/sysdeps/ieee754/ldbl-96/ldbl2mpn.c
> @@ -22,6 +22,7 @@
> #include <float.h>
> #include <math.h>
> #include <stdlib.h>
> +#include <stdbit.h>
>
> /* Convert a `long double' in IEEE854 standard double-precision format to a
> multi-precision integer representing the significand scaled up by its
> @@ -67,7 +68,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
>
> if (res_ptr[N - 1] != 0)
> {
> - count_leading_zeros (cnt, res_ptr[N - 1]);
> + cnt = stdc_leading_zeros (res_ptr[N - 1]);
> if (cnt != 0)
> {
> #if N == 2
> @@ -82,7 +83,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
> }
> else
> {
> - count_leading_zeros (cnt, res_ptr[0]);
> + cnt = stdc_leading_zeros (res_ptr[0]);
> res_ptr[N - 1] = res_ptr[0] << cnt;
> res_ptr[0] = 0;
> *expt = LDBL_MIN_EXP - 1 - BITS_PER_MP_LIMB - cnt;
> diff --git a/sysdeps/wordsize-32/divdi3.c b/sysdeps/wordsize-32/divdi3.c
> index 015494a14f..387022ab14 100644
> --- a/sysdeps/wordsize-32/divdi3.c
> +++ b/sysdeps/wordsize-32/divdi3.c
> @@ -19,6 +19,7 @@
> #include <endian.h>
> #include <stdlib.h>
> #include <bits/wordsize.h>
> +#include <stdbit.h>
>
> #if __WORDSIZE != 32
> #error This is for 32-bit targets only
> @@ -113,7 +114,7 @@ __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
> {
> /* 0q = nn / 0D */
>
> - count_leading_zeros (bm, d0);
> + bm = stdc_leading_zeros (d0);
>
> if (bm != 0)
> {
> @@ -137,7 +138,7 @@ __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
> if (d0 == 0)
> d0 = 1 / d0; /* Divide intentionally by zero. */
>
> - count_leading_zeros (bm, d0);
> + bm = stdc_leading_zeros (d0);
>
> if (bm == 0)
> {
> @@ -202,7 +203,7 @@ __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
> {
> /* 0q = NN / dd */
>
> - count_leading_zeros (bm, d1);
> + bm = stdc_leading_zeros (d1);
> if (bm == 0)
> {
> /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
> diff --git a/sysdeps/x86/ldbl2mpn.c b/sysdeps/x86/ldbl2mpn.c
> index d1dabab938..c41fb8da14 100644
> --- a/sysdeps/x86/ldbl2mpn.c
> +++ b/sysdeps/x86/ldbl2mpn.c
> @@ -21,6 +21,7 @@
> #include <ieee754.h>
> #include <float.h>
> #include <stdlib.h>
> +#include <stdbit.h>
>
> /* Convert a `long double' in IEEE854 standard double-precision format to a
> multi-precision integer representing the significand scaled up by its
> @@ -73,7 +74,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
>
> if (res_ptr[N - 1] != 0)
> {
> - count_leading_zeros (cnt, res_ptr[N - 1]);
> + cnt = stdc_leading_zeros (res_ptr[N - 1]);
> if (cnt != 0)
> {
> #if N == 2
> @@ -88,7 +89,7 @@ __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
> }
> else if (res_ptr[0] != 0)
> {
> - count_leading_zeros (cnt, res_ptr[0]);
> + cnt = stdc_leading_zeros (res_ptr[0]);
> res_ptr[N - 1] = res_ptr[0] << cnt;
> res_ptr[0] = 0;
> *expt = LDBL_MIN_EXP - 1 - BITS_PER_MP_LIMB - cnt;
> --
> 2.43.0
>
LGTM.
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
Thanks.
--
H.J.
More information about the Libc-alpha
mailing list