Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / multiprecision / tommath.hpp
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>
17 #include <cctype>
18 #include <cmath>
19 #include <limits>
20 #include <climits>
21
22 namespace boost { namespace multiprecision {
23 namespace backends {
24
25 namespace detail {
26
27 inline void check_tommath_result(unsigned v)
28 {
29    if (v != MP_OKAY)
30    {
31       BOOST_THROW_EXCEPTION(std::runtime_error(mp_error_to_string(v)));
32    }
33 }
34
35 } // namespace detail
36
37 struct tommath_int;
38
39 void eval_multiply(tommath_int& t, const tommath_int& o);
40 void eval_add(tommath_int& t, const tommath_int& o);
41
42 struct tommath_int
43 {
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;
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    {
59       m_data      = o.m_data;
60       o.m_data.dp = 0;
61    }
62    tommath_int& operator=(tommath_int&& o)
63    {
64       mp_exch(&m_data, &o.m_data);
65       return *this;
66    }
67 #endif
68    tommath_int& operator=(const tommath_int& o)
69    {
70       if (m_data.dp == 0)
71          detail::check_tommath_result(mp_init(&m_data));
72       if (o.m_data.dp)
73          detail::check_tommath_result(mp_copy(const_cast< ::mp_int*>(&o.m_data), &m_data));
74       return *this;
75    }
76    tommath_int& operator=(boost::ulong_long_type i)
77    {
78       if (m_data.dp == 0)
79          detail::check_tommath_result(mp_init(&m_data));
80       boost::ulong_long_type mask  = ((1uLL << std::numeric_limits<unsigned>::digits) - 1);
81       unsigned               shift = 0;
82       ::mp_int               t;
83       detail::check_tommath_result(mp_init(&t));
84       mp_zero(&m_data);
85       while (i)
86       {
87          detail::check_tommath_result(mp_set_int(&t, static_cast<unsigned>(i & mask)));
88          if (shift)
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    }
97    tommath_int& operator=(boost::long_long_type i)
98    {
99       if (m_data.dp == 0)
100          detail::check_tommath_result(mp_init(&m_data));
101       bool neg = i < 0;
102       *this    = boost::multiprecision::detail::unsigned_abs(i);
103       if (neg)
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    //
112    tommath_int& operator=(boost::uint32_t i)
113    {
114       if (m_data.dp == 0)
115          detail::check_tommath_result(mp_init(&m_data));
116       detail::check_tommath_result((mp_set_int(&m_data, i)));
117       return *this;
118    }
119    tommath_int& operator=(boost::int32_t i)
120    {
121       if (m_data.dp == 0)
122          detail::check_tommath_result(mp_init(&m_data));
123       bool neg = i < 0;
124       *this    = boost::multiprecision::detail::unsigned_abs(i);
125       if (neg)
126          detail::check_tommath_result(mp_neg(&m_data, &m_data));
127       return *this;
128    }
129    tommath_int& operator=(long double a)
130    {
131       using std::floor;
132       using std::frexp;
133       using std::ldexp;
134
135       if (m_data.dp == 0)
136          detail::check_tommath_result(mp_init(&m_data));
137
138       if (a == 0)
139       {
140          detail::check_tommath_result(mp_set_int(&m_data, 0));
141          return *this;
142       }
143
144       if (a == 1)
145       {
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
153       int         e;
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
163       while (f)
164       {
165          // extract int sized bits from f:
166          f    = ldexp(f, shift);
167          term = floor(f);
168          e -= shift;
169          detail::check_tommath_result(mp_mul_2d(&m_data, shift, &m_data));
170          if (term > 0)
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       }
182       if (e > 0)
183          detail::check_tommath_result(mp_mul_2d(&m_data, e, &m_data));
184       else if (e < 0)
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    }
192    tommath_int& operator=(const char* s)
193    {
194       //
195       // We don't use libtommath's own routine because it doesn't error check the input :-(
196       //
197       if (m_data.dp == 0)
198          detail::check_tommath_result(mp_init(&m_data));
199       std::size_t n  = s ? std::strlen(s) : 0;
200       *this          = static_cast<boost::uint32_t>(0u);
201       unsigned radix = 10;
202       bool     isneg = false;
203       if (n && (*s == '-'))
204       {
205          --n;
206          ++s;
207          isneg = true;
208       }
209       if (n && (*s == '0'))
210       {
211          if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
212          {
213             radix = 16;
214             s += 2;
215             n -= 2;
216          }
217          else
218          {
219             radix = 8;
220             n -= 1;
221          }
222       }
223       if (n)
224       {
225          if (radix == 8 || radix == 16)
226          {
227             unsigned               shift       = radix == 8 ? 3 : 4;
228             unsigned               block_count = DIGIT_BIT / shift;
229             unsigned               block_shift = shift * block_count;
230             boost::ulong_long_type val, block;
231             while (*s)
232             {
233                block = 0;
234                for (unsigned i = 0; (i < block_count); ++i)
235                {
236                   if (*s >= '0' && *s <= '9')
237                      val = *s - '0';
238                   else if (*s >= 'a' && *s <= 'f')
239                      val = 10 + *s - 'a';
240                   else if (*s >= 'A' && *s <= 'F')
241                      val = 10 + *s - 'A';
242                   else
243                      val = 400;
244                   if (val > radix)
245                   {
246                      BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
247                   }
248                   block <<= shift;
249                   block |= val;
250                   if (!*++s)
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()));
258                if (data().used)
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;
269             while (*s)
270             {
271                boost::uint32_t block = 0;
272                for (unsigned i = 0; i < 9; ++i)
273                {
274                   boost::uint32_t val;
275                   if (*s >= '0' && *s <= '9')
276                      val = *s - '0';
277                   else
278                      BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
279                   block *= 10;
280                   block += val;
281                   if (!*++s)
282                   {
283                      static const boost::uint32_t block_multiplier[9] = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
284                      block_mult                                       = block_multiplier[i];
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       }
296       if (isneg)
297          this->negate();
298       return *this;
299    }
300    std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
301    {
302       BOOST_ASSERT(m_data.dp);
303       int base = 10;
304       if ((f & std::ios_base::oct) == std::ios_base::oct)
305          base = 8;
306       else if ((f & std::ios_base::hex) == std::ios_base::hex)
307          base = 16;
308       //
309       // sanity check, bases 8 and 16 are only available for positive numbers:
310       //
311       if ((base != 10) && m_data.sign)
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));
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));
317       std::string result = a.get();
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))
322       {
323          int         pos = result[0] == '-' ? 1 : 0;
324          const char* pp  = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
325          result.insert(static_cast<std::string::size_type>(pos), pp);
326       }
327       if ((f & std::ios_base::showpos) && (result[0] != '-'))
328          result.insert(static_cast<std::string::size_type>(0), 1, '+');
329       return result;
330    }
331    ~tommath_int()
332    {
333       if (m_data.dp)
334          mp_clear(&m_data);
335    }
336    void negate()
337    {
338       BOOST_ASSERT(m_data.dp);
339       mp_neg(&m_data, &m_data);
340    }
341    int compare(const tommath_int& o) const
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>
347    int compare(V v) const
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    }
355    ::mp_int& data()
356    {
357       BOOST_ASSERT(m_data.dp);
358       return m_data;
359    }
360    const ::mp_int& data() const
361    {
362       BOOST_ASSERT(m_data.dp);
363       return m_data;
364    }
365    void swap(tommath_int& o) BOOST_NOEXCEPT
366    {
367       mp_exch(&m_data, &o.data());
368    }
369
370  protected:
371    ::mp_int m_data;
372 };
373
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"))
377
378 int eval_get_sign(const tommath_int& val);
379
380 inline 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 }
384 inline 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 }
388 inline 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 }
392 inline void eval_divide(tommath_int& t, const tommath_int& o)
393 {
394    using default_ops::eval_is_zero;
395    tommath_int temp;
396    if (eval_is_zero(o))
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 }
400 inline void eval_modulus(tommath_int& t, const tommath_int& o)
401 {
402    using default_ops::eval_is_zero;
403    if (eval_is_zero(o))
404       BOOST_THROW_EXCEPTION(std::overflow_error("Integer division by zero"));
405    bool neg  = eval_get_sign(t) < 0;
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()));
408    if ((neg != neg2) && (eval_get_sign(t) != 0))
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    }
414    else if (neg && (t.compare(o) == 0))
415    {
416       mp_zero(&t.data());
417    }
418 }
419 template <class UI>
420 inline 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 }
424 template <class UI>
425 inline void eval_right_shift(tommath_int& t, UI i)
426 {
427    using default_ops::eval_decrement;
428    using default_ops::eval_increment;
429    bool        neg = eval_get_sign(t) < 0;
430    tommath_int d;
431    if (neg)
432       eval_increment(t);
433    detail::check_tommath_result(mp_div_2d(&t.data(), static_cast<unsigned>(i), &t.data(), &d.data()));
434    if (neg)
435       eval_decrement(t);
436 }
437 template <class UI>
438 inline 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 /*
443 template <class UI>
444 inline 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 */
450 inline 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
457 inline 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
464 inline 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
471 inline 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 }
475 inline 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 }
479 inline 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 }
483 inline 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;
487    if (eval_is_zero(o))
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 }
491 inline void eval_modulus(tommath_int& t, const tommath_int& p, const tommath_int& o)
492 {
493    using default_ops::eval_is_zero;
494    if (eval_is_zero(o))
495       BOOST_THROW_EXCEPTION(std::overflow_error("Integer division by zero"));
496    bool neg  = eval_get_sign(p) < 0;
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()));
499    if ((neg != neg2) && (eval_get_sign(t) != 0))
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    }
505    else if (neg && (t.compare(o) == 0))
506    {
507       mp_zero(&t.data());
508    }
509 }
510
511 inline 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
518 inline 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
525 inline 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 /*
532 inline 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 */
561 inline bool eval_is_zero(const tommath_int& val)
562 {
563    return mp_iszero(&val.data());
564 }
565 inline int eval_get_sign(const tommath_int& val)
566 {
567    return mp_iszero(&val.data()) ? 0 : SIGN(&val.data()) ? -1 : 1;
568 }
569 /*
570 template <class A>
571 inline 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 }
575 inline 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 }
579 inline 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 }
583 inline 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 }
587 */
588 inline 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 }
592 inline 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 }
596 inline 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 }
600 inline void eval_powm(tommath_int& result, const tommath_int& base, const tommath_int& p, const tommath_int& m)
601 {
602    if (eval_get_sign(p) < 0)
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
609 inline void eval_qr(const tommath_int& x, const tommath_int& y,
610                     tommath_int& q, tommath_int& r)
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
615 inline unsigned eval_lsb(const tommath_int& val)
616 {
617    int c = eval_get_sign(val);
618    if (c == 0)
619    {
620       BOOST_THROW_EXCEPTION(std::range_error("No bits were set in the operand."));
621    }
622    if (c < 0)
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
629 inline unsigned eval_msb(const tommath_int& val)
630 {
631    int c = eval_get_sign(val);
632    if (c == 0)
633    {
634       BOOST_THROW_EXCEPTION(std::range_error("No bits were set in the operand."));
635    }
636    if (c < 0)
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
643 template <class Integer>
644 inline 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;
647    if (val <= m)
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 }
658 template <class Integer>
659 inline 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
664 inline std::size_t hash_value(const tommath_int& val)
665 {
666    std::size_t result = 0;
667    std::size_t len    = val.data().used;
668    for (std::size_t i = 0; i < len; ++i)
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
676 using boost::multiprecision::backends::tommath_int;
677
678 template <>
679 struct number_category<tommath_int> : public mpl::int_<number_kind_integer>
680 {};
681
682 typedef number<tommath_int>           tom_int;
683 typedef rational_adaptor<tommath_int> tommath_rational;
684 typedef number<tommath_rational>      tom_rational;
685
686 }} // namespace boost::multiprecision
687
688 namespace std {
689
690 template <boost::multiprecision::expression_template_option ExpressionTemplates>
691 class numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >
692 {
693    typedef boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> number_type;
694
695  public:
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    //
701    static number_type(min)()
702    {
703       return number_type();
704    }
705    static number_type(max)()
706    {
707       return number_type();
708    }
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;
738 };
739
740 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
741
742 template <boost::multiprecision::expression_template_option ExpressionTemplates>
743 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::digits;
744 template <boost::multiprecision::expression_template_option ExpressionTemplates>
745 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::digits10;
746 template <boost::multiprecision::expression_template_option ExpressionTemplates>
747 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::max_digits10;
748 template <boost::multiprecision::expression_template_option ExpressionTemplates>
749 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_signed;
750 template <boost::multiprecision::expression_template_option ExpressionTemplates>
751 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_integer;
752 template <boost::multiprecision::expression_template_option ExpressionTemplates>
753 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_exact;
754 template <boost::multiprecision::expression_template_option ExpressionTemplates>
755 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::radix;
756 template <boost::multiprecision::expression_template_option ExpressionTemplates>
757 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::min_exponent;
758 template <boost::multiprecision::expression_template_option ExpressionTemplates>
759 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::min_exponent10;
760 template <boost::multiprecision::expression_template_option ExpressionTemplates>
761 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::max_exponent;
762 template <boost::multiprecision::expression_template_option ExpressionTemplates>
763 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::max_exponent10;
764 template <boost::multiprecision::expression_template_option ExpressionTemplates>
765 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_infinity;
766 template <boost::multiprecision::expression_template_option ExpressionTemplates>
767 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_quiet_NaN;
768 template <boost::multiprecision::expression_template_option ExpressionTemplates>
769 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_signaling_NaN;
770 template <boost::multiprecision::expression_template_option ExpressionTemplates>
771 BOOST_CONSTEXPR_OR_CONST float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_denorm;
772 template <boost::multiprecision::expression_template_option ExpressionTemplates>
773 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::has_denorm_loss;
774 template <boost::multiprecision::expression_template_option ExpressionTemplates>
775 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_iec559;
776 template <boost::multiprecision::expression_template_option ExpressionTemplates>
777 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_bounded;
778 template <boost::multiprecision::expression_template_option ExpressionTemplates>
779 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::is_modulo;
780 template <boost::multiprecision::expression_template_option ExpressionTemplates>
781 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::traps;
782 template <boost::multiprecision::expression_template_option ExpressionTemplates>
783 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::tinyness_before;
784 template <boost::multiprecision::expression_template_option ExpressionTemplates>
785 BOOST_CONSTEXPR_OR_CONST float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::tommath_int, ExpressionTemplates> >::round_style;
786
787 #endif
788 } // namespace std
789
790 #endif