PR middle-end/30262
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / builtin-bitops-1.c
blob8c3122cc6332beea5a4648844b31d93fa7f22b00
1 #include <limits.h>
2 #include <assert.h>
4 #if __INT_MAX__ > 2147483647L
5 # if __INT_MAX__ >= 9223372036854775807L
6 # define BITSIZEOF_INT 64
7 # else
8 # define BITSIZEOF_INT 32
9 # endif
10 #else
11 # if __INT_MAX__ >= 2147483647L
12 # define BITSIZEOF_INT 32
13 # else
14 # define BITSIZEOF_INT 16
15 # endif
16 #endif
18 #if __LONG_MAX__ > 2147483647L
19 # if __LONG_MAX__ >= 9223372036854775807L
20 # define BITSIZEOF_LONG 64
21 # else
22 # define BITSIZEOF_LONG 32
23 # endif
24 #else
25 # define BITSIZEOF_LONG 32
26 #endif
28 #if __LONG_LONG_MAX__ > 2147483647L
29 # if __LONG_LONG_MAX__ >= 9223372036854775807L
30 # define BITSIZEOF_LONG_LONG 64
31 # else
32 # define BITSIZEOF_LONG_LONG 32
33 # endif
34 #else
35 # define BITSIZEOF_LONG_LONG 32
36 #endif
38 #define MAKE_FUNS(suffix, type) \
39 int my_ffs##suffix(type x) { \
40 int i; \
41 if (x == 0) \
42 return 0; \
43 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
44 if (x & ((type) 1 << i)) \
45 break; \
46 return i + 1; \
47 } \
49 int my_ctz##suffix(type x) { \
50 int i; \
51 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
52 if (x & ((type) 1 << i)) \
53 break; \
54 return i; \
55 } \
57 int my_clz##suffix(type x) { \
58 int i; \
59 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
60 if (x & ((type) 1 << ((CHAR_BIT * sizeof (type)) - i - 1))) \
61 break; \
62 return i; \
63 } \
65 int my_popcount##suffix(type x) { \
66 int i; \
67 int count = 0; \
68 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
69 if (x & ((type) 1 << i)) \
70 count++; \
71 return count; \
72 } \
74 int my_parity##suffix(type x) { \
75 int i; \
76 int count = 0; \
77 for (i = 0; i < CHAR_BIT * sizeof (type); i++) \
78 if (x & ((type) 1 << i)) \
79 count++; \
80 return count & 1; \
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);
90 #define NUMS16 \
91 { \
92 0x0000U, \
93 0x0001U, \
94 0x8000U, \
95 0x0002U, \
96 0x4000U, \
97 0x0100U, \
98 0x0080U, \
99 0xa5a5U, \
100 0x5a5aU, \
101 0xcafeU, \
102 0xffffU \
105 #define NUMS32 \
107 0x00000000UL, \
108 0x00000001UL, \
109 0x80000000UL, \
110 0x00000002UL, \
111 0x40000000UL, \
112 0x00010000UL, \
113 0x00008000UL, \
114 0xa5a5a5a5UL, \
115 0x5a5a5a5aUL, \
116 0xcafe0000UL, \
117 0x00cafe00UL, \
118 0x0000cafeUL, \
119 0xffffffffUL \
122 #define NUMS64 \
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
141 NUMS64;
142 #elif BITSIZEOF_INT == 32
143 NUMS32;
144 #else
145 NUMS16;
146 #endif
148 unsigned long longs[] =
149 #if BITSIZEOF_LONG == 64
150 NUMS64;
151 #else
152 NUMS32;
153 #endif
155 unsigned long long longlongs[] =
156 #if BITSIZEOF_LONG_LONG == 64
157 NUMS64;
158 #else
159 NUMS32;
160 #endif
162 #define N(table) (sizeof (table) / sizeof (table[0]))
165 main (void)
167 int i;
169 for (i = 0; i < N(ints); i++)
171 if (__builtin_ffs (ints[i]) != my_ffs (ints[i]))
172 abort ();
173 if (ints[i] != 0
174 && __builtin_clz (ints[i]) != my_clz (ints[i]))
175 abort ();
176 if (ints[i] != 0
177 && __builtin_ctz (ints[i]) != my_ctz (ints[i]))
178 abort ();
179 if (__builtin_popcount (ints[i]) != my_popcount (ints[i]))
180 abort ();
181 if (__builtin_parity (ints[i]) != my_parity (ints[i]))
182 abort ();
185 for (i = 0; i < N(longs); i++)
187 if (__builtin_ffsl (longs[i]) != my_ffsl (longs[i]))
188 abort ();
189 if (longs[i] != 0
190 && __builtin_clzl (longs[i]) != my_clzl (longs[i]))
191 abort ();
192 if (longs[i] != 0
193 && __builtin_ctzl (longs[i]) != my_ctzl (longs[i]))
194 abort ();
195 if (__builtin_popcountl (longs[i]) != my_popcountl (longs[i]))
196 abort ();
197 if (__builtin_parityl (longs[i]) != my_parityl (longs[i]))
198 abort ();
201 for (i = 0; i < N(longlongs); i++)
203 if (__builtin_ffsll (longlongs[i]) != my_ffsll (longlongs[i]))
204 abort ();
205 if (longlongs[i] != 0
206 && __builtin_clzll (longlongs[i]) != my_clzll (longlongs[i]))
207 abort ();
208 if (longlongs[i] != 0
209 && __builtin_ctzll (longlongs[i]) != my_ctzll (longlongs[i]))
210 abort ();
211 if (__builtin_popcountll (longlongs[i]) != my_popcountll (longlongs[i]))
212 abort ();
213 if (__builtin_parityll (longlongs[i]) != my_parityll (longlongs[i]))
214 abort ();
217 /* Test constant folding. */
219 #define TEST(x, suffix) \
220 if (__builtin_ffs##suffix (x) != my_ffs##suffix (x)) \
221 abort (); \
222 if (x != 0 && __builtin_clz##suffix (x) != my_clz##suffix (x)) \
223 abort (); \
224 if (x != 0 && __builtin_ctz##suffix (x) != my_ctz##suffix (x)) \
225 abort (); \
226 if (__builtin_popcount##suffix (x) != my_popcount##suffix (x)) \
227 abort (); \
228 if (__builtin_parity##suffix (x) != my_parity##suffix (x)) \
229 abort ();
231 #if BITSIZEOF_INT == 32
232 TEST(0x00000000UL,);
233 TEST(0x00000001UL,);
234 TEST(0x80000000UL,);
235 TEST(0x40000000UL,);
236 TEST(0x00010000UL,);
237 TEST(0x00008000UL,);
238 TEST(0xa5a5a5a5UL,);
239 TEST(0x5a5a5a5aUL,);
240 TEST(0xcafe0000UL,);
241 TEST(0x00cafe00UL,);
242 TEST(0x0000cafeUL,);
243 TEST(0xffffffffUL,);
244 #endif
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);
260 #endif
262 exit (0);