New module 'count-trailing-zeros'.
[gnulib.git] / lib / count-trailing-zeros.h
1 /* count-trailing-zeros.h -- counts the number of trailing 0 bits in a word.
2    Copyright 2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program 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 General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://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 #include <limits.h>
23 #include <stdlib.h>
24
25 #ifndef _GL_INLINE_HEADER_BEGIN
26  #error "Please include config.h first."
27 #endif
28 _GL_INLINE_HEADER_BEGIN
29 #ifndef COUNT_TRAILING_ZEROS_INLINE
30 # define COUNT_TRAILING_ZEROS_INLINE _GL_INLINE
31 #endif
32
33 /* Assuming the GCC builtin is BUILTIN and the MSC builtin is MSC_BUILTIN,
34    expand to code that computes the number of trailing zeros of the local
35    variable 'x' of type TYPE (an unsigned integer type) and return it
36    from the current function.  */
37 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
38 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE)               \
39   return x ? BUILTIN (x) : CHAR_BIT * sizeof x;
40 #elif _MSC_VER
41 # pragma intrinsic _BitScanForward
42 # pragma intrinsic _BitScanForward64
43 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE)               \
44     do                                                                  \
45       {                                                                 \
46         unsigned long result;                                           \
47         return MSC_BUILTIN (&result, x) ? result : CHAR_BIT * sizeof x; \
48       }                                                                 \
49     while (0)
50 #else
51 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE)               \
52     do                                                                  \
53       {                                                                 \
54         int count = 0;                                                  \
55         if (! x)                                                        \
56           return CHAR_BIT * sizeof x;                                   \
57         for (count = 0;                                                 \
58              (count < CHAR_BIT * sizeof x - 32                          \
59               && ! (x & 0xffffffffU));                                  \
60              count += 32)                                               \
61           x = x >> 31 >> 1;                                             \
62         return count + count_trailing_zeros_32 (x);                     \
63       }                                                                 \
64     while (0)
65
66 /* Compute and return the number of trailing zeros in the least
67    significant 32 bits of X.  One of these bits must be nonzero.  */
68 COUNT_TRAILING_ZEROS_INLINE int
69 count_trailing_zeros_32 (unsigned int x)
70 {
71   /* http://graphics.stanford.edu/~seander/bithacks.html */
72   static const char de_Bruijn_lookup[32] = {
73     0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
74     31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
75   };
76   return de_Bruijn_lookup[(((x & -x) * 0x077cb531U) & 0xffffffffU) >> 27];
77 }
78 #endif
79
80 /* Compute and return the number of trailing zeros in X. */
81 COUNT_TRAILING_ZEROS_INLINE int
82 count_trailing_zeros (unsigned int x)
83 {
84   COUNT_TRAILING_ZEROS (__builtin_ctz, _BitScanForward, unsigned int);
85 }
86
87 /* Compute and return the number of trailing zeros in X. */
88 COUNT_TRAILING_ZEROS_INLINE int
89 count_trailing_zeros_l (unsigned long int x)
90 {
91   COUNT_TRAILING_ZEROS (__builtin_ctzl, _BitScanForward, unsigned long int);
92 }
93
94 #if HAVE_UNSIGNED_LONG_LONG_INT
95 /* Compute and return the number of trailing zeros in X. */
96 COUNT_TRAILING_ZEROS_INLINE int
97 count_trailing_zeros_ll (unsigned long long int x)
98 {
99   COUNT_TRAILING_ZEROS (__builtin_ctzll, _BitScanForward64,
100                         unsigned long long int);
101 }
102 #endif
103
104 _GL_INLINE_HEADER_END
105
106 #endif