1 /* intprops.h -- properties of integer types
3 Copyright (C) 2001-2017 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 <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 #ifndef _GL_INTPROPS_H
21 #define _GL_INTPROPS_H
25 /* Return a value with the common real type of E and V and the value of V. */
26 #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
28 /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
29 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
30 #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
32 /* The extra casts in the following macros work around compiler bugs,
33 e.g., in Cray C 5.0.3.0. */
35 /* True if the arithmetic type T is an integer type. bool counts as
37 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
39 /* True if the real type T is signed. */
40 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
42 /* Return 1 if the real expression E, after promotion, has a
43 signed or floating type. */
44 #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
47 /* Minimum and maximum values for integer types and expressions. */
49 /* The width in bits of the integer type or expression T.
50 Padding bits are not supported; this is checked at compile-time below. */
51 #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
53 /* The maximum and minimum values for the integer type T. */
54 #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
55 #define TYPE_MAXIMUM(t) \
56 ((t) (! TYPE_SIGNED (t) \
58 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
60 /* The maximum and minimum values for the type of the expression E,
61 after integer promotion. E should not have side effects. */
62 #define _GL_INT_MINIMUM(e) \
64 ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
65 : _GL_INT_CONVERT (e, 0))
66 #define _GL_INT_MAXIMUM(e) \
68 ? _GL_SIGNED_INT_MAXIMUM (e) \
69 : _GL_INT_NEGATE_CONVERT (e, 1))
70 #define _GL_SIGNED_INT_MAXIMUM(e) \
71 (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
73 /* Work around OpenVMS incompatibility with C99. */
74 #if !defined LLONG_MAX && defined __INT64_MAX
75 # define LLONG_MAX __INT64_MAX
76 # define LLONG_MIN __INT64_MIN
79 /* This include file assumes that signed types are two's complement without
80 padding bits; the above macros have undefined behavior otherwise.
81 If this is a problem for you, please let us know how to fix it for your host.
82 This assumption is tested by the intprops-tests module. */
84 /* Does the __typeof__ keyword work? This could be done by
85 'configure', but for now it's easier to do it by hand. */
87 || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
88 || (0x5110 <= __SUNPRO_C && !__STDC__))
89 # define _GL_HAVE___TYPEOF__ 1
91 # define _GL_HAVE___TYPEOF__ 0
94 /* Return 1 if the integer type or expression T might be signed. Return 0
95 if it is definitely unsigned. This macro does not evaluate its argument,
96 and expands to an integer constant expression. */
97 #if _GL_HAVE___TYPEOF__
98 # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
100 # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
103 /* Bound on length of the string representing an unsigned integer
104 value representable in B bits. log10 (2.0) < 146/485. The
105 smallest value of B where this bound is not tight is 2621. */
106 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
108 /* Bound on length of the string representing an integer type or expression T.
109 Subtract 1 for the sign bit if T is signed, and then add 1 more for
110 a minus sign if needed.
112 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
113 signed, this macro may overestimate the true bound by one byte when
114 applied to unsigned types of size 2, 4, 16, ... bytes. */
115 #define INT_STRLEN_BOUND(t) \
116 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
117 + _GL_SIGNED_TYPE_OR_EXPR (t))
119 /* Bound on buffer size needed to represent an integer type or expression T,
120 including the terminating null. */
121 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
124 /* Range overflow checks.
126 The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
127 operators might not yield numerically correct answers due to
128 arithmetic overflow. They do not rely on undefined or
129 implementation-defined behavior. Their implementations are simple
130 and straightforward, but they are a bit harder to use than the
131 INT_<op>_OVERFLOW macros described below.
137 if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
138 printf ("multiply would overflow");
140 printf ("product is %ld", i * j);
142 Restrictions on *_RANGE_OVERFLOW macros:
144 These macros do not check for all possible numerical problems or
145 undefined or unspecified behavior: they do not check for division
146 by zero, for bad shift counts, or for shifting negative numbers.
148 These macros may evaluate their arguments zero or multiple times,
149 so the arguments should not have side effects. The arithmetic
150 arguments (including the MIN and MAX arguments) must be of the same
151 integer type after the usual arithmetic conversions, and the type
152 must have minimum value MIN and maximum MAX. Unsigned types should
153 use a zero MIN of the proper type.
155 These macros are tuned for constant MIN and MAX. For commutative
156 operations such as A + B, they are also tuned for constant B. */
158 /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
159 See above for restrictions. */
160 #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
162 ? (a) < (min) - (b) \
165 /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
166 See above for restrictions. */
167 #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
169 ? (max) + (b) < (a) \
172 /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
173 See above for restrictions. */
174 #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
179 /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
180 See above for restrictions. Avoid && and || as they tickle
181 bugs in Sun C 5.11 2010/08/13 and other compilers; see
182 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
183 #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
186 ? (a) < (max) / (b) \
189 : (min) / (b) < (a)) \
193 ? (a) < (min) / (b) \
194 : (max) / (b) < (a)))
196 /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
197 See above for restrictions. Do not check for division by zero. */
198 #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
199 ((min) < 0 && (b) == -1 && (a) < - (max))
201 /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
202 See above for restrictions. Do not check for division by zero.
203 Mathematically, % should never overflow, but on x86-like hosts
204 INT_MIN % -1 traps, and the C standard permits this, so treat this
205 as an overflow too. */
206 #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
207 INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
209 /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
210 See above for restrictions. Here, MIN and MAX are for A only, and B need
211 not be of the same type as the other arguments. The C standard says that
212 behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
213 A is negative then A << B has undefined behavior and A >> B has
214 implementation-defined behavior, but do not check these other
216 #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
218 ? (a) < (min) >> (b) \
219 : (max) >> (b) < (a))
221 /* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
222 #if 5 <= __GNUC__ && !defined __ICC
223 # define _GL_HAS_BUILTIN_OVERFLOW 1
225 # define _GL_HAS_BUILTIN_OVERFLOW 0
228 /* True if __builtin_add_overflow_p (A, B, C) works. */
229 #define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
231 /* The _GL*_OVERFLOW macros have the same restrictions as the
232 *_RANGE_OVERFLOW macros, except that they do not assume that operands
233 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
234 that the result (e.g., A + B) has that type. */
235 #if _GL_HAS_BUILTIN_OVERFLOW_P
236 # define _GL_ADD_OVERFLOW(a, b, min, max) \
237 __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
238 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
239 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
240 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
241 __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
243 # define _GL_ADD_OVERFLOW(a, b, min, max) \
244 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
245 : (a) < 0 ? (b) <= (a) + (b) \
246 : (b) < 0 ? (a) <= (a) + (b) \
248 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
249 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
251 : (b) < 0 ? (a) - (b) <= (a) \
253 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
254 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
255 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
257 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
258 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
259 : (a) < 0 ? (b) <= (a) + (b) - 1 \
260 : (b) < 0 && (a) + (b) <= (a))
261 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
262 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
263 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
264 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
266 /* Return a nonzero value if A is a mathematical multiple of B, where
267 A is unsigned, B is negative, and MAX is the maximum value of A's
268 type. A's type must be the same as (A % B)'s type. Normally (A %
269 -B == 0) suffices, but things get tricky if -B would overflow. */
270 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
271 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
272 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
274 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
278 /* Check for integer overflow, and report low order bits of answer.
280 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
281 might not yield numerically correct answers due to arithmetic overflow.
282 The INT_<op>_WRAPV macros also store the low-order bits of the answer.
283 These macros work correctly on all known practical hosts, and do not rely
284 on undefined behavior due to signed arithmetic overflow.
286 Example usage, assuming A and B are long int:
288 if (INT_MULTIPLY_OVERFLOW (a, b))
289 printf ("result would overflow\n");
291 printf ("result is %ld (no overflow)\n", a * b);
293 Example usage with WRAPV flavor:
296 bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
297 printf ("result is %ld (%s)\n", result,
298 overflow ? "after overflow" : "no overflow");
300 Restrictions on these macros:
302 These macros do not check for all possible numerical problems or
303 undefined or unspecified behavior: they do not check for division
304 by zero, for bad shift counts, or for shifting negative numbers.
306 These macros may evaluate their arguments zero or multiple times, so the
307 arguments should not have side effects.
309 The WRAPV macros are not constant expressions. They support only
310 +, binary -, and *. The result type must be signed.
312 These macros are tuned for their last argument being a constant.
314 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
315 A % B, and A << B would overflow, respectively. */
317 #define INT_ADD_OVERFLOW(a, b) \
318 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
319 #define INT_SUBTRACT_OVERFLOW(a, b) \
320 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
321 #if _GL_HAS_BUILTIN_OVERFLOW_P
322 # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
324 # define INT_NEGATE_OVERFLOW(a) \
325 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
327 #define INT_MULTIPLY_OVERFLOW(a, b) \
328 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
329 #define INT_DIVIDE_OVERFLOW(a, b) \
330 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
331 #define INT_REMAINDER_OVERFLOW(a, b) \
332 _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
333 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
334 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
335 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
337 /* Return 1 if the expression A <op> B would overflow,
338 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
339 assuming MIN and MAX are the minimum and maximum for the result type.
340 Arguments should be free of side effects. */
341 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
342 op_result_overflow (a, b, \
343 _GL_INT_MINIMUM (0 * (b) + (a)), \
344 _GL_INT_MAXIMUM (0 * (b) + (a)))
346 /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
347 Return 1 if the result overflows. See above for restrictions. */
348 #define INT_ADD_WRAPV(a, b, r) \
349 _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW)
350 #define INT_SUBTRACT_WRAPV(a, b, r) \
351 _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
352 #define INT_MULTIPLY_WRAPV(a, b, r) \
353 _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
355 /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
356 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
357 https://llvm.org/bugs/show_bug.cgi?id=25390
358 For now, assume all versions of GCC-like compilers generate bogus
359 warnings for _Generic. This matters only for older compilers that
360 lack __builtin_add_overflow. */
362 # define _GL__GENERIC_BOGUS 1
364 # define _GL__GENERIC_BOGUS 0
367 /* Store the low-order bits of A <op> B into *R, where OP specifies
368 the operation. BUILTIN is the builtin operation, and OVERFLOW the
369 overflow predicate. Return 1 if the result overflows. See above
371 #if _GL_HAS_BUILTIN_OVERFLOW
372 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
373 #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
374 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
378 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
379 signed char, SCHAR_MIN, SCHAR_MAX), \
381 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
382 short int, SHRT_MIN, SHRT_MAX), \
384 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
385 int, INT_MIN, INT_MAX), \
387 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
388 long int, LONG_MIN, LONG_MAX), \
390 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
391 long long int, LLONG_MIN, LLONG_MAX)))
393 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
394 (sizeof *(r) == sizeof (signed char) \
395 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
396 signed char, SCHAR_MIN, SCHAR_MAX) \
397 : sizeof *(r) == sizeof (short int) \
398 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
399 short int, SHRT_MIN, SHRT_MAX) \
400 : sizeof *(r) == sizeof (int) \
401 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
402 int, INT_MIN, INT_MAX) \
403 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
405 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
406 (sizeof *(r) == sizeof (long int) \
407 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
408 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_LONGISH(a, b, r, op, overflow) \
413 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
414 long int, LONG_MIN, LONG_MAX)
418 /* Store the low-order bits of A <op> B into *R, where the operation
419 is given by OP. Use the unsigned type UT for calculation to avoid
420 overflow problems. *R's type is T, with extrema TMIN and TMAX.
421 T must be a signed integer type. Return 1 if the result overflows. */
422 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
423 (sizeof ((a) op (b)) < sizeof (t) \
424 ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
425 : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
426 #define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
428 || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
429 || (tmax) < ((a) op (b))) \
430 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
431 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
433 /* Return the low-order bits of A <op> B, where the operation is given
434 by OP. Use the unsigned type UT for calculation to avoid undefined
435 behavior on signed integer overflow, and convert the result to type T.
436 UT is at least as wide as T and is no narrower than unsigned int,
437 T is two's complement, and there is no padding or trap representations.
438 Assume that converting UT to T yields the low-order bits, as is
439 done in all known two's-complement C compilers. E.g., see:
440 https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
442 According to the C standard, converting UT to T yields an
443 implementation-defined result or signal for values outside T's
444 range. However, code that works around this theoretical problem
445 runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
446 https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
447 As the compiler bug is real, don't try to work around the
448 theoretical problem. */
450 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
451 ((t) ((ut) (a) op (ut) (b)))
453 #endif /* _GL_INTPROPS_H */