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