Software APIs
stdint.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_BASE_FREESTANDING_STDINT_H_
6 #define OPENTITAN_SW_DEVICE_LIB_BASE_FREESTANDING_STDINT_H_
7 
8 /**
9  * @file
10  * @brief C library Integer types (Freestanding)
11  *
12  * This header implements the stdint.h standard header, as required by C11 S4p6.
13  * This header is specified in detail in S7.20 the same.
14  *
15  * Actual limit values below are defined as:
16  * - Unsigned MAXs are defined as the `0xff..ffU` all-ones representation.
17  * - Signed MINs are defined as `0x80..00` all-zeroes-but-the-last
18  * representation.
19  * - Unsigned MAXs are defined as `0x7f..ff` all-ones-but-the-last
20  * representation.
21  * These shorthands are correct for two's complement and the ones least likely
22  * to get a mistake in.
23  *
24  * It goes without saying that this file assumes the underlying representation
25  * is two's complement and little-endian.
26  *
27  * See https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md
28  * for the RISC-V specifics this file conforms to.
29  */
30 
31 typedef __UINT8_TYPE__ uint8_t;
32 typedef __UINT16_TYPE__ uint16_t;
33 typedef __UINT32_TYPE__ uint32_t;
34 typedef __UINT64_TYPE__ uint64_t;
35 typedef __INT8_TYPE__ int8_t;
36 typedef __INT16_TYPE__ int16_t;
37 typedef __INT32_TYPE__ int32_t;
38 typedef __INT64_TYPE__ int64_t;
39 
40 typedef __UINT_LEAST8_TYPE__ uint_least8_t;
41 typedef __UINT_LEAST16_TYPE__ uint_least16_t;
42 typedef __UINT_LEAST32_TYPE__ uint_least32_t;
43 typedef __UINT_LEAST64_TYPE__ uint_least64_t;
44 typedef __INT_LEAST8_TYPE__ int_least8_t;
45 typedef __INT_LEAST16_TYPE__ int_least16_t;
46 typedef __INT_LEAST32_TYPE__ int_least32_t;
47 typedef __INT_LEAST64_TYPE__ int_least64_t;
48 
49 typedef __UINT_FAST8_TYPE__ uint_fast8_t;
50 typedef __UINT_FAST16_TYPE__ uint_fast16_t;
51 typedef __UINT_FAST32_TYPE__ uint_fast32_t;
52 typedef __UINT_FAST64_TYPE__ uint_fast64_t;
53 typedef __INT_FAST8_TYPE__ int_fast8_t;
54 typedef __INT_FAST16_TYPE__ int_fast16_t;
55 typedef __INT_FAST32_TYPE__ int_fast32_t;
56 typedef __INT_FAST64_TYPE__ int_fast64_t;
57 
58 typedef __UINTPTR_TYPE__ uintptr_t;
59 typedef __INTPTR_TYPE__ intptr_t;
60 
61 typedef __UINTMAX_TYPE__ uintmax_t;
62 typedef __INTMAX_TYPE__ intmax_t;
63 
64 // NOTE: Below, we use GCC/Clang's built-in defines for limits. However, only
65 // MAX limits are provided and, as such, we define a signed MIN, given a MAX, as
66 // (-MIN - 1). (MAX + 1) will trigger signed overflow and, as such, Undefined
67 // Behavior.
68 
69 #define INT8_MIN (-__INT8_MAX__ - 1) /**< @hideinitializer */
70 #define INT8_MAX __INT8_MAX__ /**< @hideinitializer */
71 #define UINT8_MAX __UINT8_MAX__ /**< @hideinitializer */
72 #define INT16_MIN (-__INT16_MAX__ - 1) /**< @hideinitializer */
73 #define INT16_MAX __INT16_MAX__ /**< @hideinitializer */
74 #define UINT16_MAX __UINT16_MAX__ /**< @hideinitializer */
75 #define INT32_MIN (-__INT32_MAX__ - 1) /**< @hideinitializer */
76 #define INT32_MAX __INT32_MAX__ /**< @hideinitializer */
77 #define UINT32_MAX __UINT32_MAX__ /**< @hideinitializer */
78 #define INT64_MIN (-__INT64_MAX__ - 1) /**< @hideinitializer */
79 #define INT64_MAX __INT64_MAX__ /**< @hideinitializer */
80 #define UINT64_MAX __UINT64_MAX__ /**< @hideinitializer */
81 
82 #define INT_LEAST8_MIN (-__INT_LEAST8_MAX__ - 1) /**< @hideinitializer */
83 #define INT_LEAST8_MAX __INT_LEAST8_MAX__ /**< @hideinitializer */
84 #define UINT_LEAST8_MAX __UINT_LEAST8_MAX__ /**< @hideinitializer */
85 #define INT_LEAST16_MIN (-__INT_LEAST16_MAX__ - 1) /**< @hideinitializer */
86 #define INT_LEAST16_MAX __INT_LEAST16_MAX__ /**< @hideinitializer */
87 #define UINT_LEAST16_MAX __UINT_LEAST16_MAX__ /**< @hideinitializer */
88 #define INT_LEAST32_MIN (-__INT_LEAST32_MAX__ - 1) /**< @hideinitializer */
89 #define INT_LEAST32_MAX __INT_LEAST32_MAX__ /**< @hideinitializer */
90 #define UINT_LEAST32_MAX __UINT_LEAST32_MAX__ /**< @hideinitializer */
91 #define INT_LEAST64_MIN (-__INT_LEAST64_MAX__ - 1) /**< @hideinitializer */
92 #define INT_LEAST64_MAX __INT_LEAST64_MAX__ /**< @hideinitializer */
93 #define UINT_LEAST64_MAX __UINT_LEAST64_MAX__ /**< @hideinitializer */
94 
95 #define INT_FAST8_MIN (-__INT_FAST8_MAX__ - 1) /**< @hideinitializer */
96 #define INT_FAST8_MAX __INT_FAST8_MAX__ /**< @hideinitializer */
97 #define UINT_FAST8_MAX __UINT_FAST8_MAX__ /**< @hideinitializer */
98 #define INT_FAST16_MIN (-__INT_FAST16_MAX__ - 1) /**< @hideinitializer */
99 #define INT_FAST16_MAX __INT_FAST16_MAX__ /**< @hideinitializer */
100 #define UINT_FAST16_MAX __UINT_FAST16_MAX__ /**< @hideinitializer */
101 #define INT_FAST32_MIN (-__INT_FAST32_MAX__ - 1) /**< @hideinitializer */
102 #define INT_FAST32_MAX __INT_FAST32_MAX__ /**< @hideinitializer */
103 #define UINT_FAST32_MAX __UINT_FAST32_MAX__ /**< @hideinitializer */
104 #define INT_FAST64_MIN (-__INT_FAST64_MAX__ - 1) /**< @hideinitializer */
105 #define INT_FAST64_MAX __INT_FAST64_MAX__ /**< @hideinitializer */
106 #define UINT_FAST64_MAX __UINT_FAST64_MAX__ /**< @hideinitializer */
107 
108 #define INTPTR_MIN (-__INTPTR_MAX__ - 1) /**< @hideinitializer */
109 #define INTPTR_MAX __INTPTR_MAX__ /**< @hideinitializer */
110 #define UINTPTR_MAX __UINTPTR_MAX__ /**< @hideinitializer */
111 
112 #define INTMAX_MIN (-__INTMAX_MAX__ - 1) /**< @hideinitializer */
113 #define INTMAX_MAX __INTMAX_MAX__ /**< @hideinitializer */
114 #define UINTMAX_MAX __UINTMAX_MAX__ /**< @hideinitializer */
115 
116 #define PTRDIFF_MIN (-__PTRDIFF_MAX__ - 1) /**< @hideinitializer */
117 #define PTRDIFF_MAX __PTRDIFF_MAX__ /**< @hideinitializer */
118 
119 #define SIZE_MAX __SIZE_MAX__ /**< @hideinitializer */
120 
121 #define WCHAR_MIN (-__WCHAR_MAX__ - 1) /**< @hideinitializer */
122 #define WCHAR_MAX __WCHAR_MAX__ /**< @hideinitializer */
123 
124 // NOTE: While GCC and Clang both provide macros for implememting the _C macros,
125 // they are inconsistent on which is correct, so we implement them ourselves
126 // below.
127 
128 #define INT8_C(value) (value) /**< @hideinitializer */
129 #define INT16_C(value) (value) /**< @hideinitializer */
130 #define INT32_C(value) (value) /**< @hideinitializer */
131 #define INT64_C(value) (value##LL) /**< @hideinitializer */
132 #define INTMAX_C(value) (value##LL) /**< @hideinitializer */
133 
134 #define UINT8_C(value) (value##U) /**< @hideinitializer */
135 #define UINT16_C(value) (value##U) /**< @hideinitializer */
136 #define UINT32_C(value) (value##U) /**< @hideinitializer */
137 #define UINT64_C(value) (value##ULL) /**< @hideinitializer */
138 #define UINTMAX_C(value) (value##ULL) /**< @hideinitializer */
139 
140 #endif // OPENTITAN_SW_DEVICE_LIB_BASE_FREESTANDING_STDINT_H_