Lines 1-6
Link Here
|
1 |
/* intprops.h -- properties of integer types |
1 |
/* intprops.h -- properties of integer types |
2 |
|
2 |
|
3 |
Copyright (C) 2001-2016 Free Software Foundation, Inc. |
3 |
Copyright (C) 2001-2017 Free Software Foundation, Inc. |
4 |
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify it |
5 |
This program is free software: you can redistribute it and/or modify it |
6 |
under the terms of the GNU General Public License as published |
6 |
under the terms of the GNU General Public License as published |
Lines 47-58
Link Here
|
47 |
|
47 |
|
48 |
/* Minimum and maximum values for integer types and expressions. */ |
48 |
/* Minimum and maximum values for integer types and expressions. */ |
49 |
|
49 |
|
|
|
50 |
/* The width in bits of the integer type or expression T. |
51 |
Padding bits are not supported; this is checked at compile-time below. */ |
52 |
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT) |
53 |
|
50 |
/* The maximum and minimum values for the integer type T. */ |
54 |
/* The maximum and minimum values for the integer type T. */ |
51 |
#define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t)) |
55 |
#define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t)) |
52 |
#define TYPE_MAXIMUM(t) \ |
56 |
#define TYPE_MAXIMUM(t) \ |
53 |
((t) (! TYPE_SIGNED (t) \ |
57 |
((t) (! TYPE_SIGNED (t) \ |
54 |
? (t) -1 \ |
58 |
? (t) -1 \ |
55 |
: ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1))) |
59 |
: ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1))) |
56 |
|
60 |
|
57 |
/* The maximum and minimum values for the type of the expression E, |
61 |
/* The maximum and minimum values for the type of the expression E, |
58 |
after integer promotion. E should not have side effects. */ |
62 |
after integer promotion. E should not have side effects. */ |
Lines 65-71
Link Here
|
65 |
? _GL_SIGNED_INT_MAXIMUM (e) \ |
69 |
? _GL_SIGNED_INT_MAXIMUM (e) \ |
66 |
: _GL_INT_NEGATE_CONVERT (e, 1)) |
70 |
: _GL_INT_NEGATE_CONVERT (e, 1)) |
67 |
#define _GL_SIGNED_INT_MAXIMUM(e) \ |
71 |
#define _GL_SIGNED_INT_MAXIMUM(e) \ |
68 |
(((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1) |
72 |
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1) |
|
|
73 |
|
74 |
/* Work around OpenVMS incompatibility with C99. */ |
75 |
#if !defined LLONG_MAX && defined __INT64_MAX |
76 |
# define LLONG_MAX __INT64_MAX |
77 |
# define LLONG_MIN __INT64_MIN |
78 |
#endif |
69 |
|
79 |
|
70 |
/* This include file assumes that signed types are two's complement without |
80 |
/* This include file assumes that signed types are two's complement without |
71 |
padding bits; the above macros have undefined behavior otherwise. |
81 |
padding bits; the above macros have undefined behavior otherwise. |
Lines 84-93
verify (TYPE_MAXIMUM (long int) == LONG_
Link Here
|
84 |
verify (TYPE_MINIMUM (long long int) == LLONG_MIN); |
94 |
verify (TYPE_MINIMUM (long long int) == LLONG_MIN); |
85 |
verify (TYPE_MAXIMUM (long long int) == LLONG_MAX); |
95 |
verify (TYPE_MAXIMUM (long long int) == LLONG_MAX); |
86 |
#endif |
96 |
#endif |
|
|
97 |
/* Similarly, sanity-check one ISO/IEC TS 18661-1:2014 macro if defined. */ |
98 |
#ifdef UINT_WIDTH |
99 |
verify (TYPE_WIDTH (unsigned int) == UINT_WIDTH); |
100 |
#endif |
87 |
|
101 |
|
88 |
/* Does the __typeof__ keyword work? This could be done by |
102 |
/* Does the __typeof__ keyword work? This could be done by |
89 |
'configure', but for now it's easier to do it by hand. */ |
103 |
'configure', but for now it's easier to do it by hand. */ |
90 |
#if (2 <= __GNUC__ || defined __IBM__TYPEOF__ \ |
104 |
#if (2 <= __GNUC__ \ |
|
|
105 |
|| (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \ |
91 |
|| (0x5110 <= __SUNPRO_C && !__STDC__)) |
106 |
|| (0x5110 <= __SUNPRO_C && !__STDC__)) |
92 |
# define _GL_HAVE___TYPEOF__ 1 |
107 |
# define _GL_HAVE___TYPEOF__ 1 |
93 |
#else |
108 |
#else |
Lines 116-123
verify (TYPE_MAXIMUM (long long int) ==
Link Here
|
116 |
signed, this macro may overestimate the true bound by one byte when |
131 |
signed, this macro may overestimate the true bound by one byte when |
117 |
applied to unsigned types of size 2, 4, 16, ... bytes. */ |
132 |
applied to unsigned types of size 2, 4, 16, ... bytes. */ |
118 |
#define INT_STRLEN_BOUND(t) \ |
133 |
#define INT_STRLEN_BOUND(t) \ |
119 |
(INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \ |
134 |
(INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \ |
120 |
- _GL_SIGNED_TYPE_OR_EXPR (t)) \ |
|
|
121 |
+ _GL_SIGNED_TYPE_OR_EXPR (t)) |
135 |
+ _GL_SIGNED_TYPE_OR_EXPR (t)) |
122 |
|
136 |
|
123 |
/* Bound on buffer size needed to represent an integer type or expression T, |
137 |
/* Bound on buffer size needed to represent an integer type or expression T, |
Lines 222-241
verify (TYPE_MAXIMUM (long long int) ==
Link Here
|
222 |
? (a) < (min) >> (b) \ |
236 |
? (a) < (min) >> (b) \ |
223 |
: (max) >> (b) < (a)) |
237 |
: (max) >> (b) < (a)) |
224 |
|
238 |
|
225 |
/* True if __builtin_add_overflow (A, B, P) works when P is null. */ |
239 |
/* True if __builtin_add_overflow (A, B, P) works when P is non-null. */ |
226 |
#define _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL (7 <= __GNUC__) |
240 |
#define _GL_HAS_BUILTIN_OVERFLOW (5 <= __GNUC__) |
|
|
241 |
|
242 |
/* True if __builtin_add_overflow_p (A, B, C) works. */ |
243 |
#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__) |
227 |
|
244 |
|
228 |
/* The _GL*_OVERFLOW macros have the same restrictions as the |
245 |
/* The _GL*_OVERFLOW macros have the same restrictions as the |
229 |
*_RANGE_OVERFLOW macros, except that they do not assume that operands |
246 |
*_RANGE_OVERFLOW macros, except that they do not assume that operands |
230 |
(e.g., A and B) have the same type as MIN and MAX. Instead, they assume |
247 |
(e.g., A and B) have the same type as MIN and MAX. Instead, they assume |
231 |
that the result (e.g., A + B) has that type. */ |
248 |
that the result (e.g., A + B) has that type. */ |
232 |
#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL |
249 |
#if _GL_HAS_BUILTIN_OVERFLOW_P |
233 |
# define _GL_ADD_OVERFLOW(a, b, min, max) |
250 |
# define _GL_ADD_OVERFLOW(a, b, min, max) \ |
234 |
__builtin_add_overflow (a, b, (__typeof__ ((a) + (b)) *) 0) |
251 |
__builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0) |
235 |
# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) |
252 |
# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ |
236 |
__builtin_sub_overflow (a, b, (__typeof__ ((a) - (b)) *) 0) |
253 |
__builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0) |
237 |
# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) |
254 |
# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ |
238 |
__builtin_mul_overflow (a, b, (__typeof__ ((a) * (b)) *) 0) |
255 |
__builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0) |
239 |
#else |
256 |
#else |
240 |
# define _GL_ADD_OVERFLOW(a, b, min, max) \ |
257 |
# define _GL_ADD_OVERFLOW(a, b, min, max) \ |
241 |
((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ |
258 |
((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ |
Lines 315-321
verify (TYPE_MAXIMUM (long long int) ==
Link Here
|
315 |
_GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) |
332 |
_GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) |
316 |
#define INT_SUBTRACT_OVERFLOW(a, b) \ |
333 |
#define INT_SUBTRACT_OVERFLOW(a, b) \ |
317 |
_GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) |
334 |
_GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) |
318 |
#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL |
335 |
#if _GL_HAS_BUILTIN_OVERFLOW_P |
319 |
# define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a) |
336 |
# define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a) |
320 |
#else |
337 |
#else |
321 |
# define INT_NEGATE_OVERFLOW(a) \ |
338 |
# define INT_NEGATE_OVERFLOW(a) \ |
Lines 349-358
verify (TYPE_MAXIMUM (long long int) ==
Link Here
|
349 |
#define INT_MULTIPLY_WRAPV(a, b, r) \ |
366 |
#define INT_MULTIPLY_WRAPV(a, b, r) \ |
350 |
_GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW) |
367 |
_GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW) |
351 |
|
368 |
|
352 |
#ifndef __has_builtin |
|
|
353 |
# define __has_builtin(x) 0 |
354 |
#endif |
355 |
|
356 |
/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See: |
369 |
/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See: |
357 |
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193 |
370 |
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193 |
358 |
https://llvm.org/bugs/show_bug.cgi?id=25390 |
371 |
https://llvm.org/bugs/show_bug.cgi?id=25390 |
Lines 369-375
verify (TYPE_MAXIMUM (long long int) ==
Link Here
|
369 |
the operation. BUILTIN is the builtin operation, and OVERFLOW the |
382 |
the operation. BUILTIN is the builtin operation, and OVERFLOW the |
370 |
overflow predicate. Return 1 if the result overflows. See above |
383 |
overflow predicate. Return 1 if the result overflows. See above |
371 |
for restrictions. */ |
384 |
for restrictions. */ |
372 |
#if 5 <= __GNUC__ || __has_builtin (__builtin_add_overflow) |
385 |
#if _GL_HAS_BUILTIN_OVERFLOW |
373 |
# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r) |
386 |
# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r) |
374 |
#elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS |
387 |
#elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS |
375 |
# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \ |
388 |
# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \ |
Lines 412-418
verify (TYPE_MAXIMUM (long long int) ==
Link Here
|
412 |
# else |
425 |
# else |
413 |
# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ |
426 |
# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ |
414 |
_GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ |
427 |
_GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ |
415 |
long int, LONG_MIN, LONG_MAX)) |
428 |
long int, LONG_MIN, LONG_MAX) |
416 |
# endif |
429 |
# endif |
417 |
#endif |
430 |
#endif |
418 |
|
431 |
|