Debian Codegraph @

lib/intprops-internal.h

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