double-int.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* Operations with long integers.
  2. Copyright (C) 2006-2019 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef DOUBLE_INT_H
  16. #define DOUBLE_INT_H
  17. /* A large integer is currently represented as a pair of HOST_WIDE_INTs.
  18. It therefore represents a number with precision of
  19. 2 * HOST_BITS_PER_WIDE_INT bits (it is however possible that the
  20. internal representation will change, if numbers with greater precision
  21. are needed, so the users should not rely on it). The representation does
  22. not contain any information about signedness of the represented value, so
  23. it can be used to represent both signed and unsigned numbers. For
  24. operations where the results depend on signedness (division, comparisons),
  25. it must be specified separately. For each such operation, there are three
  26. versions of the function -- double_int_op, that takes an extra UNS argument
  27. giving the signedness of the values, and double_int_sop and double_int_uop
  28. that stand for its specializations for signed and unsigned values.
  29. You may also represent with numbers in smaller precision using double_int.
  30. You however need to use double_int_ext (that fills in the bits of the
  31. number over the prescribed precision with zeros or with the sign bit) before
  32. operations that do not perform arithmetics modulo 2^precision (comparisons,
  33. division), and possibly before storing the results, if you want to keep
  34. them in some canonical form). In general, the signedness of double_int_ext
  35. should match the signedness of the operation.
  36. ??? The components of double_int differ in signedness mostly for
  37. historical reasons (they replace an older structure used to represent
  38. numbers with precision higher than HOST_WIDE_INT). It might be less
  39. confusing to have them both signed or both unsigned. */
  40. struct double_int
  41. {
  42. /* Normally, we would define constructors to create instances.
  43. Two things prevent us from doing so.
  44. First, defining a constructor makes the class non-POD in C++03,
  45. and we certainly want double_int to be a POD.
  46. Second, the GCC conding conventions prefer explicit conversion,
  47. and explicit conversion operators are not available until C++11. */
  48. static double_int from_uhwi (unsigned HOST_WIDE_INT cst);
  49. static double_int from_shwi (HOST_WIDE_INT cst);
  50. static double_int from_pair (HOST_WIDE_INT high, unsigned HOST_WIDE_INT low);
  51. /* Construct from a fuffer of length LEN. BUFFER will be read according
  52. to byte endianness and word endianness. */
  53. static double_int from_buffer (const unsigned char *buffer, int len);
  54. /* No copy assignment operator or destructor to keep the type a POD. */
  55. /* There are some special value-creation static member functions. */
  56. static double_int mask (unsigned prec);
  57. static double_int max_value (unsigned int prec, bool uns);
  58. static double_int min_value (unsigned int prec, bool uns);
  59. /* The following functions are mutating operations. */
  60. double_int &operator ++ (); // prefix
  61. double_int &operator -- (); // prefix
  62. double_int &operator *= (double_int);
  63. double_int &operator += (double_int);
  64. double_int &operator -= (double_int);
  65. double_int &operator &= (double_int);
  66. double_int &operator ^= (double_int);
  67. double_int &operator |= (double_int);
  68. /* The following functions are non-mutating operations. */
  69. /* Conversion functions. */
  70. HOST_WIDE_INT to_shwi () const;
  71. unsigned HOST_WIDE_INT to_uhwi () const;
  72. /* Conversion query functions. */
  73. bool fits_uhwi () const;
  74. bool fits_shwi () const;
  75. bool fits_hwi (bool uns) const;
  76. /* Attribute query functions. */
  77. int trailing_zeros () const;
  78. int popcount () const;
  79. /* Arithmetic query operations. */
  80. bool multiple_of (double_int, bool, double_int *) const;
  81. /* Arithmetic operation functions. */
  82. /* The following operations perform arithmetics modulo 2^precision, so you
  83. do not need to call .ext between them, even if you are representing
  84. numbers with precision less than HOST_BITS_PER_DOUBLE_INT bits. */
  85. double_int set_bit (unsigned) const;
  86. double_int mul_with_sign (double_int, bool unsigned_p, bool *overflow) const;
  87. double_int wide_mul_with_sign (double_int, bool unsigned_p,
  88. double_int *higher, bool *overflow) const;
  89. double_int add_with_sign (double_int, bool unsigned_p, bool *overflow) const;
  90. double_int sub_with_overflow (double_int, bool *overflow) const;
  91. double_int neg_with_overflow (bool *overflow) const;
  92. double_int operator * (double_int) const;
  93. double_int operator + (double_int) const;
  94. double_int operator - (double_int) const;
  95. double_int operator - () const;
  96. double_int operator ~ () const;
  97. double_int operator & (double_int) const;
  98. double_int operator | (double_int) const;
  99. double_int operator ^ (double_int) const;
  100. double_int and_not (double_int) const;
  101. double_int lshift (HOST_WIDE_INT count) const;
  102. double_int lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const;
  103. double_int rshift (HOST_WIDE_INT count) const;
  104. double_int rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const;
  105. double_int alshift (HOST_WIDE_INT count, unsigned int prec) const;
  106. double_int arshift (HOST_WIDE_INT count, unsigned int prec) const;
  107. double_int llshift (HOST_WIDE_INT count, unsigned int prec) const;
  108. double_int lrshift (HOST_WIDE_INT count, unsigned int prec) const;
  109. double_int lrotate (HOST_WIDE_INT count, unsigned int prec) const;
  110. double_int rrotate (HOST_WIDE_INT count, unsigned int prec) const;
  111. /* You must ensure that double_int::ext is called on the operands
  112. of the following operations, if the precision of the numbers
  113. is less than HOST_BITS_PER_DOUBLE_INT bits. */
  114. double_int div (double_int, bool, unsigned) const;
  115. double_int sdiv (double_int, unsigned) const;
  116. double_int udiv (double_int, unsigned) const;
  117. double_int mod (double_int, bool, unsigned) const;
  118. double_int smod (double_int, unsigned) const;
  119. double_int umod (double_int, unsigned) const;
  120. double_int divmod_with_overflow (double_int, bool, unsigned,
  121. double_int *, bool *) const;
  122. double_int divmod (double_int, bool, unsigned, double_int *) const;
  123. double_int sdivmod (double_int, unsigned, double_int *) const;
  124. double_int udivmod (double_int, unsigned, double_int *) const;
  125. /* Precision control functions. */
  126. double_int ext (unsigned prec, bool uns) const;
  127. double_int zext (unsigned prec) const;
  128. double_int sext (unsigned prec) const;
  129. /* Comparative functions. */
  130. bool is_zero () const;
  131. bool is_one () const;
  132. bool is_minus_one () const;
  133. bool is_negative () const;
  134. int cmp (double_int b, bool uns) const;
  135. int ucmp (double_int b) const;
  136. int scmp (double_int b) const;
  137. bool ult (double_int b) const;
  138. bool ule (double_int b) const;
  139. bool ugt (double_int b) const;
  140. bool slt (double_int b) const;
  141. bool sle (double_int b) const;
  142. bool sgt (double_int b) const;
  143. double_int max (double_int b, bool uns);
  144. double_int smax (double_int b);
  145. double_int umax (double_int b);
  146. double_int min (double_int b, bool uns);
  147. double_int smin (double_int b);
  148. double_int umin (double_int b);
  149. bool operator == (double_int cst2) const;
  150. bool operator != (double_int cst2) const;
  151. /* Please migrate away from using these member variables publicly. */
  152. unsigned HOST_WIDE_INT low;
  153. HOST_WIDE_INT high;
  154. };
  155. #define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT)
  156. /* Constructors and conversions. */
  157. /* Constructs double_int from integer CST. The bits over the precision of
  158. HOST_WIDE_INT are filled with the sign bit. */
  159. inline double_int
  160. double_int::from_shwi (HOST_WIDE_INT cst)
  161. {
  162. double_int r;
  163. r.low = (unsigned HOST_WIDE_INT) cst;
  164. r.high = cst < 0 ? -1 : 0;
  165. return r;
  166. }
  167. /* Some useful constants. */
  168. /* FIXME(crowl): Maybe remove after converting callers?
  169. The problem is that a named constant would not be as optimizable,
  170. while the functional syntax is more verbose. */
  171. #define double_int_minus_one (double_int::from_shwi (-1))
  172. #define double_int_zero (double_int::from_shwi (0))
  173. #define double_int_one (double_int::from_shwi (1))
  174. #define double_int_two (double_int::from_shwi (2))
  175. #define double_int_ten (double_int::from_shwi (10))
  176. /* Constructs double_int from unsigned integer CST. The bits over the
  177. precision of HOST_WIDE_INT are filled with zeros. */
  178. inline double_int
  179. double_int::from_uhwi (unsigned HOST_WIDE_INT cst)
  180. {
  181. double_int r;
  182. r.low = cst;
  183. r.high = 0;
  184. return r;
  185. }
  186. inline double_int
  187. double_int::from_pair (HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
  188. {
  189. double_int r;
  190. r.low = low;
  191. r.high = high;
  192. return r;
  193. }
  194. inline double_int &
  195. double_int::operator ++ ()
  196. {
  197. *this += double_int_one;
  198. return *this;
  199. }
  200. inline double_int &
  201. double_int::operator -- ()
  202. {
  203. *this -= double_int_one;
  204. return *this;
  205. }
  206. inline double_int &
  207. double_int::operator &= (double_int b)
  208. {
  209. *this = *this & b;
  210. return *this;
  211. }
  212. inline double_int &
  213. double_int::operator ^= (double_int b)
  214. {
  215. *this = *this ^ b;
  216. return *this;
  217. }
  218. inline double_int &
  219. double_int::operator |= (double_int b)
  220. {
  221. *this = *this | b;
  222. return *this;
  223. }
  224. /* Returns value of CST as a signed number. CST must satisfy
  225. double_int::fits_signed. */
  226. inline HOST_WIDE_INT
  227. double_int::to_shwi () const
  228. {
  229. return (HOST_WIDE_INT) low;
  230. }
  231. /* Returns value of CST as an unsigned number. CST must satisfy
  232. double_int::fits_unsigned. */
  233. inline unsigned HOST_WIDE_INT
  234. double_int::to_uhwi () const
  235. {
  236. return low;
  237. }
  238. /* Returns true if CST fits in unsigned HOST_WIDE_INT. */
  239. inline bool
  240. double_int::fits_uhwi () const
  241. {
  242. return high == 0;
  243. }
  244. /* Logical operations. */
  245. /* Returns ~A. */
  246. inline double_int
  247. double_int::operator ~ () const
  248. {
  249. double_int result;
  250. result.low = ~low;
  251. result.high = ~high;
  252. return result;
  253. }
  254. /* Returns A | B. */
  255. inline double_int
  256. double_int::operator | (double_int b) const
  257. {
  258. double_int result;
  259. result.low = low | b.low;
  260. result.high = high | b.high;
  261. return result;
  262. }
  263. /* Returns A & B. */
  264. inline double_int
  265. double_int::operator & (double_int b) const
  266. {
  267. double_int result;
  268. result.low = low & b.low;
  269. result.high = high & b.high;
  270. return result;
  271. }
  272. /* Returns A & ~B. */
  273. inline double_int
  274. double_int::and_not (double_int b) const
  275. {
  276. double_int result;
  277. result.low = low & ~b.low;
  278. result.high = high & ~b.high;
  279. return result;
  280. }
  281. /* Returns A ^ B. */
  282. inline double_int
  283. double_int::operator ^ (double_int b) const
  284. {
  285. double_int result;
  286. result.low = low ^ b.low;
  287. result.high = high ^ b.high;
  288. return result;
  289. }
  290. void dump_double_int (FILE *, double_int, bool);
  291. #define ALL_ONES HOST_WIDE_INT_M1U
  292. /* The operands of the following comparison functions must be processed
  293. with double_int_ext, if their precision is less than
  294. HOST_BITS_PER_DOUBLE_INT bits. */
  295. /* Returns true if CST is zero. */
  296. inline bool
  297. double_int::is_zero () const
  298. {
  299. return low == 0 && high == 0;
  300. }
  301. /* Returns true if CST is one. */
  302. inline bool
  303. double_int::is_one () const
  304. {
  305. return low == 1 && high == 0;
  306. }
  307. /* Returns true if CST is minus one. */
  308. inline bool
  309. double_int::is_minus_one () const
  310. {
  311. return low == ALL_ONES && high == -1;
  312. }
  313. /* Returns true if CST is negative. */
  314. inline bool
  315. double_int::is_negative () const
  316. {
  317. return high < 0;
  318. }
  319. /* Returns true if CST1 == CST2. */
  320. inline bool
  321. double_int::operator == (double_int cst2) const
  322. {
  323. return low == cst2.low && high == cst2.high;
  324. }
  325. /* Returns true if CST1 != CST2. */
  326. inline bool
  327. double_int::operator != (double_int cst2) const
  328. {
  329. return low != cst2.low || high != cst2.high;
  330. }
  331. /* Return number of set bits of CST. */
  332. inline int
  333. double_int::popcount () const
  334. {
  335. return popcount_hwi (high) + popcount_hwi (low);
  336. }
  337. #ifndef GENERATOR_FILE
  338. /* Conversion to and from GMP integer representations. */
  339. void mpz_set_double_int (mpz_t, double_int, bool);
  340. double_int mpz_get_double_int (const_tree, mpz_t, bool);
  341. #endif
  342. namespace wi
  343. {
  344. template <>
  345. struct int_traits <double_int>
  346. {
  347. static const enum precision_type precision_type = CONST_PRECISION;
  348. static const bool host_dependent_precision = true;
  349. static const unsigned int precision = HOST_BITS_PER_DOUBLE_INT;
  350. static unsigned int get_precision (const double_int &);
  351. static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
  352. const double_int &);
  353. };
  354. }
  355. inline unsigned int
  356. wi::int_traits <double_int>::get_precision (const double_int &)
  357. {
  358. return precision;
  359. }
  360. inline wi::storage_ref
  361. wi::int_traits <double_int>::decompose (HOST_WIDE_INT *scratch, unsigned int p,
  362. const double_int &x)
  363. {
  364. gcc_checking_assert (precision == p);
  365. scratch[0] = x.low;
  366. if ((x.high == 0 && scratch[0] >= 0) || (x.high == -1 && scratch[0] < 0))
  367. return wi::storage_ref (scratch, 1, precision);
  368. scratch[1] = x.high;
  369. return wi::storage_ref (scratch, 2, precision);
  370. }
  371. #endif /* DOUBLE_INT_H */