1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #ifndef _GL_INTPROPS_H
20 #define _GL_INTPROPS_H
21
22 #include <limits.h>
23
24
25
26 #define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
27
28
29
30 #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
31
32
33
34
35
36
37 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
38
39
40 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
41
42
43
44 #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
45
46
47
48
49
50
51
52 #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
53
54
55 #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
56 #define TYPE_MAXIMUM(t) \
57 ((t) (! TYPE_SIGNED (t) \
58 ? (t) -1 \
59 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
60
61
62
63 #define _GL_INT_MINIMUM(e) \
64 (EXPR_SIGNED (e) \
65 ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
66 : _GL_INT_CONVERT (e, 0))
67 #define _GL_INT_MAXIMUM(e) \
68 (EXPR_SIGNED (e) \
69 ? _GL_SIGNED_INT_MAXIMUM (e) \
70 : _GL_INT_NEGATE_CONVERT (e, 1))
71 #define _GL_SIGNED_INT_MAXIMUM(e) \
72 (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)
73
74
75 #if !defined LLONG_MAX && defined __INT64_MAX
76 # define LLONG_MAX __INT64_MAX
77 # define LLONG_MIN __INT64_MIN
78 #endif
79
80
81
82
83
84
85
86
87 #if (2 <= __GNUC__ \
88 || (4 <= __clang_major__) \
89 || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
90 || (0x5110 <= __SUNPRO_C && !__STDC__))
91 # define _GL_HAVE___TYPEOF__ 1
92 #else
93 # define _GL_HAVE___TYPEOF__ 0
94 #endif
95
96
97
98
99
100 #if _GL_HAVE___TYPEOF__
101 # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
102 #else
103 # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
104 #endif
105
106
107
108
109 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
110
111
112
113
114
115
116
117
118
119
120 #define INT_STRLEN_BOUND(t) \
121 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
122 + _GL_SIGNED_TYPE_OR_EXPR (t))
123
124
125
126 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
170 ((b) < 0 \
171 ? (a) < (min) - (b) \
172 : (max) - (b) < (a))
173
174
175
176 #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
177 ((b) < 0 \
178 ? (max) + (b) < (a) \
179 : (a) < (min) + (b))
180
181
182
183 #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
184 ((min) < 0 \
185 ? (a) < - (max) \
186 : 0 < (a))
187
188
189
190
191
192 #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
193 ((b) < 0 \
194 ? ((a) < 0 \
195 ? (a) < (max) / (b) \
196 : (b) == -1 \
197 ? 0 \
198 : (min) / (b) < (a)) \
199 : (b) == 0 \
200 ? 0 \
201 : ((a) < 0 \
202 ? (a) < (min) / (b) \
203 : (max) / (b) < (a)))
204
205
206
207 #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
208 ((min) < 0 && (b) == -1 && (a) < - (max))
209
210
211
212
213
214
215 #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
216 INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
217
218
219
220
221
222
223
224
225 #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
226 ((a) < 0 \
227 ? (a) < (min) >> (b) \
228 : (max) >> (b) < (a))
229
230
231
232 #if defined __has_builtin
233 # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
234
235
236 #elif 7 <= __GNUC__ && !defined __EDG__
237 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
238 #else
239 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
240 #endif
241
242
243 #if defined __clang_major_ && __clang_major__ < 14
244
245 # define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
246 #else
247 # define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
248 #endif
249
250
251
252 #ifdef __EDG__
253
254
255
256 # define _GL_HAS_BUILTIN_OVERFLOW_P 0
257 #elif defined __has_builtin
258 # define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
259 #else
260 # define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
261 #endif
262
263
264
265
266
267 #if _GL_HAS_BUILTIN_OVERFLOW_P
268 # define _GL_ADD_OVERFLOW(a, b, min, max) \
269 __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
270 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
271 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
272 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
273 __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
274 #else
275 # define _GL_ADD_OVERFLOW(a, b, min, max) \
276 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
277 : (a) < 0 ? (b) <= (a) + (b) \
278 : (b) < 0 ? (a) <= (a) + (b) \
279 : (a) + (b) < (b))
280 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
281 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
282 : (a) < 0 ? 1 \
283 : (b) < 0 ? (a) - (b) <= (a) \
284 : (a) < (b))
285 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
286 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
287 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
288 #endif
289 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
290 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
291 : (a) < 0 ? (b) <= (a) + (b) - 1 \
292 : (b) < 0 && (a) + (b) <= (a))
293 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
294 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
295 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
296 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
297
298
299
300
301
302 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
303 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
304 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
305 ? (a) \
306 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
307 : (a) % - (b)) \
308 == 0)
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359 #define INT_ADD_OVERFLOW(a, b) \
360 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
361 #define INT_SUBTRACT_OVERFLOW(a, b) \
362 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
363 #if _GL_HAS_BUILTIN_OVERFLOW_P
364 # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
365 #else
366 # define INT_NEGATE_OVERFLOW(a) \
367 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
368 #endif
369 #define INT_MULTIPLY_OVERFLOW(a, b) \
370 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
371 #define INT_DIVIDE_OVERFLOW(a, b) \
372 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
373 #define INT_REMAINDER_OVERFLOW(a, b) \
374 _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
375 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
376 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
377 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
378
379
380
381
382
383 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
384 op_result_overflow (a, b, \
385 _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
386 _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
387
388
389
390 #if _GL_HAS_BUILTIN_ADD_OVERFLOW
391 # define INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
392 # define INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
393 #else
394 # define INT_ADD_WRAPV(a, b, r) \
395 _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
396 # define INT_SUBTRACT_WRAPV(a, b, r) \
397 _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
398 #endif
399 #if _GL_HAS_BUILTIN_MUL_OVERFLOW
400 # if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
401 || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \
402 && !defined __EDG__)
403 # define INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
404 # else
405
406 # define INT_MULTIPLY_WRAPV(a, b, r) \
407 ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \
408 && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
409 ? ((void) __builtin_mul_overflow (a, b, r), 1) \
410 : __builtin_mul_overflow (a, b, r))
411 # endif
412 #else
413 # define INT_MULTIPLY_WRAPV(a, b, r) \
414 _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
415 #endif
416
417
418
419
420
421
422
423 #if __GNUC__ || defined __clang__
424 # define _GL__GENERIC_BOGUS 1
425 #else
426 # define _GL__GENERIC_BOGUS 0
427 #endif
428
429
430
431
432 #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
433 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
434 (_Generic \
435 (*(r), \
436 signed char: \
437 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
438 signed char, SCHAR_MIN, SCHAR_MAX), \
439 unsigned char: \
440 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
441 unsigned char, 0, UCHAR_MAX), \
442 short int: \
443 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
444 short int, SHRT_MIN, SHRT_MAX), \
445 unsigned short int: \
446 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
447 unsigned short int, 0, USHRT_MAX), \
448 int: \
449 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
450 int, INT_MIN, INT_MAX), \
451 unsigned int: \
452 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
453 unsigned int, 0, UINT_MAX), \
454 long int: \
455 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
456 long int, LONG_MIN, LONG_MAX), \
457 unsigned long int: \
458 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
459 unsigned long int, 0, ULONG_MAX), \
460 long long int: \
461 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
462 long long int, LLONG_MIN, LLONG_MAX), \
463 unsigned long long int: \
464 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
465 unsigned long long int, 0, ULLONG_MAX)))
466 #else
467
468
469
470
471
472 # if _GL_HAVE___TYPEOF__
473 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
474 (TYPE_SIGNED (__typeof__ (*(r))) \
475 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
476 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
477 # else
478 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
479 (overflow (a, b, smin, smax) \
480 ? (overflow (a, b, 0, umax) \
481 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
482 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
483 : (overflow (a, b, 0, umax) \
484 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
485 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
486 # endif
487
488 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
489 (sizeof *(r) == sizeof (signed char) \
490 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
491 signed char, SCHAR_MIN, SCHAR_MAX, \
492 unsigned char, UCHAR_MAX) \
493 : sizeof *(r) == sizeof (short int) \
494 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
495 short int, SHRT_MIN, SHRT_MAX, \
496 unsigned short int, USHRT_MAX) \
497 : sizeof *(r) == sizeof (int) \
498 ? (EXPR_SIGNED (*(r)) \
499 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
500 int, INT_MIN, INT_MAX) \
501 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
502 unsigned int, 0, UINT_MAX)) \
503 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
504 # ifdef LLONG_MAX
505 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
506 (sizeof *(r) == sizeof (long int) \
507 ? (EXPR_SIGNED (*(r)) \
508 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
509 long int, LONG_MIN, LONG_MAX) \
510 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
511 unsigned long int, 0, ULONG_MAX)) \
512 : (EXPR_SIGNED (*(r)) \
513 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
514 long long int, LLONG_MIN, LLONG_MAX) \
515 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
516 unsigned long long int, 0, ULLONG_MAX)))
517 # else
518 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
519 (EXPR_SIGNED (*(r)) \
520 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
521 long int, LONG_MIN, LONG_MAX) \
522 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
523 unsigned long int, 0, ULONG_MAX))
524 # endif
525 #endif
526
527
528
529
530
531 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
532 (overflow (a, b, tmin, tmax) \
533 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
534 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
554 ((t) ((ut) (a) op (ut) (b)))
555
556
557
558
559
560 #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
561 ((b) < 0 \
562 ? (((tmin) \
563 ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
564 && (a) < (tmin) - (b)) \
565 : (a) <= -1 - (b)) \
566 || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
567 : (a) < 0 \
568 ? (((tmin) \
569 ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
570 && (b) < (tmin) - (a)) \
571 : (b) <= -1 - (a)) \
572 || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
573 && (tmax) < (a) + (b))) \
574 : (tmax) < (b) || (tmax) - (b) < (a))
575 #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
576 (((a) < 0) == ((b) < 0) \
577 ? ((a) < (b) \
578 ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
579 : (tmax) < (a) - (b)) \
580 : (a) < 0 \
581 ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
582 || (a) - (tmin) < (b)) \
583 : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
584 && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
585 && (tmax) <= -1 - (b)) \
586 || (tmax) + (b) < (a)))
587 #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
588 ((b) < 0 \
589 ? ((a) < 0 \
590 ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
591 ? (a) < (tmax) / (b) \
592 : ((INT_NEGATE_OVERFLOW (b) \
593 ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (+ (b)) - 1) \
594 : (tmax) / -(b)) \
595 <= -1 - (a))) \
596 : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
597 ? (EXPR_SIGNED (a) \
598 ? 0 < (a) + (tmin) \
599 : 0 < (a) && -1 - (tmin) < (a) - 1) \
600 : (tmin) / (b) < (a)) \
601 : (b) == 0 \
602 ? 0 \
603 : ((a) < 0 \
604 ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
605 ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
606 : (tmin) / (a) < (b)) \
607 : (tmax) / (b) < (a)))
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634 #define INT_ADD_OK(a, b, r) ! INT_ADD_WRAPV (a, b, r)
635 #define INT_SUBTRACT_OK(a, b, r) ! INT_SUBTRACT_WRAPV (a, b, r)
636 #define INT_MULTIPLY_OK(a, b, r) ! INT_MULTIPLY_WRAPV (a, b, r)
637
638 #endif