xref: /freebsd/include/stdckdint.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * Copyright (c) 2023 Dag-Erling Smørgrav
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #ifndef __STDC_VERSION_STDCKDINT_H__
8 #define __STDC_VERSION_STDCKDINT_H__ 202311L
9 
10 #include <sys/cdefs.h>
11 
12 #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 2023
13 
14 #if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_add_overflow)
15 #define ckd_add(result, a, b)						\
16 	(_Bool)__builtin_add_overflow((a), (b), (result))
17 #else
18 #define ckd_add(result, a, b)						\
19 	_Static_assert(0, "checked addition not supported")
20 #endif
21 
22 #if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_sub_overflow)
23 #define ckd_sub(result, a, b)						\
24 	(_Bool)__builtin_sub_overflow((a), (b), (result))
25 #else
26 #define ckd_sub(result, a, b)						\
27 	_Static_assert(0, "checked subtraction not supported")
28 #endif
29 
30 #if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_mul_overflow)
31 #define ckd_mul(result, a, b)						\
32 	(_Bool)__builtin_mul_overflow((a), (b), (result))
33 #else
34 #define ckd_mul(result, a, b)						\
35 	_Static_assert(0, "checked multiplication not supported")
36 #endif
37 
38 #endif
39 
40 #endif
41