1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #ifndef _GL_INTPROPS_INTERNAL_H
19 #define _GL_INTPROPS_INTERNAL_H
20
21 #include <limits.h>
22
23
24
25 #define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
26
27
28
29 #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
30
31
32
33
34
35 #define _GL_TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
36
37
38
39 #define _GL_EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
40
41
42
43
44
45
46
47 #define _GL_TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
48
49
50
51 #define _GL_INT_MINIMUM(e) \
52 (_GL_EXPR_SIGNED (e) \
53 ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
54 : _GL_INT_CONVERT (e, 0))
55 #define _GL_INT_MAXIMUM(e) \
56 (_GL_EXPR_SIGNED (e) \
57 ? _GL_SIGNED_INT_MAXIMUM (e) \
58 : _GL_INT_NEGATE_CONVERT (e, 1))
59 #define _GL_SIGNED_INT_MAXIMUM(e) \
60 (((_GL_INT_CONVERT (e, 1) << (_GL_TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)
61
62
63 #if !defined LLONG_MAX && defined __INT64_MAX
64 # define LLONG_MAX __INT64_MAX
65 # define LLONG_MIN __INT64_MIN
66 #endif
67
68
69
70
71
72
73
74
75 #if (2 <= __GNUC__ \
76 || (4 <= __clang_major__) \
77 || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
78 || (0x5110 <= __SUNPRO_C && !__STDC__))
79 # define _GL_HAVE___TYPEOF__ 1
80 #else
81 # define _GL_HAVE___TYPEOF__ 0
82 #endif
83
84
85
86
87
88 #if _GL_HAVE___TYPEOF__
89 # define _GL_SIGNED_TYPE_OR_EXPR(t) _GL_TYPE_SIGNED (__typeof__ (t))
90 #else
91 # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
92 #endif
93
94
95
96
97 #define _GL_INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
98 ((min) < 0 ? (a) < - (max) : 0 < (a))
99
100
101
102 #ifdef __EDG__
103
104
105 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
106 #elif defined __has_builtin
107 # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
108
109
110 #elif 7 <= __GNUC__
111 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
112 #else
113 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
114 #endif
115
116
117 #if defined __clang_major__ && __clang_major__ < 14
118
119 # define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
120 #else
121 # define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
122 #endif
123
124
125
126 #ifdef __EDG__
127
128
129
130 # define _GL_HAS_BUILTIN_OVERFLOW_P 0
131 #elif defined __has_builtin
132 # define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
133 #else
134 # define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
135 #endif
136
137 #if (!defined _GL_STDCKDINT_H && 202311 <= __STDC_VERSION__ \
138 && ! (_GL_HAS_BUILTIN_ADD_OVERFLOW && _GL_HAS_BUILTIN_MUL_OVERFLOW))
139 # include <stdckdint.h>
140 #endif
141
142
143
144
145
146 #if _GL_HAS_BUILTIN_ADD_OVERFLOW
147 # define _GL_INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
148 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
149 #elif defined ckd_add && defined ckd_sub && !defined _GL_STDCKDINT_H
150 # define _GL_INT_ADD_WRAPV(a, b, r) ckd_add (r, + (a), + (b))
151 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) ckd_sub (r, + (a), + (b))
152 #else
153 # define _GL_INT_ADD_WRAPV(a, b, r) \
154 _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
155 # define _GL_INT_SUBTRACT_WRAPV(a, b, r) \
156 _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
157 #endif
158 #if _GL_HAS_BUILTIN_MUL_OVERFLOW
159 # if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
160 || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \
161 && !defined __EDG__)
162 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
163 # else
164
165 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) \
166 ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && _GL_EXPR_SIGNED (a) && _GL_EXPR_SIGNED (b) \
167 && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
168 ? ((void) __builtin_mul_overflow (a, b, r), 1) \
169 : __builtin_mul_overflow (a, b, r))
170 # endif
171 #elif defined ckd_mul && !defined _GL_STDCKDINT_H
172 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) ckd_mul (r, + (a), + (b))
173 #else
174 # define _GL_INT_MULTIPLY_WRAPV(a, b, r) \
175 _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
176 #endif
177
178
179
180
181
182
183
184 #if __GNUC__ || defined __clang__
185 # define _GL__GENERIC_BOGUS 1
186 #else
187 # define _GL__GENERIC_BOGUS 0
188 #endif
189
190
191
192
193
194
195 #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
196 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
197 (_Generic \
198 (*(r), \
199 signed char: \
200 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
201 signed char, SCHAR_MIN, SCHAR_MAX), \
202 unsigned char: \
203 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
204 unsigned char, 0, UCHAR_MAX), \
205 short int: \
206 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
207 short int, SHRT_MIN, SHRT_MAX), \
208 unsigned short int: \
209 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
210 unsigned short int, 0, USHRT_MAX), \
211 int: \
212 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
213 int, INT_MIN, INT_MAX), \
214 unsigned int: \
215 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
216 unsigned int, 0, UINT_MAX), \
217 long int: \
218 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
219 long int, LONG_MIN, LONG_MAX), \
220 unsigned long int: \
221 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
222 unsigned long int, 0, ULONG_MAX), \
223 long long int: \
224 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
225 long long int, LLONG_MIN, LLONG_MAX), \
226 unsigned long long int: \
227 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
228 unsigned long long int, 0, ULLONG_MAX)))
229 #else
230
231
232
233
234
235
236
237 # if _GL_HAVE___TYPEOF__
238 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
239 (_GL_TYPE_SIGNED (__typeof__ (*(r))) \
240 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
241 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
242 # else
243 # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
244 (overflow (a, b, smin, smax) \
245 ? (overflow (a, b, 0, umax) \
246 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
247 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
248 : (overflow (a, b, 0, umax) \
249 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
250 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
251 # endif
252
253 # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
254 (sizeof *(r) == sizeof (signed char) \
255 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
256 signed char, SCHAR_MIN, SCHAR_MAX, \
257 unsigned char, UCHAR_MAX) \
258 : sizeof *(r) == sizeof (short int) \
259 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
260 short int, SHRT_MIN, SHRT_MAX, \
261 unsigned short int, USHRT_MAX) \
262 : sizeof *(r) == sizeof (int) \
263 ? (_GL_EXPR_SIGNED (*(r)) \
264 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
265 int, INT_MIN, INT_MAX) \
266 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
267 unsigned int, 0, UINT_MAX)) \
268 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
269 # ifdef LLONG_MAX
270 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
271 (sizeof *(r) == sizeof (long int) \
272 ? (_GL_EXPR_SIGNED (*(r)) \
273 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
274 long int, LONG_MIN, LONG_MAX) \
275 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
276 unsigned long int, 0, ULONG_MAX)) \
277 : (_GL_EXPR_SIGNED (*(r)) \
278 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
279 long long int, LLONG_MIN, LLONG_MAX) \
280 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
281 unsigned long long int, 0, ULLONG_MAX)))
282 # else
283 # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
284 (_GL_EXPR_SIGNED (*(r)) \
285 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
286 long int, LONG_MIN, LONG_MAX) \
287 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
288 unsigned long int, 0, ULONG_MAX))
289 # endif
290 #endif
291
292
293
294
295
296
297
298 #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
299 (overflow (a, b, tmin, tmax) \
300 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
301 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
302
303
304
305
306
307
308
309 #if _GL_HAS_BUILTIN_OVERFLOW_P
310 # define _GL_INT_NEGATE_OVERFLOW(a) \
311 __builtin_sub_overflow_p (0, a, (__typeof__ (- (a))) 0)
312 #else
313 # define _GL_INT_NEGATE_OVERFLOW(a) \
314 _GL_INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
315 #endif
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334 #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
335 ((t) ((ut) (a) op (ut) (b)))
336
337
338
339
340
341
342
343 #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
344 ((b) < 0 \
345 ? (((tmin) \
346 ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
347 && (a) < (tmin) - (b)) \
348 : (a) <= -1 - (b)) \
349 || ((_GL_EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
350 : (a) < 0 \
351 ? (((tmin) \
352 ? ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
353 && (b) < (tmin) - (a)) \
354 : (b) <= -1 - (a)) \
355 || ((_GL_EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
356 && (tmax) < (a) + (b))) \
357 : (tmax) < (b) || (tmax) - (b) < (a))
358 #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
359 (((a) < 0) == ((b) < 0) \
360 ? ((a) < (b) \
361 ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
362 : (tmax) < (a) - (b)) \
363 : (a) < 0 \
364 ? ((!_GL_EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
365 || (a) - (tmin) < (b)) \
366 : ((! (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
367 && _GL_EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
368 && (tmax) <= -1 - (b)) \
369 || (tmax) + (b) < (a)))
370 #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
371 ((b) < 0 \
372 ? ((a) < 0 \
373 ? (_GL_EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
374 ? (a) < (tmax) / (b) \
375 : ((_GL_INT_NEGATE_OVERFLOW (b) \
376 ? _GL_INT_CONVERT (b, tmax) >> (_GL_TYPE_WIDTH (+ (b)) - 1) \
377 : (tmax) / -(b)) \
378 <= -1 - (a))) \
379 : _GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
380 ? (_GL_EXPR_SIGNED (a) \
381 ? 0 < (a) + (tmin) \
382 : 0 < (a) && -1 - (tmin) < (a) - 1) \
383 : (tmin) / (b) < (a)) \
384 : (b) == 0 \
385 ? 0 \
386 : ((a) < 0 \
387 ? (_GL_INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
388 ? (_GL_EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
389 : (tmin) / (a) < (b)) \
390 : (tmax) / (b) < (a)))
391
392 #endif