1 /* Test for binary integer constants. */
2
3 /* Derived from: c99-intconst-1.c, bye Joseph Myers <jsm28@cam.ac.uk>. */
4 /* Origin: Joerg Wunsch <j.gnu@uriah.heep.sax.de>. */
5 /* { dg-do compile } */
6 /* { dg-options "-std=gnu99" } */
7
8 #include <limits.h>
9
10 /* Assertion that constant C is of type T. */
11 #define ASSERT_CONST_TYPE(C, T) \
12 do { \
13 typedef T type; \
14 typedef type **typepp; \
15 typedef __typeof__((C)) ctype; \
16 typedef ctype **ctypepp; \
17 typepp x = 0; \
18 ctypepp y = 0; \
19 x = y; \
20 y = x; \
21 } while (0)
22
23 /* (T *) if E is zero, (void *) otherwise. */
24 #define type_if_not(T, E) __typeof__(0 ? (T *)0 : (void *)(E))
25
26 /* (T *) if E is nonzero, (void *) otherwise. */
27 #define type_if(T, E) type_if_not(T, !(E))
28
29 /* Combine pointer types, all but one (void *). */
30 #define type_comb2(T1, T2) __typeof__(0 ? (T1)0 : (T2)0)
31 #define type_comb3(T1, T2, T3) type_comb2(T1, type_comb2(T2, T3))
32 #define type_comb4(T1, T2, T3, T4) \
33 type_comb2(T1, type_comb2(T2, type_comb2(T3, T4)))
34 #define type_comb6(T1, T2, T3, T4, T5, T6) \
35 type_comb2(T1, \
36 type_comb2(T2, \
37 type_comb2(T3, \
38 type_comb2(T4, \
39 type_comb2(T5, T6)))))
40
41 /* (T1 *) if E1, otherwise (T2 *) if E2. */
42 #define first_of2p(T1, E1, T2, E2) type_comb2(type_if(T1, (E1)), \
43 type_if(T2, (!(E1) && (E2))))
44 /* (T1 *) if E1, otherwise (T2 *) if E2, otherwise (T3 *) if E3. */
45 #define first_of3p(T1, E1, T2, E2, T3, E3) \
46 type_comb3(type_if(T1, (E1)), \
47 type_if(T2, (!(E1) && (E2))), \
48 type_if(T3, (!(E1) && !(E2) && (E3))))
49 /* (T1 *) if E1, otherwise (T2 *) if E2, otherwise (T3 *) if E3, otherwise
50 (T4 *) if E4. */
51 #define first_of4p(T1, E1, T2, E2, T3, E3, T4, E4) \
52 type_comb4(type_if(T1, (E1)), \
53 type_if(T2, (!(E1) && (E2))), \
54 type_if(T3, (!(E1) && !(E2) && (E3))), \
55 type_if(T4, (!(E1) && !(E2) && !(E3) && (E4))))
56 /* (T1 *) if E1, otherwise (T2 *) if E2, otherwise (T3 *) if E3, otherwise
57 (T4 *) if E4, otherwise (T5 *) if E5, otherwise (T6 *) if E6. */
58 #define first_of6p(T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6) \
59 type_comb6(type_if(T1, (E1)), \
60 type_if(T2, (!(E1) && (E2))), \
61 type_if(T3, (!(E1) && !(E2) && (E3))), \
62 type_if(T4, (!(E1) && !(E2) && !(E3) && (E4))), \
63 type_if(T5, (!(E1) && !(E2) && !(E3) && !(E4) && (E5))), \
64 type_if(T6, (!(E1) && !(E2) && !(E3) \
65 && !(E4) && !(E5) && (E6))))
66
67 /* Likewise, but return the original type rather than a pointer type. */
68 #define first_of2(T1, E1, T2, E2) \
69 __typeof__(*((first_of2p(T1, (E1), T2, (E2)))0))
70 #define first_of3(T1, E1, T2, E2, T3, E3) \
71 __typeof__(*((first_of3p(T1, (E1), T2, (E2), T3, (E3)))0))
72 #define first_of4(T1, E1, T2, E2, T3, E3, T4, E4) \
73 __typeof__(*((first_of4p(T1, (E1), T2, (E2), T3, (E3), T4, (E4)))0))
74 #define first_of6(T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6) \
75 __typeof__(*((first_of6p(T1, (E1), T2, (E2), T3, (E3), \
76 T4, (E4), T5, (E5), T6, (E6)))0))
77
78 /* Types of constants according to the C99 rules. */
79 #define C99_UNSUF_TYPE(C) \
80 first_of6(int, (C) <= INT_MAX, \
81 unsigned int, (C) <= UINT_MAX, \
82 long int, (C) <= LONG_MAX, \
83 unsigned long int, (C) <= ULONG_MAX, \
84 long long int, (C) <= LLONG_MAX, \
85 unsigned long long int, (C) <= ULLONG_MAX)
86 #define C99_SUFu_TYPE(C) \
87 first_of3(unsigned int, (C) <= UINT_MAX, \
88 unsigned long int, (C) <= ULONG_MAX, \
89 unsigned long long int, (C) <= ULLONG_MAX)
90 #define C99_SUFl_TYPE(C) \
91 first_of4(long int, (C) <= LONG_MAX, \
92 unsigned long int, (C) <= ULONG_MAX, \
93 long long int, (C) <= LLONG_MAX, \
94 unsigned long long int, (C) <= ULLONG_MAX)
95 #define C99_SUFul_TYPE(C) \
96 first_of2(unsigned long int, (C) <= ULONG_MAX, \
97 unsigned long long int, (C) <= ULLONG_MAX)
98 #define C99_SUFll_TYPE(C) \
99 first_of2(long long int, (C) <= LLONG_MAX, \
100 unsigned long long int, (C) <= ULLONG_MAX)
101
102 /* Checks that constants have correct type. */
103 #define CHECK_UNSUF_TYPE(C) \
104 ASSERT_CONST_TYPE((C), C99_UNSUF_TYPE((C)))
105 #define CHECK_SUFu_TYPE(C) ASSERT_CONST_TYPE((C), C99_SUFu_TYPE((C)))
106 #define CHECK_SUFl_TYPE(C) \
107 ASSERT_CONST_TYPE((C), C99_SUFl_TYPE((C)))
108 #define CHECK_SUFul_TYPE(C) ASSERT_CONST_TYPE((C), C99_SUFul_TYPE((C)))
109 #define CHECK_SUFll_TYPE(C) \
110 ASSERT_CONST_TYPE((C), C99_SUFll_TYPE((C)))
111 #define CHECK_SUFull_TYPE(C) ASSERT_CONST_TYPE((C), unsigned long long int)
112
113 /* Check an octal or hexadecimal value, with all suffixes. */
114 #define CHECK_CONST(C) \
115 CHECK_UNSUF_TYPE(C); \
116 CHECK_SUFu_TYPE(C##u); \
117 CHECK_SUFu_TYPE(C##U); \
118 CHECK_SUFl_TYPE(C##l); \
119 CHECK_SUFl_TYPE(C##L); \
120 CHECK_SUFul_TYPE(C##ul); \
121 CHECK_SUFul_TYPE(C##uL); \
122 CHECK_SUFul_TYPE(C##Ul); \
123 CHECK_SUFul_TYPE(C##UL); \
124 CHECK_SUFll_TYPE(C##ll); \
125 CHECK_SUFll_TYPE(C##LL); \
126 CHECK_SUFull_TYPE(C##ull); \
127 CHECK_SUFull_TYPE(C##uLL); \
128 CHECK_SUFull_TYPE(C##Ull); \
129 CHECK_SUFull_TYPE(C##ULL);
130
131 #define CHECK_BIN_CONST(C) \
132 CHECK_CONST(0b##C); \
133 CHECK_CONST(0B##C);
134
135 /* True iff "long long" is at least B bits. This presumes that (B-2)/3 is at
136 most 63. */
137 #define LLONG_AT_LEAST(B) \
138 (LLONG_MAX >> ((B)-2)/3 >> ((B)-2)/3 \
139 >> ((B)-2 - ((B)-2)/3 - ((B)-2)/3))
140
141 #define LLONG_HAS_BITS(B) (LLONG_AT_LEAST((B)) && !LLONG_AT_LEAST((B) + 1))
142
143 #define FOO 0b1101
144 #if !FOO
145 # error "preprocessor does not accept binary constants"
146 #endif
147
148 void
foo(void)149 foo (void)
150 {
151 /* Check all 2^n and 2^n - 1 up to 2^72 - 1. */
152 CHECK_BIN_CONST(1);
153 CHECK_BIN_CONST(10);
154 CHECK_BIN_CONST(11);
155 CHECK_BIN_CONST(100);
156 CHECK_BIN_CONST(111);
157 CHECK_BIN_CONST(1000);
158 CHECK_BIN_CONST(1111);
159 CHECK_BIN_CONST(10000);
160 CHECK_BIN_CONST(11111);
161 CHECK_BIN_CONST(100000);
162 CHECK_BIN_CONST(111111);
163 CHECK_BIN_CONST(1000000);
164 CHECK_BIN_CONST(1111111);
165 CHECK_BIN_CONST(10000000);
166 CHECK_BIN_CONST(11111111);
167 CHECK_BIN_CONST(100000000);
168 CHECK_BIN_CONST(111111111);
169 CHECK_BIN_CONST(1000000000);
170 CHECK_BIN_CONST(1111111111);
171 CHECK_BIN_CONST(10000000000);
172 CHECK_BIN_CONST(11111111111);
173 CHECK_BIN_CONST(100000000000);
174 CHECK_BIN_CONST(111111111111);
175 CHECK_BIN_CONST(1000000000000);
176 CHECK_BIN_CONST(1111111111111);
177 CHECK_BIN_CONST(10000000000000);
178 CHECK_BIN_CONST(11111111111111);
179 CHECK_BIN_CONST(100000000000000);
180 CHECK_BIN_CONST(111111111111111);
181 CHECK_BIN_CONST(1000000000000000);
182 CHECK_BIN_CONST(1111111111111111);
183 CHECK_BIN_CONST(10000000000000000);
184 CHECK_BIN_CONST(11111111111111111);
185 CHECK_BIN_CONST(100000000000000000);
186 CHECK_BIN_CONST(111111111111111111);
187 CHECK_BIN_CONST(1000000000000000000);
188 CHECK_BIN_CONST(1111111111111111111);
189 CHECK_BIN_CONST(10000000000000000000);
190 CHECK_BIN_CONST(11111111111111111111);
191 CHECK_BIN_CONST(100000000000000000000);
192 CHECK_BIN_CONST(111111111111111111111);
193 CHECK_BIN_CONST(1000000000000000000000);
194 CHECK_BIN_CONST(1111111111111111111111);
195 CHECK_BIN_CONST(10000000000000000000000);
196 CHECK_BIN_CONST(11111111111111111111111);
197 CHECK_BIN_CONST(100000000000000000000000);
198 CHECK_BIN_CONST(111111111111111111111111);
199 CHECK_BIN_CONST(1000000000000000000000000);
200 CHECK_BIN_CONST(1111111111111111111111111);
201 CHECK_BIN_CONST(10000000000000000000000000);
202 CHECK_BIN_CONST(11111111111111111111111111);
203 CHECK_BIN_CONST(100000000000000000000000000);
204 CHECK_BIN_CONST(111111111111111111111111111);
205 CHECK_BIN_CONST(1000000000000000000000000000);
206 CHECK_BIN_CONST(1111111111111111111111111111);
207 CHECK_BIN_CONST(10000000000000000000000000000);
208 CHECK_BIN_CONST(11111111111111111111111111111);
209 CHECK_BIN_CONST(100000000000000000000000000000);
210 CHECK_BIN_CONST(111111111111111111111111111111);
211 CHECK_BIN_CONST(1000000000000000000000000000000);
212 CHECK_BIN_CONST(1111111111111111111111111111111);
213 CHECK_BIN_CONST(10000000000000000000000000000000);
214 CHECK_BIN_CONST(11111111111111111111111111111111);
215 CHECK_BIN_CONST(100000000000000000000000000000000);
216 CHECK_BIN_CONST(111111111111111111111111111111111);
217 CHECK_BIN_CONST(1000000000000000000000000000000000);
218 CHECK_BIN_CONST(1111111111111111111111111111111111);
219 CHECK_BIN_CONST(10000000000000000000000000000000000);
220 CHECK_BIN_CONST(11111111111111111111111111111111111);
221 CHECK_BIN_CONST(100000000000000000000000000000000000);
222 CHECK_BIN_CONST(111111111111111111111111111111111111);
223 CHECK_BIN_CONST(1000000000000000000000000000000000000);
224 CHECK_BIN_CONST(1111111111111111111111111111111111111);
225 CHECK_BIN_CONST(10000000000000000000000000000000000000);
226 CHECK_BIN_CONST(11111111111111111111111111111111111111);
227 CHECK_BIN_CONST(100000000000000000000000000000000000000);
228 CHECK_BIN_CONST(111111111111111111111111111111111111111);
229 CHECK_BIN_CONST(1000000000000000000000000000000000000000);
230 CHECK_BIN_CONST(1111111111111111111111111111111111111111);
231 CHECK_BIN_CONST(10000000000000000000000000000000000000000);
232 CHECK_BIN_CONST(11111111111111111111111111111111111111111);
233 CHECK_BIN_CONST(100000000000000000000000000000000000000000);
234 CHECK_BIN_CONST(111111111111111111111111111111111111111111);
235 CHECK_BIN_CONST(1000000000000000000000000000000000000000000);
236 CHECK_BIN_CONST(1111111111111111111111111111111111111111111);
237 CHECK_BIN_CONST(10000000000000000000000000000000000000000000);
238 CHECK_BIN_CONST(11111111111111111111111111111111111111111111);
239 CHECK_BIN_CONST(100000000000000000000000000000000000000000000);
240 CHECK_BIN_CONST(111111111111111111111111111111111111111111111);
241 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000);
242 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111);
243 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000);
244 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111);
245 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000);
246 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111);
247 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000);
248 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111);
249 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000);
250 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111);
251 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000);
252 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111);
253 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000);
254 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111);
255 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000);
256 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111);
257 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000);
258 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111);
259 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000);
260 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111);
261 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000);
262 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111);
263 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000);
264 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111);
265 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000);
266 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111);
267 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000);
268 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111);
269 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000);
270 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111);
271 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000000);
272 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111111);
273 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000000);
274 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111111);
275 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000000);
276 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111111);
277 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000000000);
278 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111111111);
279 #if LLONG_AT_LEAST(65)
280 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000000000);
281 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111111111);
282 #endif
283 #if LLONG_AT_LEAST(66)
284 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000000000);
285 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111111111);
286 #endif
287 #if LLONG_AT_LEAST(67)
288 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000000000000);
289 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111111111111);
290 #endif
291 #if LLONG_AT_LEAST(68)
292 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000000000000);
293 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111111111111);
294 #endif
295 #if LLONG_AT_LEAST(69)
296 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000000000000);
297 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111111111111);
298 #endif
299 #if LLONG_AT_LEAST(70)
300 CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000000000000000);
301 CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111111111111111);
302 #endif
303 #if LLONG_AT_LEAST(71)
304 CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000000000000000);
305 CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111111111111111);
306 #endif
307 #if LLONG_AT_LEAST(72)
308 CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000000000000000);
309 CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111111111111111);
310 #endif
311 }
312
313