]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer stdbit.h to count-leading-zeros.h
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 17 May 2024 04:33:08 +0000 (21:33 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 17 May 2024 06:09:32 +0000 (23:09 -0700)
<stdbit.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
gl/lib/randperm.c
gl/modules/randperm
src/ioblksize.h

index 126e1e80a59fe1e7d2d3cf97da726b438720c766..43924c5b120f88c3ef3b16430f82dd8682395172 100644 (file)
@@ -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
index 9903966e4dd21c0a9f8127190742e151c766103e..50328cd9a5eb1734333bc94b324ada6808dd18ca 100644 (file)
 #include "randperm.h"
 
 #include <limits.h>
+#include <stdbit.h>
 #include <stdint.h>
 #include <stdlib.h>
 
 #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;
index 5bb5faefcca85c4a54c6dbf4e3fcbccaae34feac..ae54bf7ab6452a65452d1ef2922ccb65a96c96ca 100644 (file)
@@ -7,8 +7,8 @@ lib/randperm.h
 
 Depends-on:
 assert-h
-count-leading-zeros
 randint
+stdc_bit_width
 stdint
 xalloc
 hash
index dcf94fc6000251a05710fb8b61e6729c4e72cf32..d6aabebeb7015ebeec3e665a9f31c600e8745d77 100644 (file)
@@ -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 <stdbit.h>
 #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);