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