Lines Matching +full:max +full:- +full:by +full:- +full:define

1 /* intprops.h -- properties of integer types
3 Copyright (C) 2001-2018 Free Software Foundation, Inc.
7 by the Free Software Foundation; either version 2.1 of the License, or
18 /* Written by Paul Eggert. */
21 #define _GL_INTPROPS_H
26 #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
28 /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
29 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
30 #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
37 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
40 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
44 #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
50 Padding bits are not supported; this is checked at compile-time below. */
51 #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
54 #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
55 #define TYPE_MAXIMUM(t) \
57 ? (t) -1 \
58 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
62 #define _GL_INT_MINIMUM(e) \
66 #define _GL_INT_MAXIMUM(e) \
70 #define _GL_SIGNED_INT_MAXIMUM(e) \
71 (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
75 # define LLONG_MAX __INT64_MAX
76 # define LLONG_MIN __INT64_MIN
82 This assumption is tested by the intprops-tests module. */
84 /* Does the __typeof__ keyword work? This could be done by
85 'configure', but for now it's easier to do it by hand. */
89 # define _GL_HAVE___TYPEOF__ 1
91 # define _GL_HAVE___TYPEOF__ 0
98 # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
100 # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
106 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
113 signed, this macro may overestimate the true bound by one byte when
115 #define INT_STRLEN_BOUND(t) \
116 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
121 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
129 implementation-defined behavior. Their implementations are simple
146 by zero, for bad shift counts, or for shifting negative numbers.
150 arguments (including the MIN and MAX arguments) must be of the same
152 must have minimum value MIN and maximum MAX. Unsigned types should
155 These macros are tuned for constant MIN and MAX. For commutative
158 /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
160 #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \ argument
162 ? (a) < (min) - (b) \
163 : (max) - (b) < (a))
165 /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
167 #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \ argument
169 ? (max) + (b) < (a) \
172 /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
174 #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ argument
176 ? (a) < - (max) \
179 /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
182 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
183 #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ argument
186 ? (a) < (max) / (b) \
187 : (b) == -1 \
194 : (max) / (b) < (a)))
196 /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
197 See above for restrictions. Do not check for division by zero. */
198 #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \ argument
199 ((min) < 0 && (b) == -1 && (a) < - (max))
201 /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
202 See above for restrictions. Do not check for division by zero.
203 Mathematically, % should never overflow, but on x86-like hosts
204 INT_MIN % -1 traps, and the C standard permits this, so treat this
206 #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \ argument
207 INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
209 /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
210 See above for restrictions. Here, MIN and MAX are for A only, and B need
214 implementation-defined behavior, but do not check these other
216 #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ argument
219 : (max) >> (b) < (a))
221 /* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
223 # define _GL_HAS_BUILTIN_OVERFLOW 1
225 # define _GL_HAS_BUILTIN_OVERFLOW 0
229 #define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
233 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
236 # define _GL_ADD_OVERFLOW(a, b, min, max) \ argument
238 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ argument
239 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
240 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ argument
243 # define _GL_ADD_OVERFLOW(a, b, min, max) \ argument
244 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
248 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ argument
249 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
251 : (b) < 0 ? (a) - (b) <= (a) \
253 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ argument
255 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
257 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ argument
258 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
259 : (a) < 0 ? (b) <= (a) + (b) - 1 \
261 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \ argument
262 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
263 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
264 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
267 A is unsigned, B is negative, and MAX is the maximum value of A's
269 -B == 0) suffices, but things get tricky if -B would overflow. */
270 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \ argument
271 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
272 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
275 : (a) % - (b)) \
282 The INT_<op>_WRAPV macros also store the low-order bits of the answer.
304 by zero, for bad shift counts, or for shifting negative numbers.
310 +, binary -, and *. The result type must be signed.
314 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
317 #define INT_ADD_OVERFLOW(a, b) \
319 #define INT_SUBTRACT_OVERFLOW(a, b) \
322 # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
324 # define INT_NEGATE_OVERFLOW(a) \
327 #define INT_MULTIPLY_OVERFLOW(a, b) \
329 #define INT_DIVIDE_OVERFLOW(a, b) \
331 #define INT_REMAINDER_OVERFLOW(a, b) \
333 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
338 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
339 assuming MIN and MAX are the minimum and maximum for the result type.
341 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
346 /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
348 #define INT_ADD_WRAPV(a, b, r) \
350 #define INT_SUBTRACT_WRAPV(a, b, r) \
351 _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
352 #define INT_MULTIPLY_WRAPV(a, b, r) \
358 For now, assume all versions of GCC-like compilers generate bogus
362 # define _GL__GENERIC_BOGUS 1
364 # define _GL__GENERIC_BOGUS 0
367 /* Store the low-order bits of A <op> B into *R, where OP specifies
372 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
374 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
393 # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
405 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
412 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
418 /* Store the low-order bits of A <op> B into *R, where the operation
419 is given by OP. Use the unsigned type UT for calculation to avoid
422 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
426 #define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
433 /* Return the low-order bits of A <op> B, where the operation is given
434 by OP. Use the unsigned type UT for calculation to avoid undefined
438 Assume that converting UT to T yields the low-order bits, as is
439 done in all known two's-complement C compilers. E.g., see:
440 https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
443 implementation-defined result or signal for values outside T's
446 https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
450 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \