2011-01-09 01:20:28 +00:00
|
|
|
/* intprops.h -- properties of integer types
|
|
|
|
|
2023-01-01 10:31:12 +00:00
|
|
|
Copyright (C) 2001-2023 Free Software Foundation, Inc.
|
2011-01-09 01:20:28 +00:00
|
|
|
|
2016-03-10 15:55:38 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify it
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
under the terms of the GNU Lesser General Public License as published
|
|
|
|
by the Free Software Foundation; either version 2.1 of the License, or
|
2011-01-09 01:20:28 +00:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
GNU Lesser General Public License for more details.
|
2011-01-09 01:20:28 +00:00
|
|
|
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2017-09-13 09:07:03 +00:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2011-01-09 01:20:28 +00:00
|
|
|
|
2011-05-16 00:51:54 +00:00
|
|
|
#ifndef _GL_INTPROPS_H
|
|
|
|
#define _GL_INTPROPS_H
|
2011-01-09 01:20:28 +00:00
|
|
|
|
2022-08-18 01:50:06 +00:00
|
|
|
#include "intprops-internal.h"
|
2011-05-22 21:02:48 +00:00
|
|
|
|
2011-01-09 01:20:28 +00:00
|
|
|
/* The extra casts in the following macros work around compiler bugs,
|
|
|
|
e.g., in Cray C 5.0.3.0. */
|
|
|
|
|
|
|
|
/* True if the arithmetic type T is an integer type. bool counts as
|
|
|
|
an integer. */
|
2011-05-16 00:51:54 +00:00
|
|
|
#define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
|
2011-01-09 01:20:28 +00:00
|
|
|
|
2015-11-10 20:42:06 +00:00
|
|
|
/* True if the real type T is signed. */
|
2022-08-18 01:50:06 +00:00
|
|
|
#define TYPE_SIGNED(t) _GL_TYPE_SIGNED (t)
|
2011-05-16 00:51:54 +00:00
|
|
|
|
2015-11-10 20:42:06 +00:00
|
|
|
/* Return 1 if the real expression E, after promotion, has a
|
2018-08-28 01:59:46 +00:00
|
|
|
signed or floating type. Do not evaluate E. */
|
2022-08-18 01:50:06 +00:00
|
|
|
#define EXPR_SIGNED(e) _GL_EXPR_SIGNED (e)
|
2011-01-09 01:20:28 +00:00
|
|
|
|
2011-05-16 00:51:54 +00:00
|
|
|
|
2016-05-02 01:36:38 +00:00
|
|
|
/* Minimum and maximum values for integer types and expressions. */
|
2011-05-16 00:51:54 +00:00
|
|
|
|
2016-09-15 18:50:23 +00:00
|
|
|
/* The width in bits of the integer type or expression T.
|
2020-08-23 21:09:48 +00:00
|
|
|
Do not evaluate T. T must not be a bit-field expression.
|
2016-09-15 18:50:23 +00:00
|
|
|
Padding bits are not supported; this is checked at compile-time below. */
|
2022-08-18 01:50:06 +00:00
|
|
|
#define TYPE_WIDTH(t) _GL_TYPE_WIDTH (t)
|
2016-09-15 18:50:23 +00:00
|
|
|
|
2011-05-16 00:51:54 +00:00
|
|
|
/* The maximum and minimum values for the integer type T. */
|
2016-05-02 01:36:38 +00:00
|
|
|
#define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
|
2011-05-16 00:51:54 +00:00
|
|
|
#define TYPE_MAXIMUM(t) \
|
|
|
|
((t) (! TYPE_SIGNED (t) \
|
|
|
|
? (t) -1 \
|
2016-09-15 18:50:23 +00:00
|
|
|
: ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
|
2011-01-09 01:20:28 +00:00
|
|
|
|
|
|
|
/* Bound on length of the string representing an unsigned integer
|
|
|
|
value representable in B bits. log10 (2.0) < 146/485. The
|
|
|
|
smallest value of B where this bound is not tight is 2621. */
|
2011-05-16 00:51:54 +00:00
|
|
|
#define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
|
2011-01-09 01:20:28 +00:00
|
|
|
|
|
|
|
/* Bound on length of the string representing an integer type or expression T.
|
2020-08-23 21:09:48 +00:00
|
|
|
T must not be a bit-field expression.
|
|
|
|
|
2011-01-09 01:20:28 +00:00
|
|
|
Subtract 1 for the sign bit if T is signed, and then add 1 more for
|
2011-05-16 00:51:54 +00:00
|
|
|
a minus sign if needed.
|
|
|
|
|
2019-08-15 01:13:27 +00:00
|
|
|
Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is
|
|
|
|
unsigned, this macro may overestimate the true bound by one byte when
|
2011-05-16 00:51:54 +00:00
|
|
|
applied to unsigned types of size 2, 4, 16, ... bytes. */
|
|
|
|
#define INT_STRLEN_BOUND(t) \
|
2016-09-15 18:50:23 +00:00
|
|
|
(INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
|
2011-05-16 00:51:54 +00:00
|
|
|
+ _GL_SIGNED_TYPE_OR_EXPR (t))
|
2011-01-09 01:20:28 +00:00
|
|
|
|
|
|
|
/* Bound on buffer size needed to represent an integer type or expression T,
|
2020-08-23 21:09:48 +00:00
|
|
|
including the terminating null. T must not be a bit-field expression. */
|
2011-05-16 00:51:54 +00:00
|
|
|
#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
|
|
|
|
|
|
|
|
|
|
|
|
/* Range overflow checks.
|
|
|
|
|
|
|
|
The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
|
2022-08-18 01:50:06 +00:00
|
|
|
operators overflow arithmetically when given the same arguments.
|
|
|
|
These macros do not rely on undefined or implementation-defined behavior.
|
|
|
|
Although their implementations are simple and straightforward,
|
|
|
|
they are harder to use and may be less efficient than the
|
|
|
|
INT_<op>_WRAPV, INT_<op>_OK, and INT_<op>_OVERFLOW macros described below.
|
2011-05-16 00:51:54 +00:00
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
long int i = ...;
|
|
|
|
long int j = ...;
|
|
|
|
if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
|
|
|
|
printf ("multiply would overflow");
|
|
|
|
else
|
|
|
|
printf ("product is %ld", i * j);
|
|
|
|
|
|
|
|
Restrictions on *_RANGE_OVERFLOW macros:
|
|
|
|
|
|
|
|
These macros do not check for all possible numerical problems or
|
|
|
|
undefined or unspecified behavior: they do not check for division
|
|
|
|
by zero, for bad shift counts, or for shifting negative numbers.
|
|
|
|
|
|
|
|
These macros may evaluate their arguments zero or multiple times,
|
|
|
|
so the arguments should not have side effects. The arithmetic
|
|
|
|
arguments (including the MIN and MAX arguments) must be of the same
|
|
|
|
integer type after the usual arithmetic conversions, and the type
|
|
|
|
must have minimum value MIN and maximum MAX. Unsigned types should
|
|
|
|
use a zero MIN of the proper type.
|
|
|
|
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
Because all arguments are subject to integer promotions, these
|
|
|
|
macros typically do not work on types narrower than 'int'.
|
|
|
|
|
2011-05-16 00:51:54 +00:00
|
|
|
These macros are tuned for constant MIN and MAX. For commutative
|
|
|
|
operations such as A + B, they are also tuned for constant B. */
|
|
|
|
|
|
|
|
/* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
|
|
|
|
See above for restrictions. */
|
|
|
|
#define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
|
((b) < 0 \
|
|
|
|
? (a) < (min) - (b) \
|
|
|
|
: (max) - (b) < (a))
|
|
|
|
|
|
|
|
/* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
|
|
|
|
See above for restrictions. */
|
|
|
|
#define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
|
((b) < 0 \
|
|
|
|
? (max) + (b) < (a) \
|
|
|
|
: (a) < (min) + (b))
|
|
|
|
|
|
|
|
/* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
|
|
|
|
See above for restrictions. */
|
|
|
|
#define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
|
2022-08-18 01:50:06 +00:00
|
|
|
_GL_INT_NEGATE_RANGE_OVERFLOW (a, min, max)
|
2011-05-16 00:51:54 +00:00
|
|
|
|
|
|
|
/* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
|
2011-05-22 21:02:48 +00:00
|
|
|
See above for restrictions. Avoid && and || as they tickle
|
|
|
|
bugs in Sun C 5.11 2010/08/13 and other compilers; see
|
Merge from Gnulib
This incorporates:
2017-11-23 stat: work around Solaris bug with tv_nsec < 0
2017-11-12 maint: shorten https://lists.gnu.org/archive/html/... links
* build-aux/config.sub, doc/misc/texinfo.tex, lib/allocator.h:
* lib/fstatat.c, lib/intprops.h, lib/lstat.c, lib/signal.in.h:
* lib/stat-time.h, lib/stdio-impl.h, lib/stdio.in.h:
* lib/timespec.h, m4/alloca.m4, m4/extern-inline.m4:
* m4/faccessat.m4, m4/fstatat.m4, m4/gnulib-common.m4:
* m4/lstat.m4, m4/std-gnu11.m4, m4/sys_types_h.m4:
* m4/vararrays.m4:
Copy from Gnulib.
2017-11-26 06:28:31 +00:00
|
|
|
<https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
|
2011-05-16 00:51:54 +00:00
|
|
|
#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
|
((b) < 0 \
|
|
|
|
? ((a) < 0 \
|
|
|
|
? (a) < (max) / (b) \
|
2011-05-22 21:02:48 +00:00
|
|
|
: (b) == -1 \
|
|
|
|
? 0 \
|
|
|
|
: (min) / (b) < (a)) \
|
|
|
|
: (b) == 0 \
|
|
|
|
? 0 \
|
|
|
|
: ((a) < 0 \
|
|
|
|
? (a) < (min) / (b) \
|
|
|
|
: (max) / (b) < (a)))
|
2011-05-16 00:51:54 +00:00
|
|
|
|
|
|
|
/* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
|
|
|
|
See above for restrictions. Do not check for division by zero. */
|
|
|
|
#define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
|
((min) < 0 && (b) == -1 && (a) < - (max))
|
|
|
|
|
|
|
|
/* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
|
|
|
|
See above for restrictions. Do not check for division by zero.
|
|
|
|
Mathematically, % should never overflow, but on x86-like hosts
|
|
|
|
INT_MIN % -1 traps, and the C standard permits this, so treat this
|
|
|
|
as an overflow too. */
|
|
|
|
#define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
|
INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
|
|
|
|
|
|
|
|
/* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
|
|
|
|
See above for restrictions. Here, MIN and MAX are for A only, and B need
|
|
|
|
not be of the same type as the other arguments. The C standard says that
|
|
|
|
behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
|
|
|
|
A is negative then A << B has undefined behavior and A >> B has
|
|
|
|
implementation-defined behavior, but do not check these other
|
|
|
|
restrictions. */
|
|
|
|
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
|
((a) < 0 \
|
|
|
|
? (a) < (min) >> (b) \
|
|
|
|
: (max) >> (b) < (a))
|
|
|
|
|
|
|
|
/* The _GL*_OVERFLOW macros have the same restrictions as the
|
|
|
|
*_RANGE_OVERFLOW macros, except that they do not assume that operands
|
|
|
|
(e.g., A and B) have the same type as MIN and MAX. Instead, they assume
|
|
|
|
that the result (e.g., A + B) has that type. */
|
2016-08-30 20:15:04 +00:00
|
|
|
#if _GL_HAS_BUILTIN_OVERFLOW_P
|
|
|
|
# define _GL_ADD_OVERFLOW(a, b, min, max) \
|
|
|
|
__builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
|
|
|
|
# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
|
|
|
|
__builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
|
|
|
|
# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
|
|
|
|
__builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
|
2016-06-24 11:49:32 +00:00
|
|
|
#else
|
|
|
|
# define _GL_ADD_OVERFLOW(a, b, min, max) \
|
|
|
|
((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
|
|
|
|
: (a) < 0 ? (b) <= (a) + (b) \
|
|
|
|
: (b) < 0 ? (a) <= (a) + (b) \
|
|
|
|
: (a) + (b) < (b))
|
|
|
|
# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
|
|
|
|
((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
|
|
|
|
: (a) < 0 ? 1 \
|
|
|
|
: (b) < 0 ? (a) - (b) <= (a) \
|
|
|
|
: (a) < (b))
|
|
|
|
# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
|
|
|
|
(((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
|
|
|
|
|| INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
|
|
|
|
#endif
|
2011-05-16 00:51:54 +00:00
|
|
|
#define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
|
2011-05-22 21:02:48 +00:00
|
|
|
((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
|
2011-05-16 00:51:54 +00:00
|
|
|
: (a) < 0 ? (b) <= (a) + (b) - 1 \
|
|
|
|
: (b) < 0 && (a) + (b) <= (a))
|
|
|
|
#define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
|
2011-05-22 21:02:48 +00:00
|
|
|
((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
|
2011-05-16 00:51:54 +00:00
|
|
|
: (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
|
|
|
|
: (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
|
|
|
|
|
|
|
|
/* Return a nonzero value if A is a mathematical multiple of B, where
|
|
|
|
A is unsigned, B is negative, and MAX is the maximum value of A's
|
|
|
|
type. A's type must be the same as (A % B)'s type. Normally (A %
|
|
|
|
-B == 0) suffices, but things get tricky if -B would overflow. */
|
|
|
|
#define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
|
|
|
|
(((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
|
|
|
|
? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
|
|
|
|
? (a) \
|
|
|
|
: (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
|
|
|
|
: (a) % - (b)) \
|
|
|
|
== 0)
|
|
|
|
|
2015-10-31 02:21:29 +00:00
|
|
|
/* Check for integer overflow, and report low order bits of answer.
|
2011-05-16 00:51:54 +00:00
|
|
|
|
|
|
|
The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
|
|
|
|
might not yield numerically correct answers due to arithmetic overflow.
|
2019-08-15 01:13:27 +00:00
|
|
|
The INT_<op>_WRAPV macros compute the low-order bits of the sum,
|
|
|
|
difference, and product of two C integers, and return 1 if these
|
|
|
|
low-order bits are not numerically correct.
|
2015-10-31 02:21:29 +00:00
|
|
|
These macros work correctly on all known practical hosts, and do not rely
|
2011-05-16 00:51:54 +00:00
|
|
|
on undefined behavior due to signed arithmetic overflow.
|
|
|
|
|
2015-11-08 07:36:45 +00:00
|
|
|
Example usage, assuming A and B are long int:
|
2011-05-16 00:51:54 +00:00
|
|
|
|
2015-12-18 23:41:45 +00:00
|
|
|
if (INT_MULTIPLY_OVERFLOW (a, b))
|
|
|
|
printf ("result would overflow\n");
|
|
|
|
else
|
|
|
|
printf ("result is %ld (no overflow)\n", a * b);
|
2015-10-31 02:21:29 +00:00
|
|
|
|
2015-11-08 07:36:45 +00:00
|
|
|
Example usage with WRAPV flavor:
|
|
|
|
|
|
|
|
long int result;
|
|
|
|
bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
|
|
|
|
printf ("result is %ld (%s)\n", result,
|
|
|
|
overflow ? "after overflow" : "no overflow");
|
2015-10-31 02:21:29 +00:00
|
|
|
|
|
|
|
Restrictions on these macros:
|
2011-05-16 00:51:54 +00:00
|
|
|
|
|
|
|
These macros do not check for all possible numerical problems or
|
|
|
|
undefined or unspecified behavior: they do not check for division
|
|
|
|
by zero, for bad shift counts, or for shifting negative numbers.
|
|
|
|
|
|
|
|
These macros may evaluate their arguments zero or multiple times, so the
|
|
|
|
arguments should not have side effects.
|
|
|
|
|
2015-11-08 07:36:45 +00:00
|
|
|
The WRAPV macros are not constant expressions. They support only
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
+, binary -, and *.
|
|
|
|
|
|
|
|
Because the WRAPV macros convert the result, they report overflow
|
|
|
|
in different circumstances than the OVERFLOW macros do. For
|
|
|
|
example, in the typical case with 16-bit 'short' and 32-bit 'int',
|
2022-08-18 01:50:06 +00:00
|
|
|
if A, B and *R are all of type 'short' then INT_ADD_OVERFLOW (A, B)
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
returns false because the addition cannot overflow after A and B
|
2022-08-18 01:50:06 +00:00
|
|
|
are converted to 'int', whereas INT_ADD_WRAPV (A, B, R) returns
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
true or false depending on whether the sum fits into 'short'.
|
2015-10-31 02:21:29 +00:00
|
|
|
|
2019-08-15 01:13:27 +00:00
|
|
|
These macros are tuned for their last input argument being a constant.
|
2011-05-16 00:51:54 +00:00
|
|
|
|
2022-08-18 01:50:06 +00:00
|
|
|
A, B, and *R should be integers; they need not be the same type,
|
|
|
|
and they need not be all signed or all unsigned.
|
|
|
|
However, none of the integer types should be bit-precise,
|
|
|
|
and *R's type should not be char, bool, or an enumeration type.
|
|
|
|
|
2011-05-16 00:51:54 +00:00
|
|
|
Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
|
|
|
|
A % B, and A << B would overflow, respectively. */
|
|
|
|
|
2015-11-08 07:36:45 +00:00
|
|
|
#define INT_ADD_OVERFLOW(a, b) \
|
2011-05-16 00:51:54 +00:00
|
|
|
_GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
|
2015-11-08 07:36:45 +00:00
|
|
|
#define INT_SUBTRACT_OVERFLOW(a, b) \
|
2011-05-16 00:51:54 +00:00
|
|
|
_GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
|
2022-08-18 01:50:06 +00:00
|
|
|
#define INT_NEGATE_OVERFLOW(a) _GL_INT_NEGATE_OVERFLOW (a)
|
2015-11-08 07:36:45 +00:00
|
|
|
#define INT_MULTIPLY_OVERFLOW(a, b) \
|
2011-05-16 00:51:54 +00:00
|
|
|
_GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
|
|
|
|
#define INT_DIVIDE_OVERFLOW(a, b) \
|
|
|
|
_GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
|
|
|
|
#define INT_REMAINDER_OVERFLOW(a, b) \
|
|
|
|
_GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
|
|
|
|
#define INT_LEFT_SHIFT_OVERFLOW(a, b) \
|
|
|
|
INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
|
|
|
|
_GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
|
|
|
|
|
|
|
|
/* Return 1 if the expression A <op> B would overflow,
|
|
|
|
where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
|
|
|
|
assuming MIN and MAX are the minimum and maximum for the result type.
|
2011-05-22 21:02:48 +00:00
|
|
|
Arguments should be free of side effects. */
|
2011-05-16 00:51:54 +00:00
|
|
|
#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
|
|
|
|
op_result_overflow (a, b, \
|
Update from Gnulib
This incorporates:
2018-09-10 timespec: fix resolution confusion
2018-09-09 mktime: simplify in prep for glibc merge
2018-09-07 intprops: minor clarification of code
2018-09-06 stddef: Override max_align_t on NetBSD 8.0/x86
2018-09-06 fcntl: Fix F_DUPFD_CLOEXEC behaviour on Haiku
2018-09-06 strtoll, strtoull: Rely on limits-h module
2018-09-06 limits-h: Provide numerical limits macros
2018-09-06 fcntl: Don't access nonexistent optional argument
2018-09-02 mktime: fix unlikely race+overflow bug
2018-08-31 mktime, timegm: simplify glibc time64_t
2018-08-31 mktime, timegm: simplify merge to glibc
* build-aux/config.guess, build-aux/config.sub:
* lib/dtotimespec.c, lib/fcntl.c, lib/intprops.h:
* lib/limits.in.h, lib/mktime-internal.h, lib/mktime.c:
* lib/stat-time.h, lib/strtol.c, lib/timegm.c:
* lib/timespec-add.c, lib/timespec-sub.c, lib/timespec.h:
* lib/utimens.c, m4/limits-h.m4, m4/stddef_h.m4:
Copy from Gnulib.
2018-09-11 04:16:03 +00:00
|
|
|
_GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
|
|
|
|
_GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
|
2011-01-09 01:20:28 +00:00
|
|
|
|
2016-06-24 11:49:32 +00:00
|
|
|
/* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
|
2015-11-08 07:36:45 +00:00
|
|
|
Return 1 if the result overflows. See above for restrictions. */
|
2022-08-18 01:50:06 +00:00
|
|
|
#define INT_ADD_WRAPV(a, b, r) _GL_INT_ADD_WRAPV (a, b, r)
|
|
|
|
#define INT_SUBTRACT_WRAPV(a, b, r) _GL_INT_SUBTRACT_WRAPV (a, b, r)
|
|
|
|
#define INT_MULTIPLY_WRAPV(a, b, r) _GL_INT_MULTIPLY_WRAPV (a, b, r)
|
2019-08-15 01:13:27 +00:00
|
|
|
|
2020-12-24 15:48:40 +00:00
|
|
|
/* The following macros compute A + B, A - B, and A * B, respectively.
|
|
|
|
If no overflow occurs, they set *R to the result and return 1;
|
|
|
|
otherwise, they return 0 and may modify *R.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
long int result;
|
|
|
|
if (INT_ADD_OK (a, b, &result))
|
|
|
|
printf ("result is %ld\n", result);
|
|
|
|
else
|
|
|
|
printf ("overflow\n");
|
|
|
|
|
|
|
|
A, B, and *R should be integers; they need not be the same type,
|
|
|
|
and they need not be all signed or all unsigned.
|
2022-08-18 01:50:06 +00:00
|
|
|
However, none of the integer types should be bit-precise,
|
|
|
|
and *R's type should not be char, bool, or an enumeration type.
|
2020-12-24 15:48:40 +00:00
|
|
|
|
|
|
|
These macros work correctly on all known practical hosts, and do not rely
|
|
|
|
on undefined behavior due to signed arithmetic overflow.
|
|
|
|
|
|
|
|
These macros are not constant expressions.
|
|
|
|
|
|
|
|
These macros may evaluate their arguments zero or multiple times, so the
|
|
|
|
arguments should not have side effects.
|
|
|
|
|
|
|
|
These macros are tuned for B being a constant. */
|
|
|
|
|
2022-08-18 01:50:06 +00:00
|
|
|
#define INT_ADD_OK(a, b, r) (! INT_ADD_WRAPV (a, b, r))
|
|
|
|
#define INT_SUBTRACT_OK(a, b, r) (! INT_SUBTRACT_WRAPV (a, b, r))
|
|
|
|
#define INT_MULTIPLY_OK(a, b, r) (! INT_MULTIPLY_WRAPV (a, b, r))
|
2020-12-24 15:48:40 +00:00
|
|
|
|
2011-05-16 00:51:54 +00:00
|
|
|
#endif /* _GL_INTPROPS_H */
|