tommath.hpp 33 KB

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