1 /* intprops-internal.h -- properties of integer types not visible to users
2
3 Copyright (C) 2001-2023 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published
7 by 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 <https://www.gnu.org/licenses/>. */
17
18 #ifndef _GL_INTPROPS_INTERNAL_H
19 #define _GL_INTPROPS_INTERNAL_H
20
21 #include <limits.h>
22
23 /* Pacify GCC 13.2 in some calls to _GL_EXPR_SIGNED. */
24 #if defined __GNUC__ && 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
25 # pragma GCC diagnostic ignored "-Wtype-limits"
26 #endif
27
28 /* Return a value with the common real type of E and V and the value of V.
29 Do not evaluate E. */
30 #define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
31
32 /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
33 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
34 #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
35
36 /* The extra casts in the following macros work around compiler bugs,
37 e.g., in Cray C 5.0.3.0. */
38
39 /* True if the real type T is signed. */
40 #define _GL_TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
41
42 /* Return 1 if the real expression E, after promotion, has a
43 signed or floating type. Do not evaluate E. */
44 #define _GL_EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
45
46
47 /* Minimum and maximum values for integer types and expressions. */
48
49 /* The width in bits of the integer type or expression T.
50 Do not evaluate T. T must not be a bit-field expression.
51 Padding bits are not supported; this is checked at compile-time below. */
52 #define _GL_TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
53
54 /* The maximum and minimum values for the type of the expression E,
55 after integer promotion. E is not evaluated. */
56 #define _GL_INT_MINIMUM(e) \
57 (_GL_EXPR_SIGNED (e) \
58 ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
59 : _GL_INT_CONVERT (e, 0))
60 #define _GL_INT_MAXIMUM(e) \
61 (_GL_EXPR_SIGNED (e) \
62 ? _GL_SIGNED_INT_MAXIMUM (e) \
63 : _GL_INT_NEGATE_CONVERT (e, 1))
64 #define _GL_SIGNED_INT_MAXIMUM(e) \
65 (((_GL_INT_CONVERT (e, 1) << (_GL_TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)
66
67 /* Work around OpenVMS incompatibility with C99. */
68 #if !defined LLONG_MAX && defined __INT64_MAX
69 # define LLONG_MAX __INT64_MAX
70 # define LLONG_MIN __INT64_MIN
71 #endif
72
73 /* This include file assumes that signed types are two's complement without
74 padding bits; the above macros have undefined behavior otherwise.
75 If this is a problem for you, please let us know how to fix it for your host.
76 This assumption is tested by the intprops-tests module. */
77
78 /* Does the __typeof__ keyword work? This could be done by
79 'configure', but for now it's easier to do it by hand. */
80 #if (2 <= __GNUC__ \
81 || (4 <= __clang_major__) \
82 || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
83 || (0x5110 <= __SUNPRO_C && !__STDC__))
84 # define _GL_HAVE___TYPEOF__ 1
85 #else
86 # define _GL_HAVE___TYPEOF__ 0
87 #endif
88
89 /* Return 1 if the integer type or expression T might be signed. Return 0
90 if it is definitely unsigned. T must not be a bit-field expression.
91 This macro does not evaluate its argument, and expands to an
92 integer constant expression. */
93 #if _GL_HAVE___TYPEOF__
94 # define _GL_SIGNED_TYPE_OR_EXPR(t) _GL_TYPE_SIGNED (__typeof__ (t))
95 #else
96 # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
97 #endif
98
99 /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
100 A should not have side effects, and A's type should be an
101 integer with minimum value MIN and maximum MAX. */
102 #define _GL_INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
103 ((min) < 0 ? (a) < - (max) : 0 < (a))
104
105 /* True if __builtin_add_overflow (A, B, P) and __builtin_sub_overflow
106 (A, B, P) work when P is non-null. */
107 #ifdef __EDG__
108 /* EDG-based compilers like nvc 22.1 cannot add 64-bit signed to unsigned
109 <https://bugs.gnu.org/53256>. */
110 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
111 #elif defined __has_builtin
112 # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
113 /* __builtin_{add,sub}_overflow exists but is not reliable in GCC 5.x and 6.x,
114 see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98269>. */
115 #elif 7 <= __GNUC__
116 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
117 #else
118 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
119 #endif
120
121 /* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */
122 #if defined __clang_major__ && __clang_major__ < 14
123 /* Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */
124 # define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
125 #else
126 # define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
127 #endif
128
129 /* True if __builtin_add_overflow_p (A, B, C) works, and similarly for
130 __builtin_sub_overflow_p and __builtin_mul_overflow_p. */
131 #ifdef __EDG__
132 /* In EDG-based compilers like ICC 2021.3 and earlier,
133 __builtin_add_overflow_p etc. are not treated as integral constant
134 expressions even when all arguments are. */
135 # define _GL_HAS_BUILTIN_OVERFLOW_P 0
136 #elif defined __has_builtin
137 # define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
138 #else
139 # define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
140 #endif
141
142 #if (!defined _GL_STDCKDINT_H && 202311 <= __STDC_VERSION__ \
143 && ! (_GL_HAS_BUILTIN_ADD_OVERFLOW && _GL_HAS_BUILTIN_MUL_OVERFLOW))
144 # include <stdckdint.h>
145 #endif
146
147 /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
148 Return 1 if the result overflows. Arguments should not have side
149 effects and A, B and *R can be of any integer type other than char,
150 bool, a bit-precise integer type, or an enumeration type. */
151 #if _GL_HAS_BUILTIN_ADD_OVERFLOW
152 # define _GL_INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
153 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
154 #elif defined ckd_add && defined ckd_sub && !defined _GL_STDCKDINT_H
155 # define _GL_INT_ADD_WRAPV(a, b, r) ckd_add (r, + (a), + (b))
156 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) ckd_sub (r, + (a), + (b))
157 #else
158 # define _GL_INT_ADD_WRAPV(a, b, r) \
159 _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
160 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) \
161 _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
162 #endif
163 #if _GL_HAS_BUILTIN_MUL_OVERFLOW
164 # if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
165 || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \
166 && !defined __EDG__)
167 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
168 # else
169 /* Work around GCC bug 91450. */
170 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) \
171 ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && _GL_EXPR_SIGNED (a) && _GL_EXPR_SIGNED (b) \
172 && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
173 ? ((void) __builtin_mul_overflow (a, b, r), 1) \
174 : __builtin_mul_overflow (a, b, r))
175 # endif
176 #elif defined ckd_mul && !defined _GL_STDCKDINT_H
177 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) ckd_mul (r, + (a), + (b))
178 #else
179 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) \
180 _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
181 #endif
182
183 /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
184 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
185 https://llvm.org/bugs/show_bug.cgi?id=25390
186 For now, assume all versions of GCC-like compilers generate bogus
187 warnings for _Generic. This matters only for compilers that
188 lack relevant builtins. */
189 #if __GNUC__ || defined __clang__
190 # define _GL__GENERIC_BOGUS 1
191 #else
192 # define _GL__GENERIC_BOGUS 0
193 #endif
194
195 /* Store the low-order bits of A <op> B into *R, where OP specifies
196 the operation and OVERFLOW the overflow predicate. Return 1 if the
197 result overflows. Arguments should not have side effects,
198 and A, B and *R can be of any integer type other than char, bool, a
199 bit-precise integer type, or an enumeration type. */
200 #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
201 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
202 (_Generic \
203 (*(r), \
204 signed char: \
205 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
206 signed char, SCHAR_MIN, SCHAR_MAX), \
207 unsigned char: \
208 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
209 unsigned char, 0, UCHAR_MAX), \
210 short int: \
211 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
212 short int, SHRT_MIN, SHRT_MAX), \
213 unsigned short int: \
214 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
215 unsigned short int, 0, USHRT_MAX), \
216 int: \
217 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
218 int, INT_MIN, INT_MAX), \
219 unsigned int: \
220 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
221 unsigned int, 0, UINT_MAX), \
222 long int: \
223 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
224 long int, LONG_MIN, LONG_MAX), \
225 unsigned long int: \
226 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
227 unsigned long int, 0, ULONG_MAX), \
228 long long int: \
229 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
230 long long int, LLONG_MIN, LLONG_MAX), \
231 unsigned long long int: \
232 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
233 unsigned long long int, 0, ULLONG_MAX)))
234 #else
235 /* Store the low-order bits of A <op> B into *R, where OP specifies
236 the operation and OVERFLOW the overflow predicate. If *R is
237 signed, its type is ST with bounds SMIN..SMAX; otherwise its type
238 is UT with bounds U..UMAX. ST and UT are narrower than int.
239 Return 1 if the result overflows. Arguments should not have side
240 effects, and A, B and *R can be of any integer type other than
241 char, bool, a bit-precise integer type, or an enumeration type. */
242 # if _GL_HAVE___TYPEOF__
243 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
244 (_GL_TYPE_SIGNED (__typeof__ (*(r))) \
245 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
246 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
247 # else
248 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
249 (overflow (a, b, smin, smax) \
250 ? (overflow (a, b, 0, umax) \
251 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
252 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
253 : (overflow (a, b, 0, umax) \
254 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
255 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
256 # endif
257
258 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
259 (sizeof *(r) == sizeof (signed char) \
260 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
261 signed char, SCHAR_MIN, SCHAR_MAX, \
262 unsigned char, UCHAR_MAX) \
263 : sizeof *(r) == sizeof (short int) \
264 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
265 short int, SHRT_MIN, SHRT_MAX, \
266 unsigned short int, USHRT_MAX) \
267 : sizeof *(r) == sizeof (int) \
268 ? (_GL_EXPR_SIGNED (*(r)) \
269 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
270 int, INT_MIN, INT_MAX) \
271 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
272 unsigned int, 0, UINT_MAX)) \
273 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
274 # ifdef LLONG_MAX
275 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
276 (sizeof *(r) == sizeof (long int) \
277 ? (_GL_EXPR_SIGNED (*(r)) \
278 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
279 long int, LONG_MIN, LONG_MAX) \
280 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
281 unsigned long int, 0, ULONG_MAX)) \
282 : (_GL_EXPR_SIGNED (*(r)) \
283 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
284 long long int, LLONG_MIN, LLONG_MAX) \
285 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
286 unsigned long long int, 0, ULLONG_MAX)))
287 # else
288 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
289 (_GL_EXPR_SIGNED (*(r)) \
290 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
291 long int, LONG_MIN, LONG_MAX) \
292 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
293 unsigned long int, 0, ULONG_MAX))
294 # endif
295 #endif
296
297 /* Store the low-order bits of A <op> B into *R, where the operation
298 is given by OP. Use the unsigned type UT for calculation to avoid
299 overflow problems. *R's type is T, with extrema TMIN and TMAX.
300 T can be any signed integer type other than char, bool, a
301 bit-precise integer type, or an enumeration type.
302 Return 1 if the result overflows. */
303 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
304 (overflow (a, b, tmin, tmax) \
305 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
306 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
307
308 /* Return 1 if the integer expressions A - B and -A would overflow,
309 respectively. Arguments should not have side effects,
310 and can be any signed integer type other than char, bool, a
311 bit-precise integer type, or an enumeration type.
312 These macros are tuned for their last input argument being a constant. */
313
314 #if _GL_HAS_BUILTIN_OVERFLOW_P
315 # define _GL_INT_NEGATE_OVERFLOW(a) \
316 __builtin_sub_overflow_p (0, a, (__typeof__ (- (a))) 0)
317 #else
318 # define _GL_INT_NEGATE_OVERFLOW(a) \
319 _GL_INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
320 #endif
321
322 /* Return the low-order bits of A <op> B, where the operation is given
323 by OP. Use the unsigned type UT for calculation to avoid undefined
324 behavior on signed integer overflow, and convert the result to type T.
325 UT is at least as wide as T and is no narrower than unsigned int,
326 T is two's complement, and there is no padding or trap representations.
327 Assume that converting UT to T yields the low-order bits, as is
328 done in all known two's-complement C compilers. E.g., see:
329 https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
330
331 According to the C standard, converting UT to T yields an
332 implementation-defined result or signal for values outside T's
333 range. However, code that works around this theoretical problem
334 runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
335 https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
336 As the compiler bug is real, don't try to work around the
337 theoretical problem. */
338
339 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
340 ((t) ((ut) (a) op (ut) (b)))
341
342 /* Return true if the numeric values A + B, A - B, A * B fall outside
343 the range TMIN..TMAX. Arguments should not have side effects
344 and can be any integer type other than char, bool,
345 a bit-precise integer type, or an enumeration type.
346 TMIN should be signed and nonpositive.
347 TMAX should be positive, and should be signed unless TMIN is zero. */
348 #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
349 ((b) < 0 \
350 ? (((tmin) \
351 ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
352 && (a) < (tmin) - (b)) \
353 : (a) <= -1 - (b)) \
354 || ((_GL_EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
355 : (a) < 0 \
356 ? (((tmin) \
357 ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
358 && (b) < (tmin) - (a)) \
359 : (b) <= -1 - (a)) \
360 || ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
361 && (tmax) < (a) + (b))) \
362 : (tmax) < (b) || (tmax) - (b) < (a))
363 #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
364 (((a) < 0) == ((b) < 0) \
365 ? ((a) < (b) \
366 ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
367 : (tmax) < (a) - (b)) \
368 : (a) < 0 \
369 ? ((!_GL_EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
370 || (a) - (tmin) < (b)) \
371 : ((! (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
372 && _GL_EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
373 && (tmax) <= -1 - (b)) \
374 || (tmax) + (b) < (a)))
375 #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
376 ((b) < 0 \
377 ? ((a) < 0 \
378 ? (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
379 ? (a) < (tmax) / (b) \
380 : ((_GL_INT_NEGATE_OVERFLOW (b) \
381 ? _GL_INT_CONVERT (b, tmax) >> (_GL_TYPE_WIDTH (+ (b)) - 1) \
382 : (tmax) / -(b)) \
383 <= -1 - (a))) \
384 : _GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
385 ? (_GL_EXPR_SIGNED (a) \
386 ? 0 < (a) + (tmin) \
387 : 0 < (a) && -1 - (tmin) < (a) - 1) \
388 : (tmin) / (b) < (a)) \
389 : (b) == 0 \
390 ? 0 \
391 : ((a) < 0 \
392 ? (_GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
393 ? (_GL_EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
394 : (tmin) / (a) < (b)) \
395 : (tmax) / (b) < (a)))
396
397 #endif /* _GL_INTPROPS_INTERNAL_H */