From 26ba820157bf3a6c5eee8cd57fff472d045c3d8c Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 16 May 2024 21:33:08 -0700 Subject: [PATCH] maint: prefer stdbit.h to count-leading-zeros.h is in C23 and should be more portable in the long run, now that Gnulib supports it. * bootstrap.conf (gnulib_modules): Remove count-leading-zeros. Add stdc_leading_zeros. * gl/lib/randperm.c, src/ioblksize.h: Include stdbit.h instead of count-leading-zeros.h. * gl/lib/randperm.c (floor_lg): Remove; no longer needed. (randperm_bound): Use stdc_bit_width instead of floor_lg; * gl/modules/randperm (Depends-on): Remove count-leading-zeros. Add stdc_bit_width. * src/ioblksize.h (io_blksize): Use stdc_leading_zeros_ull instead of count_leading_zeros_ll. --- bootstrap.conf | 2 +- gl/lib/randperm.c | 18 ++---------------- gl/modules/randperm | 2 +- src/ioblksize.h | 4 ++-- 4 files changed, 6 insertions(+), 20 deletions(-) diff --git a/bootstrap.conf b/bootstrap.conf index 126e1e80a5..43924c5b12 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -64,7 +64,6 @@ gnulib_modules=" config-h configmake copy-file-range - count-leading-zeros crypto/md5 crypto/sha1 crypto/sha256 @@ -247,6 +246,7 @@ gnulib_modules=" stat-size stat-time stdbool + stdc_leading_zeros stdckdint stdlib-safer stpcpy diff --git a/gl/lib/randperm.c b/gl/lib/randperm.c index 9903966e4d..50328cd9a5 100644 --- a/gl/lib/randperm.c +++ b/gl/lib/randperm.c @@ -22,28 +22,14 @@ #include "randperm.h" #include +#include #include #include #include "attribute.h" -#include "count-leading-zeros.h" #include "hash.h" #include "xalloc.h" -/* Return the floor of the log base 2 of N. If N is zero, return -1. */ - -ATTRIBUTE_CONST static int -floor_lg (size_t n) -{ - static_assert (SIZE_WIDTH <= ULLONG_WIDTH); - return (n == 0 ? -1 - : SIZE_WIDTH <= UINT_WIDTH - ? UINT_WIDTH - 1 - count_leading_zeros (n) - : SIZE_WIDTH <= ULONG_WIDTH - ? ULONG_WIDTH - 1 - count_leading_zeros_l (n) - : ULLONG_WIDTH - 1 - count_leading_zeros_ll (n)); -} - /* Return an upper bound on the number of random bytes needed to generate the first H elements of a random permutation of N elements. H must not exceed N. */ @@ -53,7 +39,7 @@ randperm_bound (size_t h, size_t n) { /* Upper bound on number of bits needed to generate the first number of the permutation. */ - uintmax_t lg_n = floor_lg (n) + 1; + uintmax_t lg_n = stdc_bit_width (n) + 1; /* Upper bound on number of bits needed to generated the first H elements. */ uintmax_t ar = lg_n * h; diff --git a/gl/modules/randperm b/gl/modules/randperm index 5bb5faefcc..ae54bf7ab6 100644 --- a/gl/modules/randperm +++ b/gl/modules/randperm @@ -7,8 +7,8 @@ lib/randperm.h Depends-on: assert-h -count-leading-zeros randint +stdc_bit_width stdint xalloc hash diff --git a/src/ioblksize.h b/src/ioblksize.h index dcf94fc600..d6aabebeb7 100644 --- a/src/ioblksize.h +++ b/src/ioblksize.h @@ -17,7 +17,7 @@ /* Include this file _after_ system headers if possible. */ /* sys/stat.h and minmax.h will already have been included by system.h. */ -#include "count-leading-zeros.h" +#include #include "stat-size.h" @@ -94,7 +94,7 @@ io_blksize (struct stat const *st) power of two when the reported blocksize is not a power of two. */ if (S_ISREG (st->st_mode) && blocksize & (blocksize - 1)) { - int leading_zeros = count_leading_zeros_ll (blocksize); + int leading_zeros = stdc_leading_zeros_ull (blocksize); if (IDX_MAX < ULLONG_MAX || leading_zeros) { unsigned long long power = 1ull << (ULLONG_WIDTH - leading_zeros); -- 2.47.2