New variable 'project-files-relative-names'
[emacs.git] / lib / count-one-bits.h
blob8d67f8718a4e29767ef71383e1d34aff85fa7aca
1 /* count-one-bits.h -- counts the number of 1-bits in a word.
2 Copyright (C) 2007-2024 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 # pragma intrinsic (__cpuid)
89 # pragma intrinsic (__popcnt)
90 # if defined _M_X64
91 # pragma intrinsic (__popcnt64)
92 # endif
93 # endif
95 # if !defined _M_X64
96 static inline __popcnt64 (unsigned long long x)
98 return __popcnt ((unsigned int) (x >> 32)) + __popcnt ((unsigned int) x);
100 # endif
102 /* Return nonzero if popcount is supported. */
104 /* 1 if supported, 0 if not supported, -1 if unknown. */
105 extern int popcount_support;
107 COUNT_ONE_BITS_INLINE int
108 popcount_supported (void)
110 if (popcount_support < 0)
112 /* Do as described in
113 <https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64> */
114 int cpu_info[4];
115 __cpuid (cpu_info, 1);
116 popcount_support = (cpu_info[2] >> 23) & 1;
118 return popcount_support;
121 # define COUNT_ONE_BITS(GCC_BUILTIN, MSC_BUILTIN, TYPE) \
122 do \
124 if (popcount_supported ()) \
125 return MSC_BUILTIN (x); \
126 else \
127 COUNT_ONE_BITS_GENERIC (TYPE); \
129 while (0)
131 # else
133 # define COUNT_ONE_BITS(GCC_BUILTIN, MSC_BUILTIN, TYPE) \
134 COUNT_ONE_BITS_GENERIC (TYPE)
136 # endif
137 #endif
139 /* Compute and return the number of 1-bits set in X. */
140 COUNT_ONE_BITS_INLINE int
141 count_one_bits (unsigned int x)
143 COUNT_ONE_BITS (__builtin_popcount, __popcnt, unsigned int);
146 /* Compute and return the number of 1-bits set in X. */
147 COUNT_ONE_BITS_INLINE int
148 count_one_bits_l (unsigned long int x)
150 COUNT_ONE_BITS (__builtin_popcountl, __popcnt, unsigned long int);
153 /* Compute and return the number of 1-bits set in X. */
154 COUNT_ONE_BITS_INLINE int
155 count_one_bits_ll (unsigned long long int x)
157 COUNT_ONE_BITS (__builtin_popcountll, __popcnt64, unsigned long long int);
160 #ifdef __cplusplus
162 #endif
164 _GL_INLINE_HEADER_END
166 #endif /* COUNT_ONE_BITS_H */