xref: /freebsd/contrib/ntp/sntp/libopts/intprops.h (revision 0d66206fff44f864ea8a4b220c3a53b4caa959a0)
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 Lesser General Public License as published by
7    the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser 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 
267 /* Integer overflow checks.
268 
269    The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
270    might not yield numerically correct answers due to arithmetic overflow.
271    They work correctly on all known practical hosts, and do not rely
272    on undefined behavior due to signed arithmetic overflow.
273 
274    Example usage:
275 
276      long int i = ...;
277      long int j = ...;
278      if (INT_MULTIPLY_OVERFLOW (i, j))
279        printf ("multiply would overflow");
280      else
281        printf ("product is %ld", i * j);
282 
283    These macros do not check for all possible numerical problems or
284    undefined or unspecified behavior: they do not check for division
285    by zero, for bad shift counts, or for shifting negative numbers.
286 
287    These macros may evaluate their arguments zero or multiple times, so the
288    arguments should not have side effects.
289 
290    These macros are tuned for their last argument being a constant.
291 
292    Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
293    A % B, and A << B would overflow, respectively.  */
294 
295 #define INT_ADD_OVERFLOW(a, b) \
296   _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
297 #define INT_SUBTRACT_OVERFLOW(a, b) \
298   _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
299 #define INT_NEGATE_OVERFLOW(a) \
300   INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
301 #define INT_MULTIPLY_OVERFLOW(a, b) \
302   _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
303 #define INT_DIVIDE_OVERFLOW(a, b) \
304   _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
305 #define INT_REMAINDER_OVERFLOW(a, b) \
306   _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
307 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
308   INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
309                                  _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
310 
311 /* Return 1 if the expression A <op> B would overflow,
312    where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
313    assuming MIN and MAX are the minimum and maximum for the result type.
314    Arguments should be free of side effects.  */
315 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow)        \
316   op_result_overflow (a, b,                                     \
317                       _GL_INT_MINIMUM (0 * (b) + (a)),          \
318                       _GL_INT_MAXIMUM (0 * (b) + (a)))
319 
320 #endif /* _GL_INTPROPS_H */
321