hwint.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* HOST_WIDE_INT definitions for the GNU compiler.
  2. Copyright (C) 1998-2015 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. /* The string that should be inserted into a printf style format to
  14. indicate a "long" operand. */
  15. #ifndef HOST_LONG_FORMAT
  16. #define HOST_LONG_FORMAT "l"
  17. #endif
  18. /* The string that should be inserted into a printf style format to
  19. indicate a "long long" operand. */
  20. #ifndef HOST_LONG_LONG_FORMAT
  21. #define HOST_LONG_LONG_FORMAT "ll"
  22. #endif
  23. /* If HAVE_LONG_LONG and SIZEOF_LONG_LONG aren't defined, but
  24. GCC_VERSION >= 3000, assume this is the second or later stage of a
  25. bootstrap, we do have long long, and it's 64 bits. (This is
  26. required by C99; we do have some ports that violate that assumption
  27. but they're all cross-compile-only.) Just in case, force a
  28. constraint violation if that assumption is incorrect. */
  29. #if !defined HAVE_LONG_LONG
  30. # if GCC_VERSION >= 3000
  31. # define HAVE_LONG_LONG 1
  32. # define SIZEOF_LONG_LONG 8
  33. extern char sizeof_long_long_must_be_8[sizeof (long long) == 8 ? 1 : -1];
  34. # endif
  35. #endif
  36. #ifdef HAVE_LONG_LONG
  37. # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
  38. #endif
  39. /* Set HOST_WIDE_INT, this should be always 64 bits.
  40. The underlying type is matched to that of int64_t and assumed
  41. to be either long or long long. */
  42. #define HOST_BITS_PER_WIDE_INT 64
  43. #if INT64_T_IS_LONG
  44. # define HOST_WIDE_INT long
  45. # define HOST_WIDE_INT_C(X) X ## L
  46. #else
  47. # if HOST_BITS_PER_LONGLONG == 64
  48. # define HOST_WIDE_INT long long
  49. # define HOST_WIDE_INT_C(X) X ## LL
  50. # else
  51. #error "Unable to find a suitable type for HOST_WIDE_INT"
  52. # endif
  53. #endif
  54. #define HOST_WIDE_INT_UC(X) HOST_WIDE_INT_C (X ## U)
  55. #define HOST_WIDE_INT_1 HOST_WIDE_INT_C (1)
  56. #define HOST_WIDE_INT_1U HOST_WIDE_INT_UC (1)
  57. #define HOST_WIDE_INT_M1 HOST_WIDE_INT_C (-1)
  58. #define HOST_WIDE_INT_M1U HOST_WIDE_INT_UC (-1)
  59. /* This is a magic identifier which allows GCC to figure out the type
  60. of HOST_WIDE_INT for %wd specifier checks. You must issue this
  61. typedef before using the __asm_fprintf__ format attribute. */
  62. typedef HOST_WIDE_INT __gcc_host_wide_int__;
  63. /* Provide C99 <inttypes.h> style format definitions for 64bits. */
  64. #ifndef HAVE_INTTYPES_H
  65. #if INT64_T_IS_LONG
  66. # define GCC_PRI64 HOST_LONG_FORMAT
  67. #else
  68. # define GCC_PRI64 HOST_LONG_LONG_FORMAT
  69. #endif
  70. #undef PRId64
  71. #define PRId64 GCC_PRI64 "d"
  72. #undef PRIi64
  73. #define PRIi64 GCC_PRI64 "i"
  74. #undef PRIo64
  75. #define PRIo64 GCC_PRI64 "o"
  76. #undef PRIu64
  77. #define PRIu64 GCC_PRI64 "u"
  78. #undef PRIx64
  79. #define PRIx64 GCC_PRI64 "x"
  80. #undef PRIX64
  81. #define PRIX64 GCC_PRI64 "X"
  82. #endif
  83. /* Various printf format strings for HOST_WIDE_INT. */
  84. #if INT64_T_IS_LONG
  85. # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
  86. # define HOST_WIDE_INT_PRINT_C "L"
  87. #else
  88. # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
  89. # define HOST_WIDE_INT_PRINT_C "LL"
  90. #endif
  91. #define HOST_WIDE_INT_PRINT_DEC "%" PRId64
  92. #define HOST_WIDE_INT_PRINT_DEC_C "%" PRId64 HOST_WIDE_INT_PRINT_C
  93. #define HOST_WIDE_INT_PRINT_UNSIGNED "%" PRIu64
  94. #define HOST_WIDE_INT_PRINT_HEX "%#" PRIx64
  95. #define HOST_WIDE_INT_PRINT_HEX_PURE "%" PRIx64
  96. #define HOST_WIDE_INT_PRINT_DOUBLE_HEX "0x%" PRIx64 "%016" PRIx64
  97. #define HOST_WIDE_INT_PRINT_PADDED_HEX "%016" PRIx64
  98. /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
  99. efficiently in hardware. (That is, the widest integer type that fits
  100. in a hardware register.) Normally this is "long" but on some hosts it
  101. should be "long long" or "__int64". This is no convenient way to
  102. autodetect this, so such systems must set a flag in config.host; see there
  103. for details. */
  104. #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
  105. # ifdef HAVE_LONG_LONG
  106. # define HOST_WIDEST_FAST_INT long long
  107. # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
  108. # else
  109. # error "Your host said it wanted to use long long but that does not exist"
  110. # endif
  111. #else
  112. # define HOST_WIDEST_FAST_INT long
  113. # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
  114. #endif
  115. /* Inline functions operating on HOST_WIDE_INT. */
  116. #if GCC_VERSION < 3004
  117. extern int clz_hwi (unsigned HOST_WIDE_INT x);
  118. extern int ctz_hwi (unsigned HOST_WIDE_INT x);
  119. extern int ffs_hwi (unsigned HOST_WIDE_INT x);
  120. /* Return the number of set bits in X. */
  121. extern int popcount_hwi (unsigned HOST_WIDE_INT x);
  122. /* Return log2, or -1 if not exact. */
  123. extern int exact_log2 (unsigned HOST_WIDE_INT);
  124. /* Return floor of log2, with -1 for zero. */
  125. extern int floor_log2 (unsigned HOST_WIDE_INT);
  126. /* Return the smallest n such that 2**n >= X. */
  127. extern int ceil_log2 (unsigned HOST_WIDE_INT);
  128. #else /* GCC_VERSION >= 3004 */
  129. /* For convenience, define 0 -> word_size. */
  130. static inline int
  131. clz_hwi (unsigned HOST_WIDE_INT x)
  132. {
  133. if (x == 0)
  134. return HOST_BITS_PER_WIDE_INT;
  135. # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
  136. return __builtin_clzl (x);
  137. # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
  138. return __builtin_clzll (x);
  139. # else
  140. return __builtin_clz (x);
  141. # endif
  142. }
  143. static inline int
  144. ctz_hwi (unsigned HOST_WIDE_INT x)
  145. {
  146. if (x == 0)
  147. return HOST_BITS_PER_WIDE_INT;
  148. # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
  149. return __builtin_ctzl (x);
  150. # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
  151. return __builtin_ctzll (x);
  152. # else
  153. return __builtin_ctz (x);
  154. # endif
  155. }
  156. static inline int
  157. ffs_hwi (unsigned HOST_WIDE_INT x)
  158. {
  159. # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
  160. return __builtin_ffsl (x);
  161. # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
  162. return __builtin_ffsll (x);
  163. # else
  164. return __builtin_ffs (x);
  165. # endif
  166. }
  167. static inline int
  168. popcount_hwi (unsigned HOST_WIDE_INT x)
  169. {
  170. # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
  171. return __builtin_popcountl (x);
  172. # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
  173. return __builtin_popcountll (x);
  174. # else
  175. return __builtin_popcount (x);
  176. # endif
  177. }
  178. static inline int
  179. floor_log2 (unsigned HOST_WIDE_INT x)
  180. {
  181. return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
  182. }
  183. static inline int
  184. ceil_log2 (unsigned HOST_WIDE_INT x)
  185. {
  186. return floor_log2 (x - 1) + 1;
  187. }
  188. static inline int
  189. exact_log2 (unsigned HOST_WIDE_INT x)
  190. {
  191. return x == (x & -x) && x ? ctz_hwi (x) : -1;
  192. }
  193. #endif /* GCC_VERSION >= 3004 */
  194. #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
  195. ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
  196. #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
  197. extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
  198. extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
  199. extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
  200. extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
  201. extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
  202. extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
  203. /* Sign extend SRC starting from PREC. */
  204. static inline HOST_WIDE_INT
  205. sext_hwi (HOST_WIDE_INT src, unsigned int prec)
  206. {
  207. if (prec == HOST_BITS_PER_WIDE_INT)
  208. return src;
  209. else
  210. {
  211. gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
  212. int shift = HOST_BITS_PER_WIDE_INT - prec;
  213. return (src << shift) >> shift;
  214. }
  215. }
  216. /* Zero extend SRC starting from PREC. */
  217. static inline unsigned HOST_WIDE_INT
  218. zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
  219. {
  220. if (prec == HOST_BITS_PER_WIDE_INT)
  221. return src;
  222. else
  223. {
  224. gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
  225. return src & (((unsigned HOST_WIDE_INT) 1 << prec) - 1);
  226. }
  227. }
  228. /* Compute the absolute value of X. */
  229. inline HOST_WIDE_INT
  230. abs_hwi (HOST_WIDE_INT x)
  231. {
  232. gcc_checking_assert (x != HOST_WIDE_INT_MIN);
  233. return x >= 0 ? x : -x;
  234. }
  235. /* Compute the absolute value of X as an unsigned type. */
  236. inline unsigned HOST_WIDE_INT
  237. absu_hwi (HOST_WIDE_INT x)
  238. {
  239. return x >= 0 ? (unsigned HOST_WIDE_INT)x : -(unsigned HOST_WIDE_INT)x;
  240. }
  241. #endif /* ! GCC_HWINT_H */