]> Savannah Git Hosting - gnulib.git/commitdiff
stdbit: tweak first_leading for GCC
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 May 2024 22:14:01 +0000 (15:14 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 May 2024 22:14:22 +0000 (15:14 -0700)
* lib/stdbit.in.h (stdc_first_leading_zero)
(stdc_first_leading_one, stdc_first_trailing_zero_uc)
(stdc_first_trailing_one_uc): Redo to avoid the need for a
conditional branch, at least on x86-64 with GCC 14.

ChangeLog
lib/stdbit.in.h

index f37d9f279649af241c1226402c46eb0340dd733f..049f27b3b8e74e354d0479e48762828f6f45ae02 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,11 @@
 2024-05-15  Paul Eggert  <eggert@cs.ucla.edu>
 
-       stdbit: tweak for non-GCC non-Clang
+       stdbit: tweak first_leading for GCC
+       * lib/stdbit.in.h (stdc_first_leading_zero)
+       (stdc_first_leading_one, stdc_first_trailing_zero_uc)
+       (stdc_first_trailing_one_uc): Redo to avoid the need for a
+       conditional branch, at least on x86-64 with GCC 14.
+
        * lib/stdbit.in.h (__gl_stdbit_clz, __gl_stdbit_clzl)
        (__gl_stdbit_clzll, __gl_stdbit_ctz, __gl_stdbit_ctzl)
        (__gl_stdbit_ctzll): Work even if the argument is zero.
index 984ef44ef920b9063cbf1503ef106e2015aed465..ab328a7793df94b8e5d6e845156a69d4a5aafd21 100644 (file)
@@ -494,35 +494,40 @@ _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_zero_uc (unsigned char n)
 {
   unsigned int count = stdc_leading_ones_uc (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_zero_us (unsigned short int n)
 {
   unsigned int count = stdc_leading_ones_us (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_zero_ui (unsigned int n)
 {
   unsigned int count = stdc_leading_ones_ui (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_zero_ul (unsigned long int n)
 {
   unsigned int count = stdc_leading_ones_ul (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_zero_ull (unsigned long long int n)
 {
   unsigned int count = stdc_leading_ones_ull (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 #define stdc_first_leading_zero(n) \
@@ -537,35 +542,40 @@ _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_one_uc (unsigned char n)
 {
   unsigned int count = stdc_leading_zeros_uc (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_one_us (unsigned short int n)
 {
   unsigned int count = stdc_leading_zeros_us (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_one_ui (unsigned int n)
 {
   unsigned int count = stdc_leading_zeros_ui (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_one_ul (unsigned long int n)
 {
   unsigned int count = stdc_leading_zeros_ul (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_leading_one_ull (unsigned long long int n)
 {
   unsigned int count = stdc_leading_zeros_ull (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 #define stdc_first_leading_one(n) \
@@ -580,35 +590,40 @@ _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_zero_uc (unsigned char n)
 {
   unsigned int count = stdc_trailing_ones_uc (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_zero_us (unsigned short int n)
 {
   unsigned int count = stdc_trailing_ones_us (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_zero_ui (unsigned int n)
 {
   unsigned int count = stdc_trailing_ones_ui (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_zero_ul (unsigned long int n)
 {
   unsigned int count = stdc_trailing_ones_ul (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_zero_ull (unsigned long long int n)
 {
   unsigned int count = stdc_trailing_ones_ull (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 #define stdc_first_trailing_zero(n) \
@@ -623,35 +638,40 @@ _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_one_uc (unsigned char n)
 {
   unsigned int count = stdc_trailing_zeros_uc (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_one_us (unsigned short int n)
 {
   unsigned int count = stdc_trailing_zeros_us (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_one_ui (unsigned int n)
 {
   unsigned int count = stdc_trailing_zeros_ui (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_one_ul (unsigned long int n)
 {
   unsigned int count = stdc_trailing_zeros_ul (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 _GL_STDBIT_INLINE unsigned int
 stdc_first_trailing_one_ull (unsigned long long int n)
 {
   unsigned int count = stdc_trailing_zeros_ull (n);
-  return count == 8 * sizeof n ? 0 : count + 1;
+  unsigned int bits = 8 * sizeof n;
+  return count % bits + (count < bits);
 }
 
 #define stdc_first_trailing_one(n) \