1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #include <limits.h>
7 
8 /*
9  * This file provides what C99 standard requires in
10  * 7.18 interger types <stdint.h>
11  */
12 
13 #ifndef STDINT_H
14 #define STDINT_H
15 #define _STDINT_H
16 
17 /*
18  * If compiler supplies neither __ILP32__ or __LP64__, try to figure it out
19  * here.
20  */
21 #if !defined(__ILP32__) && !defined(__LP64__)
22 #if defined(__SIZEOF_INT__) && defined(__SIZEOF_POINTER__) && \
23 	defined(__SIZEOF_LONG__)
24 #if __SIZEOF_INT__ == 4 && __SIZEOF_POINTER__ == 4 && __SIZEOF_LONG__ == 4
25 #define __ILP32__ 1
26 #endif
27 #if __SIZEOF_INT__ == 4 && __SIZEOF_POINTER__ == 8 && __SIZEOF_LONG__ == 8
28 #define __LP64__ 1
29 #endif
30 #endif
31 #endif /* !defined(__ILP32__) && !defined(__LP64__) */
32 
33 #if !defined(__ILP32__) && !defined(__LP64__)
34 #error Neither __ILP32__ nor __LP64__ is defined
35 #endif
36 
37 #ifndef __ASSEMBLER__
38 
39 /* 7.18.1.1 Exact-width interger types */
40 #ifndef __int8_t_defined
41 # define __int8_t_defined
42 typedef signed char             int8_t;
43 typedef short int               int16_t;
44 typedef int                     int32_t;
45 #ifdef __ILP32__
46 __extension__
47 typedef long long int           int64_t;
48 #endif /*__ILP32__*/
49 #ifdef __LP64__
50 typedef long int		int64_t;
51 #endif /*__LP64__*/
52 #endif
53 
54 /* Unsigned.  */
55 typedef unsigned char           uint8_t;
56 typedef unsigned short int      uint16_t;
57 #ifndef __uint32_t_defined
58 typedef unsigned int            uint32_t;
59 # define __uint32_t_defined
60 #endif
61 #ifdef __ILP32__
62 __extension__
63 typedef unsigned long long int  uint64_t;
64 #endif /*__ILP32__*/
65 #ifdef __LP64__
66 typedef unsigned long int	uint64_t;
67 #endif /*__LP64__*/
68 
69 /* 7.18.1.2 Minimum-width integer types */
70 typedef int8_t int_least8_t;
71 typedef int16_t int_least16_t;
72 typedef int32_t int_least32_t;
73 typedef int64_t int_least64_t;
74 typedef uint8_t uint_least8_t;
75 typedef uint16_t uint_least16_t;
76 typedef uint32_t uint_least32_t;
77 typedef uint64_t uint_least64_t;
78 
79 /* 7.18.1.3 Fastest minimum-width integer types */
80 typedef int8_t int_fast8_t;
81 typedef int16_t int_fast16_t;
82 typedef int32_t int_fast32_t;
83 typedef int64_t int_fast64_t;
84 typedef uint8_t uint_fast8_t;
85 typedef uint16_t uint_fast16_t;
86 typedef uint32_t uint_fast32_t;
87 typedef uint64_t uint_fast64_t;
88 
89 /* 7.18.1.4 Integer types capable of holding object pointers */
90 typedef long intptr_t;
91 typedef unsigned long uintptr_t;
92 
93 typedef int64_t intmax_t;
94 typedef uint64_t uintmax_t;
95 
96 #endif /*__ASSEMBLER__*/
97 
98 /*
99  * 7.18.2 Limits of specified-width integer types
100  */
101 
102 /* 7.18.2.1 Limits of exact-width interger types */
103 
104 #define INT8_MIN    (-0x7f-1)
105 #define INT16_MIN   (-0x7fff-1)
106 #define INT32_MIN   (-0x7fffffff-1)
107 #define INT64_MIN   (-0x7fffffffffffffffL-1)
108 
109 #define INT8_MAX    0x7f
110 #define INT16_MAX   0x7fff
111 #define INT32_MAX   0x7fffffff
112 #define INT64_MAX   0x7fffffffffffffffL
113 
114 #define UINT8_MAX    0xff
115 #define UINT16_MAX   0xffff
116 #define UINT32_MAX   0xffffffffU
117 #define UINT64_MAX   0xffffffffffffffffUL
118 
119 /* 7.18.2.2 Limits of minimum-width integer types */
120 
121 #define INT_LEAST8_MIN		INT8_MIN
122 #define INT_LEAST16_MIN		INT16_MIN
123 #define INT_LEAST32_MIN		INT32_MIN
124 #define INT_LEAST64_MIN		INT64_MIN
125 
126 #define INT_LEAST8_MAX		INT8_MAX
127 #define INT_LEAST16_MAX		INT16_MAX
128 #define INT_LEAST32_MAX		INT32_MAX
129 #define INT_LEAST64_MAX		INT64_MAX
130 
131 #define UINT_LEAST8_MAX		UINT8_MAX
132 #define UINT_LEAST16_MAX	UINT16_MAX
133 #define UINT_LEAST32_MAX	UINT32_MAX
134 #define UINT_LEAST64_MAX	UINT64_MAX
135 
136 /* 7.18.2.3 Limits of fastest minimum-width integer types */
137 
138 #define INT_FAST8_MIN		INT8_MIN
139 #define INT_FAST16_MIN		INT16_MIN
140 #define INT_FAST32_MIN		INT32_MIN
141 #define INT_FAST64_MIN		INT64_MIN
142 
143 #define INT_FAST8_MAX		INT8_MAX
144 #define INT_FAST16_MAX		INT16_MAX
145 #define INT_FAST32_MAX		INT32_MAX
146 #define INT_FAST64_MAX		INT64_MAX
147 
148 #define UINT_FAST8_MAX		UINT8_MAX
149 #define UINT_FAST16_MAX		UINT16_MAX
150 #define UINT_FAST32_MAX		UINT32_MAX
151 #define UINT_FAST64_MAX		UINT64_MAX
152 
153 /* 7.18.2.4 Limits of integer types capable of holding object pointers */
154 
155 #define INTPTR_MIN  LONG_MIN
156 #define INTPTR_MAX  LONG_MAX
157 #define UINTPTR_MAX ULONG_MAX
158 
159 /* 7.18.2.5  Limits of greatest-width integer types */
160 #define INTMAX_MAX  INT64_MAX
161 #define INTMAX_MIN  INT64_MIN
162 #define UINTMAX_MAX UINT64_MAX
163 
164 /* 7.18.3  Limits of other integer types */
165 #define SIZE_MAX	ULONG_MAX
166 
167 /*
168  * 7.18.4 Macros for integer constants
169  */
170 
171 #ifdef __ASSEMBLER__
172 #define U(v)		v
173 #define UL(v)		v
174 #define ULL(v)		v
175 #define L(v)		v
176 #define LL(v)		v
177 #else
178 #define U(v)		v ## U
179 #define UL(v)		v ## UL
180 #define ULL(v)		v ## ULL
181 #define L(v)		v ## L
182 #define LL(v)		v ## LL
183 #endif
184 
185 /* 7.18.4.1 Macros for minimum-width integer constants */
186 
187 #define INT8_C(v)	v
188 #define UINT8_C(v)	v
189 #define INT16_C(v)	v
190 #define UINT16_C(v)	v
191 #define INT32_C(v)	v
192 #define UINT32_C(v)	U(v)
193 #ifdef __ILP32__
194 #define INT64_C(v)	LL(v)
195 #define UINT64_C(v)	ULL(v)
196 #endif
197 #ifdef __LP64__
198 #define INT64_C(v)	L(v)
199 #define UINT64_C(v)	UL(v)
200 #endif
201 
202 #define UINTPTR_C(v)	UL(v)
203 
204 /* 7.18.4.2 Macros for greatest-width integer constants */
205 
206 #define INTMAX_C(v)	INT64_C(v)
207 #define UINTMAX_C(v)	UINT64_C(v)
208 
209 #endif /* STDINT_H */
210