double-int.h 13 KB

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