1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #ifndef _GL_INTPROPS_INTERNAL_H
19 #define _GL_INTPROPS_INTERNAL_H
20
21 #include <limits.h>
22
23
24 #if defined __GNUC__ && 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
25 # pragma GCC diagnostic ignored "-Wtype-limits"
26 #endif
27
28
29
30 #define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
31
32
33
34 #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
35
36
37
38
39
40 #define _GL_TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
41
42
43
44 #define _GL_EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
45
46
47
48
49
50
51
52 #define _GL_TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
53
54
55
56 #define _GL_INT_MINIMUM(e) \
57 (_GL_EXPR_SIGNED (e) \
58 ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
59 : _GL_INT_CONVERT (e, 0))
60 #define _GL_INT_MAXIMUM(e) \
61 (_GL_EXPR_SIGNED (e) \
62 ? _GL_SIGNED_INT_MAXIMUM (e) \
63 : _GL_INT_NEGATE_CONVERT (e, 1))
64 #define _GL_SIGNED_INT_MAXIMUM(e) \
65 (((_GL_INT_CONVERT (e, 1) << (_GL_TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)
66
67
68 #if !defined LLONG_MAX && defined __INT64_MAX
69 # define LLONG_MAX __INT64_MAX
70 # define LLONG_MIN __INT64_MIN
71 #endif
72
73
74
75
76
77
78
79
80 #if (2 <= __GNUC__ \
81 || (4 <= __clang_major__) \
82 || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
83 || (0x5110 <= __SUNPRO_C && !__STDC__))
84 # define _GL_HAVE___TYPEOF__ 1
85 #else
86 # define _GL_HAVE___TYPEOF__ 0
87 #endif
88
89
90
91
92
93 #if _GL_HAVE___TYPEOF__
94 # define _GL_SIGNED_TYPE_OR_EXPR(t) _GL_TYPE_SIGNED (__typeof__ (t))
95 #else
96 # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
97 #endif
98
99
100
101
102 #define _GL_INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
103 ((min) < 0 ? (a) < - (max) : 0 < (a))
104
105
106
107 #ifdef __EDG__
108
109
110 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
111 #elif defined __has_builtin
112 # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
113
114
115 #elif 7 <= __GNUC__
116 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
117 #else
118 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
119 #endif
120
121
122 #if defined __clang_major__ && __clang_major__ < 14
123
124 # define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
125 #else
126 # define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
127 #endif
128
129
130
131 #ifdef __EDG__
132
133
134
135 # define _GL_HAS_BUILTIN_OVERFLOW_P 0
136 #elif defined __has_builtin
137 # define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
138 #else
139 # define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
140 #endif
141
142 #if (!defined _GL_STDCKDINT_H && 202311 <= __STDC_VERSION__ \
143 && ! (_GL_HAS_BUILTIN_ADD_OVERFLOW && _GL_HAS_BUILTIN_MUL_OVERFLOW))
144 # include <stdckdint.h>
145 #endif
146
147
148
149
150
151 #if _GL_HAS_BUILTIN_ADD_OVERFLOW
152 # define _GL_INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
153 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
154 #elif defined ckd_add && defined ckd_sub && !defined _GL_STDCKDINT_H
155 # define _GL_INT_ADD_WRAPV(a, b, r) ckd_add (r, + (a), + (b))
156 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) ckd_sub (r, + (a), + (b))
157 #else
158 # define _GL_INT_ADD_WRAPV(a, b, r) \
159 _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
160 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) \
161 _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
162 #endif
163 #if _GL_HAS_BUILTIN_MUL_OVERFLOW
164 # if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
165 || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \
166 && !defined __EDG__)
167 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
168 # else
169
170 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) \
171 ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && _GL_EXPR_SIGNED (a) && _GL_EXPR_SIGNED (b) \
172 && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, \
173 (__typeof__ (*(r))) 0, \
174 (__typeof__ (*(r))) -1)) \
175 ? ((void) __builtin_mul_overflow (a, b, r), 1) \
176 : __builtin_mul_overflow (a, b, r))
177 # endif
178 #elif defined ckd_mul && !defined _GL_STDCKDINT_H
179 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) ckd_mul (r, + (a), + (b))
180 #else
181 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) \
182 _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
183 #endif
184
185
186
187
188
189
190
191 #if (__GNUC__ && __GNUC__ < 14) || defined __clang__
192 # define _GL__GENERIC_BOGUS 1
193 #else
194 # define _GL__GENERIC_BOGUS 0
195 #endif
196
197
198
199
200
201
202 #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
203 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
204 (_Generic \
205 (*(r), \
206 signed char: \
207 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
208 signed char, SCHAR_MIN, SCHAR_MAX), \
209 unsigned char: \
210 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
211 unsigned char, 0, UCHAR_MAX), \
212 short int: \
213 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
214 short int, SHRT_MIN, SHRT_MAX), \
215 unsigned short int: \
216 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
217 unsigned short int, 0, USHRT_MAX), \
218 int: \
219 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
220 int, INT_MIN, INT_MAX), \
221 unsigned int: \
222 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
223 unsigned int, 0, UINT_MAX), \
224 long int: \
225 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
226 long int, LONG_MIN, LONG_MAX), \
227 unsigned long int: \
228 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
229 unsigned long int, 0, ULONG_MAX), \
230 long long int: \
231 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
232 long long int, LLONG_MIN, LLONG_MAX), \
233 unsigned long long int: \
234 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
235 unsigned long long int, 0, ULLONG_MAX)))
236 #else
237
238
239
240
241
242
243
244 # if _GL_HAVE___TYPEOF__
245 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
246 (_GL_TYPE_SIGNED (__typeof__ (*(r))) \
247 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
248 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
249 # else
250 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
251 (overflow (a, b, smin, smax) \
252 ? (overflow (a, b, 0, umax) \
253 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
254 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
255 : (overflow (a, b, 0, umax) \
256 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
257 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
258 # endif
259
260 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
261 (sizeof *(r) == sizeof (signed char) \
262 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
263 signed char, SCHAR_MIN, SCHAR_MAX, \
264 unsigned char, UCHAR_MAX) \
265 : sizeof *(r) == sizeof (short int) \
266 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
267 short int, SHRT_MIN, SHRT_MAX, \
268 unsigned short int, USHRT_MAX) \
269 : sizeof *(r) == sizeof (int) \
270 ? (_GL_EXPR_SIGNED (*(r)) \
271 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
272 int, INT_MIN, INT_MAX) \
273 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
274 unsigned int, 0, UINT_MAX)) \
275 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
276 # ifdef LLONG_MAX
277 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
278 (sizeof *(r) == sizeof (long int) \
279 ? (_GL_EXPR_SIGNED (*(r)) \
280 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
281 long int, LONG_MIN, LONG_MAX) \
282 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
283 unsigned long int, 0, ULONG_MAX)) \
284 : (_GL_EXPR_SIGNED (*(r)) \
285 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
286 long long int, LLONG_MIN, LLONG_MAX) \
287 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
288 unsigned long long int, 0, ULLONG_MAX)))
289 # else
290 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
291 (_GL_EXPR_SIGNED (*(r)) \
292 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
293 long int, LONG_MIN, LONG_MAX) \
294 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
295 unsigned long int, 0, ULONG_MAX))
296 # endif
297 #endif
298
299
300
301
302
303
304
305 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
306 (overflow (a, b, tmin, tmax) \
307 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
308 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
309
310
311
312
313
314
315
316 #if _GL_HAS_BUILTIN_OVERFLOW_P
317 # define _GL_INT_NEGATE_OVERFLOW(a) \
318 __builtin_sub_overflow_p (0, a, (__typeof__ (- (a))) 0)
319 #else
320 # define _GL_INT_NEGATE_OVERFLOW(a) \
321 _GL_INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
322 #endif
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
342 ((t) ((ut) (a) op (ut) (b)))
343
344
345
346
347
348
349
350 #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
351 ((b) < 0 \
352 ? (((tmin) \
353 ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
354 && (a) < (tmin) - (b)) \
355 : (a) <= -1 - (b)) \
356 || ((_GL_EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
357 : (a) < 0 \
358 ? (((tmin) \
359 ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
360 && (b) < (tmin) - (a)) \
361 : (b) <= -1 - (a)) \
362 || ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
363 && (tmax) < (a) + (b))) \
364 : (tmax) < (b) || (tmax) - (b) < (a))
365 #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
366 (((a) < 0) == ((b) < 0) \
367 ? ((a) < (b) \
368 ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
369 : (tmax) < (a) - (b)) \
370 : (a) < 0 \
371 ? ((!_GL_EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
372 || (a) - (tmin) < (b)) \
373 : ((! (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
374 && _GL_EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
375 && (tmax) <= -1 - (b)) \
376 || (tmax) + (b) < (a)))
377 #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
378 ((b) < 0 \
379 ? ((a) < 0 \
380 ? (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
381 ? (a) < (tmax) / (b) \
382 : ((_GL_INT_NEGATE_OVERFLOW (b) \
383 ? _GL_INT_CONVERT (b, tmax) >> (_GL_TYPE_WIDTH (+ (b)) - 1) \
384 : (tmax) / -(b)) \
385 <= -1 - (a))) \
386 : _GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
387 ? (_GL_EXPR_SIGNED (a) \
388 ? 0 < (a) + (tmin) \
389 : 0 < (a) && -1 - (tmin) < (a) - 1) \
390 : (tmin) / (b) < (a)) \
391 : (b) == 0 \
392 ? 0 \
393 : ((a) < 0 \
394 ? (_GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
395 ? (_GL_EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
396 : (tmin) / (a) < (b)) \
397 : (tmax) / (b) < (a)))
398
399 #endif