hwint.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* HOST_WIDE_INT definitions for the GNU compiler.
  2. Copyright (C) 1998-2019 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. Provide definitions for macros which depend on HOST_BITS_PER_INT
  5. and HOST_BITS_PER_LONG. */
  6. #ifndef GCC_HWINT_H
  7. #define GCC_HWINT_H
  8. /* This describes the machine the compiler is hosted on. */
  9. #define HOST_BITS_PER_CHAR CHAR_BIT
  10. #define HOST_BITS_PER_SHORT (CHAR_BIT * SIZEOF_SHORT)
  11. #define HOST_BITS_PER_INT (CHAR_BIT * SIZEOF_INT)
  12. #define HOST_BITS_PER_LONG (CHAR_BIT * SIZEOF_LONG)
  13. #define HOST_BITS_PER_PTR (CHAR_BIT * SIZEOF_VOID_P)
  14. /* The string that should be inserted into a printf style format to
  15. indicate a "long" operand. */
  16. #ifndef HOST_LONG_FORMAT
  17. #define HOST_LONG_FORMAT "l"
  18. #endif
  19. /* The string that should be inserted into a printf style format to
  20. indicate a "long long" operand. */
  21. #ifndef HOST_LONG_LONG_FORMAT
  22. #define HOST_LONG_LONG_FORMAT "ll"
  23. #endif
  24. /* If HAVE_LONG_LONG and SIZEOF_LONG_LONG aren't defined, but
  25. GCC_VERSION >= 3000, assume this is the second or later stage of a
  26. bootstrap, we do have long long, and it's 64 bits. (This is
  27. required by C99; we do have some ports that violate that assumption
  28. but they're all cross-compile-only.) Just in case, force a
  29. constraint violation if that assumption is incorrect. */
  30. #if !defined HAVE_LONG_LONG
  31. # if GCC_VERSION >= 3000
  32. # define HAVE_LONG_LONG 1
  33. # define SIZEOF_LONG_LONG 8
  34. extern char sizeof_long_long_must_be_8[sizeof (long long) == 8 ? 1 : -1];
  35. # endif
  36. #endif
  37. #ifdef HAVE_LONG_LONG
  38. # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
  39. #endif
  40. /* Set HOST_WIDE_INT, this should be always 64 bits.
  41. The underlying type is matched to that of int64_t and assumed
  42. to be either long or long long. */
  43. #define HOST_BITS_PER_WIDE_INT 64
  44. #if INT64_T_IS_LONG
  45. # define HOST_WIDE_INT long
  46. # define HOST_WIDE_INT_C(X) X ## L
  47. #else
  48. # if HOST_BITS_PER_LONGLONG == 64
  49. # define HOST_WIDE_INT long long
  50. # define HOST_WIDE_INT_C(X) X ## LL
  51. # else
  52. #error "Unable to find a suitable type for HOST_WIDE_INT"
  53. # endif
  54. #endif
  55. #define HOST_WIDE_INT_UC(X) HOST_WIDE_INT_C (X ## U)
  56. #define HOST_WIDE_INT_0 HOST_WIDE_INT_C (0)
  57. #define HOST_WIDE_INT_0U HOST_WIDE_INT_UC (0)
  58. #define HOST_WIDE_INT_1 HOST_WIDE_INT_C (1)
  59. #define HOST_WIDE_INT_1U HOST_WIDE_INT_UC (1)
  60. #define HOST_WIDE_INT_M1 HOST_WIDE_INT_C (-1)
  61. #define HOST_WIDE_INT_M1U HOST_WIDE_INT_UC (-1)
  62. /* This is a magic identifier which allows GCC to figure out the type
  63. of HOST_WIDE_INT for %wd specifier checks. You must issue this
  64. typedef before using the __asm_fprintf__ format attribute. */
  65. typedef HOST_WIDE_INT __gcc_host_wide_int__;
  66. /* Provide C99 <inttypes.h> style format definitions for 64bits. */
  67. #ifndef HAVE_INTTYPES_H
  68. #if INT64_T_IS_LONG
  69. # define GCC_PRI64 HOST_LONG_FORMAT
  70. #else
  71. # define GCC_PRI64 HOST_LONG_LONG_FORMAT
  72. #endif
  73. #undef PRId64
  74. #define PRId64 GCC_PRI64 "d"
  75. #undef PRIi64
  76. #define PRIi64 GCC_PRI64 "i"
  77. #undef PRIo64
  78. #define PRIo64 GCC_PRI64 "o"
  79. #undef PRIu64
  80. #define PRIu64 GCC_PRI64 "u"
  81. #undef PRIx64
  82. #define PRIx64 GCC_PRI64 "x"
  83. #undef PRIX64
  84. #define PRIX64 GCC_PRI64 "X"
  85. #endif
  86. /* Various printf format strings for HOST_WIDE_INT. */
  87. #if INT64_T_IS_LONG
  88. # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
  89. # define HOST_WIDE_INT_PRINT_C "L"
  90. #else
  91. # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
  92. # define HOST_WIDE_INT_PRINT_C "LL"
  93. #endif
  94. #define HOST_WIDE_INT_PRINT_DEC "%" PRId64
  95. #define HOST_WIDE_INT_PRINT_DEC_C "%" PRId64 HOST_WIDE_INT_PRINT_C
  96. #define HOST_WIDE_INT_PRINT_UNSIGNED "%" PRIu64
  97. #define HOST_WIDE_INT_PRINT_HEX "%#" PRIx64
  98. #define HOST_WIDE_INT_PRINT_HEX_PURE "%" PRIx64
  99. #define HOST_WIDE_INT_PRINT_DOUBLE_HEX "0x%" PRIx64 "%016" PRIx64
  100. #define HOST_WIDE_INT_PRINT_PADDED_HEX "%016" PRIx64
  101. /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
  102. efficiently in hardware. (That is, the widest integer type that fits
  103. in a hardware register.) Normally this is "long" but on some hosts it
  104. should be "long long" or "__int64". This is no convenient way to
  105. autodetect this, so such systems must set a flag in config.host; see there
  106. for details. */
  107. #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
  108. # ifdef HAVE_LONG_LONG
  109. # define HOST_WIDEST_FAST_INT long long
  110. # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
  111. # else
  112. # error "Your host said it wanted to use long long but that does not exist"
  113. # endif
  114. #else
  115. # define HOST_WIDEST_FAST_INT long
  116. # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
  117. #endif
  118. /* Inline functions operating on HOST_WIDE_INT. */
  119. /* Return X with all but the lowest bit masked off. */
  120. static inline unsigned HOST_WIDE_INT
  121. least_bit_hwi (unsigned HOST_WIDE_INT x)
  122. {
  123. return (x & -x);
  124. }
  125. /* True if X is zero or a power of two. */
  126. static inline bool
  127. pow2_or_zerop (unsigned HOST_WIDE_INT x)
  128. {
  129. return least_bit_hwi (x) == x;
  130. }
  131. /* True if X is a power of two. */
  132. static inline bool
  133. pow2p_hwi (unsigned HOST_WIDE_INT x)
  134. {
  135. return x && pow2_or_zerop (x);
  136. }
  137. #if GCC_VERSION < 3004
  138. extern int clz_hwi (unsigned HOST_WIDE_INT x);
  139. extern int ctz_hwi (unsigned HOST_WIDE_INT x);
  140. extern int ffs_hwi (unsigned HOST_WIDE_INT x);
  141. /* Return the number of set bits in X. */
  142. extern int popcount_hwi (unsigned HOST_WIDE_INT x);
  143. /* Return log2, or -1 if not exact. */
  144. extern int exact_log2 (unsigned HOST_WIDE_INT);
  145. /* Return floor of log2, with -1 for zero. */
  146. extern int floor_log2 (unsigned HOST_WIDE_INT);
  147. /* Return the smallest n such that 2**n >= X. */
  148. extern int ceil_log2 (unsigned HOST_WIDE_INT);
  149. #else /* GCC_VERSION >= 3004 */
  150. /* For convenience, define 0 -> word_size. */
  151. static inline int
  152. clz_hwi (unsigned HOST_WIDE_INT x)
  153. {
  154. if (x == 0)
  155. return HOST_BITS_PER_WIDE_INT;
  156. # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
  157. return __builtin_clzl (x);
  158. # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
  159. return __builtin_clzll (x);
  160. # else
  161. return __builtin_clz (x);
  162. # endif
  163. }
  164. static inline int
  165. ctz_hwi (unsigned HOST_WIDE_INT x)
  166. {
  167. if (x == 0)
  168. return HOST_BITS_PER_WIDE_INT;
  169. # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
  170. return __builtin_ctzl (x);
  171. # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
  172. return __builtin_ctzll (x);
  173. # else
  174. return __builtin_ctz (x);
  175. # endif
  176. }
  177. static inline int
  178. ffs_hwi (unsigned HOST_WIDE_INT x)
  179. {
  180. # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
  181. return __builtin_ffsl (x);
  182. # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
  183. return __builtin_ffsll (x);
  184. # else
  185. return __builtin_ffs (x);
  186. # endif
  187. }
  188. static inline int
  189. popcount_hwi (unsigned HOST_WIDE_INT x)
  190. {
  191. # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
  192. return __builtin_popcountl (x);
  193. # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
  194. return __builtin_popcountll (x);
  195. # else
  196. return __builtin_popcount (x);
  197. # endif
  198. }
  199. static inline int
  200. floor_log2 (unsigned HOST_WIDE_INT x)
  201. {
  202. return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
  203. }
  204. static inline int
  205. ceil_log2 (unsigned HOST_WIDE_INT x)
  206. {
  207. return x == 0 ? 0 : floor_log2 (x - 1) + 1;
  208. }
  209. static inline int
  210. exact_log2 (unsigned HOST_WIDE_INT x)
  211. {
  212. return pow2p_hwi (x) ? ctz_hwi (x) : -1;
  213. }
  214. #endif /* GCC_VERSION >= 3004 */
  215. #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
  216. (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1))
  217. #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
  218. extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
  219. extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
  220. extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
  221. extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
  222. extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
  223. extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
  224. /* Like ctz_hwi, except 0 when x == 0. */
  225. static inline int
  226. ctz_or_zero (unsigned HOST_WIDE_INT x)
  227. {
  228. return ffs_hwi (x) - 1;
  229. }
  230. /* Sign extend SRC starting from PREC. */
  231. static inline HOST_WIDE_INT
  232. sext_hwi (HOST_WIDE_INT src, unsigned int prec)
  233. {
  234. if (prec == HOST_BITS_PER_WIDE_INT)
  235. return src;
  236. else
  237. #if defined (__GNUC__)
  238. {
  239. /* Take the faster path if the implementation-defined bits it's relying
  240. on are implemented the way we expect them to be. Namely, conversion
  241. from unsigned to signed preserves bit pattern, and right shift of
  242. a signed value propagates the sign bit.
  243. We have to convert from signed to unsigned and back, because when left
  244. shifting signed values, any overflow is undefined behavior. */
  245. gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
  246. int shift = HOST_BITS_PER_WIDE_INT - prec;
  247. return ((HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) src << shift)) >> shift;
  248. }
  249. #else
  250. {
  251. /* Fall back to the slower, well defined path otherwise. */
  252. gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
  253. HOST_WIDE_INT sign_mask = HOST_WIDE_INT_1 << (prec - 1);
  254. HOST_WIDE_INT value_mask = (HOST_WIDE_INT_1U << prec) - HOST_WIDE_INT_1U;
  255. return (((src & value_mask) ^ sign_mask) - sign_mask);
  256. }
  257. #endif
  258. }
  259. /* Zero extend SRC starting from PREC. */
  260. static inline unsigned HOST_WIDE_INT
  261. zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
  262. {
  263. if (prec == HOST_BITS_PER_WIDE_INT)
  264. return src;
  265. else
  266. {
  267. gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
  268. return src & ((HOST_WIDE_INT_1U << prec) - 1);
  269. }
  270. }
  271. /* Compute the absolute value of X. */
  272. inline HOST_WIDE_INT
  273. abs_hwi (HOST_WIDE_INT x)
  274. {
  275. gcc_checking_assert (x != HOST_WIDE_INT_MIN);
  276. return x >= 0 ? x : -x;
  277. }
  278. /* Compute the absolute value of X as an unsigned type. */
  279. inline unsigned HOST_WIDE_INT
  280. absu_hwi (HOST_WIDE_INT x)
  281. {
  282. return x >= 0 ? (unsigned HOST_WIDE_INT)x : -(unsigned HOST_WIDE_INT)x;
  283. }
  284. #endif /* ! GCC_HWINT_H */