tommath.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2011 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_MP_TOMMATH_BACKEND_HPP
  6. #define BOOST_MATH_MP_TOMMATH_BACKEND_HPP
  7. #include <boost/multiprecision/number.hpp>
  8. #include <boost/multiprecision/rational_adaptor.hpp>
  9. #include <boost/multiprecision/detail/integer_ops.hpp>
  10. #include <boost/math/special_functions/fpclassify.hpp>
  11. #include <boost/cstdint.hpp>
  12. #include <boost/scoped_array.hpp>
  13. #include <boost/functional/hash_fwd.hpp>
  14. #include <tommath.h>
  15. #include <cctype>
  16. #include <cmath>
  17. #include <limits>
  18. #include <climits>
  19. namespace boost {
  20. namespace multiprecision {
  21. namespace backends {
  22. namespace detail {
  23. template <class ErrType>
  24. inline void check_tommath_result(ErrType v)
  25. {
  26. if (v != MP_OKAY)
  27. {
  28. BOOST_THROW_EXCEPTION(std::runtime_error(mp_error_to_string(v)));
  29. }
  30. }
  31. } // namespace detail
  32. struct tommath_int;
  33. void eval_multiply(tommath_int& t, const tommath_int& o);
  34. void eval_add(tommath_int& t, const tommath_int& o);
  35. struct tommath_int
  36. {
  37. typedef mpl::list<boost::int32_t, boost::long_long_type> signed_types;
  38. typedef mpl::list<boost::uint32_t, boost::ulong_long_type> unsigned_types;
  39. typedef mpl::list<long double> float_types;
  40. tommath_int()
  41. {
  42. detail::check_tommath_result(mp_init(&m_data));
  43. }
  44. tommath_int(const tommath_int& o)
  45. {
  46. detail::check_tommath_result(mp_init_copy(&m_data, const_cast< ::mp_int*>(&o.m_data)));
  47. }
  48. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  49. tommath_int(tommath_int&& o) BOOST_NOEXCEPT
  50. {
  51. m_data = o.m_data;
  52. o.m_data.dp = 0;
  53. }
  54. tommath_int& operator=(tommath_int&& o)
  55. {
  56. mp_exch(&m_data, &o.m_data);
  57. return *this;
  58. }
  59. #endif
  60. tommath_int& operator=(const tommath_int& o)
  61. {
  62. if (m_data.dp == 0)
  63. detail::check_tommath_result(mp_init(&m_data));
  64. if (o.m_data.dp)
  65. detail::check_tommath_result(mp_copy(const_cast< ::mp_int*>(&o.m_data), &m_data));
  66. return *this;
  67. }
  68. #if defined(DIGIT_BIT)
  69. // Pick off 32 bit chunks for mp_set_int:
  70. tommath_int& operator=(boost::ulong_long_type i)
  71. {
  72. if (m_data.dp == 0)
  73. detail::check_tommath_result(mp_init(&m_data));
  74. boost::ulong_long_type mask = ((1uLL << 32) - 1);
  75. unsigned shift = 0;
  76. ::mp_int t;
  77. detail::check_tommath_result(mp_init(&t));
  78. mp_zero(&m_data);
  79. while (i)
  80. {
  81. detail::check_tommath_result(mp_set_int(&t, static_cast<unsigned>(i & mask)));
  82. if (shift)
  83. detail::check_tommath_result(mp_mul_2d(&t, shift, &t));
  84. detail::check_tommath_result((mp_add(&m_data, &t, &m_data)));
  85. shift += 32;
  86. i >>= 32;
  87. }
  88. mp_clear(&t);
  89. return *this;
  90. }
  91. #elif !defined(ULLONG_MAX) || (ULLONG_MAX != 18446744073709551615uLL)
  92. // Pick off 64 bit chunks for mp_set_i64:
  93. tommath_int& operator=(boost::ulong_long_type i)
  94. {
  95. if (m_data.dp == 0)
  96. detail::check_tommath_result(mp_init(&m_data));
  97. if(sizeof(boost::ulong_long_type) * CHAR_BIT == 64)
  98. {
  99. mp_set_u64(&m_data, i);
  100. return *this;
  101. }
  102. boost::ulong_long_type mask = ((1uLL << 64) - 1);
  103. unsigned shift = 0;
  104. ::mp_int t;
  105. detail::check_tommath_result(mp_init(&t));
  106. mp_zero(&m_data);
  107. while (i)
  108. {
  109. detail::check_tommath_result(mp_set_i64(&t, static_cast<boost::uint64_t>(i & mask)));
  110. if (shift)
  111. detail::check_tommath_result(mp_mul_2d(&t, shift, &t));
  112. detail::check_tommath_result((mp_add(&m_data, &t, &m_data)));
  113. shift += 64;
  114. i >>= 64;
  115. }
  116. mp_clear(&t);
  117. return *this;
  118. }
  119. #else
  120. tommath_int& operator=(boost::ulong_long_type i)
  121. {
  122. if (m_data.dp == 0)
  123. detail::check_tommath_result(mp_init(&m_data));
  124. mp_set_u64(&m_data, i);
  125. return *this;
  126. }
  127. #endif
  128. tommath_int& operator=(boost::long_long_type i)
  129. {
  130. if (m_data.dp == 0)
  131. detail::check_tommath_result(mp_init(&m_data));
  132. bool neg = i < 0;
  133. *this = boost::multiprecision::detail::unsigned_abs(i);
  134. if (neg)
  135. detail::check_tommath_result(mp_neg(&m_data, &m_data));
  136. return *this;
  137. }
  138. //
  139. // Note that although mp_set_int takes an unsigned long as an argument
  140. // it only sets the first 32-bits to the result, and ignores the rest.
  141. // So use uint32_t as the largest type to pass to this function.
  142. //
  143. tommath_int& operator=(boost::uint32_t i)
  144. {
  145. if (m_data.dp == 0)
  146. detail::check_tommath_result(mp_init(&m_data));
  147. #ifdef DIGIT_BIT
  148. detail::check_tommath_result((mp_set_int(&m_data, i)));
  149. #else
  150. mp_set_u32(&m_data, i);
  151. #endif
  152. return *this;
  153. }
  154. tommath_int& operator=(boost::int32_t i)
  155. {
  156. if (m_data.dp == 0)
  157. detail::check_tommath_result(mp_init(&m_data));
  158. bool neg = i < 0;
  159. *this = boost::multiprecision::detail::unsigned_abs(i);
  160. if (neg)
  161. detail::check_tommath_result(mp_neg(&m_data, &m_data));
  162. return *this;
  163. }
  164. tommath_int& operator=(long double a)
  165. {
  166. using std::floor;
  167. using std::frexp;
  168. using std::ldexp;
  169. if (m_data.dp == 0)
  170. detail::check_tommath_result(mp_init(&m_data));
  171. if (a == 0)
  172. {
  173. #ifdef DIGIT_BIT
  174. detail::check_tommath_result(mp_set_int(&m_data, 0));
  175. #else
  176. mp_set_i32(&m_data, 0);
  177. #endif
  178. return *this;
  179. }
  180. if (a == 1)
  181. {
  182. #ifdef DIGIT_BIT
  183. detail::check_tommath_result(mp_set_int(&m_data, 1));
  184. #else
  185. mp_set_i32(&m_data, 1);
  186. #endif
  187. return *this;
  188. }
  189. BOOST_ASSERT(!(boost::math::isinf)(a));
  190. BOOST_ASSERT(!(boost::math::isnan)(a));
  191. int e;
  192. long double f, term;
  193. #ifdef DIGIT_BIT
  194. detail::check_tommath_result(mp_set_int(&m_data, 0u));
  195. #else
  196. mp_set_i32(&m_data, 0);
  197. #endif
  198. ::mp_int t;
  199. detail::check_tommath_result(mp_init(&t));
  200. f = frexp(a, &e);
  201. #ifdef DIGIT_BIT
  202. static const int shift = std::numeric_limits<int>::digits - 1;
  203. typedef int part_type;
  204. #else
  205. static const int shift = std::numeric_limits<boost::int64_t>::digits - 1;
  206. typedef boost::int64_t part_type;
  207. #endif
  208. while (f)
  209. {
  210. // extract int sized bits from f:
  211. f = ldexp(f, shift);
  212. term = floor(f);
  213. e -= shift;
  214. detail::check_tommath_result(mp_mul_2d(&m_data, shift, &m_data));
  215. if (term > 0)
  216. {
  217. #ifdef DIGIT_BIT
  218. detail::check_tommath_result(mp_set_int(&t, static_cast<part_type>(term)));
  219. #else
  220. mp_set_i64(&t, static_cast<part_type>(term));
  221. #endif
  222. detail::check_tommath_result(mp_add(&m_data, &t, &m_data));
  223. }
  224. else
  225. {
  226. #ifdef DIGIT_BIT
  227. detail::check_tommath_result(mp_set_int(&t, static_cast<part_type>(-term)));
  228. #else
  229. mp_set_i64(&t, static_cast<part_type>(-term));
  230. #endif
  231. detail::check_tommath_result(mp_sub(&m_data, &t, &m_data));
  232. }
  233. f -= term;
  234. }
  235. if (e > 0)
  236. detail::check_tommath_result(mp_mul_2d(&m_data, e, &m_data));
  237. else if (e < 0)
  238. {
  239. tommath_int t2;
  240. detail::check_tommath_result(mp_div_2d(&m_data, -e, &m_data, &t2.data()));
  241. }
  242. mp_clear(&t);
  243. return *this;
  244. }
  245. tommath_int& operator=(const char* s)
  246. {
  247. //
  248. // We don't use libtommath's own routine because it doesn't error check the input :-(
  249. //
  250. if (m_data.dp == 0)
  251. detail::check_tommath_result(mp_init(&m_data));
  252. std::size_t n = s ? std::strlen(s) : 0;
  253. *this = static_cast<boost::uint32_t>(0u);
  254. unsigned radix = 10;
  255. bool isneg = false;
  256. if (n && (*s == '-'))
  257. {
  258. --n;
  259. ++s;
  260. isneg = true;
  261. }
  262. if (n && (*s == '0'))
  263. {
  264. if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
  265. {
  266. radix = 16;
  267. s += 2;
  268. n -= 2;
  269. }
  270. else
  271. {
  272. radix = 8;
  273. n -= 1;
  274. }
  275. }
  276. if (n)
  277. {
  278. if (radix == 8 || radix == 16)
  279. {
  280. unsigned shift = radix == 8 ? 3 : 4;
  281. #ifdef DIGIT_BIT
  282. unsigned block_count = DIGIT_BIT / shift;
  283. #else
  284. unsigned block_count = MP_DIGIT_BIT / shift;
  285. #endif
  286. unsigned block_shift = shift * block_count;
  287. boost::ulong_long_type val, block;
  288. while (*s)
  289. {
  290. block = 0;
  291. for (unsigned i = 0; (i < block_count); ++i)
  292. {
  293. if (*s >= '0' && *s <= '9')
  294. val = *s - '0';
  295. else if (*s >= 'a' && *s <= 'f')
  296. val = 10 + *s - 'a';
  297. else if (*s >= 'A' && *s <= 'F')
  298. val = 10 + *s - 'A';
  299. else
  300. val = 400;
  301. if (val > radix)
  302. {
  303. BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
  304. }
  305. block <<= shift;
  306. block |= val;
  307. if (!*++s)
  308. {
  309. // final shift is different:
  310. block_shift = (i + 1) * shift;
  311. break;
  312. }
  313. }
  314. detail::check_tommath_result(mp_mul_2d(&data(), block_shift, &data()));
  315. if (data().used)
  316. data().dp[0] |= block;
  317. else
  318. *this = block;
  319. }
  320. }
  321. else
  322. {
  323. // Base 10, we extract blocks of size 10^9 at a time, that way
  324. // the number of multiplications is kept to a minimum:
  325. boost::uint32_t block_mult = 1000000000;
  326. while (*s)
  327. {
  328. boost::uint32_t block = 0;
  329. for (unsigned i = 0; i < 9; ++i)
  330. {
  331. boost::uint32_t val;
  332. if (*s >= '0' && *s <= '9')
  333. val = *s - '0';
  334. else
  335. BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
  336. block *= 10;
  337. block += val;
  338. if (!*++s)
  339. {
  340. static const boost::uint32_t block_multiplier[9] = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
  341. block_mult = block_multiplier[i];
  342. break;
  343. }
  344. }
  345. tommath_int t;
  346. t = block_mult;
  347. eval_multiply(*this, t);
  348. t = block;
  349. eval_add(*this, t);
  350. }
  351. }
  352. }
  353. if (isneg)
  354. this->negate();
  355. return *this;
  356. }
  357. std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
  358. {
  359. BOOST_ASSERT(m_data.dp);
  360. int base = 10;
  361. if ((f & std::ios_base::oct) == std::ios_base::oct)
  362. base = 8;
  363. else if ((f & std::ios_base::hex) == std::ios_base::hex)
  364. base = 16;
  365. //
  366. // sanity check, bases 8 and 16 are only available for positive numbers:
  367. //
  368. if ((base != 10) && m_data.sign)
  369. BOOST_THROW_EXCEPTION(std::runtime_error("Formatted output in bases 8 or 16 is only available for positive numbers"));
  370. #ifdef DIGIT_BIT
  371. int s;
  372. detail::check_tommath_result(mp_radix_size(const_cast< ::mp_int*>(&m_data), base, &s));
  373. #else
  374. std::size_t s;
  375. detail::check_tommath_result(mp_radix_size(const_cast< ::mp_int*>(&m_data), base, &s));
  376. #endif
  377. boost::scoped_array<char> a(new char[s + 1]);
  378. #ifdef DIGIT_BIT
  379. detail::check_tommath_result(mp_toradix_n(const_cast< ::mp_int*>(&m_data), a.get(), base, s + 1));
  380. #else
  381. std::size_t written;
  382. detail::check_tommath_result(mp_to_radix(&m_data, a.get(), s + 1, &written, base));
  383. #endif
  384. std::string result = a.get();
  385. if (f & std::ios_base::uppercase)
  386. for (size_t i = 0; i < result.length(); ++i)
  387. result[i] = std::toupper(result[i]);
  388. if ((base != 10) && (f & std::ios_base::showbase))
  389. {
  390. int pos = result[0] == '-' ? 1 : 0;
  391. const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
  392. result.insert(static_cast<std::string::size_type>(pos), pp);
  393. }
  394. if ((f & std::ios_base::showpos) && (result[0] != '-'))
  395. result.insert(static_cast<std::string::size_type>(0), 1, '+');
  396. if (((f & std::ios_base::uppercase) == 0) && (base == 16))
  397. {
  398. for (std::size_t i = 0; i < result.size(); ++i)
  399. result[i] = std::tolower(result[i]);
  400. }
  401. return result;
  402. }
  403. ~tommath_int()
  404. {
  405. if (m_data.dp)
  406. mp_clear(&m_data);
  407. }
  408. void negate()
  409. {
  410. BOOST_ASSERT(m_data.dp);
  411. detail::check_tommath_result(mp_neg(&m_data, &m_data));
  412. }
  413. int compare(const tommath_int& o) const
  414. {
  415. BOOST_ASSERT(m_data.dp && o.m_data.dp);
  416. return mp_cmp(const_cast< ::mp_int*>(&m_data), const_cast< ::mp_int*>(&o.m_data));
  417. }
  418. template <class V>
  419. int compare(V v) const
  420. {
  421. tommath_int d;
  422. tommath_int t(*this);
  423. detail::check_tommath_result(mp_shrink(&t.data()));
  424. d = v;
  425. return t.compare(d);
  426. }
  427. ::mp_int& data()
  428. {
  429. BOOST_ASSERT(m_data.dp);
  430. return m_data;
  431. }
  432. const ::mp_int& data() const
  433. {
  434. BOOST_ASSERT(m_data.dp);
  435. return m_data;
  436. }
  437. void swap(tommath_int& o) BOOST_NOEXCEPT
  438. {
  439. mp_exch(&m_data, &o.data());
  440. }
  441. protected:
  442. ::mp_int m_data;
  443. };
  444. #ifdef SIGN
  445. #define BOOST_MP_TOMMATH_BIT_OP_CHECK(x) \
  446. if (SIGN(&x.data())) \
  447. BOOST_THROW_EXCEPTION(std::runtime_error("Bitwise operations on libtommath negative valued integers are disabled as they produce unpredictable results"))
  448. #else
  449. #define BOOST_MP_TOMMATH_BIT_OP_CHECK(x) \
  450. if (mp_isneg(&x.data())) \
  451. BOOST_THROW_EXCEPTION(std::runtime_error("Bitwise operations on libtommath negative valued integers are disabled as they produce unpredictable results"))
  452. #endif
  453. int eval_get_sign(const tommath_int& val);
  454. inline void eval_add(tommath_int& t, const tommath_int& o)
  455. {
  456. detail::check_tommath_result(mp_add(&t.data(), const_cast< ::mp_int*>(&o.data()), &t.data()));
  457. }
  458. inline void eval_subtract(tommath_int& t, const tommath_int& o)
  459. {
  460. detail::check_tommath_result(mp_sub(&t.data(), const_cast< ::mp_int*>(&o.data()), &t.data()));
  461. }
  462. inline void eval_multiply(tommath_int& t, const tommath_int& o)
  463. {
  464. detail::check_tommath_result(mp_mul(&t.data(), const_cast< ::mp_int*>(&o.data()), &t.data()));
  465. }
  466. inline void eval_divide(tommath_int& t, const tommath_int& o)
  467. {
  468. using default_ops::eval_is_zero;
  469. tommath_int temp;
  470. if (eval_is_zero(o))
  471. BOOST_THROW_EXCEPTION(std::overflow_error("Integer division by zero"));
  472. detail::check_tommath_result(mp_div(&t.data(), const_cast< ::mp_int*>(&o.data()), &t.data(), &temp.data()));
  473. }
  474. inline void eval_modulus(tommath_int& t, const tommath_int& o)
  475. {
  476. using default_ops::eval_is_zero;
  477. if (eval_is_zero(o))
  478. BOOST_THROW_EXCEPTION(std::overflow_error("Integer division by zero"));
  479. bool neg = eval_get_sign(t) < 0;
  480. bool neg2 = eval_get_sign(o) < 0;
  481. detail::check_tommath_result(mp_mod(&t.data(), const_cast< ::mp_int*>(&o.data()), &t.data()));
  482. if ((neg != neg2) && (eval_get_sign(t) != 0))
  483. {
  484. t.negate();
  485. detail::check_tommath_result(mp_add(&t.data(), const_cast< ::mp_int*>(&o.data()), &t.data()));
  486. t.negate();
  487. }
  488. else if (neg && (t.compare(o) == 0))
  489. {
  490. mp_zero(&t.data());
  491. }
  492. }
  493. template <class UI>
  494. inline void eval_left_shift(tommath_int& t, UI i)
  495. {
  496. detail::check_tommath_result(mp_mul_2d(&t.data(), static_cast<unsigned>(i), &t.data()));
  497. }
  498. template <class UI>
  499. inline void eval_right_shift(tommath_int& t, UI i)
  500. {
  501. using default_ops::eval_decrement;
  502. using default_ops::eval_increment;
  503. bool neg = eval_get_sign(t) < 0;
  504. tommath_int d;
  505. if (neg)
  506. eval_increment(t);
  507. detail::check_tommath_result(mp_div_2d(&t.data(), static_cast<unsigned>(i), &t.data(), &d.data()));
  508. if (neg)
  509. eval_decrement(t);
  510. }
  511. template <class UI>
  512. inline void eval_left_shift(tommath_int& t, const tommath_int& v, UI i)
  513. {
  514. detail::check_tommath_result(mp_mul_2d(const_cast< ::mp_int*>(&v.data()), static_cast<unsigned>(i), &t.data()));
  515. }
  516. /*
  517. template <class UI>
  518. inline void eval_right_shift(tommath_int& t, const tommath_int& v, UI i)
  519. {
  520. tommath_int d;
  521. detail::check_tommath_result(mp_div_2d(const_cast< ::mp_int*>(&v.data()), static_cast<unsigned long>(i), &t.data(), &d.data()));
  522. }
  523. */
  524. inline void eval_bitwise_and(tommath_int& result, const tommath_int& v)
  525. {
  526. BOOST_MP_TOMMATH_BIT_OP_CHECK(result);
  527. BOOST_MP_TOMMATH_BIT_OP_CHECK(v);
  528. detail::check_tommath_result(mp_and(&result.data(), const_cast< ::mp_int*>(&v.data()), &result.data()));
  529. }
  530. inline void eval_bitwise_or(tommath_int& result, const tommath_int& v)
  531. {
  532. BOOST_MP_TOMMATH_BIT_OP_CHECK(result);
  533. BOOST_MP_TOMMATH_BIT_OP_CHECK(v);
  534. detail::check_tommath_result(mp_or(&result.data(), const_cast< ::mp_int*>(&v.data()), &result.data()));
  535. }
  536. inline void eval_bitwise_xor(tommath_int& result, const tommath_int& v)
  537. {
  538. BOOST_MP_TOMMATH_BIT_OP_CHECK(result);
  539. BOOST_MP_TOMMATH_BIT_OP_CHECK(v);
  540. detail::check_tommath_result(mp_xor(&result.data(), const_cast< ::mp_int*>(&v.data()), &result.data()));
  541. }
  542. inline void eval_add(tommath_int& t, const tommath_int& p, const tommath_int& o)
  543. {
  544. detail::check_tommath_result(mp_add(const_cast< ::mp_int*>(&p.data()), const_cast< ::mp_int*>(&o.data()), &t.data()));
  545. }
  546. inline void eval_subtract(tommath_int& t, const tommath_int& p, const tommath_int& o)
  547. {
  548. detail::check_tommath_result(mp_sub(const_cast< ::mp_int*>(&p.data()), const_cast< ::mp_int*>(&o.data()), &t.data()));
  549. }
  550. inline void eval_multiply(tommath_int& t, const tommath_int& p, const tommath_int& o)
  551. {
  552. detail::check_tommath_result(mp_mul(const_cast< ::mp_int*>(&p.data()), const_cast< ::mp_int*>(&o.data()), &t.data()));
  553. }
  554. inline void eval_divide(tommath_int& t, const tommath_int& p, const tommath_int& o)
  555. {
  556. using default_ops::eval_is_zero;
  557. tommath_int d;
  558. if (eval_is_zero(o))
  559. BOOST_THROW_EXCEPTION(std::overflow_error("Integer division by zero"));
  560. detail::check_tommath_result(mp_div(const_cast< ::mp_int*>(&p.data()), const_cast< ::mp_int*>(&o.data()), &t.data(), &d.data()));
  561. }
  562. inline void eval_modulus(tommath_int& t, const tommath_int& p, const tommath_int& o)
  563. {
  564. using default_ops::eval_is_zero;
  565. if (eval_is_zero(o))
  566. BOOST_THROW_EXCEPTION(std::overflow_error("Integer division by zero"));
  567. bool neg = eval_get_sign(p) < 0;
  568. bool neg2 = eval_get_sign(o) < 0;
  569. detail::check_tommath_result(mp_mod(const_cast< ::mp_int*>(&p.data()), const_cast< ::mp_int*>(&o.data()), &t.data()));
  570. if ((neg != neg2) && (eval_get_sign(t) != 0))
  571. {
  572. t.negate();
  573. detail::check_tommath_result(mp_add(&t.data(), const_cast< ::mp_int*>(&o.data()), &t.data()));
  574. t.negate();
  575. }
  576. else if (neg && (t.compare(o) == 0))
  577. {
  578. mp_zero(&t.data());
  579. }
  580. }
  581. inline void eval_bitwise_and(tommath_int& result, const tommath_int& u, const tommath_int& v)
  582. {
  583. BOOST_MP_TOMMATH_BIT_OP_CHECK(u);
  584. BOOST_MP_TOMMATH_BIT_OP_CHECK(v);
  585. detail::check_tommath_result(mp_and(const_cast< ::mp_int*>(&u.data()), const_cast< ::mp_int*>(&v.data()), &result.data()));
  586. }
  587. inline void eval_bitwise_or(tommath_int& result, const tommath_int& u, const tommath_int& v)
  588. {
  589. BOOST_MP_TOMMATH_BIT_OP_CHECK(u);
  590. BOOST_MP_TOMMATH_BIT_OP_CHECK(v);
  591. detail::check_tommath_result(mp_or(const_cast< ::mp_int*>(&u.data()), const_cast< ::mp_int*>(&v.data()), &result.data()));
  592. }
  593. inline void eval_bitwise_xor(tommath_int& result, const tommath_int& u, const tommath_int& v)
  594. {
  595. BOOST_MP_TOMMATH_BIT_OP_CHECK(u);
  596. BOOST_MP_TOMMATH_BIT_OP_CHECK(v);
  597. detail::check_tommath_result(mp_xor(const_cast< ::mp_int*>(&u.data()), const_cast< ::mp_int*>(&v.data()), &result.data()));
  598. }
  599. /*
  600. inline void eval_complement(tommath_int& result, const tommath_int& u)
  601. {
  602. //
  603. // Although this code works, it doesn't really do what the user might expect....
  604. // and it's hard to see how it ever could. Disabled for now:
  605. //
  606. result = u;
  607. for(int i = 0; i < result.data().used; ++i)
  608. {
  609. result.data().dp[i] = MP_MASK & ~(result.data().dp[i]);
  610. }
  611. //
  612. // We now need to pad out the left of the value with 1's to round up to a whole number of
  613. // CHAR_BIT * sizeof(mp_digit) units. Otherwise we'll end up with a very strange number of
  614. // bits set!
  615. //
  616. unsigned shift = result.data().used * DIGIT_BIT; // How many bits we're actually using
  617. // How many bits we actually need, reduced by one to account for a mythical sign bit:
  618. int padding = result.data().used * std::numeric_limits<mp_digit>::digits - shift - 1;
  619. while(padding >= std::numeric_limits<mp_digit>::digits)
  620. padding -= std::numeric_limits<mp_digit>::digits;
  621. // Create a mask providing the extra bits we need and add to result:
  622. tommath_int mask;
  623. mask = static_cast<boost::long_long_type>((1u << padding) - 1);
  624. eval_left_shift(mask, shift);
  625. add(result, mask);
  626. }
  627. */
  628. inline bool eval_is_zero(const tommath_int& val)
  629. {
  630. return mp_iszero(&val.data());
  631. }
  632. inline int eval_get_sign(const tommath_int& val)
  633. {
  634. #ifdef SIGN
  635. return mp_iszero(&val.data()) ? 0 : SIGN(&val.data()) ? -1 : 1;
  636. #else
  637. return mp_iszero(&val.data()) ? 0 : mp_isneg(&val.data()) ? -1 : 1;
  638. #endif
  639. }
  640. /*
  641. template <class A>
  642. inline void eval_convert_to(A* result, const tommath_int& val)
  643. {
  644. *result = boost::lexical_cast<A>(val.str(0, std::ios_base::fmtflags(0)));
  645. }
  646. inline void eval_convert_to(char* result, const tommath_int& val)
  647. {
  648. *result = static_cast<char>(boost::lexical_cast<int>(val.str(0, std::ios_base::fmtflags(0))));
  649. }
  650. inline void eval_convert_to(unsigned char* result, const tommath_int& val)
  651. {
  652. *result = static_cast<unsigned char>(boost::lexical_cast<unsigned>(val.str(0, std::ios_base::fmtflags(0))));
  653. }
  654. inline void eval_convert_to(signed char* result, const tommath_int& val)
  655. {
  656. *result = static_cast<signed char>(boost::lexical_cast<int>(val.str(0, std::ios_base::fmtflags(0))));
  657. }
  658. */
  659. inline void eval_abs(tommath_int& result, const tommath_int& val)
  660. {
  661. detail::check_tommath_result(mp_abs(const_cast< ::mp_int*>(&val.data()), &result.data()));
  662. }
  663. inline void eval_gcd(tommath_int& result, const tommath_int& a, const tommath_int& b)
  664. {
  665. detail::check_tommath_result(mp_gcd(const_cast< ::mp_int*>(&a.data()), const_cast< ::mp_int*>(&b.data()), const_cast< ::mp_int*>(&result.data())));
  666. }
  667. inline void eval_lcm(tommath_int& result, const tommath_int& a, const tommath_int& b)
  668. {
  669. detail::check_tommath_result(mp_lcm(const_cast< ::mp_int*>(&a.data()), const_cast< ::mp_int*>(&b.data()), const_cast< ::mp_int*>(&result.data())));
  670. }
  671. inline void eval_powm(tommath_int& result, const tommath_int& base, const tommath_int& p, const tommath_int& m)
  672. {
  673. if (eval_get_sign(p) < 0)
  674. {
  675. BOOST_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
  676. }
  677. detail::check_tommath_result(mp_exptmod(const_cast< ::mp_int*>(&base.data()), const_cast< ::mp_int*>(&p.data()), const_cast< ::mp_int*>(&m.data()), &result.data()));
  678. }
  679. inline void eval_qr(const tommath_int& x, const tommath_int& y,
  680. tommath_int& q, tommath_int& r)
  681. {
  682. detail::check_tommath_result(mp_div(const_cast< ::mp_int*>(&x.data()), const_cast< ::mp_int*>(&y.data()), &q.data(), &r.data()));
  683. }
  684. inline unsigned eval_lsb(const tommath_int& val)
  685. {
  686. int c = eval_get_sign(val);
  687. if (c == 0)
  688. {
  689. BOOST_THROW_EXCEPTION(std::range_error("No bits were set in the operand."));
  690. }
  691. if (c < 0)
  692. {
  693. BOOST_THROW_EXCEPTION(std::range_error("Testing individual bits in negative values is not supported - results are undefined."));
  694. }
  695. return mp_cnt_lsb(const_cast< ::mp_int*>(&val.data()));
  696. }
  697. inline unsigned eval_msb(const tommath_int& val)
  698. {
  699. int c = eval_get_sign(val);
  700. if (c == 0)
  701. {
  702. BOOST_THROW_EXCEPTION(std::range_error("No bits were set in the operand."));
  703. }
  704. if (c < 0)
  705. {
  706. BOOST_THROW_EXCEPTION(std::range_error("Testing individual bits in negative values is not supported - results are undefined."));
  707. }
  708. return mp_count_bits(const_cast< ::mp_int*>(&val.data())) - 1;
  709. }
  710. template <class Integer>
  711. inline typename enable_if<is_unsigned<Integer>, Integer>::type eval_integer_modulus(const tommath_int& x, Integer val)
  712. {
  713. #ifdef DIGIT_BIT
  714. static const mp_digit m = (static_cast<mp_digit>(1) << DIGIT_BIT) - 1;
  715. #else
  716. static const mp_digit m = (static_cast<mp_digit>(1) << MP_DIGIT_BIT) - 1;
  717. #endif
  718. if (val <= m)
  719. {
  720. mp_digit d;
  721. detail::check_tommath_result(mp_mod_d(const_cast< ::mp_int*>(&x.data()), static_cast<mp_digit>(val), &d));
  722. return d;
  723. }
  724. else
  725. {
  726. return default_ops::eval_integer_modulus(x, val);
  727. }
  728. }
  729. template <class Integer>
  730. inline typename enable_if<is_signed<Integer>, Integer>::type eval_integer_modulus(const tommath_int& x, Integer val)
  731. {
  732. return eval_integer_modulus(x, boost::multiprecision::detail::unsigned_abs(val));
  733. }
  734. inline std::size_t hash_value(const tommath_int& val)
  735. {
  736. std::size_t result = 0;
  737. std::size_t len = val.data().used;
  738. for (std::size_t i = 0; i < len; ++i)
  739. boost::hash_combine(result, val.data().dp[i]);
  740. boost::hash_combine(result, val.data().sign);
  741. return result;
  742. }
  743. } // namespace backends
  744. using boost::multiprecision::backends::tommath_int;
  745. template <>
  746. struct number_category<tommath_int> : public mpl::int_<number_kind_integer>
  747. {};
  748. typedef number<tommath_int> tom_int;
  749. typedef rational_adaptor<tommath_int> tommath_rational;
  750. typedef number<tommath_rational> tom_rational;
  751. }
  752. } // namespace boost::multiprecision
  753. namespace std {
  754. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  755. class numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >
  756. {
  757. typedef boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> number_type;
  758. public:
  759. BOOST_STATIC_CONSTEXPR bool is_specialized = true;
  760. //
  761. // Largest and smallest numbers are bounded only by available memory, set
  762. // to zero:
  763. //
  764. static number_type(min)()
  765. {
  766. return number_type();
  767. }
  768. static number_type(max)()
  769. {
  770. return number_type();
  771. }
  772. static number_type lowest() { return (min)(); }
  773. BOOST_STATIC_CONSTEXPR int digits = INT_MAX;
  774. BOOST_STATIC_CONSTEXPR int digits10 = (INT_MAX / 1000) * 301L;
  775. BOOST_STATIC_CONSTEXPR int max_digits10 = digits10 + 3;
  776. BOOST_STATIC_CONSTEXPR bool is_signed = true;
  777. BOOST_STATIC_CONSTEXPR bool is_integer = true;
  778. BOOST_STATIC_CONSTEXPR bool is_exact = true;
  779. BOOST_STATIC_CONSTEXPR int radix = 2;
  780. static number_type epsilon() { return number_type(); }
  781. static number_type round_error() { return number_type(); }
  782. BOOST_STATIC_CONSTEXPR int min_exponent = 0;
  783. BOOST_STATIC_CONSTEXPR int min_exponent10 = 0;
  784. BOOST_STATIC_CONSTEXPR int max_exponent = 0;
  785. BOOST_STATIC_CONSTEXPR int max_exponent10 = 0;
  786. BOOST_STATIC_CONSTEXPR bool has_infinity = false;
  787. BOOST_STATIC_CONSTEXPR bool has_quiet_NaN = false;
  788. BOOST_STATIC_CONSTEXPR bool has_signaling_NaN = false;
  789. BOOST_STATIC_CONSTEXPR float_denorm_style has_denorm = denorm_absent;
  790. BOOST_STATIC_CONSTEXPR bool has_denorm_loss = false;
  791. static number_type infinity() { return number_type(); }
  792. static number_type quiet_NaN() { return number_type(); }
  793. static number_type signaling_NaN() { return number_type(); }
  794. static number_type denorm_min() { return number_type(); }
  795. BOOST_STATIC_CONSTEXPR bool is_iec559 = false;
  796. BOOST_STATIC_CONSTEXPR bool is_bounded = false;
  797. BOOST_STATIC_CONSTEXPR bool is_modulo = false;
  798. BOOST_STATIC_CONSTEXPR bool traps = false;
  799. BOOST_STATIC_CONSTEXPR bool tinyness_before = false;
  800. BOOST_STATIC_CONSTEXPR float_round_style round_style = round_toward_zero;
  801. };
  802. #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  803. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  804. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::digits;
  805. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  806. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::digits10;
  807. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  808. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::max_digits10;
  809. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  810. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_signed;
  811. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  812. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_integer;
  813. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  814. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_exact;
  815. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  816. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::radix;
  817. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  818. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::min_exponent;
  819. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  820. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::min_exponent10;
  821. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  822. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::max_exponent;
  823. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  824. BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::max_exponent10;
  825. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  826. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_infinity;
  827. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  828. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_quiet_NaN;
  829. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  830. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_signaling_NaN;
  831. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  832. BOOST_CONSTEXPR_OR_CONST float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_denorm;
  833. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  834. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_denorm_loss;
  835. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  836. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_iec559;
  837. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  838. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_bounded;
  839. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  840. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_modulo;
  841. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  842. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::traps;
  843. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  844. BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::tinyness_before;
  845. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  846. BOOST_CONSTEXPR_OR_CONST float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::round_style;
  847. #endif
  848. } // namespace std
  849. #endif