aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert2015-10-30 19:21:29 -0700
committerPaul Eggert2015-10-30 19:21:51 -0700
commitb059c6b584e964296d425667642650f42972c238 (patch)
tree6fb75417015f334689101115595ebad2e0d38ca1 /lib
parente11aaee266da52937a3a031cb108fe13f68958c3 (diff)
downloademacs-b059c6b584e964296d425667642650f42972c238.tar.gz
emacs-b059c6b584e964296d425667642650f42972c238.zip
Merge from gnulib.
This incorporates: 2015-10-30 intprops: add WRAPV and const flavors for GCC 5 2015-10-25 stdalign: port to Sun C 5.9 * doc/misc/texinfo.tex, lib/intprops.h, lib/stdalign.in.h: Copy from gnulib.
Diffstat (limited to 'lib')
-rw-r--r--lib/intprops.h141
-rw-r--r--lib/stdalign.in.h2
2 files changed, 130 insertions, 13 deletions
diff --git a/lib/intprops.h b/lib/intprops.h
index f85ccade4e1..4441f1c294e 100644
--- a/lib/intprops.h
+++ b/lib/intprops.h
@@ -263,22 +263,31 @@
263 : (a) % - (b)) \ 263 : (a) % - (b)) \
264 == 0) 264 == 0)
265 265
266 266/* Check for integer overflow, and report low order bits of answer.
267/* Integer overflow checks.
268 267
269 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators 268 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
270 might not yield numerically correct answers due to arithmetic overflow. 269 might not yield numerically correct answers due to arithmetic overflow.
271 They work correctly on all known practical hosts, and do not rely 270 The INT_<op>_WRAPV macros return the low-order bits of the answer.
271 For example, INT_ADD_WRAPV (INT_MAX, 1) returns INT_MIN on a two's
272 complement host, even if INT_MAX + 1 would trap.
273
274 These macros work correctly on all known practical hosts, and do not rely
272 on undefined behavior due to signed arithmetic overflow. 275 on undefined behavior due to signed arithmetic overflow.
273 276
274 Example usage: 277 Example usage:
275 278
276 long int i = ...; 279 long int a = ...;
277 long int j = ...; 280 long int b = ...;
278 if (INT_MULTIPLY_OVERFLOW (i, j)) 281 long int result = INT_MULTIPLY_WRAPV (a, b);
279 printf ("multiply would overflow"); 282 printf ("result is %ld (%s)\n", result,
280 else 283 INT_MULTIPLY_OVERFLOW (a, b) ? "after overflow" : "no overflow");
281 printf ("product is %ld", i * j); 284
285 enum {
286 INT_PRODUCTS_FIT_IN_LONG
287 = ! INT_CONST_MULTIPLY_OVERFLOW ((long int) INT_MIN, INT_MIN)
288 };
289
290 Restrictions on these macros:
282 291
283 These macros do not check for all possible numerical problems or 292 These macros do not check for all possible numerical problems or
284 undefined or unspecified behavior: they do not check for division 293 undefined or unspecified behavior: they do not check for division
@@ -287,18 +296,35 @@
287 These macros may evaluate their arguments zero or multiple times, so the 296 These macros may evaluate their arguments zero or multiple times, so the
288 arguments should not have side effects. 297 arguments should not have side effects.
289 298
299 On non-GCC-compatible compilers that do not support C11, the type
300 of INT_<op>_WRAPV (A, B) might differ from the native type of (A op
301 B), so it is wise to convert the result to the native type. Such a
302 conversion is safe and cannot trap.
303
304 For runtime efficiency GCC 5 and later has builtin functions for +,
305 -, * when doing integer overflow checking or wraparound arithmetic.
306 Unfortunately, these builtins require nonnull pointer arguments and
307 so cannot be used in constant expressions; see GCC bug 68120
308 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68120>. In constant
309 expressions, use the macros INT_CONST_ADD_OVERFLOW and
310 INT_CONST_ADD_WRAPV instead, and similarly for SUBTRACT and
311 MULTIPLY; these macros avoid the builtins and are slower in
312 non-constant expressions. Perhaps someday GCC's API for overflow
313 checking will be improved and we can remove the need for the
314 INT_CONST_ variants.
315
290 These macros are tuned for their last argument being a constant. 316 These macros are tuned for their last argument being a constant.
291 317
292 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, 318 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
293 A % B, and A << B would overflow, respectively. */ 319 A % B, and A << B would overflow, respectively. */
294 320
295#define INT_ADD_OVERFLOW(a, b) \ 321#define INT_CONST_ADD_OVERFLOW(a, b) \
296 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) 322 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
297#define INT_SUBTRACT_OVERFLOW(a, b) \ 323#define INT_CONST_SUBTRACT_OVERFLOW(a, b) \
298 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) 324 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
299#define INT_NEGATE_OVERFLOW(a) \ 325#define INT_NEGATE_OVERFLOW(a) \
300 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) 326 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
301#define INT_MULTIPLY_OVERFLOW(a, b) \ 327#define INT_CONST_MULTIPLY_OVERFLOW(a, b) \
302 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) 328 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
303#define INT_DIVIDE_OVERFLOW(a, b) \ 329#define INT_DIVIDE_OVERFLOW(a, b) \
304 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW) 330 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
@@ -317,4 +343,95 @@
317 _GL_INT_MINIMUM (0 * (b) + (a)), \ 343 _GL_INT_MINIMUM (0 * (b) + (a)), \
318 _GL_INT_MAXIMUM (0 * (b) + (a))) 344 _GL_INT_MAXIMUM (0 * (b) + (a)))
319 345
346/* Return the low order bits of the integer expressions
347 A * B, A - B, -A, A * B, A / B, A % B, and A << B, respectively.
348 See above for restrictions. */
349#define INT_CONST_ADD_WRAPV(a, b) _GL_INT_OP_WRAPV (a, b, +)
350#define INT_CONST_SUBTRACT_WRAPV(a, b) _GL_INT_OP_WRAPV (a, b, -)
351#define INT_NEGATE_WRAPV(a) INT_CONST_SUBTRACT_WRAPV (0, a)
352#define INT_CONST_MULTIPLY_WRAPV(a, b) _GL_INT_OP_WRAPV (a, b, *)
353#define INT_DIVIDE_WRAPV(a, b) \
354 (INT_DIVIDE_OVERFLOW(a, b) ? INT_NEGATE_WRAPV (a) : (a) / (b))
355#define INT_REMAINDER_WRAPV(a, b) \
356 (INT_REMAINDER_OVERFLOW(a, b) ? 0 : (a) % (b))
357#define INT_LEFT_SHIFT_WRAPV(a, b) _GL_INT_OP_WRAPV (a, b, <<)
358
359/* Return the low order bits of A <op> B, where OP specifies the operation.
360 See above for restrictions. */
361#if !_GL_HAVE___TYPEOF__ && 201112 <= __STDC_VERSION__
362# define _GL_INT_OP_WRAPV(a, b, op) \
363 _Generic ((a) op (b), \
364 int: _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, int), \
365 long int: _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, long int), \
366 long long int: _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, \
367 long long int), \
368 default: (a) op (b))
369#else
370# define _GL_INT_OP_WRAPV(a, b, op) \
371 (! _GL_INT_SIGNED ((0 * (a)) op (0 * (b))) \
372 ? ((a) op (b)) \
373 : _GL_EXPR_CAST ((a) op (b), \
374 (sizeof ((a) op (b)) <= sizeof (int) \
375 ? _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, int) \
376 : _GL_INT_OP_WRAPV_LONGISH (a, b, op))))
377
378/* Cast to E's type the value of V if possible. Yield V as-is otherwise. */
379# if _GL_HAVE___TYPEOF__
380# define _GL_EXPR_CAST(e, v) ((__typeof__ (e)) (v))
381# else
382# define _GL_EXPR_CAST(e, v) (v)
383# endif
384
385# ifdef LLONG_MAX
386# define _GL_INT_OP_WRAPV_LONGISH(a, b, op) \
387 (sizeof ((a) op (b)) <= sizeof (long int) \
388 ? _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, long int) \
389 : _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, long long int))
390# else
391# define _GL_INT_OP_WRAPV_LONGISH(a, b, op) \
392 _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, long int)
393# endif
394#endif
395
396/* Return A <op> B, where the operation is given by OP and the result
397 type is T. T is a signed integer type that is at least as wide as int.
398 Do arithmetic using 'unsigned T' to avoid signed integer overflow.
399 Subtract TYPE_MINIMUM (T) before converting back to T, and add it
400 back afterwards, to avoid signed overflow during conversion. */
401#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, t) \
402 ((unsigned t) (a) op (unsigned t) (b) <= TYPE_MAXIMUM (t) \
403 ? (t) ((unsigned t) (a) op (unsigned t) (b)) \
404 : ((t) ((unsigned t) (a) op (unsigned t) (b) - TYPE_MINIMUM (t)) \
405 + TYPE_MINIMUM (t)))
406
407/* Calls to the INT_<op>_<result> macros are like their INT_CONST_<op>_<result>
408 counterparts, except they are faster with GCC 5 or later, and they
409 are not constant expressions due to limitations in the GNU C API. */
410
411#define INT_ADD_OVERFLOW(a, b) \
412 _GL_OP_OVERFLOW (a, b, INT_CONST_ADD_OVERFLOW, __builtin_add_overflow)
413#define INT_SUBTRACT_OVERFLOW(a, b) \
414 _GL_OP_OVERFLOW (a, b, INT_CONST_SUBTRACT_OVERFLOW, __builtin_sub_overflow)
415#define INT_MULTIPLY_OVERFLOW(a, b) \
416 _GL_OP_OVERFLOW (a, b, INT_CONST_MULTIPLY_OVERFLOW, __builtin_mul_overflow)
417
418#define INT_ADD_WRAPV(a, b) \
419 _GL_OP_WRAPV (a, b, INT_CONST_ADD_WRAPV, __builtin_add_overflow)
420#define INT_SUBTRACT_WRAPV(a, b) \
421 _GL_OP_WRAPV (a, b, INT_CONST_SUBTRACT_WRAPV, __builtin_sub_overflow)
422#define INT_MULTIPLY_WRAPV(a, b) \
423 _GL_OP_WRAPV (a, b, INT_CONST_MULTIPLY_WRAPV, __builtin_mul_overflow)
424
425#if __GNUC__ < 5
426# define _GL_OP_OVERFLOW(a, b, portable, builtin) portable (a, b)
427# define _GL_OP_WRAPV(a, b, portable, builtin) portable (a, b)
428#else
429# define _GL_OP_OVERFLOW(a, b, portable, builtin) \
430 builtin (a, b, &(__typeof__ ((a) + (b))) {0})
431# define _GL_OP_WRAPV(a, b, portable, builtin) \
432 _GL_OP_WRAPV_GENSYM(a, b, builtin, __gl_wrapv##__COUNTER__)
433# define _GL_OP_WRAPV_GENSYM(a, b, builtin, r) \
434 ({__typeof__ ((a) + (b)) r; builtin (a, b, &r); r; })
435#endif
436
320#endif /* _GL_INTPROPS_H */ 437#endif /* _GL_INTPROPS_H */
diff --git a/lib/stdalign.in.h b/lib/stdalign.in.h
index ce1e793e273..b16ccc86998 100644
--- a/lib/stdalign.in.h
+++ b/lib/stdalign.in.h
@@ -104,7 +104,7 @@
104 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \ 104 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
105 : __GNUC__) \ 105 : __GNUC__) \
106 || __HP_cc || __HP_aCC || __IBMC__ || __IBMCPP__ \ 106 || __HP_cc || __HP_aCC || __IBMC__ || __IBMCPP__ \
107 || __ICC || 0x5110 <= __SUNPRO_C) 107 || __ICC || 0x590 <= __SUNPRO_C)
108# define _Alignas(a) __attribute__ ((__aligned__ (a))) 108# define _Alignas(a) __attribute__ ((__aligned__ (a)))
109# elif 1300 <= _MSC_VER 109# elif 1300 <= _MSC_VER
110# define _Alignas(a) __declspec (align (a)) 110# define _Alignas(a) __declspec (align (a))