1 /* 2 * nasmint.h 3 * 4 * Small ersatz subset of <inttypes.h>, deriving the types from 5 * <limits.h>. 6 * 7 * Important: the preprocessor may truncate numbers too large for it. 8 * Therefore, test the signed types only ... truncation won't generate 9 * a 01111111... bit pattern. 10 */ 11 12 #ifndef NASM_NASMINT_H 13 #define NASM_NASMINT_H 14 15 #include <limits.h> 16 17 /*** 64-bit type: __int64, long or long long ***/ 18 19 /* Some old versions of gcc <limits.h> omit LLONG_MAX */ 20 #ifndef LLONG_MAX 21 # ifdef __LONG_LONG_MAX__ 22 # define LLONG_MAX __LONG_LONG_MAX__ 23 # else 24 # define LLONG_MAX 0 /* Assume long long is unusable */ 25 # endif 26 #endif 27 28 #ifndef _I64_MAX 29 # ifdef _MSC_VER 30 # define _I64_MAX 9223372036854775807 31 # else 32 # define _I64_MAX 0 33 # endif 34 #endif 35 36 #if _I64_MAX == 9223372036854775807 37 38 /* Windows-based compiler: use __int64 */ 39 typedef signed __int64 int64_t; 40 typedef unsigned __int64 uint64_t; 41 #define _scn64 "I64" 42 #define _pri64 "I64" 43 #define INT64_C(x) x ## i64 44 #define UINT64_C(x) x ## ui64 45 46 #elif LONG_MAX == 9223372036854775807L 47 48 /* long is 64 bits */ 49 typedef signed long int64_t; 50 typedef unsigned long uint64_t; 51 #define _scn64 "l" 52 #define _pri64 "l" 53 #define INT64_C(x) x ## L 54 #define UINT64_C(x) x ## UL 55 56 #elif LLONG_MAX == 9223372036854775807LL 57 58 /* long long is 64 bits */ 59 typedef signed long long int64_t; 60 typedef unsigned long long uint64_t; 61 #define _scn64 "ll" 62 #define _pri64 "ll" 63 #define INT64_C(x) x ## LL 64 #define UINT64_C(x) x ## ULL 65 66 #else 67 68 #error "Neither long nor long long is 64 bits in size" 69 70 #endif 71 72 /*** 32-bit type: int or long ***/ 73 74 #if INT_MAX == 2147483647 75 76 /* int is 32 bits */ 77 typedef signed int int32_t; 78 typedef unsigned int uint32_t; 79 #define _scn32 "" 80 #define _pri32 "" 81 #define INT32_C(x) x 82 #define UINT32_C(x) x ## U 83 84 #elif LONG_MAX == 2147483647L 85 86 /* long is 32 bits */ 87 typedef signed long int32_t; 88 typedef unsigned long uint32_t; 89 #define _scn32 "l" 90 #define _pri32 "l" 91 #define INT32_C(x) x ## L 92 #define UINT32_C(x) x ## UL 93 94 #else 95 96 #error "Neither int nor long is 32 bits in size" 97 98 #endif 99 100 /*** 16-bit size: int or short ***/ 101 102 #if INT_MAX == 32767 103 104 /* int is 16 bits */ 105 typedef signed int int16_t; 106 typedef unsigned int uint16_t; 107 #define _scn16 "" 108 #define _pri16 "" 109 #define INT16_C(x) x 110 #define UINT16_C(x) x ## U 111 112 #elif SHRT_MAX == 32767 113 114 /* short is 16 bits */ 115 typedef signed short int16_t; 116 typedef unsigned short uint16_t; 117 #define _scn16 "h" 118 #define _pri16 "" 119 #define INT16_C(x) x 120 #define UINT16_C(x) x ## U 121 122 #else 123 124 #error "Neither short nor int is 16 bits in size" 125 126 #endif 127 128 /*** 8-bit size: char ***/ 129 130 #if SCHAR_MAX == 127 131 132 /* char is 8 bits */ 133 typedef signed char int8_t; 134 typedef unsigned char uint8_t; 135 #define _scn8 "hh" 136 #define _pri8 "" 137 #define INT8_C(x) x 138 #define UINT8_C(x) x ## U 139 140 #else 141 142 #error "char is not 8 bits in size" 143 144 #endif 145 146 /* The rest of this is common to all models */ 147 148 #define PRId8 _pri8 "d" 149 #define PRId16 _pri16 "d" 150 #define PRId32 _pri32 "d" 151 #define PRId64 _pri64 "d" 152 153 #define PRIi8 _pri8 "i" 154 #define PRIi16 _pri16 "i" 155 #define PRIi32 _pri32 "i" 156 #define PRIi64 _pri64 "i" 157 158 #define PRIo8 _pri8 "o" 159 #define PRIo16 _pri16 "o" 160 #define PRIo32 _pri32 "o" 161 #define PRIo64 _pri64 "o" 162 163 #define PRIu8 _pri8 "u" 164 #define PRIu16 _pri16 "u" 165 #define PRIu32 _pri32 "u" 166 #define PRIu64 _pri64 "u" 167 168 #define PRIx8 _pri8 "x" 169 #define PRIx16 _pri16 "x" 170 #define PRIx32 _pri32 "x" 171 #define PRIx64 _pri64 "x" 172 173 #define PRIX8 _pri8 "X" 174 #define PRIX16 _pri16 "X" 175 #define PRIX32 _pri32 "X" 176 #define PRIX64 _pri64 "X" 177 178 #define SCNd8 _scn8 "d" 179 #define SCNd16 _scn16 "d" 180 #define SCNd32 _scn32 "d" 181 #define SCNd64 _scn64 "d" 182 183 #define SCNi8 _scn8 "i" 184 #define SCNi16 _scn16 "i" 185 #define SCNi32 _scn32 "i" 186 #define SCNi64 _scn64 "i" 187 188 #define SCNo8 _scn8 "o" 189 #define SCNo16 _scn16 "o" 190 #define SCNo32 _scn32 "o" 191 #define SCNo64 _scn64 "o" 192 193 #define SCNu8 _scn8 "u" 194 #define SCNu16 _scn16 "u" 195 #define SCNu32 _scn32 "u" 196 #define SCNu64 _scn64 "u" 197 198 #define SCNx8 _scn8 "x" 199 #define SCNx16 _scn16 "x" 200 #define SCNx32 _scn32 "x" 201 #define SCNx64 _scn64 "x" 202 203 #define INT8_MIN INT8_C(-128) 204 #define INT8_MAX INT8_C(127) 205 #define UINT8_MAX UINT8_C(255) 206 207 #define INT16_MIN INT16_C(-32768) 208 #define INT16_MAX INT16_C(32767) 209 #define UINT16_MAX UINT16_C(65535) 210 211 #define INT32_MIN INT32_C(-2147483648) 212 #define INT32_MAX INT32_C(2147483647) 213 #define UINT32_MAX UINT32_C(4294967295) 214 215 #define INT64_MIN INT64_C(-9223372036854775808) 216 #define INT64_MAX INT64_C(9223372036854775807) 217 #define UINT64_MAX UINT64_C(18446744073709551615) 218 219 #endif /* NASM_NASMINT_H */ 220