1 /* Verify that <stdint.h> is present and follows the C99 requirements.
2    If this test fails because of the header being missing on a
3    particular target, this indicates GCC has not been correctly
4    configured regarding what version of <stdint.h> to install or what
5    the <stdint.h> types are on that target.  If GCC is wrapping a
6    system copy of the header and some tests fail because of bugs in
7    that copy, they should be fixed with fixincludes (and the bugs
8    reported to maintainer of that copy if still present in the latest
9    version).  */
10 /* { dg-do compile } */
11 /* { dg-options "-std=iso9899:1999 -pedantic-errors -fhosted" } */
12 /* { dg-require-effective-target ptr32plus } */
13 /* { dg-additional-options "-DSIGNAL_SUPPRESS" { target { ! signal } } } */
14 
15 #include <limits.h>
16 #include <stdint.h>
17 /* This and the later SIG_ATOMIC_* tests should be appropriately
18    conditioned for any freestanding targets with no <signal.h>.  */
19 #ifndef SIGNAL_SUPPRESS
20 #include <signal.h>
21 #endif
22 
23 /* Note that some of these conditions assume two's complement and no
24    padding bits; GCC only supports two's complement, and no supported
25    target has padding bits in any integer type of the standard
26    widths.  */
27 #define CHECK_SIGNED(TYPE) \
28   do { TYPE a; int b[(TYPE)-1 < 0 ? 1 : -1]; } while (0)
29 #define CHECK_UNSIGNED(TYPE) \
30   do { TYPE a; int b[(TYPE)-1 < 0 ? -1 : 1]; } while (0)
31 #define CHECK_WIDTH_EQUALS(TYPE, WIDTH) \
32   do { int a[sizeof(TYPE) * CHAR_BIT == (WIDTH) ? 1 : -1]; } while (0)
33 #define CHECK_WIDTH_AT_LEAST(TYPE, WIDTH) \
34   do { int a[sizeof(TYPE) * CHAR_BIT >= (WIDTH) ? 1 : -1]; } while (0)
35 #define CHECK_WIDTH_ORDER(TYPE1, TYPE2) \
36   do { int a[sizeof(TYPE2) >= sizeof(TYPE1) ? 1 : -1]; } while (0)
37 #define CHECK_EXPR_TYPE(TYPE, EXPR) \
38   do { __typeof__(EXPR) a; __typeof__((TYPE)0 + 0) *b = &a; } while (0)
39 #define UNSIGNED_MAX_COND(TYPE, EXPR) \
40   ((EXPR) == (TYPE)-1)
41 #define SIGNED_MIN_MAX_COND(TYPE, MIN, MAX)				\
42   ((MIN) == -(MAX)-1							\
43    && ((MAX) & 1)							\
44    && ((((MAX) >> 1) + 1) >> (sizeof(TYPE) * CHAR_BIT - 2)) == 1)
45 #define MIN_MAX_COND(TYPE, MIN, MAX)			\
46   ((TYPE)-1 < 0						\
47    ? SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX))		\
48    : ((MIN) == 0 && UNSIGNED_MAX_COND(TYPE, (MAX))))
49 #define CHECK_SIGNED_LIMITS(TYPE, MIN, MAX)				\
50   CHECK_SIGNED(TYPE);							\
51   CHECK_EXPR_TYPE(TYPE, (MIN));						\
52   CHECK_EXPR_TYPE(TYPE, (MAX));						\
53   do { int a[SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX)) ? 1 : -1]; } while (0)
54 #define CHECK_SIGNED_LIMITS_2(TYPE, MIN, MAX, MINBD, MAXBD)	\
55   CHECK_SIGNED(TYPE);						\
56   CHECK_EXPR_TYPE(TYPE, (MIN));					\
57   CHECK_EXPR_TYPE(TYPE, (MAX));					\
58   do { int a[(SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX))		\
59 	      && (MIN) <= (MINBD)				\
60 	      && (MAX) >= (MAXBD)) ? 1 : -1]; } while (0)
61 #define CHECK_UNSIGNED_LIMITS(TYPE, MAX)				\
62   CHECK_UNSIGNED(TYPE);							\
63   CHECK_EXPR_TYPE(TYPE, (MAX));						\
64   do { int a[UNSIGNED_MAX_COND(TYPE, (MAX)) ? 1 : -1]; } while (0)
65 #define CHECK_UNSIGNED_LIMITS_2(TYPE, MAX, MAXBD)		\
66   CHECK_UNSIGNED(TYPE);						\
67   CHECK_EXPR_TYPE(TYPE, (MAX));					\
68   do { int a[(UNSIGNED_MAX_COND(TYPE, (MAX))			\
69 	      && (MAX) >= (MAXBD)) ? 1 : -1]; } while (0)
70 #define CHECK_LIMITS_2(TYPE, MIN, MAX, SMINBD, SMAXBD, UMAXBD)	\
71   do { int a[(MIN_MAX_COND(TYPE, (MIN), (MAX))			\
72 	      && ((TYPE)-1 < 0					\
73 		  ? ((MIN) <= (SMINBD) && (MAX) >= (SMAXBD))	\
74 		  : (MAX) >= (UMAXBD))) ? 1 : -1]; } while (0)
75 #define CHECK_CONSTS(TYPE, MACRO)				\
76   CHECK_EXPR_TYPE(TYPE, MACRO(01));				\
77   CHECK_EXPR_TYPE(TYPE, MACRO(2));				\
78   CHECK_EXPR_TYPE(TYPE, MACRO(0x3));				\
79   do { int a[(MACRO(12) == 12					\
80 	      && MACRO(012) == 012				\
81 	      && MACRO(0x12) == 0x12) ? 1 : -1]; } while (0);
82 
83 void
test_exact(void)84 test_exact (void)
85 {
86 #ifdef INT8_MIN
87   CHECK_WIDTH_EQUALS(int8_t, 8);
88   CHECK_SIGNED_LIMITS(int8_t, INT8_MIN, INT8_MAX);
89 #else
90   CHECK_WIDTH_AT_LEAST(int_least8_t, 9);
91 #endif
92 #ifdef INT16_MIN
93   CHECK_WIDTH_EQUALS(int16_t, 16);
94   CHECK_SIGNED_LIMITS(int16_t, INT16_MIN, INT16_MAX);
95 #else
96   CHECK_WIDTH_AT_LEAST(int_least16_t, 17);
97 #endif
98 #ifdef INT32_MIN
99   CHECK_WIDTH_EQUALS(int32_t, 32);
100   CHECK_SIGNED_LIMITS(int32_t, INT32_MIN, INT32_MAX);
101 #else
102   CHECK_WIDTH_AT_LEAST(int_least32_t, 33);
103 #endif
104 #ifdef INT64_MIN
105   CHECK_WIDTH_EQUALS(int64_t, 64);
106   CHECK_SIGNED_LIMITS(int64_t, INT64_MIN, INT64_MAX);
107 #else
108   CHECK_WIDTH_AT_LEAST(int_least64_t, 65);
109 #endif
110 #ifdef UINT8_MAX
111   CHECK_WIDTH_EQUALS(uint8_t, 8);
112   CHECK_UNSIGNED_LIMITS(uint8_t, UINT8_MAX);
113 #else
114   CHECK_WIDTH_AT_LEAST(uint_least8_t, 9);
115 #endif
116 #ifdef UINT16_MAX
117   CHECK_WIDTH_EQUALS(uint16_t, 16);
118   CHECK_UNSIGNED_LIMITS(uint16_t, UINT16_MAX);
119 #else
120   CHECK_WIDTH_AT_LEAST(uint_least16_t, 17);
121 #endif
122 #ifdef UINT32_MAX
123   CHECK_WIDTH_EQUALS(uint32_t, 32);
124   CHECK_UNSIGNED_LIMITS(uint32_t, UINT32_MAX);
125 #else
126   CHECK_WIDTH_AT_LEAST(uint_least32_t, 33);
127 #endif
128 #ifdef UINT64_MAX
129   CHECK_WIDTH_EQUALS(uint64_t, 64);
130   CHECK_UNSIGNED_LIMITS(uint64_t, UINT64_MAX);
131 #else
132   CHECK_WIDTH_AT_LEAST(uint_least64_t, 65);
133 #endif
134 }
135 
136 void
test_least(void)137 test_least (void)
138 {
139   CHECK_WIDTH_AT_LEAST(int_least8_t, 8);
140   CHECK_WIDTH_ORDER(int_least8_t, int_fast8_t);
141   CHECK_SIGNED_LIMITS(int_least8_t, INT_LEAST8_MIN, INT_LEAST8_MAX);
142   CHECK_WIDTH_AT_LEAST(int_least16_t, 16);
143   CHECK_WIDTH_ORDER(int_least16_t, int_fast16_t);
144   CHECK_SIGNED_LIMITS(int_least16_t, INT_LEAST16_MIN, INT_LEAST16_MAX);
145   CHECK_WIDTH_AT_LEAST(int_least32_t, 32);
146   CHECK_WIDTH_ORDER(int_least32_t, int_fast32_t);
147   CHECK_SIGNED_LIMITS(int_least32_t, INT_LEAST32_MIN, INT_LEAST32_MAX);
148   CHECK_WIDTH_AT_LEAST(int_least64_t, 64);
149   CHECK_WIDTH_ORDER(int_least64_t, int_fast64_t);
150   CHECK_SIGNED_LIMITS(int_least64_t, INT_LEAST64_MIN, INT_LEAST64_MAX);
151   CHECK_WIDTH_AT_LEAST(uint_least8_t, 8);
152   CHECK_WIDTH_ORDER(uint_least8_t, uint_fast8_t);
153   CHECK_UNSIGNED_LIMITS(uint_least8_t, UINT_LEAST8_MAX);
154   CHECK_WIDTH_AT_LEAST(uint_least16_t, 16);
155   CHECK_WIDTH_ORDER(uint_least16_t, uint_fast16_t);
156   CHECK_UNSIGNED_LIMITS(uint_least16_t, UINT_LEAST16_MAX);
157   CHECK_WIDTH_AT_LEAST(uint_least32_t, 32);
158   CHECK_WIDTH_ORDER(uint_least32_t, uint_fast32_t);
159   CHECK_UNSIGNED_LIMITS(uint_least32_t, UINT_LEAST32_MAX);
160   CHECK_WIDTH_AT_LEAST(uint_least64_t, 64);
161   CHECK_WIDTH_ORDER(uint_least64_t, uint_fast64_t);
162   CHECK_UNSIGNED_LIMITS(uint_least64_t, UINT_LEAST64_MAX);
163 }
164 
165 void
test_fast(void)166 test_fast (void)
167 {
168   CHECK_WIDTH_AT_LEAST(int_fast8_t, 8);
169   CHECK_SIGNED_LIMITS(int_fast8_t, INT_FAST8_MIN, INT_FAST8_MAX);
170   CHECK_WIDTH_AT_LEAST(int_fast16_t, 16);
171   CHECK_SIGNED_LIMITS(int_fast16_t, INT_FAST16_MIN, INT_FAST16_MAX);
172   CHECK_WIDTH_AT_LEAST(int_fast32_t, 32);
173   CHECK_SIGNED_LIMITS(int_fast32_t, INT_FAST32_MIN, INT_FAST32_MAX);
174   CHECK_WIDTH_AT_LEAST(int_fast64_t, 64);
175   CHECK_SIGNED_LIMITS(int_fast64_t, INT_FAST64_MIN, INT_FAST64_MAX);
176   CHECK_WIDTH_AT_LEAST(uint_fast8_t, 8);
177   CHECK_UNSIGNED_LIMITS(uint_fast8_t, UINT_FAST8_MAX);
178   CHECK_WIDTH_AT_LEAST(uint_fast16_t, 16);
179   CHECK_UNSIGNED_LIMITS(uint_fast16_t, UINT_FAST16_MAX);
180   CHECK_WIDTH_AT_LEAST(uint_fast32_t, 32);
181   CHECK_UNSIGNED_LIMITS(uint_fast32_t, UINT_FAST32_MAX);
182   CHECK_WIDTH_AT_LEAST(uint_fast64_t, 64);
183   CHECK_UNSIGNED_LIMITS(uint_fast64_t, UINT_FAST64_MAX);
184 }
185 
186 void
test_ptr(void)187 test_ptr (void)
188 {
189 #ifdef INTPTR_MIN
190   CHECK_SIGNED_LIMITS_2(intptr_t, INTPTR_MIN, INTPTR_MAX, -0x7fff, 0x7fff);
191 #endif
192 #ifdef UINTPTR_MAX
193   CHECK_UNSIGNED_LIMITS_2(uintptr_t, UINTPTR_MAX, 0xffffU);
194 #endif
195 }
196 
197 void
test_max(void)198 test_max (void)
199 {
200   CHECK_WIDTH_AT_LEAST(intmax_t, 64);
201   CHECK_WIDTH_ORDER(long long, intmax_t);
202   CHECK_WIDTH_ORDER(int_fast8_t, intmax_t);
203   CHECK_WIDTH_ORDER(int_fast16_t, intmax_t);
204   CHECK_WIDTH_ORDER(int_fast32_t, intmax_t);
205   CHECK_WIDTH_ORDER(int_fast64_t, intmax_t);
206   CHECK_SIGNED_LIMITS(intmax_t, INTMAX_MIN, INTMAX_MAX);
207   CHECK_WIDTH_AT_LEAST(uintmax_t, 64);
208   CHECK_WIDTH_ORDER(unsigned long long, uintmax_t);
209   CHECK_WIDTH_ORDER(uint_fast8_t, uintmax_t);
210   CHECK_WIDTH_ORDER(uint_fast16_t, uintmax_t);
211   CHECK_WIDTH_ORDER(uint_fast32_t, uintmax_t);
212   CHECK_WIDTH_ORDER(uint_fast64_t, uintmax_t);
213   CHECK_UNSIGNED_LIMITS(uintmax_t, UINTMAX_MAX);
214 }
215 
216 void
test_misc_limits(void)217 test_misc_limits (void)
218 {
219   CHECK_SIGNED_LIMITS_2(__PTRDIFF_TYPE__, PTRDIFF_MIN, PTRDIFF_MAX, -65535L, 65535L);
220 #ifndef SIGNAL_SUPPRESS
221   CHECK_LIMITS_2(sig_atomic_t, SIG_ATOMIC_MIN, SIG_ATOMIC_MAX, -127, 127, 255);
222 #endif
223   CHECK_UNSIGNED_LIMITS_2(__SIZE_TYPE__, SIZE_MAX, 65535U);
224   CHECK_LIMITS_2(__WCHAR_TYPE__, WCHAR_MIN, WCHAR_MAX, -127, 127, 255);
225   CHECK_LIMITS_2(__WINT_TYPE__, WINT_MIN, WINT_MAX, -32767, 32767, 65535);
226 }
227 
228 void
test_constants(void)229 test_constants (void)
230 {
231   CHECK_CONSTS(int_least8_t, INT8_C);
232   CHECK_CONSTS(int_least16_t, INT16_C);
233   CHECK_CONSTS(int_least32_t, INT32_C);
234   CHECK_CONSTS(int_least64_t, INT64_C);
235   CHECK_CONSTS(intmax_t, INTMAX_C);
236   CHECK_CONSTS(uint_least8_t, UINT8_C);
237   CHECK_CONSTS(uint_least16_t, UINT16_C);
238   CHECK_CONSTS(uint_least32_t, UINT32_C);
239   CHECK_CONSTS(uint_least64_t, UINT64_C);
240   CHECK_CONSTS(uintmax_t, UINTMAX_C);
241 #if INT8_C(12) != 12
242 #error "INT8_C not usable in #if"
243 #endif
244 #if INT16_C(12) != 12
245 #error "INT16_C not usable in #if"
246 #endif
247 #if INT32_C(12) != 12
248 #error "INT32_C not usable in #if"
249 #endif
250 #if INT64_C(12) != 12
251 #error "INT64_C not usable in #if"
252 #endif
253 #if INTMAX_C(12) != 12
254 #error "INTMAX_C not usable in #if"
255 #endif
256 #if UINT8_C(12) != 12
257 #error "UINT8_C not usable in #if"
258 #endif
259 #if UINT16_C(12) != 12
260 #error "UINT16_C not usable in #if"
261 #endif
262 #if UINT32_C(12) != 12
263 #error "UINT32_C not usable in #if"
264 #endif
265 #if UINT64_C(12) != 12
266 #error "UINT64_C not usable in #if"
267 #endif
268 #if UINTMAX_C(12) != 12
269 #error "UINTMAX_C not usable in #if"
270 #endif
271 }
272