errno-h: document Haiku errors can’t be -1
[gnulib.git] / lib / count-one-bits.h
blob43c10b3615ad0a9ec1448d11daafae8538ccadf5
1 /* count-one-bits.h -- counts the number of 1-bits in a word.
2 Copyright (C) 2007-2025 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Ben Pfaff. */
19 #ifndef COUNT_ONE_BITS_H
20 #define COUNT_ONE_BITS_H 1
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
27 #include <limits.h>
28 #include <stdlib.h>
30 _GL_INLINE_HEADER_BEGIN
31 #ifndef COUNT_ONE_BITS_INLINE
32 # define COUNT_ONE_BITS_INLINE _GL_INLINE
33 #endif
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
39 /* Assuming the GCC builtin is GCC_BUILTIN and the MSC builtin is MSC_BUILTIN,
40 expand to code that computes the number of 1-bits 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_ONE_BITS(GCC_BUILTIN, MSC_BUILTIN, TYPE) \
46 return GCC_BUILTIN (x)
47 #else
49 /* Compute and return the number of 1-bits set in the least
50 significant 32 bits of X. */
51 COUNT_ONE_BITS_INLINE int
52 count_one_bits_32 (unsigned int x)
54 x = ((x & 0xaaaaaaaaU) >> 1) + (x & 0x55555555U);
55 x = ((x & 0xccccccccU) >> 2) + (x & 0x33333333U);
56 x = (x >> 16) + (x & 0xffff);
57 x = ((x & 0xf0f0) >> 4) + (x & 0x0f0f);
58 return (x >> 8) + (x & 0x00ff);
61 /* Expand to code that computes the number of 1-bits of the local
62 variable 'x' of type TYPE (an unsigned integer type) and return it
63 from the current function. */
64 # define COUNT_ONE_BITS_GENERIC(TYPE) \
65 do \
66 { \
67 int count = 0; \
68 int bits; \
69 for (bits = 0; bits < sizeof (TYPE) * CHAR_BIT; bits += 32) \
70 { \
71 count += count_one_bits_32 (x); \
72 x = x >> 31 >> 1; \
73 } \
74 return count; \
75 } \
76 while (0)
78 # if 1500 <= _MSC_VER && (defined _M_IX86 || defined _M_X64)
80 /* While gcc falls back to its own generic code if the machine
81 on which it's running doesn't support popcount, with Microsoft's
82 compiler we need to detect and fallback ourselves. */
84 # if 0
85 # include <intrin.h>
86 # else
87 /* Don't pollute the namespace with too many MSVC intrinsics. */
88 extern void __cpuid (int[4], int);
89 # pragma intrinsic (__cpuid)
90 extern unsigned int __popcnt (unsigned int);
91 # pragma intrinsic (__popcnt)
92 # if defined _M_X64
93 extern unsigned long long __popcnt64 (unsigned long long);
94 # pragma intrinsic (__popcnt64)
95 # endif
96 # endif
98 # if !defined _M_X64
99 static inline __popcnt64 (unsigned long long x)
101 return __popcnt ((unsigned int) (x >> 32)) + __popcnt ((unsigned int) x);
103 # endif
105 /* Return nonzero if popcount is supported. */
107 /* 1 if supported, 0 if not supported, -1 if unknown. */
108 extern int popcount_support;
110 COUNT_ONE_BITS_INLINE int
111 popcount_supported (void)
113 if (popcount_support < 0)
115 /* Do as described in
116 <https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64> */
117 int cpu_info[4];
118 __cpuid (cpu_info, 1);
119 popcount_support = (cpu_info[2] >> 23) & 1;
121 return popcount_support;
124 # define COUNT_ONE_BITS(GCC_BUILTIN, MSC_BUILTIN, TYPE) \
125 do \
127 if (popcount_supported ()) \
128 return MSC_BUILTIN (x); \
129 else \
130 COUNT_ONE_BITS_GENERIC (TYPE); \
132 while (0)
134 # else
136 # define COUNT_ONE_BITS(GCC_BUILTIN, MSC_BUILTIN, TYPE) \
137 COUNT_ONE_BITS_GENERIC (TYPE)
139 # endif
140 #endif
142 /* Compute and return the number of 1-bits set in X. */
143 COUNT_ONE_BITS_INLINE int
144 count_one_bits (unsigned int x)
146 COUNT_ONE_BITS (__builtin_popcount, __popcnt, unsigned int);
149 /* Compute and return the number of 1-bits set in X. */
150 COUNT_ONE_BITS_INLINE int
151 count_one_bits_l (unsigned long int x)
153 COUNT_ONE_BITS (__builtin_popcountl, __popcnt, unsigned long int);
156 /* Compute and return the number of 1-bits set in X. */
157 COUNT_ONE_BITS_INLINE int
158 count_one_bits_ll (unsigned long long int x)
160 COUNT_ONE_BITS (__builtin_popcountll, __popcnt64, unsigned long long int);
163 #ifdef __cplusplus
165 #endif
167 _GL_INLINE_HEADER_END
169 #endif /* COUNT_ONE_BITS_H */