Merge pull request #2046 from RincewindsHat/fix/format_string
[monitoring-plugins.git] / gl / intprops-internal.h
blobb5ba8d7cbd25f4c9a40b8b979b859d54ca26ba25
1 /* intprops-internal.h -- properties of integer types not visible to users
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
18 #ifndef _GL_INTPROPS_INTERNAL_H
19 #define _GL_INTPROPS_INTERNAL_H
21 #include <limits.h>
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
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))
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))
36 /* The extra casts in the following macros work around compiler bugs,
37 e.g., in Cray C 5.0.3.0. */
39 /* True if the real type T is signed. */
40 #define _GL_TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
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)
47 /* Minimum and maximum values for integer types and expressions. */
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)
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)
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
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. */
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
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
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))
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
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
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
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
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, \
173 (__typeof__ (*(r))) 0, \
174 (__typeof__ (*(r))) -1)) \
175 ? ((void) __builtin_mul_overflow (a, b, r), 1) \
176 : __builtin_mul_overflow (a, b, r))
177 # endif
178 #elif defined ckd_mul && !defined _GL_STDCKDINT_H
179 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) ckd_mul (r, + (a), + (b))
180 #else
181 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) \
182 _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
183 #endif
185 /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
186 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
187 https://llvm.org/bugs/show_bug.cgi?id=25390
188 For now, assume all versions of GCC-like compilers generate bogus
189 warnings for _Generic. This matters only for compilers that
190 lack relevant builtins. */
191 #if __GNUC__ || defined __clang__
192 # define _GL__GENERIC_BOGUS 1
193 #else
194 # define _GL__GENERIC_BOGUS 0
195 #endif
197 /* Store the low-order bits of A <op> B into *R, where OP specifies
198 the operation and OVERFLOW the overflow predicate. Return 1 if the
199 result overflows. Arguments should not have side effects,
200 and A, B and *R can be of any integer type other than char, bool, a
201 bit-precise integer type, or an enumeration type. */
202 #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
203 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
204 (_Generic \
205 (*(r), \
206 signed char: \
207 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
208 signed char, SCHAR_MIN, SCHAR_MAX), \
209 unsigned char: \
210 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
211 unsigned char, 0, UCHAR_MAX), \
212 short int: \
213 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
214 short int, SHRT_MIN, SHRT_MAX), \
215 unsigned short int: \
216 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
217 unsigned short int, 0, USHRT_MAX), \
218 int: \
219 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
220 int, INT_MIN, INT_MAX), \
221 unsigned int: \
222 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
223 unsigned int, 0, UINT_MAX), \
224 long int: \
225 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
226 long int, LONG_MIN, LONG_MAX), \
227 unsigned long int: \
228 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
229 unsigned long int, 0, ULONG_MAX), \
230 long long int: \
231 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
232 long long int, LLONG_MIN, LLONG_MAX), \
233 unsigned long long int: \
234 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
235 unsigned long long int, 0, ULLONG_MAX)))
236 #else
237 /* Store the low-order bits of A <op> B into *R, where OP specifies
238 the operation and OVERFLOW the overflow predicate. If *R is
239 signed, its type is ST with bounds SMIN..SMAX; otherwise its type
240 is UT with bounds U..UMAX. ST and UT are narrower than int.
241 Return 1 if the result overflows. Arguments should not have side
242 effects, and A, B and *R can be of any integer type other than
243 char, bool, a bit-precise integer type, or an enumeration type. */
244 # if _GL_HAVE___TYPEOF__
245 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
246 (_GL_TYPE_SIGNED (__typeof__ (*(r))) \
247 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
248 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
249 # else
250 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
251 (overflow (a, b, smin, smax) \
252 ? (overflow (a, b, 0, umax) \
253 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
254 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
255 : (overflow (a, b, 0, umax) \
256 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
257 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
258 # endif
260 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
261 (sizeof *(r) == sizeof (signed char) \
262 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
263 signed char, SCHAR_MIN, SCHAR_MAX, \
264 unsigned char, UCHAR_MAX) \
265 : sizeof *(r) == sizeof (short int) \
266 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
267 short int, SHRT_MIN, SHRT_MAX, \
268 unsigned short int, USHRT_MAX) \
269 : sizeof *(r) == sizeof (int) \
270 ? (_GL_EXPR_SIGNED (*(r)) \
271 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
272 int, INT_MIN, INT_MAX) \
273 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
274 unsigned int, 0, UINT_MAX)) \
275 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
276 # ifdef LLONG_MAX
277 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
278 (sizeof *(r) == sizeof (long int) \
279 ? (_GL_EXPR_SIGNED (*(r)) \
280 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
281 long int, LONG_MIN, LONG_MAX) \
282 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
283 unsigned long int, 0, ULONG_MAX)) \
284 : (_GL_EXPR_SIGNED (*(r)) \
285 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
286 long long int, LLONG_MIN, LLONG_MAX) \
287 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
288 unsigned long long int, 0, ULLONG_MAX)))
289 # else
290 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
291 (_GL_EXPR_SIGNED (*(r)) \
292 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
293 long int, LONG_MIN, LONG_MAX) \
294 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
295 unsigned long int, 0, ULONG_MAX))
296 # endif
297 #endif
299 /* Store the low-order bits of A <op> B into *R, where the operation
300 is given by OP. Use the unsigned type UT for calculation to avoid
301 overflow problems. *R's type is T, with extrema TMIN and TMAX.
302 T can be any signed integer type other than char, bool, a
303 bit-precise integer type, or an enumeration type.
304 Return 1 if the result overflows. */
305 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
306 (overflow (a, b, tmin, tmax) \
307 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
308 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
310 /* Return 1 if the integer expressions A - B and -A would overflow,
311 respectively. Arguments should not have side effects,
312 and can be any signed integer type other than char, bool, a
313 bit-precise integer type, or an enumeration type.
314 These macros are tuned for their last input argument being a constant. */
316 #if _GL_HAS_BUILTIN_OVERFLOW_P
317 # define _GL_INT_NEGATE_OVERFLOW(a) \
318 __builtin_sub_overflow_p (0, a, (__typeof__ (- (a))) 0)
319 #else
320 # define _GL_INT_NEGATE_OVERFLOW(a) \
321 _GL_INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
322 #endif
324 /* Return the low-order bits of A <op> B, where the operation is given
325 by OP. Use the unsigned type UT for calculation to avoid undefined
326 behavior on signed integer overflow, and convert the result to type T.
327 UT is at least as wide as T and is no narrower than unsigned int,
328 T is two's complement, and there is no padding or trap representations.
329 Assume that converting UT to T yields the low-order bits, as is
330 done in all known two's-complement C compilers. E.g., see:
331 https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
333 According to the C standard, converting UT to T yields an
334 implementation-defined result or signal for values outside T's
335 range. However, code that works around this theoretical problem
336 runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
337 https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
338 As the compiler bug is real, don't try to work around the
339 theoretical problem. */
341 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
342 ((t) ((ut) (a) op (ut) (b)))
344 /* Return true if the numeric values A + B, A - B, A * B fall outside
345 the range TMIN..TMAX. Arguments should not have side effects
346 and can be any integer type other than char, bool,
347 a bit-precise integer type, or an enumeration type.
348 TMIN should be signed and nonpositive.
349 TMAX should be positive, and should be signed unless TMIN is zero. */
350 #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
351 ((b) < 0 \
352 ? (((tmin) \
353 ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
354 && (a) < (tmin) - (b)) \
355 : (a) <= -1 - (b)) \
356 || ((_GL_EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
357 : (a) < 0 \
358 ? (((tmin) \
359 ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
360 && (b) < (tmin) - (a)) \
361 : (b) <= -1 - (a)) \
362 || ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
363 && (tmax) < (a) + (b))) \
364 : (tmax) < (b) || (tmax) - (b) < (a))
365 #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
366 (((a) < 0) == ((b) < 0) \
367 ? ((a) < (b) \
368 ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
369 : (tmax) < (a) - (b)) \
370 : (a) < 0 \
371 ? ((!_GL_EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
372 || (a) - (tmin) < (b)) \
373 : ((! (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
374 && _GL_EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
375 && (tmax) <= -1 - (b)) \
376 || (tmax) + (b) < (a)))
377 #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
378 ((b) < 0 \
379 ? ((a) < 0 \
380 ? (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
381 ? (a) < (tmax) / (b) \
382 : ((_GL_INT_NEGATE_OVERFLOW (b) \
383 ? _GL_INT_CONVERT (b, tmax) >> (_GL_TYPE_WIDTH (+ (b)) - 1) \
384 : (tmax) / -(b)) \
385 <= -1 - (a))) \
386 : _GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
387 ? (_GL_EXPR_SIGNED (a) \
388 ? 0 < (a) + (tmin) \
389 : 0 < (a) && -1 - (tmin) < (a) - 1) \
390 : (tmin) / (b) < (a)) \
391 : (b) == 0 \
392 ? 0 \
393 : ((a) < 0 \
394 ? (_GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
395 ? (_GL_EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
396 : (tmin) / (a) < (b)) \
397 : (tmax) / (b) < (a)))
399 #endif /* _GL_INTPROPS_INTERNAL_H */