644f4cb23a13f777d999a98c3c7816c6e3b124b3
[gnulib.git] / lib / count-one-bits.h
1 /* count-one-bits.h -- counts the number of 1-bits in a word.
2    Copyright (C) 2007-2012 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 Ben Pfaff.  */
18
19 #ifndef COUNT_ONE_BITS_H
20 # define COUNT_ONE_BITS_H 1
21
22 #include <stdlib.h>
23 #include "verify.h"
24
25 _GL_INLINE_HEADER_BEGIN
26 #ifndef COUNT_ONE_BITS_INLINE
27 # define COUNT_ONE_BITS_INLINE _GL_INLINE
28 #endif
29
30 /* Expand the code which computes the number of 1-bits of the local
31    variable 'x' of type TYPE (an unsigned integer type) and returns it
32    from the current function.  */
33 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
34 #define COUNT_ONE_BITS(BUILTIN, TYPE)              \
35         return BUILTIN (x);
36 #else
37 #define COUNT_ONE_BITS(BUILTIN, TYPE)                                       \
38         /* This condition is written so as to avoid shifting by more than   \
39            31 bits at once, and also avoids a random HP-UX cc bug.  */      \
40         verify (((TYPE) -1 >> 31 >> 31 >> 2) == 0); /* TYPE has at most 64 bits */ \
41         int count = count_one_bits_32 (x);                                  \
42         if (1 < (TYPE) -1 >> 31) /* TYPE has more than 32 bits? */          \
43           count += count_one_bits_32 (x >> 31 >> 1);                        \
44         return count;
45
46 /* Compute and return the number of 1-bits set in the least
47    significant 32 bits of X. */
48 COUNT_ONE_BITS_INLINE int
49 count_one_bits_32 (unsigned int x)
50 {
51   x = ((x & 0xaaaaaaaaU) >> 1) + (x & 0x55555555U);
52   x = ((x & 0xccccccccU) >> 2) + (x & 0x33333333U);
53   x = (x >> 16) + (x & 0xffff);
54   x = ((x & 0xf0f0) >> 4) + (x & 0x0f0f);
55   return (x >> 8) + (x & 0x00ff);
56 }
57 #endif
58
59 /* Compute and return the number of 1-bits set in X. */
60 COUNT_ONE_BITS_INLINE int
61 count_one_bits (unsigned int x)
62 {
63   COUNT_ONE_BITS (__builtin_popcount, unsigned int);
64 }
65
66 /* Compute and return the number of 1-bits set in X. */
67 COUNT_ONE_BITS_INLINE int
68 count_one_bits_l (unsigned long int x)
69 {
70   COUNT_ONE_BITS (__builtin_popcountl, unsigned long int);
71 }
72
73 #if HAVE_UNSIGNED_LONG_LONG_INT
74 /* Compute and return the number of 1-bits set in X. */
75 COUNT_ONE_BITS_INLINE int
76 count_one_bits_ll (unsigned long long int x)
77 {
78   COUNT_ONE_BITS (__builtin_popcountll, unsigned long long int);
79 }
80 #endif
81
82 _GL_INLINE_HEADER_END
83
84 #endif /* COUNT_ONE_BITS_H */