xref: /freebsd/contrib/llvm-project/clang/lib/Headers/stdckdint.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1*5f757f3fSDimitry Andric /*===---- stdckdint.h - Standard header for checking integer----------------===
2*5f757f3fSDimitry Andric  *
3*5f757f3fSDimitry Andric  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*5f757f3fSDimitry Andric  * See https://llvm.org/LICENSE.txt for license information.
5*5f757f3fSDimitry Andric  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*5f757f3fSDimitry Andric  *
7*5f757f3fSDimitry Andric  *===-----------------------------------------------------------------------===
8*5f757f3fSDimitry Andric  */
9*5f757f3fSDimitry Andric 
10*5f757f3fSDimitry Andric #ifndef __STDCKDINT_H
11*5f757f3fSDimitry Andric #define __STDCKDINT_H
12*5f757f3fSDimitry Andric 
13*5f757f3fSDimitry Andric /* If we're hosted, fall back to the system's stdckdint.h. FreeBSD, for
14*5f757f3fSDimitry Andric  * example, already has a Clang-compatible stdckdint.h header.
15*5f757f3fSDimitry Andric  *
16*5f757f3fSDimitry Andric  * The `stdckdint.h` header requires C 23 or newer.
17*5f757f3fSDimitry Andric  */
18*5f757f3fSDimitry Andric #if __STDC_HOSTED__ && __has_include_next(<stdckdint.h>)
19*5f757f3fSDimitry Andric #include_next <stdckdint.h>
20*5f757f3fSDimitry Andric #else
21*5f757f3fSDimitry Andric 
22*5f757f3fSDimitry Andric /* C23 7.20.1 Defines several macros for performing checked integer arithmetic*/
23*5f757f3fSDimitry Andric 
24*5f757f3fSDimitry Andric #define __STDC_VERSION_STDCKDINT_H__ 202311L
25*5f757f3fSDimitry Andric 
26*5f757f3fSDimitry Andric // Both A and B shall be any integer type other than "plain" char, bool, a bit-
27*5f757f3fSDimitry Andric // precise integer type, or an enumerated type, and they need not be the same.
28*5f757f3fSDimitry Andric 
29*5f757f3fSDimitry Andric // R shall be a modifiable lvalue of any integer type other than "plain" char,
30*5f757f3fSDimitry Andric // bool, a bit-precise integer type, or an enumerated type. It shouldn't be
31*5f757f3fSDimitry Andric // short type, either. Otherwise, it may be unable to hold two the result of
32*5f757f3fSDimitry Andric // operating two 'int's.
33*5f757f3fSDimitry Andric 
34*5f757f3fSDimitry Andric // A diagnostic message will be produced if A or B are not suitable integer
35*5f757f3fSDimitry Andric // types, or if R is not a modifiable lvalue of a suitable integer type or R
36*5f757f3fSDimitry Andric // is short type.
37*5f757f3fSDimitry Andric #define ckd_add(R, A, B) __builtin_add_overflow((A), (B), (R))
38*5f757f3fSDimitry Andric #define ckd_sub(R, A, B) __builtin_sub_overflow((A), (B), (R))
39*5f757f3fSDimitry Andric #define ckd_mul(R, A, B) __builtin_mul_overflow((A), (B), (R))
40*5f757f3fSDimitry Andric 
41*5f757f3fSDimitry Andric #endif /* __STDC_HOSTED__ */
42*5f757f3fSDimitry Andric #endif /* __STDCKDINT_H */
43