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