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