annotate DEPENDENCIES/generic/include/boost/multiprecision/tommath.hpp @ 125:34e428693f5d vext

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