11 #ifndef HWLOC_PRIVATE_MISC_H
12 #define HWLOC_PRIVATE_MISC_H
14 #include <hwloc/autogen/config.h>
15 #include <private/autogen/config.h>
16 #include <private/private.h>
21 int hwloc_snprintf(
char *str,
size_t size,
const char *format, ...) __hwloc_attribute_format(printf, 3, 4);
26 int hwloc_namecoloncmp(const
char *haystack, const
char *needle,
size_t n);
29 #define HWLOC_BUILD_ASSERT(condition) ((void)sizeof(char[1 - 2*!(condition)]))
33 #define HWLOC_BITS_PER_LONG (HWLOC_SIZEOF_UNSIGNED_LONG * 8)
34 #define HWLOC_BITS_PER_INT (HWLOC_SIZEOF_UNSIGNED_INT * 8)
36 #if (HWLOC_BITS_PER_LONG != 32) && (HWLOC_BITS_PER_LONG != 64)
37 #error "unknown size for unsigned long."
40 #if (HWLOC_BITS_PER_INT != 16) && (HWLOC_BITS_PER_INT != 32) && (HWLOC_BITS_PER_INT != 64)
41 #error "unknown size for unsigned int."
49 #if defined(HWLOC_HAVE_BROKEN_FFS)
56 #elif defined(__GNUC__)
58 # if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))
60 # define hwloc_ffsl(x) __builtin_ffsl(x)
62 # define hwloc_ffs(x) __builtin_ffs(x)
63 # define HWLOC_NEED_FFSL
66 #elif defined(HWLOC_HAVE_FFSL)
68 # ifndef HWLOC_HAVE_DECL_FFSL
69 extern int ffsl(
long) __hwloc_attribute_const;
72 # define hwloc_ffsl(x) ffsl(x)
74 #elif defined(HWLOC_HAVE_FFS)
76 # ifndef HWLOC_HAVE_DECL_FFS
77 extern int ffs(
int) __hwloc_attribute_const;
80 # define hwloc_ffs(x) ffs(x)
81 # define HWLOC_NEED_FFSL
92 static __hwloc_inline
int __hwloc_attribute_const
93 hwloc_ffsl(
unsigned long x)
101 #if HWLOC_BITS_PER_LONG >= 64
102 if (!(x & 0xfffffffful)) {
107 if (!(x & 0xffffu)) {
131 #elif defined(HWLOC_NEED_FFSL)
136 static __hwloc_inline
int __hwloc_attribute_const
137 hwloc_ffs32(
unsigned long x)
139 #if HWLOC_BITS_PER_INT == 16
142 low_ffs = hwloc_ffs(x & 0xfffful);
146 hi_ffs = hwloc_ffs(x >> 16);
157 static __hwloc_inline
int __hwloc_attribute_const
158 hwloc_ffsl(
unsigned long x)
160 #if HWLOC_BITS_PER_LONG == 64
163 low_ffs = hwloc_ffs32(x & 0xfffffffful);
167 hi_ffs = hwloc_ffs32(x >> 32);
173 return hwloc_ffs32(x);
183 # if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))
184 # define hwloc_flsl(x) (x ? 8*sizeof(long) - __builtin_clzl(x) : 0)
186 # define hwloc_fls(x) (x ? 8*sizeof(int) - __builtin_clz(x) : 0)
187 # define HWLOC_NEED_FLSL
190 #elif defined(HWLOC_HAVE_FLSL)
192 # ifndef HWLOC_HAVE_DECL_FLSL
193 extern int flsl(
long) __hwloc_attribute_const;
196 # define hwloc_flsl(x) flsl(x)
198 #elif defined(HWLOC_HAVE_CLZL)
200 # ifndef HWLOC_HAVE_DECL_CLZL
201 extern int clzl(
long) __hwloc_attribute_const;
204 # define hwloc_flsl(x) (x ? 8*sizeof(long) - clzl(x) : 0)
206 #elif defined(HWLOC_HAVE_FLS)
208 # ifndef HWLOC_HAVE_DECL_FLS
209 extern int fls(
int) __hwloc_attribute_const;
212 # define hwloc_fls(x) fls(x)
213 # define HWLOC_NEED_FLSL
215 #elif defined(HWLOC_HAVE_CLZ)
217 # ifndef HWLOC_HAVE_DECL_CLZ
218 extern int clz(
int) __hwloc_attribute_const;
221 # define hwloc_fls(x) (x ? 8*sizeof(int) - clz(x) : 0)
222 # define HWLOC_NEED_FLSL
226 static __hwloc_inline
int __hwloc_attribute_const
227 hwloc_flsl(
unsigned long x)
235 #if HWLOC_BITS_PER_LONG >= 64
236 if ((x & 0xffffffff00000000ul)) {
241 if ((x & 0xffff0000u)) {
267 #ifdef HWLOC_NEED_FLSL
272 static __hwloc_inline
int __hwloc_attribute_const
273 hwloc_fls32(
unsigned long x)
275 #if HWLOC_BITS_PER_INT == 16
278 hi_fls = hwloc_fls(x >> 16);
282 low_fls = hwloc_fls(x & 0xfffful);
293 static __hwloc_inline
int __hwloc_attribute_const
294 hwloc_flsl(
unsigned long x)
296 #if HWLOC_BITS_PER_LONG == 64
299 hi_fls = hwloc_fls32(x >> 32);
303 low_fls = hwloc_fls32(x & 0xfffffffful);
309 return hwloc_fls32(x);
314 static __hwloc_inline
int __hwloc_attribute_const
315 hwloc_weight_long(
unsigned long w)
317 #if HWLOC_BITS_PER_LONG == 32
318 #if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__) >= 4)
319 return __builtin_popcount(w);
321 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
322 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
323 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
324 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
325 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
328 #if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__) >= 4)
329 return __builtin_popcountll(w);
332 res = (w & 0x5555555555555555ul) + ((w >> 1) & 0x5555555555555555ul);
333 res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
334 res = (res & 0x0F0F0F0F0F0F0F0Ful) + ((res >> 4) & 0x0F0F0F0F0F0F0F0Ful);
335 res = (res & 0x00FF00FF00FF00FFul) + ((res >> 8) & 0x00FF00FF00FF00FFul);
336 res = (res & 0x0000FFFF0000FFFFul) + ((res >> 16) & 0x0000FFFF0000FFFFul);
337 return (res & 0x00000000FFFFFFFFul) + ((res >> 32) & 0x00000000FFFFFFFFul);