nasmint.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #ifndef NASM_NASMINT_H
  12. #define NASM_NASMINT_H
  13. #include <limits.h>
  14. /*** 64-bit type: __int64, long or long long ***/
  15. /* Some old versions of gcc <limits.h> omit LLONG_MAX */
  16. #ifndef LLONG_MAX
  17. # ifdef __LONG_LONG_MAX__
  18. # define LLONG_MAX __LONG_LONG_MAX__
  19. # else
  20. # define LLONG_MAX 0 /* Assume long long is unusable */
  21. # endif
  22. #endif
  23. #ifndef _I64_MAX
  24. # ifdef _MSC_VER
  25. # define _I64_MAX 9223372036854775807
  26. # else
  27. # define _I64_MAX 0
  28. # endif
  29. #endif
  30. #if _I64_MAX == 9223372036854775807
  31. /* Windows-based compiler: use __int64 */
  32. typedef signed __int64 int64_t;
  33. typedef unsigned __int64 uint64_t;
  34. #define _scn64 "I64"
  35. #define _pri64 "I64"
  36. #define INT64_C(x) x ## i64
  37. #define UINT64_C(x) x ## ui64
  38. #elif LONG_MAX == 9223372036854775807L
  39. /* long is 64 bits */
  40. typedef signed long int64_t;
  41. typedef unsigned long uint64_t;
  42. #define _scn64 "l"
  43. #define _pri64 "l"
  44. #define INT64_C(x) x ## L
  45. #define UINT64_C(x) x ## UL
  46. #elif LLONG_MAX == 9223372036854775807LL
  47. /* long long is 64 bits */
  48. typedef signed long long int64_t;
  49. typedef unsigned long long uint64_t;
  50. #define _scn64 "ll"
  51. #define _pri64 "ll"
  52. #define INT64_C(x) x ## LL
  53. #define UINT64_C(x) x ## ULL
  54. #else
  55. #error "Neither long nor long long is 64 bits in size"
  56. #endif
  57. /*** 32-bit type: int or long ***/
  58. #if INT_MAX == 2147483647
  59. /* int is 32 bits */
  60. typedef signed int int32_t;
  61. typedef unsigned int uint32_t;
  62. #define _scn32 ""
  63. #define _pri32 ""
  64. #define INT32_C(x) x
  65. #define UINT32_C(x) x ## U
  66. #elif LONG_MAX == 2147483647L
  67. /* long is 32 bits */
  68. typedef signed long int32_t;
  69. typedef unsigned long uint32_t;
  70. #define _scn32 "l"
  71. #define _pri32 "l"
  72. #define INT32_C(x) x ## L
  73. #define UINT32_C(x) x ## UL
  74. #else
  75. #error "Neither int nor long is 32 bits in size"
  76. #endif
  77. /*** 16-bit size: int or short ***/
  78. #if INT_MAX == 32767
  79. /* int is 16 bits */
  80. typedef signed int int16_t;
  81. typedef unsigned int uint16_t;
  82. #define _scn16 ""
  83. #define _pri16 ""
  84. #define INT16_C(x) x
  85. #define UINT16_C(x) x ## U
  86. #elif SHRT_MAX == 32767
  87. /* short is 16 bits */
  88. typedef signed short int16_t;
  89. typedef unsigned short uint16_t;
  90. #define _scn16 "h"
  91. #define _pri16 ""
  92. #define INT16_C(x) x
  93. #define UINT16_C(x) x ## U
  94. #else
  95. #error "Neither short nor int is 16 bits in size"
  96. #endif
  97. /*** 8-bit size: char ***/
  98. #if SCHAR_MAX == 127
  99. /* char is 8 bits */
  100. typedef signed char int8_t;
  101. typedef unsigned char uint8_t;
  102. #define _scn8 "hh"
  103. #define _pri8 ""
  104. #define INT8_C(x) x
  105. #define UINT8_C(x) x ## U
  106. #else
  107. #error "char is not 8 bits in size"
  108. #endif
  109. /* The rest of this is common to all models */
  110. #define PRId8 _pri8 "d"
  111. #define PRId16 _pri16 "d"
  112. #define PRId32 _pri32 "d"
  113. #define PRId64 _pri64 "d"
  114. #define PRIi8 _pri8 "i"
  115. #define PRIi16 _pri16 "i"
  116. #define PRIi32 _pri32 "i"
  117. #define PRIi64 _pri64 "i"
  118. #define PRIo8 _pri8 "o"
  119. #define PRIo16 _pri16 "o"
  120. #define PRIo32 _pri32 "o"
  121. #define PRIo64 _pri64 "o"
  122. #define PRIu8 _pri8 "u"
  123. #define PRIu16 _pri16 "u"
  124. #define PRIu32 _pri32 "u"
  125. #define PRIu64 _pri64 "u"
  126. #define PRIx8 _pri8 "x"
  127. #define PRIx16 _pri16 "x"
  128. #define PRIx32 _pri32 "x"
  129. #define PRIx64 _pri64 "x"
  130. #define PRIX8 _pri8 "X"
  131. #define PRIX16 _pri16 "X"
  132. #define PRIX32 _pri32 "X"
  133. #define PRIX64 _pri64 "X"
  134. #define SCNd8 _scn8 "d"
  135. #define SCNd16 _scn16 "d"
  136. #define SCNd32 _scn32 "d"
  137. #define SCNd64 _scn64 "d"
  138. #define SCNi8 _scn8 "i"
  139. #define SCNi16 _scn16 "i"
  140. #define SCNi32 _scn32 "i"
  141. #define SCNi64 _scn64 "i"
  142. #define SCNo8 _scn8 "o"
  143. #define SCNo16 _scn16 "o"
  144. #define SCNo32 _scn32 "o"
  145. #define SCNo64 _scn64 "o"
  146. #define SCNu8 _scn8 "u"
  147. #define SCNu16 _scn16 "u"
  148. #define SCNu32 _scn32 "u"
  149. #define SCNu64 _scn64 "u"
  150. #define SCNx8 _scn8 "x"
  151. #define SCNx16 _scn16 "x"
  152. #define SCNx32 _scn32 "x"
  153. #define SCNx64 _scn64 "x"
  154. #define INT8_MIN INT8_C(-128)
  155. #define INT8_MAX INT8_C(127)
  156. #define UINT8_MAX UINT8_C(255)
  157. #define INT16_MIN INT16_C(-32768)
  158. #define INT16_MAX INT16_C(32767)
  159. #define UINT16_MAX UINT16_C(65535)
  160. #define INT32_MIN INT32_C(-2147483648)
  161. #define INT32_MAX INT32_C(2147483647)
  162. #define UINT32_MAX UINT32_C(4294967295)
  163. #define INT64_MIN INT64_C(-9223372036854775808)
  164. #define INT64_MAX INT64_C(9223372036854775807)
  165. #define UINT64_MAX UINT64_C(18446744073709551615)
  166. #endif /* NASM_NASMINT_H */