1 /* intprops.h -- properties of integer types
3 Copyright (C) 2001-2016 Free Software Foundation, Inc.
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
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 #ifndef _GL_INTPROPS_H
21 #define _GL_INTPROPS_H
27 # define __has_builtin(x) 0
30 /* Return a value with the common real type of E and V and the value of V. */
31 #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
33 /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
34 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */
35 #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
37 /* The extra casts in the following macros work around compiler bugs,
38 e.g., in Cray C 5.0.3.0. */
40 /* True if the arithmetic type T is an integer type. bool counts as
42 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
44 /* True if the real type T is signed. */
45 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
47 /* Return 1 if the real expression E, after promotion, has a
48 signed or floating type. */
49 #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
52 /* Minimum and maximum values for integer types and expressions. */
54 /* The width in bits of the integer type or expression T.
55 Padding bits are not supported; this is checked at compile-time below. */
56 #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
58 /* The maximum and minimum values for the integer type T. */
59 #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
60 #define TYPE_MAXIMUM(t) \
61 ((t) (! TYPE_SIGNED (t) \
63 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
65 /* The maximum and minimum values for the type of the expression E,
66 after integer promotion. E should not have side effects. */
67 #define _GL_INT_MINIMUM(e) \
69 ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
70 : _GL_INT_CONVERT (e, 0))
71 #define _GL_INT_MAXIMUM(e) \
73 ? _GL_SIGNED_INT_MAXIMUM (e) \
74 : _GL_INT_NEGATE_CONVERT (e, 1))
75 #define _GL_SIGNED_INT_MAXIMUM(e) \
76 (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
78 /* Work around OpenVMS incompatibility with C99. */
79 #if !defined LLONG_MAX && defined __INT64_MAX
80 # define LLONG_MAX __INT64_MAX
81 # define LLONG_MIN __INT64_MIN
84 /* This include file assumes that signed types are two's complement without
85 padding bits; the above macros have undefined behavior otherwise.
86 If this is a problem for you, please let us know how to fix it for your host.
87 As a sanity check, test the assumption for some signed types that
89 verify (TYPE_MINIMUM (signed char) == SCHAR_MIN
);
90 verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX
);
91 verify (TYPE_MINIMUM (short int) == SHRT_MIN
);
92 verify (TYPE_MAXIMUM (short int) == SHRT_MAX
);
93 verify (TYPE_MINIMUM (int) == INT_MIN
);
94 verify (TYPE_MAXIMUM (int) == INT_MAX
);
95 verify (TYPE_MINIMUM (long int) == LONG_MIN
);
96 verify (TYPE_MAXIMUM (long int) == LONG_MAX
);
98 verify (TYPE_MINIMUM (long long int) == LLONG_MIN
);
99 verify (TYPE_MAXIMUM (long long int) == LLONG_MAX
);
101 /* Similarly, sanity-check one ISO/IEC TS 18661-1:2014 macro if defined. */
103 verify (TYPE_WIDTH (unsigned int) == UINT_WIDTH
);
106 /* Does the __typeof__ keyword work? This could be done by
107 'configure', but for now it's easier to do it by hand. */
108 #if (2 <= __GNUC__ || defined __IBM__TYPEOF__ \
109 || (0x5110 <= __SUNPRO_C && !__STDC__))
110 # define _GL_HAVE___TYPEOF__ 1
112 # define _GL_HAVE___TYPEOF__ 0
115 /* Return 1 if the integer type or expression T might be signed. Return 0
116 if it is definitely unsigned. This macro does not evaluate its argument,
117 and expands to an integer constant expression. */
118 #if _GL_HAVE___TYPEOF__
119 # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
121 # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
124 /* Bound on length of the string representing an unsigned integer
125 value representable in B bits. log10 (2.0) < 146/485. The
126 smallest value of B where this bound is not tight is 2621. */
127 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
129 /* Bound on length of the string representing an integer type or expression T.
130 Subtract 1 for the sign bit if T is signed, and then add 1 more for
131 a minus sign if needed.
133 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
134 signed, this macro may overestimate the true bound by one byte when
135 applied to unsigned types of size 2, 4, 16, ... bytes. */
136 #define INT_STRLEN_BOUND(t) \
137 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
138 + _GL_SIGNED_TYPE_OR_EXPR (t))
140 /* Bound on buffer size needed to represent an integer type or expression T,
141 including the terminating null. */
142 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
145 /* Range overflow checks.
147 The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
148 operators might not yield numerically correct answers due to
149 arithmetic overflow. They do not rely on undefined or
150 implementation-defined behavior. Their implementations are simple
151 and straightforward, but they are a bit harder to use than the
152 INT_<op>_OVERFLOW macros described below.
158 if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
159 printf ("multiply would overflow");
161 printf ("product is %ld", i * j);
163 Restrictions on *_RANGE_OVERFLOW macros:
165 These macros do not check for all possible numerical problems or
166 undefined or unspecified behavior: they do not check for division
167 by zero, for bad shift counts, or for shifting negative numbers.
169 These macros may evaluate their arguments zero or multiple times,
170 so the arguments should not have side effects. The arithmetic
171 arguments (including the MIN and MAX arguments) must be of the same
172 integer type after the usual arithmetic conversions, and the type
173 must have minimum value MIN and maximum MAX. Unsigned types should
174 use a zero MIN of the proper type.
176 These macros are tuned for constant MIN and MAX. For commutative
177 operations such as A + B, they are also tuned for constant B. */
179 /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
180 See above for restrictions. */
181 #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
183 ? (a) < (min) - (b) \
186 /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
187 See above for restrictions. */
188 #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
190 ? (max) + (b) < (a) \
193 /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
194 See above for restrictions. */
195 #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
200 /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
201 See above for restrictions. Avoid && and || as they tickle
202 bugs in Sun C 5.11 2010/08/13 and other compilers; see
203 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
204 #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
207 ? (a) < (max) / (b) \
210 : (min) / (b) < (a)) \
214 ? (a) < (min) / (b) \
215 : (max) / (b) < (a)))
217 /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
218 See above for restrictions. Do not check for division by zero. */
219 #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
220 ((min) < 0 && (b) == -1 && (a) < - (max))
222 /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
223 See above for restrictions. Do not check for division by zero.
224 Mathematically, % should never overflow, but on x86-like hosts
225 INT_MIN % -1 traps, and the C standard permits this, so treat this
226 as an overflow too. */
227 #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
228 INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
230 /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
231 See above for restrictions. Here, MIN and MAX are for A only, and B need
232 not be of the same type as the other arguments. The C standard says that
233 behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
234 A is negative then A << B has undefined behavior and A >> B has
235 implementation-defined behavior, but do not check these other
237 #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
239 ? (a) < (min) >> (b) \
240 : (max) >> (b) < (a))
242 /* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
243 #define _GL_HAS_BUILTIN_OVERFLOW \
244 (5 <= __GNUC__ || __has_builtin (__builtin_add_overflow))
246 /* True if __builtin_add_overflow_p (A, B, C) works. */
247 #define _GL_HAS_BUILTIN_OVERFLOW_P \
248 (7 <= __GNUC__ || __has_builtin (__builtin_add_overflow_p))
250 /* The _GL*_OVERFLOW macros have the same restrictions as the
251 *_RANGE_OVERFLOW macros, except that they do not assume that operands
252 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
253 that the result (e.g., A + B) has that type. */
254 #if _GL_HAS_BUILTIN_OVERFLOW_P
255 # define _GL_ADD_OVERFLOW(a, b, min, max) \
256 __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
257 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
258 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
259 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
260 __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
262 # define _GL_ADD_OVERFLOW(a, b, min, max) \
263 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
264 : (a) < 0 ? (b) <= (a) + (b) \
265 : (b) < 0 ? (a) <= (a) + (b) \
267 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
268 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
270 : (b) < 0 ? (a) - (b) <= (a) \
272 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
273 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
274 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
276 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
277 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
278 : (a) < 0 ? (b) <= (a) + (b) - 1 \
279 : (b) < 0 && (a) + (b) <= (a))
280 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
281 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
282 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
283 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
285 /* Return a nonzero value if A is a mathematical multiple of B, where
286 A is unsigned, B is negative, and MAX is the maximum value of A's
287 type. A's type must be the same as (A % B)'s type. Normally (A %
288 -B == 0) suffices, but things get tricky if -B would overflow. */
289 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
290 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
291 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
293 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
297 /* Check for integer overflow, and report low order bits of answer.
299 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
300 might not yield numerically correct answers due to arithmetic overflow.
301 The INT_<op>_WRAPV macros also store the low-order bits of the answer.
302 These macros work correctly on all known practical hosts, and do not rely
303 on undefined behavior due to signed arithmetic overflow.
305 Example usage, assuming A and B are long int:
307 if (INT_MULTIPLY_OVERFLOW (a, b))
308 printf ("result would overflow\n");
310 printf ("result is %ld (no overflow)\n", a * b);
312 Example usage with WRAPV flavor:
315 bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
316 printf ("result is %ld (%s)\n", result,
317 overflow ? "after overflow" : "no overflow");
319 Restrictions on these macros:
321 These macros do not check for all possible numerical problems or
322 undefined or unspecified behavior: they do not check for division
323 by zero, for bad shift counts, or for shifting negative numbers.
325 These macros may evaluate their arguments zero or multiple times, so the
326 arguments should not have side effects.
328 The WRAPV macros are not constant expressions. They support only
329 +, binary -, and *. The result type must be signed.
331 These macros are tuned for their last argument being a constant.
333 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
334 A % B, and A << B would overflow, respectively. */
336 #define INT_ADD_OVERFLOW(a, b) \
337 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
338 #define INT_SUBTRACT_OVERFLOW(a, b) \
339 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
340 #if _GL_HAS_BUILTIN_OVERFLOW_P
341 # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
343 # define INT_NEGATE_OVERFLOW(a) \
344 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
346 #define INT_MULTIPLY_OVERFLOW(a, b) \
347 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
348 #define INT_DIVIDE_OVERFLOW(a, b) \
349 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
350 #define INT_REMAINDER_OVERFLOW(a, b) \
351 _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
352 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
353 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
354 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
356 /* Return 1 if the expression A <op> B would overflow,
357 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
358 assuming MIN and MAX are the minimum and maximum for the result type.
359 Arguments should be free of side effects. */
360 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
361 op_result_overflow (a, b, \
362 _GL_INT_MINIMUM (0 * (b) + (a)), \
363 _GL_INT_MAXIMUM (0 * (b) + (a)))
365 /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
366 Return 1 if the result overflows. See above for restrictions. */
367 #define INT_ADD_WRAPV(a, b, r) \
368 _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW)
369 #define INT_SUBTRACT_WRAPV(a, b, r) \
370 _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
371 #define INT_MULTIPLY_WRAPV(a, b, r) \
372 _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
374 /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
375 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
376 https://llvm.org/bugs/show_bug.cgi?id=25390
377 For now, assume all versions of GCC-like compilers generate bogus
378 warnings for _Generic. This matters only for older compilers that
379 lack __builtin_add_overflow. */
381 # define _GL__GENERIC_BOGUS 1
383 # define _GL__GENERIC_BOGUS 0
386 /* Store the low-order bits of A <op> B into *R, where OP specifies
387 the operation. BUILTIN is the builtin operation, and OVERFLOW the
388 overflow predicate. Return 1 if the result overflows. See above
390 #if _GL_HAS_BUILTIN_OVERFLOW
391 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
392 #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
393 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
397 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
398 signed char, SCHAR_MIN, SCHAR_MAX), \
400 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
401 short int, SHRT_MIN, SHRT_MAX), \
403 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
404 int, INT_MIN, INT_MAX), \
406 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
407 long int, LONG_MIN, LONG_MAX), \
409 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
410 long long int, LLONG_MIN, LLONG_MAX)))
412 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
413 (sizeof *(r) == sizeof (signed char) \
414 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
415 signed char, SCHAR_MIN, SCHAR_MAX) \
416 : sizeof *(r) == sizeof (short int) \
417 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
418 short int, SHRT_MIN, SHRT_MAX) \
419 : sizeof *(r) == sizeof (int) \
420 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
421 int, INT_MIN, INT_MAX) \
422 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
424 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
425 (sizeof *(r) == sizeof (long int) \
426 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
427 long int, LONG_MIN, LONG_MAX) \
428 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
429 long long int, LLONG_MIN, LLONG_MAX))
431 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
432 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
433 long int, LONG_MIN, LONG_MAX)
437 /* Store the low-order bits of A <op> B into *R, where the operation
438 is given by OP. Use the unsigned type UT for calculation to avoid
439 overflow problems. *R's type is T, with extremal values TMIN and
440 TMAX. T must be a signed integer type. Return 1 if the result
442 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
443 (sizeof ((a) op (b)) < sizeof (t) \
444 ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
445 : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
446 #define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
448 || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
449 || (tmax) < ((a) op (b))) \
450 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 1) \
451 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 0))
453 /* Return A <op> B, where the operation is given by OP. Use the
454 unsigned type UT for calculation to avoid overflow problems.
455 Convert the result to type T without overflow by subtracting TMIN
456 from large values before converting, and adding it afterwards.
457 Compilers can optimize all the operations except OP. */
458 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t, tmin, tmax) \
459 (((ut) (a) op (ut) (b)) <= (tmax) \
460 ? (t) ((ut) (a) op (ut) (b)) \
461 : ((t) (((ut) (a) op (ut) (b)) - (tmin)) + (tmin)))
463 #endif /* _GL_INTPROPS_H */