root/lib/count-trailing-zeros.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. count_trailing_zeros_32
  2. count_trailing_zeros
  3. count_trailing_zeros_l
  4. count_trailing_zeros_ll

     1 /* count-trailing-zeros.h -- counts the number of trailing 0 bits in a word.
     2    Copyright 2013-2023 Free Software Foundation, Inc.
     3 
     4    This file is free software: you can redistribute it and/or modify
     5    it under the terms of the GNU Lesser General Public License as
     6    published by the Free Software Foundation; either version 2.1 of the
     7    License, or (at your option) any later version.
     8 
     9    This file is distributed in the hope that it will be useful,
    10    but WITHOUT ANY WARRANTY; without even the implied warranty of
    11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12    GNU Lesser General Public License for more details.
    13 
    14    You should have received a copy of the GNU Lesser General Public License
    15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
    16 
    17 /* Written by Paul Eggert.  */
    18 
    19 #ifndef COUNT_TRAILING_ZEROS_H
    20 #define COUNT_TRAILING_ZEROS_H 1
    21 
    22 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE.  */
    23 #if !_GL_CONFIG_H_INCLUDED
    24  #error "Please include config.h first."
    25 #endif
    26 
    27 #include <limits.h>
    28 #include <stdlib.h>
    29 
    30 _GL_INLINE_HEADER_BEGIN
    31 #ifndef COUNT_TRAILING_ZEROS_INLINE
    32 # define COUNT_TRAILING_ZEROS_INLINE _GL_INLINE
    33 #endif
    34 
    35 #ifdef __cplusplus
    36 extern "C" {
    37 #endif
    38 
    39 /* Assuming the GCC builtin is BUILTIN and the MSC builtin is MSC_BUILTIN,
    40    expand to code that computes the number of trailing zeros of the local
    41    variable 'x' of type TYPE (an unsigned integer type) and return it
    42    from the current function.  */
    43 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) \
    44     || (__clang_major__ >= 4)
    45 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE)               \
    46   return x ? BUILTIN (x) : CHAR_BIT * sizeof x;
    47 #elif _MSC_VER
    48 # pragma intrinsic (_BitScanForward)
    49 # if defined _M_X64
    50 #  pragma intrinsic (_BitScanForward64)
    51 # endif
    52 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE)               \
    53     do                                                                  \
    54       {                                                                 \
    55         unsigned long result;                                           \
    56         return MSC_BUILTIN (&result, x) ? result : CHAR_BIT * sizeof x; \
    57       }                                                                 \
    58     while (0)
    59 #else
    60 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE)               \
    61     do                                                                  \
    62       {                                                                 \
    63         int count = 0;                                                  \
    64         if (! x)                                                        \
    65           return CHAR_BIT * sizeof x;                                   \
    66         for (count = 0;                                                 \
    67              (count < CHAR_BIT * sizeof x - 32                          \
    68               && ! (x & 0xffffffffU));                                  \
    69              count += 32)                                               \
    70           x = x >> 31 >> 1;                                             \
    71         return count + count_trailing_zeros_32 (x);                     \
    72       }                                                                 \
    73     while (0)
    74 
    75 /* Compute and return the number of trailing zeros in the least
    76    significant 32 bits of X.  One of these bits must be nonzero.  */
    77 COUNT_TRAILING_ZEROS_INLINE int
    78 count_trailing_zeros_32 (unsigned int x)
    79 {
    80   /* <https://github.com/gibsjose/BitHacks>
    81      <https://www.fit.vutbr.cz/~ibarina/pub/bithacks.pdf> */
    82   static const char de_Bruijn_lookup[32] = {
    83     0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
    84     31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
    85   };
    86   return de_Bruijn_lookup[(((x & -x) * 0x077cb531U) & 0xffffffffU) >> 27];
    87 }
    88 #endif
    89 
    90 /* Compute and return the number of trailing zeros in X. */
    91 COUNT_TRAILING_ZEROS_INLINE int
    92 count_trailing_zeros (unsigned int x)
    93 {
    94   COUNT_TRAILING_ZEROS (__builtin_ctz, _BitScanForward, unsigned int);
    95 }
    96 
    97 /* Compute and return the number of trailing zeros in X. */
    98 COUNT_TRAILING_ZEROS_INLINE int
    99 count_trailing_zeros_l (unsigned long int x)
   100 {
   101   COUNT_TRAILING_ZEROS (__builtin_ctzl, _BitScanForward, unsigned long int);
   102 }
   103 
   104 /* Compute and return the number of trailing zeros in X. */
   105 COUNT_TRAILING_ZEROS_INLINE int
   106 count_trailing_zeros_ll (unsigned long long int x)
   107 {
   108 #if (defined _MSC_VER && !defined __clang__) && !defined _M_X64
   109   /* 32-bit MSVC does not have _BitScanForward64, only _BitScanForward.  */
   110   unsigned long result;
   111   if (_BitScanForward (&result, (unsigned long) x))
   112     return result;
   113   if (_BitScanForward (&result, (unsigned long) (x >> 32)))
   114     return result + 32;
   115   return CHAR_BIT * sizeof x;
   116 #else
   117   COUNT_TRAILING_ZEROS (__builtin_ctzll, _BitScanForward64,
   118                         unsigned long long int);
   119 #endif
   120 }
   121 
   122 #ifdef __cplusplus
   123 }
   124 #endif
   125 
   126 _GL_INLINE_HEADER_END
   127 
   128 #endif

/* [<][>][^][v][top][bottom][index][help] */