4 #if __INT_MAX__ > 2147483647L
5 # if __INT_MAX__ >= 9223372036854775807L
6 # define BITSIZEOF_INT 64
8 # define BITSIZEOF_INT 32
11 # if __INT_MAX__ >= 2147483647L
12 # define BITSIZEOF_INT 32
14 # define BITSIZEOF_INT 16
18 #if __LONG_MAX__ > 2147483647L
19 # if __LONG_MAX__ >= 9223372036854775807L
20 # define BITSIZEOF_LONG 64
22 # define BITSIZEOF_LONG 32
25 # define BITSIZEOF_LONG 32
28 #if __LONG_LONG_MAX__ > 2147483647L
29 # if __LONG_LONG_MAX__ >= 9223372036854775807L
30 # define BITSIZEOF_LONG_LONG 64
32 # define BITSIZEOF_LONG_LONG 32
35 # define BITSIZEOF_LONG_LONG 32
38 #define MAKE_FUNS(suffix, type) \
39 int my_ffs##suffix(type x) { \
43 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
44 if (x & ((type) 1 << i)) \
49 int my_ctz##suffix(type x) { \
51 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
52 if (x & ((type) 1 << i)) \
57 int my_clz##suffix(type x) { \
59 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
60 if (x & ((type) 1 << ((CHAR_BIT * sizeof (type)) - i - 1))) \
65 int my_popcount##suffix(type x) { \
68 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
69 if (x & ((type) 1 << i)) \
74 int my_parity##suffix(type x) { \
77 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
78 if (x & ((type) 1 << i)) \
83 MAKE_FUNS (, unsigned);
84 MAKE_FUNS (l
, unsigned long);
85 MAKE_FUNS (ll
, unsigned long long);
87 extern void abort (void);
88 extern void exit (int);
124 0x0000000000000000ULL, \
125 0x0000000000000001ULL, \
126 0x8000000000000000ULL, \
127 0x0000000000000002ULL, \
128 0x4000000000000000ULL, \
129 0x0000000100000000ULL, \
130 0x0000000080000000ULL, \
131 0xa5a5a5a5a5a5a5a5ULL, \
132 0x5a5a5a5a5a5a5a5aULL, \
133 0xcafecafe00000000ULL, \
134 0x0000cafecafe0000ULL, \
135 0x00000000cafecafeULL, \
136 0xffffffffffffffffULL \
139 unsigned int ints
[] =
140 #if BITSIZEOF_INT == 64
142 #elif BITSIZEOF_INT == 32
148 unsigned long longs
[] =
149 #if BITSIZEOF_LONG == 64
155 unsigned long long longlongs
[] =
156 #if BITSIZEOF_LONG_LONG == 64
162 #define N(table) (sizeof (table) / sizeof (table[0]))
169 for (i
= 0; i
< N(ints
); i
++)
171 if (__builtin_ffs (ints
[i
]) != my_ffs (ints
[i
]))
174 && __builtin_clz (ints
[i
]) != my_clz (ints
[i
]))
177 && __builtin_ctz (ints
[i
]) != my_ctz (ints
[i
]))
179 if (__builtin_popcount (ints
[i
]) != my_popcount (ints
[i
]))
181 if (__builtin_parity (ints
[i
]) != my_parity (ints
[i
]))
185 for (i
= 0; i
< N(longs
); i
++)
187 if (__builtin_ffsl (longs
[i
]) != my_ffsl (longs
[i
]))
190 && __builtin_clzl (longs
[i
]) != my_clzl (longs
[i
]))
193 && __builtin_ctzl (longs
[i
]) != my_ctzl (longs
[i
]))
195 if (__builtin_popcountl (longs
[i
]) != my_popcountl (longs
[i
]))
197 if (__builtin_parityl (longs
[i
]) != my_parityl (longs
[i
]))
201 for (i
= 0; i
< N(longlongs
); i
++)
203 if (__builtin_ffsll (longlongs
[i
]) != my_ffsll (longlongs
[i
]))
205 if (longlongs
[i
] != 0
206 && __builtin_clzll (longlongs
[i
]) != my_clzll (longlongs
[i
]))
208 if (longlongs
[i
] != 0
209 && __builtin_ctzll (longlongs
[i
]) != my_ctzll (longlongs
[i
]))
211 if (__builtin_popcountll (longlongs
[i
]) != my_popcountll (longlongs
[i
]))
213 if (__builtin_parityll (longlongs
[i
]) != my_parityll (longlongs
[i
]))
217 /* Test constant folding. */
219 #define TEST(x, suffix) \
220 if (__builtin_ffs##suffix (x) != my_ffs##suffix (x)) \
222 if (x != 0 && __builtin_clz##suffix (x) != my_clz##suffix (x)) \
224 if (x != 0 && __builtin_ctz##suffix (x) != my_ctz##suffix (x)) \
226 if (__builtin_popcount##suffix (x) != my_popcount##suffix (x)) \
228 if (__builtin_parity##suffix (x) != my_parity##suffix (x)) \
231 #if BITSIZEOF_INT == 32
246 #if BITSIZEOF_LONG_LONG == 64
247 TEST(0x0000000000000000ULL
, ll
);
248 TEST(0x0000000000000001ULL
, ll
);
249 TEST(0x8000000000000000ULL
, ll
);
250 TEST(0x0000000000000002ULL
, ll
);
251 TEST(0x4000000000000000ULL
, ll
);
252 TEST(0x0000000100000000ULL
, ll
);
253 TEST(0x0000000080000000ULL
, ll
);
254 TEST(0xa5a5a5a5a5a5a5a5ULL
, ll
);
255 TEST(0x5a5a5a5a5a5a5a5aULL
, ll
);
256 TEST(0xcafecafe00000000ULL
, ll
);
257 TEST(0x0000cafecafe0000ULL
, ll
);
258 TEST(0x00000000cafecafeULL
, ll
);
259 TEST(0xffffffffffffffffULL
, ll
);