Debian Codegraph @

lib/intprops-internal.h

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