annotate DEPENDENCIES/generic/include/boost/multiprecision/tommath.hpp @ 16:2665513ce2d3

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