tommath.hpp 29 KB

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