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
233
234 #if 7 <= __GNUC__ && !defined __ICC
235 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
236 #elif defined __has_builtin
237 # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
238 #else
239 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
240 #endif
241
242
243 #ifdef __clang__
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 #if defined __clang__ || defined __ICC
253
254
255
256
257 # define _GL_HAS_BUILTIN_OVERFLOW_P 0
258 #elif defined __has_builtin
259 # define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
260 #else
261 # define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
262 #endif
263
264
265
266
267
268 #if _GL_HAS_BUILTIN_OVERFLOW_P
269 # define _GL_ADD_OVERFLOW(a, b, min, max) \
270 __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
271 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
272 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
273 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
274 __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
275 #else
276 # define _GL_ADD_OVERFLOW(a, b, min, max) \
277 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
278 : (a) < 0 ? (b) <= (a) + (b) \
279 : (b) < 0 ? (a) <= (a) + (b) \
280 : (a) + (b) < (b))
281 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
282 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
283 : (a) < 0 ? 1 \
284 : (b) < 0 ? (a) - (b) <= (a) \
285 : (a) < (b))
286 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
287 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
288 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
289 #endif
290 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
291 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
292 : (a) < 0 ? (b) <= (a) + (b) - 1 \
293 : (b) < 0 && (a) + (b) <= (a))
294 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
295 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
296 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
297 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
298
299
300
301
302
303 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
304 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
305 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
306 ? (a) \
307 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
308 : (a) % - (b)) \
309 == 0)
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
360 #define INT_ADD_OVERFLOW(a, b) \
361 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
362 #define INT_SUBTRACT_OVERFLOW(a, b) \
363 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
364 #if _GL_HAS_BUILTIN_OVERFLOW_P
365 # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
366 #else
367 # define INT_NEGATE_OVERFLOW(a) \
368 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
369 #endif
370 #define INT_MULTIPLY_OVERFLOW(a, b) \
371 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
372 #define INT_DIVIDE_OVERFLOW(a, b) \
373 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
374 #define INT_REMAINDER_OVERFLOW(a, b) \
375 _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
376 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
377 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
378 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
379
380
381
382
383
384 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
385 op_result_overflow (a, b, \
386 _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
387 _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
388
389
390
391 #if _GL_HAS_BUILTIN_ADD_OVERFLOW
392 # define INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
393 # define INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
394 #else
395 # define INT_ADD_WRAPV(a, b, r) \
396 _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
397 # define INT_SUBTRACT_WRAPV(a, b, r) \
398 _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
399 #endif
400 #if _GL_HAS_BUILTIN_MUL_OVERFLOW
401 # if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
402 || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \
403 && !defined __ICC)
404 # define INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
405 # else
406
407 # define INT_MULTIPLY_WRAPV(a, b, r) \
408 ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \
409 && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
410 ? ((void) __builtin_mul_overflow (a, b, r), 1) \
411 : __builtin_mul_overflow (a, b, r))
412 # endif
413 #else
414 # define INT_MULTIPLY_WRAPV(a, b, r) \
415 _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
416 #endif
417
418
419
420
421
422
423
424 #if __GNUC__ || defined __clang__
425 # define _GL__GENERIC_BOGUS 1
426 #else
427 # define _GL__GENERIC_BOGUS 0
428 #endif
429
430
431
432
433 #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
434 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
435 (_Generic \
436 (*(r), \
437 signed char: \
438 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
439 signed char, SCHAR_MIN, SCHAR_MAX), \
440 unsigned char: \
441 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
442 unsigned char, 0, UCHAR_MAX), \
443 short int: \
444 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
445 short int, SHRT_MIN, SHRT_MAX), \
446 unsigned short int: \
447 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
448 unsigned short int, 0, USHRT_MAX), \
449 int: \
450 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
451 int, INT_MIN, INT_MAX), \
452 unsigned int: \
453 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
454 unsigned int, 0, UINT_MAX), \
455 long int: \
456 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
457 long int, LONG_MIN, LONG_MAX), \
458 unsigned long int: \
459 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
460 unsigned long int, 0, ULONG_MAX), \
461 long long int: \
462 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
463 long long int, LLONG_MIN, LLONG_MAX), \
464 unsigned long long int: \
465 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
466 unsigned long long int, 0, ULLONG_MAX)))
467 #else
468
469
470
471
472
473 # if _GL_HAVE___TYPEOF__
474 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
475 (TYPE_SIGNED (__typeof__ (*(r))) \
476 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
477 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
478 # else
479 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
480 (overflow (a, b, smin, smax) \
481 ? (overflow (a, b, 0, umax) \
482 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
483 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
484 : (overflow (a, b, 0, umax) \
485 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
486 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
487 # endif
488
489 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
490 (sizeof *(r) == sizeof (signed char) \
491 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
492 signed char, SCHAR_MIN, SCHAR_MAX, \
493 unsigned char, UCHAR_MAX) \
494 : sizeof *(r) == sizeof (short int) \
495 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
496 short int, SHRT_MIN, SHRT_MAX, \
497 unsigned short int, USHRT_MAX) \
498 : sizeof *(r) == sizeof (int) \
499 ? (EXPR_SIGNED (*(r)) \
500 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
501 int, INT_MIN, INT_MAX) \
502 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
503 unsigned int, 0, UINT_MAX)) \
504 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
505 # ifdef LLONG_MAX
506 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
507 (sizeof *(r) == sizeof (long int) \
508 ? (EXPR_SIGNED (*(r)) \
509 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
510 long int, LONG_MIN, LONG_MAX) \
511 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
512 unsigned long int, 0, ULONG_MAX)) \
513 : (EXPR_SIGNED (*(r)) \
514 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
515 long long int, LLONG_MIN, LLONG_MAX) \
516 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
517 unsigned long long int, 0, ULLONG_MAX)))
518 # else
519 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
520 (EXPR_SIGNED (*(r)) \
521 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
522 long int, LONG_MIN, LONG_MAX) \
523 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
524 unsigned long int, 0, ULONG_MAX))
525 # endif
526 #endif
527
528
529
530
531
532 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
533 (overflow (a, b, tmin, tmax) \
534 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
535 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
555 ((t) ((ut) (a) op (ut) (b)))
556
557
558
559
560
561 #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
562 ((b) < 0 \
563 ? (((tmin) \
564 ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
565 && (a) < (tmin) - (b)) \
566 : (a) <= -1 - (b)) \
567 || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
568 : (a) < 0 \
569 ? (((tmin) \
570 ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
571 && (b) < (tmin) - (a)) \
572 : (b) <= -1 - (a)) \
573 || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
574 && (tmax) < (a) + (b))) \
575 : (tmax) < (b) || (tmax) - (b) < (a))
576 #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
577 (((a) < 0) == ((b) < 0) \
578 ? ((a) < (b) \
579 ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
580 : (tmax) < (a) - (b)) \
581 : (a) < 0 \
582 ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
583 || (a) - (tmin) < (b)) \
584 : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
585 && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
586 && (tmax) <= -1 - (b)) \
587 || (tmax) + (b) < (a)))
588 #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
589 ((b) < 0 \
590 ? ((a) < 0 \
591 ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
592 ? (a) < (tmax) / (b) \
593 : ((INT_NEGATE_OVERFLOW (b) \
594 ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (+ (b)) - 1) \
595 : (tmax) / -(b)) \
596 <= -1 - (a))) \
597 : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
598 ? (EXPR_SIGNED (a) \
599 ? 0 < (a) + (tmin) \
600 : 0 < (a) && -1 - (tmin) < (a) - 1) \
601 : (tmin) / (b) < (a)) \
602 : (b) == 0 \
603 ? 0 \
604 : ((a) < 0 \
605 ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
606 ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
607 : (tmin) / (a) < (b)) \
608 : (tmax) / (b) < (a)))
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
635 #define INT_ADD_OK(a, b, r) ! INT_ADD_WRAPV (a, b, r)
636 #define INT_SUBTRACT_OK(a, b, r) ! INT_SUBTRACT_WRAPV (a, b, r)
637 #define INT_MULTIPLY_OK(a, b, r) ! INT_MULTIPLY_WRAPV (a, b, r)
638
639 #endif