1 /*
2  * Copyright (c) 2019 BayLibre SAS
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_TOOLCHAIN_STDINT_H_
8 #define ZEPHYR_INCLUDE_TOOLCHAIN_STDINT_H_
9 
10 /*
11  * Some gcc versions and/or configurations as found in the Zephyr SDK
12  * (questionably) define __INT32_TYPE__ and derivatives as a long int
13  * which makes the printf format checker to complain about long vs int
14  * mismatch when %u is given a uint32_t argument, and uint32_t pointers not
15  * being compatible with int pointers. Let's redefine them to follow
16  * common expectations and usage.
17  */
18 
19 /*
20  * If the compiler does not define __SIZEOF_INT__ deduce it from __INT_MAX__
21  * or INT_MAX.
22  */
23 #if !defined(__SIZEOF_INT__)
24 
25 #if defined(__INT_MAX__)
26 /* GCC >= 3.3.0 has __<val>__ implicitly defined. */
27 #define __Z_INT_MAX __INT_MAX__
28 #else
29 /* Fall back to POSIX versions from <limits.h> */
30 #define __Z_INT_MAX INT_MAX
31 #include <limits.h>
32 #endif
33 
34 #if __Z_INT_MAX == 0x7fff
35 #define __SIZEOF_INT__ 2
36 #elif __Z_INT_MAX == 0x7fffffffL
37 #define __SIZEOF_INT__ 4
38 #elif __Z_INT_MAX > 0x7fffffffL
39 #define __SIZEOF_INT__ 8
40 #endif
41 
42 #undef __Z_INT_MAX
43 
44 #endif
45 
46 #if __SIZEOF_INT__ != 4
47 #error "unexpected int width"
48 #endif
49 
50 #undef __INT32_TYPE__
51 #undef __UINT32_TYPE__
52 #undef __INT_FAST32_TYPE__
53 #undef __UINT_FAST32_TYPE__
54 #undef __INT_LEAST32_TYPE__
55 #undef __UINT_LEAST32_TYPE__
56 #undef __INT64_TYPE__
57 #undef __UINT64_TYPE__
58 #undef __INT_FAST64_TYPE__
59 #undef __UINT_FAST64_TYPE__
60 #undef __INT_LEAST64_TYPE__
61 #undef __UINT_LEAST64_TYPE__
62 
63 #define __INT32_TYPE__ int
64 #define __UINT32_TYPE__ unsigned int
65 #define __INT_FAST32_TYPE__ __INT32_TYPE__
66 #define __UINT_FAST32_TYPE__ __UINT32_TYPE__
67 #define __INT_LEAST32_TYPE__ __INT32_TYPE__
68 #define __UINT_LEAST32_TYPE__ __UINT32_TYPE__
69 #define __INT64_TYPE__ long long int
70 #define __UINT64_TYPE__ unsigned long long int
71 #define __INT_FAST64_TYPE__ __INT64_TYPE__
72 #define __UINT_FAST64_TYPE__ __UINT64_TYPE__
73 #define __INT_LEAST64_TYPE__ __INT64_TYPE__
74 #define __UINT_LEAST64_TYPE__ __UINT64_TYPE__
75 
76 /*
77  * The confusion also exists with __INTPTR_TYPE__ which is either an int
78  * (even when __INT32_TYPE__ is a long int) or a long int. Let's redefine
79  * it to a long int to get some uniformity. Doing so also makes it compatible
80  * with LP64 (64-bit) targets where a long is always 64-bit wide.
81  */
82 
83 #if __SIZEOF_POINTER__ != __SIZEOF_LONG__
84 #error "unexpected size difference between pointers and long ints"
85 #endif
86 
87 #undef __INTPTR_TYPE__
88 #undef __UINTPTR_TYPE__
89 #define __INTPTR_TYPE__ long int
90 #define __UINTPTR_TYPE__ long unsigned int
91 
92 /*
93  * Re-define the INTN_C(value) integer constant expression macros to match the
94  * integer types re-defined above.
95  */
96 
97 #undef __INT32_C
98 #undef __UINT32_C
99 #undef __INT64_C
100 #undef __UINT64_C
101 #define __INT32_C(c) c
102 #define __UINT32_C(c) c ## U
103 #define __INT64_C(c) c ## LL
104 #define __UINT64_C(c) c ## ULL
105 
106 #endif /* ZEPHYR_INCLUDE_TOOLCHAIN_STDINT_H_ */
107