tommath.hpp 29 KB

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