Update from gnulib
[emacs.git] / lib / intprops.h
blobe1fce5c96a1e9b7ed215822c98e834552e25dfb7
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
23 #include <limits.h>
24 #include <verify.h>
26 /* Return a value with the common real type of E and V and the value of V. */
27 #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
29 /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
30 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */
31 #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
33 /* The extra casts in the following macros work around compiler bugs,
34 e.g., in Cray C 5.0.3.0. */
36 /* True if the arithmetic type T is an integer type. bool counts as
37 an integer. */
38 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
40 /* True if the real type T is signed. */
41 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
43 /* Return 1 if the real expression E, after promotion, has a
44 signed or floating type. */
45 #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
48 /* Minimum and maximum values for integer types and expressions. */
50 /* The maximum and minimum values for the integer type T. */
51 #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
52 #define TYPE_MAXIMUM(t) \
53 ((t) (! TYPE_SIGNED (t) \
54 ? (t) -1 \
55 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
57 /* The maximum and minimum values for the type of the expression E,
58 after integer promotion. E should not have side effects. */
59 #define _GL_INT_MINIMUM(e) \
60 (EXPR_SIGNED (e) \
61 ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
62 : _GL_INT_CONVERT (e, 0))
63 #define _GL_INT_MAXIMUM(e) \
64 (EXPR_SIGNED (e) \
65 ? _GL_SIGNED_INT_MAXIMUM (e) \
66 : _GL_INT_NEGATE_CONVERT (e, 1))
67 #define _GL_SIGNED_INT_MAXIMUM(e) \
68 (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1)
70 /* This include file assumes that signed types are two's complement without
71 padding bits; the above macros have undefined behavior otherwise.
72 If this is a problem for you, please let us know how to fix it for your host.
73 As a sanity check, test the assumption for some signed types that
74 <limits.h> bounds. */
75 verify (TYPE_MINIMUM (signed char) == SCHAR_MIN);
76 verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
77 verify (TYPE_MINIMUM (short int) == SHRT_MIN);
78 verify (TYPE_MAXIMUM (short int) == SHRT_MAX);
79 verify (TYPE_MINIMUM (int) == INT_MIN);
80 verify (TYPE_MAXIMUM (int) == INT_MAX);
81 verify (TYPE_MINIMUM (long int) == LONG_MIN);
82 verify (TYPE_MAXIMUM (long int) == LONG_MAX);
83 #ifdef LLONG_MAX
84 verify (TYPE_MINIMUM (long long int) == LLONG_MIN);
85 verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
86 #endif
88 /* Does the __typeof__ keyword work? This could be done by
89 'configure', but for now it's easier to do it by hand. */
90 #if (2 <= __GNUC__ || defined __IBM__TYPEOF__ \
91 || (0x5110 <= __SUNPRO_C && !__STDC__))
92 # define _GL_HAVE___TYPEOF__ 1
93 #else
94 # define _GL_HAVE___TYPEOF__ 0
95 #endif
97 /* Return 1 if the integer type or expression T might be signed. Return 0
98 if it is definitely unsigned. This macro does not evaluate its argument,
99 and expands to an integer constant expression. */
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
106 /* Bound on length of the string representing an unsigned integer
107 value representable in B bits. log10 (2.0) < 146/485. The
108 smallest value of B where this bound is not tight is 2621. */
109 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
111 /* Bound on length of the string representing an integer type or expression T.
112 Subtract 1 for the sign bit if T is signed, and then add 1 more for
113 a minus sign if needed.
115 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
116 signed, this macro may overestimate the true bound by one byte when
117 applied to unsigned types of size 2, 4, 16, ... bytes. */
118 #define INT_STRLEN_BOUND(t) \
119 (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \
120 - _GL_SIGNED_TYPE_OR_EXPR (t)) \
121 + _GL_SIGNED_TYPE_OR_EXPR (t))
123 /* Bound on buffer size needed to represent an integer type or expression T,
124 including the terminating null. */
125 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
128 /* Range overflow checks.
130 The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
131 operators might not yield numerically correct answers due to
132 arithmetic overflow. They do not rely on undefined or
133 implementation-defined behavior. Their implementations are simple
134 and straightforward, but they are a bit harder to use than the
135 INT_<op>_OVERFLOW macros described below.
137 Example usage:
139 long int i = ...;
140 long int j = ...;
141 if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
142 printf ("multiply would overflow");
143 else
144 printf ("product is %ld", i * j);
146 Restrictions on *_RANGE_OVERFLOW macros:
148 These macros do not check for all possible numerical problems or
149 undefined or unspecified behavior: they do not check for division
150 by zero, for bad shift counts, or for shifting negative numbers.
152 These macros may evaluate their arguments zero or multiple times,
153 so the arguments should not have side effects. The arithmetic
154 arguments (including the MIN and MAX arguments) must be of the same
155 integer type after the usual arithmetic conversions, and the type
156 must have minimum value MIN and maximum MAX. Unsigned types should
157 use a zero MIN of the proper type.
159 These macros are tuned for constant MIN and MAX. For commutative
160 operations such as A + B, they are also tuned for constant B. */
162 /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
163 See above for restrictions. */
164 #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
165 ((b) < 0 \
166 ? (a) < (min) - (b) \
167 : (max) - (b) < (a))
169 /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
170 See above for restrictions. */
171 #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
172 ((b) < 0 \
173 ? (max) + (b) < (a) \
174 : (a) < (min) + (b))
176 /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
177 See above for restrictions. */
178 #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
179 ((min) < 0 \
180 ? (a) < - (max) \
181 : 0 < (a))
183 /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
184 See above for restrictions. Avoid && and || as they tickle
185 bugs in Sun C 5.11 2010/08/13 and other compilers; see
186 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
187 #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
188 ((b) < 0 \
189 ? ((a) < 0 \
190 ? (a) < (max) / (b) \
191 : (b) == -1 \
192 ? 0 \
193 : (min) / (b) < (a)) \
194 : (b) == 0 \
195 ? 0 \
196 : ((a) < 0 \
197 ? (a) < (min) / (b) \
198 : (max) / (b) < (a)))
200 /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
201 See above for restrictions. Do not check for division by zero. */
202 #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
203 ((min) < 0 && (b) == -1 && (a) < - (max))
205 /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
206 See above for restrictions. Do not check for division by zero.
207 Mathematically, % should never overflow, but on x86-like hosts
208 INT_MIN % -1 traps, and the C standard permits this, so treat this
209 as an overflow too. */
210 #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
211 INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
213 /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
214 See above for restrictions. Here, MIN and MAX are for A only, and B need
215 not be of the same type as the other arguments. The C standard says that
216 behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
217 A is negative then A << B has undefined behavior and A >> B has
218 implementation-defined behavior, but do not check these other
219 restrictions. */
220 #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
221 ((a) < 0 \
222 ? (a) < (min) >> (b) \
223 : (max) >> (b) < (a))
225 /* True if __builtin_add_overflow (A, B, P) works when P is null. */
226 #define _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL (7 <= __GNUC__)
228 /* The _GL*_OVERFLOW macros have the same restrictions as the
229 *_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
231 that the result (e.g., A + B) has that type. */
232 #if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
233 # define _GL_ADD_OVERFLOW(a, b, min, max)
234 __builtin_add_overflow (a, b, (__typeof__ ((a) + (b)) *) 0)
235 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max)
236 __builtin_sub_overflow (a, b, (__typeof__ ((a) - (b)) *) 0)
237 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max)
238 __builtin_mul_overflow (a, b, (__typeof__ ((a) * (b)) *) 0)
239 #else
240 # define _GL_ADD_OVERFLOW(a, b, min, max) \
241 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
242 : (a) < 0 ? (b) <= (a) + (b) \
243 : (b) < 0 ? (a) <= (a) + (b) \
244 : (a) + (b) < (b))
245 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
246 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
247 : (a) < 0 ? 1 \
248 : (b) < 0 ? (a) - (b) <= (a) \
249 : (a) < (b))
250 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
251 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
252 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
253 #endif
254 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
255 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
256 : (a) < 0 ? (b) <= (a) + (b) - 1 \
257 : (b) < 0 && (a) + (b) <= (a))
258 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
259 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
260 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
261 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
263 /* Return a nonzero value if A is a mathematical multiple of B, where
264 A is unsigned, B is negative, and MAX is the maximum value of A's
265 type. A's type must be the same as (A % B)'s type. Normally (A %
266 -B == 0) suffices, but things get tricky if -B would overflow. */
267 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
268 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
269 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
270 ? (a) \
271 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
272 : (a) % - (b)) \
273 == 0)
275 /* Check for integer overflow, and report low order bits of answer.
277 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
278 might not yield numerically correct answers due to arithmetic overflow.
279 The INT_<op>_WRAPV macros also store the low-order bits of the answer.
280 These macros work correctly on all known practical hosts, and do not rely
281 on undefined behavior due to signed arithmetic overflow.
283 Example usage, assuming A and B are long int:
285 if (INT_MULTIPLY_OVERFLOW (a, b))
286 printf ("result would overflow\n");
287 else
288 printf ("result is %ld (no overflow)\n", a * b);
290 Example usage with WRAPV flavor:
292 long int result;
293 bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
294 printf ("result is %ld (%s)\n", result,
295 overflow ? "after overflow" : "no overflow");
297 Restrictions on these macros:
299 These macros do not check for all possible numerical problems or
300 undefined or unspecified behavior: they do not check for division
301 by zero, for bad shift counts, or for shifting negative numbers.
303 These macros may evaluate their arguments zero or multiple times, so the
304 arguments should not have side effects.
306 The WRAPV macros are not constant expressions. They support only
307 +, binary -, and *. The result type must be signed.
309 These macros are tuned for their last argument being a constant.
311 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
312 A % B, and A << B would overflow, respectively. */
314 #define INT_ADD_OVERFLOW(a, b) \
315 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
316 #define INT_SUBTRACT_OVERFLOW(a, b) \
317 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
318 #if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
319 # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
320 #else
321 # define INT_NEGATE_OVERFLOW(a) \
322 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
323 #endif
324 #define INT_MULTIPLY_OVERFLOW(a, b) \
325 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
326 #define INT_DIVIDE_OVERFLOW(a, b) \
327 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
328 #define INT_REMAINDER_OVERFLOW(a, b) \
329 _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
330 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
331 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
332 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
334 /* Return 1 if the expression A <op> B would overflow,
335 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
336 assuming MIN and MAX are the minimum and maximum for the result type.
337 Arguments should be free of side effects. */
338 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
339 op_result_overflow (a, b, \
340 _GL_INT_MINIMUM (0 * (b) + (a)), \
341 _GL_INT_MAXIMUM (0 * (b) + (a)))
343 /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
344 Return 1 if the result overflows. See above for restrictions. */
345 #define INT_ADD_WRAPV(a, b, r) \
346 _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW)
347 #define INT_SUBTRACT_WRAPV(a, b, r) \
348 _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
349 #define INT_MULTIPLY_WRAPV(a, b, r) \
350 _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
352 #ifndef __has_builtin
353 # define __has_builtin(x) 0
354 #endif
356 /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
357 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
358 https://llvm.org/bugs/show_bug.cgi?id=25390
359 For now, assume all versions of GCC-like compilers generate bogus
360 warnings for _Generic. This matters only for older compilers that
361 lack __builtin_add_overflow. */
362 #if __GNUC__
363 # define _GL__GENERIC_BOGUS 1
364 #else
365 # define _GL__GENERIC_BOGUS 0
366 #endif
368 /* Store the low-order bits of A <op> B into *R, where OP specifies
369 the operation. BUILTIN is the builtin operation, and OVERFLOW the
370 overflow predicate. Return 1 if the result overflows. See above
371 for restrictions. */
372 #if 5 <= __GNUC__ || __has_builtin (__builtin_add_overflow)
373 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
374 #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
375 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
376 (_Generic \
377 (*(r), \
378 signed char: \
379 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
380 signed char, SCHAR_MIN, SCHAR_MAX), \
381 short int: \
382 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
383 short int, SHRT_MIN, SHRT_MAX), \
384 int: \
385 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
386 int, INT_MIN, INT_MAX), \
387 long int: \
388 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
389 long int, LONG_MIN, LONG_MAX), \
390 long long int: \
391 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
392 long long int, LLONG_MIN, LLONG_MAX)))
393 #else
394 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
395 (sizeof *(r) == sizeof (signed char) \
396 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
397 signed char, SCHAR_MIN, SCHAR_MAX) \
398 : sizeof *(r) == sizeof (short int) \
399 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
400 short int, SHRT_MIN, SHRT_MAX) \
401 : sizeof *(r) == sizeof (int) \
402 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
403 int, INT_MIN, INT_MAX) \
404 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
405 # ifdef LLONG_MAX
406 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
407 (sizeof *(r) == sizeof (long int) \
408 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
409 long int, LONG_MIN, LONG_MAX) \
410 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
411 long long int, LLONG_MIN, LLONG_MAX))
412 # else
413 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
414 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
415 long int, LONG_MIN, LONG_MAX))
416 # endif
417 #endif
419 /* Store the low-order bits of A <op> B into *R, where the operation
420 is given by OP. Use the unsigned type UT for calculation to avoid
421 overflow problems. *R's type is T, with extremal values TMIN and
422 TMAX. T must be a signed integer type. Return 1 if the result
423 overflows. */
424 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
425 (sizeof ((a) op (b)) < sizeof (t) \
426 ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
427 : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
428 #define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
429 ((overflow (a, b) \
430 || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
431 || (tmax) < ((a) op (b))) \
432 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 1) \
433 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 0))
435 /* Return A <op> B, where the operation is given by OP. Use the
436 unsigned type UT for calculation to avoid overflow problems.
437 Convert the result to type T without overflow by subtracting TMIN
438 from large values before converting, and adding it afterwards.
439 Compilers can optimize all the operations except OP. */
440 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t, tmin, tmax) \
441 (((ut) (a) op (ut) (b)) <= (tmax) \
442 ? (t) ((ut) (a) op (ut) (b)) \
443 : ((t) (((ut) (a) op (ut) (b)) - (tmin)) + (tmin)))
445 #endif /* _GL_INTPROPS_H */