test_cpp_int.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. //
  6. // Compare arithmetic results using fixed_int to GMP results.
  7. //
  8. #ifdef _MSC_VER
  9. #define _SCL_SECURE_NO_WARNINGS
  10. #endif
  11. //
  12. // This ensures all our code gets tested, even though it may
  13. // not be the fastest configuration in normal use:
  14. //
  15. #define BOOST_MP_USE_LIMB_SHIFT
  16. #include <boost/multiprecision/gmp.hpp>
  17. #include <boost/multiprecision/cpp_int.hpp>
  18. #include <boost/random/mersenne_twister.hpp>
  19. #include <boost/random/uniform_int.hpp>
  20. #include <boost/timer.hpp>
  21. #include "test.hpp"
  22. #ifdef _MSC_VER
  23. #pragma warning(disable : 4127) // Conditional expression is constant
  24. #endif
  25. #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3)
  26. #define TEST1
  27. #define TEST2
  28. #define TEST3
  29. #endif
  30. template <class T>
  31. T generate_random(unsigned bits_wanted)
  32. {
  33. static boost::random::mt19937 gen;
  34. typedef boost::random::mt19937::result_type random_type;
  35. T max_val;
  36. unsigned digits;
  37. if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
  38. {
  39. max_val = (std::numeric_limits<T>::max)();
  40. digits = std::numeric_limits<T>::digits;
  41. }
  42. else
  43. {
  44. max_val = T(1) << bits_wanted;
  45. digits = bits_wanted;
  46. }
  47. unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
  48. while ((random_type(1) << bits_per_r_val) > (gen.max)())
  49. --bits_per_r_val;
  50. unsigned terms_needed = digits / bits_per_r_val + 1;
  51. T val = 0;
  52. for (unsigned i = 0; i < terms_needed; ++i)
  53. {
  54. val *= (gen.max)();
  55. val += gen();
  56. }
  57. val %= max_val;
  58. return val;
  59. }
  60. template <class T>
  61. struct is_checked_cpp_int : public boost::mpl::false_
  62. {};
  63. template <unsigned MinBits, unsigned MaxBits, boost::multiprecision::cpp_integer_type SignType, class Allocator, boost::multiprecision::expression_template_option ET>
  64. struct is_checked_cpp_int<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, boost::multiprecision::checked, Allocator>, ET> > : public boost::mpl::true_
  65. {};
  66. template <class Number>
  67. struct tester
  68. {
  69. typedef Number test_type;
  70. typedef typename test_type::backend_type::checked_type checked;
  71. unsigned last_error_count;
  72. boost::timer tim;
  73. boost::multiprecision::mpz_int a, b, c, d;
  74. int si;
  75. unsigned ui;
  76. test_type a1, b1, c1, d1;
  77. void t1()
  78. {
  79. using namespace boost::multiprecision;
  80. BOOST_CHECK_EQUAL(a.str(), a1.str());
  81. BOOST_CHECK_EQUAL(b.str(), b1.str());
  82. BOOST_CHECK_EQUAL(c.str(), c1.str());
  83. BOOST_CHECK_EQUAL(d.str(), d1.str());
  84. BOOST_CHECK_EQUAL(mpz_int(a + b).str(), test_type(a1 + b1).str());
  85. BOOST_CHECK_EQUAL((mpz_int(a) += b).str(), (test_type(a1) += b1).str());
  86. BOOST_CHECK_EQUAL((mpz_int(b) += a).str(), (test_type(b1) += a1).str());
  87. BOOST_CHECK_EQUAL(mpz_int(a - b).str(), test_type(a1 - b1).str());
  88. BOOST_CHECK_EQUAL((mpz_int(a) -= b).str(), (test_type(a1) -= b1).str());
  89. BOOST_CHECK_EQUAL(mpz_int(mpz_int(-a) + b).str(), test_type(test_type(-a1) + b1).str());
  90. BOOST_CHECK_EQUAL(mpz_int(mpz_int(-a) - b).str(), test_type(test_type(-a1) - b1).str());
  91. BOOST_CHECK_EQUAL(mpz_int(c * d).str(), test_type(c1 * d1).str());
  92. BOOST_CHECK_EQUAL((mpz_int(c) *= d).str(), (test_type(c1) *= d1).str());
  93. BOOST_CHECK_EQUAL((mpz_int(d) *= c).str(), (test_type(d1) *= c1).str());
  94. BOOST_CHECK_EQUAL(mpz_int(c * -d).str(), test_type(c1 * -d1).str());
  95. BOOST_CHECK_EQUAL(mpz_int(-c * d).str(), test_type(-c1 * d1).str());
  96. BOOST_CHECK_EQUAL((mpz_int(c) *= -d).str(), (test_type(c1) *= -d1).str());
  97. BOOST_CHECK_EQUAL((mpz_int(-d) *= c).str(), (test_type(-d1) *= c1).str());
  98. BOOST_CHECK_EQUAL(mpz_int(b * c).str(), test_type(b1 * c1).str());
  99. BOOST_CHECK_EQUAL(mpz_int(a / b).str(), test_type(a1 / b1).str());
  100. BOOST_CHECK_EQUAL((mpz_int(a) /= b).str(), (test_type(a1) /= b1).str());
  101. BOOST_CHECK_EQUAL(mpz_int(a / -b).str(), test_type(a1 / -b1).str());
  102. BOOST_CHECK_EQUAL(mpz_int(-a / b).str(), test_type(-a1 / b1).str());
  103. BOOST_CHECK_EQUAL((mpz_int(a) /= -b).str(), (test_type(a1) /= -b1).str());
  104. BOOST_CHECK_EQUAL((mpz_int(-a) /= b).str(), (test_type(-a1) /= b1).str());
  105. BOOST_CHECK_EQUAL(mpz_int(a / d).str(), test_type(a1 / d1).str());
  106. BOOST_CHECK_EQUAL(mpz_int(a % b).str(), test_type(a1 % b1).str());
  107. BOOST_CHECK_EQUAL((mpz_int(a) %= b).str(), (test_type(a1) %= b1).str());
  108. BOOST_CHECK_EQUAL(mpz_int(a % -b).str(), test_type(a1 % -b1).str());
  109. BOOST_CHECK_EQUAL((mpz_int(a) %= -b).str(), (test_type(a1) %= -b1).str());
  110. BOOST_CHECK_EQUAL(mpz_int(-a % b).str(), test_type(-a1 % b1).str());
  111. BOOST_CHECK_EQUAL((mpz_int(-a) %= b).str(), (test_type(-a1) %= b1).str());
  112. BOOST_CHECK_EQUAL(mpz_int(a % d).str(), test_type(a1 % d1).str());
  113. BOOST_CHECK_EQUAL((mpz_int(a) %= d).str(), (test_type(a1) %= d1).str());
  114. if (!std::numeric_limits<test_type>::is_bounded)
  115. {
  116. test_type p = a1 * b1;
  117. test_type r;
  118. divide_qr(p, b1, p, r);
  119. BOOST_CHECK_EQUAL(p, a1);
  120. BOOST_CHECK_EQUAL(r, test_type(0));
  121. p = a1 * d1;
  122. divide_qr(p, d1, p, r);
  123. BOOST_CHECK_EQUAL(p, a1);
  124. BOOST_CHECK_EQUAL(r, test_type(0));
  125. divide_qr(p, test_type(1), p, r);
  126. BOOST_CHECK_EQUAL(p, a1);
  127. BOOST_CHECK_EQUAL(r, test_type(0));
  128. }
  129. }
  130. void t2()
  131. {
  132. using namespace boost::multiprecision;
  133. // bitwise ops:
  134. BOOST_CHECK_EQUAL(mpz_int(a | b).str(), test_type(a1 | b1).str());
  135. BOOST_CHECK_EQUAL((mpz_int(a) |= b).str(), (test_type(a1) |= b1).str());
  136. if (!is_checked_cpp_int<test_type>::value)
  137. {
  138. BOOST_CHECK_EQUAL(mpz_int(-a | b).str(), test_type(-a1 | b1).str());
  139. BOOST_CHECK_EQUAL((mpz_int(-a) |= b).str(), (test_type(-a1) |= b1).str());
  140. BOOST_CHECK_EQUAL(mpz_int(a | -b).str(), test_type(a1 | -b1).str());
  141. BOOST_CHECK_EQUAL((mpz_int(a) |= -b).str(), (test_type(a1) |= -b1).str());
  142. BOOST_CHECK_EQUAL(mpz_int(-a | -b).str(), test_type(-a1 | -b1).str());
  143. BOOST_CHECK_EQUAL((mpz_int(-a) |= -b).str(), (test_type(-a1) |= -b1).str());
  144. }
  145. BOOST_CHECK_EQUAL(mpz_int(a & b).str(), test_type(a1 & b1).str());
  146. BOOST_CHECK_EQUAL((mpz_int(a) &= b).str(), (test_type(a1) &= b1).str());
  147. if (!is_checked_cpp_int<test_type>::value)
  148. {
  149. BOOST_CHECK_EQUAL(mpz_int(-a & b).str(), test_type(-a1 & b1).str());
  150. BOOST_CHECK_EQUAL((mpz_int(-a) &= b).str(), (test_type(-a1) &= b1).str());
  151. BOOST_CHECK_EQUAL(mpz_int(a & -b).str(), test_type(a1 & -b1).str());
  152. BOOST_CHECK_EQUAL((mpz_int(a) &= -b).str(), (test_type(a1) &= -b1).str());
  153. BOOST_CHECK_EQUAL(mpz_int(-a & -b).str(), test_type(-a1 & -b1).str());
  154. BOOST_CHECK_EQUAL((mpz_int(-a) &= -b).str(), (test_type(-a1) &= -b1).str());
  155. }
  156. BOOST_CHECK_EQUAL(mpz_int(a ^ b).str(), test_type(a1 ^ b1).str());
  157. BOOST_CHECK_EQUAL((mpz_int(a) ^= b).str(), (test_type(a1) ^= b1).str());
  158. if (!is_checked_cpp_int<test_type>::value)
  159. {
  160. BOOST_CHECK_EQUAL(mpz_int(-a ^ b).str(), test_type(-a1 ^ b1).str());
  161. BOOST_CHECK_EQUAL((mpz_int(-a) ^= b).str(), (test_type(-a1) ^= b1).str());
  162. BOOST_CHECK_EQUAL(mpz_int(a ^ -b).str(), test_type(a1 ^ -b1).str());
  163. BOOST_CHECK_EQUAL((mpz_int(a) ^= -b).str(), (test_type(a1) ^= -b1).str());
  164. BOOST_CHECK_EQUAL(mpz_int(-a ^ -b).str(), test_type(-a1 ^ -b1).str());
  165. BOOST_CHECK_EQUAL((mpz_int(-a) ^= -b).str(), (test_type(-a1) ^= -b1).str());
  166. }
  167. // Shift ops:
  168. for (unsigned i = 0; i < 128; ++i)
  169. {
  170. if (!std::numeric_limits<test_type>::is_bounded)
  171. {
  172. BOOST_CHECK_EQUAL(mpz_int(a << i).str(), test_type(a1 << i).str());
  173. BOOST_CHECK_EQUAL(mpz_int(-a << i).str(), test_type(-a1 << i).str());
  174. }
  175. else if (!is_checked_cpp_int<test_type>::value)
  176. {
  177. test_type t1(mpz_int(a << i).str());
  178. test_type t2 = a1 << i;
  179. BOOST_CHECK_EQUAL(t1, t2);
  180. t1 = test_type(mpz_int(-a << i).str());
  181. t2 = -a1 << i;
  182. BOOST_CHECK_EQUAL(t1, t2);
  183. }
  184. BOOST_CHECK_EQUAL(mpz_int(a >> i).str(), test_type(a1 >> i).str());
  185. if (!is_checked_cpp_int<test_type>::value)
  186. {
  187. BOOST_CHECK_EQUAL(mpz_int(-a >> i).str(), test_type(-a1 >> i).str());
  188. }
  189. }
  190. // gcd/lcm
  191. BOOST_CHECK_EQUAL(mpz_int(gcd(a, b)).str(), test_type(gcd(a1, b1)).str());
  192. BOOST_CHECK_EQUAL(mpz_int(lcm(c, d)).str(), test_type(lcm(c1, d1)).str());
  193. BOOST_CHECK_EQUAL(mpz_int(gcd(-a, b)).str(), test_type(gcd(-a1, b1)).str());
  194. BOOST_CHECK_EQUAL(mpz_int(lcm(-c, d)).str(), test_type(lcm(-c1, d1)).str());
  195. BOOST_CHECK_EQUAL(mpz_int(gcd(-a, -b)).str(), test_type(gcd(-a1, -b1)).str());
  196. BOOST_CHECK_EQUAL(mpz_int(lcm(-c, -d)).str(), test_type(lcm(-c1, -d1)).str());
  197. BOOST_CHECK_EQUAL(mpz_int(gcd(a, -b)).str(), test_type(gcd(a1, -b1)).str());
  198. BOOST_CHECK_EQUAL(mpz_int(lcm(c, -d)).str(), test_type(lcm(c1, -d1)).str());
  199. // Integer sqrt:
  200. mpz_int r;
  201. test_type r1;
  202. BOOST_CHECK_EQUAL(sqrt(a, r).str(), sqrt(a1, r1).str());
  203. BOOST_CHECK_EQUAL(r.str(), r1.str());
  204. }
  205. void t3()
  206. {
  207. using namespace boost::multiprecision;
  208. // Now check operations involving signed integers:
  209. BOOST_CHECK_EQUAL(mpz_int(a + si).str(), test_type(a1 + si).str());
  210. BOOST_CHECK_EQUAL(mpz_int(a + -si).str(), test_type(a1 + -si).str());
  211. BOOST_CHECK_EQUAL(mpz_int(-a + si).str(), test_type(-a1 + si).str());
  212. BOOST_CHECK_EQUAL(mpz_int(si + a).str(), test_type(si + a1).str());
  213. BOOST_CHECK_EQUAL((mpz_int(a) += si).str(), (test_type(a1) += si).str());
  214. BOOST_CHECK_EQUAL((mpz_int(a) += -si).str(), (test_type(a1) += -si).str());
  215. BOOST_CHECK_EQUAL((mpz_int(-a) += si).str(), (test_type(-a1) += si).str());
  216. BOOST_CHECK_EQUAL((mpz_int(-a) += -si).str(), (test_type(-a1) += -si).str());
  217. BOOST_CHECK_EQUAL(mpz_int(a - si).str(), test_type(a1 - si).str());
  218. BOOST_CHECK_EQUAL(mpz_int(a - -si).str(), test_type(a1 - -si).str());
  219. BOOST_CHECK_EQUAL(mpz_int(-a - si).str(), test_type(-a1 - si).str());
  220. BOOST_CHECK_EQUAL(mpz_int(si - a).str(), test_type(si - a1).str());
  221. BOOST_CHECK_EQUAL((mpz_int(a) -= si).str(), (test_type(a1) -= si).str());
  222. BOOST_CHECK_EQUAL((mpz_int(a) -= -si).str(), (test_type(a1) -= -si).str());
  223. BOOST_CHECK_EQUAL((mpz_int(-a) -= si).str(), (test_type(-a1) -= si).str());
  224. BOOST_CHECK_EQUAL((mpz_int(-a) -= -si).str(), (test_type(-a1) -= -si).str());
  225. BOOST_CHECK_EQUAL(mpz_int(b * si).str(), test_type(b1 * si).str());
  226. BOOST_CHECK_EQUAL(mpz_int(b * -si).str(), test_type(b1 * -si).str());
  227. BOOST_CHECK_EQUAL(mpz_int(-b * si).str(), test_type(-b1 * si).str());
  228. BOOST_CHECK_EQUAL(mpz_int(si * b).str(), test_type(si * b1).str());
  229. BOOST_CHECK_EQUAL((mpz_int(a) *= si).str(), (test_type(a1) *= si).str());
  230. BOOST_CHECK_EQUAL((mpz_int(a) *= -si).str(), (test_type(a1) *= -si).str());
  231. BOOST_CHECK_EQUAL((mpz_int(-a) *= si).str(), (test_type(-a1) *= si).str());
  232. BOOST_CHECK_EQUAL((mpz_int(-a) *= -si).str(), (test_type(-a1) *= -si).str());
  233. BOOST_CHECK_EQUAL(mpz_int(a / si).str(), test_type(a1 / si).str());
  234. BOOST_CHECK_EQUAL(mpz_int(a / -si).str(), test_type(a1 / -si).str());
  235. BOOST_CHECK_EQUAL(mpz_int(-a / si).str(), test_type(-a1 / si).str());
  236. BOOST_CHECK_EQUAL((mpz_int(a) /= si).str(), (test_type(a1) /= si).str());
  237. BOOST_CHECK_EQUAL((mpz_int(a) /= -si).str(), (test_type(a1) /= -si).str());
  238. BOOST_CHECK_EQUAL((mpz_int(-a) /= si).str(), (test_type(-a1) /= si).str());
  239. BOOST_CHECK_EQUAL((mpz_int(-a) /= -si).str(), (test_type(-a1) /= -si).str());
  240. BOOST_CHECK_EQUAL(mpz_int(a % si).str(), test_type(a1 % si).str());
  241. BOOST_CHECK_EQUAL(mpz_int(a % -si).str(), test_type(a1 % -si).str());
  242. BOOST_CHECK_EQUAL(mpz_int(-a % si).str(), test_type(-a1 % si).str());
  243. BOOST_CHECK_EQUAL((mpz_int(a) %= si).str(), (test_type(a1) %= si).str());
  244. BOOST_CHECK_EQUAL((mpz_int(a) %= -si).str(), (test_type(a1) %= -si).str());
  245. BOOST_CHECK_EQUAL((mpz_int(-a) %= si).str(), (test_type(-a1) %= si).str());
  246. BOOST_CHECK_EQUAL((mpz_int(-a) %= -si).str(), (test_type(-a1) %= -si).str());
  247. if ((si > 0) || !is_checked_cpp_int<test_type>::value)
  248. {
  249. BOOST_CHECK_EQUAL(mpz_int(a | si).str(), test_type(a1 | si).str());
  250. BOOST_CHECK_EQUAL((mpz_int(a) |= si).str(), (test_type(a1) |= si).str());
  251. BOOST_CHECK_EQUAL(mpz_int(a & si).str(), test_type(a1 & si).str());
  252. BOOST_CHECK_EQUAL((mpz_int(a) &= si).str(), (test_type(a1) &= si).str());
  253. BOOST_CHECK_EQUAL(mpz_int(a ^ si).str(), test_type(a1 ^ si).str());
  254. BOOST_CHECK_EQUAL((mpz_int(a) ^= si).str(), (test_type(a1) ^= si).str());
  255. BOOST_CHECK_EQUAL(mpz_int(si | a).str(), test_type(si | a1).str());
  256. BOOST_CHECK_EQUAL(mpz_int(si & a).str(), test_type(si & a1).str());
  257. BOOST_CHECK_EQUAL(mpz_int(si ^ a).str(), test_type(si ^ a1).str());
  258. }
  259. BOOST_CHECK_EQUAL(mpz_int(gcd(a, si)).str(), test_type(gcd(a1, si)).str());
  260. BOOST_CHECK_EQUAL(mpz_int(gcd(si, b)).str(), test_type(gcd(si, b1)).str());
  261. BOOST_CHECK_EQUAL(mpz_int(lcm(c, si)).str(), test_type(lcm(c1, si)).str());
  262. BOOST_CHECK_EQUAL(mpz_int(lcm(si, d)).str(), test_type(lcm(si, d1)).str());
  263. BOOST_CHECK_EQUAL(mpz_int(gcd(-a, si)).str(), test_type(gcd(-a1, si)).str());
  264. BOOST_CHECK_EQUAL(mpz_int(gcd(-si, b)).str(), test_type(gcd(-si, b1)).str());
  265. BOOST_CHECK_EQUAL(mpz_int(lcm(-c, si)).str(), test_type(lcm(-c1, si)).str());
  266. BOOST_CHECK_EQUAL(mpz_int(lcm(-si, d)).str(), test_type(lcm(-si, d1)).str());
  267. BOOST_CHECK_EQUAL(mpz_int(gcd(-a, -si)).str(), test_type(gcd(-a1, -si)).str());
  268. BOOST_CHECK_EQUAL(mpz_int(gcd(-si, -b)).str(), test_type(gcd(-si, -b1)).str());
  269. BOOST_CHECK_EQUAL(mpz_int(lcm(-c, -si)).str(), test_type(lcm(-c1, -si)).str());
  270. BOOST_CHECK_EQUAL(mpz_int(lcm(-si, -d)).str(), test_type(lcm(-si, -d1)).str());
  271. BOOST_CHECK_EQUAL(mpz_int(gcd(a, -si)).str(), test_type(gcd(a1, -si)).str());
  272. BOOST_CHECK_EQUAL(mpz_int(gcd(si, -b)).str(), test_type(gcd(si, -b1)).str());
  273. BOOST_CHECK_EQUAL(mpz_int(lcm(c, -si)).str(), test_type(lcm(c1, -si)).str());
  274. BOOST_CHECK_EQUAL(mpz_int(lcm(si, -d)).str(), test_type(lcm(si, -d1)).str());
  275. }
  276. void t4()
  277. {
  278. using namespace boost::multiprecision;
  279. // Now check operations involving unsigned integers:
  280. BOOST_CHECK_EQUAL(mpz_int(a + ui).str(), test_type(a1 + ui).str());
  281. BOOST_CHECK_EQUAL(mpz_int(-a + ui).str(), test_type(-a1 + ui).str());
  282. BOOST_CHECK_EQUAL(mpz_int(ui + a).str(), test_type(ui + a1).str());
  283. BOOST_CHECK_EQUAL((mpz_int(a) += ui).str(), (test_type(a1) += ui).str());
  284. BOOST_CHECK_EQUAL((mpz_int(-a) += ui).str(), (test_type(-a1) += ui).str());
  285. BOOST_CHECK_EQUAL(mpz_int(a - ui).str(), test_type(a1 - ui).str());
  286. BOOST_CHECK_EQUAL(mpz_int(-a - ui).str(), test_type(-a1 - ui).str());
  287. BOOST_CHECK_EQUAL(mpz_int(ui - a).str(), test_type(ui - a1).str());
  288. BOOST_CHECK_EQUAL((mpz_int(a) -= ui).str(), (test_type(a1) -= ui).str());
  289. BOOST_CHECK_EQUAL((mpz_int(-a) -= ui).str(), (test_type(-a1) -= ui).str());
  290. BOOST_CHECK_EQUAL(mpz_int(b * ui).str(), test_type(b1 * ui).str());
  291. BOOST_CHECK_EQUAL(mpz_int(-b * ui).str(), test_type(-b1 * ui).str());
  292. BOOST_CHECK_EQUAL(mpz_int(ui * b).str(), test_type(ui * b1).str());
  293. BOOST_CHECK_EQUAL((mpz_int(a) *= ui).str(), (test_type(a1) *= ui).str());
  294. BOOST_CHECK_EQUAL((mpz_int(-a) *= ui).str(), (test_type(-a1) *= ui).str());
  295. BOOST_CHECK_EQUAL(mpz_int(a / ui).str(), test_type(a1 / ui).str());
  296. BOOST_CHECK_EQUAL(mpz_int(-a / ui).str(), test_type(-a1 / ui).str());
  297. BOOST_CHECK_EQUAL((mpz_int(a) /= ui).str(), (test_type(a1) /= ui).str());
  298. BOOST_CHECK_EQUAL((mpz_int(-a) /= ui).str(), (test_type(-a1) /= ui).str());
  299. BOOST_CHECK_EQUAL(mpz_int(a % ui).str(), test_type(a1 % ui).str());
  300. BOOST_CHECK_EQUAL(mpz_int(-a % ui).str(), test_type(-a1 % ui).str());
  301. BOOST_CHECK_EQUAL((mpz_int(a) %= ui).str(), (test_type(a1) %= ui).str());
  302. BOOST_CHECK_EQUAL((mpz_int(-a) %= ui).str(), (test_type(-a1) %= ui).str());
  303. BOOST_CHECK_EQUAL(mpz_int(a | ui).str(), test_type(a1 | ui).str());
  304. BOOST_CHECK_EQUAL((mpz_int(a) |= ui).str(), (test_type(a1) |= ui).str());
  305. BOOST_CHECK_EQUAL(mpz_int(a & ui).str(), test_type(a1 & ui).str());
  306. BOOST_CHECK_EQUAL((mpz_int(a) &= ui).str(), (test_type(a1) &= ui).str());
  307. BOOST_CHECK_EQUAL(mpz_int(a ^ ui).str(), test_type(a1 ^ ui).str());
  308. BOOST_CHECK_EQUAL((mpz_int(a) ^= ui).str(), (test_type(a1) ^= ui).str());
  309. BOOST_CHECK_EQUAL(mpz_int(ui | a).str(), test_type(ui | a1).str());
  310. BOOST_CHECK_EQUAL(mpz_int(ui & a).str(), test_type(ui & a1).str());
  311. BOOST_CHECK_EQUAL(mpz_int(ui ^ a).str(), test_type(ui ^ a1).str());
  312. BOOST_CHECK_EQUAL(mpz_int(gcd(a, ui)).str(), test_type(gcd(a1, ui)).str());
  313. BOOST_CHECK_EQUAL(mpz_int(gcd(ui, b)).str(), test_type(gcd(ui, b1)).str());
  314. BOOST_CHECK_EQUAL(mpz_int(lcm(c, ui)).str(), test_type(lcm(c1, ui)).str());
  315. BOOST_CHECK_EQUAL(mpz_int(lcm(ui, d)).str(), test_type(lcm(ui, d1)).str());
  316. BOOST_CHECK_EQUAL(mpz_int(gcd(-a, ui)).str(), test_type(gcd(-a1, ui)).str());
  317. BOOST_CHECK_EQUAL(mpz_int(lcm(-c, ui)).str(), test_type(lcm(-c1, ui)).str());
  318. BOOST_CHECK_EQUAL(mpz_int(gcd(ui, -b)).str(), test_type(gcd(ui, -b1)).str());
  319. BOOST_CHECK_EQUAL(mpz_int(lcm(ui, -d)).str(), test_type(lcm(ui, -d1)).str());
  320. if (std::numeric_limits<test_type>::is_modulo && checked::value)
  321. {
  322. static mpz_int m = mpz_int(1) << std::numeric_limits<test_type>::digits;
  323. mpz_int t(a);
  324. test_type t1(a1);
  325. for (unsigned i = 0; i < 10; ++i)
  326. {
  327. t *= a;
  328. t %= m;
  329. t += a;
  330. t %= m;
  331. t1 *= a1;
  332. t1 += a1;
  333. }
  334. BOOST_CHECK_EQUAL(t.str(), t1.str());
  335. }
  336. }
  337. void t5()
  338. {
  339. using namespace boost::multiprecision;
  340. //
  341. // Now integer functions:
  342. //
  343. mpz_int z1, z2;
  344. test_type t1, t2;
  345. divide_qr(a, b, z1, z2);
  346. divide_qr(a1, b1, t1, t2);
  347. BOOST_CHECK_EQUAL(z1.str(), t1.str());
  348. BOOST_CHECK_EQUAL(z2.str(), t2.str());
  349. BOOST_CHECK_EQUAL(integer_modulus(a, si), integer_modulus(a1, si));
  350. BOOST_CHECK_EQUAL(lsb(a), lsb(a1));
  351. BOOST_CHECK_EQUAL(msb(a), msb(a1));
  352. for (unsigned i = 0; i < 1000; i += 13)
  353. {
  354. BOOST_CHECK_EQUAL(bit_test(a, i), bit_test(a1, i));
  355. }
  356. if (!std::numeric_limits<test_type>::is_modulo)
  357. {
  358. // We have to take care that our powers don't grow too large, otherwise this takes "forever",
  359. // also don't test for modulo types, as these may give a different result from arbitrary
  360. // precision types:
  361. BOOST_CHECK_EQUAL(mpz_int(pow(d, ui % 19)).str(), test_type(pow(d1, ui % 19)).str());
  362. BOOST_CHECK_EQUAL(mpz_int(powm(a, b, c)).str(), test_type(powm(a1, b1, c1)).str());
  363. BOOST_CHECK_EQUAL(mpz_int(powm(a, b, ui)).str(), test_type(powm(a1, b1, ui)).str());
  364. BOOST_CHECK_EQUAL(mpz_int(powm(a, ui, c)).str(), test_type(powm(a1, ui, c1)).str());
  365. }
  366. BOOST_CHECK_EQUAL(lsb(a), lsb(a1));
  367. BOOST_CHECK_EQUAL(msb(a), msb(a1));
  368. }
  369. static void test_bug_cases()
  370. {
  371. if (!std::numeric_limits<test_type>::is_bounded)
  372. {
  373. // https://svn.boost.org/trac/boost/ticket/7878
  374. test_type a("0x1000000000000000000000000000000000000000000000000000000000000000");
  375. test_type b = 0xFFFFFFFF;
  376. test_type c = a * b + b; // quotient has 1 in the final place
  377. test_type q, r;
  378. divide_qr(c, b, q, r);
  379. BOOST_CHECK_EQUAL(a + 1, q);
  380. BOOST_CHECK_EQUAL(r, 0);
  381. b = static_cast<test_type>("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
  382. c = a * b + b; // quotient has 1 in the final place
  383. divide_qr(c, b, q, r);
  384. BOOST_CHECK_EQUAL(a + 1, q);
  385. BOOST_CHECK_EQUAL(r, 0);
  386. //
  387. // Not a bug, but test some other special cases that don't otherwise occur through
  388. // random testing:
  389. //
  390. c = a * b; // quotient has zero in the final place
  391. divide_qr(c, b, q, r);
  392. BOOST_CHECK_EQUAL(q, a);
  393. BOOST_CHECK_EQUAL(r, 0);
  394. divide_qr(c, a, q, r);
  395. BOOST_CHECK_EQUAL(q, b);
  396. BOOST_CHECK_EQUAL(r, 0);
  397. ++c;
  398. divide_qr(c, b, q, r);
  399. BOOST_CHECK_EQUAL(q, a);
  400. BOOST_CHECK_EQUAL(r, 1);
  401. }
  402. // Bug https://svn.boost.org/trac/boost/ticket/8126:
  403. test_type a("-4294967296");
  404. test_type b("4294967296");
  405. test_type c("-1");
  406. a = (a / b);
  407. BOOST_CHECK_EQUAL(a, -1);
  408. a = -4294967296;
  409. a = (a / b) * c;
  410. BOOST_CHECK_EQUAL(a, 1);
  411. a = -23;
  412. b = 23;
  413. a = (a / b) * c;
  414. BOOST_CHECK_EQUAL(a, 1);
  415. a = -23;
  416. a = (a / b) / c;
  417. BOOST_CHECK_EQUAL(a, 1);
  418. a = test_type("-26607734784073568386365259775");
  419. b = test_type("8589934592");
  420. a = a / b;
  421. BOOST_CHECK_EQUAL(a, test_type("-3097548007973652377"));
  422. // Bug https://svn.boost.org/trac/boost/ticket/8133:
  423. a = test_type("0x12345600012434ffffffffffffffffffffffff");
  424. unsigned ui = 0xffffffff;
  425. a = a - ui;
  426. BOOST_CHECK_EQUAL(a, test_type("0x12345600012434ffffffffffffffff00000000"));
  427. a = test_type("0x12345600012434ffffffffffffffffffffffff");
  428. #ifndef BOOST_NO_LONG_LONG
  429. unsigned long long ull = 0xffffffffffffffffuLL;
  430. a = a - ull;
  431. BOOST_CHECK_EQUAL(a, test_type("0x12345600012434ffffffff0000000000000000"));
  432. #endif
  433. //
  434. // Now check that things which should be zero really are
  435. // https://svn.boost.org/trac/boost/ticket/8145:
  436. //
  437. a = -1;
  438. a += 1;
  439. BOOST_CHECK_EQUAL(a, 0);
  440. a = 1;
  441. a += -1;
  442. BOOST_CHECK_EQUAL(a, 0);
  443. a = -1;
  444. a += test_type(1);
  445. BOOST_CHECK_EQUAL(a, 0);
  446. a = 1;
  447. a += test_type(-1);
  448. BOOST_CHECK_EQUAL(a, 0);
  449. a = test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
  450. a -= test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
  451. BOOST_CHECK_EQUAL(a, 0);
  452. a = -test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
  453. a += test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
  454. BOOST_CHECK_EQUAL(a, 0);
  455. a = 2;
  456. a *= 0;
  457. BOOST_CHECK_EQUAL(a, 0);
  458. a = -2;
  459. a *= 0;
  460. BOOST_CHECK_EQUAL(a, 0);
  461. a = 2;
  462. a *= test_type(0);
  463. BOOST_CHECK_EQUAL(a, 0);
  464. a = -2;
  465. a *= test_type(0);
  466. BOOST_CHECK_EQUAL(a, 0);
  467. a = -2;
  468. a /= 50;
  469. BOOST_CHECK_EQUAL(a, 0);
  470. a = -test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
  471. a /= (1 + test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
  472. BOOST_CHECK_EQUAL(a, 0);
  473. // https://svn.boost.org/trac/boost/ticket/8160
  474. a = 1;
  475. a = 0 / test_type(1);
  476. BOOST_CHECK_EQUAL(a, 0);
  477. a = 1;
  478. a = 0 % test_type(25);
  479. BOOST_CHECK_EQUAL(a, 0);
  480. #ifndef TEST2
  481. // https://svn.boost.org/trac/boost/ticket/11364
  482. a = 0xfffffffeu;
  483. b = -2;
  484. c = a ^ b;
  485. test_type d = ~(a ^ ~b);
  486. BOOST_CHECK_EQUAL(c, d);
  487. #endif
  488. #if defined(TEST2) || defined(TEST3)
  489. // https://svn.boost.org/trac/boost/ticket/11648
  490. a = (std::numeric_limits<test_type>::max)() - 69;
  491. b = a / 139;
  492. ++b;
  493. c = a / b;
  494. test_type r = a % b;
  495. BOOST_CHECK(r < b);
  496. BOOST_CHECK_EQUAL(a - c * b, r);
  497. #endif
  498. for (ui = 0; ui < 1000; ++ui)
  499. {
  500. boost::multiprecision::mpz_int t;
  501. boost::multiprecision::mpz_int s1 = sqrt(boost::multiprecision::mpz_int(ui), t);
  502. a = sqrt(test_type(ui), b);
  503. BOOST_CHECK_EQUAL(a.str(), s1.str());
  504. BOOST_CHECK_EQUAL(b.str(), t.str());
  505. }
  506. a = -1;
  507. ++a;
  508. BOOST_CHECK_EQUAL(a, 0);
  509. ++--a;
  510. BOOST_CHECK_EQUAL(a, 0);
  511. --++a;
  512. BOOST_CHECK_EQUAL(a, 0);
  513. {
  514. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<> > bigint;
  515. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<64, 64, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u64;
  516. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<128, 128, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u128;
  517. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u256;
  518. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> > s256;
  519. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u160;
  520. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<160, 160, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> > s160;
  521. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<512, 512, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u512;
  522. typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<512, 512, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> > s512;
  523. {
  524. u256 a = 14;
  525. bigint b = bigint("115792089237316195423570985008687907853269984665640564039457584007913129639948");
  526. // to fix cast `a` to dev::bigint
  527. BOOST_CHECK(a < b);
  528. }
  529. {
  530. u256 a = 1;
  531. boost::uint64_t amount = 1;
  532. u256 b = a << amount;
  533. BOOST_CHECK_EQUAL(b, 2);
  534. u256 high_bit = u256(0);
  535. bit_set(high_bit, 255);
  536. BOOST_CHECK_EQUAL(a << 255, high_bit);
  537. BOOST_CHECK_EQUAL(a << boost::uint64_t(256), 0);
  538. BOOST_CHECK_EQUAL(a << 0, a);
  539. u256 c = 3;
  540. BOOST_CHECK_EQUAL(c, 3);
  541. BOOST_CHECK_EQUAL(c << boost::uint64_t(256), 0);
  542. BOOST_CHECK_EQUAL(c << 0, c);
  543. // Bug workaround:
  544. BOOST_CHECK_EQUAL(static_cast<u256>(bigint(u256(3)) << 255), u256(1) << 255);
  545. }
  546. {
  547. BOOST_CHECK_EQUAL(u256(3) << 255, u256(1) << 255);
  548. u256 a = 1;
  549. boost::uint64_t amount = 1;
  550. u256 b = a >> amount;
  551. BOOST_CHECK_EQUAL(b, 0);
  552. BOOST_CHECK_EQUAL(a >> 255, 0);
  553. BOOST_CHECK_EQUAL(a >> boost::uint64_t(256), 0);
  554. BOOST_CHECK_EQUAL(a >> boost::uint64_t(-1), 0);
  555. u256 h;
  556. bit_set(h, 255);
  557. BOOST_CHECK_EQUAL(h >> 0, u256(1) << 255);
  558. BOOST_CHECK_EQUAL(h >> 1, u256(1) << 254);
  559. BOOST_CHECK_EQUAL(h >> 2, u256(1) << 253);
  560. BOOST_CHECK_EQUAL(h >> 254, u256(1) << 1);
  561. BOOST_CHECK_EQUAL(h >> 255, u256(1) << 0);
  562. BOOST_CHECK_EQUAL(h >> 256, 0);
  563. BOOST_CHECK_EQUAL(h >> boost::uint64_t(-1), 0);
  564. u256 g;
  565. bit_set(g, 255);
  566. bit_set(g, 254);
  567. BOOST_CHECK_EQUAL(g >> 255, 1);
  568. BOOST_CHECK_EQUAL(g >> 254, 3);
  569. BOOST_CHECK_EQUAL(g >> 253, 3 << 1);
  570. BOOST_CHECK_EQUAL(g >> 252, 3 << 2);
  571. BOOST_CHECK_EQUAL(g >> 251, 3 << 3);
  572. BOOST_CHECK_EQUAL(g >> 0, u256(3) << 254);
  573. BOOST_CHECK_EQUAL(g >> 1, u256(3) << 253);
  574. BOOST_CHECK_EQUAL(g >> 2, u256(3) << 252);
  575. BOOST_CHECK_EQUAL(g >> 3, u256(3) << 251);
  576. BOOST_CHECK_EQUAL(g >> 100, u256(3) << 154);
  577. BOOST_CHECK_EQUAL(g >> 256, 0);
  578. BOOST_CHECK_EQUAL(g >> 257, 0);
  579. BOOST_CHECK_EQUAL(g >> boost::uint32_t(-1), 0);
  580. BOOST_CHECK_EQUAL(g >> boost::uint64_t(-1), 0);
  581. BOOST_CHECK_EQUAL(g >> boost::uint16_t(-1), 0);
  582. BOOST_CHECK_EQUAL(g >> (boost::uint16_t(-1) - 1), 0);
  583. }
  584. {
  585. s256 a = 1;
  586. uint64_t amount = 1;
  587. s256 b = a >> amount;
  588. BOOST_CHECK_EQUAL(b, 0);
  589. BOOST_CHECK_EQUAL(a >> 255, 0);
  590. BOOST_CHECK_EQUAL(a >> boost::uint64_t(256), 0);
  591. BOOST_CHECK_EQUAL(a >> boost::uint64_t(-1), 0);
  592. s256 n = -1;
  593. BOOST_CHECK_EQUAL(n >> 0, n);
  594. BOOST_CHECK_EQUAL(n >> 1, n);
  595. BOOST_CHECK_EQUAL(n >> 2, n);
  596. BOOST_CHECK_EQUAL(n >> 254, n);
  597. BOOST_CHECK_EQUAL(n >> 255, n);
  598. BOOST_CHECK_EQUAL(n >> 256, n);
  599. BOOST_CHECK_EQUAL(n >> 257, n);
  600. BOOST_CHECK_EQUAL(n >> ~boost::uint64_t(0), n);
  601. // Test min value. This actually -(2^256-1), not -(2^255) as in C.
  602. s256 h = (std::numeric_limits<s256>::min)();
  603. BOOST_CHECK_LT(h, 0);
  604. BOOST_CHECK_EQUAL(h >> 0, h);
  605. BOOST_CHECK_EQUAL(h >> 256, -1);
  606. // Test EVM min value.
  607. s256 g = s256(-1) << 255;
  608. BOOST_CHECK_LT(g, 0);
  609. BOOST_CHECK_EQUAL(static_cast<u256>(g), u256(1) << 255);
  610. BOOST_CHECK_EQUAL(g >> 0, g);
  611. BOOST_CHECK_EQUAL(static_cast<u256>(g >> 1), u256(0b11) << 254);
  612. BOOST_CHECK_EQUAL(static_cast<u256>(g >> 2), u256(0b111) << 253);
  613. BOOST_CHECK_EQUAL(static_cast<u256>(g >> 3), u256(0b1111) << 252);
  614. BOOST_CHECK_EQUAL(static_cast<u256>(g >> 255), ~u256(0));
  615. BOOST_CHECK_EQUAL(static_cast<u256>(g >> 254), ~u256(0b1));
  616. BOOST_CHECK_EQUAL(static_cast<u256>(g >> 253), ~u256(0b11));
  617. // Test shifting more that one bit.
  618. s256 k = s256(0b111) << 252;
  619. BOOST_CHECK_EQUAL(k, u256(0b111) << 252);
  620. BOOST_CHECK_EQUAL(k >> 1, u256(0b111) << 251);
  621. BOOST_CHECK_EQUAL(k >> 2, u256(0b111) << 250);
  622. BOOST_CHECK_EQUAL(k >> 252, 0b111);
  623. BOOST_CHECK_EQUAL(k >> 253, 0b11);
  624. BOOST_CHECK_EQUAL(k >> 254, 0b1);
  625. BOOST_CHECK_EQUAL(k >> 255, 0);
  626. BOOST_CHECK_EQUAL(k >> 256, 0);
  627. BOOST_CHECK_EQUAL(k >> ~boost::uint32_t(0), 0);
  628. // Division equivalence.
  629. // Built-in type:
  630. if (std::numeric_limits<boost::int64_t>::is_specialized)
  631. {
  632. boost::int64_t d = (std::numeric_limits<boost::int64_t>::min)();
  633. BOOST_CHECK_EQUAL(d >> 1, d / 2);
  634. int64_t e = d + 1;
  635. BOOST_CHECK_EQUAL(e >> 1, e / 2 - 1);
  636. // Boost type:
  637. BOOST_CHECK_EQUAL(h >> 1, h / 2 - 1);
  638. }
  639. }
  640. }
  641. }
  642. void test()
  643. {
  644. using namespace boost::multiprecision;
  645. test_bug_cases();
  646. last_error_count = 0;
  647. BOOST_CHECK_EQUAL(Number(), 0);
  648. for (int i = 0; i < 10000; ++i)
  649. {
  650. a = generate_random<mpz_int>(1000);
  651. b = generate_random<mpz_int>(512);
  652. c = generate_random<mpz_int>(256);
  653. d = generate_random<mpz_int>(32);
  654. si = d.convert_to<int>();
  655. ui = si;
  656. a1 = static_cast<test_type>(a.str());
  657. b1 = static_cast<test_type>(b.str());
  658. c1 = static_cast<test_type>(c.str());
  659. d1 = static_cast<test_type>(d.str());
  660. t1();
  661. t2();
  662. #ifndef SLOW_COMPILER
  663. t3();
  664. t4();
  665. t5();
  666. #endif
  667. if (last_error_count != (unsigned)boost::detail::test_errors())
  668. {
  669. last_error_count = boost::detail::test_errors();
  670. std::cout << std::hex << std::showbase;
  671. std::cout << "a = " << a << std::endl;
  672. std::cout << "a1 = " << a1 << std::endl;
  673. std::cout << "b = " << b << std::endl;
  674. std::cout << "b1 = " << b1 << std::endl;
  675. std::cout << "c = " << c << std::endl;
  676. std::cout << "c1 = " << c1 << std::endl;
  677. std::cout << "d = " << d << std::endl;
  678. std::cout << "d1 = " << d1 << std::endl;
  679. std::cout << "a + b = " << a + b << std::endl;
  680. std::cout << "a1 + b1 = " << a1 + b1 << std::endl;
  681. std::cout << std::dec;
  682. std::cout << "a - b = " << a - b << std::endl;
  683. std::cout << "a1 - b1 = " << a1 - b1 << std::endl;
  684. std::cout << "-a + b = " << mpz_int(-a) + b << std::endl;
  685. std::cout << "-a1 + b1 = " << test_type(-a1) + b1 << std::endl;
  686. std::cout << "-a - b = " << mpz_int(-a) - b << std::endl;
  687. std::cout << "-a1 - b1 = " << test_type(-a1) - b1 << std::endl;
  688. std::cout << "c*d = " << c * d << std::endl;
  689. std::cout << "c1*d1 = " << c1 * d1 << std::endl;
  690. std::cout << "b*c = " << b * c << std::endl;
  691. std::cout << "b1*c1 = " << b1 * c1 << std::endl;
  692. std::cout << "a/b = " << a / b << std::endl;
  693. std::cout << "a1/b1 = " << a1 / b1 << std::endl;
  694. std::cout << "a/d = " << a / d << std::endl;
  695. std::cout << "a1/d1 = " << a1 / d1 << std::endl;
  696. std::cout << "a%b = " << a % b << std::endl;
  697. std::cout << "a1%b1 = " << a1 % b1 << std::endl;
  698. std::cout << "a%d = " << a % d << std::endl;
  699. std::cout << "a1%d1 = " << a1 % d1 << std::endl;
  700. }
  701. //
  702. // Check to see if test is taking too long.
  703. // Tests run on the compiler farm time out after 300 seconds,
  704. // so don't get too close to that:
  705. //
  706. #ifndef CI_SUPPRESS_KNOWN_ISSUES
  707. if (tim.elapsed() > 200)
  708. #else
  709. if (tim.elapsed() > 25)
  710. #endif
  711. {
  712. std::cout << "Timeout reached, aborting tests now....\n";
  713. break;
  714. }
  715. }
  716. }
  717. };
  718. int main()
  719. {
  720. using namespace boost::multiprecision;
  721. #ifdef TEST1
  722. tester<cpp_int> t1;
  723. t1.test();
  724. #endif
  725. #ifdef TEST2
  726. tester<number<cpp_int_backend<2048, 2048, signed_magnitude, checked, void> > > t2;
  727. t2.test();
  728. #endif
  729. #ifdef TEST3
  730. // Unchecked test verifies modulo arithmetic:
  731. tester<number<cpp_int_backend<2048, 2048, signed_magnitude, unchecked, void> > > t3;
  732. t3.test();
  733. #endif
  734. #ifdef TEST4
  735. tester<number<cpp_int_backend<0, 2048, signed_magnitude, unchecked, std::allocator<char> > > > t4;
  736. t4.test();
  737. #endif
  738. #ifdef TEST5
  739. tester<number<cpp_int_backend<0, 2048, signed_magnitude, unchecked> > > t5;
  740. t5.test();
  741. #endif
  742. return boost::report_errors();
  743. }