Debian Codegraph @

src/utils-checked-arithmetic.h

raw
1 /*
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright 2023 Justine Alexandra Roberts Tunney
5 *
6 * Permission to use, copy, modify, and/or distribute this software for
7 * any purpose with or without fee is hereby granted, provided that the
8 * above copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /**
21 * @fileoverview C23 Checked Arithmetic
22 *
23 * This header defines three type generic functions:
24 *
25 * - `bool ckd_add(res, a, b)`
26 * - `bool ckd_sub(res, a, b)`
27 * - `bool ckd_mul(res, a, b)`
28 *
29 * Which allow integer arithmetic errors to be detected. There are many
30 * kinds of integer errors, e.g. overflow, truncation, etc. These funcs
31 * catch them all. Here's an example of how it works:
32 *
33 * uint32_t c;
34 * int32_t a = 0x7fffffff;
35 * int32_t b = 2;
36 * assert(!ckd_add(&c, a, b));
37 * assert(c == 0x80000001u);
38 *
39 * Experienced C / C++ users should find this example counter-intuitive
40 * because the expression `0x7fffffff + 2` not only overflows it's also
41 * undefined behavior. However here we see it's specified, and does not
42 * result in an error. That's because C23 checked arithmetic is not the
43 * arithmetic you're used to. The new standard changes the mathematics.
44 *
45 * C23 checked arithmetic is defined as performing the arithmetic using
46 * infinite precision and then checking if the resulting value will fit
47 * in the output type. Our example above did not result in an error due
48 * to `0x80000001` being a legal value for `uint32_t`.
49 *
50 * This implementation will use the GNU compiler builtins, when they're
51 * available, only if you don't use build flags like `-std=c11` because
52 * they define `__STRICT_ANSI__` and GCC extensions aren't really ANSI.
53 * Instead, you'll get a pretty good pure C11 and C++11 implementation.
54 *
55 * @see https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf
56 * @version 1.0 (2024-12-07)
57 */
58
59 #pragma once
60
61 #include "config.h"
62
63 #ifdef __has_include
64 #define __ckd_has_include(x) __has_include(x)
65 #else
66 #define __ckd_has_include(x) 0
67 #endif
68
69 #if __ckd_has_include(<stdckdint.h>)
70 #include <stdckdint.h>
71 #else
72
73 #define __STDC_VERSION_STDCKDINT_H__ 202311L
74
75 #if (!defined(__STRICT_ANSI__) && defined(__SIZEOF_INT128__))
76 #define __ckd_have_int128
77 #define __ckd_intmax __int128
78 #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
79 #define __ckd_intmax long long
80 #else
81 #define __ckd_intmax long
82 #endif
83
84 typedef signed __ckd_intmax __ckd_intmax_t;
85 typedef unsigned __ckd_intmax __ckd_uintmax_t;
86
87 #ifdef __has_builtin
88 #define __ckd_has_builtin(x) __has_builtin(x)
89 #else
90 #define __ckd_has_builtin(x) 0
91 #endif
92
93 #if (!defined(__STRICT_ANSI__) && \
94 ((defined(__GNUC__) && __GNUC__ >= 5 && !defined(__ICC)) || \
95 (__ckd_has_builtin(__builtin_add_overflow) && \
96 __ckd_has_builtin(__builtin_sub_overflow) && \
97 __ckd_has_builtin(__builtin_mul_overflow))))
98 #define ckd_add(res, x, y) __builtin_add_overflow((x), (y), (res))
99 #define ckd_sub(res, x, y) __builtin_sub_overflow((x), (y), (res))
100 #define ckd_mul(res, x, y) __builtin_mul_overflow((x), (y), (res))
101
102 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
103
104 #define ckd_add(res, a, b) __ckd_expr(add, (res), (a), (b))
105 #define ckd_sub(res, a, b) __ckd_expr(sub, (res), (a), (b))
106 #define ckd_mul(res, a, b) __ckd_expr(mul, (res), (a), (b))
107
108 #if defined(__GNUC__) || defined(__llvm__)
109 #define __ckd_inline \
110 extern __inline __attribute__((__gnu_inline__, \
111 __always_inline__, \
112 __artificial__))
113 #else
114 #define __ckd_inline static inline
115 #endif
116
117 #ifdef __ckd_have_int128
118 #define __ckd_generic_int128(x, y) , signed __int128: x, unsigned __int128: y
119 #else
120 #define __ckd_generic_int128(x, y)
121 #endif
122
123 #define __ckd_sign(T) \
124 ((T)1 << (sizeof(T) * 8 - 1))
125
126 #define __ckd_is_signed(x) \
127 _Generic(x, \
128 signed char: 1, \
129 unsigned char: 0, \
130 signed short: 1, \
131 unsigned short: 0, \
132 signed int: 1, \
133 unsigned int: 0, \
134 signed long: 1, \
135 unsigned long: 0, \
136 signed long long: 1, \
137 unsigned long long: 0 \
138 __ckd_generic_int128(1, 0))
139
140 #define __ckd_expr(op, res, a, b) \
141 (_Generic(*res, \
142 signed char: __ckd_##op##_schar, \
143 unsigned char: __ckd_##op##_uchar, \
144 signed short: __ckd_##op##_sshort, \
145 unsigned short: __ckd_##op##_ushort, \
146 signed int: __ckd_##op##_sint, \
147 unsigned int: __ckd_##op##_uint, \
148 signed long: __ckd_##op##_slong, \
149 unsigned long: __ckd_##op##_ulong, \
150 signed long long: __ckd_##op##_slonger, \
151 unsigned long long: __ckd_##op##_ulonger \
152 __ckd_generic_int128( \
153 __ckd_##op##_sint128, \
154 __ckd_##op##_uint128))( \
155 res, a, b, \
156 __ckd_is_signed(a), \
157 __ckd_is_signed(b)))
158
159 #define __ckd_declare_add(S, T) \
160 __ckd_inline char S(void *__res, \
161 __ckd_uintmax_t __x, \
162 __ckd_uintmax_t __y, \
163 char __a_signed, \
164 char __b_signed) { \
165 __ckd_uintmax_t __z = __x + __y; \
166 *(T *)__res = (T)__z; \
167 char __truncated = 0; \
168 if (sizeof(T) < sizeof(__ckd_intmax_t)) { \
169 __truncated = __z != (__ckd_uintmax_t)(T)__z; \
170 } \
171 switch (__ckd_is_signed((T)0) << 2 | \
172 __a_signed << 1 | __b_signed) { \
173 case 0: /* u = u + u */ \
174 return __truncated | (__z < __x); \
175 case 1: /* u = u + s */ \
176 __y ^= __ckd_sign(__ckd_uintmax_t); \
177 return __truncated | \
178 ((__ckd_intmax_t)((__z ^ __x) & \
179 (__z ^ __y)) < 0); \
180 case 2: /* u = s + u */ \
181 __x ^= __ckd_sign(__ckd_uintmax_t); \
182 return __truncated | \
183 ((__ckd_intmax_t)((__z ^ __x) & \
184 (__z ^ __y)) < 0); \
185 case 3: /* u = s + s */ \
186 return __truncated | \
187 ((__ckd_intmax_t)(((__z | __x) & __y) | \
188 ((__z & __x) & ~__y)) < 0); \
189 case 4: /* s = u + u */ \
190 return __truncated | (__z < __x) | ((__ckd_intmax_t)__z < 0); \
191 case 5: /* s = u + s */ \
192 __y ^= __ckd_sign(__ckd_uintmax_t); \
193 return __truncated | (__x + __y < __y); \
194 case 6: /* s = s + u */ \
195 __x ^= __ckd_sign(__ckd_uintmax_t); \
196 return __truncated | (__x + __y < __x); \
197 case 7: /* s = s + s */ \
198 return __truncated | \
199 ((__ckd_intmax_t)((__z ^ __x) & \
200 (__z ^ __y)) < 0); \
201 default: \
202 for (;;) (void)0; \
203 } \
204 }
205
206 __ckd_declare_add(__ckd_add_schar, signed char)
207 __ckd_declare_add(__ckd_add_uchar, unsigned char)
208 __ckd_declare_add(__ckd_add_sshort, signed short)
209 __ckd_declare_add(__ckd_add_ushort, unsigned short)
210 __ckd_declare_add(__ckd_add_sint, signed int)
211 __ckd_declare_add(__ckd_add_uint, unsigned int)
212 __ckd_declare_add(__ckd_add_slong, signed long)
213 __ckd_declare_add(__ckd_add_ulong, unsigned long)
214 __ckd_declare_add(__ckd_add_slonger, signed long long)
215 __ckd_declare_add(__ckd_add_ulonger, unsigned long long)
216 #ifdef __ckd_have_int128
217 __ckd_declare_add(__ckd_add_sint128, signed __int128)
218 __ckd_declare_add(__ckd_add_uint128, unsigned __int128)
219 #endif
220
221 #define __ckd_declare_sub(S, T) \
222 __ckd_inline char S(void *__res, \
223 __ckd_uintmax_t __x, \
224 __ckd_uintmax_t __y, \
225 char __a_signed, \
226 char __b_signed) { \
227 __ckd_uintmax_t __z = __x - __y; \
228 *(T *)__res = (T)__z; \
229 char __truncated = 0; \
230 if (sizeof(T) < sizeof(__ckd_intmax_t)) { \
231 __truncated = __z != (__ckd_uintmax_t)(T)__z; \
232 } \
233 switch (__ckd_is_signed((T)0) << 2 | \
234 __a_signed << 1 | __b_signed) { \
235 case 0: /* u = u - u */ \
236 return __truncated | (__x < __y); \
237 case 1: /* u = u - s */ \
238 __y ^= __ckd_sign(__ckd_uintmax_t); \
239 return __truncated | \
240 ((__ckd_intmax_t)((__x ^ __y) & \
241 (__z ^ __x)) < 0); \
242 case 2: /* u = s - u */ \
243 return __truncated | (__y > __x) | ((__ckd_intmax_t)__x < 0); \
244 case 3: /* u = s - s */ \
245 return __truncated | \
246 ((__ckd_intmax_t)(((__z & __x) & __y) | \
247 ((__z | __x) & ~__y)) < 0); \
248 case 4: /* s = u - u */ \
249 return __truncated | ((__x < __y) ^ ((__ckd_intmax_t)__z < 0)); \
250 case 5: /* s = u - s */ \
251 __y ^= __ckd_sign(__ckd_uintmax_t); \
252 return __truncated | (__x >= __y); \
253 case 6: /* s = s - u */ \
254 __x ^= __ckd_sign(__ckd_uintmax_t); \
255 return __truncated | (__x < __y); \
256 case 7: /* s = s - s */ \
257 return __truncated | \
258 ((__ckd_intmax_t)((__x ^ __y) & \
259 (__z ^ __x)) < 0); \
260 default: \
261 for (;;) (void)0; \
262 } \
263 }
264
265 __ckd_declare_sub(__ckd_sub_schar, signed char)
266 __ckd_declare_sub(__ckd_sub_uchar, unsigned char)
267 __ckd_declare_sub(__ckd_sub_sshort, signed short)
268 __ckd_declare_sub(__ckd_sub_ushort, unsigned short)
269 __ckd_declare_sub(__ckd_sub_sint, signed int)
270 __ckd_declare_sub(__ckd_sub_uint, unsigned int)
271 __ckd_declare_sub(__ckd_sub_slong, signed long)
272 __ckd_declare_sub(__ckd_sub_ulong, unsigned long)
273 __ckd_declare_sub(__ckd_sub_slonger, signed long long)
274 __ckd_declare_sub(__ckd_sub_ulonger, unsigned long long)
275 #ifdef __ckd_have_int128
276 __ckd_declare_sub(__ckd_sub_sint128, signed __int128)
277 __ckd_declare_sub(__ckd_sub_uint128, unsigned __int128)
278 #endif
279
280 #define __ckd_declare_mul(S, T) \
281 __ckd_inline char S(void *__res, \
282 __ckd_uintmax_t __x, \
283 __ckd_uintmax_t __y, \
284 char __a_signed, \
285 char __b_signed) { \
286 switch (__ckd_is_signed((T)0) << 2 | \
287 __a_signed << 1 | __b_signed) { \
288 case 0: { /* u = u * u */ \
289 __ckd_uintmax_t __z = __x * __y; \
290 int __o = __x && __z / __x != __y; \
291 *(T *)__res = (T)__z; \
292 return (char) \
293 (__o | (sizeof(T) < sizeof(__z) && \
294 __z != (__ckd_uintmax_t)*(T *)__res)); \
295 } \
296 case 1: { /* u = u * s */ \
297 __ckd_uintmax_t __z = __x * __y; \
298 int __o = __x && __z / __x != __y; \
299 *(T *)__res = (T)__z; \
300 return (char) \
301 (__o | (((__ckd_intmax_t)__y < 0) & !!__x) | \
302 (sizeof(T) < sizeof(__z) && \
303 __z != (__ckd_uintmax_t)*(T *)__res)); \
304 } \
305 case 2: { /* u = s * u */ \
306 __ckd_uintmax_t __z = __x * __y; \
307 int __o = __x && __z / __x != __y; \
308 *(T *)__res = (T)__z; \
309 return (char) \
310 (__o | (((__ckd_intmax_t)__x < 0) & !!__y) | \
311 (sizeof(T) < sizeof(__z) && \
312 __z != (__ckd_uintmax_t)*(T *)__res)); \
313 } \
314 case 3: { /* u = s * s */ \
315 int __o = 0; \
316 if ((__ckd_intmax_t)(__x & __y) < 0) { \
317 __x = 0 - __x; \
318 __y = 0 - __y; \
319 } else if ((__ckd_intmax_t)(__x ^ __y) < 0) { \
320 __o = __x && __y; \
321 } \
322 __ckd_uintmax_t __z = __x * __y; \
323 __o |= __x && __z / __x != __y; \
324 *(T *)__res = (T)__z; \
325 return (char) \
326 (__o | (sizeof(T) < sizeof(__z) && \
327 __z != (__ckd_uintmax_t)*(T *)__res)); \
328 } \
329 case 4: { /* s = u * u */ \
330 __ckd_uintmax_t __z = __x * __y; \
331 int __o = __x && __z / __x != __y; \
332 *(T *)__res = (T)__z; \
333 return (char) \
334 (__o | ((__ckd_intmax_t)(__z) < 0) | \
335 (sizeof(T) < sizeof(__z) && \
336 __z != (__ckd_uintmax_t)*(T *)__res)); \
337 } \
338 case 5: { /* s = u * s */ \
339 __ckd_uintmax_t __t = 0 - __y; \
340 __t = (__ckd_intmax_t)(__t) < 0 ? __y : __t; \
341 __ckd_uintmax_t __p = __t * __x; \
342 int __o = __t && __p / __t != __x; \
343 int __n = (__ckd_intmax_t)__y < 0; \
344 __ckd_uintmax_t __z = __n ? 0 - __p : __p; \
345 *(T *)__res = (T)__z; \
346 __ckd_uintmax_t __m = __ckd_sign(__ckd_uintmax_t) - 1; \
347 return (char) \
348 (__o | (__p > __m + __n) | \
349 (sizeof(T) < sizeof(__z) && \
350 __z != (__ckd_uintmax_t)*(T *)__res)); \
351 } \
352 case 6: { /* s = s * u */ \
353 __ckd_uintmax_t __t = 0 - __x; \
354 __t = (__ckd_intmax_t)(__t) < 0 ? __x : __t; \
355 __ckd_uintmax_t __p = __t * __y; \
356 int __o = __t && __p / __t != __y; \
357 int __n = (__ckd_intmax_t)__x < 0; \
358 __ckd_uintmax_t __z = __n ? 0 - __p : __p; \
359 *(T *)__res = (T)__z; \
360 __ckd_uintmax_t __m = __ckd_sign(__ckd_uintmax_t) - 1; \
361 return (char) \
362 (__o | (__p > __m + __n) | \
363 (sizeof(T) < sizeof(__z) && \
364 __z != (__ckd_uintmax_t)*(T *)__res)); \
365 } \
366 case 7: { /* s = s * s */ \
367 __ckd_uintmax_t __z = __x * __y; \
368 *(T *)__res = (T)__z; \
369 return (char) \
370 (((((__ckd_intmax_t)__y < 0) && \
371 (__x == __ckd_sign(__ckd_uintmax_t))) || \
372 (__y && (((__ckd_intmax_t)__z / \
373 (__ckd_intmax_t)__y) != \
374 (__ckd_intmax_t)__x))) | \
375 (sizeof(T) < sizeof(__z) && \
376 __z != (__ckd_uintmax_t)*(T *)__res)); \
377 } \
378 default: \
379 for (;;) (void)0; \
380 } \
381 }
382
383 __ckd_declare_mul(__ckd_mul_schar, signed char)
384 __ckd_declare_mul(__ckd_mul_uchar, unsigned char)
385 __ckd_declare_mul(__ckd_mul_sshort, signed short)
386 __ckd_declare_mul(__ckd_mul_ushort, unsigned short)
387 __ckd_declare_mul(__ckd_mul_sint, signed int)
388 __ckd_declare_mul(__ckd_mul_uint, unsigned int)
389 __ckd_declare_mul(__ckd_mul_slong, signed long)
390 __ckd_declare_mul(__ckd_mul_ulong, unsigned long)
391 __ckd_declare_mul(__ckd_mul_slonger, signed long long)
392 __ckd_declare_mul(__ckd_mul_ulonger, unsigned long long)
393 #ifdef __ckd_have_int128
394 __ckd_declare_mul(__ckd_mul_sint128, signed __int128)
395 __ckd_declare_mul(__ckd_mul_uint128, unsigned __int128)
396 #endif
397
398 #else
399 #ifdef _MSC_VER
400 #pragma warning("checked integer arithmetic unsupported in this environment")
401 #else
402 #pragma GCC warning "checked integer arithmetic unsupported in this environment"
403 #endif
404
405 #define ckd_add(res, x, y) (*(res) = (x) + (y), 0)
406 #define ckd_sub(res, x, y) (*(res) = (x) - (y), 0)
407 #define ckd_mul(res, x, y) (*(res) = (x) * (y), 0)
408
409 #endif /* GNU */
410 #endif /* stdckdint.h */