Initial Commit

This commit is contained in:
Yousef Khadadeh 2025-07-06 20:43:53 +03:00
commit 6088fc29ed
978 changed files with 353647 additions and 0 deletions

View file

@ -0,0 +1,24 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2018 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_COMPLEX128_HPP
#define BOOST_MP_COMPLEX128_HPP
#include <boost/multiprecision/float128.hpp>
#include <boost/multiprecision/complex_adaptor.hpp>
namespace boost {
namespace multiprecision {
template <>
struct component_type<number<complex_adaptor<float128_backend> > >
{
using type = float128;
};
}
} // namespace boost::multiprecision
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,257 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_MP_NUMBER_ARCHETYPES_HPP
#define BOOST_MP_MP_NUMBER_ARCHETYPES_HPP
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <tuple>
#include <functional>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#include <boost/multiprecision/detail/fpclassify.hpp>
namespace boost {
namespace multiprecision {
namespace concepts {
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4244)
#endif
struct number_backend_float_architype
{
using signed_types = std::tuple<long long> ;
using unsigned_types = std::tuple<unsigned long long>;
using float_types = std::tuple<long double>;
using exponent_type = int;
number_backend_float_architype()
{
m_value = 0;
std::cout << "Default construct" << std::endl;
}
number_backend_float_architype(const number_backend_float_architype& o)
{
std::cout << "Copy construct" << std::endl;
m_value = o.m_value;
}
number_backend_float_architype& operator=(const number_backend_float_architype& o)
{
m_value = o.m_value;
std::cout << "Assignment (" << m_value << ")" << std::endl;
return *this;
}
number_backend_float_architype& operator=(unsigned long long i)
{
m_value = i;
std::cout << "UInt Assignment (" << i << ")" << std::endl;
return *this;
}
number_backend_float_architype& operator=(long long i)
{
m_value = i;
std::cout << "Int Assignment (" << i << ")" << std::endl;
return *this;
}
number_backend_float_architype& operator=(long double d)
{
m_value = d;
std::cout << "long double Assignment (" << d << ")" << std::endl;
return *this;
}
number_backend_float_architype& operator=(const char* s)
{
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
#ifndef BOOST_MP_STANDALONE
m_value = boost::lexical_cast<long double>(s);
#else
m_value = std::strtold(s, nullptr);
if(m_value == HUGE_VALL || m_value == 0)
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error("Value can not be assigned in standalone mode. Please disable and try again."));
}
#endif
#ifndef BOOST_NO_EXCEPTIONS
}
catch (const std::exception&)
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error(std::string("Unable to parse input string: \"") + s + std::string("\" as a valid floating point number.")));
}
#endif
std::cout << "const char* Assignment (" << s << ")" << std::endl;
return *this;
}
void swap(number_backend_float_architype& o)
{
std::cout << "Swapping (" << m_value << " with " << o.m_value << ")" << std::endl;
std::swap(m_value, o.m_value);
}
std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
{
std::stringstream ss;
ss.flags(f);
if (digits)
ss.precision(digits);
else
ss.precision(std::numeric_limits<long double>::digits10 + 3);
std::intmax_t i = m_value;
std::uintmax_t u = m_value;
if (!(f & std::ios_base::scientific) && m_value == i)
ss << i;
else if (!(f & std::ios_base::scientific) && m_value == u)
ss << u;
else
ss << m_value;
std::string s = ss.str();
std::cout << "Converting to string (" << s << ")" << std::endl;
return s;
}
void negate()
{
std::cout << "Negating (" << m_value << ")" << std::endl;
m_value = -m_value;
}
int compare(const number_backend_float_architype& o) const
{
std::cout << "Comparison" << std::endl;
return m_value > o.m_value ? 1 : (m_value < o.m_value ? -1 : 0);
}
int compare(long long i) const
{
std::cout << "Comparison with int" << std::endl;
return m_value > i ? 1 : (m_value < i ? -1 : 0);
}
int compare(unsigned long long i) const
{
std::cout << "Comparison with unsigned" << std::endl;
return m_value > i ? 1 : (m_value < i ? -1 : 0);
}
int compare(long double d) const
{
std::cout << "Comparison with long double" << std::endl;
return m_value > d ? 1 : (m_value < d ? -1 : 0);
}
long double m_value;
};
inline void eval_add(number_backend_float_architype& result, const number_backend_float_architype& o)
{
std::cout << "Addition (" << result.m_value << " += " << o.m_value << ")" << std::endl;
result.m_value += o.m_value;
}
inline void eval_subtract(number_backend_float_architype& result, const number_backend_float_architype& o)
{
std::cout << "Subtraction (" << result.m_value << " -= " << o.m_value << ")" << std::endl;
result.m_value -= o.m_value;
}
inline void eval_multiply(number_backend_float_architype& result, const number_backend_float_architype& o)
{
std::cout << "Multiplication (" << result.m_value << " *= " << o.m_value << ")" << std::endl;
result.m_value *= o.m_value;
}
inline void eval_divide(number_backend_float_architype& result, const number_backend_float_architype& o)
{
std::cout << "Division (" << result.m_value << " /= " << o.m_value << ")" << std::endl;
result.m_value /= o.m_value;
}
inline void eval_convert_to(unsigned long long* result, const number_backend_float_architype& val)
{
*result = static_cast<unsigned long long>(val.m_value);
}
inline void eval_convert_to(long long* result, const number_backend_float_architype& val)
{
*result = static_cast<long long>(val.m_value);
}
inline void eval_convert_to(long double* result, number_backend_float_architype& val)
{
*result = val.m_value;
}
inline void eval_frexp(number_backend_float_architype& result, const number_backend_float_architype& arg, int* exp)
{
result = std::frexp(arg.m_value, exp);
}
inline void eval_ldexp(number_backend_float_architype& result, const number_backend_float_architype& arg, int exp)
{
result = std::ldexp(arg.m_value, exp);
}
inline void eval_floor(number_backend_float_architype& result, const number_backend_float_architype& arg)
{
result = std::floor(arg.m_value);
}
inline void eval_ceil(number_backend_float_architype& result, const number_backend_float_architype& arg)
{
result = std::ceil(arg.m_value);
}
inline void eval_sqrt(number_backend_float_architype& result, const number_backend_float_architype& arg)
{
result = std::sqrt(arg.m_value);
}
inline int eval_fpclassify(const number_backend_float_architype& arg)
{
return BOOST_MP_FPCLASSIFY(arg.m_value);
}
inline std::size_t hash_value(const number_backend_float_architype& v)
{
std::hash<long double> hasher;
return hasher(v.m_value);
}
using mp_number_float_architype = boost::multiprecision::number<number_backend_float_architype>;
} // namespace concepts
template <>
struct number_category<concepts::number_backend_float_architype> : public std::integral_constant<int, number_kind_floating_point>
{};
}} // namespace boost::multiprecision
namespace std {
template <boost::multiprecision::expression_template_option ExpressionTemplates>
class numeric_limits<boost::multiprecision::number<boost::multiprecision::concepts::number_backend_float_architype, ExpressionTemplates> > : public std::numeric_limits<long double>
{
using base_type = std::numeric_limits<long double> ;
using number_type = boost::multiprecision::number<boost::multiprecision::concepts::number_backend_float_architype, ExpressionTemplates>;
public:
static number_type(min)() noexcept { return (base_type::min)(); }
static number_type(max)() noexcept { return (base_type::max)(); }
static number_type lowest() noexcept { return -(max)(); }
static number_type epsilon() noexcept { return base_type::epsilon(); }
static number_type round_error() noexcept { return base_type::round_error(); }
static number_type infinity() noexcept { return base_type::infinity(); }
static number_type quiet_NaN() noexcept { return base_type::quiet_NaN(); }
static number_type signaling_NaN() noexcept { return base_type::signaling_NaN(); }
static number_type denorm_min() noexcept { return base_type::denorm_min(); }
};
} // namespace std
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,700 @@
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_CPP_BIN_FLOAT_IO_HPP
#define BOOST_MP_CPP_BIN_FLOAT_IO_HPP
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#include <boost/multiprecision/detail/assert.hpp>
namespace boost { namespace multiprecision {
namespace cpp_bf_io_detail {
//
// Multiplies a by b and shifts the result so it fits inside max_bits bits,
// returns by how much the result was shifted.
//
template <class I>
inline I restricted_multiply(cpp_int& result, const cpp_int& a, const cpp_int& b, I max_bits, std::int64_t& error)
{
using local_integral_type = I;
result = a * b;
local_integral_type gb = static_cast<local_integral_type>(msb(result));
local_integral_type rshift = 0;
if (gb > max_bits)
{
rshift = gb - max_bits;
local_integral_type lb = static_cast<local_integral_type>(lsb(result));
int roundup = 0;
// The error rate increases by the error of both a and b,
// this may be overly pessimistic in many case as we're assuming
// that a and b have the same level of uncertainty...
if (lb < rshift)
error = error ? error * 2 : 1;
if (rshift)
{
BOOST_MP_ASSERT(rshift < INT_MAX);
if (bit_test(result, static_cast<unsigned>(rshift - 1)))
{
if (lb == rshift - 1)
roundup = 1;
else
roundup = 2;
}
result >>= rshift;
}
if ((roundup == 2) || ((roundup == 1) && (result.backend().limbs()[0] & 1)))
++result;
}
return rshift;
}
//
// Computes a^e shifted to the right so it fits in max_bits, returns how far
// to the right we are shifted.
//
template <class I>
inline I restricted_pow(cpp_int& result, const cpp_int& a, I e, I max_bits, std::int64_t& error)
{
BOOST_MP_ASSERT(&result != &a);
I exp = 0;
if (e == 1)
{
result = a;
return exp;
}
else if (e == 2)
{
return restricted_multiply(result, a, a, max_bits, error);
}
else if (e == 3)
{
exp = restricted_multiply(result, a, a, max_bits, error);
exp += restricted_multiply(result, result, a, max_bits, error);
return exp;
}
I p = e / 2;
exp = restricted_pow(result, a, p, max_bits, error);
exp *= 2;
exp += restricted_multiply(result, result, result, max_bits, error);
if (e & 1)
exp += restricted_multiply(result, result, a, max_bits, error);
return exp;
}
inline int get_round_mode(const cpp_int& what, std::int64_t location, std::int64_t error)
{
//
// Can we round what at /location/, if the error in what is /error/ in
// units of 0.5ulp. Return:
//
// -1: Can't round.
// 0: leave as is.
// 1: tie.
// 2: round up.
//
BOOST_MP_ASSERT(location >= 0);
BOOST_MP_ASSERT(location < INT_MAX);
std::int64_t error_radius = error & 1 ? (1 + error) / 2 : error / 2;
if (error_radius && (static_cast<int>(msb(error_radius)) >= location))
return -1;
if (bit_test(what, static_cast<unsigned>(location)))
{
if (static_cast<int>(lsb(what)) == location)
return error ? -1 : 1; // Either a tie or can't round depending on whether we have any error
if (!error)
return 2; // no error, round up.
cpp_int t = what - error_radius;
if (static_cast<int>(lsb(t)) >= location)
return -1;
return 2;
}
else if (error)
{
cpp_int t = what + error_radius;
return bit_test(t, static_cast<unsigned>(location)) ? -1 : 0;
}
return 0;
}
inline int get_round_mode(cpp_int& r, cpp_int& d, std::int64_t error, const cpp_int& q)
{
//
// Lets suppose we have an inexact division by d+delta, where the true
// value for the divisor is d, and with |delta| <= error/2, then
// we have calculated q and r such that:
//
// n r
// --- = q + -----------
// d + error d + error
//
// Rearranging for n / d we get:
//
// n delta*q + r
// --- = q + -------------
// d d
//
// So rounding depends on whether 2r + error * q > d.
//
// We return:
// 0 = down down.
// 1 = tie.
// 2 = round up.
// -1 = couldn't decide.
//
r <<= 1;
int c = r.compare(d);
if (c == 0)
return error ? -1 : 1;
if (c > 0)
{
if (error)
{
r -= error * q;
return r.compare(d) > 0 ? 2 : -1;
}
return 2;
}
if (error)
{
r += error * q;
return r.compare(d) < 0 ? 0 : -1;
}
return 0;
}
} // namespace cpp_bf_io_detail
namespace backends {
template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::operator=(const char* s)
{
cpp_int n;
std::intmax_t decimal_exp = 0;
std::intmax_t digits_seen = 0;
constexpr std::intmax_t max_digits_seen = 4 + (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count * 301L) / 1000;
bool ss = false;
//
// Extract the sign:
//
if (*s == '-')
{
ss = true;
++s;
}
else if (*s == '+')
++s;
//
// Special cases first:
//
if ((std::strcmp(s, "nan") == 0) || (std::strcmp(s, "NaN") == 0) || (std::strcmp(s, "NAN") == 0))
{
return *this = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
}
if ((std::strcmp(s, "inf") == 0) || (std::strcmp(s, "Inf") == 0) || (std::strcmp(s, "INF") == 0) || (std::strcmp(s, "infinity") == 0) || (std::strcmp(s, "Infinity") == 0) || (std::strcmp(s, "INFINITY") == 0))
{
*this = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
if (ss)
negate();
return *this;
}
//
// Digits before the point:
//
while (*s && (*s >= '0') && (*s <= '9'))
{
n *= 10u;
n += *s - '0';
if (digits_seen || (*s != '0'))
++digits_seen;
++s;
}
// The decimal point (we really should localise this!!)
if (*s && (*s == '.'))
++s;
//
// Digits after the point:
//
while (*s && (*s >= '0') && (*s <= '9'))
{
n *= 10u;
n += *s - '0';
--decimal_exp;
if (digits_seen || (*s != '0'))
++digits_seen;
++s;
if (digits_seen > max_digits_seen)
break;
}
//
// Digits we're skipping:
//
while (*s && (*s >= '0') && (*s <= '9'))
++s;
//
// See if there's an exponent:
//
if (*s && ((*s == 'e') || (*s == 'E')))
{
++s;
std::intmax_t e = 0;
bool es = false;
if (*s && (*s == '-'))
{
es = true;
++s;
}
else if (*s && (*s == '+'))
++s;
while (*s && (*s >= '0') && (*s <= '9'))
{
e *= 10u;
e += *s - '0';
++s;
}
if (es)
e = -e;
decimal_exp += e;
}
if (*s)
{
//
// Oops unexpected input at the end of the number:
//
BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unable to parse string as a valid floating point number."));
}
if (n == 0)
{
// Result is necessarily zero:
*this = static_cast<limb_type>(0u);
return *this;
}
constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
//
// Set our working precision - this is heuristic based, we want
// a value as small as possible > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count to avoid large computations
// and excessive memory usage, but we also want to avoid having to
// up the computation and start again at a higher precision.
// So we round cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count up to the nearest whole number of limbs, and add
// one limb for good measure. This works very well for small exponents,
// but for larger exponents we may may need to restart, we could add some
// extra precision right from the start for larger exponents, but this
// seems to be slightly slower in the *average* case:
//
#ifdef BOOST_MP_STRESS_IO
std::intmax_t max_bits = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + 32;
#else
std::intmax_t max_bits = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + ((cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count % limb_bits) ? (limb_bits - cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count % limb_bits) : 0) + limb_bits;
#endif
std::int64_t error = 0;
std::intmax_t calc_exp = 0;
std::intmax_t final_exponent = 0;
if (decimal_exp >= 0)
{
// Nice and simple, the result is an integer...
do
{
cpp_int t;
if (decimal_exp)
{
calc_exp = boost::multiprecision::cpp_bf_io_detail::restricted_pow(t, cpp_int(5), decimal_exp, max_bits, error);
calc_exp += boost::multiprecision::cpp_bf_io_detail::restricted_multiply(t, t, n, max_bits, error);
}
else
t = n;
final_exponent = (std::int64_t)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 + decimal_exp + calc_exp;
std::ptrdiff_t rshift = static_cast<std::ptrdiff_t>(static_cast<std::ptrdiff_t>(msb(t)) - static_cast<std::ptrdiff_t>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) + 1);
if (rshift > 0)
{
final_exponent += rshift;
int roundup = boost::multiprecision::cpp_bf_io_detail::get_round_mode(t, rshift - 1, error);
t >>= rshift;
if ((roundup == 2) || ((roundup == 1) && t.backend().limbs()[0] & 1))
++t;
else if (roundup < 0)
{
#ifdef BOOST_MP_STRESS_IO
max_bits += 32;
#else
max_bits *= 2;
#endif
error = 0;
continue;
}
}
else
{
BOOST_MP_ASSERT(!error);
}
if (final_exponent > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
{
exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent;
final_exponent -= cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent;
}
else if (final_exponent < cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent)
{
// Underflow:
exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent;
final_exponent -= cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent;
}
else
{
exponent() = static_cast<Exponent>(final_exponent);
final_exponent = 0;
}
copy_and_round(*this, t.backend());
break;
} while (true);
if (ss != sign())
negate();
}
else
{
// Result is the ratio of two integers: we need to organise the
// division so as to produce at least an N-bit result which we can
// round according to the remainder.
//cpp_int d = pow(cpp_int(5), -decimal_exp);
do
{
cpp_int d;
calc_exp = boost::multiprecision::cpp_bf_io_detail::restricted_pow(d, cpp_int(5), -decimal_exp, max_bits, error);
const std::ptrdiff_t shift = static_cast<std::ptrdiff_t>(static_cast<std::ptrdiff_t>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) - static_cast<std::ptrdiff_t>(msb(n)) + static_cast<std::ptrdiff_t>(msb(d)));
final_exponent = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 + decimal_exp - calc_exp;
if (shift > 0)
{
n <<= shift;
final_exponent -= static_cast<Exponent>(shift);
}
cpp_int q, r;
divide_qr(n, d, q, r);
std::ptrdiff_t gb = static_cast<std::ptrdiff_t>(msb(q));
BOOST_MP_ASSERT((gb >= static_cast<int>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) - 1));
//
// Check for rounding conditions we have to
// handle ourselves:
//
int roundup = 0;
if (gb == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1)
{
// Exactly the right number of bits, use the remainder to round:
roundup = boost::multiprecision::cpp_bf_io_detail::get_round_mode(r, d, error, q);
}
else if (bit_test(q, static_cast<std::size_t>(gb - static_cast<std::ptrdiff_t>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count))) && (static_cast<std::ptrdiff_t>(lsb(q)) == static_cast<std::ptrdiff_t>(gb - static_cast<std::ptrdiff_t>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count))))
{
// Too many bits in q and the bits in q indicate a tie, but we can break that using r,
// note that the radius of error in r is error/2 * q:
std::ptrdiff_t lshift = gb - static_cast<int>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) + 1;
q >>= lshift;
final_exponent += static_cast<Exponent>(lshift);
BOOST_MP_ASSERT((msb(q) >= cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1));
if (error && (r < (error / 2) * q))
roundup = -1;
else if (error && (r + (error / 2) * q >= d))
roundup = -1;
else
roundup = r ? 2 : 1;
}
else if (error && (((error / 2) * q + r >= d) || (r < (error / 2) * q)))
{
// We might have been rounding up, or got the wrong quotient: can't tell!
roundup = -1;
}
if (roundup < 0)
{
#ifdef BOOST_MP_STRESS_IO
max_bits += 32;
#else
max_bits *= 2;
#endif
error = 0;
if (shift > 0)
{
n >>= shift;
final_exponent += static_cast<Exponent>(shift);
}
continue;
}
else if ((roundup == 2) || ((roundup == 1) && q.backend().limbs()[0] & 1))
++q;
if (final_exponent > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
{
// Overflow:
exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent;
final_exponent -= cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent;
}
else if (final_exponent < cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent)
{
// Underflow:
exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent;
final_exponent -= cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent;
}
else
{
exponent() = static_cast<Exponent>(final_exponent);
final_exponent = 0;
}
copy_and_round(*this, q.backend());
if (ss != sign())
negate();
break;
} while (true);
}
//
// Check for scaling and/or over/under-flow:
//
final_exponent += exponent();
if (final_exponent > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
{
// Overflow:
exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
bits() = limb_type(0);
}
else if (final_exponent < cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent)
{
// Underflow:
exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
bits() = limb_type(0);
sign() = 0;
}
else
{
exponent() = static_cast<Exponent>(final_exponent);
}
return *this;
}
template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
std::string cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::str(std::streamsize dig, std::ios_base::fmtflags f) const
{
bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
bool fixed = !scientific && (f & std::ios_base::fixed);
if (dig == 0 && !fixed)
dig = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::max_digits10;
std::string s;
if (exponent() <= cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
{
// How far to left-shift in order to demormalise the mantissa:
std::intmax_t shift = (std::intmax_t)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - (std::intmax_t)exponent() - 1;
std::intmax_t digits_wanted = static_cast<int>(dig);
std::intmax_t base10_exp = exponent() >= 0 ? static_cast<std::intmax_t>(std::floor(0.30103 * exponent())) : static_cast<std::intmax_t>(std::ceil(0.30103 * exponent()));
//
// For fixed formatting we want /dig/ digits after the decimal point,
// so if the exponent is zero, allowing for the one digit before the
// decimal point, we want 1 + dig digits etc.
//
if (fixed)
digits_wanted += 1 + base10_exp;
if (scientific)
digits_wanted += 1;
if (digits_wanted < -1)
{
// Fixed precision, no significant digits, and nothing to round!
s = "0";
if (sign())
s.insert(static_cast<std::string::size_type>(0), 1, '-');
boost::multiprecision::detail::format_float_string(s, base10_exp, dig, f, true);
return s;
}
//
// power10 is the base10 exponent we need to multiply/divide by in order
// to convert our denormalised number to an integer with the right number of digits:
//
std::intmax_t power10 = digits_wanted - base10_exp - 1;
//
// If we calculate 5^power10 rather than 10^power10 we need to move
// 2^power10 into /shift/
//
shift -= power10;
cpp_int i;
int roundup = 0; // 0=no rounding, 1=tie, 2=up
constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
//
// Set our working precision - this is heuristic based, we want
// a value as small as possible > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count to avoid large computations
// and excessive memory usage, but we also want to avoid having to
// up the computation and start again at a higher precision.
// So we round cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count up to the nearest whole number of limbs, and add
// one limb for good measure. This works very well for small exponents,
// but for larger exponents we add a few extra limbs to max_bits:
//
#ifdef BOOST_MP_STRESS_IO
std::intmax_t max_bits = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + 32;
#else
std::intmax_t max_bits = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + ((cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count % limb_bits) ? (limb_bits - cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count % limb_bits) : 0) + limb_bits;
if (power10)
{
const std::uintmax_t abs_power10 = static_cast<std::uintmax_t>(boost::multiprecision::detail::abs(power10));
const std::intmax_t msb_div8 = static_cast<std::intmax_t>(msb(abs_power10) / 8u);
max_bits += (msb_div8 * static_cast<std::intmax_t>(limb_bits));
}
#endif
do
{
std::int64_t error = 0;
std::intmax_t calc_exp = 0;
//
// Our integer result is: bits() * 2^-shift * 5^power10
//
i = bits();
if (shift < 0)
{
if (power10 >= 0)
{
// We go straight to the answer with all integer arithmetic,
// the result is always exact and never needs rounding:
BOOST_MP_ASSERT(power10 <= (std::intmax_t)INT_MAX);
i <<= -shift;
if (power10)
i *= pow(cpp_int(5), static_cast<unsigned>(power10));
}
else if (power10 < 0)
{
cpp_int d;
calc_exp = boost::multiprecision::cpp_bf_io_detail::restricted_pow(d, cpp_int(5), -power10, max_bits, error);
shift += calc_exp;
BOOST_MP_ASSERT(shift < 0); // Must still be true!
i <<= -shift;
cpp_int r;
divide_qr(i, d, i, r);
roundup = boost::multiprecision::cpp_bf_io_detail::get_round_mode(r, d, error, i);
if (roundup < 0)
{
#ifdef BOOST_MP_STRESS_IO
max_bits += 32;
#else
max_bits *= 2;
#endif
shift = (std::intmax_t)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - exponent() - 1 - power10;
continue;
}
}
}
else
{
//
// Our integer is bits() * 2^-shift * 10^power10
//
if (power10 > 0)
{
if (power10)
{
cpp_int t;
calc_exp = boost::multiprecision::cpp_bf_io_detail::restricted_pow(t, cpp_int(5), power10, max_bits, error);
calc_exp += boost::multiprecision::cpp_bf_io_detail::restricted_multiply(i, i, t, max_bits, error);
shift -= calc_exp;
}
if ((shift < 0) || ((shift == 0) && error))
{
// We only get here if we were asked for a crazy number of decimal digits -
// more than are present in a 2^max_bits number.
#ifdef BOOST_MP_STRESS_IO
max_bits += 32;
#else
max_bits *= 2;
#endif
shift = (std::intmax_t)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - exponent() - 1 - power10;
continue;
}
if (shift)
{
roundup = boost::multiprecision::cpp_bf_io_detail::get_round_mode(i, shift - 1, error);
if (roundup < 0)
{
#ifdef BOOST_MP_STRESS_IO
max_bits += 32;
#else
max_bits *= 2;
#endif
shift = (std::intmax_t)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - exponent() - 1 - power10;
continue;
}
i >>= shift;
}
}
else
{
// We're right shifting, *and* dividing by 5^-power10,
// so 5^-power10 can never be that large or we'd simply
// get zero as a result, and that case is already handled above:
cpp_int r;
BOOST_MP_ASSERT(-power10 < INT_MAX);
cpp_int d = pow(cpp_int(5), static_cast<unsigned>(-power10));
d <<= shift;
divide_qr(i, d, i, r);
r <<= 1;
int c = r.compare(d);
roundup = c < 0 ? 0 : c == 0 ? 1 : 2;
}
}
s = i.str(0, std::ios_base::fmtflags(0));
//
// Check if we got the right number of digits, this
// is really a test of whether we calculated the
// decimal exponent correctly:
//
std::intmax_t digits_got = i ? static_cast<std::intmax_t>(s.size()) : 0;
if (digits_got != digits_wanted)
{
base10_exp += digits_got - digits_wanted;
if (fixed)
digits_wanted = digits_got; // strange but true.
power10 = digits_wanted - base10_exp - 1;
shift = (std::intmax_t)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - exponent() - 1 - power10;
if (fixed)
break;
roundup = 0;
}
else
break;
} while (true);
//
// Check whether we need to round up: note that we could equally round up
// the integer /i/ above, but since we need to perform the rounding *after*
// the conversion to a string and the digit count check, we might as well
// do it here:
//
if ((roundup == 2) || ((roundup == 1) && ((s[s.size() - 1] - '0') & 1)))
{
boost::multiprecision::detail::round_string_up_at(s, static_cast<int>(s.size() - 1), base10_exp);
}
if (sign())
s.insert(static_cast<std::string::size_type>(0), 1, '-');
boost::multiprecision::detail::format_float_string(s, base10_exp, dig, f, false);
}
else
{
switch (exponent())
{
case exponent_zero:
s = sign() ? "-0" : f & std::ios_base::showpos ? "+0" : "0";
boost::multiprecision::detail::format_float_string(s, 0, dig, f, true);
break;
case exponent_nan:
s = "nan";
break;
case exponent_infinity:
s = sign() ? "-inf" : f & std::ios_base::showpos ? "+inf" : "inf";
break;
default:
break;
}
}
return s;
}
} // namespace backends
}} // namespace boost::multiprecision
#endif

View file

@ -0,0 +1,157 @@
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_CPP_BIN_FLOAT_TRANSCENDENTAL_HPP
#define BOOST_MP_CPP_BIN_FLOAT_TRANSCENDENTAL_HPP
#include <boost/multiprecision/detail/assert.hpp>
namespace boost { namespace multiprecision { namespace backends {
template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
void eval_exp_taylor(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
{
constexpr std::ptrdiff_t bits = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count;
//
// Taylor series for small argument, note returns exp(x) - 1:
//
res = limb_type(0);
cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> num(arg), denom, t;
denom = limb_type(1);
eval_add(res, num);
for (std::size_t k = 2;; ++k)
{
eval_multiply(denom, k);
eval_multiply(num, arg);
eval_divide(t, num, denom);
eval_add(res, t);
if (eval_is_zero(t) || (res.exponent() - bits > t.exponent()))
break;
}
}
template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
void eval_exp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
{
//
// This is based on MPFR's method, let:
//
// n = floor(x / ln(2))
//
// Then:
//
// r = x - n ln(2) : 0 <= r < ln(2)
//
// We can reduce r further by dividing by 2^k, with k ~ sqrt(n),
// so if:
//
// e0 = exp(r / 2^k) - 1
//
// With e0 evaluated by taylor series for small arguments, then:
//
// exp(x) = 2^n (1 + e0)^2^k
//
// Note that to preserve precision we actually square (1 + e0) k times, calculating
// the result less one each time, i.e.
//
// (1 + e0)^2 - 1 = e0^2 + 2e0
//
// Then add the final 1 at the end, given that e0 is small, this effectively wipes
// out the error in the last step.
//
using default_ops::eval_add;
using default_ops::eval_convert_to;
using default_ops::eval_increment;
using default_ops::eval_multiply;
using default_ops::eval_subtract;
int type = eval_fpclassify(arg);
bool isneg = eval_get_sign(arg) < 0;
if (type == static_cast<int>(FP_NAN))
{
res = arg;
errno = EDOM;
return;
}
else if (type == static_cast<int>(FP_INFINITE))
{
res = arg;
if (isneg)
res = limb_type(0u);
else
res = arg;
return;
}
else if (type == static_cast<int>(FP_ZERO))
{
res = limb_type(1);
return;
}
cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> t, n;
if (isneg)
{
t = arg;
t.negate();
eval_exp(res, t);
t.swap(res);
res = limb_type(1);
eval_divide(res, t);
return;
}
eval_divide(n, arg, default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >());
eval_floor(n, n);
eval_multiply(t, n, default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >());
eval_subtract(t, arg);
t.negate();
if (t.compare(default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >()) > 0)
{
// There are some rare cases where the multiply rounds down leaving a remainder > ln2
// See https://github.com/boostorg/multiprecision/issues/120
eval_increment(n);
t = limb_type(0);
}
if (eval_get_sign(t) < 0)
{
// There are some very rare cases where arg/ln2 is an integer, and the subsequent multiply
// rounds up, in that situation t ends up negative at this point which breaks our invariants below:
t = limb_type(0);
}
Exponent k, nn;
eval_convert_to(&nn, n);
if (nn == (std::numeric_limits<Exponent>::max)())
{
// The result will necessarily oveflow:
res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
return;
}
BOOST_MP_ASSERT(t.compare(default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >()) < 0);
k = nn ? Exponent(1) << (msb(nn) / 2) : 0;
k = (std::min)(k, (Exponent)(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count / 4));
eval_ldexp(t, t, -k);
eval_exp_taylor(res, t);
//
// Square 1 + res k times:
//
for (Exponent s = 0; s < k; ++s)
{
t.swap(res);
eval_multiply(res, t, t);
eval_ldexp(t, t, 1);
eval_add(res, t);
}
eval_add(res, limb_type(1));
eval_ldexp(res, res, nn);
}
}}} // namespace boost::multiprecision::backends
#endif

View file

@ -0,0 +1,12 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2018 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_CPP_COMPLEX_HPP
#define BOOST_MP_CPP_COMPLEX_HPP
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/complex_adaptor.hpp>
#endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,368 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Comparison operators for cpp_int_backend:
//
#ifndef BOOST_MP_CPP_INT_ADD_HPP
#define BOOST_MP_CPP_INT_ADD_HPP
#include <boost/multiprecision/detail/constexpr.hpp>
#include <boost/multiprecision/cpp_int/add_unsigned.hpp>
namespace boost { namespace multiprecision { namespace backends {
//
// As above, but for adding a single limb to a non-trivial cpp_int:
//
template <class CppInt1, class CppInt2>
inline BOOST_MP_CXX14_CONSTEXPR void add_unsigned(CppInt1& result, const CppInt2& a, const limb_type& o) noexcept(is_non_throwing_cpp_int<CppInt1>::value)
{
// Addition using modular arithmetic.
// Nothing fancy, just let uintmax_t take the strain:
if (&result != &a)
result.resize(a.size(), a.size());
double_limb_type carry = o;
typename CppInt1::limb_pointer pr = result.limbs();
typename CppInt2::const_limb_pointer pa = a.limbs();
std::size_t i = 0;
// Addition with carry until we either run out of digits or carry is zero:
for (; carry && (i < result.size()); ++i)
{
carry += static_cast<double_limb_type>(pa[i]);
#ifdef __MSVC_RUNTIME_CHECKS
pr[i] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
#else
pr[i] = static_cast<limb_type>(carry);
#endif
carry >>= CppInt1::limb_bits;
}
// Just copy any remaining digits:
if (&a != &result)
{
std_constexpr::copy(pa + i, pa + a.size(), pr + i);
}
if (carry)
{
// We overflowed, need to add one more limb:
std::size_t x = result.size();
result.resize(x + 1, x + 1);
if (result.size() > x)
result.limbs()[x] = static_cast<limb_type>(carry);
}
result.normalize();
result.sign(a.sign());
}
//
// And again to subtract a single limb:
//
template <class CppInt1, class CppInt2>
inline BOOST_MP_CXX14_CONSTEXPR void subtract_unsigned(CppInt1& result, const CppInt2& a, const limb_type& b) noexcept(is_non_throwing_cpp_int<CppInt1>::value)
{
// Subtract one limb.
// Nothing fancy, just let uintmax_t take the strain:
constexpr double_limb_type borrow = static_cast<double_limb_type>(CppInt1::max_limb_value) + 1;
result.resize(a.size(), a.size());
typename CppInt1::limb_pointer pr = result.limbs();
typename CppInt2::const_limb_pointer pa = a.limbs();
if (*pa >= b)
{
*pr = *pa - b;
if (&result != &a)
{
std_constexpr::copy(pa + 1, pa + a.size(), pr + 1);
result.sign(a.sign());
}
else if ((result.size() == 1) && (*pr == 0))
{
result.sign(false); // zero is unsigned.
}
}
else if (result.size() == 1)
{
*pr = b - *pa;
result.sign(!a.sign());
}
else
{
*pr = static_cast<limb_type>((borrow + *pa) - b);
std::size_t i = 1;
while (!pa[i])
{
pr[i] = CppInt1::max_limb_value;
++i;
}
pr[i] = pa[i] - 1;
if (&result != &a)
{
++i;
std_constexpr::copy(pa + i, pa + a.size(), pr + i);
}
result.normalize();
result.sign(a.sign());
}
}
//
// Now the actual functions called by the front end, all of which forward to one of the above:
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_add(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
eval_add(result, result, o);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, std::size_t MinBits3, std::size_t MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
eval_add(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (a.sign() != b.sign())
{
subtract_unsigned(result, a, b);
return;
}
add_unsigned(result, a, b);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_add(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result, const limb_type& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (result.sign())
{
subtract_unsigned(result, result, o);
}
else
add_unsigned(result, result, o);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_add(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const limb_type& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (a.sign())
{
subtract_unsigned(result, a, o);
}
else
add_unsigned(result, a, o);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_add(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const signed_limb_type& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (o < 0)
eval_subtract(result, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(o)));
else if (o > 0)
eval_add(result, static_cast<limb_type>(o));
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_add(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const signed_limb_type& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (o < 0)
eval_subtract(result, a, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(o)));
else if (o > 0)
eval_add(result, a, static_cast<limb_type>(o));
else if (&result != &a)
result = a;
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_subtract(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const limb_type& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (result.sign())
{
add_unsigned(result, result, o);
}
else
subtract_unsigned(result, result, o);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_subtract(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const limb_type& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (a.sign())
{
add_unsigned(result, a, o);
}
else
{
subtract_unsigned(result, a, o);
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_subtract(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const signed_limb_type& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (o)
{
if (o < 0)
eval_add(result, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(o)));
else
eval_subtract(result, static_cast<limb_type>(o));
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_subtract(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const signed_limb_type& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (o)
{
if (o < 0)
eval_add(result, a, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(o)));
else
eval_subtract(result, a, static_cast<limb_type>(o));
}
else if (&result != &a)
result = a;
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_increment(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
constexpr limb_type one = 1;
if (!result.sign() && (result.limbs()[0] < cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::max_limb_value))
++result.limbs()[0];
else if (result.sign() && result.limbs()[0])
{
--result.limbs()[0];
if (!result.limbs()[0] && (result.size() == 1))
result.sign(false);
}
else
eval_add(result, one);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_decrement(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
constexpr limb_type one = 1;
if (!result.sign() && result.limbs()[0])
--result.limbs()[0];
else if (result.sign() && (result.limbs()[0] < cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::max_limb_value))
++result.limbs()[0];
else
eval_subtract(result, one);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_subtract(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
eval_subtract(result, result, o);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, std::size_t MinBits3, std::size_t MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
eval_subtract(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (a.sign() != b.sign())
{
add_unsigned(result, a, b);
return;
}
subtract_unsigned(result, a, b);
}
//
// Simple addition and subtraction routine for trivial cpp_int's come last:
//
// One of the arguments is signed:
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value)>::type
eval_add(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (result.sign() != o.sign())
{
if (*o.limbs() > *result.limbs())
{
*result.limbs() = detail::checked_subtract(*o.limbs(), *result.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.negate();
}
else
*result.limbs() = detail::checked_subtract(*result.limbs(), *o.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
}
else
*result.limbs() = detail::checked_add(*result.limbs(), *o.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.normalize();
}
// Simple version for two unsigned arguments:
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_add(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() = detail::checked_add(*result.limbs(), *o.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.normalize();
}
// signed subtraction:
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value)>::type
eval_subtract(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (result.sign() != o.sign())
{
*result.limbs() = detail::checked_add(*result.limbs(), *o.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
}
else if (*result.limbs() < *o.limbs())
{
*result.limbs() = detail::checked_subtract(*o.limbs(), *result.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.negate();
}
else
*result.limbs() = detail::checked_subtract(*result.limbs(), *o.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_subtract(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() = detail::checked_subtract(*result.limbs(), *o.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.normalize();
}
}}} // namespace boost::multiprecision::backends
#endif

View file

@ -0,0 +1,387 @@
///////////////////////////////////////////////////////////////
// Copyright 2020 Madhur Chauhan.
// Copyright 2020 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_ADD_UNSIGNED_HPP
#define BOOST_MP_ADD_UNSIGNED_HPP
#include <boost/multiprecision/cpp_int/intel_intrinsics.hpp>
#include <boost/multiprecision/detail/assert.hpp>
namespace boost { namespace multiprecision { namespace backends {
template <class CppInt1, class CppInt2, class CppInt3>
inline BOOST_MP_CXX14_CONSTEXPR void add_unsigned_constexpr(CppInt1& result, const CppInt2& a, const CppInt3& b) noexcept(is_non_throwing_cpp_int<CppInt1>::value)
{
using ::boost::multiprecision::std_constexpr::swap;
//
// This is the generic, C++ only version of addition.
// It's also used for all constexpr branches, hence the name.
// Nothing fancy, just let uintmax_t take the strain:
//
double_limb_type carry = 0;
std::size_t m(0), x(0);
std::size_t as = a.size();
std::size_t bs = b.size();
minmax(as, bs, m, x);
if (x == 1)
{
bool s = a.sign();
result = static_cast<double_limb_type>(*a.limbs()) + static_cast<double_limb_type>(*b.limbs());
result.sign(s);
return;
}
result.resize(x, x);
typename CppInt2::const_limb_pointer pa = a.limbs();
typename CppInt3::const_limb_pointer pb = b.limbs();
typename CppInt1::limb_pointer pr = result.limbs();
typename CppInt1::limb_pointer pr_end = pr + m;
if (as < bs)
swap(pa, pb);
// First where a and b overlap:
while (pr != pr_end)
{
carry += static_cast<double_limb_type>(*pa) + static_cast<double_limb_type>(*pb);
#ifdef __MSVC_RUNTIME_CHECKS
*pr = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
#else
*pr = static_cast<limb_type>(carry);
#endif
carry >>= CppInt1::limb_bits;
++pr, ++pa, ++pb;
}
pr_end += x - m;
// Now where only a has digits:
while (pr != pr_end)
{
if (!carry)
{
if (pa != pr)
std_constexpr::copy(pa, pa + (pr_end - pr), pr);
break;
}
carry += static_cast<double_limb_type>(*pa);
#ifdef __MSVC_RUNTIME_CHECKS
*pr = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
#else
*pr = static_cast<limb_type>(carry);
#endif
carry >>= CppInt1::limb_bits;
++pr, ++pa;
}
if (carry)
{
// We overflowed, need to add one more limb:
result.resize(x + 1, x + 1);
if (result.size() > x)
result.limbs()[x] = static_cast<limb_type>(1u);
}
result.normalize();
result.sign(a.sign());
}
//
// Core subtraction routine for all non-trivial cpp_int's:
//
template <class CppInt1, class CppInt2, class CppInt3>
inline BOOST_MP_CXX14_CONSTEXPR void subtract_unsigned_constexpr(CppInt1& result, const CppInt2& a, const CppInt3& b) noexcept(is_non_throwing_cpp_int<CppInt1>::value)
{
using ::boost::multiprecision::std_constexpr::swap;
//
// This is the generic, C++ only version of subtraction.
// It's also used for all constexpr branches, hence the name.
// Nothing fancy, just let uintmax_t take the strain:
//
double_limb_type borrow = 0;
std::size_t m(0), x(0);
minmax(a.size(), b.size(), m, x);
//
// special cases for small limb counts:
//
if (x == 1)
{
bool s = a.sign();
limb_type al = *a.limbs();
limb_type bl = *b.limbs();
if (bl > al)
{
::boost::multiprecision::std_constexpr::swap(al, bl);
s = !s;
}
result = al - bl;
result.sign(s);
return;
}
// This isn't used till later, but comparison has to occur before we resize the result,
// as that may also resize a or b if this is an inplace operation:
int c = a.compare_unsigned(b);
// Set up the result vector:
result.resize(x, x);
// Now that a, b, and result are stable, get pointers to their limbs:
typename CppInt2::const_limb_pointer pa = a.limbs();
typename CppInt3::const_limb_pointer pb = b.limbs();
typename CppInt1::limb_pointer pr = result.limbs();
bool swapped = false;
if (c < 0)
{
swap(pa, pb);
swapped = true;
}
else if (c == 0)
{
result = static_cast<limb_type>(0);
return;
}
std::size_t i = 0;
// First where a and b overlap:
while (i < m)
{
borrow = static_cast<double_limb_type>(pa[i]) - static_cast<double_limb_type>(pb[i]) - borrow;
pr[i] = static_cast<limb_type>(borrow);
borrow = (borrow >> CppInt1::limb_bits) & 1u;
++i;
}
// Now where only a has digits, only as long as we've borrowed:
while (borrow && (i < x))
{
borrow = static_cast<double_limb_type>(pa[i]) - borrow;
pr[i] = static_cast<limb_type>(borrow);
borrow = (borrow >> CppInt1::limb_bits) & 1u;
++i;
}
// Any remaining digits are the same as those in pa:
if ((x != i) && (pa != pr))
std_constexpr::copy(pa + i, pa + x, pr + i);
BOOST_MP_ASSERT(0 == borrow);
//
// We may have lost digits, if so update limb usage count:
//
result.normalize();
result.sign(a.sign());
if (swapped)
result.negate();
}
#ifdef BOOST_MP_HAS_IMMINTRIN_H
//
// This is the key addition routine where all the argument types are non-trivial cpp_int's:
//
//
// This optimization is limited to: GCC, LLVM, ICC (Intel), MSVC for x86_64 and i386.
// If your architecture and compiler supports ADC intrinsic, please file a bug
//
// As of May, 2020 major compilers don't recognize carry chain though adc
// intrinsics are used to hint compilers to use ADC and still compilers don't
// unroll the loop efficiently (except LLVM) so manual unrolling is done.
//
// Also note that these intrinsics were only introduced by Intel as part of the
// ADX processor extensions, even though the addc instruction has been available
// for basically all x86 processors. That means gcc-9, clang-9, msvc-14.2 and up
// are required to support these intrinsics.
//
template <class CppInt1, class CppInt2, class CppInt3>
inline BOOST_MP_CXX14_CONSTEXPR void add_unsigned(CppInt1& result, const CppInt2& a, const CppInt3& b) noexcept(is_non_throwing_cpp_int<CppInt1>::value)
{
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(a.size()))
{
add_unsigned_constexpr(result, a, b);
}
else
#endif
{
using std::swap;
// Nothing fancy, just let uintmax_t take the strain:
std::size_t m(0), x(0);
std::size_t as = a.size();
std::size_t bs = b.size();
minmax(as, bs, m, x);
if (x == 1)
{
bool s = a.sign();
result = static_cast<double_limb_type>(*a.limbs()) + static_cast<double_limb_type>(*b.limbs());
result.sign(s);
return;
}
result.resize(x, x);
typename CppInt2::const_limb_pointer pa = a.limbs();
typename CppInt3::const_limb_pointer pb = b.limbs();
typename CppInt1::limb_pointer pr = result.limbs();
if (as < bs)
swap(pa, pb);
// First where a and b overlap:
std::size_t i = 0;
unsigned char carry = 0;
#if defined(BOOST_MSVC) && !defined(BOOST_HAS_INT128) && defined(_M_X64)
//
// Special case for 32-bit limbs on 64-bit architecture - we can process
// 2 limbs with each instruction.
//
for (; i + 8 <= m; i += 8)
{
carry = _addcarry_u64(carry, *(unsigned long long*)(pa + i + 0), *(unsigned long long*)(pb + i + 0), (unsigned long long*)(pr + i));
carry = _addcarry_u64(carry, *(unsigned long long*)(pa + i + 2), *(unsigned long long*)(pb + i + 2), (unsigned long long*)(pr + i + 2));
carry = _addcarry_u64(carry, *(unsigned long long*)(pa + i + 4), *(unsigned long long*)(pb + i + 4), (unsigned long long*)(pr + i + 4));
carry = _addcarry_u64(carry, *(unsigned long long*)(pa + i + 6), *(unsigned long long*)(pb + i + 6), (unsigned long long*)(pr + i + 6));
}
#else
for (; i + 4 <= m; i += 4)
{
carry = ::boost::multiprecision::detail::addcarry_limb(carry, pa[i + 0], pb[i + 0], pr + i);
carry = ::boost::multiprecision::detail::addcarry_limb(carry, pa[i + 1], pb[i + 1], pr + i + 1);
carry = ::boost::multiprecision::detail::addcarry_limb(carry, pa[i + 2], pb[i + 2], pr + i + 2);
carry = ::boost::multiprecision::detail::addcarry_limb(carry, pa[i + 3], pb[i + 3], pr + i + 3);
}
#endif
for (; i < m; ++i)
carry = ::boost::multiprecision::detail::addcarry_limb(carry, pa[i], pb[i], pr + i);
for (; i < x && carry; ++i)
// We know carry is 1, so we just need to increment pa[i] (ie add a literal 1) and capture the carry:
carry = ::boost::multiprecision::detail::addcarry_limb(0, pa[i], 1, pr + i);
if (i == x && carry)
{
// We overflowed, need to add one more limb:
result.resize(x + 1, x + 1);
if (result.size() > x)
result.limbs()[x] = static_cast<limb_type>(1u);
}
else if ((x != i) && (pa != pr))
// Copy remaining digits only if we need to:
std_constexpr::copy(pa + i, pa + x, pr + i);
result.normalize();
result.sign(a.sign());
}
}
template <class CppInt1, class CppInt2, class CppInt3>
inline BOOST_MP_CXX14_CONSTEXPR void subtract_unsigned(CppInt1& result, const CppInt2& a, const CppInt3& b) noexcept(is_non_throwing_cpp_int<CppInt1>::value)
{
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(a.size()))
{
subtract_unsigned_constexpr(result, a, b);
}
else
#endif
{
using std::swap;
// Nothing fancy, just let uintmax_t take the strain:
std::size_t m(0), x(0);
minmax(a.size(), b.size(), m, x);
//
// special cases for small limb counts:
//
if (x == 1)
{
bool s = a.sign();
limb_type al = *a.limbs();
limb_type bl = *b.limbs();
if (bl > al)
{
::boost::multiprecision::std_constexpr::swap(al, bl);
s = !s;
}
result = al - bl;
result.sign(s);
return;
}
// This isn't used till later, but comparison has to occur before we resize the result,
// as that may also resize a or b if this is an inplace operation:
int c = a.compare_unsigned(b);
// Set up the result vector:
result.resize(x, x);
// Now that a, b, and result are stable, get pointers to their limbs:
typename CppInt2::const_limb_pointer pa = a.limbs();
typename CppInt3::const_limb_pointer pb = b.limbs();
typename CppInt1::limb_pointer pr = result.limbs();
bool swapped = false;
if (c < 0)
{
swap(pa, pb);
swapped = true;
}
else if (c == 0)
{
result = static_cast<limb_type>(0);
return;
}
std::size_t i = 0;
unsigned char borrow = 0;
// First where a and b overlap:
#if defined(BOOST_MSVC) && !defined(BOOST_HAS_INT128) && defined(_M_X64)
//
// Special case for 32-bit limbs on 64-bit architecture - we can process
// 2 limbs with each instruction.
//
for (; i + 8 <= m; i += 8)
{
borrow = _subborrow_u64(borrow, *reinterpret_cast<const unsigned long long*>(pa + i), *reinterpret_cast<const unsigned long long*>(pb + i), reinterpret_cast<unsigned long long*>(pr + i));
borrow = _subborrow_u64(borrow, *reinterpret_cast<const unsigned long long*>(pa + i + 2), *reinterpret_cast<const unsigned long long*>(pb + i + 2), reinterpret_cast<unsigned long long*>(pr + i + 2));
borrow = _subborrow_u64(borrow, *reinterpret_cast<const unsigned long long*>(pa + i + 4), *reinterpret_cast<const unsigned long long*>(pb + i + 4), reinterpret_cast<unsigned long long*>(pr + i + 4));
borrow = _subborrow_u64(borrow, *reinterpret_cast<const unsigned long long*>(pa + i + 6), *reinterpret_cast<const unsigned long long*>(pb + i + 6), reinterpret_cast<unsigned long long*>(pr + i + 6));
}
#else
for(; i + 4 <= m; i += 4)
{
borrow = boost::multiprecision::detail::subborrow_limb(borrow, pa[i], pb[i], pr + i);
borrow = boost::multiprecision::detail::subborrow_limb(borrow, pa[i + 1], pb[i + 1], pr + i + 1);
borrow = boost::multiprecision::detail::subborrow_limb(borrow, pa[i + 2], pb[i + 2], pr + i + 2);
borrow = boost::multiprecision::detail::subborrow_limb(borrow, pa[i + 3], pb[i + 3], pr + i + 3);
}
#endif
for (; i < m; ++i)
borrow = boost::multiprecision::detail::subborrow_limb(borrow, pa[i], pb[i], pr + i);
// Now where only a has digits, only as long as we've borrowed:
while (borrow && (i < x))
{
borrow = boost::multiprecision::detail::subborrow_limb(borrow, pa[i], 0, pr + i);
++i;
}
// Any remaining digits are the same as those in pa:
if ((x != i) && (pa != pr))
std_constexpr::copy(pa + i, pa + x, pr + i);
BOOST_MP_ASSERT(0 == borrow);
//
// We may have lost digits, if so update limb usage count:
//
result.normalize();
result.sign(a.sign());
if (swapped)
result.negate();
} // constepxr.
}
#else
template <class CppInt1, class CppInt2, class CppInt3>
inline BOOST_MP_CXX14_CONSTEXPR void add_unsigned(CppInt1& result, const CppInt2& a, const CppInt3& b) noexcept(is_non_throwing_cpp_int<CppInt1>::value)
{
add_unsigned_constexpr(result, a, b);
}
template <class CppInt1, class CppInt2, class CppInt3>
inline BOOST_MP_CXX14_CONSTEXPR void subtract_unsigned(CppInt1& result, const CppInt2& a, const CppInt3& b) noexcept(is_non_throwing_cpp_int<CppInt1>::value)
{
subtract_unsigned_constexpr(result, a, b);
}
#endif
} } } // namespaces
#endif

View file

@ -0,0 +1,889 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Comparison operators for cpp_int_backend:
//
#ifndef BOOST_MP_CPP_INT_BITWISE_HPP
#define BOOST_MP_CPP_INT_BITWISE_HPP
#include <stdexcept>
#include <type_traits>
#include <boost/multiprecision/detail/endian.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#include <boost/multiprecision/detail/assert.hpp>
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4319)
#endif
namespace boost { namespace multiprecision { namespace backends {
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_CXX14_CONSTEXPR void is_valid_bitwise_op(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<int, checked>&)
{
if (result.sign() || o.sign())
BOOST_MP_THROW_EXCEPTION(std::range_error("Bitwise operations on negative values results in undefined behavior."));
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_CXX14_CONSTEXPR void is_valid_bitwise_op(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>&, const std::integral_constant<int, unchecked>&) {}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_CXX14_CONSTEXPR void is_valid_bitwise_op(
const cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>& result, const std::integral_constant<int, checked>&)
{
if (result.sign())
BOOST_MP_THROW_EXCEPTION(std::range_error("Bitwise operations on negative values results in undefined behavior."));
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_CXX14_CONSTEXPR void is_valid_bitwise_op(
const cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1>&, const std::integral_constant<int, checked>&) {}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_CXX14_CONSTEXPR void is_valid_bitwise_op(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&, const std::integral_constant<int, unchecked>&) {}
template <class CppInt1, class CppInt2, class Op>
BOOST_MP_CXX14_CONSTEXPR void bitwise_op(
CppInt1& result,
const CppInt2& o,
Op op, const std::integral_constant<bool, true>&) noexcept((is_non_throwing_cpp_int<CppInt1>::value))
{
//
// There are 4 cases:
// * Both positive.
// * result negative, o positive.
// * o negative, result positive.
// * Both negative.
//
// When one arg is negative we convert to 2's complement form "on the fly",
// and then convert back to signed-magnitude form at the end.
//
// Note however, that if the type is checked, then bitwise ops on negative values
// are not permitted and an exception will result.
//
is_valid_bitwise_op(result, o, typename CppInt1::checked_type());
//
// First figure out how big the result needs to be and set up some data:
//
std::size_t rs = result.size();
std::size_t os = o.size();
std::size_t m(0), x(0);
minmax(rs, os, m, x);
result.resize(x, x);
typename CppInt1::limb_pointer pr = result.limbs();
typename CppInt2::const_limb_pointer po = o.limbs();
for (std::size_t i = rs; i < x; ++i)
pr[i] = 0;
limb_type next_limb = 0;
if (!result.sign())
{
if (!o.sign())
{
for (std::size_t i = 0; i < os; ++i)
pr[i] = op(pr[i], po[i]);
for (std::size_t i = os; i < x; ++i)
pr[i] = op(pr[i], limb_type(0));
}
else
{
// "o" is negative:
double_limb_type carry = 1;
for (std::size_t i = 0; i < os; ++i)
{
carry += static_cast<double_limb_type>(~po[i]);
pr[i] = op(pr[i], static_cast<limb_type>(carry));
carry >>= CppInt1::limb_bits;
}
for (std::size_t i = os; i < x; ++i)
{
carry += static_cast<double_limb_type>(~limb_type(0));
pr[i] = op(pr[i], static_cast<limb_type>(carry));
carry >>= CppInt1::limb_bits;
}
// Set the overflow into the "extra" limb:
carry += static_cast<double_limb_type>(~limb_type(0));
next_limb = op(limb_type(0), static_cast<limb_type>(carry));
}
}
else
{
if (!o.sign())
{
// "result" is negative:
double_limb_type carry = 1;
for (std::size_t i = 0; i < os; ++i)
{
carry += static_cast<double_limb_type>(~pr[i]);
pr[i] = op(static_cast<limb_type>(carry), po[i]);
carry >>= CppInt1::limb_bits;
}
for (std::size_t i = os; i < x; ++i)
{
carry += static_cast<double_limb_type>(~pr[i]);
pr[i] = op(static_cast<limb_type>(carry), limb_type(0));
carry >>= CppInt1::limb_bits;
}
// Set the overflow into the "extra" limb:
carry += static_cast<double_limb_type>(~limb_type(0));
next_limb = op(static_cast<limb_type>(carry), limb_type(0));
}
else
{
// both are negative:
double_limb_type r_carry = 1;
double_limb_type o_carry = 1;
for (std::size_t i = 0; i < os; ++i)
{
r_carry += static_cast<double_limb_type>(~pr[i]);
o_carry += static_cast<double_limb_type>(~po[i]);
pr[i] = op(static_cast<limb_type>(r_carry), static_cast<limb_type>(o_carry));
r_carry >>= CppInt1::limb_bits;
o_carry >>= CppInt1::limb_bits;
}
for (std::size_t i = os; i < x; ++i)
{
r_carry += static_cast<double_limb_type>(~pr[i]);
o_carry += static_cast<double_limb_type>(~limb_type(0));
pr[i] = op(static_cast<limb_type>(r_carry), static_cast<limb_type>(o_carry));
r_carry >>= CppInt1::limb_bits;
o_carry >>= CppInt1::limb_bits;
}
// Set the overflow into the "extra" limb:
r_carry += static_cast<double_limb_type>(~limb_type(0));
o_carry += static_cast<double_limb_type>(~limb_type(0));
next_limb = op(static_cast<limb_type>(r_carry), static_cast<limb_type>(o_carry));
}
}
//
// See if the result is negative or not:
//
if (static_cast<signed_limb_type>(next_limb) < 0)
{
double_limb_type carry = 1;
for (std::size_t i = 0; i < x; ++i)
{
carry += static_cast<double_limb_type>(~pr[i]);
pr[i] = static_cast<limb_type>(carry);
carry >>= CppInt1::limb_bits;
}
if (carry)
{
result.resize(x + 1, x);
if (result.size() > x)
result.limbs()[x] = static_cast<limb_type>(carry);
}
result.sign(true);
}
else
result.sign(false);
result.normalize();
}
template <class CppInt1, class CppInt2, class Op>
BOOST_MP_CXX14_CONSTEXPR void bitwise_op(
CppInt1& result,
const CppInt2& o,
Op op, const std::integral_constant<bool, false>&) noexcept((is_non_throwing_cpp_int<CppInt1>::value))
{
//
// Both arguments are unsigned types, very simple case handled as a special case.
//
// First figure out how big the result needs to be and set up some data:
//
std::size_t rs = result.size();
std::size_t os = o.size();
std::size_t m(0), x(0);
minmax(rs, os, m, x);
result.resize(x, x);
typename CppInt1::limb_pointer pr = result.limbs();
typename CppInt2::const_limb_pointer po = o.limbs();
for (std::size_t i = rs; i < x; ++i)
pr[i] = 0;
for (std::size_t i = 0; i < os; ++i)
pr[i] = op(pr[i], po[i]);
for (std::size_t i = os; i < x; ++i)
pr[i] = op(pr[i], limb_type(0));
result.normalize();
}
struct bit_and
{
BOOST_MP_CXX14_CONSTEXPR limb_type operator()(limb_type a, limb_type b) const noexcept { return a & b; }
};
struct bit_or
{
BOOST_MP_CXX14_CONSTEXPR limb_type operator()(limb_type a, limb_type b) const noexcept { return a | b; }
};
struct bit_xor
{
BOOST_MP_CXX14_CONSTEXPR limb_type operator()(limb_type a, limb_type b) const noexcept { return a ^ b; }
};
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_bitwise_and(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
bitwise_op(result, o, bit_and(),
std::integral_constant<bool, std::numeric_limits<number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> > >::is_signed || std::numeric_limits<number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> > >::is_signed > ());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_bitwise_or(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
bitwise_op(result, o, bit_or(),
std::integral_constant<bool, std::numeric_limits<number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> > >::is_signed || std::numeric_limits<number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> > >::is_signed > ());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_bitwise_xor(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
bitwise_op(result, o, bit_xor(),
std::integral_constant<bool, std::numeric_limits<number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> > >::is_signed || std::numeric_limits<number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> > >::is_signed > ());
}
//
// Again for operands which are single limbs:
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1> >::value>::type
eval_bitwise_and(
cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1>& result,
limb_type l) noexcept
{
result.limbs()[0] &= l;
result.resize(1, 1);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1> >::value>::type
eval_bitwise_or(
cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1>& result,
limb_type l) noexcept
{
result.limbs()[0] |= l;
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1> >::value>::type
eval_bitwise_xor(
cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1>& result,
limb_type l) noexcept
{
result.limbs()[0] ^= l;
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_complement(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
static_assert(((Checked1 != checked) || (Checked2 != checked)), "Attempt to take the complement of a signed type results in undefined behavior.");
// Increment and negate:
result = o;
eval_increment(result);
result.negate();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_complement(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
std::size_t os = o.size();
result.resize(SIZE_MAX, os);
for (std::size_t i = 0; i < os; ++i)
result.limbs()[i] = ~o.limbs()[i];
for (std::size_t i = os; i < result.size(); ++i)
result.limbs()[i] = ~static_cast<limb_type>(0);
result.normalize();
}
template <class Int>
inline void left_shift_byte(Int& result, double_limb_type s)
{
limb_type offset = static_cast<limb_type>(s / Int::limb_bits);
limb_type shift = static_cast<limb_type>(s % Int::limb_bits);
std::size_t ors = result.size();
if ((ors == 1) && (!*result.limbs()))
return; // shifting zero yields zero.
std::size_t rs = ors;
if (shift && (result.limbs()[ors - 1] >> (Int::limb_bits - shift)))
++rs; // Most significant limb will overflow when shifted
rs += offset;
result.resize(rs, rs);
rs = result.size();
typename Int::limb_pointer pr = result.limbs();
if (rs != ors)
pr[rs - 1] = 0u;
std::size_t bytes = static_cast<std::size_t>(s / CHAR_BIT);
std::size_t len = (std::min)(ors * sizeof(limb_type), rs * sizeof(limb_type) - bytes);
if (bytes >= rs * sizeof(limb_type))
result = static_cast<limb_type>(0u);
else
{
unsigned char* pc = reinterpret_cast<unsigned char*>(pr);
std::memmove(pc + bytes, pc, len);
std::memset(pc, 0, bytes);
}
}
template <class Int>
inline BOOST_MP_CXX14_CONSTEXPR void left_shift_limb(Int& result, double_limb_type s)
{
limb_type offset = static_cast<limb_type>(s / Int::limb_bits);
limb_type shift = static_cast<limb_type>(s % Int::limb_bits);
std::size_t ors = result.size();
if ((ors == 1) && (!*result.limbs()))
return; // shifting zero yields zero.
std::size_t rs = ors;
if (shift && (result.limbs()[ors - 1] >> (Int::limb_bits - shift)))
++rs; // Most significant limb will overflow when shifted
rs += offset;
result.resize(rs, rs);
typename Int::limb_pointer pr = result.limbs();
if (offset > rs)
{
// The result is shifted past the end of the result:
result = static_cast<limb_type>(0);
return;
}
std::size_t i = rs - result.size();
for (; i < ors; ++i)
pr[rs - 1 - i] = pr[ors - 1 - i];
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(s))
{
for (; i < rs; ++i)
pr[rs - 1 - i] = 0;
}
else
#endif
{
std::memset(pr, 0, (rs - i) * sizeof(*pr));
}
}
template <class Int>
inline BOOST_MP_CXX14_CONSTEXPR void left_shift_generic(Int& result, double_limb_type s)
{
limb_type offset = static_cast<limb_type>(s / Int::limb_bits);
limb_type shift = static_cast<limb_type>(s % Int::limb_bits);
std::size_t ors = result.size();
if ((ors == 1) && (!*result.limbs()))
return; // shifting zero yields zero.
std::size_t rs = ors;
if (shift && (result.limbs()[ors - 1] >> (Int::limb_bits - shift)))
++rs; // Most significant limb will overflow when shifted
rs += offset;
result.resize(rs, rs);
bool truncated = result.size() != rs;
typename Int::limb_pointer pr = result.limbs();
if (offset > rs)
{
// The result is shifted past the end of the result:
result = static_cast<limb_type>(0);
return;
}
std::size_t i = rs - result.size();
// This code only works when shift is non-zero, otherwise we invoke undefined behaviour!
BOOST_MP_ASSERT(shift);
if (!truncated)
{
if (rs > ors + offset)
{
pr[rs - 1 - i] = pr[ors - 1 - i] >> (Int::limb_bits - shift);
--rs;
}
else
{
pr[rs - 1 - i] = pr[ors - 1 - i] << shift;
if (ors > 1)
pr[rs - 1 - i] |= pr[ors - 2 - i] >> (Int::limb_bits - shift);
++i;
}
}
for (; rs - i >= static_cast<std::size_t>(static_cast<std::size_t>(2u) + offset); ++i)
{
pr[rs - 1 - i] = pr[rs - 1 - i - offset] << shift;
pr[rs - 1 - i] |= pr[rs - 2 - i - offset] >> (Int::limb_bits - shift);
}
if (rs - i >= static_cast<std::size_t>(static_cast<std::size_t>(1u) + offset))
{
pr[rs - 1 - i] = pr[rs - 1 - i - offset] << shift;
++i;
}
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(s))
{
for (; i < rs; ++i)
pr[rs - 1 - i] = 0;
}
else
#endif
{
std::memset(pr, 0, (rs - i) * sizeof(*pr));
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_left_shift(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
double_limb_type s) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
is_valid_bitwise_op(result, typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
if (!s)
return;
#if BOOST_MP_ENDIAN_LITTLE_BYTE && defined(BOOST_MP_USE_LIMB_SHIFT)
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_bits - 1;
constexpr limb_type byte_shift_mask = CHAR_BIT - 1;
if ((s & limb_shift_mask) == 0)
{
left_shift_limb(result, s);
}
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
else if ((s & byte_shift_mask) == 0)
#else
else if (((s & byte_shift_mask) == 0) && !BOOST_MP_IS_CONST_EVALUATED(s))
#endif
{
left_shift_byte(result, s);
}
#elif BOOST_MP_ENDIAN_LITTLE_BYTE
constexpr limb_type byte_shift_mask = CHAR_BIT - 1;
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
if ((s & byte_shift_mask) == 0)
#else
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_bits - 1;
if (BOOST_MP_IS_CONST_EVALUATED(s) && ((s & limb_shift_mask) == 0))
left_shift_limb(result, s);
else if (((s & byte_shift_mask) == 0) && !BOOST_MP_IS_CONST_EVALUATED(s))
#endif
{
left_shift_byte(result, s);
}
#else
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_bits - 1;
if ((s & limb_shift_mask) == 0)
{
left_shift_limb(result, s);
}
#endif
else
{
left_shift_generic(result, s);
}
//
// We may have shifted off the end and have leading zeros:
//
result.normalize();
}
template <class Int>
inline void right_shift_byte(Int& result, double_limb_type s)
{
limb_type offset = static_cast<limb_type>(s / Int::limb_bits);
BOOST_MP_ASSERT((s % CHAR_BIT) == 0);
std::size_t ors = result.size();
std::size_t rs = ors;
if (offset >= rs)
{
result = limb_type(0);
return;
}
rs -= offset;
typename Int::limb_pointer pr = result.limbs();
unsigned char* pc = reinterpret_cast<unsigned char*>(pr);
limb_type shift = static_cast<limb_type>(s / CHAR_BIT);
std::memmove(pc, pc + shift, ors * sizeof(pr[0]) - shift);
shift = (sizeof(limb_type) - shift % sizeof(limb_type)) * CHAR_BIT;
if (shift < Int::limb_bits)
{
pr[ors - offset - 1] &= (static_cast<limb_type>(1u) << shift) - 1;
if (!pr[ors - offset - 1] && (rs > 1))
--rs;
}
result.resize(rs, rs);
}
template <class Int>
inline BOOST_MP_CXX14_CONSTEXPR void right_shift_limb(Int& result, double_limb_type s)
{
limb_type offset = static_cast<limb_type>(s / Int::limb_bits);
BOOST_MP_ASSERT((s % Int::limb_bits) == 0);
std::size_t ors = result.size();
std::size_t rs = ors;
if (offset >= rs)
{
result = limb_type(0);
return;
}
rs -= offset;
typename Int::limb_pointer pr = result.limbs();
std::size_t i = 0;
for (; i < rs; ++i)
pr[i] = pr[i + offset];
result.resize(rs, rs);
}
template <class Int>
inline BOOST_MP_CXX14_CONSTEXPR void right_shift_generic(Int& result, double_limb_type s)
{
limb_type offset = static_cast<limb_type>(s / Int::limb_bits);
limb_type shift = static_cast<limb_type>(s % Int::limb_bits);
std::size_t ors = result.size();
std::size_t rs = ors;
if (offset >= rs)
{
result = limb_type(0);
return;
}
rs -= offset;
typename Int::limb_pointer pr = result.limbs();
if ((pr[ors - 1] >> shift) == 0)
{
if (--rs == 0)
{
result = limb_type(0);
return;
}
}
std::size_t i = 0;
// This code only works for non-zero shift, otherwise we invoke undefined behaviour!
BOOST_MP_ASSERT(shift);
for (; i + offset + 1 < ors; ++i)
{
pr[i] = pr[i + offset] >> shift;
pr[i] |= pr[i + offset + 1] << (Int::limb_bits - shift);
}
pr[i] = pr[i + offset] >> shift;
result.resize(rs, rs);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_int_check_type Checked1, class Allocator1>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1> >::value>::type
eval_right_shift(
cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1>& result,
double_limb_type s) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1> >::value))
{
is_valid_bitwise_op(result, typename cpp_int_backend<MinBits1, MaxBits1, unsigned_magnitude, Checked1, Allocator1>::checked_type());
if (!s)
return;
#if BOOST_MP_ENDIAN_LITTLE_BYTE && defined(BOOST_MP_USE_LIMB_SHIFT)
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>::limb_bits - 1;
constexpr limb_type byte_shift_mask = CHAR_BIT - 1;
if ((s & limb_shift_mask) == 0)
right_shift_limb(result, s);
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
else if ((s & byte_shift_mask) == 0)
#else
else if (((s & byte_shift_mask) == 0) && !BOOST_MP_IS_CONST_EVALUATED(s))
#endif
right_shift_byte(result, s);
#elif BOOST_MP_ENDIAN_LITTLE_BYTE
constexpr limb_type byte_shift_mask = CHAR_BIT - 1;
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
if ((s & byte_shift_mask) == 0)
#else
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>::limb_bits - 1;
if (BOOST_MP_IS_CONST_EVALUATED(s) && ((s & limb_shift_mask) == 0))
right_shift_limb(result, s);
else if (((s & byte_shift_mask) == 0) && !BOOST_MP_IS_CONST_EVALUATED(s))
#endif
right_shift_byte(result, s);
#else
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>::limb_bits - 1;
if ((s & limb_shift_mask) == 0)
right_shift_limb(result, s);
#endif
else
right_shift_generic(result, s);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_int_check_type Checked1, class Allocator1>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1> >::value>::type
eval_right_shift(
cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>& result,
double_limb_type s) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1> >::value))
{
is_valid_bitwise_op(result, typename cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>::checked_type());
if (!s)
return;
bool is_neg = result.sign();
if (is_neg)
eval_increment(result);
#if BOOST_MP_ENDIAN_LITTLE_BYTE && defined(BOOST_MP_USE_LIMB_SHIFT)
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>::limb_bits - 1;
constexpr limb_type byte_shift_mask = CHAR_BIT - 1;
if ((s & limb_shift_mask) == 0)
right_shift_limb(result, s);
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
else if ((s & byte_shift_mask) == 0)
#else
else if (((s & byte_shift_mask) == 0) && !BOOST_MP_IS_CONST_EVALUATED(s))
#endif
right_shift_byte(result, s);
#elif BOOST_MP_ENDIAN_LITTLE_BYTE
constexpr limb_type byte_shift_mask = CHAR_BIT - 1;
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
if ((s & byte_shift_mask) == 0)
#else
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>::limb_bits - 1;
if (BOOST_MP_IS_CONST_EVALUATED(s) && ((s & limb_shift_mask) == 0))
right_shift_limb(result, s);
else if (((s & byte_shift_mask) == 0) && !BOOST_MP_IS_CONST_EVALUATED(s))
#endif
right_shift_byte(result, s);
#else
constexpr limb_type limb_shift_mask = cpp_int_backend<MinBits1, MaxBits1, signed_magnitude, Checked1, Allocator1>::limb_bits - 1;
if ((s & limb_shift_mask) == 0)
right_shift_limb(result, s);
#endif
else
right_shift_generic(result, s);
if (is_neg)
eval_decrement(result);
}
//
// Over again for trivial cpp_int's:
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, class T>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value >::type
eval_left_shift(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result, T s) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
is_valid_bitwise_op(result, typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
*result.limbs() = detail::checked_left_shift(*result.limbs(), s, typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, class T>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value >::type
eval_right_shift(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result, T s) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
// Nothing to check here... just make sure we don't invoke undefined behavior:
is_valid_bitwise_op(result, typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
*result.limbs() = (static_cast<unsigned>(s) >= sizeof(*result.limbs()) * CHAR_BIT) ? 0 : (result.sign() ? ((--*result.limbs()) >> s) + 1 : *result.limbs() >> s);
if (result.sign() && (*result.limbs() == 0))
result = static_cast<signed_limb_type>(-1);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value)>::type
eval_complement(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
static_assert(((Checked1 != checked) || (Checked2 != checked)), "Attempt to take the complement of a signed type results in undefined behavior.");
//
// If we're not checked then emulate 2's complement behavior:
//
if (o.sign())
{
*result.limbs() = *o.limbs() - 1;
result.sign(false);
}
else
{
*result.limbs() = 1 + *o.limbs();
result.sign(true);
}
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_complement(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() = ~*o.limbs();
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_bitwise_and(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() &= *o.limbs();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value)>::type
eval_bitwise_and(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
is_valid_bitwise_op(result, o, typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
using default_ops::eval_bit_test;
using default_ops::eval_increment;
if (result.sign() || o.sign())
{
constexpr std::size_t m = detail::static_unsigned_max<detail::static_unsigned_max<MinBits1, MinBits2>::value, detail::static_unsigned_max<MaxBits1, MaxBits2>::value>::value;
cpp_int_backend<m + 1, m + 1, unsigned_magnitude, unchecked, void> t1(result);
cpp_int_backend<m + 1, m + 1, unsigned_magnitude, unchecked, void> t2(o);
eval_bitwise_and(t1, t2);
bool s = eval_bit_test(t1, m + 1);
if (s)
{
eval_complement(t1, t1);
eval_increment(t1);
}
result = t1;
result.sign(s);
}
else
{
*result.limbs() &= *o.limbs();
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_bitwise_or(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() |= *o.limbs();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value)>::type
eval_bitwise_or(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
is_valid_bitwise_op(result, o, typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
using default_ops::eval_bit_test;
using default_ops::eval_increment;
if (result.sign() || o.sign())
{
constexpr std::size_t m = detail::static_unsigned_max<detail::static_unsigned_max<MinBits1, MinBits2>::value, detail::static_unsigned_max<MaxBits1, MaxBits2>::value>::value;
cpp_int_backend<m + 1, m + 1, unsigned_magnitude, unchecked, void> t1(result);
cpp_int_backend<m + 1, m + 1, unsigned_magnitude, unchecked, void> t2(o);
eval_bitwise_or(t1, t2);
bool s = eval_bit_test(t1, m + 1);
if (s)
{
eval_complement(t1, t1);
eval_increment(t1);
}
result = t1;
result.sign(s);
}
else
{
*result.limbs() |= *o.limbs();
result.normalize();
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_bitwise_xor(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() ^= *o.limbs();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value)>::type
eval_bitwise_xor(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
is_valid_bitwise_op(result, o, typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
using default_ops::eval_bit_test;
using default_ops::eval_increment;
if (result.sign() || o.sign())
{
constexpr std::size_t m = detail::static_unsigned_max<detail::static_unsigned_max<MinBits1, MinBits2>::value, detail::static_unsigned_max<MaxBits1, MaxBits2>::value>::value;
cpp_int_backend<m + 1, m + 1, unsigned_magnitude, unchecked, void> t1(result);
cpp_int_backend<m + 1, m + 1, unsigned_magnitude, unchecked, void> t2(o);
eval_bitwise_xor(t1, t2);
bool s = eval_bit_test(t1, m + 1);
if (s)
{
eval_complement(t1, t1);
eval_increment(t1);
}
result = t1;
result.sign(s);
}
else
{
*result.limbs() ^= *o.limbs();
}
}
}}} // namespace boost::multiprecision::backends
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#endif

View file

@ -0,0 +1,178 @@
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_CPP_INT_CHECKED_HPP
#define BOOST_MP_CPP_INT_CHECKED_HPP
#include <climits>
#include <limits>
#include <type_traits>
#include <stdexcept>
#include <string>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
namespace boost { namespace multiprecision { namespace backends { namespace detail {
//
// Simple routines for performing checked arithmetic with a builtin arithmetic type.
// Note that this is not a complete header, it must be included as part of boost/multiprecision/cpp_int.hpp.
//
template <typename T>
inline constexpr T type_max() noexcept
{
return
#ifdef BOOST_HAS_INT128
std::is_same<T, boost::multiprecision::int128_type>::value ? INT128_MAX :
std::is_same<T, boost::multiprecision::uint128_type>::value ? UINT128_MAX :
#endif
(std::numeric_limits<T>::max)();
}
template <typename T>
inline constexpr T type_min() noexcept
{
return
#ifdef BOOST_HAS_INT128
std::is_same<T, boost::multiprecision::int128_type>::value ? INT128_MIN :
std::is_same<T, boost::multiprecision::uint128_type>::value ? T(0) :
#endif
(std::numeric_limits<T>::min)();
}
inline void raise_overflow(std::string op)
{
BOOST_MP_THROW_EXCEPTION(std::overflow_error("overflow in " + op));
}
inline void raise_add_overflow()
{
raise_overflow("addition");
}
inline void raise_subtract_overflow()
{
BOOST_MP_THROW_EXCEPTION(std::range_error("Subtraction resulted in a negative value, but the type is unsigned"));
}
inline void raise_mul_overflow()
{
raise_overflow("multiplication");
}
inline void raise_div_overflow()
{
raise_overflow("division");
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_add_imp(A a, A b, const std::integral_constant<bool, true>&)
{
if (a > 0)
{
if ((b > 0) && ((type_max<A>() - b) < a))
raise_add_overflow();
}
else
{
if ((b < 0) && ((type_min<A>() - b) > a))
raise_add_overflow();
}
return a + b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_add_imp(A a, A b, const std::integral_constant<bool, false>&)
{
if ((type_max<A>() - b) < a)
raise_add_overflow();
return a + b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_add(A a, A b, const std::integral_constant<int, checked>&)
{
return checked_add_imp(a, b, std::integral_constant<bool, boost::multiprecision::detail::is_signed<A>::value && boost::multiprecision::detail::is_integral<A>::value > ());
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_add(A a, A b, const std::integral_constant<int, unchecked>&)
{
return a + b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract_imp(A a, A b, const std::integral_constant<bool, true>&)
{
if (a > 0)
{
if ((b < 0) && ((type_max<A>() + b) < a))
raise_subtract_overflow();
}
else
{
if ((b > 0) && ((type_min<A>() + b) > a))
raise_subtract_overflow();
}
return a - b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract_imp(A a, A b, const std::integral_constant<bool, false>&)
{
if (a < b)
raise_subtract_overflow();
return a - b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract(A a, A b, const std::integral_constant<int, checked>&)
{
return checked_subtract_imp(a, b, std::integral_constant<bool, boost::multiprecision::detail::is_signed<A>::value && boost::multiprecision::detail::is_integral<A>::value>());
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract(A a, A b, const std::integral_constant<int, unchecked>&)
{
return a - b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_multiply(A a, A b, const std::integral_constant<int, checked>&)
{
BOOST_MP_USING_ABS
if (a && (type_max<A>() / abs(a) < abs(b)))
raise_mul_overflow();
return a * b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_multiply(A a, A b, const std::integral_constant<int, unchecked>&)
{
return a * b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_divide(A a, A b, const std::integral_constant<int, checked>&)
{
if (b == 0)
raise_div_overflow();
return a / b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_divide(A a, A b, const std::integral_constant<int, unchecked>&)
{
return a / b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_left_shift(A a, unsigned long long shift, const std::integral_constant<int, checked>&)
{
if (a && shift)
{
if ((shift > sizeof(A) * CHAR_BIT) || (a >> (sizeof(A) * CHAR_BIT - shift)))
BOOST_MP_THROW_EXCEPTION(std::overflow_error("Shift out of range"));
}
return a << shift;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR A checked_left_shift(A a, unsigned long long shift, const std::integral_constant<int, unchecked>&)
{
return (shift >= sizeof(A) * CHAR_BIT) ? 0 : a << shift;
}
}}}} // namespace boost::multiprecision::backends::detail
#endif

View file

@ -0,0 +1,374 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Comparison operators for cpp_int_backend:
//
#ifndef BOOST_MP_CPP_INT_COMPARISON_HPP
#define BOOST_MP_CPP_INT_COMPARISON_HPP
#include <boost/multiprecision/detail/constexpr.hpp>
namespace boost { namespace multiprecision { namespace backends {
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4018 4389 4996)
#endif
//
// Start with non-trivial cpp_int's:
//
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& a, const cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& b) noexcept
{
return (a.sign() == b.sign()) && (a.size() == b.size()) && std_constexpr::equal(a.limbs(), a.limbs() + a.size(), b.limbs());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& a, const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& b) noexcept
{
return (a.sign() == b.sign()) && (a.size() == b.size()) && std_constexpr::equal(a.limbs(), a.limbs() + a.size(), b.limbs());
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& a, limb_type b) noexcept
{
return (a.sign() == false) && (a.size() == 1) && (*a.limbs() == b);
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& a, signed_limb_type b) noexcept
{
return (a.sign() == (b < 0)) && (a.size() == 1) && (*a.limbs() == boost::multiprecision::detail::unsigned_abs(b));
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator>& a, limb_type b) noexcept
{
return (a.size() == 1) && (*a.limbs() == b);
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator>& a, signed_limb_type b) noexcept
{
return (b < 0) ? eval_eq(a, cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator>(b)) : eval_eq(a, static_cast<limb_type>(b)); // Use bit pattern of b for comparison
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& a, limb_type b) noexcept
{
if (a.sign())
return true;
if (a.size() > 1)
return false;
return *a.limbs() < b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& a, signed_limb_type b) noexcept
{
if ((b == 0) || (a.sign() != (b < 0)))
return a.sign();
if (a.sign())
{
if (a.size() > 1)
return true;
return *a.limbs() > boost::multiprecision::detail::unsigned_abs(b);
}
else
{
if (a.size() > 1)
return false;
return *a.limbs() < boost::multiprecision::detail::unsigned_abs(b);
}
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator>& a, limb_type b) noexcept
{
if (a.size() > 1)
return false;
return *a.limbs() < b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator>& a, signed_limb_type b) noexcept
{
return (b < 0) ? a.compare(b) < 0 : eval_lt(a, static_cast<limb_type>(b)); // Use bit pattern of b for comparison
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& a, limb_type b) noexcept
{
if (a.sign())
return false;
if (a.size() > 1)
return true;
return *a.limbs() > b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& a, signed_limb_type b) noexcept
{
if (b == 0)
return !a.sign() && ((a.size() > 1) || *a.limbs());
if (a.sign() != (b < 0))
return !a.sign();
if (a.sign())
{
if (a.size() > 1)
return false;
return *a.limbs() < boost::multiprecision::detail::unsigned_abs(b);
}
else
{
if (a.size() > 1)
return true;
return *a.limbs() > boost::multiprecision::detail::unsigned_abs(b);
}
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator>& a, limb_type b) noexcept
{
if (a.size() > 1)
return true;
return *a.limbs() > b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator>& a, signed_limb_type b) noexcept
{
return (b < 0) ? a.compare(b) > 0 : eval_gt(a, static_cast<limb_type>(b)); // Use bit pattern of b for comparison.
}
//
// And again for trivial cpp_ints:
//
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::value
eval_eq(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& b) noexcept
{
return (a.sign() == b.sign()) && (*a.limbs() == *b.limbs());
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::value
eval_eq(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& b) noexcept
{
return *a.limbs() == *b.limbs();
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class U>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_unsigned<U>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, U b) noexcept
{
return !a.sign() && (*a.limbs() == b);
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class S>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, S b) noexcept
{
return (a.sign() == (b < 0)) && (*a.limbs() == boost::multiprecision::detail::unsigned_abs(b));
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class U>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_unsigned<U>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, U b) noexcept
{
return *a.limbs() == b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class S>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::type
eval_eq(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, S b) noexcept
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<S>::type;
if (b < 0)
{
cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> t(b);
return *a.limbs() == *t.limbs();
}
else
{
return *a.limbs() == static_cast<ui_type>(b);
}
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& b) noexcept
{
if (a.sign() != b.sign())
return a.sign();
return a.sign() ? *a.limbs() > *b.limbs() : *a.limbs() < *b.limbs();
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& b) noexcept
{
return *a.limbs() < *b.limbs();
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class U>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_unsigned<U>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, U b) noexcept
{
if (a.sign())
return true;
return *a.limbs() < b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class S>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, S b) noexcept
{
if (a.sign() != (b < 0))
return a.sign();
return a.sign() ? (*a.limbs() > boost::multiprecision::detail::unsigned_abs(b)) : (*a.limbs() < boost::multiprecision::detail::unsigned_abs(b));
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class U>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_unsigned<U>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, U b) noexcept
{
return *a.limbs() < b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class S>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::type
eval_lt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, S b) noexcept
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<S>::type;
if (b < 0)
{
cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> t(b);
return *a.limbs() < *t.limbs();
}
else
{
return *a.limbs() < static_cast<ui_type>(b);
}
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& b) noexcept
{
if (a.sign() != b.sign())
return !a.sign();
return a.sign() ? *a.limbs() < *b.limbs() : *a.limbs() > *b.limbs();
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& b) noexcept
{
return *a.limbs() > *b.limbs();
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class U>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_unsigned<U>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, U b) noexcept
{
if (a.sign())
return false;
return *a.limbs() > b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class S>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, void>& a, S b) noexcept
{
if (a.sign() != (b < 0))
return !a.sign();
return a.sign() ? (*a.limbs() < boost::multiprecision::detail::unsigned_abs(b)) : (*a.limbs() > boost::multiprecision::detail::unsigned_abs(b));
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class U>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_unsigned<U>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, U b) noexcept
{
return *a.limbs() > b;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class S>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value && is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> >::value,
bool>::type
eval_gt(const cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void>& a, S b) noexcept
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<S>::type;
if (b < 0)
{
cpp_int_backend<MinBits, MaxBits, unsigned_magnitude, Checked, void> t(b);
return *a.limbs() > *t.limbs();
}
else
{
return *a.limbs() > static_cast<ui_type>(b);
}
}
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
}}} // namespace boost::multiprecision::backends
#endif

View file

@ -0,0 +1,161 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 - 2021 John Maddock.
// Copyright 2021 Matt Borland.
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_CPP_INT_CONFIG_HPP
#define BOOST_MP_CPP_INT_CONFIG_HPP
#include <cstdint>
#include <type_traits>
#include <limits>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/assert.hpp>
namespace boost {
namespace multiprecision {
namespace detail {
//
// These traits calculate the largest type in the list
// [unsigned] long long, long, int, which has the specified number
// of bits. Note that int_t and uint_t find the first
// member of the above list, not the last. We want the last in the
// list to ensure that mixed arithmetic operations are as efficient
// as possible.
//
template <std::size_t Bits>
struct int_t
{
using exact = typename std::conditional<Bits <= sizeof(signed char) * CHAR_BIT, signed char,
typename std::conditional<Bits <= sizeof(short) * CHAR_BIT, short,
typename std::conditional<Bits <= sizeof(int) * CHAR_BIT, int,
typename std::conditional<Bits <= sizeof(long) * CHAR_BIT, long,
typename std::conditional<Bits <= sizeof(long long) * CHAR_BIT, long long, void
>::type>::type>::type>::type>::type;
using least = typename std::conditional<Bits-1 <= std::numeric_limits<signed char>::digits, signed char,
typename std::conditional<Bits-1 <= std::numeric_limits<short>::digits, short,
typename std::conditional<Bits-1 <= std::numeric_limits<int>::digits, int,
typename std::conditional<Bits-1 <= std::numeric_limits<long>::digits, long,
typename std::conditional<Bits-1 <= std::numeric_limits<long long>::digits, long long, void
>::type>::type>::type>::type>::type;
static_assert(!std::is_same<void, exact>::value && !std::is_same<void, least>::value, "Number of bits does not match any standard data type. \
Please file an issue at https://github.com/boostorg/multiprecision/ referencing this error from cpp_int_config.hpp");
};
template <std::size_t Bits>
struct uint_t
{
using exact = typename std::conditional<Bits <= sizeof(unsigned char) * CHAR_BIT, unsigned char,
typename std::conditional<Bits <= sizeof(unsigned short) * CHAR_BIT, unsigned short,
typename std::conditional<Bits <= sizeof(unsigned int) * CHAR_BIT, unsigned int,
typename std::conditional<Bits <= sizeof(unsigned long) * CHAR_BIT, unsigned long,
typename std::conditional<Bits <= sizeof(unsigned long long) * CHAR_BIT, unsigned long long, void
>::type>::type>::type>::type>::type;
using least = typename std::conditional<Bits <= std::numeric_limits<unsigned char>::digits, unsigned char,
typename std::conditional<Bits <= std::numeric_limits<unsigned short>::digits, unsigned short,
typename std::conditional<Bits <= std::numeric_limits<unsigned int>::digits, unsigned int,
typename std::conditional<Bits <= std::numeric_limits<unsigned long>::digits, unsigned long,
typename std::conditional<Bits <= std::numeric_limits<unsigned long long>::digits, unsigned long long, void
>::type>::type>::type>::type>::type;
static_assert(!std::is_same<void, exact>::value && !std::is_same<void, least>::value, "Number of bits does not match any standard data type. \
Please file an issue at https://github.com/boostorg/multiprecision/ referencing this error from cpp_int_config.hpp");
};
template <std::size_t N>
struct largest_signed_type
{
using type = typename std::conditional<
1 + std::numeric_limits<long long>::digits == N,
long long,
typename std::conditional<
1 + std::numeric_limits<long>::digits == N,
long,
typename std::conditional<
1 + std::numeric_limits<int>::digits == N,
int,
typename int_t<N>::exact>::type>::type>::type;
};
template <std::size_t N>
struct largest_unsigned_type
{
using type = typename std::conditional<
std::numeric_limits<unsigned long long>::digits == N,
unsigned long long,
typename std::conditional<
std::numeric_limits<unsigned long>::digits == N,
unsigned long,
typename std::conditional<
std::numeric_limits<unsigned int>::digits == N,
unsigned int,
typename uint_t<N>::exact>::type>::type>::type;
};
} // namespace detail
#if defined(BOOST_HAS_INT128)
using limb_type = detail::largest_unsigned_type<64>::type;
using signed_limb_type = detail::largest_signed_type<64>::type;
using double_limb_type = boost::multiprecision::uint128_type;
using signed_double_limb_type = boost::multiprecision::int128_type;
constexpr limb_type max_block_10 = 1000000000000000000uLL;
constexpr limb_type digits_per_block_10 = 18;
inline BOOST_MP_CXX14_CONSTEXPR limb_type block_multiplier(std::size_t count)
{
constexpr limb_type values[digits_per_block_10] = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000};
BOOST_MP_ASSERT(count < digits_per_block_10);
return values[count];
}
// Can't do formatted IO on an __int128
#define BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
#else
using limb_type = detail::largest_unsigned_type<32>::type;
using signed_limb_type = detail::largest_signed_type<32>::type ;
using double_limb_type = detail::largest_unsigned_type<64>::type;
using signed_double_limb_type = detail::largest_signed_type<64>::type ;
constexpr limb_type max_block_10 = 1000000000;
constexpr limb_type digits_per_block_10 = 9;
inline limb_type block_multiplier(std::size_t count)
{
constexpr limb_type values[digits_per_block_10] = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
BOOST_MP_ASSERT(count < digits_per_block_10);
return values[count];
}
#endif
constexpr std::size_t bits_per_limb = sizeof(limb_type) * CHAR_BIT;
template <class T>
inline BOOST_MP_CXX14_CONSTEXPR void minmax(const T& a, const T& b, T& aa, T& bb)
{
if (a < b)
{
aa = a;
bb = b;
}
else
{
aa = b;
bb = a;
}
}
} // namespace multiprecision
} // namespace boost
#endif // BOOST_MP_CPP_INT_CONFIG_HPP

View file

@ -0,0 +1,703 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Comparison operators for cpp_int_backend:
//
#ifndef BOOST_MP_CPP_INT_DIVIDE_HPP
#define BOOST_MP_CPP_INT_DIVIDE_HPP
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#include <boost/multiprecision/detail/assert.hpp>
namespace boost { namespace multiprecision { namespace backends {
template <class CppInt1, class CppInt2, class CppInt3>
BOOST_MP_CXX14_CONSTEXPR void divide_unsigned_helper(
CppInt1* result,
const CppInt2& x,
const CppInt3& y,
CppInt1& r)
{
if (((void*)result == (void*)&x) || ((void*)&r == (void*)&x))
{
CppInt2 t(x);
divide_unsigned_helper(result, t, y, r);
return;
}
if (((void*)result == (void*)&y) || ((void*)&r == (void*)&y))
{
CppInt3 t(y);
divide_unsigned_helper(result, x, t, r);
return;
}
/*
Very simple, fairly braindead long division.
Start by setting the remainder equal to x, and the
result equal to 0. Then in each loop we calculate our
"best guess" for how many times y divides into r,
add our guess to the result, and subtract guess*y
from the remainder r. One wrinkle is that the remainder
may go negative, in which case we subtract the current guess
from the result rather than adding. The value of the guess
is determined by dividing the most-significant-limb of the
current remainder by the most-significant-limb of y.
Note that there are more efficient algorithms than this
available, in particular see Knuth Vol 2. However for small
numbers of limbs this generally outperforms the alternatives
and avoids the normalisation step which would require extra storage.
*/
using default_ops::eval_subtract;
if (result == &r)
{
CppInt1 rem;
divide_unsigned_helper(result, x, y, rem);
r = rem;
return;
}
//
// Find the most significant words of numerator and denominator.
//
std::size_t y_order = y.size() - 1;
if (y_order == 0)
{
//
// Only a single non-zero limb in the denominator, in this case
// we can use a specialized divide-by-single-limb routine which is
// much faster. This also handles division by zero:
//
divide_unsigned_helper(result, x, y.limbs()[y_order], r);
return;
}
typename CppInt2::const_limb_pointer px = x.limbs();
typename CppInt3::const_limb_pointer py = y.limbs();
std::size_t r_order = x.size() - 1;
if ((r_order == 0) && (*px == 0))
{
// x is zero, so is the result:
r = x;
if (result)
*result = x;
return;
}
r = x;
r.sign(false);
if (result)
*result = static_cast<limb_type>(0u);
//
// Check if the remainder is already less than the divisor, if so
// we already have the result. Note we try and avoid a full compare
// if we can:
//
if (r_order <= y_order)
{
if ((r_order < y_order) || (r.compare_unsigned(y) < 0))
{
return;
}
}
CppInt1 t;
bool r_neg = false;
//
// See if we can short-circuit long division, and use basic arithmetic instead:
//
if (r_order == 0)
{
if (result)
{
*result = px[0] / py[0];
}
r = px[0] % py[0];
return;
}
else if (r_order == 1)
{
double_limb_type a = (static_cast<double_limb_type>(px[1]) << CppInt1::limb_bits) | px[0];
double_limb_type b = y_order ? (static_cast<double_limb_type>(py[1]) << CppInt1::limb_bits) | py[0]
: py[0];
if (result)
{
*result = a / b;
}
r = a % b;
return;
}
//
// prepare result:
//
if (result)
result->resize(1 + r_order - y_order, 1 + r_order - y_order);
typename CppInt1::const_limb_pointer prem = r.limbs();
// This is initialised just to keep the compiler from emitting useless warnings later on:
typename CppInt1::limb_pointer pr = typename CppInt1::limb_pointer();
if (result)
{
pr = result->limbs();
for (std::size_t i = 1; i < 1 + r_order - y_order; ++i)
pr[i] = 0;
}
bool first_pass = true;
do
{
//
// Calculate our best guess for how many times y divides into r:
//
limb_type guess = 1;
if ((prem[r_order] <= py[y_order]) && (r_order > 0))
{
double_limb_type a = (static_cast<double_limb_type>(prem[r_order]) << CppInt1::limb_bits) | prem[r_order - 1];
double_limb_type b = py[y_order];
double_limb_type v = a / b;
if (v <= CppInt1::max_limb_value)
{
guess = static_cast<limb_type>(v);
--r_order;
}
}
else if (r_order == 0)
{
guess = prem[0] / py[y_order];
}
else
{
double_limb_type a = (static_cast<double_limb_type>(prem[r_order]) << CppInt1::limb_bits) | prem[r_order - 1];
double_limb_type b = (y_order > 0) ? (static_cast<double_limb_type>(py[y_order]) << CppInt1::limb_bits) | py[y_order - 1] : (static_cast<double_limb_type>(py[y_order]) << CppInt1::limb_bits);
BOOST_MP_ASSERT(b);
double_limb_type v = a / b;
guess = static_cast<limb_type>(v);
}
BOOST_MP_ASSERT(guess); // If the guess ever gets to zero we go on forever....
//
// Update result:
//
std::size_t shift = r_order - y_order;
if (result)
{
if (r_neg)
{
if (pr[shift] > guess)
pr[shift] -= guess;
else
{
t.resize(shift + 1, shift + 1);
t.limbs()[shift] = guess;
for (std::size_t i = 0; i < shift; ++i)
t.limbs()[i] = 0;
eval_subtract(*result, t);
}
}
else if (CppInt1::max_limb_value - pr[shift] > guess)
pr[shift] += guess;
else
{
t.resize(shift + 1, shift + 1);
t.limbs()[shift] = guess;
for (std::size_t i = 0; i < shift; ++i)
t.limbs()[i] = 0;
eval_add(*result, t);
}
}
//
// Calculate guess * y, we use a fused mutiply-shift O(N) for this
// rather than a full O(N^2) multiply:
//
double_limb_type carry = 0;
t.resize(y.size() + shift + 1, y.size() + shift);
bool truncated_t = (t.size() != y.size() + shift + 1);
typename CppInt1::limb_pointer pt = t.limbs();
for (std::size_t i = 0; i < shift; ++i)
pt[i] = 0;
for (std::size_t i = 0; i < y.size(); ++i)
{
carry += static_cast<double_limb_type>(py[i]) * static_cast<double_limb_type>(guess);
#ifdef __MSVC_RUNTIME_CHECKS
pt[i + shift] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
#else
pt[i + shift] = static_cast<limb_type>(carry);
#endif
carry >>= CppInt1::limb_bits;
}
if (carry && !truncated_t)
{
#ifdef __MSVC_RUNTIME_CHECKS
pt[t.size() - 1] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
#else
pt[t.size() - 1] = static_cast<limb_type>(carry);
#endif
}
else if (!truncated_t)
{
t.resize(t.size() - 1, t.size() - 1);
}
//
// Update r in a way that won't actually produce a negative result
// in case the argument types are unsigned:
//
if (truncated_t && carry)
{
// We need to calculate 2^n + t - r
// where n is the number of bits in this type.
// Simplest way is to get 2^n - r by complementing
// r, then add t to it. Note that we can't call eval_complement
// in case this is a signed checked type:
for (std::size_t i = 0; i <= r_order; ++i)
r.limbs()[i] = ~prem[i];
r.normalize();
eval_increment(r);
eval_add(r, t);
r_neg = !r_neg;
}
else if (r.compare(t) > 0)
{
eval_subtract(r, t);
}
else
{
r.swap(t);
eval_subtract(r, t);
prem = r.limbs();
r_neg = !r_neg;
}
//
// First time through we need to strip any leading zero, otherwise
// the termination condition goes belly-up:
//
if (result && first_pass)
{
first_pass = false;
while (pr[result->size() - 1] == 0)
result->resize(result->size() - 1, result->size() - 1);
}
//
// Update r_order:
//
r_order = r.size() - 1;
if (r_order < y_order)
break;
}
// Termination condition is really just a check that r > y, but with a common
// short-circuit case handled first:
while ((r_order > y_order) || (r.compare_unsigned(y) >= 0));
//
// We now just have to normalise the result:
//
if (r_neg && eval_get_sign(r))
{
// We have one too many in the result:
if (result)
eval_decrement(*result);
if (y.sign())
{
r.negate();
eval_subtract(r, y);
}
else
eval_subtract(r, y, r);
}
BOOST_MP_ASSERT(r.compare_unsigned(y) < 0); // remainder must be less than the divisor or our code has failed
}
template <class CppInt1, class CppInt2>
BOOST_MP_CXX14_CONSTEXPR void divide_unsigned_helper(
CppInt1* result,
const CppInt2& x,
limb_type y,
CppInt1& r)
{
if (((void*)result == (void*)&x) || ((void*)&r == (void*)&x))
{
CppInt2 t(x);
divide_unsigned_helper(result, t, y, r);
return;
}
if (result == &r)
{
CppInt1 rem;
divide_unsigned_helper(result, x, y, rem);
r = rem;
return;
}
// As above, but simplified for integer divisor:
using default_ops::eval_subtract;
if (y == 0)
{
BOOST_MP_THROW_EXCEPTION(std::overflow_error("Integer Division by zero."));
}
//
// Find the most significant word of numerator.
//
std::size_t r_order = x.size() - 1;
//
// Set remainder and result to their initial values:
//
r = x;
r.sign(false);
typename CppInt1::limb_pointer pr = r.limbs();
//
// check for x < y, try to do this without actually having to
// do a full comparison:
//
if ((r_order == 0) && (*pr < y))
{
if (result)
*result = static_cast<limb_type>(0u);
return;
}
//
// See if we can short-circuit long division, and use basic arithmetic instead:
//
if (r_order == 0)
{
if (result)
{
*result = *pr / y;
result->sign(x.sign());
}
*pr %= y;
r.sign(x.sign());
return;
}
else if (r_order == 1)
{
double_limb_type a = (static_cast<double_limb_type>(pr[r_order]) << CppInt1::limb_bits) | pr[0];
if (result)
{
*result = a / y;
result->sign(x.sign());
}
r = a % y;
r.sign(x.sign());
return;
}
// This is initialised just to keep the compiler from emitting useless warnings later on:
typename CppInt1::limb_pointer pres = typename CppInt1::limb_pointer();
if (result)
{
result->resize(r_order + 1, r_order + 1);
pres = result->limbs();
if (result->size() > r_order)
pres[r_order] = 0; // just in case we don't set the most significant limb below.
}
do
{
//
// Calculate our best guess for how many times y divides into r:
//
if ((pr[r_order] < y) && r_order)
{
double_limb_type a = (static_cast<double_limb_type>(pr[r_order]) << CppInt1::limb_bits) | pr[r_order - 1];
double_limb_type b = a % y;
r.resize(r.size() - 1, r.size() - 1);
--r_order;
pr[r_order] = static_cast<limb_type>(b);
if (result)
pres[r_order] = static_cast<limb_type>(a / y);
if (r_order && pr[r_order] == 0)
{
--r_order; // No remainder, division was exact.
r.resize(r.size() - 1, r.size() - 1);
if (result)
pres[r_order] = static_cast<limb_type>(0u);
}
}
else
{
if (result)
pres[r_order] = pr[r_order] / y;
pr[r_order] %= y;
if (r_order && pr[r_order] == 0)
{
--r_order; // No remainder, division was exact.
r.resize(r.size() - 1, r.size() - 1);
if (result)
pres[r_order] = static_cast<limb_type>(0u);
}
}
}
// Termination condition is really just a check that r >= y, but with two common
// short-circuit cases handled first:
while (r_order || (pr[r_order] >= y));
if (result)
{
result->normalize();
result->sign(x.sign());
}
r.normalize();
r.sign(x.sign());
BOOST_MP_ASSERT(r.compare(y) < 0); // remainder must be less than the divisor or our code has failed
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, std::size_t MinBits3, std::size_t MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
eval_divide(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
{
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
bool s = a.sign() != b.sign();
divide_unsigned_helper(&result, a, b, r);
result.sign(s);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_divide(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
limb_type& b)
{
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
bool s = a.sign();
divide_unsigned_helper(&result, a, b, r);
result.sign(s);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_divide(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
signed_limb_type& b)
{
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
bool s = a.sign() != (b < 0);
divide_unsigned_helper(&result, a, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(b)), r);
result.sign(s);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_divide(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& b)
{
// There is no in place divide:
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
eval_divide(result, a, b);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_divide(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
limb_type b)
{
// There is no in place divide:
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
eval_divide(result, a, b);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_divide(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
signed_limb_type b)
{
// There is no in place divide:
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
eval_divide(result, a, b);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, std::size_t MinBits3, std::size_t MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
{
bool s = a.sign();
if (b.size() == 1)
eval_modulus(result, a, *b.limbs());
else
{
using cpp_int_backend1_type = cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>;
divide_unsigned_helper(static_cast<cpp_int_backend1_type*>(nullptr), a, b, result);
}
result.sign(s);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const limb_type mod)
{
if(mod == 0)
BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
const std::ptrdiff_t n = static_cast<std::ptrdiff_t>(a.size());
const double_limb_type two_n_mod =
static_cast<double_limb_type>
(
static_cast<double_limb_type>(1u) + static_cast<limb_type>(static_cast<limb_type>(~static_cast<limb_type>(0u) - mod) % mod)
);
limb_type res = a.limbs()[n - 1] % mod;
for (std::ptrdiff_t i = n - 2; i >= 0; --i)
res = static_cast<limb_type>(static_cast<double_limb_type>(static_cast<double_limb_type>(res * two_n_mod) + a.limbs()[i]) % mod);
//
// We must not modify result until here in case
// result and a are the same object:
//
result.resize(1, 1);
*result.limbs() = res;
result.sign(a.sign());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
signed_limb_type b)
{
const limb_type t = b < 0 ? static_cast<limb_type>(-b) : static_cast<limb_type>(b);
eval_modulus(result, a, t);
result.sign(a.sign());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& b)
{
// There is no in place divide:
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
eval_modulus(result, a, b);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
limb_type b)
{
// Single limb modulus is in place:
eval_modulus(result, result, b);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
signed_limb_type b)
{
// Single limb modulus is in place:
eval_modulus(result, result, b);
}
//
// Over again for trivial cpp_int's:
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value)>::type
eval_divide(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o)
{
if (!*o.limbs())
BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
*result.limbs() /= *o.limbs();
result.sign(result.sign() != o.sign());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_divide(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o)
{
if (!*o.limbs())
BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
*result.limbs() /= *o.limbs();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o)
{
if (!*o.limbs())
BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
*result.limbs() %= *o.limbs();
result.sign(result.sign());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && std::is_unsigned<V>::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& a,
V o)
{
using local_limb_type = typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::local_limb_type;
BOOST_IF_CONSTEXPR(std::numeric_limits<V>::digits > MaxBits1)
{
if (o >= (static_cast<V>(1u) << MaxBits1))
{
// Modulus is larger than any value that the result can hold,
// so the result is just "a":
result = a;
return;
}
}
if (!o)
BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
*result.limbs() = *a.limbs() % static_cast<local_limb_type>(o);
result.sign(a.sign());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && std::is_signed<V>::value>::type
eval_modulus(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& a,
V o)
{
using unsigned_type = typename std::make_unsigned<V>::type;
eval_modulus(result, a, static_cast<unsigned_type>(o < 0 ? -o : o));
}
}}} // namespace boost::multiprecision::backends
#endif

View file

@ -0,0 +1,248 @@
///////////////////////////////////////////////////////////////
// Copyright 2015 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_CPP_INT_IMPORT_EXPORT_HPP
#define BOOST_MP_CPP_INT_IMPORT_EXPORT_HPP
#include <climits>
#include <cstring>
#include <boost/multiprecision/detail/endian.hpp>
namespace boost {
namespace multiprecision {
namespace detail {
template <class Backend, class Unsigned>
void assign_bits(Backend& val, Unsigned bits, std::size_t bit_location, std::size_t chunk_bits, const std::integral_constant<bool, false>& tag)
{
std::size_t limb = bit_location / (sizeof(limb_type) * CHAR_BIT);
std::size_t shift = bit_location % (sizeof(limb_type) * CHAR_BIT);
limb_type mask = chunk_bits >= sizeof(limb_type) * CHAR_BIT ? ~static_cast<limb_type>(0u) : (static_cast<limb_type>(1u) << chunk_bits) - 1;
limb_type value = static_cast<limb_type>(bits & mask) << shift;
if (value)
{
if (val.size() == limb)
{
val.resize(limb + 1, limb + 1);
if (val.size() > limb)
val.limbs()[limb] = value;
}
else if (val.size() > limb)
val.limbs()[limb] |= value;
}
if (chunk_bits > sizeof(limb_type) * CHAR_BIT - shift)
{
shift = sizeof(limb_type) * CHAR_BIT - shift;
chunk_bits -= shift;
bit_location += shift;
bits >>= shift;
if (bits)
assign_bits(val, bits, bit_location, chunk_bits, tag);
}
}
template <class Backend, class Unsigned>
void assign_bits(Backend& val, Unsigned bits, std::size_t bit_location, std::size_t chunk_bits, const std::integral_constant<bool, true>&)
{
using local_limb_type = typename Backend::local_limb_type;
//
// Check for possible overflow, this may trigger an exception, or have no effect
// depending on whether this is a checked integer or not:
//
if ((bit_location >= sizeof(local_limb_type) * CHAR_BIT) && bits)
val.resize(2, 2);
else
{
local_limb_type mask = chunk_bits >= sizeof(local_limb_type) * CHAR_BIT ? ~static_cast<local_limb_type>(0u) : (static_cast<local_limb_type>(1u) << chunk_bits) - 1;
local_limb_type value = (static_cast<local_limb_type>(bits) & mask) << bit_location;
*val.limbs() |= value;
//
// Check for overflow bits:
//
bit_location = sizeof(local_limb_type) * CHAR_BIT - bit_location;
if ((bit_location < sizeof(bits) * CHAR_BIT) && (bits >>= bit_location))
val.resize(2, 2); // May throw!
}
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
inline void resize_to_bit_size(cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& newval, std::size_t bits, const std::integral_constant<bool, false>&)
{
std::size_t limb_count = static_cast<unsigned>(bits / (sizeof(limb_type) * CHAR_BIT));
if (bits % (sizeof(limb_type) * CHAR_BIT))
++limb_count;
constexpr std::size_t max_limbs = MaxBits ? MaxBits / (CHAR_BIT * sizeof(limb_type)) + ((MaxBits % (CHAR_BIT * sizeof(limb_type))) ? 1 : 0) : (std::numeric_limits<unsigned>::max)();
if (limb_count > max_limbs)
limb_count = max_limbs;
newval.resize(limb_count, limb_count);
std::memset(newval.limbs(), 0, newval.size() * sizeof(limb_type));
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
inline void resize_to_bit_size(cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& newval, unsigned, const std::integral_constant<bool, true>&)
{
*newval.limbs() = 0;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, expression_template_option ExpressionTemplates, class Iterator>
number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>&
import_bits_generic(
number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val, Iterator i, Iterator j, std::size_t chunk_size = 0, bool msv_first = true)
{
typename number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>::backend_type newval;
using value_type = typename std::iterator_traits<Iterator>::value_type ;
using unsigned_value_type = typename boost::multiprecision::detail::make_unsigned<value_type>::type ;
using difference_type = typename std::iterator_traits<Iterator>::difference_type ;
using size_type = typename boost::multiprecision::detail::make_unsigned<difference_type>::type ;
using tag_type = typename cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>::trivial_tag;
if (!chunk_size)
chunk_size = std::numeric_limits<value_type>::digits;
size_type limbs = std::distance(i, j);
size_type bits = limbs * chunk_size;
detail::resize_to_bit_size(newval, static_cast<unsigned>(bits), tag_type());
difference_type bit_location = msv_first ? bits - chunk_size : 0;
difference_type bit_location_change = msv_first ? -static_cast<difference_type>(chunk_size) : chunk_size;
while (i != j)
{
detail::assign_bits(newval, static_cast<unsigned_value_type>(*i), static_cast<std::size_t>(bit_location), chunk_size, tag_type());
++i;
bit_location += bit_location_change;
}
newval.normalize();
val.backend().swap(newval);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, expression_template_option ExpressionTemplates, class T>
inline typename std::enable_if< !boost::multiprecision::backends::is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value, number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>&>::type
import_bits_fast(
number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val, T* i, T* j, std::size_t chunk_size = 0)
{
std::size_t byte_len = (j - i) * (chunk_size ? chunk_size / CHAR_BIT : sizeof(*i));
std::size_t limb_len = byte_len / sizeof(limb_type);
if (byte_len % sizeof(limb_type))
++limb_len;
cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& result = val.backend();
result.resize(static_cast<unsigned>(limb_len), static_cast<unsigned>(limb_len)); // checked types may throw here if they're not large enough to hold the data!
result.limbs()[result.size() - 1] = 0u;
std::memcpy(result.limbs(), i, (std::min)(byte_len, result.size() * sizeof(limb_type)));
result.normalize(); // In case data has leading zeros.
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, expression_template_option ExpressionTemplates, class T>
inline typename std::enable_if<boost::multiprecision::backends::is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value, number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>&>::type
import_bits_fast(
number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val, T* i, T* j, std::size_t chunk_size = 0)
{
cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& result = val.backend();
std::size_t byte_len = (j - i) * (chunk_size ? chunk_size / CHAR_BIT : sizeof(*i));
std::size_t limb_len = byte_len / sizeof(result.limbs()[0]);
if (byte_len % sizeof(result.limbs()[0]))
++limb_len;
result.limbs()[0] = 0u;
result.resize(static_cast<unsigned>(limb_len), static_cast<unsigned>(limb_len)); // checked types may throw here if they're not large enough to hold the data!
std::memcpy(result.limbs(), i, (std::min)(byte_len, result.size() * sizeof(result.limbs()[0])));
result.normalize(); // In case data has leading zeros.
return val;
}
} // namespace detail
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, expression_template_option ExpressionTemplates, class Iterator>
inline number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>&
import_bits(
number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val, Iterator i, Iterator j, std::size_t chunk_size = 0, bool msv_first = true)
{
return detail::import_bits_generic(val, i, j, chunk_size, msv_first);
}
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, expression_template_option ExpressionTemplates, class T>
inline number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>&
import_bits(
number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val, T* i, T* j, std::size_t chunk_size = 0, bool msv_first = true)
{
#if BOOST_MP_ENDIAN_LITTLE_BYTE
if (((chunk_size % CHAR_BIT) == 0) && !msv_first && (sizeof(*i) * CHAR_BIT == chunk_size))
return detail::import_bits_fast(val, i, j, chunk_size);
#endif
return detail::import_bits_generic(val, i, j, chunk_size, msv_first);
}
namespace detail {
template <class Backend>
std::uintmax_t extract_bits(const Backend& val, std::size_t location, std::size_t count, const std::integral_constant<bool, false>& tag)
{
std::size_t limb = location / (sizeof(limb_type) * CHAR_BIT);
std::size_t shift = location % (sizeof(limb_type) * CHAR_BIT);
std::uintmax_t result = 0;
std::uintmax_t mask = count == std::numeric_limits<std::uintmax_t>::digits ? ~static_cast<std::uintmax_t>(0) : (static_cast<std::uintmax_t>(1u) << count) - 1;
if (count > (sizeof(limb_type) * CHAR_BIT - shift))
{
result = extract_bits(val, location + sizeof(limb_type) * CHAR_BIT - shift, count - sizeof(limb_type) * CHAR_BIT + shift, tag);
result <<= sizeof(limb_type) * CHAR_BIT - shift;
}
if (limb < val.size())
result |= (val.limbs()[limb] >> shift) & mask;
return result;
}
template <class Backend>
inline std::uintmax_t extract_bits(const Backend& val, std::size_t location, std::size_t count, const std::integral_constant<bool, true>&)
{
typename Backend::local_limb_type result = *val.limbs();
typename Backend::local_limb_type mask = count >= std::numeric_limits<typename Backend::local_limb_type>::digits ? ~static_cast<typename Backend::local_limb_type>(0) : (static_cast<typename Backend::local_limb_type>(1u) << count) - 1;
return (result >> location) & mask;
}
} // namespace detail
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, expression_template_option ExpressionTemplates, class OutputIterator>
OutputIterator export_bits(
const number<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>& val, OutputIterator out, std::size_t chunk_size, bool msv_first = true)
{
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4244)
#endif
using tag_type = typename cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>::trivial_tag;
if (!val)
{
*out = 0;
++out;
return out;
}
std::size_t bitcount = boost::multiprecision::backends::eval_msb_imp(val.backend()) + 1;
std::ptrdiff_t bit_location = msv_first ? static_cast<std::ptrdiff_t>(bitcount - chunk_size) : 0;
const std::ptrdiff_t bit_step = msv_first ? static_cast<std::ptrdiff_t>(-static_cast<std::ptrdiff_t>(chunk_size)) : static_cast<std::ptrdiff_t>(chunk_size);
while (bit_location % bit_step)
++bit_location;
do
{
*out = detail::extract_bits(val.backend(), bit_location, chunk_size, tag_type());
++out;
bit_location += bit_step;
} while ((bit_location >= 0) && (bit_location < static_cast<int>(bitcount)));
return out;
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
}
}
} // namespace boost::multiprecision
#endif // BOOST_MP_CPP_INT_IMPORT_EXPORT_HPP

View file

@ -0,0 +1,138 @@
///////////////////////////////////////////////////////////////
// Copyright 2020 Madhur Chauhan.
// Copyright 2020 John Maddock.
// Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_INTEL_INTRINSICS_HPP
#define BOOST_MP_INTEL_INTRINSICS_HPP
//
// Select which actual implementation header to use:
//
#ifdef __has_include
#if __has_include(<immintrin.h>)
#define BOOST_MP_HAS_IMMINTRIN_H
#endif
#endif
//
// If this is GCC/clang, then check that the actual intrinsic exists:
//
#if defined(__has_builtin) && defined(__GNUC__)
#if !__has_builtin(__builtin_ia32_addcarryx_u64) && defined(BOOST_MP_HAS_IMMINTRIN_H) \
&& !(defined(BOOST_GCC) && (__GNUC__ >= 9) \
&& (defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)\
|| defined(i386) || defined(__i386) || defined(__i386__) || defined(_M_AMD64) \
|| defined(_M_X64) || defined(__amd64__) || defined(_M_X64)))
#undef BOOST_MP_HAS_IMMINTRIN_H
#endif
#elif defined(BOOST_MP_HAS_IMMINTRIN_H) && defined(__GNUC__) && !(defined(BOOST_GCC) && (__GNUC__ >= 9))
#undef BOOST_MP_HAS_IMMINTRIN_H
#endif
#if defined(__clang_major__) && (__clang_major__ < 9)
// We appear to crash the compiler if we try to use these intrinsics?
#undef BOOST_MP_HAS_IMMINTRIN_H
#endif
#if defined(_WIN32) && (defined(_M_ARM64) || defined(_M_ARM))
//
// When targeting platforms such as ARM, msvc (and also clang when emulating msvc) still has the
// Intel headers in its include path even though they're not usable.
// See https://github.com/boostorg/multiprecision/issues/321
// Also https://github.com/boostorg/multiprecision/issues/475
//
#undef BOOST_MP_HAS_IMMINTRIN_H
#endif
#if defined(__APPLE_CC__) && defined(__clang_major__) && (__clang_major__ < 11) && defined(BOOST_MP_HAS_IMMINTRIN_H)
// Apple clang has it's own version numbers.
#undef BOOST_MP_HAS_IMMINTRIN_H
#endif
//
// If the compiler supports the intrinsics used by GCC internally
// inside <immintrin.h> then we'll use them directly.
// This is a bit of defensive programming, mostly for a modern clang
// sitting on top of an older GCC header install.
//
#if defined(__has_builtin) && !defined(BOOST_INTEL)
# if __has_builtin(__builtin_ia32_addcarryx_u64)
# define BOOST_MP_ADDC __builtin_ia32_addcarryx_u
# endif
# if __has_builtin(__builtin_ia32_subborrow_u64)
# define BOOST_MP_SUBB __builtin_ia32_subborrow_u
# elif __has_builtin(__builtin_ia32_sbb_u64)
# define BOOST_MP_SUBB __builtin_ia32_sbb_u
# endif
#endif
#ifndef BOOST_MP_ADDC
#define BOOST_MP_ADDC _addcarry_u
#endif
#ifndef BOOST_MP_SUBB
#define BOOST_MP_SUBB _subborrow_u
#endif
#ifdef BOOST_MP_HAS_IMMINTRIN_H
#ifdef BOOST_MSVC
//
// This is a subset of the full <immintrin.h> :
//
#include <intrin.h>
#else
#include <immintrin.h>
#endif
#if defined(BOOST_HAS_INT128)
namespace boost { namespace multiprecision { namespace detail {
BOOST_MP_FORCEINLINE unsigned char addcarry_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
{
#ifdef BOOST_INTEL
using cast_type = unsigned __int64;
#else
using cast_type = unsigned long long;
#endif
return BOOST_JOIN(BOOST_MP_ADDC, 64)(carry, a, b, reinterpret_cast<cast_type*>(p_result));
}
BOOST_MP_FORCEINLINE unsigned char subborrow_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
{
#ifdef BOOST_INTEL
using cast_type = unsigned __int64;
#else
using cast_type = unsigned long long;
#endif
return BOOST_JOIN(BOOST_MP_SUBB, 64)(carry, a, b, reinterpret_cast<cast_type*>(p_result));
}
}}} // namespace boost::multiprecision::detail
#else
namespace boost { namespace multiprecision { namespace detail {
BOOST_MP_FORCEINLINE unsigned char addcarry_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
{
return BOOST_JOIN(BOOST_MP_ADDC, 32)(carry, a, b, reinterpret_cast<unsigned int*>(p_result));
}
BOOST_MP_FORCEINLINE unsigned char subborrow_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
{
return BOOST_JOIN(BOOST_MP_SUBB, 32)(carry, a, b, reinterpret_cast<unsigned int*>(p_result));
}
}}} // namespace boost::multiprecision::detail
#endif
#endif
#endif

View file

@ -0,0 +1,282 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Comparison operators for cpp_int_backend:
//
#ifndef BOOST_MP_CPP_INT_LIMITS_HPP
#define BOOST_MP_CPP_INT_LIMITS_HPP
#include <boost/multiprecision/traits/max_digits10.hpp>
namespace std {
namespace detail {
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4307)
#endif
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
inline BOOST_CXX14_CONSTEXPR_IF_DETECTION boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_min(const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&)
{
// Bounded, signed, and no allocator.
using result_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> ;
using ui_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MaxBits, MaxBits, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked>, ExpressionTemplates>;
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
static
#else
constexpr
#endif
const result_type val = -result_type(~ui_type(0));
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_min(const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&)
{
// Bounded, signed, and an allocator (can't be constexpr).
using result_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> ;
using ui_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MaxBits, MaxBits, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked>, ExpressionTemplates>;
static const result_type val = -result_type(~ui_type(0));
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
inline BOOST_CXX14_CONSTEXPR_IF_DETECTION boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_min(const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&)
{
// Bounded, unsigned, no allocator (can be constexpr):
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
static
#else
constexpr
#endif
const boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> val(0u);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_min(const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&)
{
// Bounded and std::size_t with allocator (no constexpr):
static const boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> val(0u);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates, bool has_allocator>
inline boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_min(const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&, const std::integral_constant<bool, has_allocator>&)
{
// Unbounded and signed, never constexpr because there must be an allocator.
// There is no minimum value, just return 0:
static const boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> val(0u);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates, bool has_allocator>
inline boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_min(const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&, const std::integral_constant<bool, has_allocator>&)
{
// Unbound and unsigned, never constexpr because there must be an allocator.
static const boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> val(0u);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
inline BOOST_CXX14_CONSTEXPR_IF_DETECTION boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_max(const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&)
{
// Bounded and signed, no allocator, can be constexpr.
using result_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> ;
using ui_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MaxBits, MaxBits, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked>, ExpressionTemplates>;
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
static
#else
constexpr
#endif
const result_type val = ~ui_type(0);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_max(const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&)
{
// Bounded and signed, has an allocator, never constexpr.
using result_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> ;
using ui_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MaxBits, MaxBits, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked>, ExpressionTemplates>;
static const result_type val = ~ui_type(0);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
inline BOOST_CXX14_CONSTEXPR_IF_DETECTION boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_max(const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&)
{
// Bound and unsigned, no allocator so can be constexpr:
using result_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> ;
using ui_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, Allocator>, ExpressionTemplates>;
#ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
static
#else
constexpr
#endif
const result_type val = ~ui_type(0);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_max(const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&)
{
// Bound and unsigned, has an allocator so can never be constexpr:
using result_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> ;
using ui_type = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, Allocator>, ExpressionTemplates>;
static const result_type val = ~ui_type(0);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates, bool has_allocator>
inline boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_max(const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&, const std::integral_constant<bool, has_allocator>&)
{
// Unbounded and signed.
// There is no maximum value, just return 0:
static const boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> val(0u);
return val;
}
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates, bool has_allocator>
inline boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates>
get_max(const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&, const std::integral_constant<bool, has_allocator>&)
{
// Unbound and unsigned:
static const boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> val(0u);
return val;
}
} // namespace detail
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
class numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >
{
using backend_type = boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
using number_type = boost::multiprecision::number<backend_type, ExpressionTemplates> ;
public:
static constexpr bool is_specialized = true;
//
// Largest and smallest numbers are bounded only by available memory, set
// to zero:
//
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type(min)()
{
return detail::get_min<MinBits, MaxBits, SignType, Checked, Allocator, ExpressionTemplates>(boost::multiprecision::backends::is_fixed_precision<backend_type>(), boost::multiprecision::is_signed_number<backend_type>(), std::integral_constant<bool, std::is_void<Allocator>::value>());
}
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type(max)()
{
return detail::get_max<MinBits, MaxBits, SignType, Checked, Allocator, ExpressionTemplates>(boost::multiprecision::backends::is_fixed_precision<backend_type>(), boost::multiprecision::is_signed_number<backend_type>(), std::integral_constant<bool, std::is_void<Allocator>::value>());
}
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type lowest() { return (min)(); }
static constexpr int digits = boost::multiprecision::backends::max_precision<backend_type>::value == SIZE_MAX ? INT_MAX : boost::multiprecision::backends::max_precision<backend_type>::value;
static constexpr int digits10 = static_cast<int>(boost::multiprecision::detail::calc_digits10_s<static_cast<std::size_t>(digits)>::value);
static constexpr int max_digits10 = static_cast<int>(boost::multiprecision::detail::calc_max_digits10_s<static_cast<std::size_t>(digits)>::value);
static constexpr bool is_signed = boost::multiprecision::is_signed_number<backend_type>::value;
static constexpr bool is_integer = true;
static constexpr bool is_exact = true;
static constexpr int radix = 2;
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type epsilon() { return 0; }
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type round_error() { return 0; }
static constexpr int min_exponent = 0;
static constexpr int min_exponent10 = 0;
static constexpr int max_exponent = 0;
static constexpr int max_exponent10 = 0;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4996)
#endif
static constexpr float_denorm_style has_denorm = denorm_absent;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
static constexpr bool has_denorm_loss = false;
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type infinity() { return 0; }
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type quiet_NaN() { return 0; }
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type signaling_NaN() { return 0; }
static BOOST_CXX14_CONSTEXPR_IF_DETECTION number_type denorm_min() { return 0; }
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = boost::multiprecision::backends::is_fixed_precision<backend_type>::value;
static constexpr bool is_modulo = (boost::multiprecision::backends::is_fixed_precision<backend_type>::value && (Checked == boost::multiprecision::unchecked));
static constexpr bool traps = false;
static constexpr bool tinyness_before = false;
static constexpr float_round_style round_style = round_toward_zero;
};
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::digits;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::digits10;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::max_digits10;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::is_signed;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::is_integer;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::is_exact;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::radix;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::min_exponent;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::min_exponent10;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::max_exponent;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::max_exponent10;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::has_infinity;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::has_quiet_NaN;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::has_signaling_NaN;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4996)
#endif
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::has_denorm;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::has_denorm_loss;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::is_iec559;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::is_bounded;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::is_modulo;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::traps;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::tinyness_before;
template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >::round_style;
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
} // namespace std
#endif

View file

@ -0,0 +1,295 @@
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_CPP_INT_LITERALS_HPP
#define BOOST_MP_CPP_INT_LITERALS_HPP
#include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
namespace boost { namespace multiprecision {
namespace literals {
namespace detail {
template <char>
struct hex_value;
template <>
struct hex_value<'0'>
{
static constexpr limb_type value = 0;
};
template <>
struct hex_value<'1'>
{
static constexpr limb_type value = 1;
};
template <>
struct hex_value<'2'>
{
static constexpr limb_type value = 2;
};
template <>
struct hex_value<'3'>
{
static constexpr limb_type value = 3;
};
template <>
struct hex_value<'4'>
{
static constexpr limb_type value = 4;
};
template <>
struct hex_value<'5'>
{
static constexpr limb_type value = 5;
};
template <>
struct hex_value<'6'>
{
static constexpr limb_type value = 6;
};
template <>
struct hex_value<'7'>
{
static constexpr limb_type value = 7;
};
template <>
struct hex_value<'8'>
{
static constexpr limb_type value = 8;
};
template <>
struct hex_value<'9'>
{
static constexpr limb_type value = 9;
};
template <>
struct hex_value<'a'>
{
static constexpr limb_type value = 10;
};
template <>
struct hex_value<'b'>
{
static constexpr limb_type value = 11;
};
template <>
struct hex_value<'c'>
{
static constexpr limb_type value = 12;
};
template <>
struct hex_value<'d'>
{
static constexpr limb_type value = 13;
};
template <>
struct hex_value<'e'>
{
static constexpr limb_type value = 14;
};
template <>
struct hex_value<'f'>
{
static constexpr limb_type value = 15;
};
template <>
struct hex_value<'A'>
{
static constexpr limb_type value = 10;
};
template <>
struct hex_value<'B'>
{
static constexpr limb_type value = 11;
};
template <>
struct hex_value<'C'>
{
static constexpr limb_type value = 12;
};
template <>
struct hex_value<'D'>
{
static constexpr limb_type value = 13;
};
template <>
struct hex_value<'E'>
{
static constexpr limb_type value = 14;
};
template <>
struct hex_value<'F'>
{
static constexpr limb_type value = 15;
};
template <class Pack, limb_type value>
struct combine_value_to_pack;
template <limb_type first, limb_type... ARGS, limb_type value>
struct combine_value_to_pack<value_pack<first, ARGS...>, value>
{
using type = value_pack<first | value, ARGS...>;
};
template <char NextChar, char... CHARS>
struct pack_values
{
static constexpr std::size_t chars_per_limb = sizeof(limb_type) * CHAR_BIT / 4;
static constexpr std::size_t shift = ((sizeof...(CHARS)) % chars_per_limb) * 4;
static constexpr limb_type value_to_add = shift ? hex_value<NextChar>::value << shift : hex_value<NextChar>::value;
using recursive_packed_type = typename pack_values<CHARS...>::type ;
using pack_type = typename std::conditional<shift == 0,
typename recursive_packed_type::next_type,
recursive_packed_type>::type;
using type = typename combine_value_to_pack<pack_type, value_to_add>::type;
};
template <char NextChar>
struct pack_values<NextChar>
{
static constexpr limb_type value_to_add = hex_value<NextChar>::value;
using type = value_pack<value_to_add>;
};
template <class T>
struct strip_leading_zeros_from_pack;
template <limb_type... PACK>
struct strip_leading_zeros_from_pack<value_pack<PACK...> >
{
using type = value_pack<PACK...>;
};
template <limb_type... PACK>
struct strip_leading_zeros_from_pack<value_pack<0u, PACK...> >
{
using type = typename strip_leading_zeros_from_pack<value_pack<PACK...> >::type;
};
template <limb_type v, class PACK>
struct append_value_to_pack;
template <limb_type v, limb_type... PACK>
struct append_value_to_pack<v, value_pack<PACK...> >
{
using type = value_pack<PACK..., v>;
};
template <class T>
struct reverse_value_pack;
template <limb_type v, limb_type... VALUES>
struct reverse_value_pack<value_pack<v, VALUES...> >
{
using lead_values = typename reverse_value_pack<value_pack<VALUES...> >::type;
using type = typename append_value_to_pack<v, lead_values>::type ;
};
template <limb_type v>
struct reverse_value_pack<value_pack<v> >
{
using type = value_pack<v>;
};
template <>
struct reverse_value_pack<value_pack<> >
{
using type = value_pack<>;
};
template <char l1, char l2, char... STR>
struct make_packed_value_from_str
{
static_assert(l1 == '0', "Multi-precision integer literals must be in hexadecimal notation.");
static_assert((l2 == 'X') || (l2 == 'x'), "Multi-precision integer literals must be in hexadecimal notation.");
using packed_type = typename pack_values<STR...>::type ;
using stripped_type = typename strip_leading_zeros_from_pack<packed_type>::type;
using type = typename reverse_value_pack<stripped_type>::type ;
};
template <class Pack, class B>
struct make_backend_from_pack
{
static constexpr Pack p = {};
static constexpr B value = p;
};
#if !defined(__cpp_inline_variables)
template <class Pack, class B>
constexpr B make_backend_from_pack<Pack, B>::value;
#endif
template <unsigned Digits>
struct signed_cpp_int_literal_result_type
{
static constexpr unsigned bits = Digits * 4;
using backend_type = boost::multiprecision::backends::cpp_int_backend<bits, bits, signed_magnitude, unchecked, void>;
using number_type = number<backend_type, et_off> ;
};
template <unsigned Digits>
struct unsigned_cpp_int_literal_result_type
{
static constexpr unsigned bits = Digits * 4;
using backend_type = boost::multiprecision::backends::cpp_int_backend<bits, bits, unsigned_magnitude, unchecked, void>;
using number_type = number<backend_type, et_off> ;
};
} // namespace detail
template <char... STR>
constexpr typename boost::multiprecision::literals::detail::signed_cpp_int_literal_result_type<static_cast<unsigned>((sizeof...(STR)) - 2u)>::number_type operator ""_cppi()
{
using pt = typename boost::multiprecision::literals::detail::make_packed_value_from_str<STR...>::type;
return boost::multiprecision::literals::detail::make_backend_from_pack<pt, typename boost::multiprecision::literals::detail::signed_cpp_int_literal_result_type<static_cast<unsigned>((sizeof...(STR)) - 2u)>::backend_type>::value;
}
template <char... STR>
constexpr typename boost::multiprecision::literals::detail::unsigned_cpp_int_literal_result_type<static_cast<unsigned>((sizeof...(STR)) - 2u)>::number_type operator ""_cppui()
{
using pt = typename boost::multiprecision::literals::detail::make_packed_value_from_str<STR...>::type;
return boost::multiprecision::literals::detail::make_backend_from_pack<pt, typename boost::multiprecision::literals::detail::unsigned_cpp_int_literal_result_type<static_cast<unsigned>((sizeof...(STR)) - 2u)>::backend_type>::value;
}
#define BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL(Bits) \
template <char... STR> \
constexpr boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<Bits, Bits, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> > operator BOOST_JOIN(""_cppi, Bits)() \
{ \
using pt = typename boost::multiprecision::literals::detail::make_packed_value_from_str<STR...>::type; \
return boost::multiprecision::literals::detail::make_backend_from_pack< \
pt, \
boost::multiprecision::backends::cpp_int_backend<Bits, Bits, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> >::value; \
} \
template <char... STR> \
constexpr boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<Bits, Bits, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > operator BOOST_JOIN(""_cppui, Bits)() \
{ \
using pt = typename boost::multiprecision::literals::detail::make_packed_value_from_str<STR...>::type; \
return boost::multiprecision::literals::detail::make_backend_from_pack< \
pt, \
boost::multiprecision::backends::cpp_int_backend<Bits, Bits, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> >::value; \
}
BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL(128)
BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL(256)
BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL(512)
BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL(1024)
} // namespace literals
//
// Overload unary minus operator for constexpr use:
//
template <std::size_t MinBits, cpp_int_check_type Checked>
constexpr number<cpp_int_backend<MinBits, MinBits, signed_magnitude, Checked, void>, et_off>
operator-(const number<cpp_int_backend<MinBits, MinBits, signed_magnitude, Checked, void>, et_off>& a)
{
return cpp_int_backend<MinBits, MinBits, signed_magnitude, Checked, void>(a.backend(), boost::multiprecision::literals::detail::make_negate_tag());
}
template <std::size_t MinBits, cpp_int_check_type Checked>
constexpr number<cpp_int_backend<MinBits, MinBits, signed_magnitude, Checked, void>, et_off>
operator-(number<cpp_int_backend<MinBits, MinBits, signed_magnitude, Checked, void>, et_off>&& a)
{
return cpp_int_backend<MinBits, MinBits, signed_magnitude, Checked, void>(static_cast<const number<cpp_int_backend<MinBits, MinBits, signed_magnitude, Checked, void>, et_off>&>(a).backend(), boost::multiprecision::literals::detail::make_negate_tag());
}
}} // namespace boost::multiprecision
#endif // BOOST_MP_CPP_INT_CORE_HPP

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,848 @@
///////////////////////////////////////////////////////////////
// Copyright 2012-20 John Maddock.
// Copyright 2019-20 Christopher Kormanyos.
// Copyright 2019-20 Madhur Chauhan.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Comparison operators for cpp_int_backend:
//
#ifndef BOOST_MP_CPP_INT_MULTIPLY_HPP
#define BOOST_MP_CPP_INT_MULTIPLY_HPP
#include <limits>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/endian.hpp>
#include <boost/multiprecision/detail/assert.hpp>
#include <boost/multiprecision/integer.hpp>
namespace boost { namespace multiprecision { namespace backends {
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4127) // conditional expression is constant
#endif
//
// Multiplication by a single limb:
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const limb_type& val) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
if (!val)
{
result = static_cast<limb_type>(0);
return;
}
if ((void*)&a != (void*)&result)
result.resize(a.size(), a.size());
double_limb_type carry = 0;
typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_pointer p = result.limbs();
typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_pointer pe = result.limbs() + result.size();
typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::const_limb_pointer pa = a.limbs();
while (p != pe)
{
carry += static_cast<double_limb_type>(*pa) * static_cast<double_limb_type>(val);
#ifdef __MSVC_RUNTIME_CHECKS
*p = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
#else
*p = static_cast<limb_type>(carry);
#endif
carry >>= cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_bits;
++p, ++pa;
}
if (carry)
{
std::size_t i = result.size();
result.resize(i + 1, i + 1);
if (result.size() > i)
result.limbs()[i] = static_cast<limb_type>(carry);
}
result.sign(a.sign());
if (is_fixed_precision<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value)
result.normalize();
}
//
// resize_for_carry forces a resize of the underlying buffer only if a previous request
// for "required" elements could possibly have failed, *and* we have checking enabled.
// This will cause an overflow error inside resize():
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
inline BOOST_MP_CXX14_CONSTEXPR void resize_for_carry(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& /*result*/, std::size_t /*required*/) {}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, class Allocator1>
inline BOOST_MP_CXX14_CONSTEXPR void resize_for_carry(cpp_int_backend<MinBits1, MaxBits1, SignType1, checked, Allocator1>& result, std::size_t required)
{
if (result.size() < required)
result.resize(required, required);
}
//
// Minimum number of limbs required for Karatsuba to be worthwhile:
//
#ifdef BOOST_MP_KARATSUBA_CUTOFF
const size_t karatsuba_cutoff = BOOST_MP_KARATSUBA_CUTOFF;
#else
const size_t karatsuba_cutoff = 40;
#endif
//
// Core (recursive) Karatsuba multiplication, all the storage required is allocated upfront and
// passed down the stack in this routine. Note that all the cpp_int_backend's must be the same type
// and full variable precision. Karatsuba really doesn't play nice with fixed-size integers. If necessary
// fixed precision integers will get aliased as variable-precision types before this is called.
//
template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
inline void multiply_karatsuba(
cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& result,
const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& a,
const cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>& b,
typename cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>::scoped_shared_storage& storage)
{
using cpp_int_type = cpp_int_backend<MinBits, MaxBits, signed_magnitude, Checked, Allocator>;
std::size_t as = a.size();
std::size_t bs = b.size();
//
// Termination condition: if either argument is smaller than karatsuba_cutoff
// then schoolboy multiplication will be faster:
//
if ((as < karatsuba_cutoff) || (bs < karatsuba_cutoff))
{
eval_multiply(result, a, b);
return;
}
//
// Partitioning size: split the larger of a and b into 2 halves
//
std::size_t n = (as > bs ? as : bs) / 2 + 1;
//
// Partition a and b into high and low parts.
// ie write a, b as a = a_h * 2^n + a_l, b = b_h * 2^n + b_l
//
// We could copy the high and low parts into new variables, but we'll
// use aliasing to reference the internal limbs of a and b. There is one wart here:
// if a and b are mismatched in size, then n may be larger than the smaller
// of a and b. In that situation the high part is zero, and we have no limbs
// to alias, so instead alias a local variable.
// This raises 2 questions:
// * Is this the best way to partition a and b?
// * Since we have one high part zero, the arithmetic simplifies considerably,
// so should we have a special routine for this?
//
std::size_t sz = (std::min)(as, n);
const cpp_int_type a_l(a.limbs(), 0, sz);
sz = (std::min)(bs, n);
const cpp_int_type b_l(b.limbs(), 0, sz);
limb_type zero = 0;
const cpp_int_type a_h(as > n ? a.limbs() + n : &zero, 0, as > n ? as - n : 1);
const cpp_int_type b_h(bs > n ? b.limbs() + n : &zero, 0, bs > n ? bs - n : 1);
//
// The basis for the Karatsuba algorithm is as follows:
//
// let x = a_h * b_ h
// y = a_l * b_l
// z = (a_h + a_l)*(b_h + b_l) - x - y
// and therefore a * b = x * (2 ^ (2 * n))+ z * (2 ^ n) + y
//
// Begin by allocating our temporaries, these alias the memory already allocated in the shared storage:
//
cpp_int_type t1(storage, 2 * n + 2);
cpp_int_type t2(storage, n + 1);
cpp_int_type t3(storage, n + 1);
//
// Now we want:
//
// result = | a_h*b_h | a_l*b_l |
// (bits) <-- 2*n -->
//
// We create aliases for the low and high parts of result, and multiply directly into them:
//
cpp_int_type result_low(result.limbs(), 0, 2 * n);
cpp_int_type result_high(result.limbs(), 2 * n, result.size() - 2 * n);
//
// low part of result is a_l * b_l:
//
multiply_karatsuba(result_low, a_l, b_l, storage);
//
// We haven't zeroed out memory in result, so set to zero any unused limbs,
// if a_l and b_l have mostly random bits then nothing happens here, but if
// one is zero or nearly so, then a memset might be faster... it's not clear
// that it's worth the extra logic though (and is darn hard to measure
// what the "average" case is).
//
for (std::size_t i = result_low.size(); i < 2 * n; ++i)
result.limbs()[i] = 0;
//
// Set the high part of result to a_h * b_h:
//
multiply_karatsuba(result_high, a_h, b_h, storage);
for (std::size_t i = result_high.size() + 2 * n; i < result.size(); ++i)
result.limbs()[i] = 0;
//
// Now calculate (a_h+a_l)*(b_h+b_l):
//
add_unsigned(t2, a_l, a_h);
add_unsigned(t3, b_l, b_h);
multiply_karatsuba(t1, t2, t3, storage); // t1 = (a_h+a_l)*(b_h+b_l)
//
// There is now a slight deviation from Karatsuba, we want to subtract
// a_l*b_l + a_h*b_h from t1, but rather than use an addition and a subtraction
// plus one temporary, we'll use 2 subtractions. On the minus side, a subtraction
// is on average slightly slower than an addition, but we save a temporary (ie memory)
// and also hammer the same piece of memory over and over rather than 2 disparate
// memory regions. Overall it seems to be a slight win.
//
subtract_unsigned(t1, t1, result_high);
subtract_unsigned(t1, t1, result_low);
//
// The final step is to left shift t1 by n bits and add to the result.
// Rather than do an actual left shift, we can simply alias the result
// and add to the alias:
//
cpp_int_type result_alias(result.limbs(), n, result.size() - n);
add_unsigned(result_alias, result_alias, t1);
//
// Free up storage for use by sister branches to this one:
//
storage.deallocate(t1.capacity() + t2.capacity() + t3.capacity());
result.normalize();
}
inline std::size_t karatsuba_storage_size(std::size_t s)
{
//
// This estimates how much memory we will need based on
// s-limb multiplication. In an ideal world the number of limbs
// would halve with each recursion, and our storage requirements
// would be 4s in the limit, and rather less in practice since
// we bail out long before we reach one limb. In the real world
// we don't quite halve s in each recursion, so this is an heuristic
// which over-estimates how much we need. We could compute an exact
// value, but it would be rather time consuming.
//
return 5 * s;
}
//
// There are 2 entry point routines for Karatsuba multiplication:
// one for variable precision types, and one for fixed precision types.
// These are responsible for allocating all the storage required for the recursive
// routines above, and are always at the outermost level.
//
// Normal variable precision case comes first:
//
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
inline typename std::enable_if<!is_fixed_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>::type
setup_karatsuba(
cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& result,
const cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& a,
const cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& b)
{
std::size_t as = a.size();
std::size_t bs = b.size();
std::size_t s = as > bs ? as : bs;
std::size_t storage_size = karatsuba_storage_size(s);
if (storage_size < 300)
{
//
// Special case: if we don't need too much memory, we can use stack based storage
// and save a call to the allocator, this allows us to use Karatsuba multiply
// at lower limb counts than would otherwise be possible:
//
limb_type limbs[300];
typename cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>::scoped_shared_storage storage(limbs, storage_size);
multiply_karatsuba(result, a, b, storage);
}
else
{
typename cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>::scoped_shared_storage storage(result.allocator(), storage_size);
multiply_karatsuba(result, a, b, storage);
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, std::size_t MinBits3, std::size_t MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
inline typename std::enable_if<is_fixed_precision<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_fixed_precision<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value || is_fixed_precision<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
setup_karatsuba(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
{
//
// Now comes the fixed precision case.
// In fact Karatsuba doesn't really work with fixed precision since the logic
// requires that we calculate all the bits of the result (especially in the
// temporaries used internally). So... we'll convert all the arguments
// to variable precision types by aliasing them, this also
// reduce the number of template instantations:
//
using variable_precision_type = cpp_int_backend<0, 0, signed_magnitude, unchecked, std::allocator<limb_type> >;
variable_precision_type a_t(a.limbs(), 0, a.size()), b_t(b.limbs(), 0, b.size());
std::size_t as = a.size();
std::size_t bs = b.size();
std::size_t s = as > bs ? as : bs;
std::size_t sz = as + bs;
std::size_t storage_size = karatsuba_storage_size(s);
if (!is_fixed_precision<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || (sz * sizeof(limb_type) * CHAR_BIT <= MaxBits1))
{
// Result is large enough for all the bits of the result, so we can use aliasing:
result.resize(sz, sz);
variable_precision_type t(result.limbs(), 0, result.size());
typename variable_precision_type::scoped_shared_storage storage(t.allocator(), storage_size);
multiply_karatsuba(t, a_t, b_t, storage);
result.resize(t.size(), t.size());
}
else
{
//
// Not enough bit in result for the answer, so we must use a temporary
// and then truncate (ie modular arithmetic):
//
typename variable_precision_type::scoped_shared_storage storage(variable_precision_type::allocator_type(), sz + storage_size);
variable_precision_type t(storage, sz);
multiply_karatsuba(t, a_t, b_t, storage);
//
// If there is truncation, and result is a checked type then this will throw:
//
result = t;
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, std::size_t MinBits3, std::size_t MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
inline typename std::enable_if<!is_fixed_precision<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_fixed_precision<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && !is_fixed_precision<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
setup_karatsuba(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
{
//
// Variable precision, mixed arguments, just alias and forward:
//
using variable_precision_type = cpp_int_backend<0, 0, signed_magnitude, unchecked, std::allocator<limb_type> >;
variable_precision_type a_t(a.limbs(), 0, a.size()), b_t(b.limbs(), 0, b.size());
std::size_t as = a.size();
std::size_t bs = b.size();
std::size_t s = as > bs ? as : bs;
std::size_t sz = as + bs;
std::size_t storage_size = karatsuba_storage_size(s);
result.resize(sz, sz);
variable_precision_type t(result.limbs(), 0, result.size());
typename variable_precision_type::scoped_shared_storage storage(t.allocator(), storage_size);
multiply_karatsuba(t, a_t, b_t, storage);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, std::size_t MinBits3, std::size_t MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
inline BOOST_MP_CXX14_CONSTEXPR void
eval_multiply_comba(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
//
// see PR #182
// Comba Multiplier - based on Paul Comba's
// Exponentiation cryptosystems on the IBM PC, 1990
//
std::ptrdiff_t as = a.size(),
bs = b.size(),
rs = result.size();
typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_pointer pr = result.limbs();
double_limb_type carry = 0,
temp = 0;
limb_type overflow = 0;
const std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
const bool must_throw = rs < as + bs - 1;
for (std::ptrdiff_t r = 0, lim = (std::min)(rs, as + bs - 1); r < lim; ++r, overflow = 0)
{
std::ptrdiff_t i = r >= as ? as - 1 : r,
j = r - i,
k = i < bs - j ? i + 1 : bs - j; // min(i+1, bs-j);
typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::const_limb_pointer pa = a.limbs() + i;
typename cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>::const_limb_pointer pb = b.limbs() + j;
temp = carry;
carry += static_cast<double_limb_type>(*(pa)) * (*(pb));
overflow += carry < temp;
for (--k; k; k--)
{
temp = carry;
carry += static_cast<double_limb_type>(*(--pa)) * (*(++pb));
overflow += carry < temp;
}
*(pr++) = static_cast<limb_type>(carry);
carry = (static_cast<double_limb_type>(overflow) << limb_bits) | (carry >> limb_bits);
}
if (carry || must_throw)
{
resize_for_carry(result, as + bs);
if (static_cast<int>(result.size()) >= as + bs)
*pr = static_cast<limb_type>(carry);
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, std::size_t MinBits3, std::size_t MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value
&& (karatsuba_cutoff * sizeof(limb_type) * CHAR_BIT > MaxBits1)
&& (karatsuba_cutoff * sizeof(limb_type)* CHAR_BIT > MaxBits2)
&& (karatsuba_cutoff * sizeof(limb_type)* CHAR_BIT > MaxBits3)))
{
// Uses simple (O(n^2)) multiplication when the limbs are less
// otherwise switches to karatsuba algorithm based on experimental value (~40 limbs)
//
// Trivial cases first:
//
std::size_t as = a.size();
std::size_t bs = b.size();
typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::const_limb_pointer pa = a.limbs();
typename cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>::const_limb_pointer pb = b.limbs();
if (as == 1)
{
bool s = b.sign() != a.sign();
if (bs == 1)
{
result = static_cast<double_limb_type>(*pa) * static_cast<double_limb_type>(*pb);
}
else
{
limb_type l = *pa;
eval_multiply(result, b, l);
}
result.sign(s);
return;
}
if (bs == 1)
{
bool s = b.sign() != a.sign();
limb_type l = *pb;
eval_multiply(result, a, l);
result.sign(s);
return;
}
if ((void*)&result == (void*)&a)
{
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> t(a);
eval_multiply(result, t, b);
return;
}
if ((void*)&result == (void*)&b)
{
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> t(b);
eval_multiply(result, a, t);
return;
}
constexpr double_limb_type limb_max = static_cast<double_limb_type>(~static_cast<limb_type>(0u));
constexpr double_limb_type double_limb_max = static_cast<double_limb_type>(~static_cast<double_limb_type>(0u));
result.resize(as + bs, as + bs - 1);
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (!BOOST_MP_IS_CONST_EVALUATED(as) && (as >= karatsuba_cutoff && bs >= karatsuba_cutoff))
#else
if (as >= karatsuba_cutoff && bs >= karatsuba_cutoff)
#endif
{
setup_karatsuba(result, a, b);
//
// Set the sign of the result:
//
result.sign(a.sign() != b.sign());
return;
}
typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_pointer pr = result.limbs();
static_assert(double_limb_max - 2 * limb_max >= limb_max * limb_max, "failed limb size sanity check");
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(as))
{
for (std::size_t i = 0; i < result.size(); ++i)
pr[i] = 0;
}
else
#endif
std::memset(pr, 0, result.size() * sizeof(limb_type));
#if defined(BOOST_MP_COMBA)
//
// Comba Multiplier might not be efficient because of less efficient assembly
// by the compiler as of 09/01/2020 (DD/MM/YY). See PR #182
// Till then this will lay dormant :(
//
eval_multiply_comba(result, a, b);
#else
double_limb_type carry = 0;
for (std::size_t i = 0; i < as; ++i)
{
BOOST_MP_ASSERT(result.size() > i);
std::size_t inner_limit = !is_fixed_precision<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value ? bs : (std::min)(result.size() - i, bs);
std::size_t j = 0;
for (; j < inner_limit; ++j)
{
BOOST_MP_ASSERT(i + j < result.size());
#if (!defined(__GLIBCXX__) && !defined(__GLIBCPP__)) || !BOOST_WORKAROUND(BOOST_GCC_VERSION, <= 50100)
BOOST_MP_ASSERT(!std::numeric_limits<double_limb_type>::is_specialized || ((std::numeric_limits<double_limb_type>::max)() - carry >
static_cast<double_limb_type>(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::max_limb_value) * static_cast<double_limb_type>(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::max_limb_value)));
#endif
carry += static_cast<double_limb_type>(pa[i]) * static_cast<double_limb_type>(pb[j]);
BOOST_MP_ASSERT(!std::numeric_limits<double_limb_type>::is_specialized || ((std::numeric_limits<double_limb_type>::max)() - carry >= pr[i + j]));
carry += pr[i + j];
#ifdef __MSVC_RUNTIME_CHECKS
pr[i + j] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
#else
pr[i + j] = static_cast<limb_type>(carry);
#endif
carry >>= cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::limb_bits;
BOOST_MP_ASSERT(carry <= (cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::max_limb_value));
}
if (carry)
{
resize_for_carry(result, i + j + 1); // May throw if checking is enabled
if (i + j < result.size())
#ifdef __MSVC_RUNTIME_CHECKS
pr[i + j] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
#else
pr[i + j] = static_cast<limb_type>(carry);
#endif
}
carry = 0;
}
#endif // ifdef(BOOST_MP_COMBA) ends
result.normalize();
//
// Set the sign of the result:
//
result.sign(a.sign() != b.sign());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a)
noexcept((noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>&>()))))
{
eval_multiply(result, result, a);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_multiply(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result, const limb_type& val)
noexcept((noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const limb_type&>()))))
{
eval_multiply(result, result, val);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const double_limb_type& val)
noexcept(
(noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>&>(), std::declval<const limb_type&>())))
&& (noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>())))
)
{
if (val <= (std::numeric_limits<limb_type>::max)())
{
eval_multiply(result, a, static_cast<limb_type>(val));
}
else
{
#if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> t(val);
#else
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> t;
t = val;
#endif
eval_multiply(result, a, t);
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_multiply(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result, const double_limb_type& val)
noexcept((noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const double_limb_type&>()))))
{
eval_multiply(result, result, val);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const signed_limb_type& val)
noexcept((noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>&>(), std::declval<const limb_type&>()))))
{
if (val > 0)
eval_multiply(result, a, static_cast<limb_type>(val));
else
{
eval_multiply(result, a, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(val)));
result.negate();
}
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_multiply(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result, const signed_limb_type& val)
noexcept((noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const limb_type&>()))))
{
eval_multiply(result, result, val);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
const signed_double_limb_type& val)
noexcept(
(noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>&>(), std::declval<const limb_type&>())))
&& (noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>())))
)
{
if (val > 0)
{
if (val <= (std::numeric_limits<limb_type>::max)())
{
eval_multiply(result, a, static_cast<limb_type>(val));
return;
}
}
else if (val >= -static_cast<signed_double_limb_type>((std::numeric_limits<limb_type>::max)()))
{
eval_multiply(result, a, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(val)));
result.negate();
return;
}
#if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> t(val);
#else
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> t;
t = val;
#endif
eval_multiply(result, a, t);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_multiply(cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result, const signed_double_limb_type& val)
noexcept(
(noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const limb_type&>())))
&& (noexcept(eval_multiply(std::declval<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>(), std::declval<const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>&>())))
)
{
eval_multiply(result, result, val);
}
//
// Now over again for trivial cpp_int's:
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value)>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() = detail::checked_multiply(*result.limbs(), *o.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.sign(result.sign() != o.sign());
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() = detail::checked_multiply(*result.limbs(), *o.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value)>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& a,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& b) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() = detail::checked_multiply(*a.limbs(), *b.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.sign(a.sign() != b.sign());
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& a,
const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& b) noexcept((is_non_throwing_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value))
{
*result.limbs() = detail::checked_multiply(*a.limbs(), *b.limbs(), typename cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>::checked_type());
result.normalize();
}
//
// Special routines for multiplying two integers to obtain a multiprecision result:
//
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
signed_double_limb_type a, signed_double_limb_type b)
{
constexpr signed_double_limb_type mask = static_cast<signed_double_limb_type>(~static_cast<limb_type>(0));
constexpr std::size_t limb_bits = static_cast<std::size_t>(sizeof(limb_type) * CHAR_BIT);
bool s = false;
if (a < 0)
{
a = -a;
s = true;
}
if (b < 0)
{
b = -b;
s = !s;
}
double_limb_type w = a & mask;
double_limb_type x = static_cast<double_limb_type>(a >> limb_bits);
double_limb_type y = b & mask;
double_limb_type z = static_cast<double_limb_type>(b >> limb_bits);
result.resize(4, 4);
limb_type* pr = result.limbs();
double_limb_type carry = w * y;
#ifdef __MSVC_RUNTIME_CHECKS
pr[0] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
carry >>= limb_bits;
carry += w * z + x * y;
pr[1] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
carry >>= limb_bits;
carry += x * z;
pr[2] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
pr[3] = static_cast<limb_type>(carry >> limb_bits);
#else
pr[0] = static_cast<limb_type>(carry);
carry >>= limb_bits;
carry += w * z + x * y;
pr[1] = static_cast<limb_type>(carry);
carry >>= limb_bits;
carry += x * z;
pr[2] = static_cast<limb_type>(carry);
pr[3] = static_cast<limb_type>(carry >> limb_bits);
#endif
result.sign(s);
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
double_limb_type a, double_limb_type b)
{
constexpr signed_double_limb_type mask = static_cast<signed_double_limb_type>(~static_cast<limb_type>(0));
constexpr std::size_t limb_bits = static_cast<std::size_t>(sizeof(limb_type) * CHAR_BIT);
double_limb_type w = a & mask;
double_limb_type x = a >> limb_bits;
double_limb_type y = b & mask;
double_limb_type z = b >> limb_bits;
result.resize(4, 4);
limb_type* pr = result.limbs();
double_limb_type carry = w * y;
#ifdef __MSVC_RUNTIME_CHECKS
pr[0] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
carry >>= limb_bits;
carry += w * z;
pr[1] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
carry >>= limb_bits;
pr[2] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
carry = x * y + pr[1];
pr[1] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
carry >>= limb_bits;
carry += pr[2] + x * z;
pr[2] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
pr[3] = static_cast<limb_type>(carry >> limb_bits);
#else
pr[0] = static_cast<limb_type>(carry);
carry >>= limb_bits;
carry += w * z;
pr[1] = static_cast<limb_type>(carry);
carry >>= limb_bits;
pr[2] = static_cast<limb_type>(carry);
carry = x * y + pr[1];
pr[1] = static_cast<limb_type>(carry);
carry >>= limb_bits;
carry += pr[2] + x * z;
pr[2] = static_cast<limb_type>(carry);
pr[3] = static_cast<limb_type>(carry >> limb_bits);
#endif
result.sign(false);
result.normalize();
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1,
std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> const& a,
cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> const& b)
{
using canonical_type = typename boost::multiprecision::detail::canonical<typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::local_limb_type, cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::type;
eval_multiply(result, static_cast<canonical_type>(*a.limbs()), static_cast<canonical_type>(*b.limbs()));
result.sign(a.sign() != b.sign());
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, class SI>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (sizeof(SI) <= sizeof(signed_double_limb_type) / 2)>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
SI a, SI b)
{
result = static_cast<signed_double_limb_type>(a) * static_cast<signed_double_limb_type>(b);
}
template <std::size_t MinBits1, std::size_t MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, class UI>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (sizeof(UI) <= sizeof(signed_double_limb_type) / 2)>::type
eval_multiply(
cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
UI a, UI b)
{
result = static_cast<double_limb_type>(a) * static_cast<double_limb_type>(b);
}
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
}}} // namespace boost::multiprecision::backends
#endif

View file

@ -0,0 +1,211 @@
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_CPP_INT_SERIALIZE_HPP
#define BOOST_MP_CPP_INT_SERIALIZE_HPP
#ifndef BOOST_MP_STANDALONE
namespace boost {
namespace archive {
class binary_oarchive;
class binary_iarchive;
} // namespace archive
namespace serialization {
namespace mp = boost::multiprecision;
namespace cpp_int_detail {
using namespace boost::multiprecision;
using namespace boost::multiprecision::backends;
template <class T>
struct is_binary_archive : public std::integral_constant<bool, false>
{};
template <>
struct is_binary_archive<boost::archive::binary_oarchive> : public std::integral_constant<bool, true>
{};
template <>
struct is_binary_archive<boost::archive::binary_iarchive> : public std::integral_constant<bool, true>
{};
//
// We have 8 serialization methods to fill out (and test), they are all permutations of:
// Load vs Store.
// Trivial or non-trivial cpp_int type.
// Binary or not archive.
//
template <class Archive, class Int>
void do_serialize(Archive& ar, Int& val, std::integral_constant<bool, false> const&, std::integral_constant<bool, false> const&, std::integral_constant<bool, false> const&)
{
// Load.
// Non-trivial.
// Non binary.
using boost::make_nvp;
bool s;
ar& make_nvp("sign", s);
std::size_t limb_count;
std::size_t byte_count;
ar& make_nvp("byte-count", byte_count);
limb_count = byte_count / sizeof(limb_type) + ((byte_count % sizeof(limb_type)) ? 1 : 0);
val.resize(limb_count, limb_count);
limb_type* pl = val.limbs();
for (std::size_t i = 0; i < limb_count; ++i)
{
pl[i] = 0;
for (std::size_t j = 0; (j < sizeof(limb_type)) && byte_count; ++j)
{
unsigned char byte;
ar& make_nvp("byte", byte);
pl[i] |= static_cast<limb_type>(byte) << (j * CHAR_BIT);
--byte_count;
}
}
if (s != val.sign())
val.negate();
val.normalize();
}
template <class Archive, class Int>
void do_serialize(Archive& ar, Int& val, std::integral_constant<bool, true> const&, std::integral_constant<bool, false> const&, std::integral_constant<bool, false> const&)
{
// Store.
// Non-trivial.
// Non binary.
using boost::make_nvp;
bool s = val.sign();
ar& make_nvp("sign", s);
limb_type* pl = val.limbs();
std::size_t limb_count = val.size();
std::size_t byte_count = limb_count * sizeof(limb_type);
ar& make_nvp("byte-count", byte_count);
for (std::size_t i = 0; i < limb_count; ++i)
{
limb_type l = pl[i];
for (std::size_t j = 0; j < sizeof(limb_type); ++j)
{
unsigned char byte = static_cast<unsigned char>((l >> (j * CHAR_BIT)) & ((1u << CHAR_BIT) - 1));
ar& make_nvp("byte", byte);
}
}
}
template <class Archive, class Int>
void do_serialize(Archive& ar, Int& val, std::integral_constant<bool, false> const&, std::integral_constant<bool, true> const&, std::integral_constant<bool, false> const&)
{
// Load.
// Trivial.
// Non binary.
using boost::make_nvp;
bool s;
typename Int::local_limb_type l = 0;
ar& make_nvp("sign", s);
std::size_t byte_count;
ar& make_nvp("byte-count", byte_count);
for (std::size_t i = 0; i < byte_count; ++i)
{
unsigned char b;
ar& make_nvp("byte", b);
l |= static_cast<typename Int::local_limb_type>(b) << (i * CHAR_BIT);
}
*val.limbs() = l;
if (s != val.sign())
val.negate();
}
template <class Archive, class Int>
void do_serialize(Archive& ar, Int& val, std::integral_constant<bool, true> const&, std::integral_constant<bool, true> const&, std::integral_constant<bool, false> const&)
{
// Store.
// Trivial.
// Non binary.
using boost::make_nvp;
bool s = val.sign();
typename Int::local_limb_type l = *val.limbs();
ar& make_nvp("sign", s);
std::size_t limb_count = sizeof(l);
ar& make_nvp("byte-count", limb_count);
for (std::size_t i = 0; i < limb_count; ++i)
{
unsigned char b = static_cast<unsigned char>(static_cast<typename Int::local_limb_type>(l >> (i * CHAR_BIT)) & static_cast<typename Int::local_limb_type>((1u << CHAR_BIT) - 1));
ar& make_nvp("byte", b);
}
}
template <class Archive, class Int>
void do_serialize(Archive& ar, Int& val, std::integral_constant<bool, false> const&, std::integral_constant<bool, false> const&, std::integral_constant<bool, true> const&)
{
// Load.
// Non-trivial.
// Binary.
bool s;
std::size_t c;
ar& s;
ar& c;
val.resize(c, c);
ar.load_binary(val.limbs(), c * sizeof(limb_type));
if (s != val.sign())
val.negate();
val.normalize();
}
template <class Archive, class Int>
void do_serialize(Archive& ar, Int& val, std::integral_constant<bool, true> const&, std::integral_constant<bool, false> const&, std::integral_constant<bool, true> const&)
{
// Store.
// Non-trivial.
// Binary.
bool s = val.sign();
std::size_t c = val.size();
ar& s;
ar& c;
ar.save_binary(val.limbs(), c * sizeof(limb_type));
}
template <class Archive, class Int>
void do_serialize(Archive& ar, Int& val, std::integral_constant<bool, false> const&, std::integral_constant<bool, true> const&, std::integral_constant<bool, true> const&)
{
// Load.
// Trivial.
// Binary.
bool s;
ar& s;
ar.load_binary(val.limbs(), sizeof(*val.limbs()));
if (s != val.sign())
val.negate();
}
template <class Archive, class Int>
void do_serialize(Archive& ar, Int& val, std::integral_constant<bool, true> const&, std::integral_constant<bool, true> const&, std::integral_constant<bool, true> const&)
{
// Store.
// Trivial.
// Binary.
bool s = val.sign();
ar& s;
ar.save_binary(val.limbs(), sizeof(*val.limbs()));
}
} // namespace cpp_int_detail
template <class Archive, std::size_t MinBits, std::size_t MaxBits, mp::cpp_integer_type SignType, mp::cpp_int_check_type Checked, class Allocator>
void serialize(Archive& ar, mp::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& val, const unsigned int /*version*/)
{
using archive_save_tag = typename Archive::is_saving ;
using save_tag = std::integral_constant<bool, archive_save_tag::value> ;
using trivial_tag = std::integral_constant<bool, mp::backends::is_trivial_cpp_int<mp::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>;
using binary_tag = typename cpp_int_detail::is_binary_archive<Archive>::type ;
// Just dispatch to the correct method:
cpp_int_detail::do_serialize(ar, val, save_tag(), trivial_tag(), binary_tag());
}
} // namespace serialization
} // namespace boost
#endif // BOOST_MP_STANDALONE
#endif // BOOST_MP_CPP_INT_SERIALIZE_HPP

View file

@ -0,0 +1,42 @@
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_CPP_INT_VP_HPP
#define BOOST_MP_CPP_INT_VP_HPP
namespace boost {
namespace multiprecision {
namespace literals { namespace detail {
template <limb_type... VALUES>
struct value_pack
{
constexpr value_pack() {}
using next_type = value_pack<0, VALUES...>;
};
template <class T>
struct is_value_pack
{
static constexpr bool value = false;
};
template <limb_type... VALUES>
struct is_value_pack<value_pack<VALUES...> >
{
static constexpr bool value = true;
};
struct negate_tag
{};
constexpr negate_tag make_negate_tag()
{
return negate_tag();
}
}}}} // namespace boost::multiprecision::literals::detail
#endif // BOOST_MP_CPP_INT_CORE_HPP

View file

@ -0,0 +1,760 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_DEBUG_ADAPTOR_HPP
#define BOOST_MP_DEBUG_ADAPTOR_HPP
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/traits/extract_exponent_type.hpp>
#include <boost/multiprecision/detail/integer_ops.hpp>
namespace boost {
namespace multiprecision {
namespace backends {
template <class Backend>
struct debug_adaptor
{
using signed_types = typename Backend::signed_types ;
using unsigned_types = typename Backend::unsigned_types;
using float_types = typename Backend::float_types ;
using exponent_type = typename extract_exponent_type<Backend, number_category<Backend>::value>::type;
private:
std::string debug_value;
Backend m_value;
public:
void update_view()
{
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
debug_value = m_value.str(0, static_cast<std::ios_base::fmtflags>(0));
#ifndef BOOST_NO_EXCEPTIONS
}
catch (const std::exception& e)
{
debug_value = "String conversion failed with message: \"";
debug_value += e.what();
debug_value += "\"";
}
#endif
}
debug_adaptor()
{
update_view();
}
debug_adaptor(const debug_adaptor& o) : debug_value(o.debug_value), m_value(o.m_value)
{
}
debug_adaptor& operator=(const debug_adaptor& o)
{
debug_value = o.debug_value;
m_value = o.m_value;
return *this;
}
template <class T>
debug_adaptor(const T& i, const typename std::enable_if<std::is_convertible<T, Backend>::value>::type* = nullptr)
: m_value(i)
{
update_view();
}
template <class T>
debug_adaptor(const debug_adaptor<T>& i, const typename std::enable_if<std::is_convertible<T, Backend>::value>::type* = nullptr)
: m_value(i.value())
{
update_view();
}
template <class T, class U>
debug_adaptor(const T& i, const U& j, typename std::enable_if<std::is_constructible<Backend, const T&, const U&>::value>::type* = nullptr)
: m_value(i, j)
{
update_view();
}
template <class B2>
debug_adaptor(const B2& i, unsigned digits10, typename std::enable_if<std::is_same<B2, Backend>::value && std::is_constructible<Backend, const Backend&, unsigned>::value>::type* = nullptr)
: m_value(i, digits10)
{
update_view();
}
template <class T>
typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value || std::is_assignable<Backend, T>::value, debug_adaptor&>::type operator=(const T& i)
{
m_value = i;
update_view();
return *this;
}
debug_adaptor& operator=(const char* s)
{
m_value = s;
update_view();
return *this;
}
void swap(debug_adaptor& o)
{
std::swap(m_value, o.value());
std::swap(debug_value, o.debug_value);
}
std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
{
return m_value.str(digits, f);
}
void negate()
{
m_value.negate();
update_view();
}
int compare(const debug_adaptor& o) const
{
return m_value.compare(o.value());
}
template <class T>
int compare(const T& i) const
{
return m_value.compare(i);
}
Backend& value()
{
return m_value;
}
const Backend& value() const
{
return m_value;
}
#ifndef BOOST_MP_STANDALONE
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/)
{
ar & boost::make_nvp("value", m_value);
using tag = typename Archive::is_loading;
if (tag::value)
update_view();
}
#endif
static unsigned default_precision() noexcept
{
return Backend::default_precision();
}
static void default_precision(unsigned v) noexcept
{
Backend::default_precision(v);
}
static unsigned thread_default_precision() noexcept
{
return Backend::thread_default_precision();
}
static void thread_default_precision(unsigned v) noexcept
{
Backend::thread_default_precision(v);
}
unsigned precision() const noexcept
{
return value().precision();
}
void precision(unsigned digits10) noexcept
{
value().precision(digits10);
}
//
// Variable precision options:
//
static constexpr variable_precision_options default_variable_precision_options()noexcept
{
return Backend::default_variable_precision_options();
}
static constexpr variable_precision_options thread_default_variable_precision_options()noexcept
{
return Backend::thread_default_variable_precision_options();
}
static BOOST_MP_CXX14_CONSTEXPR void default_variable_precision_options(variable_precision_options opts)
{
Backend::default_variable_precision_options(opts);
}
static BOOST_MP_CXX14_CONSTEXPR void thread_default_variable_precision_options(variable_precision_options opts)
{
Backend::thread_default_variable_precision_options(opts);
}
};
template <class Backend>
inline Backend const& unwrap_debug_type(debug_adaptor<Backend> const& val)
{
return val.value();
}
template <class T>
inline const T& unwrap_debug_type(const T& val)
{
return val;
}
template <class Backend, class V, class U>
inline BOOST_MP_CXX14_CONSTEXPR void assign_components(debug_adaptor<Backend>& result, const V& v1, const U& v2)
{
using default_ops::assign_components;
assign_components(result.value(), unwrap_debug_type(v1), unwrap_debug_type(v2));
result.update_view();
}
#define NON_MEMBER_OP1(name, str) \
template <class Backend> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value()); \
result.update_view(); \
}
#define NON_MEMBER_OP2(name, str) \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const T& a) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a)); \
result.update_view(); \
} \
template <class Backend> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const debug_adaptor<Backend>& a) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a)); \
result.update_view(); \
}
#define NON_MEMBER_OP3(name, str) \
template <class Backend, class T, class U> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const T& a, const U& b) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b)); \
result.update_view(); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const debug_adaptor<Backend>& a, const T& b) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b)); \
result.update_view(); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const T& a, const debug_adaptor<Backend>& b) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b)); \
result.update_view(); \
} \
template <class Backend> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const debug_adaptor<Backend>& a, const debug_adaptor<Backend>& b) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b)); \
result.update_view(); \
}
#define NON_MEMBER_OP4(name, str) \
template <class Backend, class T, class U, class V> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const T& a, const U& b, const V& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b), unwrap_debug_type(c)); \
result.update_view(); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const debug_adaptor<Backend>& a, const debug_adaptor<Backend>& b, const T& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b), unwrap_debug_type(c)); \
result.update_view(); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const debug_adaptor<Backend>& a, const T& b, const debug_adaptor<Backend>& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b), unwrap_debug_type(c)); \
result.update_view(); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const T& a, const debug_adaptor<Backend>& b, const debug_adaptor<Backend>& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b), unwrap_debug_type(c)); \
result.update_view(); \
} \
template <class Backend> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const debug_adaptor<Backend>& a, const debug_adaptor<Backend>& b, const debug_adaptor<Backend>& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b), unwrap_debug_type(c)); \
result.update_view(); \
} \
template <class Backend, class T, class U> \
inline void BOOST_JOIN(eval_, name)(debug_adaptor<Backend> & result, const debug_adaptor<Backend>& a, const T& b, const U& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_debug_type(a), unwrap_debug_type(b), unwrap_debug_type(c)); \
result.update_view(); \
}
NON_MEMBER_OP2(add, "+=")
NON_MEMBER_OP2(subtract, "-=")
NON_MEMBER_OP2(multiply, "*=")
NON_MEMBER_OP2(divide, "/=")
template <class Backend, class R>
inline void eval_convert_to(R* result, const debug_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
eval_convert_to(result, val.value());
}
template <class Backend, class R>
inline void eval_convert_to(debug_adaptor<R>* result, const debug_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
eval_convert_to(&result->value(), val.value());
}
template <class Backend, class R>
inline void eval_convert_to(debug_adaptor<R>* result, const Backend& val)
{
using default_ops::eval_convert_to;
eval_convert_to(&result->value(), val);
}
template <class Backend>
inline void eval_convert_to(std::complex<float>* result, const debug_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
eval_convert_to(result, val.value());
}
template <class Backend>
inline void eval_convert_to(std::complex<double>* result, const debug_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
eval_convert_to(result, val.value());
}
template <class Backend>
inline void eval_convert_to(std::complex<long double>* result, const debug_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
eval_convert_to(result, val.value());
}
template <class Backend, class Exp>
inline void eval_frexp(debug_adaptor<Backend>& result, const debug_adaptor<Backend>& arg, Exp* exp)
{
eval_frexp(result.value(), arg.value(), exp);
result.update_view();
}
template <class Backend, class Exp>
inline void eval_ldexp(debug_adaptor<Backend>& result, const debug_adaptor<Backend>& arg, Exp exp)
{
eval_ldexp(result.value(), arg.value(), exp);
result.update_view();
}
template <class Backend, class Exp>
inline void eval_scalbn(debug_adaptor<Backend>& result, const debug_adaptor<Backend>& arg, Exp exp)
{
using default_ops::eval_scalbn;
eval_scalbn(result.value(), arg.value(), exp);
result.update_view();
}
template <class Backend>
inline typename Backend::exponent_type eval_ilogb(const debug_adaptor<Backend>& arg)
{
using default_ops::eval_ilogb;
return eval_ilogb(arg.value());
}
NON_MEMBER_OP2(floor, "floor")
NON_MEMBER_OP2(ceil, "ceil")
NON_MEMBER_OP2(sqrt, "sqrt")
NON_MEMBER_OP2(logb, "logb")
template <class Backend>
inline int eval_fpclassify(const debug_adaptor<Backend>& arg)
{
using default_ops::eval_fpclassify;
return eval_fpclassify(arg.value());
}
/*********************************************************************
*
* Optional arithmetic operations come next:
*
*********************************************************************/
NON_MEMBER_OP3(add, "+")
NON_MEMBER_OP3(subtract, "-")
NON_MEMBER_OP3(multiply, "*")
NON_MEMBER_OP3(divide, "/")
NON_MEMBER_OP3(multiply_add, "fused-multiply-add")
NON_MEMBER_OP3(multiply_subtract, "fused-multiply-subtract")
NON_MEMBER_OP4(multiply_add, "fused-multiply-add")
NON_MEMBER_OP4(multiply_subtract, "fused-multiply-subtract")
NON_MEMBER_OP1(increment, "increment")
NON_MEMBER_OP1(decrement, "decrement")
/*********************************************************************
*
* Optional integer operations come next:
*
*********************************************************************/
NON_MEMBER_OP2(modulus, "%=")
NON_MEMBER_OP3(modulus, "%")
NON_MEMBER_OP2(bitwise_or, "|=")
NON_MEMBER_OP3(bitwise_or, "|")
NON_MEMBER_OP2(bitwise_and, "&=")
NON_MEMBER_OP3(bitwise_and, "&")
NON_MEMBER_OP2(bitwise_xor, "^=")
NON_MEMBER_OP3(bitwise_xor, "^")
NON_MEMBER_OP4(qr, "quotient-and-remainder")
NON_MEMBER_OP2(complement, "~")
template <class Backend>
inline void eval_left_shift(debug_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_left_shift;
eval_left_shift(arg.value(), a);
arg.update_view();
}
template <class Backend>
inline void eval_left_shift(debug_adaptor<Backend>& arg, const debug_adaptor<Backend>& a, std::size_t b)
{
using default_ops::eval_left_shift;
eval_left_shift(arg.value(), a.value(), b);
arg.update_view();
}
template <class Backend>
inline void eval_right_shift(debug_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_right_shift;
eval_right_shift(arg.value(), a);
arg.update_view();
}
template <class Backend>
inline void eval_right_shift(debug_adaptor<Backend>& arg, const debug_adaptor<Backend>& a, std::size_t b)
{
using default_ops::eval_right_shift;
eval_right_shift(arg.value(), a.value(), b);
arg.update_view();
}
template <class Backend, class T>
inline T eval_integer_modulus(const debug_adaptor<Backend>& arg, const T& a)
{
using default_ops::eval_integer_modulus;
return eval_integer_modulus(arg.value(), a);
}
template <class Backend>
inline std::size_t eval_lsb(const debug_adaptor<Backend>& arg)
{
using default_ops::eval_lsb;
return eval_lsb(arg.value());
}
template <class Backend>
inline std::size_t eval_msb(const debug_adaptor<Backend>& arg)
{
using default_ops::eval_msb;
return eval_msb(arg.value());
}
template <class Backend>
inline bool eval_bit_test(const debug_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_bit_test;
return eval_bit_test(arg.value(), a);
}
template <class Backend>
inline void eval_bit_set(const debug_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_bit_set;
eval_bit_set(arg.value(), a);
arg.update_view();
}
template <class Backend>
inline void eval_bit_unset(const debug_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_bit_unset;
eval_bit_unset(arg.value(), a);
arg.update_view();
}
template <class Backend>
inline void eval_bit_flip(const debug_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_bit_flip;
eval_bit_flip(arg.value(), a);
arg.update_view();
}
NON_MEMBER_OP3(gcd, "gcd")
NON_MEMBER_OP3(lcm, "lcm")
NON_MEMBER_OP4(powm, "powm")
/*********************************************************************
*
* abs/fabs:
*
*********************************************************************/
NON_MEMBER_OP2(abs, "abs")
NON_MEMBER_OP2(fabs, "fabs")
/*********************************************************************
*
* Floating point functions:
*
*********************************************************************/
NON_MEMBER_OP2(trunc, "trunc")
NON_MEMBER_OP2(round, "round")
NON_MEMBER_OP2(exp, "exp")
NON_MEMBER_OP2(log, "log")
NON_MEMBER_OP2(log10, "log10")
NON_MEMBER_OP2(sin, "sin")
NON_MEMBER_OP2(cos, "cos")
NON_MEMBER_OP2(tan, "tan")
NON_MEMBER_OP2(asin, "asin")
NON_MEMBER_OP2(acos, "acos")
NON_MEMBER_OP2(atan, "atan")
NON_MEMBER_OP2(sinh, "sinh")
NON_MEMBER_OP2(cosh, "cosh")
NON_MEMBER_OP2(tanh, "tanh")
NON_MEMBER_OP2(asinh, "asinh")
NON_MEMBER_OP2(acosh, "acosh")
NON_MEMBER_OP2(atanh, "atanh")
NON_MEMBER_OP3(fmod, "fmod")
NON_MEMBER_OP3(pow, "pow")
NON_MEMBER_OP3(atan2, "atan2")
NON_MEMBER_OP2(conj, "conj")
template <class Backend>
int eval_signbit(const debug_adaptor<Backend>& val)
{
using default_ops::eval_signbit;
return eval_signbit(val.value());
}
template <class Backend>
std::size_t hash_value(const debug_adaptor<Backend>& val)
{
return hash_value(val.value());
}
template <class Backend, expression_template_option ExpressionTemplates>
inline typename std::enable_if<number_category<Backend>::value == number_kind_rational, typename number<debug_adaptor<Backend>, ExpressionTemplates>::value_type>::type
numerator(const number<debug_adaptor<Backend>, ExpressionTemplates>& arg)
{
number<Backend, ExpressionTemplates> t(arg.backend().value());
return numerator(t).backend();
}
template <class Backend, expression_template_option ExpressionTemplates>
inline typename std::enable_if<number_category<Backend>::value == number_kind_rational, typename number<debug_adaptor<Backend>, ExpressionTemplates>::value_type>::type
denominator(const number<debug_adaptor<Backend>, ExpressionTemplates>& arg)
{
number<Backend, ExpressionTemplates> t(arg.backend().value());
return denominator(t).backend();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_real(To& to, const debug_adaptor<From>& from)
{
using default_ops::eval_real;
eval_real(to, from.value());
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_real(debug_adaptor<To>& to, const debug_adaptor<From>& from)
{
using default_ops::eval_real;
eval_real(to.value(), from.value());
to.update_view();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_real(debug_adaptor<To>& to, const From& from)
{
using default_ops::eval_real;
eval_real(to.value(), from);
to.update_view();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_imag(To& to, const debug_adaptor<From>& from)
{
using default_ops::eval_imag;
eval_imag(to, from.value());
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_imag(debug_adaptor<To>& to, const debug_adaptor<From>& from)
{
using default_ops::eval_imag;
eval_imag(to.value(), from.value());
to.update_view();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_imag(debug_adaptor<To>& to, const From& from)
{
using default_ops::eval_imag;
eval_imag(to.value(), from);
to.update_view();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(To& to, const debug_adaptor<From>& from)
{
using default_ops::eval_set_real;
eval_set_real(to, from.value());
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(debug_adaptor<To>& to, const debug_adaptor<From>& from)
{
using default_ops::eval_set_real;
eval_set_real(to.value(), from.value());
to.update_view();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(debug_adaptor<To>& to, const From& from)
{
using default_ops::eval_set_real;
eval_set_real(to.value(), from);
to.update_view();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(To& to, const debug_adaptor<From>& from)
{
using default_ops::eval_set_imag;
eval_set_imag(to, from.value());
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(debug_adaptor<To>& to, const debug_adaptor<From>& from)
{
using default_ops::eval_set_imag;
eval_set_imag(to.value(), from.value());
to.update_view();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(debug_adaptor<To>& to, const From& from)
{
using default_ops::eval_set_imag;
eval_set_imag(to.value(), from);
to.update_view();
}
} // namespace backends
namespace detail {
template <class Backend>
struct is_variable_precision<debug_adaptor<Backend> > : public is_variable_precision<Backend>
{};
#ifdef BOOST_HAS_INT128
template <class Backend>
struct is_convertible_arithmetic<int128_type, debug_adaptor<Backend> > : public is_convertible_arithmetic<int128_type, Backend>
{};
template <class Backend>
struct is_convertible_arithmetic<uint128_type, debug_adaptor<Backend> > : public is_convertible_arithmetic<uint128_type, Backend>
{};
#endif
#ifdef BOOST_HAS_FLOAT128
template <class Backend>
struct is_convertible_arithmetic<float128_type, debug_adaptor<Backend> > : public is_convertible_arithmetic<float128_type, Backend>
{};
#endif
} // namespace detail
template <class Backend>
struct number_category<backends::debug_adaptor<Backend> > : public number_category<Backend>
{};
template <class Backend, expression_template_option ExpressionTemplates>
struct component_type<number<debug_adaptor<Backend>, ExpressionTemplates>>
{
//
// We'll make the component_type also a debug_adaptor:
//
using base_component_type = typename component_type<number<Backend, ExpressionTemplates>>::type;
using base_component_backend = typename base_component_type::backend_type;
using type = number<debug_adaptor<base_component_backend>, ExpressionTemplates>;
};
template <class Backend>
struct is_interval_number<backends::debug_adaptor<Backend> > : public is_interval_number<Backend> {};
}} // namespace boost::multiprecision
namespace std {
template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates>
class numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::debug_adaptor<Backend>, ExpressionTemplates> >
: public std::numeric_limits<boost::multiprecision::number<Backend, ExpressionTemplates> >
{
using base_type = std::numeric_limits<boost::multiprecision::number<Backend, ExpressionTemplates> > ;
using number_type = boost::multiprecision::number<boost::multiprecision::backends::debug_adaptor<Backend>, ExpressionTemplates>;
public:
static number_type(min)() noexcept { return (base_type::min)(); }
static number_type(max)() noexcept { return (base_type::max)(); }
static number_type lowest() noexcept { return -(max)(); }
static number_type epsilon() noexcept { return base_type::epsilon(); }
static number_type round_error() noexcept { return epsilon() / 2; }
static number_type infinity() noexcept { return base_type::infinity(); }
static number_type quiet_NaN() noexcept { return base_type::quiet_NaN(); }
static number_type signaling_NaN() noexcept { return base_type::signaling_NaN(); }
static number_type denorm_min() noexcept { return base_type::denorm_min(); }
};
} // namespace std
#ifdef BOOST_MP_MATH_AVAILABLE
namespace boost {
namespace math {
namespace policies {
template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
struct precision<boost::multiprecision::number<boost::multiprecision::debug_adaptor<Backend>, ExpressionTemplates>, Policy>
: public precision<boost::multiprecision::number<Backend, ExpressionTemplates>, Policy>
{};
#undef NON_MEMBER_OP1
#undef NON_MEMBER_OP2
#undef NON_MEMBER_OP3
#undef NON_MEMBER_OP4
}
}} // namespace boost::math::policies
#else
#undef NON_MEMBER_OP1
#undef NON_MEMBER_OP2
#undef NON_MEMBER_OP3
#undef NON_MEMBER_OP4
#endif // BOOST_MP_MATH_AVAILABLE
#endif

View file

@ -0,0 +1,29 @@
// (C) Copyright Matt Borland 2021.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// We deliberately use assert in here:
//
// boost-no-inspect
#ifndef BOOST_MP_DETAIL_ASSERT_HPP
#define BOOST_MP_DETAIL_ASSERT_HPP
#include <boost/multiprecision/detail/standalone_config.hpp>
#ifndef BOOST_MP_STANDALONE
#include <boost/assert.hpp>
#define BOOST_MP_ASSERT(expr) BOOST_ASSERT(expr)
#define BOOST_MP_ASSERT_MSG(expr, msg) BOOST_ASSERT_MSG(expr, msg)
#else // Standalone mode - use cassert
#include <cassert>
#define BOOST_MP_ASSERT(expr) assert(expr)
#define BOOST_MP_ASSERT_MSG(expr, msg) assert((expr)&&(msg))
#endif
#endif // BOOST_MP_DETAIL_ASSERT_HPP

View file

@ -0,0 +1,62 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2017 John Maddock
// Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_DETAIL_ATOMIC_HPP
#define BOOST_MP_DETAIL_ATOMIC_HPP
#include <boost/multiprecision/detail/standalone_config.hpp>
#ifdef BOOST_HAS_THREADS
# include <atomic>
# define BOOST_MATH_ATOMIC_NS std
namespace boost {
namespace multiprecision {
namespace detail {
#if ATOMIC_INT_LOCK_FREE == 2
using atomic_counter_type = std::atomic<int>;
using atomic_unsigned_type = std::atomic<unsigned>;
using atomic_integer_type = int;
using atomic_unsigned_integer_type = unsigned;
#elif ATOMIC_SHORT_LOCK_FREE == 2
using atomic_counter_type = std::atomic<short>;
using atomic_unsigned_type = std::atomic<unsigned short>;
using atomic_integer_type = short;
using atomic_unsigned_integer_type = unsigned short;
#elif ATOMIC_LONG_LOCK_FREE == 2
using atomic_unsigned_integer_type = std::atomic<long>;
using atomic_unsigned_type = std::atomic<unsigned long>;
using atomic_unsigned_integer_type = unsigned long;
using atomic_integer_type = long;
#elif ATOMIC_LLONG_LOCK_FREE == 2
using atomic_unsigned_integer_type = std::atomic<long long>;
using atomic_unsigned_type = std::atomic<unsigned long long>;
using atomic_integer_type = long long;
using atomic_unsigned_integer_type = unsigned long long;
#else
#define BOOST_MT_NO_ATOMIC_INT
#endif
}
}}
#else // BOOST_HAS_THREADS
#define BOOST_MT_NO_ATOMIC_INT
#endif // BOOST_HAS_THREADS
namespace boost { namespace multiprecision { namespace detail {
#ifdef BOOST_MT_NO_ATOMIC_INT
using precision_type = unsigned;
#else
using precision_type = atomic_unsigned_type;
#endif
} } }
#endif // BOOST_MP_DETAIL_ATOMIC_HPP

View file

@ -0,0 +1,317 @@
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Comparison operators for cpp_int_backend:
//
#ifndef BOOST_MP_DETAIL_BITSCAN_HPP
#define BOOST_MP_DETAIL_BITSCAN_HPP
#include <cstdint>
#include <climits>
#include <type_traits>
#include <boost/multiprecision/detail/endian.hpp>
#include <boost/multiprecision/detail/standalone_config.hpp>
#if (defined(BOOST_MSVC) || (defined(__clang__) && defined(__c2__)) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && (defined(_M_IX86) || defined(_M_X64))
#include <intrin.h>
#endif
namespace boost { namespace multiprecision { namespace detail {
template <class Unsigned>
inline BOOST_MP_CXX14_CONSTEXPR std::size_t find_lsb_default(Unsigned mask)
{
std::size_t result = 0;
while (!(mask & 1u))
{
mask >>= 1;
++result;
}
return result;
}
template <class Unsigned>
inline BOOST_MP_CXX14_CONSTEXPR std::size_t find_msb_default(Unsigned mask)
{
std::size_t index = 0;
while (mask)
{
++index;
mask >>= 1;
}
return --index;
}
template <class Unsigned>
inline BOOST_MP_CXX14_CONSTEXPR std::size_t find_lsb(Unsigned mask, const std::integral_constant<int, 0>&)
{
return find_lsb_default(mask);
}
template <class Unsigned>
inline BOOST_MP_CXX14_CONSTEXPR std::size_t find_msb(Unsigned mask, const std::integral_constant<int, 0>&)
{
return find_msb_default(mask);
}
#if (defined(BOOST_MSVC) || (defined(__clang__) && defined(__c2__)) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && (defined(_M_IX86) || defined(_M_X64))
#pragma intrinsic(_BitScanForward, _BitScanReverse)
BOOST_FORCEINLINE std::size_t find_lsb(unsigned long mask, const std::integral_constant<int, 1>&)
{
unsigned long result;
_BitScanForward(&result, mask);
return result;
}
BOOST_FORCEINLINE std::size_t find_msb(unsigned long mask, const std::integral_constant<int, 1>&)
{
unsigned long result;
_BitScanReverse(&result, mask);
return result;
}
#ifdef _M_X64
#pragma intrinsic(_BitScanForward64, _BitScanReverse64)
BOOST_FORCEINLINE std::size_t find_lsb(unsigned __int64 mask, const std::integral_constant<int, 2>&)
{
unsigned long result;
_BitScanForward64(&result, mask);
return result;
}
template <class Unsigned>
BOOST_FORCEINLINE std::size_t find_msb(Unsigned mask, const std::integral_constant<int, 2>&)
{
unsigned long result;
_BitScanReverse64(&result, mask);
return result;
}
#endif
template <class Unsigned>
BOOST_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR std::size_t find_lsb(Unsigned mask)
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<Unsigned>::type;
using tag_type = typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned long),
std::integral_constant<int, 1>,
#ifdef _M_X64
typename std::conditional<
sizeof(Unsigned) <= sizeof(__int64),
std::integral_constant<int, 2>,
std::integral_constant<int, 0> >::type
#else
std::integral_constant<int, 0>
#endif
>::type;
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(mask))
{
return find_lsb_default(mask);
}
else
#endif
return find_lsb(static_cast<ui_type>(mask), tag_type());
}
template <class Unsigned>
BOOST_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR std::size_t find_msb(Unsigned mask)
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<Unsigned>::type;
using tag_type = typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned long),
std::integral_constant<int, 1>,
#ifdef _M_X64
typename std::conditional<
sizeof(Unsigned) <= sizeof(__int64),
std::integral_constant<int, 2>,
std::integral_constant<int, 0> >::type
#else
std::integral_constant<int, 0>
#endif
>::type;
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(mask))
{
return find_msb_default(mask);
}
else
#endif
return find_msb(static_cast<ui_type>(mask), tag_type());
}
#elif defined(BOOST_GCC) || defined(__clang__) || (defined(BOOST_INTEL) && defined(__GNUC__))
BOOST_FORCEINLINE std::size_t find_lsb(std::size_t mask, std::integral_constant<int, 1> const&)
{
return static_cast<std::size_t>(__builtin_ctz(static_cast<unsigned int>(mask)));
}
BOOST_FORCEINLINE std::size_t find_lsb(unsigned long mask, std::integral_constant<int, 2> const&)
{
return static_cast<std::size_t>(__builtin_ctzl(static_cast<unsigned long>(mask)));
}
BOOST_FORCEINLINE std::size_t find_lsb(unsigned long long mask, std::integral_constant<int, 3> const&)
{
return static_cast<std::size_t>(__builtin_ctzll(static_cast<unsigned long long>(mask)));
}
BOOST_FORCEINLINE std::size_t find_msb(std::size_t mask, std::integral_constant<int, 1> const&)
{
return static_cast<std::size_t>(static_cast<std::size_t>(sizeof(unsigned) * static_cast<std::size_t>(CHAR_BIT) - 1u) - static_cast<std::size_t>(__builtin_clz(static_cast<unsigned int>(mask))));
}
BOOST_FORCEINLINE std::size_t find_msb(unsigned long mask, std::integral_constant<int, 2> const&)
{
return static_cast<std::size_t>(static_cast<std::size_t>(sizeof(unsigned long) * static_cast<std::size_t>(CHAR_BIT) - 1u) - static_cast<std::size_t>(__builtin_clzl(static_cast<unsigned long>(mask))));
}
BOOST_FORCEINLINE std::size_t find_msb(unsigned long long mask, std::integral_constant<int, 3> const&)
{
return static_cast<std::size_t>(static_cast<std::size_t>(sizeof(unsigned long long) * static_cast<std::size_t>(CHAR_BIT) - 1u) - static_cast<std::size_t>(__builtin_clzll(static_cast<unsigned long long>(mask))));
}
#ifdef BOOST_HAS_INT128
BOOST_FORCEINLINE std::size_t find_msb(uint128_type mask, std::integral_constant<int, 0> const&)
{
union
{
uint128_type v;
std::uint64_t sv[2];
} val;
val.v = mask;
#if BOOST_MP_ENDIAN_LITTLE_BYTE
if (val.sv[1])
return find_msb(val.sv[1], std::integral_constant<int, 3>()) + 64;
return find_msb(val.sv[0], std::integral_constant<int, 3>());
#else
if (val.sv[0])
return find_msb(val.sv[0], std::integral_constant<int, 3>()) + 64;
return find_msb(val.sv[1], std::integral_constant<int, 3>());
#endif
}
BOOST_FORCEINLINE std::size_t find_lsb(uint128_type mask, std::integral_constant<int, 0> const&)
{
union
{
uint128_type v;
std::uint64_t sv[2];
} val;
val.v = mask;
#if BOOST_MP_ENDIAN_LITTLE_BYTE
if (val.sv[0] == 0)
return find_lsb(val.sv[1], std::integral_constant<int, 3>()) + 64;
return find_lsb(val.sv[0], std::integral_constant<int, 3>());
#else
if (val.sv[1] == 0)
return find_lsb(val.sv[0], std::integral_constant<int, 3>()) + 64;
return find_lsb(val.sv[1], std::integral_constant<int, 3>());
#endif
}
#endif
template <class Unsigned>
BOOST_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR std::size_t find_lsb(Unsigned mask)
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<Unsigned>::type;
using tag_type = typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned),
std::integral_constant<int, 1>,
typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned long),
std::integral_constant<int, 2>,
typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned long long),
std::integral_constant<int, 3>,
std::integral_constant<int, 0> >::type>::type>::type;
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(mask))
{
return find_lsb_default(mask);
}
else
#endif
return find_lsb(static_cast<ui_type>(mask), tag_type());
}
template <class Unsigned>
BOOST_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR std::size_t find_msb(Unsigned mask)
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<Unsigned>::type;
using tag_type = typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned),
std::integral_constant<int, 1>,
typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned long),
std::integral_constant<int, 2>,
typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned long long),
std::integral_constant<int, 3>,
std::integral_constant<int, 0> >::type>::type>::type;
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(mask))
{
return find_msb_default(mask);
}
else
#endif
return find_msb(static_cast<ui_type>(mask), tag_type());
}
#elif defined(BOOST_INTEL)
BOOST_FORCEINLINE std::size_t find_lsb(std::size_t mask, std::integral_constant<int, 1> const&)
{
return _bit_scan_forward(mask);
}
BOOST_FORCEINLINE std::size_t find_msb(std::size_t mask, std::integral_constant<int, 1> const&)
{
return _bit_scan_reverse(mask);
}
template <class Unsigned>
BOOST_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR std::size_t find_lsb(Unsigned mask)
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<Unsigned>::type;
using tag_type = typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned),
std::integral_constant<int, 1>,
std::integral_constant<int, 0> >::type;
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(mask))
{
return find_lsb_default(mask);
}
else
#endif
return find_lsb(static_cast<ui_type>(mask), tag_type());
}
template <class Unsigned>
BOOST_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR std::size_t find_msb(Unsigned mask)
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<Unsigned>::type;
using tag_type = typename std::conditional<
sizeof(Unsigned) <= sizeof(unsigned),
std::integral_constant<int, 1>,
std::integral_constant<int, 0> >::type;
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(mask))
{
return find_msb_default(mask);
}
else
#endif
return find_msb(static_cast<ui_type>(mask), tag_type());
}
#else
template <class Unsigned>
BOOST_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR std::size_t find_lsb(Unsigned mask)
{
return find_lsb(mask, std::integral_constant<int, 0>());
}
template <class Unsigned>
BOOST_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR std::size_t find_msb(Unsigned mask)
{
return find_msb(mask, std::integral_constant<int, 0>());
}
#endif
}}} // namespace boost::multiprecision::detail
#endif

View file

@ -0,0 +1,64 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2011 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_CHECK_CPP11_CONFIG_HPP
#define BOOST_MP_CHECK_CPP11_CONFIG_HPP
//
// We now require C++11, if something we use is not supported, then error and say why:
//
#ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_RVALUE_REFERENCES being set"
#endif
#ifdef BOOST_NO_CXX11_TEMPLATE_ALIASES
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_TEMPLATE_ALIASES being set"
#endif
#ifdef BOOST_NO_CXX11_HDR_ARRAY
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_HDR_ARRAY being set"
#endif
#ifdef BOOST_NO_CXX11_HDR_TYPE_TRAITS
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_HDR_TYPE_TRAITS being set"
#endif
#ifdef BOOST_NO_CXX11_ALLOCATOR
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_ALLOCATOR being set"
#endif
#ifdef BOOST_NO_CXX11_CONSTEXPR
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_CONSTEXPR being set"
#endif
#ifdef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS being set"
#endif
#ifdef BOOST_NO_CXX11_REF_QUALIFIERS
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_REF_QUALIFIERS being set"
#endif
#ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_HDR_FUNCTIONAL being set"
#endif
#ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_VARIADIC_TEMPLATES being set"
#endif
#ifdef BOOST_NO_CXX11_USER_DEFINED_LITERALS
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_USER_DEFINED_LITERALS being set"
#endif
#ifdef BOOST_NO_CXX11_DECLTYPE
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_DECLTYPE being set"
#endif
#ifdef BOOST_NO_CXX11_STATIC_ASSERT
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_STATIC_ASSERT being set"
#endif
#ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_DEFAULTED_FUNCTIONS being set"
#endif
#ifdef BOOST_NO_CXX11_NOEXCEPT
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_NOEXCEPT being set"
#endif
#ifdef BOOST_NO_CXX11_REF_QUALIFIERS
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_REF_QUALIFIERS being set"
#endif
#ifdef BOOST_NO_CXX11_USER_DEFINED_LITERALS
#error "This library now requires a C++11 or later compiler - this message was generated as a result of BOOST_NO_CXX11_USER_DEFINED_LITERALS being set"
#endif
#endif // BOOST_MP_CHECK_CPP11_CONFIG_HPP

View file

@ -0,0 +1,88 @@
///////////////////////////////////////////////////////////////
// Copyright 2019 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_DETAIL_CONSTEXPR_HPP
#define BOOST_MP_DETAIL_CONSTEXPR_HPP
#include <cstring>
#include <boost/multiprecision/detail/standalone_config.hpp>
namespace boost {
namespace multiprecision {
namespace std_constexpr {
template <class T>
inline BOOST_CXX14_CONSTEXPR void swap(T& a, T& b)
{
T t(a);
a = b;
b = t;
}
template <class InputIterator, class OutputIterator>
inline BOOST_CXX14_CONSTEXPR OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
{
//
// There are 3 branches here, only one of which is selected at compile time:
//
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(*first))
{
// constexpr safe code, never generates runtime code:
while (first != last)
{
*result = *first;
++first;
++result;
}
return result;
}
else
#endif
{
#ifndef BOOST_NO_CXX17_IF_CONSTEXPR
if constexpr (std::is_pointer<InputIterator>::value && std::is_pointer<OutputIterator>::value && std::is_trivially_copyable<typename std::remove_reference<decltype(*first)>::type>::value)
{
// The normal runtime branch:
std::memcpy(result, first, static_cast<std::size_t>(static_cast<std::size_t>(last - first) * sizeof(*first)));
return result + (last - first);
}
else
#endif
{
// Alternate runtime branch:
while (first != last)
{
*result = *first;
++first;
++result;
}
return result;
}
}
}
template <class I>
inline BOOST_CXX14_CONSTEXPR bool equal(const I* first, const I* last, const I* other)
{
while (first != last)
{
if (*first != *other)
return false;
++first;
++other;
}
return true;
}
}
}
} // namespace boost::multiprecision::std_constexpr
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,49 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_DETAIL_DIGITS_HPP
#define BOOST_MP_DETAIL_DIGITS_HPP
namespace boost { namespace multiprecision { namespace detail {
inline constexpr unsigned long digits10_2_2(unsigned long d10)
{
return (d10 * 1000uL) / 301uL + ((d10 * 1000uL) % 301 ? 2u : 1u);
}
inline constexpr unsigned long digits2_2_10(unsigned long d2)
{
return (d2 * 301uL) / 1000uL;
}
#if ULONG_MAX != SIZE_MAX
inline constexpr std::size_t digits10_2_2(std::size_t d10)
{
return (d10 * 1000uL) / 301uL + ((d10 * 1000uL) % 301 ? 2u : 1u);
}
inline constexpr std::size_t digits2_2_10(std::size_t d2)
{
return (d2 * 301uL) / 1000uL;
}
template <class I>
inline constexpr typename std::enable_if<sizeof(I) <= sizeof(unsigned long), unsigned long>::type digits10_2_2(I d10)
{
return digits10_2_2(static_cast<unsigned long>(d10));
}
template <class I>
inline constexpr typename std::enable_if<sizeof(I) <= sizeof(unsigned long), unsigned long>::type digits2_2_10(I d10)
{
return digits2_2_10(static_cast<unsigned long>(d10));
}
#endif
}}} // namespace boost::multiprecision::detail
#endif

View file

@ -0,0 +1,44 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock.
// Copyright Christopher Kormanyos 2013. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_MP_DETAIL_DYNAMIC_ARRAY_HPP
#define BOOST_MP_DETAIL_DYNAMIC_ARRAY_HPP
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <vector>
#include <boost/multiprecision/detail/rebind.hpp>
namespace boost { namespace multiprecision { namespace backends { namespace detail {
template <class ValueType, const std::uint32_t ElemNumber, class my_allocator>
struct dynamic_array : public std::vector<ValueType, typename rebind<ValueType, my_allocator>::type>
{
private:
using base_class_type = std::vector<ValueType, typename rebind<ValueType, my_allocator>::type>;
public:
dynamic_array()
: base_class_type(static_cast<typename base_class_type::size_type>(ElemNumber),
static_cast<typename base_class_type::value_type>(0u)) { }
dynamic_array(std::initializer_list<std::uint32_t> lst)
: base_class_type(static_cast<typename base_class_type::size_type>(ElemNumber),
static_cast<typename base_class_type::value_type>(0u))
{
std::copy(lst.begin(),
lst.begin() + (std::min)(std::size_t(lst.size()), std::size_t(ElemNumber)),
data());
}
typename base_class_type::value_type* data() { return &(*(this->begin())); }
const typename base_class_type::value_type* data() const { return &(*(this->begin())); }
};
}}}} // namespace boost::multiprecision::backends::detail
#endif // BOOST_MP_DETAIL_DYNAMIC_ARRAY_HPP

View file

@ -0,0 +1,87 @@
/////////////////////////////////////////////////////////////////////
// Copyright 2018 Glen Joseph Fernandes.
// Copyright 2021 Matt Borland. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_DETAIL_EMPTY_VALUE_HPP
#define BOOST_MP_DETAIL_EMPTY_VALUE_HPP
#include <utility>
#include <boost/multiprecision/detail/standalone_config.hpp>
#if defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION >= 40700)
#define BOOST_DETAIL_EMPTY_VALUE_BASE
#elif defined(BOOST_INTEL) && defined(_MSC_VER) && (_MSC_VER >= 1800)
#define BOOST_DETAIL_EMPTY_VALUE_BASE
#elif defined(BOOST_MSVC) && (BOOST_MSVC >= 1800)
#define BOOST_DETAIL_EMPTY_VALUE_BASE
#elif defined(BOOST_CLANG) && !defined(__CUDACC__)
#if __has_feature(is_empty) && __has_feature(is_final)
#define BOOST_DETAIL_EMPTY_VALUE_BASE
#endif
#endif
namespace boost { namespace multiprecision { namespace detail {
template <typename T>
struct use_empty_value_base
{
#if defined(BOOST_DETAIL_EMPTY_VALUE_BASE)
static constexpr bool value = __is_empty(T) && !__is_final(T);
#else
static constexpr bool value = false;
#endif
};
struct empty_init_t {};
namespace empty_impl {
template <typename T, unsigned N = 0,
bool E = boost::multiprecision::detail::use_empty_value_base<T>::value>
class empty_value
{
private:
T value_;
public:
using type = T;
empty_value() = default;
explicit empty_value(boost::multiprecision::detail::empty_init_t) : value_ {} {}
template <typename U, typename... Args>
empty_value(boost::multiprecision::detail::empty_init_t, U&& value, Args&&... args) :
value_ {std::forward<U>(value), std::forward<Args>(args)...} {}
const T& get() const noexcept { return value_; }
T& get() noexcept { return value_; }
};
template <typename T, unsigned N>
class empty_value<T, N, true> : T
{
public:
using type = T;
empty_value() = default;
explicit empty_value(boost::multiprecision::detail::empty_init_t) : T{} {}
template <typename U, typename... Args>
empty_value(boost::multiprecision::detail::empty_init_t, U&& value, Args&&... args) :
T{std::forward<U>(value), std::forward<Args>(args)...} {}
const T& get() const noexcept { return *this; }
T& get() noexcept { return *this; }
};
} // Namespace empty impl
using empty_impl::empty_value;
BOOST_INLINE_CONSTEXPR empty_init_t empty_init = empty_init_t();
}}} // Namespaces
#endif // BOOST_MP_DETAIL_EMPTY_VALUE_HPP

View file

@ -0,0 +1,35 @@
////////////////////////////////////////////////////////////////
// Copyright 2021 Matt Borland. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_DETAIL_ENDIAN_HPP
#define BOOST_MP_DETAIL_ENDIAN_HPP
#include <boost/multiprecision/detail/standalone_config.hpp>
#ifndef BOOST_MP_STANDALONE
# include <boost/predef/other/endian.h>
# define BOOST_MP_ENDIAN_BIG_BYTE BOOST_ENDIAN_BIG_BYTE
# define BOOST_MP_ENDIAN_LITTLE_BYTE BOOST_ENDIAN_LITTLE_BYTE
#elif defined(_WIN32)
# define BOOST_MP_ENDIAN_BIG_BYTE 0
# define BOOST_MP_ENDIAN_LITTLE_BYTE 1
#elif defined(__BYTE_ORDER__)
# define BOOST_MP_ENDIAN_BIG_BYTE (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
# define BOOST_MP_ENDIAN_LITTLE_BYTE (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#else
# error Could not determine endian type. Please disable standalone mode, and file an issue at https://github.com/boostorg/multiprecision
#endif // Determine endianness
static_assert((BOOST_MP_ENDIAN_BIG_BYTE || BOOST_MP_ENDIAN_LITTLE_BYTE)
&& !(BOOST_MP_ENDIAN_BIG_BYTE && BOOST_MP_ENDIAN_LITTLE_BYTE),
"Inconsistent endianness detected. Please disable standalone mode, and file an issue at https://github.com/boostorg/multiprecision");
#endif // BOOST_MP_DETAIL_ENDIAN_HPP

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,95 @@
// (C) Copyright John Maddock 2021.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// We deliberately use assert in here:
//
#ifndef BOOST_MP_DETAIL_FLOAT128_FUNCTIONS_HPP
#define BOOST_MP_DETAIL_FLOAT128_FUNCTIONS_HPP
#include <boost/multiprecision/detail/standalone_config.hpp>
#ifndef BOOST_MP_STANDALONE
#include <boost/cstdfloat.hpp>
#if defined(BOOST_MATH_USE_FLOAT128) && !defined(BOOST_CSTDFLOAT_NO_LIBQUADMATH_SUPPORT)
# define BOOST_MP_HAVE_CSTDFLOAT
#endif
#endif
#if defined(BOOST_HAS_FLOAT128)
namespace boost
{
namespace multiprecision
{
namespace float128_procs
{
extern "C" __float128 ldexpq(__float128, int) throw();
extern "C" __float128 frexpq(__float128, int*) throw();
extern "C" __float128 floorq(__float128) throw();
extern "C" __float128 nextafterq(__float128, __float128) throw();
extern "C" int isinfq(__float128) throw();
extern "C" int isnanq(__float128) throw();
extern "C" __float128 strtoflt128(const char*, char**) throw();
#ifdef BOOST_MP_HAVE_CSTDFLOAT
using std::ldexp;
using std::frexp;
using std::floor;
using std::nextafter;
#else
inline __float128 ldexp(__float128 f, int i) throw() { return ldexpq(f, i); }
inline __float128 frexp(__float128 f, int* p) throw() { return frexpq(f, p); }
inline __float128 floor(__float128 f) throw() { return floorq(f); }
inline __float128 nextafter(__float128 a, __float128 b) throw() { return nextafterq(a, b); }
#endif
}
namespace detail {
template <class T>
struct is_float128 : public std::is_same<__float128, T>
{};
}
}
}
namespace boost {
namespace math {
inline __float128 float_next(const __float128& f)
{
return boost::multiprecision::float128_procs::nextafterq(f, 2 * f);
}
inline int (isinf)(const __float128& f)
{
return boost::multiprecision::float128_procs::isinfq(f);
}
inline int (isnan)(const __float128& f)
{
return boost::multiprecision::float128_procs::isnanq(f);
}
}}
#define BOOST_MP_FLOAT128_USING using boost::multiprecision::float128_procs::ldexp; using boost::multiprecision::float128_procs::frexp; using boost::multiprecision::float128_procs::floor; using boost::multiprecision::float128_procs::nextafter; using boost::math::isinf; using boost::math::isnan;
#else
#define BOOST_MP_FLOAT128_USING
namespace boost {
namespace multiprecision {
namespace detail {
template <class T>
struct is_float128 : public std::false_type
{};
}}} // namespace boost::multiprecision::detail
#endif
#endif // BOOST_MP_DETAIL_FLOAT128_FUNCTIONS_HPP

View file

@ -0,0 +1,333 @@
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Generic routines for converting floating point values to and from decimal strings.
// Note that these use "naive" algorithms which result in rounding error - so they
// do not round trip to and from the string representation (but should only be out
// in the last bit).
//
#ifndef BOOST_MP_FLOAT_STRING_CVT_HPP
#define BOOST_MP_FLOAT_STRING_CVT_HPP
#include <string>
#include <cctype>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#include <boost/multiprecision/detail/assert.hpp>
namespace boost { namespace multiprecision { namespace detail {
template <class I>
inline void round_string_up_at(std::string& s, std::ptrdiff_t pos, I& expon)
{
//
// Rounds up a string representation of a number at pos:
//
if (pos < 0)
{
s.insert(static_cast<std::string::size_type>(0), 1, '1');
s.erase(s.size() - 1);
++expon;
}
else if (s[static_cast<std::size_t>(pos)] == '9')
{
s[static_cast<std::size_t>(pos)] = '0';
round_string_up_at(s, pos - 1, expon);
}
else
{
if ((pos == 0) && (s[static_cast<std::size_t>(pos)] == '0') && (s.size() == 1))
++expon;
++s[static_cast<std::size_t>(pos)];
}
}
template <class Backend>
std::string convert_to_string(Backend b, std::streamsize digits, std::ios_base::fmtflags f)
{
using default_ops::eval_convert_to;
using default_ops::eval_divide;
using default_ops::eval_floor;
using default_ops::eval_fpclassify;
using default_ops::eval_log10;
using default_ops::eval_multiply;
using default_ops::eval_pow;
using default_ops::eval_subtract;
using ui_type = typename std::tuple_element<0, typename Backend::unsigned_types>::type;
using exponent_type = typename Backend::exponent_type ;
std::string result;
bool iszero = false;
bool isneg = false;
exponent_type expon = 0;
std::streamsize org_digits = digits;
BOOST_MP_ASSERT(digits > 0);
int fpt = eval_fpclassify(b);
if (fpt == static_cast<int>(FP_ZERO))
{
result = "0";
iszero = true;
}
else if (fpt == static_cast<int>(FP_INFINITE))
{
if (b.compare(ui_type(0)) < 0)
return "-inf";
else
return ((f & std::ios_base::showpos) == std::ios_base::showpos) ? "+inf" : "inf";
}
else if (fpt == static_cast<int>(FP_NAN))
{
return "nan";
}
else
{
//
// Start by figuring out the exponent:
//
isneg = b.compare(ui_type(0)) < 0;
if (isneg)
b.negate();
Backend t;
Backend ten;
ten = ui_type(10);
eval_log10(t, b);
eval_floor(t, t);
eval_convert_to(&expon, t);
if (-expon > std::numeric_limits<number<Backend> >::max_exponent10 - 3)
{
int e = -expon / 2;
Backend t2;
eval_pow(t2, ten, e);
eval_multiply(t, t2, b);
eval_multiply(t, t2);
if (expon & 1)
eval_multiply(t, ten);
}
else
{
eval_pow(t, ten, -expon);
eval_multiply(t, b);
}
//
// Make sure we're between [1,10) and adjust if not:
//
if (t.compare(ui_type(1)) < 0)
{
eval_multiply(t, ui_type(10));
--expon;
}
else if (t.compare(ui_type(10)) >= 0)
{
eval_divide(t, ui_type(10));
++expon;
}
Backend digit;
ui_type cdigit;
//
// Adjust the number of digits required based on formatting options:
//
if (((f & std::ios_base::fixed) == std::ios_base::fixed) && (expon != -1))
digits += expon + 1;
if ((f & std::ios_base::scientific) == std::ios_base::scientific)
++digits;
//
// Extract the digits one at a time:
//
for (unsigned i = 0; i < digits; ++i)
{
eval_floor(digit, t);
eval_convert_to(&cdigit, digit);
result += static_cast<char>('0' + cdigit);
eval_subtract(t, digit);
eval_multiply(t, ten);
}
//
// Possibly round result:
//
if (digits >= 0)
{
eval_floor(digit, t);
eval_convert_to(&cdigit, digit);
eval_subtract(t, digit);
if ((cdigit == 5) && (t.compare(ui_type(0)) == 0))
{
// Bankers rounding:
if ((*result.rbegin() - '0') & 1)
{
round_string_up_at(result, static_cast<std::ptrdiff_t>(result.size() - 1u), expon);
}
}
else if (cdigit >= 5)
{
round_string_up_at(result, static_cast<std::ptrdiff_t>(result.size() - 1u), expon);
}
}
eval_floor(t, b);
if ((t.compare(b) == 0) && (static_cast<std::size_t>(expon + 1) < result.size()))
{
// Input is an integer, sometimes we get a result which is not an integer here as a result of printing too
// many digits, so lets round if required:
round_string_up_at(result, expon + 1, expon);
result.erase(static_cast<std::string::size_type>(expon + 1));
}
}
while ((static_cast<std::streamsize>(result.size()) > digits) && (result.size() != 0U))
{
// We may get here as a result of rounding...
if (result.size() > 1)
result.erase(result.size() - 1);
else
{
if (expon > 0)
--expon; // so we put less padding in the result.
else
++expon;
++digits;
}
}
BOOST_MP_ASSERT(org_digits >= 0);
if (isneg)
result.insert(static_cast<std::string::size_type>(0), 1, '-');
format_float_string(result, expon, org_digits, f, iszero);
return result;
}
template <class Backend>
void convert_from_string(Backend& b, const char* p)
{
using default_ops::eval_add;
using default_ops::eval_divide;
using default_ops::eval_multiply;
using default_ops::eval_pow;
using ui_type = typename std::tuple_element<0, typename Backend::unsigned_types>::type;
b = ui_type(0);
if (!p || (*p == 0))
return;
bool is_neg = false;
bool is_neg_expon = false;
constexpr ui_type ten = ui_type(10);
typename Backend::exponent_type expon = 0;
int digits_seen = 0;
using limits = std::numeric_limits<number<Backend, et_off>>;
constexpr int max_digits = limits::is_specialized ? limits::max_digits10 + 1 : INT_MAX;
if (*p == '+')
++p;
else if (*p == '-')
{
is_neg = true;
++p;
}
if ((std::strcmp(p, "nan") == 0) || (std::strcmp(p, "NaN") == 0) || (std::strcmp(p, "NAN") == 0))
{
eval_divide(b, ui_type(0));
if (is_neg)
b.negate();
return;
}
if ((std::strcmp(p, "inf") == 0) || (std::strcmp(p, "Inf") == 0) || (std::strcmp(p, "INF") == 0))
{
b = ui_type(1);
eval_divide(b, ui_type(0));
if (is_neg)
b.negate();
return;
}
//
// Grab all the leading digits before the decimal point:
//
while (std::isdigit(*p))
{
eval_multiply(b, ten);
eval_add(b, ui_type(*p - '0'));
++p;
++digits_seen;
}
if (*p == '.')
{
//
// Grab everything after the point, stop when we've seen
// enough digits, even if there are actually more available:
//
++p;
while (std::isdigit(*p))
{
eval_multiply(b, ten);
eval_add(b, ui_type(*p - '0'));
++p;
--expon;
if (++digits_seen > max_digits)
break;
}
while (std::isdigit(*p))
++p;
}
//
// Parse the exponent:
//
if ((*p == 'e') || (*p == 'E'))
{
++p;
if (*p == '+')
++p;
else if (*p == '-')
{
is_neg_expon = true;
++p;
}
typename Backend::exponent_type e2 = 0;
while (std::isdigit(*p))
{
e2 *= 10;
e2 += (*p - '0');
++p;
}
if (is_neg_expon)
e2 = -e2;
expon += e2;
}
if (expon)
{
// Scale by 10^expon, note that 10^expon can be
// outside the range of our number type, even though the
// result is within range, if that looks likely, then split
// the calculation in two:
Backend t;
t = ten;
if (expon > limits::min_exponent10 + 2)
{
eval_pow(t, t, expon);
eval_multiply(b, t);
}
else
{
eval_pow(t, t, expon + digits_seen + 1);
eval_multiply(b, t);
t = ten;
eval_pow(t, t, -digits_seen - 1);
eval_multiply(b, t);
}
}
if (is_neg)
b.negate();
if (*p)
{
// Unexpected input in string:
BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected characters in string being interpreted as a float128."));
}
}
}}} // namespace boost::multiprecision::detail
#endif

View file

@ -0,0 +1,101 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2022 Matt Borland. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_DETAIL_FPCLASSIFY_HPP
#define BOOST_MP_DETAIL_FPCLASSIFY_HPP
#include <cmath>
#include <limits>
#include <type_traits>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/float128_functions.hpp>
#ifdef BOOST_MP_MATH_AVAILABLE
#include <boost/math/special_functions/fpclassify.hpp>
#define BOOST_MP_ISNAN(x) (boost::math::isnan)(x)
#define BOOST_MP_ISINF(x) (boost::math::isinf)(x)
#define BOOST_MP_FPCLASSIFY(x) (boost::math::fpclassify)(x)
#define BOOST_MP_ISFINITE(x) (!(boost::math::isnan)(x) && !(boost::math::isinf)(x))
#else
namespace boost { namespace multiprecision { namespace detail {
template <typename T, typename std::enable_if<std::is_floating_point<T>::value
#ifdef BOOST_HAS_FLOAT128
|| std::is_same<T, float128_type>::value
#endif
, bool>::type = true>
inline bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION (const T x)
{
BOOST_MP_FLOAT128_USING;
using std::isnan;
return static_cast<bool>((isnan)(x));
}
template <typename T, typename std::enable_if<!std::is_floating_point<T>::value
#ifdef BOOST_HAS_FLOAT128
&& !std::is_same<T, float128_type>::value
#endif
, bool>::type = true>
inline bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION (const T x)
{
return x != x;
}
template <typename T, typename std::enable_if<std::is_floating_point<T>::value
#ifdef BOOST_HAS_FLOAT128
|| std::is_same<T, float128_type>::value
#endif
, bool>::type = true>
inline bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION (const T x)
{
BOOST_MP_FLOAT128_USING;
using std::isinf;
return static_cast<bool>((isinf)(x));
}
template <typename T, typename std::enable_if<!std::is_floating_point<T>::value
#ifdef BOOST_HAS_FLOAT128
&& !std::is_same<T, float128_type>::value
#endif
, bool>::type = true>
inline bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION (const T x)
{
return x == std::numeric_limits<T>::infinity() || x == -std::numeric_limits<T>::infinity();
}
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
inline int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION (const T x)
{
using std::fpclassify;
return fpclassify(x);
}
template <typename T, typename std::enable_if<!std::is_floating_point<T>::value, bool>::type = true>
inline int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION (const T x)
{
BOOST_MP_FLOAT128_USING;
using std::isnan;
using std::isinf;
using std::abs;
return (isnan)(x) ? FP_NAN :
(isinf)(x) ? FP_INFINITE :
abs(x) == T(0) ? FP_ZERO :
abs(x) > 0 && abs(x) < (std::numeric_limits<T>::min)() ? FP_SUBNORMAL : FP_NORMAL;
}
}}} // Namespace boost::multiprecision::detail
#define BOOST_MP_ISNAN(x) (boost::multiprecision::detail::isnan)(x)
#define BOOST_MP_ISINF(x) (boost::multiprecision::detail::isinf)(x)
#define BOOST_MP_FPCLASSIFY(x) (boost::multiprecision::detail::fpclassify)(x)
#define BOOST_MP_ISFINITE(x) (!(boost::multiprecision::detail::isnan)(x) && !(boost::multiprecision::detail::isinf)(x))
#endif
#endif // BOOST_MP_DETAIL_FPCLASSIFY_HPP

View file

@ -0,0 +1,288 @@
// Copyright 2011 John Maddock.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// This file has no include guards or namespaces - it's expanded inline inside default_ops.hpp
//
template <class T>
void calc_log2(T& num, unsigned digits)
{
using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
using si_type = typename std::tuple_element<0, typename T::signed_types>::type ;
//
// String value with 1100 digits:
//
static const char* string_val = "0."
"6931471805599453094172321214581765680755001343602552541206800094933936219696947156058633269964186875"
"4200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335"
"0115364497955239120475172681574932065155524734139525882950453007095326366642654104239157814952043740"
"4303855008019441706416715186447128399681717845469570262716310645461502572074024816377733896385506952"
"6066834113727387372292895649354702576265209885969320196505855476470330679365443254763274495125040606"
"9438147104689946506220167720424524529612687946546193165174681392672504103802546259656869144192871608"
"2938031727143677826548775664850856740776484514644399404614226031930967354025744460703080960850474866"
"3852313818167675143866747664789088143714198549423151997354880375165861275352916610007105355824987941"
"4729509293113897155998205654392871700072180857610252368892132449713893203784393530887748259701715591"
"0708823683627589842589185353024363421436706118923678919237231467232172053401649256872747782344535347"
"6481149418642386776774406069562657379600867076257199184734022651462837904883062033061144630073719489";
//
// Check if we can just construct from string:
//
if (digits < 3640) // 3640 binary digits ~ 1100 decimal digits
{
num = string_val;
return;
}
//
// We calculate log2 from using the formula:
//
// ln(2) = 3/4 SUM[n>=0] ((-1)^n * N!^2 / (2^n(2n+1)!))
//
// Numerator and denominator are calculated separately and then
// divided at the end, we also precalculate the terms up to n = 5
// since these fit in a 32-bit integer anyway.
//
// See Gourdon, X., and Sebah, P. The logarithmic constant: log 2, Jan. 2004.
// Also http://www.mpfr.org/algorithms.pdf.
//
num = static_cast<ui_type>(1180509120uL);
T denom, next_term, temp;
denom = static_cast<ui_type>(1277337600uL);
next_term = static_cast<ui_type>(120uL);
si_type sign = -1;
ui_type limit = digits / 3 + 1;
for (ui_type n = 6; n < limit; ++n)
{
temp = static_cast<ui_type>(2);
eval_multiply(temp, ui_type(2 * n));
eval_multiply(temp, ui_type(2 * n + 1));
eval_multiply(num, temp);
eval_multiply(denom, temp);
sign = -sign;
eval_multiply(next_term, n);
eval_multiply(temp, next_term, next_term);
if (sign < 0)
temp.negate();
eval_add(num, temp);
}
eval_multiply(denom, ui_type(4));
eval_multiply(num, ui_type(3));
INSTRUMENT_BACKEND(denom);
INSTRUMENT_BACKEND(num);
eval_divide(num, denom);
INSTRUMENT_BACKEND(num);
}
template <class T>
void calc_e(T& result, unsigned digits)
{
using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
//
// 1100 digits in string form:
//
const char* string_val = "2."
"7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274"
"2746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901"
"1573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069"
"5517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416"
"9283681902551510865746377211125238978442505695369677078544996996794686445490598793163688923009879312"
"7736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117"
"3012381970684161403970198376793206832823764648042953118023287825098194558153017567173613320698112509"
"9618188159304169035159888851934580727386673858942287922849989208680582574927961048419844436346324496"
"8487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016"
"7683964243781405927145635490613031072085103837505101157477041718986106873969655212671546889570350354"
"0212340784981933432106817012100562788023519303322474501585390473041995777709350366041699732972508869";
//
// Check if we can just construct from string:
//
if (digits < 3640) // 3640 binary digits ~ 1100 decimal digits
{
result = string_val;
return;
}
T lim;
lim = ui_type(1);
eval_ldexp(lim, lim, digits);
//
// Standard evaluation from the definition of e: http://functions.wolfram.com/Constants/E/02/
//
result = ui_type(2);
T denom;
denom = ui_type(1);
ui_type i = 2;
do
{
eval_multiply(denom, i);
eval_multiply(result, i);
eval_add(result, ui_type(1));
++i;
} while (denom.compare(lim) <= 0);
eval_divide(result, denom);
}
template <class T>
void calc_pi(T& result, unsigned digits)
{
using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
using real_type = typename std::tuple_element<0, typename T::float_types>::type ;
//
// 1100 digits in string form:
//
const char* string_val = "3."
"1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
"8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196"
"4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273"
"7245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094"
"3305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912"
"9833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132"
"0005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235"
"4201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859"
"5024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303"
"5982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989"
"3809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913152";
//
// Check if we can just construct from string:
//
if (digits < 3640) // 3640 binary digits ~ 1100 decimal digits
{
result = string_val;
return;
}
T a;
a = ui_type(1);
T b;
T A(a);
T B;
B = real_type(0.5f);
T D;
D = real_type(0.25f);
T lim;
lim = ui_type(1);
eval_ldexp(lim, lim, -static_cast<int>(digits));
//
// This algorithm is from:
// Schonhage, A., Grotefeld, A. F. W., and Vetter, E. Fast Algorithms: A Multitape Turing
// Machine Implementation. BI Wissenschaftverlag, 1994.
// Also described in MPFR's algorithm guide: http://www.mpfr.org/algorithms.pdf.
//
// Let:
// a[0] = A[0] = 1
// B[0] = 1/2
// D[0] = 1/4
// Then:
// S[k+1] = (A[k]+B[k]) / 4
// b[k] = sqrt(B[k])
// a[k+1] = a[k]^2
// B[k+1] = 2(A[k+1]-S[k+1])
// D[k+1] = D[k] - 2^k(A[k+1]-B[k+1])
// Stop when |A[k]-B[k]| <= 2^(k-p)
// and PI = B[k]/D[k]
unsigned k = 1;
do
{
eval_add(result, A, B);
eval_ldexp(result, result, -2);
eval_sqrt(b, B);
eval_add(a, b);
eval_ldexp(a, a, -1);
eval_multiply(A, a, a);
eval_subtract(B, A, result);
eval_ldexp(B, B, 1);
eval_subtract(result, A, B);
bool neg = eval_get_sign(result) < 0;
if (neg)
result.negate();
if (result.compare(lim) <= 0)
break;
if (neg)
result.negate();
eval_ldexp(result, result, static_cast<int>(k - 1u));
eval_subtract(D, result);
++k;
eval_ldexp(lim, lim, 1);
} while (true);
eval_divide(result, B, D);
}
template <class T>
const T& get_constant_ln2()
{
static BOOST_MP_THREAD_LOCAL T result;
static BOOST_MP_THREAD_LOCAL long digits = 0;
if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
{
boost::multiprecision::detail::maybe_promote_precision(&result);
calc_log2(result, boost::multiprecision::detail::digits2<number<T, et_on> >::value());
digits = boost::multiprecision::detail::digits2<number<T> >::value();
}
return result;
}
template <class T>
const T& get_constant_e()
{
static BOOST_MP_THREAD_LOCAL T result;
static BOOST_MP_THREAD_LOCAL long digits = 0;
if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
{
boost::multiprecision::detail::maybe_promote_precision(&result);
calc_e(result, boost::multiprecision::detail::digits2<number<T, et_on> >::value());
digits = boost::multiprecision::detail::digits2<number<T> >::value();
}
return result;
}
template <class T>
const T& get_constant_pi()
{
static BOOST_MP_THREAD_LOCAL T result;
static BOOST_MP_THREAD_LOCAL long digits = 0;
if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
{
boost::multiprecision::detail::maybe_promote_precision(&result);
calc_pi(result, boost::multiprecision::detail::digits2<number<T, et_on> >::value());
digits = boost::multiprecision::detail::digits2<number<T> >::value();
}
return result;
}
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4127) // conditional expression is constant
#endif
template <class T>
const T& get_constant_one_over_epsilon()
{
static BOOST_MP_THREAD_LOCAL T result;
static BOOST_MP_THREAD_LOCAL long digits = 0;
if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
{
using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
boost::multiprecision::detail::maybe_promote_precision(&result);
result = static_cast<ui_type>(1u);
BOOST_IF_CONSTEXPR(std::numeric_limits<number<T> >::is_specialized)
eval_divide(result, std::numeric_limits<number<T> >::epsilon().backend());
else
eval_ldexp(result, result, boost::multiprecision::detail::digits2<number<T> >::value() - 1);
digits = boost::multiprecision::detail::digits2<number<T> >::value();
}
return result;
}
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif

View file

@ -0,0 +1,930 @@
// Copyright Christopher Kormanyos 2002 - 2013.
// Copyright 2011 - 2013 John Maddock.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This work is based on an earlier work:
// "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
// in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
//
// This file has no include guards or namespaces - it's expanded inline inside default_ops.hpp
//
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 6326) // comparison of two constants
#pragma warning(disable : 4127) // conditional expression is constant
#endif
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#include <boost/multiprecision/detail/assert.hpp>
namespace detail {
template <typename T, typename U>
inline void pow_imp(T& result, const T& t, const U& p, const std::integral_constant<bool, false>&)
{
// Compute the pure power of typename T t^p.
// Use the S-and-X binary method, as described in
// D. E. Knuth, "The Art of Computer Programming", Vol. 2,
// Section 4.6.3 . The resulting computational complexity
// is order log2[abs(p)].
using int_type = typename boost::multiprecision::detail::canonical<U, T>::type;
if (&result == &t)
{
T temp;
pow_imp(temp, t, p, std::integral_constant<bool, false>());
result = temp;
return;
}
switch (p)
{
case 0:
result = int_type(1);
return;
case 1:
result = t;
return;
case 2:
eval_multiply(result, t, t);
return;
case 3:
eval_multiply(result, t, t);
eval_multiply(result, t);
return;
case 4:
eval_multiply(result, t, t);
eval_multiply(result, result);
return;
default:
break;
}
// This will store the result.
if (U(p % U(2)) != U(0))
{
result = t;
}
else
result = int_type(1);
U p2(p);
// The variable x stores the binary powers of t.
T x(t);
while (U(p2 /= 2) != U(0))
{
// Square x for each binary power.
eval_multiply(x, x);
const bool has_binary_power = (U(p2 % U(2)) != U(0));
if (has_binary_power)
{
// Multiply the result with each binary power contained in the exponent.
eval_multiply(result, x);
}
}
}
template <typename T, typename U>
inline void pow_imp(T& result, const T& t, const U& p, const std::integral_constant<bool, true>&)
{
// Signed integer power, just take care of the sign then call the unsigned version:
using int_type = typename boost::multiprecision::detail::canonical<U, T>::type;
using ui_type = typename boost::multiprecision::detail::make_unsigned<U>::type ;
if (p < 0)
{
T temp;
temp = static_cast<int_type>(1);
T denom;
pow_imp(denom, t, static_cast<ui_type>(-p), std::integral_constant<bool, false>());
eval_divide(result, temp, denom);
return;
}
pow_imp(result, t, static_cast<ui_type>(p), std::integral_constant<bool, false>());
}
} // namespace detail
template <typename T, typename U>
inline typename std::enable_if<boost::multiprecision::detail::is_integral<U>::value>::type eval_pow(T& result, const T& t, const U& p)
{
detail::pow_imp(result, t, p, boost::multiprecision::detail::is_signed<U>());
}
template <class T>
void hyp0F0(T& H0F0, const T& x)
{
// Compute the series representation of Hypergeometric0F0 taken from
// http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric0F0/06/01/
// There are no checks on input range or parameter boundaries.
using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
BOOST_MP_ASSERT(&H0F0 != &x);
long tol = boost::multiprecision::detail::digits2<number<T, et_on> >::value();
T t;
T x_pow_n_div_n_fact(x);
eval_add(H0F0, x_pow_n_div_n_fact, ui_type(1));
T lim;
eval_ldexp(lim, H0F0, static_cast<int>(1L - tol));
if (eval_get_sign(lim) < 0)
lim.negate();
ui_type n;
const unsigned series_limit =
boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
? 100
: boost::multiprecision::detail::digits2<number<T, et_on> >::value();
// Series expansion of hyperg_0f0(; ; x).
for (n = 2; n < series_limit; ++n)
{
eval_multiply(x_pow_n_div_n_fact, x);
eval_divide(x_pow_n_div_n_fact, n);
eval_add(H0F0, x_pow_n_div_n_fact);
bool neg = eval_get_sign(x_pow_n_div_n_fact) < 0;
if (neg)
x_pow_n_div_n_fact.negate();
if (lim.compare(x_pow_n_div_n_fact) > 0)
break;
if (neg)
x_pow_n_div_n_fact.negate();
}
if (n >= series_limit)
BOOST_MP_THROW_EXCEPTION(std::runtime_error("H0F0 failed to converge"));
}
template <class T>
void hyp1F0(T& H1F0, const T& a, const T& x)
{
// Compute the series representation of Hypergeometric1F0 taken from
// http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric1F0/06/01/01/
// and also see the corresponding section for the power function (i.e. x^a).
// There are no checks on input range or parameter boundaries.
using si_type = typename boost::multiprecision::detail::canonical<int, T>::type;
BOOST_MP_ASSERT(&H1F0 != &x);
BOOST_MP_ASSERT(&H1F0 != &a);
T x_pow_n_div_n_fact(x);
T pochham_a(a);
T ap(a);
eval_multiply(H1F0, pochham_a, x_pow_n_div_n_fact);
eval_add(H1F0, si_type(1));
T lim;
eval_ldexp(lim, H1F0, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
if (eval_get_sign(lim) < 0)
lim.negate();
si_type n;
T term, part;
const si_type series_limit =
boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
? 100
: boost::multiprecision::detail::digits2<number<T, et_on> >::value();
// Series expansion of hyperg_1f0(a; ; x).
for (n = 2; n < series_limit; n++)
{
eval_multiply(x_pow_n_div_n_fact, x);
eval_divide(x_pow_n_div_n_fact, n);
eval_increment(ap);
eval_multiply(pochham_a, ap);
eval_multiply(term, pochham_a, x_pow_n_div_n_fact);
eval_add(H1F0, term);
if (eval_get_sign(term) < 0)
term.negate();
if (lim.compare(term) >= 0)
break;
}
if (n >= series_limit)
BOOST_MP_THROW_EXCEPTION(std::runtime_error("H1F0 failed to converge"));
}
template <class T>
void eval_exp(T& result, const T& x)
{
static_assert(number_category<T>::value == number_kind_floating_point, "The exp function is only valid for floating point types.");
if (&x == &result)
{
T temp;
eval_exp(temp, x);
result = temp;
return;
}
using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
using si_type = typename boost::multiprecision::detail::canonical<int, T>::type ;
using exp_type = typename T::exponent_type ;
using canonical_exp_type = typename boost::multiprecision::detail::canonical<exp_type, T>::type;
// Handle special arguments.
int type = eval_fpclassify(x);
bool isneg = eval_get_sign(x) < 0;
if (type == static_cast<int>(FP_NAN))
{
result = x;
errno = EDOM;
return;
}
else if (type == static_cast<int>(FP_INFINITE))
{
if (isneg)
result = ui_type(0u);
else
result = x;
return;
}
else if (type == static_cast<int>(FP_ZERO))
{
result = ui_type(1);
return;
}
// Get local copy of argument and force it to be positive.
T xx = x;
T exp_series;
if (isneg)
xx.negate();
// Check the range of the argument.
if (xx.compare(si_type(1)) <= 0)
{
//
// Use series for exp(x) - 1:
//
T lim;
BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::is_specialized)
lim = std::numeric_limits<number<T, et_on> >::epsilon().backend();
else
{
result = ui_type(1);
eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
}
unsigned k = 2;
exp_series = xx;
result = si_type(1);
if (isneg)
eval_subtract(result, exp_series);
else
eval_add(result, exp_series);
eval_multiply(exp_series, xx);
eval_divide(exp_series, ui_type(k));
eval_add(result, exp_series);
while (exp_series.compare(lim) > 0)
{
++k;
eval_multiply(exp_series, xx);
eval_divide(exp_series, ui_type(k));
if (isneg && (k & 1))
eval_subtract(result, exp_series);
else
eval_add(result, exp_series);
}
return;
}
// Check for pure-integer arguments which can be either signed or unsigned.
typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type ll;
eval_trunc(exp_series, x);
eval_convert_to(&ll, exp_series);
if (x.compare(ll) == 0)
{
detail::pow_imp(result, get_constant_e<T>(), ll, std::integral_constant<bool, true>());
return;
}
else if (exp_series.compare(x) == 0)
{
// We have a value that has no fractional part, but is too large to fit
// in a long long, in this situation the code below will fail, so
// we're just going to assume that this will overflow:
if (isneg)
result = ui_type(0);
else
result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
return;
}
// The algorithm for exp has been taken from MPFUN.
// exp(t) = [ (1 + r + r^2/2! + r^3/3! + r^4/4! ...)^p2 ] * 2^n
// where p2 is a power of 2 such as 2048, r = t_prime / p2, and
// t_prime = t - n*ln2, with n chosen to minimize the absolute
// value of t_prime. In the resulting Taylor series, which is
// implemented as a hypergeometric function, |r| is bounded by
// ln2 / p2. For small arguments, no scaling is done.
// Compute the exponential series of the (possibly) scaled argument.
eval_divide(result, xx, get_constant_ln2<T>());
exp_type n;
eval_convert_to(&n, result);
if (n == (std::numeric_limits<exp_type>::max)())
{
// Exponent is too large to fit in our exponent type:
if (isneg)
result = ui_type(0);
else
result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
return;
}
// The scaling is 2^11 = 2048.
const si_type p2 = static_cast<si_type>(si_type(1) << 11);
eval_multiply(exp_series, get_constant_ln2<T>(), static_cast<canonical_exp_type>(n));
eval_subtract(exp_series, xx);
eval_divide(exp_series, p2);
exp_series.negate();
hyp0F0(result, exp_series);
detail::pow_imp(exp_series, result, p2, std::integral_constant<bool, true>());
result = ui_type(1);
eval_ldexp(result, result, n);
eval_multiply(exp_series, result);
if (isneg)
eval_divide(result, ui_type(1), exp_series);
else
result = exp_series;
}
template <class T>
void eval_log(T& result, const T& arg)
{
static_assert(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
//
// We use a variation of http://dlmf.nist.gov/4.45#i
// using frexp to reduce the argument to x * 2^n,
// then let y = x - 1 and compute:
// log(x) = log(2) * n + log1p(1 + y)
//
using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
using exp_type = typename T::exponent_type ;
using canonical_exp_type = typename boost::multiprecision::detail::canonical<exp_type, T>::type;
using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
int s = eval_signbit(arg);
switch (eval_fpclassify(arg))
{
case FP_NAN:
result = arg;
errno = EDOM;
return;
case FP_INFINITE:
if (s)
break;
result = arg;
return;
case FP_ZERO:
result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
result.negate();
errno = ERANGE;
return;
default:
break;
}
if (s)
{
result = std::numeric_limits<number<T> >::quiet_NaN().backend();
errno = EDOM;
return;
}
exp_type e;
T t;
eval_frexp(t, arg, &e);
bool alternate = false;
if (t.compare(fp_type(2) / fp_type(3)) <= 0)
{
alternate = true;
eval_ldexp(t, t, 1);
--e;
}
eval_multiply(result, get_constant_ln2<T>(), canonical_exp_type(e));
INSTRUMENT_BACKEND(result);
eval_subtract(t, ui_type(1)); /* -0.3 <= t <= 0.3 */
if (!alternate)
t.negate(); /* 0 <= t <= 0.33333 */
T pow = t;
T lim;
T t2;
if (alternate)
eval_add(result, t);
else
eval_subtract(result, t);
BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::is_specialized)
eval_multiply(lim, result, std::numeric_limits<number<T, et_on> >::epsilon().backend());
else
eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
if (eval_get_sign(lim) < 0)
lim.negate();
INSTRUMENT_BACKEND(lim);
ui_type k = 1;
do
{
++k;
eval_multiply(pow, t);
eval_divide(t2, pow, k);
INSTRUMENT_BACKEND(t2);
if (alternate && ((k & 1) != 0))
eval_add(result, t2);
else
eval_subtract(result, t2);
INSTRUMENT_BACKEND(result);
} while (lim.compare(t2) < 0);
}
template <class T>
const T& get_constant_log10()
{
static BOOST_MP_THREAD_LOCAL T result;
static BOOST_MP_THREAD_LOCAL long digits = 0;
if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
{
using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
T ten;
ten = ui_type(10u);
eval_log(result, ten);
digits = boost::multiprecision::detail::digits2<number<T> >::value();
}
return result;
}
template <class T>
void eval_log10(T& result, const T& arg)
{
static_assert(number_category<T>::value == number_kind_floating_point, "The log10 function is only valid for floating point types.");
eval_log(result, arg);
eval_divide(result, get_constant_log10<T>());
}
template <class R, class T>
inline void eval_log2(R& result, const T& a)
{
eval_log(result, a);
eval_divide(result, get_constant_ln2<R>());
}
template <typename T>
inline void eval_pow(T& result, const T& x, const T& a)
{
static_assert(number_category<T>::value == number_kind_floating_point, "The pow function is only valid for floating point types.");
using si_type = typename boost::multiprecision::detail::canonical<int, T>::type;
using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
if ((&result == &x) || (&result == &a))
{
T t;
eval_pow(t, x, a);
result = t;
return;
}
if ((a.compare(si_type(1)) == 0) || (x.compare(si_type(1)) == 0))
{
result = x;
return;
}
if (a.compare(si_type(0)) == 0)
{
result = si_type(1);
return;
}
int type = eval_fpclassify(x);
switch (type)
{
case FP_ZERO:
switch (eval_fpclassify(a))
{
case FP_ZERO:
result = si_type(1);
break;
case FP_NAN:
result = a;
break;
case FP_NORMAL: {
// Need to check for a an odd integer as a special case:
BOOST_MP_TRY
{
typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type i;
eval_convert_to(&i, a);
if (a.compare(i) == 0)
{
if (eval_signbit(a))
{
if (i & 1)
{
result = std::numeric_limits<number<T> >::infinity().backend();
if (eval_signbit(x))
result.negate();
errno = ERANGE;
}
else
{
result = std::numeric_limits<number<T> >::infinity().backend();
errno = ERANGE;
}
}
else if (i & 1)
{
result = x;
}
else
result = si_type(0);
return;
}
}
BOOST_MP_CATCH(const std::exception&)
{
// fallthrough..
}
BOOST_MP_CATCH_END
BOOST_FALLTHROUGH;
}
default:
if (eval_signbit(a))
{
result = std::numeric_limits<number<T> >::infinity().backend();
errno = ERANGE;
}
else
result = x;
break;
}
return;
case FP_NAN:
result = x;
errno = ERANGE;
return;
default:;
}
int s = eval_get_sign(a);
if (s == 0)
{
result = si_type(1);
return;
}
if (s < 0)
{
T t, da;
t = a;
t.negate();
eval_pow(da, x, t);
eval_divide(result, si_type(1), da);
return;
}
typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type an;
typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type max_an =
std::numeric_limits<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>::is_specialized ? (std::numeric_limits<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>::max)() : static_cast<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>(1) << (sizeof(typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type) * CHAR_BIT - 2);
typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type min_an =
std::numeric_limits<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>::is_specialized ? (std::numeric_limits<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>::min)() : -min_an;
T fa;
BOOST_MP_TRY
{
eval_convert_to(&an, a);
if (a.compare(an) == 0)
{
detail::pow_imp(result, x, an, std::integral_constant<bool, true>());
return;
}
}
BOOST_MP_CATCH(const std::exception&)
{
// conversion failed, just fall through, value is not an integer.
an = (std::numeric_limits<std::intmax_t>::max)();
}
BOOST_MP_CATCH_END
if ((eval_get_sign(x) < 0))
{
typename boost::multiprecision::detail::canonical<std::uintmax_t, T>::type aun;
BOOST_MP_TRY
{
eval_convert_to(&aun, a);
if (a.compare(aun) == 0)
{
fa = x;
fa.negate();
eval_pow(result, fa, a);
if (aun & 1u)
result.negate();
return;
}
}
BOOST_MP_CATCH(const std::exception&)
{
// conversion failed, just fall through, value is not an integer.
}
BOOST_MP_CATCH_END
eval_floor(result, a);
// -1^INF is a special case in C99:
if ((x.compare(si_type(-1)) == 0) && (eval_fpclassify(a) == FP_INFINITE))
{
result = si_type(1);
}
else if (a.compare(result) == 0)
{
// exponent is so large we have no fractional part:
if (x.compare(si_type(-1)) < 0)
{
result = std::numeric_limits<number<T, et_on> >::infinity().backend();
}
else
{
result = si_type(0);
}
}
else if (type == FP_INFINITE)
{
result = std::numeric_limits<number<T, et_on> >::infinity().backend();
}
else BOOST_IF_CONSTEXPR (std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
{
result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
errno = EDOM;
}
else
{
BOOST_MP_THROW_EXCEPTION(std::domain_error("Result of pow is undefined or non-real and there is no NaN for this number type."));
}
return;
}
T t, da;
eval_subtract(da, a, an);
if ((x.compare(fp_type(0.5)) >= 0) && (x.compare(fp_type(0.9)) < 0) && (an < max_an) && (an > min_an))
{
if (a.compare(fp_type(1e-5f)) <= 0)
{
// Series expansion for small a.
eval_log(t, x);
eval_multiply(t, a);
hyp0F0(result, t);
return;
}
else
{
// Series expansion for moderately sized x. Note that for large power of a,
// the power of the integer part of a is calculated using the pown function.
if (an)
{
da.negate();
t = si_type(1);
eval_subtract(t, x);
hyp1F0(result, da, t);
detail::pow_imp(t, x, an, std::integral_constant<bool, true>());
eval_multiply(result, t);
}
else
{
da = a;
da.negate();
t = si_type(1);
eval_subtract(t, x);
hyp1F0(result, da, t);
}
}
}
else
{
// Series expansion for pow(x, a). Note that for large power of a, the power
// of the integer part of a is calculated using the pown function.
if (an)
{
eval_log(t, x);
eval_multiply(t, da);
eval_exp(result, t);
detail::pow_imp(t, x, an, std::integral_constant<bool, true>());
eval_multiply(result, t);
}
else
{
eval_log(t, x);
eval_multiply(t, a);
eval_exp(result, t);
}
}
}
template <class T, class A>
#if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
inline typename std::enable_if<!boost::multiprecision::detail::is_integral<A>::value, void>::type
#else
inline typename std::enable_if<is_compatible_arithmetic_type<A, number<T> >::value && !boost::multiprecision::detail::is_integral<A>::value, void>::type
#endif
eval_pow(T& result, const T& x, const A& a)
{
// Note this one is restricted to float arguments since pow.hpp already has a version for
// integer powers....
using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
cast_type c;
c = a;
eval_pow(result, x, c);
}
template <class T, class A>
#if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
inline void
#else
inline typename std::enable_if<is_compatible_arithmetic_type<A, number<T> >::value, void>::type
#endif
eval_pow(T& result, const A& x, const T& a)
{
using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
cast_type c;
c = x;
eval_pow(result, c, a);
}
template <class T>
void eval_exp2(T& result, const T& arg)
{
static_assert(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
// Check for pure-integer arguments which can be either signed or unsigned.
typename boost::multiprecision::detail::canonical<typename T::exponent_type, T>::type i;
T temp;
BOOST_MP_TRY
{
eval_trunc(temp, arg);
eval_convert_to(&i, temp);
if (arg.compare(i) == 0)
{
temp = static_cast<typename std::tuple_element<0, typename T::unsigned_types>::type>(1u);
eval_ldexp(result, temp, i);
return;
}
}
#ifdef BOOST_MP_MATH_AVAILABLE
BOOST_MP_CATCH(const boost::math::rounding_error&)
{ /* Fallthrough */
}
#endif
BOOST_MP_CATCH(const std::runtime_error&)
{ /* Fallthrough */
}
BOOST_MP_CATCH_END
temp = static_cast<typename std::tuple_element<0, typename T::unsigned_types>::type>(2u);
eval_pow(result, temp, arg);
}
namespace detail {
template <class T>
void small_sinh_series(T x, T& result)
{
using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
bool neg = eval_get_sign(x) < 0;
if (neg)
x.negate();
T p(x);
T mult(x);
eval_multiply(mult, x);
result = x;
ui_type k = 1;
T lim(x);
eval_ldexp(lim, lim, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
do
{
eval_multiply(p, mult);
eval_divide(p, ++k);
eval_divide(p, ++k);
eval_add(result, p);
} while (p.compare(lim) >= 0);
if (neg)
result.negate();
}
template <class T>
void sinhcosh(const T& x, T* p_sinh, T* p_cosh)
{
using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
switch (eval_fpclassify(x))
{
case FP_NAN:
errno = EDOM;
// fallthrough...
case FP_INFINITE:
if (p_sinh)
*p_sinh = x;
if (p_cosh)
{
*p_cosh = x;
if (eval_get_sign(x) < 0)
p_cosh->negate();
}
return;
case FP_ZERO:
if (p_sinh)
*p_sinh = x;
if (p_cosh)
*p_cosh = ui_type(1);
return;
default:;
}
bool small_sinh = eval_get_sign(x) < 0 ? x.compare(fp_type(-0.5)) > 0 : x.compare(fp_type(0.5)) < 0;
if (p_cosh || !small_sinh)
{
T e_px, e_mx;
eval_exp(e_px, x);
eval_divide(e_mx, ui_type(1), e_px);
if (eval_signbit(e_mx) != eval_signbit(e_px))
e_mx.negate(); // Handles lack of signed zero in some types
if (p_sinh)
{
if (small_sinh)
{
small_sinh_series(x, *p_sinh);
}
else
{
eval_subtract(*p_sinh, e_px, e_mx);
eval_ldexp(*p_sinh, *p_sinh, -1);
}
}
if (p_cosh)
{
eval_add(*p_cosh, e_px, e_mx);
eval_ldexp(*p_cosh, *p_cosh, -1);
}
}
else
{
small_sinh_series(x, *p_sinh);
}
}
} // namespace detail
template <class T>
inline void eval_sinh(T& result, const T& x)
{
static_assert(number_category<T>::value == number_kind_floating_point, "The sinh function is only valid for floating point types.");
detail::sinhcosh(x, &result, static_cast<T*>(0));
}
template <class T>
inline void eval_cosh(T& result, const T& x)
{
static_assert(number_category<T>::value == number_kind_floating_point, "The cosh function is only valid for floating point types.");
detail::sinhcosh(x, static_cast<T*>(0), &result);
}
template <class T>
inline void eval_tanh(T& result, const T& x)
{
static_assert(number_category<T>::value == number_kind_floating_point, "The tanh function is only valid for floating point types.");
T c;
detail::sinhcosh(x, &result, &c);
if ((eval_fpclassify(result) == FP_INFINITE) && (eval_fpclassify(c) == FP_INFINITE))
{
bool s = eval_signbit(result) != eval_signbit(c);
result = static_cast<typename std::tuple_element<0, typename T::unsigned_types>::type>(1u);
if (s)
result.negate();
return;
}
eval_divide(result, c);
}
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,82 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2022 Matt Borland. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP
#define BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP
#include <cmath>
#include <limits>
#include <stdexcept>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#ifdef BOOST_MP_MATH_AVAILABLE
#include <boost/math/special_functions/trunc.hpp>
#endif
namespace boost { namespace multiprecision { namespace detail {
namespace impl {
template <typename T>
inline T trunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
{
if (arg > 0)
{
using std::floor;
return floor(arg);
}
using std::ceil;
return ceil(arg);}
} // namespace impl
#ifdef BOOST_MP_MATH_AVAILABLE
template <typename T>
inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
{
return boost::math::lltrunc(arg);
}
template <typename T>
inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
{
return boost::math::itrunc(arg);
}
#else
template <typename T>
inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
{
T t = boost::multiprecision::detail::impl::trunc(arg);
if (t > LLONG_MAX)
{
BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into a long long"));
}
return static_cast<long long>(t);
}
template <typename T>
inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
{
T t = boost::multiprecision::detail::impl::trunc(arg);
if (t > static_cast<T>(INT_MAX))
{
BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into an int"));
}
return static_cast<int>(t);
}
#endif
}}} // Namespaces
#endif // BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP

View file

@ -0,0 +1,687 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2011 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_GENERIC_INTERCONVERT_HPP
#define BOOST_MP_GENERIC_INTERCONVERT_HPP
#include <cmath>
#include <limits>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/default_ops.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#include <boost/multiprecision/detail/assert.hpp>
#include <boost/multiprecision/detail/functions/trunc.hpp>
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4127 6326)
#endif
namespace boost { namespace multiprecision { namespace detail {
template <class To, class From>
inline To do_cast(const From& from)
{
return static_cast<To>(from);
}
template <class To, class B, ::boost::multiprecision::expression_template_option et>
inline To do_cast(const number<B, et>& from)
{
return from.template convert_to<To>();
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/)
{
using default_ops::eval_add;
using default_ops::eval_bitwise_and;
using default_ops::eval_convert_to;
using default_ops::eval_get_sign;
using default_ops::eval_is_zero;
using default_ops::eval_ldexp;
using default_ops::eval_right_shift;
// smallest unsigned type handled natively by "From" is likely to be it's limb_type:
using l_limb_type = typename canonical<unsigned char, From>::type;
// get the corresponding type that we can assign to "To":
using to_type = typename canonical<l_limb_type, To>::type;
From t(from);
bool is_neg = eval_get_sign(t) < 0;
if (is_neg)
t.negate();
// Pick off the first limb:
l_limb_type limb;
l_limb_type mask = static_cast<l_limb_type>(~static_cast<l_limb_type>(0));
From fl;
eval_bitwise_and(fl, t, mask);
eval_convert_to(&limb, fl);
to = static_cast<to_type>(limb);
eval_right_shift(t, std::numeric_limits<l_limb_type>::digits);
//
// Then keep picking off more limbs until "t" is zero:
//
To l;
unsigned shift = std::numeric_limits<l_limb_type>::digits;
while (!eval_is_zero(t))
{
eval_bitwise_and(fl, t, mask);
eval_convert_to(&limb, fl);
l = static_cast<to_type>(limb);
eval_right_shift(t, std::numeric_limits<l_limb_type>::digits);
eval_ldexp(l, l, shift);
eval_add(to, l);
shift += std::numeric_limits<l_limb_type>::digits;
}
//
// Finish off by setting the sign:
//
if (is_neg)
to.negate();
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/)
{
using default_ops::eval_bitwise_and;
using default_ops::eval_bitwise_or;
using default_ops::eval_convert_to;
using default_ops::eval_get_sign;
using default_ops::eval_is_zero;
using default_ops::eval_left_shift;
using default_ops::eval_right_shift;
// smallest unsigned type handled natively by "From" is likely to be it's limb_type:
using limb_type = typename canonical<unsigned char, From>::type;
// get the corresponding type that we can assign to "To":
using to_type = typename canonical<limb_type, To>::type;
From t(from);
bool is_neg = eval_get_sign(t) < 0;
if (is_neg)
t.negate();
// Pick off the first limb:
limb_type limb;
limb_type mask = static_cast<limb_type>(~static_cast<limb_type>(0));
From fl;
eval_bitwise_and(fl, t, mask);
eval_convert_to(&limb, fl);
to = static_cast<to_type>(limb);
eval_right_shift(t, std::numeric_limits<limb_type>::digits);
//
// Then keep picking off more limbs until "t" is zero:
//
To l;
unsigned shift = std::numeric_limits<limb_type>::digits;
while (!eval_is_zero(t))
{
eval_bitwise_and(fl, t, mask);
eval_convert_to(&limb, fl);
l = static_cast<to_type>(limb);
eval_right_shift(t, std::numeric_limits<limb_type>::digits);
eval_left_shift(l, shift);
eval_bitwise_or(to, l);
shift += std::numeric_limits<limb_type>::digits;
}
//
// Finish off by setting the sign:
//
if (is_neg)
to.negate();
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/)
{
#ifdef BOOST_MSVC
#pragma warning(push)
//#pragma warning(disable : 4127)
#endif
//
// The code here only works when the radix of "From" is 2, we could try shifting by other
// radixes but it would complicate things.... use a string conversion when the radix is other
// than 2:
//
BOOST_IF_CONSTEXPR(std::numeric_limits<number<From> >::radix != 2)
{
to = from.str(0, std::ios_base::fmtflags()).c_str();
return;
}
else
{
using ui_type = typename canonical<unsigned char, To>::type;
using default_ops::eval_add;
using default_ops::eval_convert_to;
using default_ops::eval_fpclassify;
using default_ops::eval_get_sign;
using default_ops::eval_is_zero;
using default_ops::eval_subtract;
//
// First classify the input, then handle the special cases:
//
int c = eval_fpclassify(from);
if (c == static_cast<int>(FP_ZERO))
{
to = ui_type(0);
return;
}
else if (c == static_cast<int>(FP_NAN))
{
to = static_cast<const char*>("nan");
return;
}
else if (c == static_cast<int>(FP_INFINITE))
{
to = static_cast<const char*>("inf");
if (eval_get_sign(from) < 0)
to.negate();
return;
}
typename From::exponent_type e;
From f, term;
to = ui_type(0);
eval_frexp(f, from, &e);
constexpr int shift = std::numeric_limits<std::intmax_t>::digits - 1;
while (!eval_is_zero(f))
{
// extract int sized bits from f:
eval_ldexp(f, f, shift);
eval_floor(term, f);
e -= shift;
eval_ldexp(to, to, shift);
typename boost::multiprecision::detail::canonical<std::intmax_t, To>::type ll;
eval_convert_to(&ll, term);
eval_add(to, ll);
eval_subtract(f, term);
}
using to_exponent = typename To::exponent_type;
if (e > (std::numeric_limits<to_exponent>::max)())
{
to = static_cast<const char*>("inf");
if (eval_get_sign(from) < 0)
to.negate();
return;
}
if (e < (std::numeric_limits<to_exponent>::min)())
{
to = ui_type(0);
if (eval_get_sign(from) < 0)
to.negate();
return;
}
eval_ldexp(to, to, static_cast<to_exponent>(e));
}
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/)
{
using to_component_type = typename component_type<number<To> >::type;
number<From> t(from);
to_component_type n(numerator(t)), d(denominator(t));
using default_ops::assign_components;
assign_components(to, n.backend(), d.backend());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/)
{
using to_component_type = typename component_type<number<To> >::type;
number<From> t(from);
to_component_type n(t), d(1);
using default_ops::assign_components;
assign_components(to, n.backend(), d.backend());
}
template <class LargeInteger>
inline typename std::enable_if<is_signed_number<LargeInteger>::value>::type make_positive(LargeInteger& val)
{
if (val.sign() < 0)
val = -val;
}
template <class LargeInteger>
inline typename std::enable_if<!is_signed_number<LargeInteger>::value>::type make_positive(LargeInteger&){}
template <class R, class LargeInteger>
R safe_convert_to_float(const LargeInteger& i)
{
if (!i)
return R(0);
BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::max_exponent)
{
using std::ldexp;
LargeInteger val(i);
make_positive(val);
std::size_t mb = msb(val);
if (mb >= std::numeric_limits<R>::max_exponent)
{
int scale_factor = static_cast<int>(mb) + 1 - std::numeric_limits<R>::max_exponent;
BOOST_MP_ASSERT(scale_factor >= 1);
val >>= scale_factor;
R result = val.template convert_to<R>();
BOOST_IF_CONSTEXPR(std::numeric_limits<R>::digits == 0 || std::numeric_limits<R>::digits >= std::numeric_limits<R>::max_exponent)
{
//
// Calculate and add on the remainder, only if there are more
// digits in the mantissa that the size of the exponent, in
// other words if we are dropping digits in the conversion
// otherwise:
//
LargeInteger remainder(i);
remainder &= (LargeInteger(1) << scale_factor) - 1;
result += ldexp(safe_convert_to_float<R>(remainder), -scale_factor);
}
return i.sign() < 0 ? static_cast<R>(-result) : result;
}
}
return i.template convert_to<R>();
}
template <class To, class Integer>
inline typename std::enable_if<!(is_number<To>::value || std::is_floating_point<To>::value)>::type
generic_convert_rational_to_float_imp(To& result, const Integer& n, const Integer& d, const std::integral_constant<bool, true>&)
{
//
// If we get here, then there's something about one type or the other
// that prevents an exactly rounded result from being calculated
// (or at least it's not clear how to implement such a thing).
//
using default_ops::eval_divide;
number<To> fn(safe_convert_to_float<number<To> >(n)), fd(safe_convert_to_float<number<To> >(d));
eval_divide(result, fn.backend(), fd.backend());
}
template <class To, class Integer>
inline typename std::enable_if<is_number<To>::value || std::is_floating_point<To>::value>::type
generic_convert_rational_to_float_imp(To& result, const Integer& n, const Integer& d, const std::integral_constant<bool, true>&)
{
//
// If we get here, then there's something about one type or the other
// that prevents an exactly rounded result from being calculated
// (or at least it's not clear how to implement such a thing).
//
To fd(safe_convert_to_float<To>(d));
result = safe_convert_to_float<To>(n);
result /= fd;
}
template <class To, class Integer>
typename std::enable_if<is_number<To>::value || std::is_floating_point<To>::value>::type
generic_convert_rational_to_float_imp(To& result, Integer& num, Integer& denom, const std::integral_constant<bool, false>&)
{
//
// If we get here, then the precision of type To is known, and the integer type is unbounded
// so we can use integer division plus manipulation of the remainder to get an exactly
// rounded result.
//
if (num == 0)
{
result = 0;
return;
}
bool s = false;
if (num < 0)
{
s = true;
num = -num;
}
std::ptrdiff_t denom_bits = msb(denom);
std::ptrdiff_t shift = std::numeric_limits<To>::digits + denom_bits - msb(num);
if (shift > 0)
num <<= shift;
else if (shift < 0)
denom <<= boost::multiprecision::detail::unsigned_abs(shift);
Integer q, r;
divide_qr(num, denom, q, r);
std::ptrdiff_t q_bits = msb(q);
if (q_bits == std::numeric_limits<To>::digits - 1)
{
//
// Round up if 2 * r > denom:
//
r <<= 1;
int c = r.compare(denom);
if (c > 0)
++q;
else if ((c == 0) && (q & 1u))
{
++q;
}
}
else
{
BOOST_MP_ASSERT(q_bits == std::numeric_limits<To>::digits);
//
// We basically already have the rounding info:
//
if (q & 1u)
{
if (r || (q & 2u))
++q;
}
}
using std::ldexp;
result = do_cast<To>(q);
result = ldexp(result, static_cast<int>(-shift));
if (s)
result = -result;
}
template <class To, class Integer>
inline typename std::enable_if<!(is_number<To>::value || std::is_floating_point<To>::value)>::type
generic_convert_rational_to_float_imp(To& result, Integer& num, Integer& denom, const std::integral_constant<bool, false>& tag)
{
number<To> t;
generic_convert_rational_to_float_imp(t, num, denom, tag);
result = t.backend();
}
template <class To, class From>
inline void generic_convert_rational_to_float(To& result, const From& f)
{
//
// Type From is always a Backend to number<>, or an
// instance of number<>, but we allow
// To to be either a Backend type, or a real number type,
// that way we can call this from generic conversions, and
// from specific conversions to built in types.
//
using actual_from_type = typename std::conditional<is_number<From>::value, From, number<From> >::type ;
using actual_to_type = typename std::conditional<is_number<To>::value || std::is_floating_point<To>::value, To, number<To> >::type ;
using integer_type = typename component_type<actual_from_type>::type ;
using dispatch_tag = std::integral_constant<bool, !std::numeric_limits<integer_type>::is_specialized || std::numeric_limits<integer_type>::is_bounded || !std::numeric_limits<actual_to_type>::is_specialized || !std::numeric_limits<actual_to_type>::is_bounded || (std::numeric_limits<actual_to_type>::radix != 2)>;
integer_type n(numerator(static_cast<actual_from_type>(f))), d(denominator(static_cast<actual_from_type>(f)));
generic_convert_rational_to_float_imp(result, n, d, dispatch_tag());
}
template <class To, class From>
inline void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/)
{
generic_convert_rational_to_float(to, from);
}
template <class To, class From>
void generic_interconvert_float2rational(To& to, const From& from, const std::integral_constant<int, 2>& /*radix*/)
{
using std::ldexp;
using std::frexp;
using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
constexpr int shift = std::numeric_limits<long long>::digits;
typename From::exponent_type e;
typename component_type<number<To>>::type num, denom;
number<From> val(from);
val = frexp(val, &e);
while (val)
{
val = ldexp(val, shift);
e -= shift;
long long ll = boost::multiprecision::detail::lltrunc(val);
val -= ll;
num <<= shift;
num += ll;
}
denom = ui_type(1u);
if (e < 0)
denom <<= -e;
else if (e > 0)
num <<= e;
assign_components(to, num.backend(), denom.backend());
}
template <class To, class From, int Radix>
void generic_interconvert_float2rational(To& to, const From& from, const std::integral_constant<int, Radix>& /*radix*/)
{
using std::ilogb;
using std::scalbn;
using std::pow;
using std::abs;
//
// This is almost the same as the binary case above, but we have to use
// scalbn and ilogb rather than ldexp and frexp, we also only extract
// one Radix digit at a time which is terribly inefficient!
//
using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
typename From::exponent_type e;
typename component_type<number<To>>::type num, denom;
number<From> val(from);
if (!val)
{
to = ui_type(0u);
return;
}
e = ilogb(val);
val = scalbn(val, -e);
while (val)
{
long long ll = boost::multiprecision::detail::lltrunc(val);
val -= ll;
val = scalbn(val, 1);
num *= Radix;
num += ll;
--e;
}
++e;
denom = ui_type(Radix);
denom = pow(denom, abs(e));
if (e > 0)
{
num *= denom;
denom = 1;
}
assign_components(to, num.backend(), denom.backend());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/)
{
generic_interconvert_float2rational(to, from, std::integral_constant<int, std::numeric_limits<number<From> >::is_specialized ? std::numeric_limits<number<From> >::radix : 2>());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/)
{
number<From> t(from);
number<To> result(numerator(t) / denominator(t));
to = result.backend();
}
template <class To, class From>
void generic_interconvert_float2int(To& to, const From& from, const std::integral_constant<int, 2>& /*radix*/)
{
using std::frexp;
using std::ldexp;
using exponent_type = typename From::exponent_type;
constexpr exponent_type shift = std::numeric_limits<long long>::digits;
exponent_type e;
number<To> num(0u);
number<From> val(from);
val = frexp(val, &e);
bool neg = false;
if (val.sign() < 0)
{
val.backend().negate();
neg = true;
}
while (e > 0)
{
exponent_type s = (std::min)(e, shift);
val = ldexp(val, s);
e -= s;
long long ll = boost::multiprecision::detail::lltrunc(val);
val -= ll;
num <<= s;
num += ll;
}
to = num.backend();
if (neg)
to.negate();
}
template <class To, class From, int Radix>
void generic_interconvert_float2int(To& to, const From& from, const std::integral_constant<int, Radix>& /*radix*/)
{
using std::ilogb;
using std::scalbn;
//
// This is almost the same as the binary case above, but we have to use
// scalbn and ilogb rather than ldexp and frexp, we also only extract
// one Radix digit at a time which is terribly inefficient!
//
typename From::exponent_type e;
number<To> num(0u);
number<From> val(from);
e = ilogb(val);
val = scalbn(val, -e);
while (e >= 0)
{
long long ll = boost::multiprecision::detail::lltrunc(val);
val -= ll;
val = scalbn(val, 1);
num *= Radix;
num += ll;
--e;
}
to = num.backend();
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/)
{
generic_interconvert_float2int(to, from, std::integral_constant<int, (std::numeric_limits<number<From> >::is_specialized ? std::numeric_limits<number<From> >::radix : 2)>());
}
template <class To, class From, class tag>
void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, true>&, const tag&)
{
// We just want the real part, and "to" is the correct type already:
eval_real(to, from);
To im;
eval_imag(im, from);
if (!eval_is_zero(im))
BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
}
template <class To, class From>
void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&)
{
using component_number = typename component_type<number<From> >::type;
using component_backend = typename component_number::backend_type ;
//
// Get the real part and copy-construct the result from it:
//
scoped_precision_options<component_number> scope(from);
component_backend r;
generic_interconvert_complex_to_scalar(r, from, std::integral_constant<bool, true>(), std::integral_constant<bool, true>());
to = r;
}
template <class To, class From>
void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&)
{
using component_number = typename component_type<number<From> >::type;
using component_backend = typename component_number::backend_type;
//
// Get the real part and use a generic_interconvert to type To:
//
scoped_precision_options<component_number> scope(from);
component_backend r;
generic_interconvert_complex_to_scalar(r, from, std::integral_constant<bool, true>(), std::integral_constant<bool, true>());
generic_interconvert(to, r, std::integral_constant<int, number_category<To>::value>(), std::integral_constant<int, number_category<component_backend>::value>());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_complex>& /*from_type*/)
{
using component_number = typename component_type<number<From> >::type;
using component_backend = typename component_number::backend_type ;
generic_interconvert_complex_to_scalar(to, from, std::integral_constant<bool, std::is_same<component_backend, To>::value>(), std::integral_constant<bool, std::is_constructible<To, const component_backend&>::value>());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_complex>& /*from_type*/)
{
using component_number = typename component_type<number<From> >::type;
using component_backend = typename component_number::backend_type ;
generic_interconvert_complex_to_scalar(to, from, std::integral_constant<bool, std::is_same<component_backend, To>::value>(), std::integral_constant<bool, std::is_constructible<To, const component_backend&>::value>());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_complex>& /*from_type*/)
{
using component_number = typename component_type<number<From> >::type;
using component_backend = typename component_number::backend_type ;
generic_interconvert_complex_to_scalar(to, from, std::integral_constant<bool, std::is_same<component_backend, To>::value>(), std::integral_constant<bool, std::is_constructible<To, const component_backend&>::value>());
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_complex>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/)
{
using component_number = typename component_type<number<To> >::type;
scoped_source_precision<number<From> > scope1;
scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
(void)scope1;
(void)scope2;
number<From> f(from);
component_number scalar(f);
number<To> result(scalar);
to = result.backend();
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_complex>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/)
{
using component_number = typename component_type<number<To> >::type;
scoped_source_precision<number<From> > scope1;
scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
(void)scope1;
(void)scope2;
number<From> f(from);
component_number scalar(f);
number<To> result(scalar);
to = result.backend();
}
template <class To, class From>
void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_complex>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/)
{
using component_number = typename component_type<number<To> >::type;
scoped_source_precision<number<From> > scope1;
scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
(void)scope1;
(void)scope2;
number<From> f(from);
component_number scalar(f);
number<To> result(scalar);
to = result.backend();
}
template <class To, class From, int Tag1, int Tag2>
void generic_interconvert(To& /*to*/, const From& /*from*/, const std::integral_constant<int, Tag1>& /*to_type*/, const std::integral_constant<int, Tag2>& /*from_type*/)
{
static_assert(sizeof(To) == 0, "Sorry, you asked for a conversion bewteen types that hasn't been implemented yet!!");
}
}
}
} // namespace boost::multiprecision::detail
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#endif // BOOST_MP_GENERIC_INTERCONVERT_HPP

View file

@ -0,0 +1,56 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2021 Matt Borland. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_DETAIL_HASH_HPP
#define BOOST_MP_DETAIL_HASH_HPP
#include <cstddef>
#include <functional>
#include <boost/multiprecision/detail/standalone_config.hpp>
namespace boost { namespace multiprecision { namespace detail {
template <typename T>
inline std::size_t hash_value(const T& v)
{
std::hash<T> hasher;
return hasher(v);
}
#if defined(BOOST_HAS_INT128)
std::size_t hash_value(const uint128_type& val);
inline std::size_t hash_value(const int128_type& val)
{
return hash_value(static_cast<uint128_type>(val));
}
#endif
inline void hash_combine(std::size_t&) {}
template <typename T, typename... Args>
inline void hash_combine(std::size_t& seed, const T& v, Args... args)
{
constexpr std::size_t adder = 0x9e3779b9;
seed = seed ^ (hash_value(v) + adder + (seed<<6) + (seed>>2));
hash_combine(seed, args...);
}
#if defined(BOOST_HAS_INT128)
inline std::size_t hash_value(const uint128_type& val)
{
std::size_t result = static_cast<std::size_t>(val);
hash_combine(result, static_cast<std::size_t>(val >> 64));
return result;
}
#endif
}}} // Namespaces
#endif // BOOST_MP_DETAIL_HASH_HPP

View file

@ -0,0 +1,474 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_DETAIL_INTEGER_OPS_HPP
#define BOOST_MP_DETAIL_INTEGER_OPS_HPP
#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
namespace boost { namespace multiprecision {
namespace default_ops {
template <class Backend>
inline BOOST_MP_CXX14_CONSTEXPR void eval_qr(const Backend& x, const Backend& y, Backend& q, Backend& r)
{
eval_divide(q, x, y);
eval_modulus(r, x, y);
}
template <class Backend, class Integer>
inline BOOST_MP_CXX14_CONSTEXPR Integer eval_integer_modulus(const Backend& x, Integer val)
{
BOOST_MP_USING_ABS
using default_ops::eval_convert_to;
using default_ops::eval_modulus;
using int_type = typename boost::multiprecision::detail::canonical<Integer, Backend>::type;
Backend t;
eval_modulus(t, x, static_cast<int_type>(val));
Integer result(0);
eval_convert_to(&result, t);
return abs(result);
}
template <class B>
inline BOOST_MP_CXX14_CONSTEXPR void eval_gcd(B& result, const B& a, const B& b)
{
using default_ops::eval_get_sign;
using default_ops::eval_is_zero;
using default_ops::eval_lsb;
std::ptrdiff_t shift(0);
B u(a), v(b);
int s = eval_get_sign(u);
/* GCD(0,x) := x */
if (s < 0)
{
u.negate();
}
else if (s == 0)
{
result = v;
return;
}
s = eval_get_sign(v);
if (s < 0)
{
v.negate();
}
else if (s == 0)
{
result = u;
return;
}
/* Let shift := lg K, where K is the greatest power of 2
dividing both u and v. */
std::size_t us = eval_lsb(u);
std::size_t vs = eval_lsb(v);
shift = static_cast<std::ptrdiff_t>((std::min)(us, vs));
eval_right_shift(u, us);
eval_right_shift(v, vs);
do
{
/* Now u and v are both odd, so diff(u, v) is even.
Let u = min(u, v), v = diff(u, v)/2. */
s = u.compare(v);
if (s > 0)
u.swap(v);
if (s == 0)
break;
eval_subtract(v, u);
vs = eval_lsb(v);
eval_right_shift(v, vs);
} while (true);
result = u;
eval_left_shift(result, shift);
}
template <class B>
inline BOOST_MP_CXX14_CONSTEXPR void eval_lcm(B& result, const B& a, const B& b)
{
using ui_type = typename std::tuple_element<0, typename B::unsigned_types>::type;
B t;
eval_gcd(t, a, b);
if (eval_is_zero(t))
{
result = static_cast<ui_type>(0);
}
else
{
eval_divide(result, a, t);
eval_multiply(result, b);
}
if (eval_get_sign(result) < 0)
result.negate();
}
} // namespace default_ops
template <class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer>::type
divide_qr(const number<Backend, ExpressionTemplates>& x, const number<Backend, ExpressionTemplates>& y,
number<Backend, ExpressionTemplates>& q, number<Backend, ExpressionTemplates>& r)
{
using default_ops::eval_qr;
eval_qr(x.backend(), y.backend(), q.backend(), r.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer>::type
divide_qr(const number<Backend, ExpressionTemplates>& x, const multiprecision::detail::expression<tag, A1, A2, A3, A4>& y,
number<Backend, ExpressionTemplates>& q, number<Backend, ExpressionTemplates>& r)
{
divide_qr(x, number<Backend, ExpressionTemplates>(y), q, r);
}
template <class tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer>::type
divide_qr(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& x, const number<Backend, ExpressionTemplates>& y,
number<Backend, ExpressionTemplates>& q, number<Backend, ExpressionTemplates>& r)
{
divide_qr(number<Backend, ExpressionTemplates>(x), y, q, r);
}
template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer>::type
divide_qr(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& x, const multiprecision::detail::expression<tagb, A1b, A2b, A3b, A4b>& y,
number<Backend, ExpressionTemplates>& q, number<Backend, ExpressionTemplates>& r)
{
divide_qr(number<Backend, ExpressionTemplates>(x), number<Backend, ExpressionTemplates>(y), q, r);
}
template <class Backend, expression_template_option ExpressionTemplates, class Integer>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value && (number_category<Backend>::value == number_kind_integer), Integer>::type
integer_modulus(const number<Backend, ExpressionTemplates>& x, Integer val)
{
using default_ops::eval_integer_modulus;
return eval_integer_modulus(x.backend(), val);
}
template <class tag, class A1, class A2, class A3, class A4, class Integer>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value && (number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_integer), Integer>::type
integer_modulus(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& x, Integer val)
{
using result_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
return integer_modulus(result_type(x), val);
}
template <class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer, std::size_t>::type
lsb(const number<Backend, ExpressionTemplates>& x)
{
using default_ops::eval_lsb;
return eval_lsb(x.backend());
}
template <class tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_integer, std::size_t>::type
lsb(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& x)
{
using number_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
number_type n(x);
using default_ops::eval_lsb;
return eval_lsb(n.backend());
}
template <class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer, std::size_t>::type
msb(const number<Backend, ExpressionTemplates>& x)
{
using default_ops::eval_msb;
return eval_msb(x.backend());
}
template <class tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_integer, std::size_t>::type
msb(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& x)
{
using number_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
number_type n(x);
using default_ops::eval_msb;
return eval_msb(n.backend());
}
template <class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer, bool>::type
bit_test(const number<Backend, ExpressionTemplates>& x, std::size_t index)
{
using default_ops::eval_bit_test;
return eval_bit_test(x.backend(), index);
}
template <class tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_integer, bool>::type
bit_test(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& x, std::size_t index)
{
using number_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
number_type n(x);
using default_ops::eval_bit_test;
return eval_bit_test(n.backend(), index);
}
template <class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer, number<Backend, ExpressionTemplates>&>::type
bit_set(number<Backend, ExpressionTemplates>& x, std::size_t index)
{
using default_ops::eval_bit_set;
eval_bit_set(x.backend(), index);
return x;
}
template <class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer, number<Backend, ExpressionTemplates>&>::type
bit_unset(number<Backend, ExpressionTemplates>& x, std::size_t index)
{
using default_ops::eval_bit_unset;
eval_bit_unset(x.backend(), index);
return x;
}
template <class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_integer, number<Backend, ExpressionTemplates>&>::type
bit_flip(number<Backend, ExpressionTemplates>& x, std::size_t index)
{
using default_ops::eval_bit_flip;
eval_bit_flip(x.backend(), index);
return x;
}
namespace default_ops {
//
// Within powm, we need a type with twice as many digits as the argument type, define
// a traits class to obtain that type:
//
template <class Backend>
struct double_precision_type
{
using type = Backend;
};
//
// If the exponent is a signed integer type, then we need to
// check the value is positive:
//
template <class Backend>
inline BOOST_MP_CXX14_CONSTEXPR void check_sign_of_backend(const Backend& v, const std::integral_constant<bool, true>)
{
if (eval_get_sign(v) < 0)
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
}
}
template <class Backend>
inline BOOST_MP_CXX14_CONSTEXPR void check_sign_of_backend(const Backend&, const std::integral_constant<bool, false>) {}
//
// Calculate (a^p)%c:
//
template <class Backend>
BOOST_MP_CXX14_CONSTEXPR void eval_powm(Backend& result, const Backend& a, const Backend& p, const Backend& c)
{
using default_ops::eval_bit_test;
using default_ops::eval_get_sign;
using default_ops::eval_modulus;
using default_ops::eval_multiply;
using default_ops::eval_right_shift;
using double_type = typename double_precision_type<Backend>::type ;
using ui_type = typename boost::multiprecision::detail::canonical<unsigned char, double_type>::type;
check_sign_of_backend(p, std::integral_constant<bool, std::numeric_limits<number<Backend> >::is_signed>());
double_type x, y(a), b(p), t;
x = ui_type(1u);
while (eval_get_sign(b) > 0)
{
if (eval_bit_test(b, 0))
{
eval_multiply(t, x, y);
eval_modulus(x, t, c);
}
eval_multiply(t, y, y);
eval_modulus(y, t, c);
eval_right_shift(b, ui_type(1));
}
Backend x2(x);
eval_modulus(result, x2, c);
}
template <class Backend, class Integer>
BOOST_MP_CXX14_CONSTEXPR void eval_powm(Backend& result, const Backend& a, const Backend& p, Integer c)
{
using double_type = typename double_precision_type<Backend>::type ;
using ui_type = typename boost::multiprecision::detail::canonical<unsigned char, double_type>::type;
using i1_type = typename boost::multiprecision::detail::canonical<Integer, double_type>::type ;
using i2_type = typename boost::multiprecision::detail::canonical<Integer, Backend>::type ;
using default_ops::eval_bit_test;
using default_ops::eval_get_sign;
using default_ops::eval_modulus;
using default_ops::eval_multiply;
using default_ops::eval_right_shift;
check_sign_of_backend(p, std::integral_constant<bool, std::numeric_limits<number<Backend> >::is_signed>());
if (eval_get_sign(p) < 0)
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
}
double_type x, y(a), b(p), t;
x = ui_type(1u);
while (eval_get_sign(b) > 0)
{
if (eval_bit_test(b, 0))
{
eval_multiply(t, x, y);
eval_modulus(x, t, static_cast<i1_type>(c));
}
eval_multiply(t, y, y);
eval_modulus(y, t, static_cast<i1_type>(c));
eval_right_shift(b, ui_type(1));
}
Backend x2(x);
eval_modulus(result, x2, static_cast<i2_type>(c));
}
template <class Backend, class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_unsigned<Integer>::value >::type eval_powm(Backend& result, const Backend& a, Integer b, const Backend& c)
{
using double_type = typename double_precision_type<Backend>::type ;
using ui_type = typename boost::multiprecision::detail::canonical<unsigned char, double_type>::type;
using default_ops::eval_bit_test;
using default_ops::eval_get_sign;
using default_ops::eval_modulus;
using default_ops::eval_multiply;
using default_ops::eval_right_shift;
double_type x, y(a), t;
x = ui_type(1u);
while (b > 0)
{
if (b & 1)
{
eval_multiply(t, x, y);
eval_modulus(x, t, c);
}
eval_multiply(t, y, y);
eval_modulus(y, t, c);
b >>= 1;
}
Backend x2(x);
eval_modulus(result, x2, c);
}
template <class Backend, class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_signed<Integer>::value && boost::multiprecision::detail::is_integral<Integer>::value>::type eval_powm(Backend& result, const Backend& a, Integer b, const Backend& c)
{
if (b < 0)
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
}
eval_powm(result, a, static_cast<typename boost::multiprecision::detail::make_unsigned<Integer>::type>(b), c);
}
template <class Backend, class Integer1, class Integer2>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_unsigned<Integer1>::value >::type eval_powm(Backend& result, const Backend& a, Integer1 b, Integer2 c)
{
using double_type = typename double_precision_type<Backend>::type ;
using ui_type = typename boost::multiprecision::detail::canonical<unsigned char, double_type>::type;
using i1_type = typename boost::multiprecision::detail::canonical<Integer1, double_type>::type ;
using i2_type = typename boost::multiprecision::detail::canonical<Integer2, Backend>::type ;
using default_ops::eval_bit_test;
using default_ops::eval_get_sign;
using default_ops::eval_modulus;
using default_ops::eval_multiply;
using default_ops::eval_right_shift;
double_type x, y(a), t;
x = ui_type(1u);
while (b > 0)
{
if (b & 1)
{
eval_multiply(t, x, y);
eval_modulus(x, t, static_cast<i1_type>(c));
}
eval_multiply(t, y, y);
eval_modulus(y, t, static_cast<i1_type>(c));
b >>= 1;
}
Backend x2(x);
eval_modulus(result, x2, static_cast<i2_type>(c));
}
template <class Backend, class Integer1, class Integer2>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_signed<Integer1>::value && boost::multiprecision::detail::is_integral<Integer1>::value>::type eval_powm(Backend& result, const Backend& a, Integer1 b, Integer2 c)
{
if (b < 0)
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
}
eval_powm(result, a, static_cast<typename boost::multiprecision::detail::make_unsigned<Integer1>::type>(b), c);
}
struct powm_func
{
template <class T, class U, class V>
BOOST_MP_CXX14_CONSTEXPR void operator()(T& result, const T& b, const U& p, const V& m) const
{
eval_powm(result, b, p, m);
}
template <class R, class T, class U, class V>
BOOST_MP_CXX14_CONSTEXPR void operator()(R& result, const T& b, const U& p, const V& m) const
{
T temp;
eval_powm(temp, b, p, m);
result = std::move(temp);
}
};
} // namespace default_ops
template <class T, class U, class V>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
(number_category<T>::value == number_kind_integer) &&
(is_number<T>::value || is_number_expression<T>::value) &&
(is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_integral<U>::value) &&
(is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_integral<V>::value),
typename std::conditional<
is_no_et_number<T>::value,
T,
typename std::conditional<
is_no_et_number<U>::value,
U,
typename std::conditional<
is_no_et_number<V>::value,
V,
detail::expression<detail::function, default_ops::powm_func, T, U, V> >::type>::type>::type>::type
powm(const T& b, const U& p, const V& mod)
{
return detail::expression<detail::function, default_ops::powm_func, T, U, V>(
default_ops::powm_func(), b, p, mod);
}
}} // namespace boost::multiprecision
#endif

View file

@ -0,0 +1,39 @@
///////////////////////////////////////////////////////////////
// Copyright 2019 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// We used to use lexical_cast internally for quick conversions from integers
// to strings, but that breaks if the global locale is something other than "C".
// See https://github.com/boostorg/multiprecision/issues/167.
//
#ifndef BOOST_MP_DETAIL_ITOS_HPP
#define BOOST_MP_DETAIL_ITOS_HPP
namespace boost { namespace multiprecision { namespace detail {
template <class Integer>
std::string itos(Integer val)
{
if (!val) return "0";
std::string result;
bool isneg = false;
if (val < 0)
{
val = -val;
isneg = true;
}
while (val)
{
result.insert(result.begin(), char('0' + (val % 10)));
val /= 10;
}
if (isneg)
result.insert(result.begin(), '-');
return result;
}
}}} // namespace boost::multiprecision::detail
#endif

View file

@ -0,0 +1,106 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2016 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_MIN_MAX_HPP
#define BOOST_MP_MIN_MAX_HPP
#include <boost/multiprecision/traits/is_backend.hpp>
namespace boost { namespace multiprecision {
//
// Expression template overloads for (min) and (max):
//
// Introduced in response to https://svn.boost.org/trac/boost/ticket/11149
// note that these can not legally be injected into namespace std, and that doing so
// may break future enhancements to the standard. None the less adding
// namespace std{ using boost::multiprecision::(min); using boost::multiprecision::(max); }
// to your code may get some generic code working that wouldn't work otherwise.
//
// The use of enable_if on the return type is to avoid poisoning std::min/max,
// otherwise attempting to make an explicit call to min<long>(a, b) when these and std
// versions are in scope, will cause the compiler to try to instantiate the signatures
// for our versions as well as the std ones, which in turn instantiates number<long>
// which fails to compile as "long" is not a valid backend type.
//
template <class Backend>
inline typename std::enable_if<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on>&>::type(min)(const number<Backend, et_on>& a, const number<Backend, et_on>& b)
{
return a < b ? a : b;
}
template <class Backend, class tag, class A1, class A2, class A3, class A4>
inline typename std::enable_if<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on> >::type(min)(const number<Backend, et_on>& a, const detail::expression<tag, A1, A2, A3, A4>& b)
{
number<Backend, et_on> t(b);
if (a < t)
return a;
return t;
}
template <class tag, class A1, class A2, class A3, class A4, class Backend>
inline typename std::enable_if<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on> >::type(min)(const detail::expression<tag, A1, A2, A3, A4>& a, const number<Backend, et_on>& b)
{
number<Backend, et_on> t(a);
if (t < b)
return t;
return b;
}
template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
inline typename detail::expression<tag, A1, A2, A3, A4>::result_type(min)(const detail::expression<tag, A1, A2, A3, A4>& a, const detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
{
typename detail::expression<tag, A1, A2, A3, A4>::result_type t1(a), t2(b);
if (t1 < t2)
return t1;
return t2;
}
template <class tag, class A1, class A2, class A3, class A4>
inline typename detail::expression<tag, A1, A2, A3, A4>::result_type(min)(const detail::expression<tag, A1, A2, A3, A4>& a, const detail::expression<tag, A1, A2, A3, A4>& b)
{
typename detail::expression<tag, A1, A2, A3, A4>::result_type t1(a), t2(b);
if (t1 < t2)
return t1;
return t2;
}
template <class Backend>
inline typename std::enable_if<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on>&>::type(max)(const number<Backend, et_on>& a, const number<Backend, et_on>& b)
{
return a > b ? a : b;
}
template <class Backend, class tag, class A1, class A2, class A3, class A4>
inline typename std::enable_if<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on> >::type(max)(const number<Backend, et_on>& a, const detail::expression<tag, A1, A2, A3, A4>& b)
{
number<Backend, et_on> t(b);
if (a > t)
return a;
return t;
}
template <class tag, class A1, class A2, class A3, class A4, class Backend>
inline typename std::enable_if<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on> >::type(max)(const detail::expression<tag, A1, A2, A3, A4>& a, const number<Backend, et_on>& b)
{
number<Backend, et_on> t(a);
if (t > b)
return t;
return b;
}
template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
inline typename detail::expression<tag, A1, A2, A3, A4>::result_type(max)(const detail::expression<tag, A1, A2, A3, A4>& a, const detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
{
typename detail::expression<tag, A1, A2, A3, A4>::result_type t1(a), t2(b);
if (t1 > t2)
return t1;
return t2;
}
template <class tag, class A1, class A2, class A3, class A4>
inline typename detail::expression<tag, A1, A2, A3, A4>::result_type(max)(const detail::expression<tag, A1, A2, A3, A4>& a, const detail::expression<tag, A1, A2, A3, A4>& b)
{
typename detail::expression<tag, A1, A2, A3, A4>::result_type t1(a), t2(b);
if (t1 > t2)
return t1;
return t2;
}
}} // namespace boost::multiprecision
#endif

View file

@ -0,0 +1,661 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_NO_ET_OPS_HPP
#define BOOST_MP_NO_ET_OPS_HPP
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4714)
#endif
namespace boost {
namespace multiprecision {
//
// Operators for non-expression template enabled number.
// NOTE: this is not a complete header - really just a suffix to default_ops.hpp.
// NOTE: these operators have to be defined after the methods in default_ops.hpp.
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator-(const number<B, et_off>& v)
{
static_assert(is_signed_number<B>::value, "Negating an unsigned type results in ill-defined behavior.");
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(v);
number<B, et_off> result(v);
result.backend().negate();
return result;
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator~(const number<B, et_off>& v)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(v);
number<B, et_off> result;
eval_complement(result.backend(), v.backend());
return result;
}
//
// Addition:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator+(const number<B, et_off>& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_add;
eval_add(result.backend(), a.backend(), b.backend());
return result;
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value, number<B, et_off> >::type
operator+(const number<B, et_off>& a, const V& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_add;
eval_add(result.backend(), a.backend(), number<B, et_off>::canonical_value(b));
return result;
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator+(const V& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(b, a);
number<B, et_off> result;
using default_ops::eval_add;
eval_add(result.backend(), b.backend(), number<B, et_off>::canonical_value(a));
return result;
}
//
// Subtraction:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator-(const number<B, et_off>& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_subtract;
eval_subtract(result.backend(), a.backend(), b.backend());
return result;
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value, number<B, et_off> >::type
operator-(const number<B, et_off>& a, const V& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_subtract;
eval_subtract(result.backend(), a.backend(), number<B, et_off>::canonical_value(b));
return result;
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator-(const V& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(b, a);
number<B, et_off> result;
using default_ops::eval_subtract;
eval_subtract(result.backend(), number<B, et_off>::canonical_value(a), b.backend());
return result;
}
//
// Multiply:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator*(const number<B, et_off>& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_multiply;
eval_multiply(result.backend(), a.backend(), b.backend());
return result;
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value, number<B, et_off> >::type
operator*(const number<B, et_off>& a, const V& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_multiply;
eval_multiply(result.backend(), a.backend(), number<B, et_off>::canonical_value(b));
return result;
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator*(const V& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(b, a);
number<B, et_off> result;
using default_ops::eval_multiply;
eval_multiply(result.backend(), b.backend(), number<B, et_off>::canonical_value(a));
return result;
}
//
// divide:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator/(const number<B, et_off>& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_divide;
eval_divide(result.backend(), a.backend(), b.backend());
return result;
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value, number<B, et_off> >::type
operator/(const number<B, et_off>& a, const V& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_divide;
eval_divide(result.backend(), a.backend(), number<B, et_off>::canonical_value(b));
return result;
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator/(const V& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(b, a);
number<B, et_off> result;
using default_ops::eval_divide;
eval_divide(result.backend(), number<B, et_off>::canonical_value(a), b.backend());
return result;
}
//
// modulus:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator%(const number<B, et_off>& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
number<B, et_off> result;
using default_ops::eval_modulus;
eval_modulus(result.backend(), a.backend(), b.backend());
return result;
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator%(const number<B, et_off>& a, const V& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a);
number<B, et_off> result;
using default_ops::eval_modulus;
eval_modulus(result.backend(), a.backend(), number<B, et_off>::canonical_value(b));
return result;
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer) && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator%(const V& a, const number<B, et_off>& b)
{
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(b);
number<B, et_off> result;
using default_ops::eval_modulus;
eval_modulus(result.backend(), number<B, et_off>::canonical_value(a), b.backend());
return result;
}
//
// Bitwise or:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator|(const number<B, et_off>& a, const number<B, et_off>& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_or;
eval_bitwise_or(result.backend(), a.backend(), b.backend());
return result;
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator|(const number<B, et_off>& a, const V& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_or;
eval_bitwise_or(result.backend(), a.backend(), number<B, et_off>::canonical_value(b));
return result;
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer) && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator|(const V& a, const number<B, et_off>& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_or;
eval_bitwise_or(result.backend(), b.backend(), number<B, et_off>::canonical_value(a));
return result;
}
//
// Bitwise xor:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator^(const number<B, et_off>& a, const number<B, et_off>& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_xor;
eval_bitwise_xor(result.backend(), a.backend(), b.backend());
return result;
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator^(const number<B, et_off>& a, const V& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_xor;
eval_bitwise_xor(result.backend(), a.backend(), number<B, et_off>::canonical_value(b));
return result;
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer) && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator^(const V& a, const number<B, et_off>& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_xor;
eval_bitwise_xor(result.backend(), b.backend(), number<B, et_off>::canonical_value(a));
return result;
}
//
// Bitwise and:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator&(const number<B, et_off>& a, const number<B, et_off>& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_and;
eval_bitwise_and(result.backend(), a.backend(), b.backend());
return result;
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator&(const number<B, et_off>& a, const V& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_and;
eval_bitwise_and(result.backend(), a.backend(), number<B, et_off>::canonical_value(b));
return result;
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer) && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator&(const V& a, const number<B, et_off>& b)
{
number<B, et_off> result;
using default_ops::eval_bitwise_and;
eval_bitwise_and(result.backend(), b.backend(), number<B, et_off>::canonical_value(a));
return result;
}
//
// shifts:
//
template <class B, class I>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator<<(const number<B, et_off>& a, const I& b)
{
number<B, et_off> result(a);
using default_ops::eval_left_shift;
detail::check_shift_range(b, std::integral_constant<bool, (sizeof(I) > sizeof(std::size_t))>(), std::integral_constant<bool, boost::multiprecision::detail::is_signed<I>::value>());
eval_left_shift(result.backend(), b);
return result;
}
template <class B, class I>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator>>(const number<B, et_off>& a, const I& b)
{
number<B, et_off> result(a);
using default_ops::eval_right_shift;
detail::check_shift_range(b, std::integral_constant<bool, (sizeof(I) > sizeof(std::size_t))>(), std::integral_constant<bool, boost::multiprecision::detail::is_signed<I>::value>());
eval_right_shift(result.backend(), b);
return result;
}
//
// If we have rvalue references go all over again with rvalue ref overloads and move semantics.
// Note that while it would be tempting to implement these so they return an rvalue reference
// (and indeed this would be optimally efficient), this is unsafe due to users propensity to
// write:
//
// const T& t = a * b;
//
// which would lead to a dangling reference if we didn't return by value. Of course move
// semantics help a great deal in return by value, so performance is still pretty good...
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator-(number<B, et_off>&& v)
{
static_assert(is_signed_number<B>::value, "Negating an unsigned type results in ill-defined behavior.");
v.backend().negate();
return std::move(v);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator~(number<B, et_off>&& v)
{
eval_complement(v.backend(), v.backend());
return std::move(v);
}
//
// Addition:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator+(number<B, et_off>&& a, const number<B, et_off>& b)
{
using default_ops::eval_add;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_add(a.backend(), b.backend());
return std::move(a);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator+(const number<B, et_off>& a, number<B, et_off>&& b)
{
using default_ops::eval_add;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_add(b.backend(), a.backend());
return std::move(b);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator+(number<B, et_off>&& a, number<B, et_off>&& b)
{
using default_ops::eval_add;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_add(a.backend(), b.backend());
return std::move(a);
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value, number<B, et_off> >::type
operator+(number<B, et_off>&& a, const V& b)
{
using default_ops::eval_add;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_add(a.backend(), number<B, et_off>::canonical_value(b));
return std::move(a);
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator+(const V& a, number<B, et_off>&& b)
{
using default_ops::eval_add;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_add(b.backend(), number<B, et_off>::canonical_value(a));
return std::move(b);
}
//
// Subtraction:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator-(number<B, et_off>&& a, const number<B, et_off>& b)
{
using default_ops::eval_subtract;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_subtract(a.backend(), b.backend());
return std::move(a);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_signed_number<B>::value, number<B, et_off> >::type operator-(const number<B, et_off>& a, number<B, et_off>&& b)
{
using default_ops::eval_subtract;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_subtract(b.backend(), a.backend());
b.backend().negate();
return std::move(b);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator-(number<B, et_off>&& a, number<B, et_off>&& b)
{
using default_ops::eval_subtract;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_subtract(a.backend(), b.backend());
return std::move(a);
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value, number<B, et_off> >::type
operator-(number<B, et_off>&& a, const V& b)
{
using default_ops::eval_subtract;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_subtract(a.backend(), number<B, et_off>::canonical_value(b));
return std::move(a);
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(is_compatible_arithmetic_type<V, number<B, et_off> >::value && is_signed_number<B>::value) && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator-(const V& a, number<B, et_off>&& b)
{
using default_ops::eval_subtract;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_subtract(b.backend(), number<B, et_off>::canonical_value(a));
b.backend().negate();
return std::move(b);
}
//
// Multiply:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator*(number<B, et_off>&& a, const number<B, et_off>& b)
{
using default_ops::eval_multiply;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_multiply(a.backend(), b.backend());
return std::move(a);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator*(const number<B, et_off>& a, number<B, et_off>&& b)
{
using default_ops::eval_multiply;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_multiply(b.backend(), a.backend());
return std::move(b);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator*(number<B, et_off>&& a, number<B, et_off>&& b)
{
using default_ops::eval_multiply;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_multiply(a.backend(), b.backend());
return std::move(a);
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value, number<B, et_off> >::type
operator*(number<B, et_off>&& a, const V& b)
{
using default_ops::eval_multiply;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_multiply(a.backend(), number<B, et_off>::canonical_value(b));
return std::move(a);
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator*(const V& a, number<B, et_off>&& b)
{
using default_ops::eval_multiply;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_multiply(b.backend(), number<B, et_off>::canonical_value(a));
return std::move(b);
}
//
// divide:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<B, et_off> operator/(number<B, et_off>&& a, const number<B, et_off>& b)
{
using default_ops::eval_divide;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_divide(a.backend(), b.backend());
return std::move(a);
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value, number<B, et_off> >::type
operator/(number<B, et_off>&& a, const V& b)
{
using default_ops::eval_divide;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_divide(a.backend(), number<B, et_off>::canonical_value(b));
return std::move(a);
}
//
// modulus:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator%(number<B, et_off>&& a, const number<B, et_off>& b)
{
using default_ops::eval_modulus;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_modulus(a.backend(), b.backend());
return std::move(a);
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator%(number<B, et_off>&& a, const V& b)
{
using default_ops::eval_modulus;
detail::scoped_default_precision<multiprecision::number<B, et_off> > precision_guard(a, b);
eval_modulus(a.backend(), number<B, et_off>::canonical_value(b));
return std::move(a);
}
//
// Bitwise or:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator|(number<B, et_off>&& a, const number<B, et_off>& b)
{
using default_ops::eval_bitwise_or;
eval_bitwise_or(a.backend(), b.backend());
return std::move(a);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator|(const number<B, et_off>& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_or;
eval_bitwise_or(b.backend(), a.backend());
return std::move(b);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator|(number<B, et_off>&& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_or;
eval_bitwise_or(a.backend(), b.backend());
return std::move(a);
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator|(number<B, et_off>&& a, const V& b)
{
using default_ops::eval_bitwise_or;
eval_bitwise_or(a.backend(), number<B, et_off>::canonical_value(b));
return std::move(a);
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer) && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator|(const V& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_or;
eval_bitwise_or(b.backend(), number<B, et_off>::canonical_value(a));
return std::move(b);
}
//
// Bitwise xor:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator^(number<B, et_off>&& a, const number<B, et_off>& b)
{
using default_ops::eval_bitwise_xor;
eval_bitwise_xor(a.backend(), b.backend());
return std::move(a);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator^(const number<B, et_off>& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_xor;
eval_bitwise_xor(b.backend(), a.backend());
return std::move(b);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator^(number<B, et_off>&& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_xor;
eval_bitwise_xor(a.backend(), b.backend());
return std::move(a);
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator^(number<B, et_off>&& a, const V& b)
{
using default_ops::eval_bitwise_xor;
eval_bitwise_xor(a.backend(), number<B, et_off>::canonical_value(b));
return std::move(a);
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer) && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator^(const V& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_xor;
eval_bitwise_xor(b.backend(), number<B, et_off>::canonical_value(a));
return std::move(b);
}
//
// Bitwise and:
//
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator&(number<B, et_off>&& a, const number<B, et_off>& b)
{
using default_ops::eval_bitwise_and;
eval_bitwise_and(a.backend(), b.backend());
return std::move(a);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator&(const number<B, et_off>& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_and;
eval_bitwise_and(b.backend(), a.backend());
return std::move(b);
}
template <class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, et_off> >::type operator&(number<B, et_off>&& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_and;
eval_bitwise_and(a.backend(), b.backend());
return std::move(a);
}
template <class B, class V>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator&(number<B, et_off>&& a, const V& b)
{
using default_ops::eval_bitwise_and;
eval_bitwise_and(a.backend(), number<B, et_off>::canonical_value(b));
return std::move(a);
}
template <class V, class B>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_compatible_arithmetic_type<V, number<B, et_off> >::value && (number_category<B>::value == number_kind_integer) && !is_equivalent_number_type<V, B>::value, number<B, et_off> >::type
operator&(const V& a, number<B, et_off>&& b)
{
using default_ops::eval_bitwise_and;
eval_bitwise_and(b.backend(), number<B, et_off>::canonical_value(a));
return std::move(b);
}
//
// shifts:
//
template <class B, class I>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator<<(number<B, et_off>&& a, const I& b)
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<I>::type;
using default_ops::eval_left_shift;
eval_left_shift(a.backend(), static_cast<ui_type>(b));
return std::move(a);
}
template <class B, class I>
BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value && (number_category<B>::value == number_kind_integer), number<B, et_off> >::type
operator>>(number<B, et_off>&& a, const I& b)
{
using ui_type = typename boost::multiprecision::detail::make_unsigned<I>::type;
using default_ops::eval_right_shift;
eval_right_shift(a.backend(), static_cast<ui_type>(b));
return std::move(a);
}
}
} // namespace boost::multiprecision
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#endif // BOOST_MP_NO_ET_OPS_HPP

View file

@ -0,0 +1,55 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2004 - 2021 Pavel Vozenilek.
// Copyright 2021 Matt Borland. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_DETAIL_NO_EXCEPTIONS_SUPPORT_HPP
#define BOOST_MP_DETAIL_NO_EXCEPTIONS_SUPPORT_HPP
#include <boost/multiprecision/detail/standalone_config.hpp>
#ifdef BOOST_MP_STANDALONE
#ifndef BOOST_NO_EXCEPTIONS
# define BOOST_MP_TRY { try
# define BOOST_MP_CATCH(x) catch(x)
# define BOOST_MP_RETHROW throw;
# define BOOST_MP_CATCH_END }
# define BOOST_MP_THROW_EXCEPTION(x) throw (x);
#else
# if !defined(BOOST_MSVC) || BOOST_MSVC >= 1900
# define BOOST_MP_TRY { if (true)
# define BOOST_MP_CATCH(x) else if (false)
# else
// warning C4127: conditional expression is constant
# define BOOST_MP_TRY { \
__pragma(warning(push)) \
__pragma(warning(disable: 4127)) \
if (true) \
__pragma(warning(pop))
# define BOOST_MP_CATCH(x) else \
__pragma(warning(push)) \
__pragma(warning(disable: 4127)) \
if (false) \
__pragma(warning(pop))
# endif
# define BOOST_MP_RETHROW
# define BOOST_MP_CATCH_END }
# define BOOST_MP_THROW_EXCEPTION(x) {static_cast<void>(x);}
#endif
#else // Not standalone mode
# include <boost/core/no_exceptions_support.hpp>
# include <boost/throw_exception.hpp>
# define BOOST_MP_TRY BOOST_TRY
# define BOOST_MP_CATCH(x) BOOST_CATCH(x)
# define BOOST_MP_RETHROW BOOST_RETHROW
# define BOOST_MP_CATCH_END BOOST_CATCH_END
# define BOOST_MP_THROW_EXCEPTION(x) BOOST_THROW_EXCEPTION(x)
#endif // BOOST_MP_STANDALONE
#endif // BOOST_MP_DETAIL_NO_EXCEPTIONS_SUPPORT_HPP

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,848 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_NUMBER_COMPARE_HPP
#define BOOST_MP_NUMBER_COMPARE_HPP
#include <boost/multiprecision/traits/is_backend.hpp>
#include <boost/multiprecision/detail/fpclassify.hpp>
//
// Comparison operators for number.
//
namespace boost { namespace multiprecision {
namespace default_ops {
//
// The dispatching mechanism used here to deal with differently typed arguments
// could be better replaced with enable_if overloads, but that breaks MSVC-12
// under strange and hard to reproduce circumstances.
//
template <class B>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_eq(const B& a, const B& b)
{
return a.compare(b) == 0;
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_eq_imp(const T& a, const U& b, const std::integral_constant<bool, true>&)
{
typename boost::multiprecision::detail::number_from_backend<T, U>::type t(b);
return eval_eq(a, t.backend());
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_eq_imp(const T& a, const U& b, const std::integral_constant<bool, false>&)
{
typename boost::multiprecision::detail::number_from_backend<U, T>::type t(a);
return eval_eq(t.backend(), b);
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_eq(const T& a, const U& b)
{
using tag_type = std::integral_constant<bool, boost::multiprecision::detail::is_first_backend<T, U>::value>;
return eval_eq_imp(a, b, tag_type());
}
template <class B>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_lt(const B& a, const B& b)
{
return a.compare(b) < 0;
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_lt_imp(const T& a, const U& b, const std::integral_constant<bool, true>&)
{
typename boost::multiprecision::detail::number_from_backend<T, U>::type t(b);
return eval_lt(a, t.backend());
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_lt_imp(const T& a, const U& b, const std::integral_constant<bool, false>&)
{
typename boost::multiprecision::detail::number_from_backend<U, T>::type t(a);
return eval_lt(t.backend(), b);
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_lt(const T& a, const U& b)
{
using tag_type = std::integral_constant<bool, boost::multiprecision::detail::is_first_backend<T, U>::value>;
return eval_lt_imp(a, b, tag_type());
}
template <class B>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_gt(const B& a, const B& b)
{
return a.compare(b) > 0;
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_gt_imp(const T& a, const U& b, const std::integral_constant<bool, true>&)
{
typename boost::multiprecision::detail::number_from_backend<T, U>::type t(b);
return eval_gt(a, t.backend());
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_gt_imp(const T& a, const U& b, const std::integral_constant<bool, false>&)
{
typename boost::multiprecision::detail::number_from_backend<U, T>::type t(a);
return eval_gt(t.backend(), b);
}
template <class T, class U>
inline BOOST_MP_CXX14_CONSTEXPR bool eval_gt(const T& a, const U& b)
{
using tag_type = std::integral_constant<bool, boost::multiprecision::detail::is_first_backend<T, U>::value>;
return eval_gt_imp(a, b, tag_type());
}
} // namespace default_ops
namespace detail {
template <class Num, class Val>
struct is_valid_mixed_compare : public std::integral_constant<bool, false>
{};
template <class B, expression_template_option ET, class Val>
struct is_valid_mixed_compare<number<B, ET>, Val> : public std::is_convertible<Val, number<B, ET> >
{};
template <class B, expression_template_option ET>
struct is_valid_mixed_compare<number<B, ET>, number<B, ET> > : public std::integral_constant<bool, false>
{};
template <class B, expression_template_option ET, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
struct is_valid_mixed_compare<number<B, ET>, expression<tag, Arg1, Arg2, Arg3, Arg4> >
: public std::is_convertible<expression<tag, Arg1, Arg2, Arg3, Arg4>, number<B, ET> >
{};
template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class B, expression_template_option ET>
struct is_valid_mixed_compare<expression<tag, Arg1, Arg2, Arg3, Arg4>, number<B, ET> >
: public std::is_convertible<expression<tag, Arg1, Arg2, Arg3, Arg4>, number<B, ET> >
{};
template <class Backend, expression_template_option ExpressionTemplates>
inline constexpr typename std::enable_if<number_category<Backend>::value != number_kind_floating_point, bool>::type is_unordered_value(const number<Backend, ExpressionTemplates>&)
{
return false;
}
template <class Backend, expression_template_option ExpressionTemplates>
inline constexpr typename std::enable_if<number_category<Backend>::value == number_kind_floating_point, bool>::type is_unordered_value(const number<Backend, ExpressionTemplates>& a)
{
using default_ops::eval_fpclassify;
return eval_fpclassify(a.backend()) == FP_NAN;
}
template <class Arithmetic>
inline constexpr typename std::enable_if<number_category<Arithmetic>::value != number_kind_floating_point, bool>::type is_unordered_value(const Arithmetic&)
{
return false;
}
template <class Arithmetic>
inline
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
BOOST_MP_CXX14_CONSTEXPR
#endif
typename std::enable_if < number_category < Arithmetic> ::value == number_kind_floating_point, bool> ::type
is_unordered_value(const Arithmetic& a)
{
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(a))
{
return a != a;
}
else
#endif
{
return BOOST_MP_ISNAN(a);
}
}
template <class T, class U>
inline constexpr bool is_unordered_comparison(const T& a, const U& b)
{
return is_unordered_value(a) || is_unordered_value(b);
}
} // namespace detail
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR bool operator==(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b)
{
using default_ops::eval_eq;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_eq(a.backend(), b.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && !is_number_expression<Arithmetic>::value, bool>::type
operator==(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b)
{
using default_ops::eval_eq;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_eq(a.backend(), number<Backend, ExpressionTemplates>::canonical_value(b));
}
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && !is_number_expression<Arithmetic>::value, bool>::type
operator==(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b)
{
using default_ops::eval_eq;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_eq(b.backend(), number<Backend, ExpressionTemplates>::canonical_value(a));
}
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
operator==(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_eq;
result_type t(b);
if (detail::is_unordered_comparison(a, t))
return false;
return eval_eq(t.backend(), result_type::canonical_value(a));
}
template <class Backend, expression_template_option ExpressionTemplates, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR bool operator==(const number<Backend, ExpressionTemplates>& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_eq;
result_type t(b);
if (detail::is_unordered_comparison(a, t))
return false;
return eval_eq(t.backend(), a.backend());
}
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
operator==(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_eq;
result_type t(a);
if (detail::is_unordered_comparison(t, b))
return false;
return eval_eq(t.backend(), result_type::canonical_value(b));
}
template <class Tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR bool operator==(const detail::expression<Tag, A1, A2, A3, A4>& a, const number<Backend, ExpressionTemplates>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_eq;
result_type t(a);
if (detail::is_unordered_comparison(t, b))
return false;
return eval_eq(t.backend(), b.backend());
}
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value, bool>::type
operator==(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b)
{
using default_ops::eval_eq;
typename detail::expression<Tag, A1, A2, A3, A4>::result_type t(a);
typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type t2(b);
if (detail::is_unordered_comparison(t, t2))
return false;
return eval_eq(t.backend(), t2.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR bool operator!=(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b)
{
using default_ops::eval_eq;
if (detail::is_unordered_comparison(a, b))
return true;
return !eval_eq(a.backend(), b.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && !is_number_expression<Arithmetic>::value, bool>::type
operator!=(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b)
{
using default_ops::eval_eq;
if (detail::is_unordered_comparison(a, b))
return true;
return !eval_eq(a.backend(), number<Backend, et_on>::canonical_value(b));
}
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && !is_number_expression<Arithmetic>::value, bool>::type
operator!=(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b)
{
using default_ops::eval_eq;
if (detail::is_unordered_comparison(a, b))
return true;
return !eval_eq(b.backend(), number<Backend, et_on>::canonical_value(a));
}
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
operator!=(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_eq;
result_type t(b);
if (detail::is_unordered_comparison(a, t))
return true;
return !eval_eq(t.backend(), result_type::canonical_value(a));
}
template <class Backend, expression_template_option ExpressionTemplates, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR bool operator!=(const number<Backend, ExpressionTemplates>& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_eq;
result_type t(b);
if (detail::is_unordered_comparison(a, t))
return true;
return !eval_eq(t.backend(), a.backend());
}
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
operator!=(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_eq;
result_type t(a);
if (detail::is_unordered_comparison(t, b))
return true;
return !eval_eq(t.backend(), result_type::canonical_value(b));
}
template <class Tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR bool operator!=(const detail::expression<Tag, A1, A2, A3, A4>& a, const number<Backend, ExpressionTemplates>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_eq;
result_type t(a);
if (detail::is_unordered_comparison(t, b))
return true;
return !eval_eq(t.backend(), result_type::canonical_value(b));
}
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value, bool>::type
operator!=(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b)
{
using default_ops::eval_eq;
typename detail::expression<Tag, A1, A2, A3, A4>::result_type t(a);
typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type t2(b);
if (detail::is_unordered_comparison(t, t2))
return true;
return !eval_eq(t.backend(), t2.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value != number_kind_complex) && (number_category<Backend2>::value != number_kind_complex), bool>::type
operator<(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b)
{
using default_ops::eval_lt;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_lt(a.backend(), b.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && (number_category<Backend>::value != number_kind_complex) && !is_number_expression<Arithmetic>::value, bool>::type
operator<(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b)
{
using default_ops::eval_lt;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_lt(a.backend(), number<Backend, ExpressionTemplates>::canonical_value(b));
}
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && (number_category<Backend>::value != number_kind_complex) && !is_number_expression<Arithmetic>::value, bool>::type
operator<(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b)
{
using default_ops::eval_gt;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_gt(b.backend(), number<Backend, ExpressionTemplates>::canonical_value(a));
}
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator<(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_gt;
result_type t(b);
if (detail::is_unordered_comparison(a, t))
return false;
return eval_gt(t.backend(), result_type::canonical_value(a));
}
template <class Backend, expression_template_option ExpressionTemplates, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR bool operator<(const number<Backend, ExpressionTemplates>& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_gt;
result_type t(b);
return a < t;
}
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator<(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_lt;
result_type t(a);
if (detail::is_unordered_comparison(t, b))
return false;
return eval_lt(t.backend(), result_type::canonical_value(b));
}
template <class Tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR bool operator<(const detail::expression<Tag, A1, A2, A3, A4>& a, const number<Backend, ExpressionTemplates>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_lt;
result_type t(a);
return t < b;
}
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator<(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b)
{
using default_ops::eval_lt;
typename detail::expression<Tag, A1, A2, A3, A4>::result_type t(a);
typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type t2(b);
if (detail::is_unordered_comparison(t, t2))
return false;
return eval_lt(t.backend(), t2.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value != number_kind_complex) && (number_category<Backend2>::value != number_kind_complex), bool>::type
operator>(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b)
{
using default_ops::eval_gt;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_gt(a.backend(), b.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && (number_category<Backend>::value != number_kind_complex) && !is_number_expression<Arithmetic>::value, bool>::type
operator>(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b)
{
using default_ops::eval_gt;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_gt(a.backend(), number<Backend, ExpressionTemplates>::canonical_value(b));
}
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && (number_category<Backend>::value != number_kind_complex) && !is_number_expression<Arithmetic>::value, bool>::type
operator>(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b)
{
using default_ops::eval_lt;
if (detail::is_unordered_comparison(a, b))
return false;
return eval_lt(b.backend(), number<Backend, ExpressionTemplates>::canonical_value(a));
}
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator>(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_lt;
result_type t(b);
return a > t;
}
template <class Backend, expression_template_option ExpressionTemplates, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR bool operator>(const number<Backend, ExpressionTemplates>& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_lt;
result_type t(b);
return a > t;
}
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator>(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_gt;
result_type t(a);
return t > b;
}
template <class Tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR bool operator>(const detail::expression<Tag, A1, A2, A3, A4>& a, const number<Backend, ExpressionTemplates>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_gt;
result_type t(a);
return t > b;
}
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator>(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b)
{
using default_ops::eval_gt;
typename detail::expression<Tag, A1, A2, A3, A4>::result_type t(a);
typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type t2(b);
return t > t2;
}
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value != number_kind_complex) && (number_category<Backend2>::value != number_kind_complex), bool>::type
operator<=(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b)
{
using default_ops::eval_gt;
if (detail::is_unordered_comparison(a, b))
return false;
return !eval_gt(a.backend(), b.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && (number_category<Backend>::value != number_kind_complex) && !is_number_expression<Arithmetic>::value, bool>::type
operator<=(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b)
{
using default_ops::eval_gt;
if (detail::is_unordered_comparison(a, b))
return false;
return !eval_gt(a.backend(), number<Backend, ExpressionTemplates>::canonical_value(b));
}
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && (number_category<Backend>::value != number_kind_complex) && !is_number_expression<Arithmetic>::value, bool>::type
operator<=(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b)
{
using default_ops::eval_lt;
if (detail::is_unordered_comparison(a, b))
return false;
return !eval_lt(b.backend(), number<Backend, ExpressionTemplates>::canonical_value(a));
}
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator<=(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_lt;
if (detail::is_unordered_value(a) || detail::is_unordered_value(b))
return false;
result_type t(b);
if (detail::is_unordered_comparison(a, t))
return false;
return !eval_lt(t.backend(), result_type::canonical_value(a));
}
template <class Backend, expression_template_option ExpressionTemplates, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR bool operator<=(const number<Backend, ExpressionTemplates>& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_lt;
if (detail::is_unordered_value(a) || detail::is_unordered_value(b))
return false;
result_type t(b);
return a <= t;
}
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator<=(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_gt;
result_type t(a);
if (detail::is_unordered_comparison(t, b))
return false;
return !eval_gt(t.backend(), result_type::canonical_value(b));
}
template <class Tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR bool operator<=(const detail::expression<Tag, A1, A2, A3, A4>& a, const number<Backend, ExpressionTemplates>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_gt;
result_type t(a);
return t <= b;
}
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator<=(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b)
{
using default_ops::eval_gt;
typename detail::expression<Tag, A1, A2, A3, A4>::result_type t(a);
typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type t2(b);
if (detail::is_unordered_comparison(t, t2))
return false;
return !eval_gt(t.backend(), t2.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value != number_kind_complex) && (number_category<Backend2>::value != number_kind_complex), bool>::type
operator>=(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b)
{
using default_ops::eval_lt;
if (detail::is_unordered_comparison(a, b))
return false;
return !eval_lt(a.backend(), b.backend());
}
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && (number_category<Backend>::value != number_kind_complex) && !is_number_expression<Arithmetic>::value, bool>::type
operator>=(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b)
{
using default_ops::eval_lt;
if (detail::is_unordered_comparison(a, b))
return false;
return !eval_lt(a.backend(), number<Backend, ExpressionTemplates>::canonical_value(b));
}
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value && (number_category<Backend>::value != number_kind_complex) && !is_number_expression<Arithmetic>::value, bool>::type
operator>=(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b)
{
using default_ops::eval_gt;
if (detail::is_unordered_comparison(a, b))
return false;
return !eval_gt(b.backend(), number<Backend, ExpressionTemplates>::canonical_value(a));
}
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator>=(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_gt;
result_type t(b);
if (detail::is_unordered_comparison(a, t))
return false;
return !eval_gt(t.backend(), result_type::canonical_value(a));
}
template <class Backend, expression_template_option ExpressionTemplates, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR bool operator>=(const number<Backend, ExpressionTemplates>& a, const detail::expression<Tag, A1, A2, A3, A4>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_gt;
result_type t(b);
return a >= t;
}
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator>=(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_lt;
result_type t(a);
if (detail::is_unordered_comparison(t, b))
return false;
return !eval_lt(t.backend(), result_type::canonical_value(b));
}
template <class Tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR bool operator>=(const detail::expression<Tag, A1, A2, A3, A4>& a, const number<Backend, ExpressionTemplates>& b)
{
using result_type = typename detail::expression<Tag, A1, A2, A3, A4>::result_type;
using default_ops::eval_lt;
result_type t(a);
return t >= b;
}
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value && (number_category<typename detail::expression<Tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex), bool>::type
operator>=(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b)
{
using default_ops::eval_lt;
typename detail::expression<Tag, A1, A2, A3, A4>::result_type t(a);
typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type t2(b);
if (detail::is_unordered_comparison(t, t2))
return false;
return !eval_lt(t.backend(), t2.backend());
}
//
// C99 comparison macros as functions:
//
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR bool isgreater BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b) { return a > b; }
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
isgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b) { return a > b; }
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
isgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b) { return a > b; }
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
isgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b) { return a > b; }
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
isgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b) { return a > b; }
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value, bool>::type
isgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b) { return a > b; }
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR bool isgreaterequal BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b) { return a >= b; }
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
isgreaterequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b) { return a >= b; }
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
isgreaterequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b) { return a >= b; }
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
isgreaterequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b) { return a >= b; }
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
isgreaterequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b) { return a >= b; }
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value, bool>::type
isgreaterequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b) { return a >= b; }
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR bool islessequal BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b) { return a <= b; }
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
islessequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b) { return a <= b; }
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
islessequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b) { return a <= b; }
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
islessequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b) { return a <= b; }
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
islessequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b) { return a <= b; }
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value, bool>::type
islessequal
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b) { return a <= b; }
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR bool isless BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b) { return a < b; }
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
isless
BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b) { return a < b; }
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
isless
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b) { return a < b; }
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
isless
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& b) { return a < b; }
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
isless
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& a, const Arithmetic& b) { return a < b; }
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value, bool>::type
isless
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& a, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& b) { return a < b; }
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR bool islessgreater BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b)
{
if (detail::is_unordered_comparison(a, b))
return false;
return a != b;
}
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
islessgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b)
{
if (detail::is_unordered_comparison(a, b))
return false;
return a != b;
}
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
islessgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b)
{
if (detail::is_unordered_comparison(a, b))
return false;
return a != b;
}
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
islessgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& bb)
{
typename detail::expression<Tag, A1, A2, A3, A4>::result_type b(bb);
return islessgreater BOOST_PREVENT_MACRO_SUBSTITUTION(a, b);
}
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
islessgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& aa, const Arithmetic& b)
{
typename detail::expression<Tag, A1, A2, A3, A4>::result_type a(aa);
return islessgreater BOOST_PREVENT_MACRO_SUBSTITUTION(a, b);
}
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value, bool>::type
islessgreater
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& aa, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& bb)
{
typename detail::expression<Tag, A1, A2, A3, A4>::result_type a(aa);
typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type b(bb);
return islessgreater BOOST_PREVENT_MACRO_SUBSTITUTION(a, b);
}
template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
inline BOOST_MP_CXX14_CONSTEXPR bool isunordered BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const number<Backend2, ExpressionTemplates2>& b) { return detail::is_unordered_comparison(a, b); }
template <class Backend, expression_template_option ExpressionTemplates, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
isunordered
BOOST_PREVENT_MACRO_SUBSTITUTION(const number<Backend, ExpressionTemplates>& a, const Arithmetic& b) { return detail::is_unordered_comparison(a, b); }
template <class Arithmetic, class Backend, expression_template_option ExpressionTemplates>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<number<Backend, ExpressionTemplates>, Arithmetic>::value, bool>::type
isunordered
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const number<Backend, ExpressionTemplates>& b) { return detail::is_unordered_comparison(a, b); }
template <class Arithmetic, class Tag, class A1, class A2, class A3, class A4>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
isunordered
BOOST_PREVENT_MACRO_SUBSTITUTION(const Arithmetic& a, const detail::expression<Tag, A1, A2, A3, A4>& bb)
{
typename detail::expression<Tag, A1, A2, A3, A4>::result_type b(bb);
return detail::is_unordered_comparison(a, b);
}
template <class Tag, class A1, class A2, class A3, class A4, class Arithmetic>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<detail::is_valid_mixed_compare<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, Arithmetic>::value, bool>::type
isunordered
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& aa, const Arithmetic& b)
{
typename detail::expression<Tag, A1, A2, A3, A4>::result_type a(aa);
return detail::is_unordered_comparison(a, b);
}
template <class Tag, class A1, class A2, class A3, class A4, class Tagb, class A1b, class A2b, class A3b, class A4b>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<is_equivalent_number_type<typename detail::expression<Tag, A1, A2, A3, A4>::result_type, typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type>::value, bool>::type
isunordered
BOOST_PREVENT_MACRO_SUBSTITUTION(const detail::expression<Tag, A1, A2, A3, A4>& aa, const detail::expression<Tagb, A1b, A2b, A3b, A4b>& bb)
{
typename detail::expression<Tag, A1, A2, A3, A4>::result_type a(aa);
typename detail::expression<Tagb, A1b, A2b, A3b, A4b>::result_type b(bb);
return detail::is_unordered_comparison(a, b);
}
}} // namespace boost::multiprecision
#endif // BOOST_MP_NUMBER_COMPARE_HPP

View file

@ -0,0 +1,313 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2018 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_DETAIL_PRECISION_HPP
#define BOOST_MP_DETAIL_PRECISION_HPP
#include <boost/multiprecision/traits/is_variable_precision.hpp>
#include <boost/multiprecision/detail/number_base.hpp>
#include <boost/multiprecision/detail/digits.hpp>
#include <boost/multiprecision/detail/assert.hpp>
namespace boost { namespace multiprecision { namespace detail {
template <class B, boost::multiprecision::expression_template_option ET>
inline constexpr unsigned current_precision_of_last_chance_imp(const boost::multiprecision::number<B, ET>&, const std::integral_constant<int, 0>&)
{
return std::numeric_limits<boost::multiprecision::number<B, ET> >::digits10;
}
template <class B, boost::multiprecision::expression_template_option ET>
inline BOOST_MP_CXX14_CONSTEXPR unsigned current_precision_of_last_chance_imp(const boost::multiprecision::number<B, ET>& val, const std::integral_constant<int, 1>&)
{
//
// We have an arbitrary precision integer, take it's "precision" as the
// location of the most-significant-bit less the location of the
// least-significant-bit, ie the number of bits required to represent the
// the value assuming we will have an exponent to shift things by:
//
return static_cast<unsigned>(val.is_zero() ? 1 : 1 + digits2_2_10(msb(abs(val)) - lsb(abs(val)) + 1));
}
template <class B, boost::multiprecision::expression_template_option ET>
inline BOOST_MP_CXX14_CONSTEXPR unsigned current_precision_of_last_chance_imp(const boost::multiprecision::number<B, ET>& val, const std::integral_constant<int, 2>&)
{
//
// We have an arbitrary precision rational, take it's "precision" as the
// the larger of the "precision" of numerator and denominator:
//
return (std::max)(current_precision_of_last_chance_imp(numerator(val), std::integral_constant<int, 1>()), current_precision_of_last_chance_imp(denominator(val), std::integral_constant<int, 1>()));
}
template <class B, boost::multiprecision::expression_template_option ET>
inline BOOST_MP_CXX14_CONSTEXPR unsigned current_precision_of_imp(const boost::multiprecision::number<B, ET>& n, const std::integral_constant<bool, true>&)
{
return n.precision();
}
template <class B, boost::multiprecision::expression_template_option ET>
inline constexpr unsigned current_precision_of_imp(const boost::multiprecision::number<B, ET>& val, const std::integral_constant<bool, false>&)
{
using tag = std::integral_constant<int,
std::numeric_limits<boost::multiprecision::number<B, ET> >::is_specialized &&
std::numeric_limits<boost::multiprecision::number<B, ET> >::is_integer &&
std::numeric_limits<boost::multiprecision::number<B, ET> >::is_exact &&
!std::numeric_limits<boost::multiprecision::number<B, ET> >::is_modulo
? 1
: boost::multiprecision::number_category<boost::multiprecision::number<B, ET> >::value == boost::multiprecision::number_kind_rational ? 2
: 0>;
return current_precision_of_last_chance_imp(val, tag());
}
template <class R, class Terminal>
inline constexpr unsigned current_precision_of_terminal(const Terminal&)
{
return (R::thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
? (std::numeric_limits<Terminal>::min_exponent ? std::numeric_limits<Terminal>::digits10 : 1 + std::numeric_limits<Terminal>::digits10) : 0;
}
template <class R, class Terminal>
inline constexpr unsigned current_precision_of(const Terminal& r)
{
return current_precision_of_terminal<R>(R::canonical_value(r));
}
template <class R>
inline constexpr unsigned current_precision_of(const float&)
{
using list = typename R::backend_type::float_types;
using first_float = typename std::tuple_element<0, list>::type;
return (R::thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision) ? std::numeric_limits<first_float>::digits10 : 0;
}
template <class R, class Terminal, std::size_t N>
inline constexpr unsigned current_precision_of(const Terminal (&)[N])
{ // For string literals:
return 0;
}
template <class R, class B, boost::multiprecision::expression_template_option ET>
inline constexpr unsigned current_precision_of_imp(const boost::multiprecision::number<B, ET>& n, const std::true_type&)
{
return std::is_same<R, boost::multiprecision::number<B, ET> >::value
|| (std::is_same<typename R::value_type, boost::multiprecision::number<B, ET> >::value && (R::thread_default_variable_precision_options() >= variable_precision_options::preserve_component_precision))
|| (R::thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
? current_precision_of_imp(n, boost::multiprecision::detail::is_variable_precision<boost::multiprecision::number<B, ET> >()) : 0;
}
template <class R, class B, boost::multiprecision::expression_template_option ET>
inline constexpr unsigned current_precision_of_imp(const boost::multiprecision::number<B, ET>& n, const std::false_type&)
{
return std::is_same<R, boost::multiprecision::number<B, ET> >::value
|| std::is_same<typename R::value_type, boost::multiprecision::number<B, ET> >::value
? current_precision_of_imp(n, boost::multiprecision::detail::is_variable_precision<boost::multiprecision::number<B, ET> >()) : 0;
}
template <class R, class B, boost::multiprecision::expression_template_option ET>
inline constexpr unsigned current_precision_of(const boost::multiprecision::number<B, ET>& n)
{
return current_precision_of_imp<R>(n, boost::multiprecision::detail::is_variable_precision<R>());
}
template <class R, class tag, class Arg1>
inline constexpr unsigned current_precision_of(const expression<tag, Arg1, void, void, void>& expr)
{
return current_precision_of<R>(expr.left_ref());
}
template <class R, class Arg1>
inline constexpr unsigned current_precision_of(const expression<terminal, Arg1, void, void, void>& expr)
{
return current_precision_of<R>(expr.value());
}
template <class R, class tag, class Arg1, class Arg2>
inline constexpr unsigned current_precision_of(const expression<tag, Arg1, Arg2, void, void>& expr)
{
return (std::max)(current_precision_of<R>(expr.left_ref()), current_precision_of<R>(expr.right_ref()));
}
template <class R, class tag, class Arg1, class Arg2, class Arg3>
inline constexpr unsigned current_precision_of(const expression<tag, Arg1, Arg2, Arg3, void>& expr)
{
return (std::max)((std::max)(current_precision_of<R>(expr.left_ref()), current_precision_of<R>(expr.right_ref())), current_precision_of<R>(expr.middle_ref()));
}
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4130)
#endif
template <class R, bool = boost::multiprecision::detail::is_variable_precision<R>::value>
struct scoped_default_precision
{
template <class T>
constexpr scoped_default_precision(const T&) {}
template <class T, class U>
constexpr scoped_default_precision(const T&, const U&) {}
template <class T, class U, class V>
constexpr scoped_default_precision(const T&, const U&, const V&) {}
//
// This function is never called: in C++17 it won't be compiled either:
//
unsigned precision() const
{
BOOST_MP_ASSERT("This function should never be called!!" == nullptr);
return 0;
}
};
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
template <class R>
struct scoped_default_precision<R, true>
{
template <class T>
BOOST_MP_CXX14_CONSTEXPR scoped_default_precision(const T& a)
{
init(has_uniform_precision() ? R::thread_default_precision() : (std::max)(R::thread_default_precision(), current_precision_of<R>(a)));
}
template <class T, class U>
BOOST_MP_CXX14_CONSTEXPR scoped_default_precision(const T& a, const U& b)
{
init(has_uniform_precision() ? R::thread_default_precision() : (std::max)(R::thread_default_precision(), (std::max)(current_precision_of<R>(a), current_precision_of<R>(b))));
}
template <class T, class U, class V>
BOOST_MP_CXX14_CONSTEXPR scoped_default_precision(const T& a, const U& b, const V& c)
{
init(has_uniform_precision() ? R::thread_default_precision() : (std::max)((std::max)(current_precision_of<R>(a), current_precision_of<R>(b)), (std::max)(R::thread_default_precision(), current_precision_of<R>(c))));
}
~scoped_default_precision()
{
if(m_new_prec != m_old_prec)
R::thread_default_precision(m_old_prec);
}
BOOST_MP_CXX14_CONSTEXPR unsigned precision() const
{
return m_new_prec;
}
static constexpr bool has_uniform_precision()
{
return R::thread_default_variable_precision_options() <= boost::multiprecision::variable_precision_options::assume_uniform_precision;
}
private:
BOOST_MP_CXX14_CONSTEXPR void init(unsigned p)
{
m_old_prec = R::thread_default_precision();
if (p && (p != m_old_prec))
{
R::thread_default_precision(p);
m_new_prec = p;
}
else
m_new_prec = m_old_prec;
}
unsigned m_old_prec, m_new_prec;
};
template <class T>
inline BOOST_MP_CXX14_CONSTEXPR void maybe_promote_precision(T*, const std::integral_constant<bool, false>&) {}
template <class T>
inline BOOST_MP_CXX14_CONSTEXPR void maybe_promote_precision(T* obj, const std::integral_constant<bool, true>&)
{
if (obj->precision() != T::thread_default_precision())
{
obj->precision(T::thread_default_precision());
}
}
template <class T>
inline BOOST_MP_CXX14_CONSTEXPR void maybe_promote_precision(T* obj)
{
maybe_promote_precision(obj, std::integral_constant<bool, boost::multiprecision::detail::is_variable_precision<T>::value>());
}
#ifndef BOOST_NO_CXX17_IF_CONSTEXPR
#define BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(T) \
if \
constexpr(boost::multiprecision::detail::is_variable_precision<T>::value)
#else
#define BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(T) if (boost::multiprecision::detail::is_variable_precision<T>::value)
#endif
template <class T, bool = boost::multiprecision::detail::is_variable_precision<T>::value>
struct scoped_target_precision
{
variable_precision_options opts;
scoped_target_precision() : opts(T::thread_default_variable_precision_options())
{
T::thread_default_variable_precision_options(variable_precision_options::preserve_target_precision);
}
~scoped_target_precision()
{
T::thread_default_variable_precision_options(opts);
}
};
template <class T>
struct scoped_target_precision<T, false> {};
template <class T, bool = boost::multiprecision::detail::is_variable_precision<T>::value>
struct scoped_source_precision
{
variable_precision_options opts;
scoped_source_precision() : opts(T::thread_default_variable_precision_options())
{
T::thread_default_variable_precision_options(variable_precision_options::preserve_source_precision);
}
~scoped_source_precision()
{
T::thread_default_variable_precision_options(opts);
}
};
template <class T>
struct scoped_source_precision<T, false> {};
template <class T, bool = boost::multiprecision::detail::is_variable_precision<T>::value>
struct scoped_precision_options
{
unsigned saved_digits;
boost::multiprecision::variable_precision_options saved_options;
scoped_precision_options(unsigned digits)
: saved_digits(T::thread_default_precision()), saved_options(T::thread_default_variable_precision_options())
{
T::thread_default_precision(digits);
}
scoped_precision_options(unsigned digits, variable_precision_options opts)
: saved_digits(T::thread_default_precision()), saved_options(T::thread_default_variable_precision_options())
{
T::thread_default_precision(digits);
T::thread_default_variable_precision_options(opts);
}
template <class U>
scoped_precision_options(const U& u)
: saved_digits(T::thread_default_precision()), saved_options(T::thread_default_variable_precision_options())
{
T::thread_default_precision(u.precision());
T::thread_default_variable_precision_options(U::thread_default_variable_precision_options());
}
~scoped_precision_options()
{
T::thread_default_variable_precision_options(saved_options);
T::thread_default_precision(saved_digits);
}
};
template <class T>
struct scoped_precision_options<T, false>
{
scoped_precision_options(unsigned) {}
scoped_precision_options(unsigned, variable_precision_options) {}
template <class U>
scoped_precision_options(const U&) {}
~scoped_precision_options() {}
};
}
}
} // namespace boost::multiprecision::detail
#endif // BOOST_MP_DETAIL_PRECISION_HPP

View file

@ -0,0 +1,19 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock.
// Copyright Christopher Kormanyos 2013. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_MP_DETAIL_REBIND_HPP
#define BOOST_MP_DETAIL_REBIND_HPP
namespace boost { namespace multiprecision { namespace backends { namespace detail {
template <class value_type, class my_allocator>
struct rebind
{
using type = typename std::allocator_traits<my_allocator>::template rebind_alloc<value_type>;
};
}}}} // namespace boost::multiprecision::backends::detail
#endif // BOOST_MP_DETAIL_REBIND_HPP

View file

@ -0,0 +1,161 @@
///////////////////////////////////////////////////////////////
// Copyright 2010 - 2021 Douglas Gregor
// Copyright 2021 Matt Borland.
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// Used to support configuration options depending on standalone context
// by providing either required support or disabling functionality
#ifndef BOOST_MP_STANDALONE_CONFIG_HPP
#define BOOST_MP_STANDALONE_CONFIG_HPP
#include <climits>
// Boost.Config is dependency free so it is considered a requirement to use Boost.Multiprecision in standalone mode
#ifdef __has_include
# if __has_include(<boost/config.hpp>)
# include <boost/config.hpp>
# include <boost/config/workaround.hpp>
# else
# error "Boost.Config is considered a requirement to use Boost.Multiprecision in standalone mode. A package is provided at https://github.com/boostorg/multiprecision/releases"
# endif
#else
// Provides the less helpful fatal error: 'boost/config.hpp' file not found if not available
# include <boost/config.hpp>
# include <boost/config/workaround.hpp>
#endif
// Minimum language standard transition
#ifdef _MSVC_LANG
# if _MSVC_LANG < 201402L
# pragma warning("The minimum language standard to use Boost.Math will be C++14 starting in July 2023 (Boost 1.82 release)");
# endif
#else
# if __cplusplus < 201402L
# warning "The minimum language standard to use Boost.Math will be C++14 starting in July 2023 (Boost 1.82 release)"
# endif
#endif
// If any of the most frequently used boost headers are missing assume that standalone mode is supposed to be used
#ifdef __has_include
#if !__has_include(<boost/assert.hpp>) || !__has_include(<boost/lexical_cast.hpp>) || \
!__has_include(<boost/throw_exception.hpp>) || !__has_include(<boost/predef/other/endian.h>)
# ifndef BOOST_MP_STANDALONE
# define BOOST_MP_STANDALONE
# endif
#endif
#endif
#ifndef BOOST_MP_STANDALONE
#include <boost/integer.hpp>
#include <boost/integer_traits.hpp>
// Required typedefs for interoperability with standalone mode
#if defined(BOOST_HAS_INT128) && defined(__cplusplus)
namespace boost { namespace multiprecision {
using int128_type = boost::int128_type;
using uint128_type = boost::uint128_type;
}}
#endif
#if defined(BOOST_HAS_FLOAT128) && defined(__cplusplus)
namespace boost { namespace multiprecision {
using float128_type = boost::float128_type;
}}
#endif
// Boost.Math available by default
#define BOOST_MP_MATH_AVAILABLE
#else // Standalone mode
#ifdef BOOST_MATH_STANDALONE
# define BOOST_MP_MATH_AVAILABLE
#endif
#ifndef BOOST_MP_MATH_AVAILABLE
# define BOOST_MATH_INSTRUMENT_CODE(x)
#endif
// Prevent Macro sub
#ifndef BOOST_PREVENT_MACRO_SUBSTITUTION
# define BOOST_PREVENT_MACRO_SUBSTITUTION
#endif
#if defined(BOOST_HAS_INT128) && defined(__cplusplus)
namespace boost { namespace multiprecision {
# ifdef __GNUC__
__extension__ typedef __int128 int128_type;
__extension__ typedef unsigned __int128 uint128_type;
# else
typedef __int128 int128_type;
typedef unsigned __int128 uint128_type;
# endif
}}
#endif
// same again for __float128:
#if defined(BOOST_HAS_FLOAT128) && defined(__cplusplus)
namespace boost { namespace multiprecision {
# ifdef __GNUC__
__extension__ typedef __float128 float128_type;
# else
typedef __float128 float128_type;
# endif
}}
#endif
#endif // BOOST_MP_STANDALONE
// Workarounds for numeric limits on old compilers
#ifdef BOOST_HAS_INT128
# ifndef INT128_MAX
# define INT128_MAX static_cast<boost::multiprecision::int128_type>((static_cast<boost::multiprecision::uint128_type>(1) << ((__SIZEOF_INT128__ * __CHAR_BIT__) - 1)) - 1)
# endif
# ifndef INT128_MIN
# define INT128_MIN (-INT128_MAX - 1)
# endif
# ifndef UINT128_MAX
# define UINT128_MAX ((2 * static_cast<boost::multiprecision::uint128_type>(INT128_MAX)) + 1)
# endif
#endif
#define BOOST_MP_CXX14_CONSTEXPR BOOST_CXX14_CONSTEXPR
//
// Early compiler versions trip over the constexpr code:
//
#if defined(__clang__) && (__clang_major__ < 5)
#undef BOOST_MP_CXX14_CONSTEXPR
#define BOOST_MP_CXX14_CONSTEXPR
#endif
#if defined(__apple_build_version__) && (__clang_major__ < 9)
#undef BOOST_MP_CXX14_CONSTEXPR
#define BOOST_MP_CXX14_CONSTEXPR
#endif
#if defined(BOOST_GCC) && (__GNUC__ < 6)
#undef BOOST_MP_CXX14_CONSTEXPR
#define BOOST_MP_CXX14_CONSTEXPR
#endif
#if defined(BOOST_INTEL)
#undef BOOST_MP_CXX14_CONSTEXPR
#define BOOST_MP_CXX14_CONSTEXPR
#define BOOST_MP_NO_CONSTEXPR_DETECTION
#endif
#ifdef __has_attribute
# if __has_attribute(fallthrough)
# define BOOST_MP_FALLTHROUGH [[fallthrough]]
# endif
#endif
#ifndef BOOST_MP_FALLTHROUGH
# if __GNUC__ >= 7
# define BOOST_MP_FALLTHROUGH __attribute__((fallthrough))
# else
# define BOOST_MP_FALLTHROUGH ((void)0)
# endif
#endif
#endif // BOOST_MP_STANDALONE_CONFIG_HPP

View file

@ -0,0 +1,42 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2021 John Maddock.
// Copyright Christopher Kormanyos 2021. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_MP_DETAIL_STATIC_ARRAY_HPP
#define BOOST_MP_DETAIL_STATIC_ARRAY_HPP
#include <array>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
namespace boost { namespace multiprecision { namespace backends { namespace detail {
template <class ValueType, const std::uint32_t ElemNumber>
struct static_array : public std::array<ValueType, std::size_t(ElemNumber)>
{
private:
using base_class_type = std::array<ValueType, std::size_t(ElemNumber)>;
public:
static_array() noexcept
{
base_class_type::fill(typename base_class_type::value_type(0u));
}
static_array(std::initializer_list<std::uint32_t> lst) noexcept
{
std::copy(lst.begin(),
lst.begin() + (std::min)(std::size_t(lst.size()), std::size_t(ElemNumber)),
base_class_type::begin());
std::fill(base_class_type::begin() + (std::min)(std::size_t(lst.size()), std::size_t(ElemNumber)),
base_class_type::end(),
typename base_class_type::value_type(0u));
}
};
}}}} // namespace boost::multiprecision::backends::detail
#endif // BOOST_MP_DETAIL_STATIC_ARRAY_HPP

View file

@ -0,0 +1,48 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2023 John Maddock.
// Copyright Christopher Kormanyos 2013. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_MP_DETAIL_STRING_HELPERS_HPP
#define BOOST_MP_DETAIL_STRING_HELPERS_HPP
#include <algorithm>
#include <cstring>
namespace boost { namespace multiprecision { namespace detail {
struct is_in_string
{
const char* begin;
const char* end;
is_in_string(const char* p) : begin(p), end(p + std::strlen(p)) {}
bool operator()(char s) { return std::find(begin, end, s) != end; }
};
struct is_not_in_string
{
const char* begin;
const char* end;
is_not_in_string(const char* p) : begin(p), end(p + std::strlen(p)) {}
bool operator()(char s) { return std::find(begin, end, s) == end; }
};
template <class Iterator>
std::size_t find_first_of(Iterator begin, Iterator end, const char* what)
{
return static_cast<std::size_t>(std::find_if(begin, end, is_in_string(what)) - begin);
}
template <class Iterator>
std::size_t find_first_not_of(Iterator begin, Iterator end, const char* what)
{
return static_cast<std::size_t>(std::find_if(begin, end, is_not_in_string(what)) - begin);
}
}}} // namespace boost::multiprecision::detail
#endif // BOOST_MP_DETAIL_STRING_HELPERS_HPP

View file

@ -0,0 +1,80 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2021 John Maddock.
// Copyright Christopher Kormanyos 2021. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_MP_DETAIL_TABLES_HPP
#define BOOST_MP_DETAIL_TABLES_HPP
#include <algorithm>
#include <array>
#include <cstdint>
namespace boost { namespace multiprecision { namespace backends { namespace detail {
struct a029750
{
static constexpr std::uint32_t a029750_as_constexpr(const std::uint32_t value)
{
// Sloane's A029750 List of numbers of the form 2^k times 1, 3, 5 or 7.
// CoefficientList[Series[-(x + 1)^2 (x^2 + 1)^2/(2 x^4 - 1), {x, 0, 78}], x]
return ((value <= UINT32_C( 32)) ? UINT32_C( 32) : ((value <= UINT32_C( 40)) ? UINT32_C( 40) : ((value <= UINT32_C( 48)) ? UINT32_C( 48) : ((value <= UINT32_C( 56)) ? UINT32_C( 56) :
((value <= UINT32_C( 64)) ? UINT32_C( 64) : ((value <= UINT32_C( 80)) ? UINT32_C( 80) : ((value <= UINT32_C( 96)) ? UINT32_C( 96) : ((value <= UINT32_C( 112)) ? UINT32_C( 112) :
((value <= UINT32_C( 128)) ? UINT32_C( 128) : ((value <= UINT32_C( 160)) ? UINT32_C( 160) : ((value <= UINT32_C( 192)) ? UINT32_C( 192) : ((value <= UINT32_C( 224)) ? UINT32_C( 224) :
((value <= UINT32_C( 256)) ? UINT32_C( 256) : ((value <= UINT32_C( 320)) ? UINT32_C( 320) : ((value <= UINT32_C( 384)) ? UINT32_C( 384) : ((value <= UINT32_C( 448)) ? UINT32_C( 448) :
((value <= UINT32_C( 512)) ? UINT32_C( 512) : ((value <= UINT32_C( 640)) ? UINT32_C( 640) : ((value <= UINT32_C( 768)) ? UINT32_C( 768) : ((value <= UINT32_C( 896)) ? UINT32_C( 896) :
((value <= UINT32_C( 1024)) ? UINT32_C( 1024) : ((value <= UINT32_C( 1280)) ? UINT32_C( 1280) : ((value <= UINT32_C( 1536)) ? UINT32_C( 1536) : ((value <= UINT32_C( 1792)) ? UINT32_C( 1792) :
((value <= UINT32_C( 2048)) ? UINT32_C( 2048) : ((value <= UINT32_C( 2560)) ? UINT32_C( 2560) : ((value <= UINT32_C( 3072)) ? UINT32_C( 3072) : ((value <= UINT32_C( 3584)) ? UINT32_C( 3584) :
((value <= UINT32_C( 4096)) ? UINT32_C( 4096) : ((value <= UINT32_C( 5120)) ? UINT32_C( 5120) : ((value <= UINT32_C( 6144)) ? UINT32_C( 6144) : ((value <= UINT32_C( 7168)) ? UINT32_C( 7168) :
((value <= UINT32_C( 8192)) ? UINT32_C( 8192) : ((value <= UINT32_C( 10240)) ? UINT32_C( 10240) : ((value <= UINT32_C( 12288)) ? UINT32_C( 12288) : ((value <= UINT32_C( 14336)) ? UINT32_C( 14336) :
((value <= UINT32_C( 16384)) ? UINT32_C( 16384) : ((value <= UINT32_C( 20480)) ? UINT32_C( 20480) : ((value <= UINT32_C( 24576)) ? UINT32_C( 24576) : ((value <= UINT32_C( 28672)) ? UINT32_C( 28672) :
((value <= UINT32_C( 32768)) ? UINT32_C( 32768) : ((value <= UINT32_C( 40960)) ? UINT32_C( 40960) : ((value <= UINT32_C( 49152)) ? UINT32_C( 49152) : ((value <= UINT32_C( 57344)) ? UINT32_C( 57344) :
((value <= UINT32_C( 65536)) ? UINT32_C( 65536) : ((value <= UINT32_C( 81920)) ? UINT32_C( 81920) : ((value <= UINT32_C( 98304)) ? UINT32_C( 98304) : ((value <= UINT32_C( 114688)) ? UINT32_C( 114688) :
((value <= UINT32_C( 131072)) ? UINT32_C( 131072) : ((value <= UINT32_C( 163840)) ? UINT32_C( 163840) : ((value <= UINT32_C( 196608)) ? UINT32_C( 196608) : ((value <= UINT32_C( 229376)) ? UINT32_C( 229376) :
((value <= UINT32_C( 262144)) ? UINT32_C( 262144) : ((value <= UINT32_C( 327680)) ? UINT32_C( 327680) : ((value <= UINT32_C( 393216)) ? UINT32_C( 393216) : ((value <= UINT32_C( 458752)) ? UINT32_C( 458752) :
((value <= UINT32_C( 524288)) ? UINT32_C( 524288) : ((value <= UINT32_C( 655360)) ? UINT32_C( 655360) : ((value <= UINT32_C( 786432)) ? UINT32_C( 786432) : ((value <= UINT32_C( 917504)) ? UINT32_C( 917504) :
((value <= UINT32_C(1048576)) ? UINT32_C(1048576) : ((value <= UINT32_C(1310720)) ? UINT32_C(1310720) : ((value <= UINT32_C(1572864)) ? UINT32_C(1572864) : ((value <= UINT32_C(1835008)) ? UINT32_C(1835008) : UINT32_C(0x7FFFFFFF)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))));
}
static std::uint32_t a029750_as_runtime_value(const std::uint32_t value)
{
// Sloane's A029750 List of numbers of the form 2^k times 1, 3, 5 or 7.
// CoefficientList[Series[-(x + 1)^2 (x^2 + 1)^2/(2 x^4 - 1), {x, 0, 78}], x]
constexpr std::array<std::uint32_t, 65U> a029750_data =
{{
UINT32_C( 32), UINT32_C( 40), UINT32_C( 48), UINT32_C( 56),
UINT32_C( 64), UINT32_C( 80), UINT32_C( 96), UINT32_C( 112),
UINT32_C( 128), UINT32_C( 160), UINT32_C( 192), UINT32_C( 224),
UINT32_C( 256), UINT32_C( 320), UINT32_C( 384), UINT32_C( 448),
UINT32_C( 512), UINT32_C( 640), UINT32_C( 768), UINT32_C( 896),
UINT32_C( 1024), UINT32_C( 1280), UINT32_C( 1536), UINT32_C( 1792),
UINT32_C( 2048), UINT32_C( 2560), UINT32_C( 3072), UINT32_C( 3584),
UINT32_C( 4096), UINT32_C( 5120), UINT32_C( 6144), UINT32_C( 7168),
UINT32_C( 8192), UINT32_C( 10240), UINT32_C( 12288), UINT32_C( 14336),
UINT32_C( 16384), UINT32_C( 20480), UINT32_C( 24576), UINT32_C( 28672),
UINT32_C( 32768), UINT32_C( 40960), UINT32_C( 49152), UINT32_C( 57344),
UINT32_C( 65536), UINT32_C( 81920), UINT32_C( 98304), UINT32_C( 114688),
UINT32_C( 131072), UINT32_C( 163840), UINT32_C( 196608), UINT32_C( 229376),
UINT32_C( 262144), UINT32_C( 327680), UINT32_C( 393216), UINT32_C( 458752),
UINT32_C( 524288), UINT32_C( 655360), UINT32_C( 786432), UINT32_C( 917504),
UINT32_C( 1048576), UINT32_C(1310720), UINT32_C(1572864), UINT32_C(1835008),
UINT32_C(0x7FFFFFFF)
}};
const std::array<std::uint32_t, 65U>::const_iterator it =
std::lower_bound(a029750_data.cbegin(), a029750_data.cend(), value);
return ((it != a029750_data.cend()) ? *it : UINT32_C(0xFFFFFFFF));
}
};
constexpr std::uint32_t pow10_maker(std::uint32_t n)
{
// Make the constant power of 10^n.
return ((n == UINT32_C(0)) ? UINT32_C(1) : pow10_maker(n - UINT32_C(1)) * UINT32_C(10));
}
}}}} // namespace boost::multiprecision::backends::detail
#endif // BOOST_MP_DETAIL_TABLES_HPP

View file

@ -0,0 +1,75 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_UBLAS_INTEROP_HPP
#define BOOST_MP_UBLAS_INTEROP_HPP
namespace boost { namespace numeric { namespace ublas {
template <class V>
class sparse_vector_element;
template <class V, class Backend, multiprecision::expression_template_option ExpressionTemplates>
inline bool operator==(const sparse_vector_element<V>& a, const ::boost::multiprecision::number<Backend, ExpressionTemplates>& b)
{
using ref_type = typename sparse_vector_element<V>::const_reference;
return static_cast<ref_type>(a) == b;
}
template <class X, class Y>
struct promote_traits;
template <class Backend1, boost::multiprecision::expression_template_option ExpressionTemplates1, class Backend2, boost::multiprecision::expression_template_option ExpressionTemplates2>
struct promote_traits<boost::multiprecision::number<Backend1, ExpressionTemplates1>, boost::multiprecision::number<Backend2, ExpressionTemplates2> >
{
using number1_t = boost::multiprecision::number<Backend1, ExpressionTemplates1>;
using number2_t = boost::multiprecision::number<Backend2, ExpressionTemplates2>;
using promote_type = typename std::conditional<
std::is_convertible<number1_t, number2_t>::value && !std::is_convertible<number2_t, number1_t>::value,
number2_t, number1_t>::type;
};
template <class Backend1, boost::multiprecision::expression_template_option ExpressionTemplates1, class Arithmetic>
struct promote_traits<boost::multiprecision::number<Backend1, ExpressionTemplates1>, Arithmetic>
{
using promote_type = boost::multiprecision::number<Backend1, ExpressionTemplates1>;
};
template <class Arithmetic, class Backend1, boost::multiprecision::expression_template_option ExpressionTemplates1>
struct promote_traits<Arithmetic, boost::multiprecision::number<Backend1, ExpressionTemplates1> >
{
using promote_type = boost::multiprecision::number<Backend1, ExpressionTemplates1>;
};
template <class Backend1, boost::multiprecision::expression_template_option ExpressionTemplates1, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
struct promote_traits<boost::multiprecision::number<Backend1, ExpressionTemplates1>, boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4> >
{
using number1_t = boost::multiprecision::number<Backend1, ExpressionTemplates1> ;
using expression_type = boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>;
using number2_t = typename expression_type::result_type ;
using promote_type = typename promote_traits<number1_t, number2_t>::promote_type ;
};
template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class Backend1, boost::multiprecision::expression_template_option ExpressionTemplates1>
struct promote_traits<boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, boost::multiprecision::number<Backend1, ExpressionTemplates1> >
{
using number1_t = boost::multiprecision::number<Backend1, ExpressionTemplates1> ;
using expression_type = boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>;
using number2_t = typename expression_type::result_type ;
using promote_type = typename promote_traits<number1_t, number2_t>::promote_type ;
};
template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class tagb, class Arg1b, class Arg2b, class Arg3b, class Arg4b>
struct promote_traits<boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, boost::multiprecision::detail::expression<tagb, Arg1b, Arg2b, Arg3b, Arg4b> >
{
using expression1_t = boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4> ;
using number1_t = typename expression1_t::result_type ;
using expression2_t = boost::multiprecision::detail::expression<tagb, Arg1b, Arg2b, Arg3b, Arg4b>;
using number2_t = typename expression2_t::result_type ;
};
}}} // namespace boost::numeric::ublas
#endif

View file

@ -0,0 +1,212 @@
///////////////////////////////////////////////////////////////
// Copyright Jens Maurer 2000-2021
// Copyright Steven Watanabe 2011-2021
// Copyright John Maddock 2015-2021
// Copyright Matt Borland 2021
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt
//
// This is a C++11 compliant port of Boost.Random's implementation
// of uniform_int_distribution. See their comments for detailed
// descriptions
#ifndef BOOST_MP_UNIFORM_INT_DISTRIBUTION_HPP
#define BOOST_MP_UNIFORM_INT_DISTRIBUTION_HPP
#include <limits>
#include <type_traits>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/assert.hpp>
#include <boost/multiprecision/traits/std_integer_traits.hpp>
namespace boost { namespace multiprecision {
namespace detail {
template <typename T, bool intrinsic>
struct make_unsigned_impl
{
using type = typename boost::multiprecision::detail::make_unsigned<T>::type;
};
template <typename T>
struct make_unsigned_impl<T, false>
{
using type = T;
};
template <typename T>
struct make_unsigned_mp
{
using type = typename make_unsigned_impl<T, boost::multiprecision::detail::is_integral<T>::value>::type;
};
template <typename Engine, typename T>
T generate_uniform_int (Engine& eng, T min_value, T max_value)
{
using range_type = typename boost::multiprecision::detail::make_unsigned_mp<T>::type;
using base_result = typename Engine::result_type;
using base_unsigned = typename boost::multiprecision::detail::make_unsigned_mp<base_result>::type;
const range_type range = max_value - min_value;
const base_result bmin = (eng.min)();
const base_unsigned brange = (eng.max)() - (eng.min)();
if(range == 0)
{
return min_value;
}
else if (brange < range)
{
for(;;)
{
range_type limit;
if(range == (std::numeric_limits<range_type>::max)())
{
limit = range / (range_type(brange) + 1);
if(range % (range_type(brange) + 1) == range_type(brange))
{
++limit;
}
}
else
{
limit = (range + 1) / (range_type(brange) + 1);
}
range_type result = 0;
range_type mult = 1;
while (mult <= limit)
{
result += static_cast<range_type>(static_cast<range_type>(eng() - bmin) * mult);
if(mult * range_type(brange) == range - mult + 1)
{
return(result);
}
mult *= range_type(brange)+range_type(1);
}
range_type result_increment = generate_uniform_int(eng, range_type(0), range_type(range/mult));
if(std::numeric_limits<range_type>::is_bounded && ((std::numeric_limits<range_type>::max)() / mult < result_increment))
{
continue;
}
result_increment *= mult;
result += result_increment;
if(result < result_increment)
{
continue;
}
if(result > range)
{
continue;
}
return result + min_value;
}
}
else
{
using mixed_range_type =
typename std::conditional<std::numeric_limits<range_type>::is_specialized && std::numeric_limits<base_unsigned>::is_specialized &&
(std::numeric_limits<range_type>::digits >= std::numeric_limits<base_unsigned>::digits),
range_type, base_unsigned>::type;
mixed_range_type bucket_size;
if(brange == (std::numeric_limits<base_unsigned>::max)())
{
bucket_size = static_cast<mixed_range_type>(brange) / (static_cast<mixed_range_type>(range)+1);
if(static_cast<mixed_range_type>(brange) % (static_cast<mixed_range_type>(range)+1) == static_cast<mixed_range_type>(range))
{
++bucket_size;
}
}
else
{
bucket_size = static_cast<mixed_range_type>(brange + 1) / (static_cast<mixed_range_type>(range)+1);
}
for(;;)
{
mixed_range_type result = eng() - bmin;
result /= bucket_size;
if(result <= static_cast<mixed_range_type>(range))
{
return result + min_value;
}
}
}
}
} // Namespace detail
template <typename Integer = int>
class uniform_int_distribution
{
private:
Integer min_;
Integer max_;
public:
class param_type
{
private:
Integer min_;
Integer max_;
public:
explicit param_type(Integer min_val, Integer max_val) : min_ {min_val}, max_ {max_val}
{
BOOST_MP_ASSERT(min_ <= max_);
}
Integer a() const { return min_; }
Integer b() const { return max_; }
};
explicit uniform_int_distribution(Integer min_arg, Integer max_arg) : min_ {min_arg}, max_ {max_arg}
{
BOOST_MP_ASSERT(min_ <= max_);
}
explicit uniform_int_distribution(const param_type& param_arg) : min_ {param_arg.a()}, max_ {param_arg.b()} {}
Integer min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return min_; }
Integer max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return max_; }
Integer a() const { return min_; }
Integer b() const { return max_; }
param_type param() const { return param_type(min_, max_); }
void param(const param_type& param_arg)
{
min_ = param_arg.a();
max_ = param_arg.b();
}
template <typename Engine>
Integer operator() (Engine& eng) const
{
return detail::generate_uniform_int(eng, min_, max_);
}
template <typename Engine>
Integer operator() (Engine& eng, const param_type& param_arg) const
{
return detail::generate_uniform_int(eng, param_arg.a(), param_arg.b());
}
};
}} // Namespaces
#endif // BOOST_MP_UNIFORM_INT_DISTRIBUTION_HPP

View file

@ -0,0 +1,374 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock.
// Copyright Christopher Kormanyos 2013. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_MP_UTYPE_HELPER_HPP
#define BOOST_MP_UTYPE_HELPER_HPP
#include <limits>
#include <cstdint>
namespace boost {
namespace multiprecision {
namespace detail {
template <const unsigned>
struct utype_helper
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<0U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<1U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<2U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<3U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<4U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<5U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<6U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<7U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<8U>
{
using exact = boost::uint8_t;
};
template <>
struct utype_helper<9U>
{
using exact = std::uint16_t;
};
template <>
struct utype_helper<10U>
{
using exact = std::uint16_t;
};
template <>
struct utype_helper<11U>
{
using exact = std::uint16_t;
};
template <>
struct utype_helper<12U>
{
using exact = std::uint16_t;
};
template <>
struct utype_helper<13U>
{
using exact = std::uint16_t;
};
template <>
struct utype_helper<14U>
{
using exact = std::uint16_t;
};
template <>
struct utype_helper<15U>
{
using exact = std::uint16_t;
};
template <>
struct utype_helper<16U>
{
using exact = std::uint16_t;
};
template <>
struct utype_helper<17U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<18U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<19U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<20U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<21U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<22U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<23U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<24U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<25U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<26U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<27U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<28U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<29U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<30U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<31U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<32U>
{
using exact = std::uint32_t;
};
template <>
struct utype_helper<33U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<34U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<35U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<36U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<37U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<38U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<39U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<40U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<41U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<42U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<43U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<44U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<45U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<46U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<47U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<48U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<49U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<50U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<51U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<52U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<53U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<54U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<55U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<56U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<57U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<58U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<59U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<60U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<61U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<62U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<63U>
{
using exact = std::uint64_t;
};
template <>
struct utype_helper<64U>
{
using exact = std::uint64_t;
};
template <class unsigned_type>
int utype_prior(unsigned_type ui)
{
// TBD: Implement a templated binary search for this.
int priority_bit;
unsigned_type priority_mask = unsigned_type(unsigned_type(1U) << (std::numeric_limits<unsigned_type>::digits - 1));
for (priority_bit = std::numeric_limits<unsigned_type>::digits - 1; priority_bit >= 0; --priority_bit)
{
if (unsigned_type(priority_mask & ui) != unsigned_type(0U))
{
break;
}
priority_mask >>= 1;
}
return priority_bit;
}
}}} // namespace boost::multiprecision::detail
#endif // BOOST_MP_UTYPE_HELPER_HPP

View file

@ -0,0 +1,248 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2018 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_EIGEN_HPP
#define BOOST_MP_EIGEN_HPP
#include <boost/multiprecision/number.hpp>
#include <Eigen/Core>
//
// Generic Eigen support code:
//
namespace Eigen {
template <class B1, class B2>
struct NumTraitsImp;
template <class B1>
struct NumTraitsImp<B1, B1>
{
using self_type = B1;
using Real = typename boost::multiprecision::scalar_result_from_possible_complex<self_type>::type;
using NonInteger = self_type; // Not correct but we can't do much better??
using Literal = double;
using Nested = self_type;
enum
{
IsComplex = boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_complex,
IsInteger = boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer,
ReadCost = 1,
AddCost = 4,
MulCost = 8,
IsSigned = std::numeric_limits<self_type>::is_specialized ? std::numeric_limits<self_type>::is_signed : true,
RequireInitialization = 1,
};
static Real epsilon()
{
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return std::numeric_limits<Real>::epsilon();
}
static Real dummy_precision()
{
return 1000 * epsilon();
}
static Real highest()
{
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return (std::numeric_limits<Real>::max)();
}
static Real lowest()
{
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return (std::numeric_limits<Real>::min)();
}
static int digits10_imp(const std::integral_constant<bool, true>&)
{
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return std::numeric_limits<Real>::digits10;
}
template <bool B>
static int digits10_imp(const std::integral_constant<bool, B>&)
{
return Real::thread_default_precision();
}
static int digits10()
{
return digits10_imp(std::integral_constant < bool, std::numeric_limits<Real>::digits10 && (std::numeric_limits<Real>::digits10 != INT_MAX) ? true : false > ());
}
static int digits()
{
// return the number of digits in the component type in case Real is complex
// and we have no numeric_limits specialization.
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return std::numeric_limits<Real>::digits;
}
static int min_exponent()
{
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return std::numeric_limits<Real>::min_exponent;
}
static int max_exponent()
{
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return std::numeric_limits<Real>::max_exponent;
}
static Real infinity()
{
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return std::numeric_limits<Real>::infinity();
}
static Real quiet_NaN()
{
static_assert(std::numeric_limits<Real>::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?");
return std::numeric_limits<Real>::quiet_NaN();
}
};
template <class B1, class B2>
struct NumTraitsImp : public NumTraitsImp<B2, B2>
{
//
// This version is instantiated when B1 and B2 are different types, this happens for rational/complex/interval
// types, in which case many methods defer to those of the "component type" B2.
//
using self_type = B1;
using Real = typename boost::multiprecision::scalar_result_from_possible_complex<self_type>::type;
using NonInteger = self_type; // Not correct but we can't do much better??
using Literal = double;
using Nested = self_type;
enum
{
IsComplex = boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_complex,
IsInteger = boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer,
ReadCost = 1,
AddCost = 4,
MulCost = 8,
IsSigned = std::numeric_limits<self_type>::is_specialized ? std::numeric_limits<self_type>::is_signed : true,
RequireInitialization = 1,
};
static B2 epsilon()
{
return NumTraitsImp<B2, B2>::epsilon();
}
static B2 dummy_precision()
{
return 1000 * epsilon();
}
static B2 highest()
{
return NumTraitsImp<B2, B2>::highest();
}
static B2 lowest()
{
return NumTraitsImp<B2, B2>::lowest();
}
static int digits10()
{
return NumTraitsImp<B2, B2>::digits10();
}
static int digits()
{
return NumTraitsImp<B2, B2>::digits();
}
static int min_exponent()
{
return NumTraitsImp<B2, B2>::min_exponent();
}
static int max_exponent()
{
return NumTraitsImp<B2, B2>::max_exponent();
}
static B2 infinity()
{
return NumTraitsImp<B2, B2>::infinity();
}
static B2 quiet_NaN()
{
return NumTraitsImp<B2, B2>::quiet_NaN();
}
};
template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates>
struct NumTraits<boost::multiprecision::number<Backend, ExpressionTemplates> > : public NumTraitsImp<boost::multiprecision::number<Backend, ExpressionTemplates>, typename boost::multiprecision::number<Backend, ExpressionTemplates>::value_type>
{};
template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
struct NumTraits<boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > : public NumTraits<typename boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>
{};
#define BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(A) \
template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates, typename BinaryOp> \
struct ScalarBinaryOpTraits<boost::multiprecision::number<Backend, ExpressionTemplates>, A, BinaryOp> \
{ \
/*static_assert(boost::multiprecision::is_compatible_arithmetic_type<A, boost::multiprecision::number<Backend, ExpressionTemplates> >::value, "Interoperability with this arithmetic type is not supported.");*/ \
using ReturnType = boost::multiprecision::number<Backend, ExpressionTemplates>; \
}; \
template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates, typename BinaryOp> \
struct ScalarBinaryOpTraits<A, boost::multiprecision::number<Backend, ExpressionTemplates>, BinaryOp> \
{ \
/*static_assert(boost::multiprecision::is_compatible_arithmetic_type<A, boost::multiprecision::number<Backend, ExpressionTemplates> >::value, "Interoperability with this arithmetic type is not supported.");*/ \
using ReturnType = boost::multiprecision::number<Backend, ExpressionTemplates>; \
};
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(float)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(double)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(long double)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(char)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(unsigned char)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(signed char)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(short)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(unsigned short)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(int)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(unsigned int)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(long)
BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(unsigned long)
#if 0
template<class Backend, boost::multiprecision::expression_template_option ExpressionTemplates, class Backend2, boost::multiprecision::expression_template_option ExpressionTemplates2, typename BinaryOp>
struct ScalarBinaryOpTraits<boost::multiprecision::number<Backend, ExpressionTemplates>, boost::multiprecision::number<Backend2, ExpressionTemplates2>, BinaryOp>
{
static_assert(
boost::multiprecision::is_compatible_arithmetic_type<boost::multiprecision::number<Backend2, ExpressionTemplates2>, boost::multiprecision::number<Backend, ExpressionTemplates> >::value
|| boost::multiprecision::is_compatible_arithmetic_type<boost::multiprecision::number<Backend, ExpressionTemplates>, boost::multiprecision::number<Backend2, ExpressionTemplates2> >::value, "Interoperability with this arithmetic type is not supported.");
using ReturnType = typename std::conditional<std::is_convertible<boost::multiprecision::number<Backend2, ExpressionTemplates2>, boost::multiprecision::number<Backend, ExpressionTemplates> >::value,
boost::multiprecision::number<Backend, ExpressionTemplates>, boost::multiprecision::number<Backend2, ExpressionTemplates2> >::type;
};
template<unsigned D, typename BinaryOp>
struct ScalarBinaryOpTraits<boost::multiprecision::number<boost::multiprecision::backends::mpc_complex_backend<D>, boost::multiprecision::et_on>, boost::multiprecision::mpfr_float, BinaryOp>
{
using ReturnType = boost::multiprecision::number<boost::multiprecision::backends::mpc_complex_backend<D>, boost::multiprecision::et_on>;
};
template<typename BinaryOp>
struct ScalarBinaryOpTraits<boost::multiprecision::mpfr_float, boost::multiprecision::mpc_complex, BinaryOp>
{
using ReturnType = boost::multiprecision::number<boost::multiprecision::backends::mpc_complex_backend<0>, boost::multiprecision::et_on>;
};
template<class Backend, boost::multiprecision::expression_template_option ExpressionTemplates, typename BinaryOp>
struct ScalarBinaryOpTraits<boost::multiprecision::number<Backend, ExpressionTemplates>, boost::multiprecision::number<Backend, ExpressionTemplates>, BinaryOp>
{
using ReturnType = boost::multiprecision::number<Backend, ExpressionTemplates>;
};
#endif
template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates, class tag, class Arg1, class Arg2, class Arg3, class Arg4, typename BinaryOp>
struct ScalarBinaryOpTraits<boost::multiprecision::number<Backend, ExpressionTemplates>, boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, BinaryOp>
{
static_assert(std::is_convertible<typename boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, boost::multiprecision::number<Backend, ExpressionTemplates> >::value, "Interoperability with this arithmetic type is not supported.");
using ReturnType = boost::multiprecision::number<Backend, ExpressionTemplates>;
};
template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class Backend, boost::multiprecision::expression_template_option ExpressionTemplates, typename BinaryOp>
struct ScalarBinaryOpTraits<boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, boost::multiprecision::number<Backend, ExpressionTemplates>, BinaryOp>
{
static_assert(std::is_convertible<typename boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, boost::multiprecision::number<Backend, ExpressionTemplates> >::value, "Interoperability with this arithmetic type is not supported.");
using ReturnType = boost::multiprecision::number<Backend, ExpressionTemplates>;
};
namespace numext {
using boost::multiprecision::conj;
}
} // namespace Eigen
#endif

View file

@ -0,0 +1,917 @@
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_FLOAT128_HPP
#define BOOST_MP_FLOAT128_HPP
// https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html
#if !defined(__amd64__) && !defined(__amd64) && !defined(__x86_64__) && !defined(__x86_64) && !defined(_M_X64) && !defined(_M_AMD64) && \
!defined(i386) && !defined(__i386) && !defined(__i386__) && !defined(_M_IX86) && !defined(__X86__) && !defined(_X86_) && !defined(__I86__) && \
!defined(__ia64__) && !defined(_IA64) && !defined(__IA64__) && !defined(__ia64) && !defined(_M_IA64) && !defined(__itanium__) && \
!defined(__hppa__) && !defined(__HPPA__) && !defined(__hppa) && \
!defined(__powerpc) && !defined(_M_PPC) && !defined(_ARCH_PPC) && !defined(_ARCH_PPC64) && !defined(__PPCBROADWAY__)
#error libquadmath only works on on i386, x86_64, IA-64, and hppa HP-UX, as well as on PowerPC GNU/Linux targets that enable the vector scalar (VSX) instruction set.
#endif
#include <memory>
#include <climits>
#include <cfloat>
#include <tuple>
#include <cstring>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/detail/hash.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#if defined(BOOST_INTEL) && !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
#if defined(BOOST_INTEL_CXX_VERSION) && (BOOST_INTEL_CXX_VERSION >= 1310) && defined(__GNUC__)
#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))
#define BOOST_MP_USE_FLOAT128
#endif
#endif
#ifndef BOOST_MP_USE_FLOAT128
#define BOOST_MP_USE_QUAD
#endif
#endif
#if defined(__GNUC__) && !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
#define BOOST_MP_USE_FLOAT128
#endif
#if !defined(BOOST_MP_USE_FLOAT128) && !defined(BOOST_MP_USE_QUAD)
#error "Sorry compiler is neither GCC, not Intel, don't know how to configure this header."
#endif
#if defined(BOOST_MP_USE_FLOAT128) && defined(BOOST_MP_USE_QUAD)
#error "Oh dear, both BOOST_MP_USE_FLOAT128 and BOOST_MP_USE_QUAD are defined, which one should I be using?"
#endif
#if defined(BOOST_MP_USE_FLOAT128)
extern "C" {
#include <quadmath.h>
}
using float128_type = __float128;
#elif defined(BOOST_MP_USE_QUAD)
#include <boost/multiprecision/detail/float_string_cvt.hpp>
using float128_type = _Quad;
extern "C" {
_Quad __ldexpq(_Quad, int);
_Quad __frexpq(_Quad, int*);
_Quad __fabsq(_Quad);
_Quad __floorq(_Quad);
_Quad __ceilq(_Quad);
_Quad __sqrtq(_Quad);
_Quad __truncq(_Quad);
_Quad __expq(_Quad);
_Quad __powq(_Quad, _Quad);
_Quad __logq(_Quad);
_Quad __log10q(_Quad);
_Quad __sinq(_Quad);
_Quad __cosq(_Quad);
_Quad __tanq(_Quad);
_Quad __asinq(_Quad);
_Quad __acosq(_Quad);
_Quad __atanq(_Quad);
_Quad __sinhq(_Quad);
_Quad __coshq(_Quad);
_Quad __tanhq(_Quad);
_Quad __fmodq(_Quad, _Quad);
_Quad __atan2q(_Quad, _Quad);
#define ldexpq __ldexpq
#define frexpq __frexpq
#define fabsq __fabsq
#define floorq __floorq
#define ceilq __ceilq
#define sqrtq __sqrtq
#define truncq __truncq
#define expq __expq
#define powq __powq
#define logq __logq
#define log10q __log10q
#define sinq __sinq
#define cosq __cosq
#define tanq __tanq
#define asinq __asinq
#define acosq __acosq
#define atanq __atanq
#define sinhq __sinhq
#define coshq __coshq
#define tanhq __tanhq
#define fmodq __fmodq
#define atan2q __atan2q
}
inline _Quad isnanq(_Quad v)
{
return v != v;
}
inline _Quad isinfq(_Quad v)
{
return __fabsq(v) > 1.18973149535723176508575932662800702e4932Q;
}
#endif
namespace boost {
namespace multiprecision {
#ifndef BOOST_MP_BITS_OF_FLOAT128_DEFINED
namespace detail {
template <>
struct bits_of<float128_type>
{
static constexpr const unsigned value = 113;
};
}
#endif
template <>
struct number_category<backends::float128_backend> : public std::integral_constant<int, number_kind_floating_point>
{};
#if defined(BOOST_MP_USE_QUAD)
template <>
struct number_category<float128_type> : public std::integral_constant<int, number_kind_floating_point>
{};
#endif
namespace quad_constants {
constexpr float128_type quad_min = static_cast<float128_type>(1) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) / 1073741824;
constexpr float128_type quad_denorm_min = static_cast<float128_type>(1) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) * static_cast<float128_type>(DBL_MIN) / 5.5751862996326557854e+42;
constexpr double dbl_mult = 8.9884656743115795386e+307; // This has one bit set only.
constexpr float128_type quad_max = (static_cast<float128_type>(1) - 9.62964972193617926527988971292463659e-35) // This now has all bits sets to 1
* static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * static_cast<float128_type>(dbl_mult) * 65536;
} // namespace quad_constants
#define BOOST_MP_QUAD_MIN boost::multiprecision::quad_constants::quad_min
#define BOOST_MP_QUAD_DENORM_MIN boost::multiprecision::quad_constants::quad_denorm_min
#define BOOST_MP_QUAD_MAX boost::multiprecision::quad_constants::quad_max
namespace backends {
struct float128_backend
{
using signed_types = std::tuple<signed char, short, int, long, long long>;
using unsigned_types = std::tuple<unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long>;
using float_types = std::tuple<float, double, long double>;
using exponent_type = int ;
private:
float128_type m_value;
public:
constexpr float128_backend() noexcept : m_value(0) {}
constexpr float128_backend(const float128_backend& o) noexcept = default;
BOOST_MP_CXX14_CONSTEXPR float128_backend& operator=(const float128_backend& o) noexcept = default;
template <class T>
constexpr float128_backend(const T& i, const typename std::enable_if<std::is_convertible<T, float128_type>::value>::type* = nullptr) noexcept(noexcept(std::declval<float128_type&>() = std::declval<const T&>()))
: m_value(i) {}
template <class T>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value || std::is_convertible<T, float128_type>::value, float128_backend&>::type operator=(const T& i) noexcept(noexcept(std::declval<float128_type&>() = std::declval<const T&>()))
{
m_value = i;
return *this;
}
BOOST_MP_CXX14_CONSTEXPR float128_backend(long double const& f) : m_value(f)
{
if (f > LDBL_MAX)
m_value = static_cast<float128_type>(HUGE_VAL);
else if (-f > LDBL_MAX)
m_value = -static_cast<float128_type>(HUGE_VAL);
}
BOOST_MP_CXX14_CONSTEXPR float128_backend& operator=(long double const& f)
{
if (f > LDBL_MAX)
m_value = static_cast<float128_type>(HUGE_VAL);
else if (-f > LDBL_MAX)
m_value = -static_cast<float128_type>(HUGE_VAL);
else
m_value = f;
return *this;
}
float128_backend& operator=(const char* s)
{
#ifndef BOOST_MP_USE_QUAD
char* p_end;
m_value = strtoflt128(s, &p_end);
if (p_end - s != (std::ptrdiff_t)std::strlen(s))
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unable to interpret input string as a floating point value"));
}
#else
boost::multiprecision::detail::convert_from_string(*this, s);
#endif
return *this;
}
BOOST_MP_CXX14_CONSTEXPR void swap(float128_backend& o) noexcept
{
// We don't call std::swap here because it's no constexpr (yet):
float128_type t(o.value());
o.value() = m_value;
m_value = t;
}
std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
{
#ifndef BOOST_MP_USE_QUAD
char buf[128];
std::string format = "%";
if (f & std::ios_base::showpos)
format += "+";
if (f & std::ios_base::showpoint)
format += "#";
format += ".*";
if ((digits == 0) && !(f & std::ios_base::fixed))
digits = 36;
format += "Q";
if (f & std::ios_base::scientific)
format += "e";
else if (f & std::ios_base::fixed)
format += "f";
else
format += "g";
int v;
if ((f & std::ios_base::scientific) && (f & std::ios_base::fixed))
{
v = quadmath_snprintf(buf, sizeof buf, "%Qa", m_value);
}
else
{
v = quadmath_snprintf(buf, sizeof buf, format.c_str(), digits, m_value);
}
if ((v < 0) || (v >= 127))
{
int v_max = v;
std::unique_ptr<char[]> buf2;
buf2.reset(new char[v + 3]);
v = quadmath_snprintf(&buf2[0], v_max + 3, format.c_str(), digits, m_value);
if (v >= v_max + 3)
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error("Formatting of float128_type failed."));
}
return &buf2[0];
}
return buf;
#else
return boost::multiprecision::detail::convert_to_string(*this, digits ? digits : 36, f);
#endif
}
BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
{
m_value = -m_value;
}
BOOST_MP_CXX14_CONSTEXPR int compare(const float128_backend& o) const
{
return m_value == o.m_value ? 0 : m_value < o.m_value ? -1 : 1;
}
template <class T>
BOOST_MP_CXX14_CONSTEXPR int compare(const T& i) const
{
return m_value == i ? 0 : m_value < i ? -1 : 1;
}
BOOST_MP_CXX14_CONSTEXPR float128_type& value()
{
return m_value;
}
BOOST_MP_CXX14_CONSTEXPR const float128_type& value() const
{
return m_value;
}
};
inline BOOST_MP_CXX14_CONSTEXPR void eval_add(float128_backend& result, const float128_backend& a)
{
result.value() += a.value();
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR void eval_add(float128_backend& result, const A& a)
{
result.value() += a;
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const float128_backend& a)
{
result.value() -= a.value();
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const A& a)
{
result.value() -= a;
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(float128_backend& result, const float128_backend& a)
{
result.value() *= a.value();
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(float128_backend& result, const A& a)
{
result.value() *= a;
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_divide(float128_backend& result, const float128_backend& a)
{
result.value() /= a.value();
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR void eval_divide(float128_backend& result, const A& a)
{
result.value() /= a;
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_add(float128_backend& result, const float128_backend& a, const float128_backend& b)
{
result.value() = a.value() + b.value();
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR void eval_add(float128_backend& result, const float128_backend& a, const A& b)
{
result.value() = a.value() + b;
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const float128_backend& a, const float128_backend& b)
{
result.value() = a.value() - b.value();
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const float128_backend& a, const A& b)
{
result.value() = a.value() - b;
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(float128_backend& result, const A& a, const float128_backend& b)
{
result.value() = a - b.value();
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(float128_backend& result, const float128_backend& a, const float128_backend& b)
{
result.value() = a.value() * b.value();
}
template <class A>
inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(float128_backend& result, const float128_backend& a, const A& b)
{
result.value() = a.value() * b;
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_divide(float128_backend& result, const float128_backend& a, const float128_backend& b)
{
result.value() = a.value() / b.value();
}
template <class R>
inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(R* result, const float128_backend& val)
{
*result = static_cast<R>(val.value());
}
inline void eval_frexp(float128_backend& result, const float128_backend& arg, int* exp)
{
result.value() = frexpq(arg.value(), exp);
}
inline void eval_ldexp(float128_backend& result, const float128_backend& arg, int exp)
{
result.value() = ldexpq(arg.value(), exp);
}
inline void eval_floor(float128_backend& result, const float128_backend& arg)
{
result.value() = floorq(arg.value());
}
inline void eval_ceil(float128_backend& result, const float128_backend& arg)
{
result.value() = ceilq(arg.value());
}
inline void eval_sqrt(float128_backend& result, const float128_backend& arg)
{
result.value() = sqrtq(arg.value());
}
inline void eval_rsqrt(float128_backend& result, const float128_backend& arg)
{
#if (LDBL_MANT_DIG > 100)
// GCC can't mix and match __float128 and quad precision long double
// error: __float128 and long double cannot be used in the same expression
result.value() = 1 / sqrtq(arg.value());
#else
if (arg.value() < std::numeric_limits<long double>::denorm_min() || arg.value() > (std::numeric_limits<long double>::max)()) {
result.value() = 1/sqrtq(arg.value());
return;
}
using std::sqrt;
float128_backend xk = 1/sqrt(static_cast<long double>(arg.value()));
// Newton iteration for f(x) = arg.value() - 1/x^2.
BOOST_IF_CONSTEXPR (sizeof(long double) == sizeof(double)) {
// If the long double is the same as a double, then we need two Newton iterations:
xk.value() = xk.value() + xk.value()*(1-arg.value()*xk.value()*xk.value())/2;
result.value() = xk.value() + xk.value()*(1-arg.value()*xk.value()*xk.value())/2;
}
else
{
// 80 bit long double only needs a single iteration to produce ~2ULPs.
result.value() = xk.value() + xk.value() * (1 - arg.value() * xk.value() * xk.value()) / 2;
}
#endif
}
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
inline BOOST_MP_CXX14_CONSTEXPR
#else
inline
#endif
int eval_fpclassify(const float128_backend& arg)
{
float128_type v = arg.value();
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
if (BOOST_MP_IS_CONST_EVALUATED(v))
{
if (v != v)
return FP_NAN;
if (v == 0)
return FP_ZERO;
float128_type t(v);
if (t < 0)
t = -t;
if (t > BOOST_MP_QUAD_MAX)
return FP_INFINITE;
if (t < BOOST_MP_QUAD_MIN)
return FP_SUBNORMAL;
return FP_NORMAL;
}
else
#endif
{
if (isnanq(v))
return FP_NAN;
else if (isinfq(v))
return FP_INFINITE;
else if (v == 0)
return FP_ZERO;
float128_backend t(arg);
if (t.value() < 0)
t.negate();
if (t.value() < BOOST_MP_QUAD_MIN)
return FP_SUBNORMAL;
return FP_NORMAL;
}
}
#if defined(BOOST_GCC) && (__GNUC__ == 9)
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91705
inline BOOST_MP_CXX14_CONSTEXPR void eval_increment(float128_backend& arg)
{
arg.value() = 1 + arg.value();
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_decrement(float128_backend& arg)
{
arg.value() = arg.value() - 1;
}
#else
inline BOOST_MP_CXX14_CONSTEXPR void eval_increment(float128_backend& arg)
{
++arg.value();
}
inline BOOST_MP_CXX14_CONSTEXPR void eval_decrement(float128_backend& arg)
{
--arg.value();
}
#endif
/*********************************************************************
*
* abs/fabs:
*
*********************************************************************/
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
inline BOOST_MP_CXX14_CONSTEXPR void eval_abs(float128_backend& result, const float128_backend& arg)
#else
inline void eval_abs(float128_backend& result, const float128_backend& arg)
#endif
{
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
float128_type v(arg.value());
if (BOOST_MP_IS_CONST_EVALUATED(v))
{
result.value() = v < 0 ? -v : v;
}
else
#endif
{
result.value() = fabsq(arg.value());
}
}
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
inline BOOST_MP_CXX14_CONSTEXPR void eval_fabs(float128_backend& result, const float128_backend& arg)
#else
inline void eval_fabs(float128_backend& result, const float128_backend& arg)
#endif
{
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
float128_type v(arg.value());
if (BOOST_MP_IS_CONST_EVALUATED(v))
{
result.value() = v < 0 ? -v : v;
}
else
#endif
{
result.value() = fabsq(arg.value());
}
}
/*********************************************************************
*
* Floating point functions:
*
*********************************************************************/
inline void eval_trunc(float128_backend& result, const float128_backend& arg)
{
result.value() = truncq(arg.value());
}
/*
//
// This doesn't actually work... rely on our own default version instead.
//
inline void eval_round(float128_backend& result, const float128_backend& arg)
{
if(isnanq(arg.value()) || isinf(arg.value()))
{
result = boost::math::policies::raise_rounding_error(
"boost::multiprecision::trunc<%1%>(%1%)", nullptr,
number<float128_backend, et_off>(arg),
number<float128_backend, et_off>(arg),
boost::math::policies::policy<>()).backend();
return;
}
result.value() = roundq(arg.value());
}
*/
inline void eval_exp(float128_backend& result, const float128_backend& arg)
{
result.value() = expq(arg.value());
}
inline void eval_log(float128_backend& result, const float128_backend& arg)
{
result.value() = logq(arg.value());
}
inline void eval_log10(float128_backend& result, const float128_backend& arg)
{
result.value() = log10q(arg.value());
}
inline void eval_sin(float128_backend& result, const float128_backend& arg)
{
result.value() = sinq(arg.value());
}
inline void eval_cos(float128_backend& result, const float128_backend& arg)
{
result.value() = cosq(arg.value());
}
inline void eval_tan(float128_backend& result, const float128_backend& arg)
{
result.value() = tanq(arg.value());
}
inline void eval_asin(float128_backend& result, const float128_backend& arg)
{
result.value() = asinq(arg.value());
}
inline void eval_acos(float128_backend& result, const float128_backend& arg)
{
result.value() = acosq(arg.value());
}
inline void eval_atan(float128_backend& result, const float128_backend& arg)
{
result.value() = atanq(arg.value());
}
inline void eval_sinh(float128_backend& result, const float128_backend& arg)
{
result.value() = sinhq(arg.value());
}
inline void eval_cosh(float128_backend& result, const float128_backend& arg)
{
result.value() = coshq(arg.value());
}
inline void eval_tanh(float128_backend& result, const float128_backend& arg)
{
result.value() = tanhq(arg.value());
}
inline void eval_fmod(float128_backend& result, const float128_backend& a, const float128_backend& b)
{
result.value() = fmodq(a.value(), b.value());
}
inline void eval_pow(float128_backend& result, const float128_backend& a, const float128_backend& b)
{
result.value() = powq(a.value(), b.value());
}
inline void eval_atan2(float128_backend& result, const float128_backend& a, const float128_backend& b)
{
result.value() = atan2q(a.value(), b.value());
}
#ifndef BOOST_MP_USE_QUAD
inline void eval_multiply_add(float128_backend& result, const float128_backend& a, const float128_backend& b, const float128_backend& c)
{
result.value() = fmaq(a.value(), b.value(), c.value());
}
inline int eval_signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const float128_backend& arg)
{
return ::signbitq(arg.value());
}
#endif
inline std::size_t hash_value(const float128_backend& val)
{
return boost::multiprecision::detail::hash_value(static_cast<double>(val.value()));
}
} // namespace backends
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> asinh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return asinhq(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> acosh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return acoshq(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> atanh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return atanhq(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return cbrtq(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return erfq(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return erfcq(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return expm1q(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return lgammaq(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
if(eval_signbit(arg.backend()) != 0)
{
const bool result_is_neg = ((static_cast<unsigned long long>(floorq(-arg.backend().value())) % 2U) == 0U);
const boost::multiprecision::number<float128_backend, ExpressionTemplates> result_of_tgammaq = fabsq(tgammaq(arg.backend().value()));
return ((result_is_neg == false) ? result_of_tgammaq : -result_of_tgammaq);
}
else
{
return tgammaq(arg.backend().value());
}
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
return log1pq(arg.backend().value());
}
template <boost::multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<float128_backend, ExpressionTemplates> rsqrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<float128_backend, ExpressionTemplates>& arg)
{
boost::multiprecision::number<float128_backend, ExpressionTemplates> res;
eval_rsqrt(res.backend(), arg.backend());
return res;
}
#ifndef BOOST_MP_USE_QUAD
template <multiprecision::expression_template_option ExpressionTemplates>
inline boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates>& a, const boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates>& b)
{
return ::copysignq(a.backend().value(), b.backend().value());
}
namespace backends {
inline void eval_remainder(float128_backend& result, const float128_backend& a, const float128_backend& b)
{
result.value() = remainderq(a.value(), b.value());
}
inline void eval_remainder(float128_backend& result, const float128_backend& a, const float128_backend& b, int* pi)
{
result.value() = remquoq(a.value(), b.value(), pi);
}
} // namespace backends
#endif
} // namespace multiprecision
namespace math {
using boost::multiprecision::copysign;
using boost::multiprecision::signbit;
} // namespace math
} // namespace boost
#ifndef BOOST_MP_STANDALONE
namespace boost {
namespace archive {
class binary_oarchive;
class binary_iarchive;
} // namespace archive
namespace serialization {
namespace float128_detail {
template <class Archive>
void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&)
{
// saving
// non-binary
std::string s(val.str(0, std::ios_base::scientific));
ar& boost::make_nvp("value", s);
}
template <class Archive>
void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&)
{
// loading
// non-binary
std::string s;
ar& boost::make_nvp("value", s);
val = s.c_str();
}
template <class Archive>
void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&)
{
// saving
// binary
ar.save_binary(&val, sizeof(val));
}
template <class Archive>
void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&)
{
// loading
// binary
ar.load_binary(&val, sizeof(val));
}
} // namespace float128_detail
template <class Archive>
void serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, unsigned int /*version*/)
{
using load_tag = typename Archive::is_loading ;
using loading = std::integral_constant<bool, load_tag::value> ;
using binary_tag = typename std::integral_constant<bool, std::is_same<Archive, boost::archive::binary_oarchive>::value || std::is_same<Archive, boost::archive::binary_iarchive>::value>;
float128_detail::do_serialize(ar, val, loading(), binary_tag());
}
} // namespace serialization
} // namespace boost
#endif // BOOST_MP_STANDALONE
namespace std {
template <boost::multiprecision::expression_template_option ExpressionTemplates>
class numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >
{
using number_type = boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates>;
public:
static constexpr bool is_specialized = true;
static BOOST_MP_CXX14_CONSTEXPR number_type(min)() noexcept { return BOOST_MP_QUAD_MIN; }
static BOOST_MP_CXX14_CONSTEXPR number_type(max)() noexcept { return BOOST_MP_QUAD_MAX; }
static BOOST_MP_CXX14_CONSTEXPR number_type lowest() noexcept { return -(max)(); }
static constexpr int digits = 113;
static constexpr int digits10 = 33;
static constexpr int max_digits10 = 36;
static constexpr bool is_signed = true;
static constexpr bool is_integer = false;
static constexpr bool is_exact = false;
static constexpr int radix = 2;
static BOOST_MP_CXX14_CONSTEXPR number_type epsilon() { return 1.92592994438723585305597794258492732e-34; /* this double value has only one bit set and so is exact */ }
static BOOST_MP_CXX14_CONSTEXPR number_type round_error() { return 0.5; }
static constexpr int min_exponent = -16381;
static constexpr int min_exponent10 = min_exponent * 301L / 1000L;
static constexpr int max_exponent = 16384;
static constexpr int max_exponent10 = max_exponent * 301L / 1000L;
static constexpr bool has_infinity = true;
static constexpr bool has_quiet_NaN = true;
static constexpr bool has_signaling_NaN = false;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
static constexpr float_denorm_style has_denorm = denorm_present;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
static constexpr bool has_denorm_loss = true;
static BOOST_MP_CXX14_CONSTEXPR number_type infinity() { return HUGE_VAL; /* conversion from double infinity OK */ }
static BOOST_MP_CXX14_CONSTEXPR number_type quiet_NaN() { return number_type(NAN); }
static BOOST_MP_CXX14_CONSTEXPR number_type signaling_NaN() { return 0; }
static BOOST_MP_CXX14_CONSTEXPR number_type denorm_min() { return BOOST_MP_QUAD_DENORM_MIN; }
static constexpr bool is_iec559 = true;
static constexpr bool is_bounded = true;
static constexpr bool is_modulo = false;
static constexpr bool traps = false;
static constexpr bool tinyness_before = false;
static constexpr float_round_style round_style = round_to_nearest;
};
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_specialized;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::digits;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::digits10;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::max_digits10;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_signed;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_integer;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_exact;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::radix;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::min_exponent;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::max_exponent;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::min_exponent10;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::max_exponent10;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_infinity;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_quiet_NaN;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_signaling_NaN;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_denorm_loss;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_iec559;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_bounded;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::is_modulo;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::traps;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::tinyness_before;
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::round_style;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
template <boost::multiprecision::expression_template_option ExpressionTemplates>
constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::has_denorm;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
} // namespace std
#endif

View file

@ -0,0 +1,268 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2023 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_FWD_HPP
#define BOOST_MP_FWD_HPP
#include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
namespace boost {
namespace multiprecision {
enum expression_template_option
{
et_off = 0,
et_on = 1
};
template <class Backend>
struct expression_template_default
{
static constexpr expression_template_option value = et_on;
};
template <class Backend, expression_template_option ExpressionTemplates = expression_template_default<Backend>::value>
class number;
template <class T>
struct is_number : public std::integral_constant<bool, false>
{};
template <class Backend, expression_template_option ExpressionTemplates>
struct is_number<number<Backend, ExpressionTemplates> > : public std::integral_constant<bool, true>
{};
namespace detail {
// Forward-declare an expression wrapper
template <class tag, class Arg1 = void, class Arg2 = void, class Arg3 = void, class Arg4 = void>
struct expression;
} // namespace detail
enum cpp_integer_type
{
signed_magnitude = 1,
unsigned_magnitude = 0,
signed_packed = 3,
unsigned_packed = 2
};
enum cpp_int_check_type
{
checked = 1,
unchecked = 0
};
enum mpfr_allocation_type
{
allocate_stack,
allocate_dynamic
};
template <class Backend>
void log_postfix_event(const Backend&, const char* /*event_description*/);
template <class Backend, class T>
void log_postfix_event(const Backend&, const T&, const char* /*event_description*/);
template <class Backend>
void log_prefix_event(const Backend&, const char* /*event_description*/);
template <class Backend, class T>
void log_prefix_event(const Backend&, const T&, const char* /*event_description*/);
template <class Backend, class T, class U>
void log_prefix_event(const Backend&, const T&, const U&, const char* /*event_description*/);
template <class Backend, class T, class U, class V>
void log_prefix_event(const Backend&, const T&, const U&, const V&, const char* /*event_description*/);
namespace backends {
template <class Backend>
struct debug_adaptor;
template <class Backend>
struct logged_adaptor;
template <class Backend>
struct complex_adaptor;
enum digit_base_type
{
digit_base_2 = 2,
digit_base_10 = 10
};
template <unsigned Digits, digit_base_type DigitBase = digit_base_10, class Allocator = void, class Exponent = int, Exponent MinExponent = 0, Exponent MaxExponent = 0>
class cpp_bin_float;
template <unsigned Digits10, class ExponentType = std::int32_t, class Allocator = void>
class cpp_dec_float;
template <std::size_t MinBits = 0, std::size_t MaxBits = 0, boost::multiprecision::cpp_integer_type SignType = signed_magnitude, cpp_int_check_type Checked = unchecked, class Allocator = typename std::conditional<MinBits && (MinBits == MaxBits), void, std::allocator<limb_type> >::type>
struct cpp_int_backend;
struct float128_backend;
struct gmp_int;
struct gmp_rational;
template <unsigned digits10>
struct gmp_float;
template <unsigned digits10>
struct mpc_complex_backend;
template <unsigned digits10>
struct mpfi_float_backend;
template <unsigned digits10, mpfr_allocation_type AllocationType = allocate_dynamic>
struct mpfr_float_backend;
template <>
struct mpfr_float_backend<0, allocate_stack>;
template <class Backend>
struct rational_adaptor;
struct tommath_int;
}
using boost::multiprecision::backends::complex_adaptor;
using boost::multiprecision::backends::debug_adaptor;
using boost::multiprecision::backends::logged_adaptor;
using backends::cpp_bin_float;
using backends::digit_base_10;
using backends::digit_base_2;
using boost::multiprecision::backends::cpp_dec_float;
using boost::multiprecision::backends::cpp_int_backend;
using boost::multiprecision::backends::float128_backend;
using boost::multiprecision::backends::gmp_float;
using boost::multiprecision::backends::gmp_int;
using boost::multiprecision::backends::gmp_rational;
using boost::multiprecision::backends::mpc_complex_backend;
using boost::multiprecision::backends::mpfi_float_backend;
using boost::multiprecision::backends::mpfr_float_backend;
using boost::multiprecision::backends::rational_adaptor;
using boost::multiprecision::backends::tommath_int;
template <unsigned Digits, backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
struct expression_template_default<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >
{
static constexpr expression_template_option value = std::is_void<Allocator>::value ? et_off : et_on;
};
template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked>
struct expression_template_default<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, void> >
{
static constexpr expression_template_option value = et_off;
};
template <class IntBackend>
struct expression_template_default<backends::rational_adaptor<IntBackend> > : public expression_template_default<IntBackend>
{};
using complex128 = number<complex_adaptor<float128_backend>, et_off>;
using cpp_bin_float_50 = number<backends::cpp_bin_float<50> >;
using cpp_bin_float_100 = number<backends::cpp_bin_float<100> >;
using cpp_bin_float_single = number<backends::cpp_bin_float<24, backends::digit_base_2, void, std::int16_t, -126, 127>, et_off>;
using cpp_bin_float_double = number<backends::cpp_bin_float<53, backends::digit_base_2, void, std::int16_t, -1022, 1023>, et_off>;
using cpp_bin_float_double_extended = number<backends::cpp_bin_float<64, backends::digit_base_2, void, std::int16_t, -16382, 16383>, et_off>;
using cpp_bin_float_quad = number<backends::cpp_bin_float<113, backends::digit_base_2, void, std::int16_t, -16382, 16383>, et_off>;
using cpp_bin_float_oct = number<backends::cpp_bin_float<237, backends::digit_base_2, void, std::int32_t, -262142, 262143>, et_off>;
template <unsigned Digits, backends::digit_base_type DigitBase = backends::digit_base_10, class Allocator = void, class Exponent = int, Exponent MinExponent = 0, Exponent MaxExponent = 0>
using cpp_complex_backend = complex_adaptor<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> >;
template <unsigned Digits, backends::digit_base_type DigitBase = digit_base_10, class Allocator = void, class Exponent = int, Exponent MinExponent = 0, Exponent MaxExponent = 0, expression_template_option ExpressionTemplates = et_off>
using cpp_complex = number<complex_adaptor<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> >, ExpressionTemplates>;
using cpp_complex_50 = cpp_complex<50>;
using cpp_complex_100 = cpp_complex<100>;
using cpp_complex_single = cpp_complex<24, backends::digit_base_2, void, std::int16_t, -126, 127>;
using cpp_complex_double = cpp_complex<53, backends::digit_base_2, void, std::int16_t, -1022, 1023>;
using cpp_complex_extended = cpp_complex<64, backends::digit_base_2, void, std::int16_t, -16382, 16383>;
using cpp_complex_quad = cpp_complex<113, backends::digit_base_2, void, std::int16_t, -16382, 16383>;
using cpp_complex_oct = cpp_complex<237, backends::digit_base_2, void, std::int32_t, -262142, 262143>;
using cpp_dec_float_50 = number<cpp_dec_float<50> >;
using cpp_dec_float_100 = number<cpp_dec_float<100> >;
using cpp_int = number<cpp_int_backend<> >;
using cpp_rational_backend = rational_adaptor<cpp_int_backend<> >;
using cpp_rational = number<cpp_rational_backend>;
// Fixed precision unsigned types:
using uint128_t = number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void> >;
using uint256_t = number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void> >;
using uint512_t = number<cpp_int_backend<512, 512, unsigned_magnitude, unchecked, void> >;
using uint1024_t = number<cpp_int_backend<1024, 1024, unsigned_magnitude, unchecked, void> >;
// Fixed precision signed types:
using int128_t = number<cpp_int_backend<128, 128, signed_magnitude, unchecked, void> >;
using int256_t = number<cpp_int_backend<256, 256, signed_magnitude, unchecked, void> >;
using int512_t = number<cpp_int_backend<512, 512, signed_magnitude, unchecked, void> >;
using int1024_t = number<cpp_int_backend<1024, 1024, signed_magnitude, unchecked, void> >;
// Over again, but with checking enabled this time:
using checked_cpp_int = number<cpp_int_backend<0, 0, signed_magnitude, checked> >;
using checked_cpp_rational_backend = rational_adaptor<cpp_int_backend<0, 0, signed_magnitude, checked> >;
using checked_cpp_rational = number<checked_cpp_rational_backend>;
// Fixed precision unsigned types:
using checked_uint128_t = number<cpp_int_backend<128, 128, unsigned_magnitude, checked, void> >;
using checked_uint256_t = number<cpp_int_backend<256, 256, unsigned_magnitude, checked, void> >;
using checked_uint512_t = number<cpp_int_backend<512, 512, unsigned_magnitude, checked, void> >;
using checked_uint1024_t = number<cpp_int_backend<1024, 1024, unsigned_magnitude, checked, void> >;
// Fixed precision signed types:
using checked_int128_t = number<cpp_int_backend<128, 128, signed_magnitude, checked, void> >;
using checked_int256_t = number<cpp_int_backend<256, 256, signed_magnitude, checked, void> >;
using checked_int512_t = number<cpp_int_backend<512, 512, signed_magnitude, checked, void> >;
using checked_int1024_t = number<cpp_int_backend<1024, 1024, signed_magnitude, checked, void> >;
template <class Number>
using debug_adaptor_t = number<debug_adaptor<typename Number::backend_type>, Number::et>;
template <class Number>
using logged_adaptor_t = number<logged_adaptor<typename Number::backend_type>, Number::et>;
using float128 = number<float128_backend, et_off>;
using mpf_float_50 = number<gmp_float<50> >;
using mpf_float_100 = number<gmp_float<100> >;
using mpf_float_500 = number<gmp_float<500> >;
using mpf_float_1000 = number<gmp_float<1000> >;
using mpf_float = number<gmp_float<0> >;
using mpz_int = number<gmp_int>;
using mpq_rational = number<gmp_rational>;
using mpc_complex_50 = number<mpc_complex_backend<50> >;
using mpc_complex_100 = number<mpc_complex_backend<100> >;
using mpc_complex_500 = number<mpc_complex_backend<500> >;
using mpc_complex_1000 = number<mpc_complex_backend<1000> >;
using mpc_complex = number<mpc_complex_backend<0> >;
using mpfi_float_50 = number<mpfi_float_backend<50> >;
using mpfi_float_100 = number<mpfi_float_backend<100> >;
using mpfi_float_500 = number<mpfi_float_backend<500> >;
using mpfi_float_1000 = number<mpfi_float_backend<1000> >;
using mpfi_float = number<mpfi_float_backend<0> >;
using mpfr_float_50 = number<mpfr_float_backend<50> >;
using mpfr_float_100 = number<mpfr_float_backend<100> >;
using mpfr_float_500 = number<mpfr_float_backend<500> >;
using mpfr_float_1000 = number<mpfr_float_backend<1000> >;
using mpfr_float = number<mpfr_float_backend<0> >;
using static_mpfr_float_50 = number<mpfr_float_backend<50, allocate_stack> >;
using static_mpfr_float_100 = number<mpfr_float_backend<100, allocate_stack> >;
using tom_int = number<tommath_int>;
using tommath_rational = rational_adaptor<tommath_int>;
using tom_rational = number<tommath_rational>;
} }
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,363 @@
///////////////////////////////////////////////////////////////
// Copyright 2012-21 John Maddock.
// Copyright 2021 Iskandarov Lev. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_INTEGER_HPP
#define BOOST_MP_INTEGER_HPP
#include <type_traits>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/detail/bitscan.hpp>
#include <boost/multiprecision/detail/no_exceptions_support.hpp>
#include <boost/multiprecision/detail/standalone_config.hpp>
namespace boost {
namespace multiprecision {
template <class Integer, class I2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value && boost::multiprecision::detail::is_integral<I2>::value, Integer&>::type
multiply(Integer& result, const I2& a, const I2& b)
{
return result = static_cast<Integer>(a) * static_cast<Integer>(b);
}
template <class Integer, class I2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value && boost::multiprecision::detail::is_integral<I2>::value, Integer&>::type
add(Integer& result, const I2& a, const I2& b)
{
return result = static_cast<Integer>(a) + static_cast<Integer>(b);
}
template <class Integer, class I2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value && boost::multiprecision::detail::is_integral<I2>::value, Integer&>::type
subtract(Integer& result, const I2& a, const I2& b)
{
return result = static_cast<Integer>(a) - static_cast<Integer>(b);
}
template <class Integer>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value>::type divide_qr(const Integer& x, const Integer& y, Integer& q, Integer& r)
{
q = x / y;
r = x % y;
}
template <class I1, class I2>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I1>::value && boost::multiprecision::detail::is_integral<I2>::value, I2>::type integer_modulus(const I1& x, I2 val)
{
return static_cast<I2>(x % val);
}
namespace detail {
//
// Figure out the kind of integer that has twice as many bits as some builtin
// integer type I. Use a native type if we can (including types which may not
// be recognised by boost::int_t because they're larger than long long),
// otherwise synthesize a cpp_int to do the job.
//
template <class I>
struct double_integer
{
static constexpr const unsigned int_t_digits =
2 * sizeof(I) <= sizeof(long long) ? std::numeric_limits<I>::digits * 2 : 1;
using type = typename std::conditional<
2 * sizeof(I) <= sizeof(long long),
typename std::conditional<
boost::multiprecision::detail::is_signed<I>::value && boost::multiprecision::detail::is_integral<I>::value,
typename boost::multiprecision::detail::int_t<int_t_digits>::least,
typename boost::multiprecision::detail::uint_t<int_t_digits>::least>::type,
typename std::conditional<
2 * sizeof(I) <= sizeof(double_limb_type),
typename std::conditional<
boost::multiprecision::detail::is_signed<I>::value && boost::multiprecision::detail::is_integral<I>::value,
signed_double_limb_type,
double_limb_type>::type,
number<cpp_int_backend<sizeof(I) * CHAR_BIT * 2, sizeof(I) * CHAR_BIT * 2, (boost::multiprecision::detail::is_signed<I>::value ? signed_magnitude : unsigned_magnitude), unchecked, void> > >::type>::type;
};
} // namespace detail
template <class I1, class I2, class I3>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I1>::value && boost::multiprecision::detail::is_unsigned<I2>::value && boost::multiprecision::detail::is_integral<I3>::value, I1>::type
powm(const I1& a, I2 b, I3 c)
{
using double_type = typename detail::double_integer<I1>::type;
I1 x(1), y(a);
double_type result(0);
while (b > 0)
{
if (b & 1)
{
multiply(result, x, y);
x = integer_modulus(result, c);
}
multiply(result, y, y);
y = integer_modulus(result, c);
b >>= 1;
}
return x % c;
}
template <class I1, class I2, class I3>
inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I1>::value && boost::multiprecision::detail::is_signed<I2>::value && boost::multiprecision::detail::is_integral<I2>::value && boost::multiprecision::detail::is_integral<I3>::value, I1>::type
powm(const I1& a, I2 b, I3 c)
{
if (b < 0)
{
BOOST_MP_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
}
return powm(a, static_cast<typename boost::multiprecision::detail::make_unsigned<I2>::type>(b), c);
}
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value, std::size_t>::type lsb(const Integer& val)
{
if (val <= 0)
{
if (val == 0)
{
BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
}
else
{
BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
}
}
return detail::find_lsb(val);
}
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value, std::size_t>::type msb(Integer val)
{
if (val <= 0)
{
if (val == 0)
{
BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
}
else
{
BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
}
}
return detail::find_msb(val);
}
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value, bool>::type bit_test(const Integer& val, std::size_t index)
{
Integer mask = 1;
if (index >= sizeof(Integer) * CHAR_BIT)
return 0;
if (index)
mask <<= index;
return val & mask ? true : false;
}
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value, Integer&>::type bit_set(Integer& val, std::size_t index)
{
Integer mask = 1;
if (index >= sizeof(Integer) * CHAR_BIT)
return val;
if (index)
mask <<= index;
val |= mask;
return val;
}
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value, Integer&>::type bit_unset(Integer& val, std::size_t index)
{
Integer mask = 1;
if (index >= sizeof(Integer) * CHAR_BIT)
return val;
if (index)
mask <<= index;
val &= ~mask;
return val;
}
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value, Integer&>::type bit_flip(Integer& val, std::size_t index)
{
Integer mask = 1;
if (index >= sizeof(Integer) * CHAR_BIT)
return val;
if (index)
mask <<= index;
val ^= mask;
return val;
}
namespace detail {
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR Integer karatsuba_sqrt(const Integer& x, Integer& r, size_t bits)
{
//
// Define the floating point type used for std::sqrt, in our tests, sqrt(double) and sqrt(long double) take
// about the same amount of time as long as long double is not an emulated 128-bit type (ie the same type
// as __float128 from libquadmath). So only use long double if it's an 80-bit type:
//
#ifndef __clang__
typedef typename std::conditional<(std::numeric_limits<long double>::digits == 64), long double, double>::type real_cast_type;
#else
// clang has buggy __int128 -> long double conversion:
typedef double real_cast_type;
#endif
//
// As per the Karatsuba sqrt algorithm, the low order bits/4 bits pay no part in the result, only in the remainder,
// so define the number of bits our argument must have before passing to std::sqrt is safe, even if doing so
// looses a few bits:
//
constexpr std::size_t cutoff = (std::numeric_limits<real_cast_type>::digits * 4) / 3;
//
// Type which can hold at least "cutoff" bits:
//
#ifdef BOOST_HAS_INT128
using cutoff_t = typename std::conditional<(cutoff > 64), uint128_type, std::uint64_t>::type;
#else
using cutoff_t = std::uint64_t;
#endif
//
// See if we can take the fast path:
//
if (bits <= cutoff)
{
constexpr cutoff_t half_bits = (cutoff_t(1u) << ((sizeof(cutoff_t) * CHAR_BIT) / 2)) - 1;
cutoff_t val = static_cast<cutoff_t>(x);
real_cast_type real_val = static_cast<real_cast_type>(val);
cutoff_t s64 = static_cast<cutoff_t>(std::sqrt(real_val));
// converting to long double can loose some precision, and `sqrt` can give eps error, so we'll fix this
// this is needed
while ((s64 > half_bits) || (s64 * s64 > val))
s64--;
// in my tests this never fired, but theoretically this might be needed
while ((s64 < half_bits) && ((s64 + 1) * (s64 + 1) <= val))
s64++;
r = static_cast<Integer>(val - s64 * s64);
return static_cast<Integer>(s64);
}
// https://hal.inria.fr/file/index/docid/72854/filename/RR-3805.pdf
std::size_t b = bits / 4;
Integer q = x;
q >>= b * 2;
Integer s = karatsuba_sqrt(q, r, bits - b * 2);
Integer t = 0u;
bit_set(t, static_cast<unsigned>(b * 2));
r <<= b;
t--;
t &= x;
t >>= b;
t += r;
s <<= 1;
divide_qr(t, s, q, r);
r <<= b;
t = 0u;
bit_set(t, static_cast<unsigned>(b));
t--;
t &= x;
r += t;
s <<= (b - 1); // we already <<1 it before
s += q;
q *= q;
// we substract after, so it works for unsigned integers too
if (r < q)
{
t = s;
t <<= 1;
t--;
r += t;
s--;
}
r -= q;
return s;
}
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR Integer bitwise_sqrt(const Integer& x, Integer& r)
{
//
// This is slow bit-by-bit integer square root, see for example
// http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29
// There are better methods such as http://hal.inria.fr/docs/00/07/28/54/PDF/RR-3805.pdf
// and http://hal.inria.fr/docs/00/07/21/13/PDF/RR-4475.pdf which should be implemented
// at some point.
//
Integer s = 0;
switch (x)
{
case 0:
r = 0;
return s;
case 1:
r = 0;
return 1;
case 2:
r = 1;
return 1;
case 3:
r = 2;
return 1;
default:
break;
// fall through:
}
std::ptrdiff_t g = msb(x);
Integer t = 0;
r = x;
g /= 2;
bit_set(s, g);
bit_set(t, 2 * g);
r = x - t;
--g;
do
{
t = s;
t <<= g + 1;
bit_set(t, 2 * g);
if (t <= r)
{
bit_set(s, g);
r -= t;
}
--g;
} while (g >= 0);
return s;
}
} // namespace detail
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value, Integer>::type sqrt(const Integer& x, Integer& r)
{
#ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
// recursive Karatsuba sqrt can cause issues in constexpr context:
if (BOOST_MP_IS_CONST_EVALUATED(x))
{
return detail::bitwise_sqrt(x, r);
}
#endif
if (x == 0u) {
r = 0u;
return 0u;
}
return detail::karatsuba_sqrt(x, r, msb(x) + 1);
}
template <class Integer>
BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Integer>::value, Integer>::type sqrt(const Integer& x)
{
Integer r(0);
return sqrt(x, r);
}
}} // namespace boost::multiprecision
#endif

View file

@ -0,0 +1,834 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_LOGGED_ADAPTER_HPP
#define BOOST_MP_LOGGED_ADAPTER_HPP
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/traits/extract_exponent_type.hpp>
#include <boost/multiprecision/detail/integer_ops.hpp>
namespace boost {
namespace multiprecision {
template <class Backend>
inline void log_postfix_event(const Backend&, const char* /*event_description*/)
{
}
template <class Backend, class T>
inline void log_postfix_event(const Backend&, const T&, const char* /*event_description*/)
{
}
template <class Backend>
inline void log_prefix_event(const Backend&, const char* /*event_description*/)
{
}
template <class Backend, class T>
inline void log_prefix_event(const Backend&, const T&, const char* /*event_description*/)
{
}
template <class Backend, class T, class U>
inline void log_prefix_event(const Backend&, const T&, const U&, const char* /*event_description*/)
{
}
template <class Backend, class T, class U, class V>
inline void log_prefix_event(const Backend&, const T&, const U&, const V&, const char* /*event_description*/)
{
}
namespace backends {
template <class Backend>
struct logged_adaptor
{
using signed_types = typename Backend::signed_types ;
using unsigned_types = typename Backend::unsigned_types;
using float_types = typename Backend::float_types ;
using exponent_type = typename extract_exponent_type<Backend, number_category<Backend>::value>::type;
private:
Backend m_value;
public:
logged_adaptor()
{
log_postfix_event(m_value, "Default construct");
}
logged_adaptor(const logged_adaptor& o)
{
log_prefix_event(m_value, o.value(), "Copy construct");
m_value = o.m_value;
log_postfix_event(m_value, "Copy construct");
}
// rvalue copy
logged_adaptor(logged_adaptor&& o)
{
log_prefix_event(m_value, o.value(), "Move construct");
m_value = static_cast<Backend&&>(o.m_value);
log_postfix_event(m_value, "Move construct");
}
logged_adaptor& operator=(logged_adaptor&& o)
{
log_prefix_event(m_value, o.value(), "Move Assignment");
m_value = static_cast<Backend&&>(o.m_value);
log_postfix_event(m_value, "Move construct");
return *this;
}
logged_adaptor& operator=(const logged_adaptor& o)
{
log_prefix_event(m_value, o.value(), "Assignment");
m_value = o.m_value;
log_postfix_event(m_value, "Copy construct");
return *this;
}
template <class T>
logged_adaptor(const T& i, const typename std::enable_if<std::is_convertible<T, Backend>::value>::type* = nullptr)
: m_value(i)
{
log_postfix_event(m_value, "construct from arithmetic type");
}
template <class T>
logged_adaptor(const logged_adaptor<T>& i, const typename std::enable_if<std::is_convertible<T, Backend>::value>::type* = nullptr)
: m_value(i.value())
{
log_postfix_event(m_value, "construct from arithmetic type");
}
template <class T, class U>
logged_adaptor(const T& i, const U& j, typename std::enable_if<std::is_constructible<Backend, const T&, const U&>::value>::type* = nullptr)
: m_value(i, j)
{
log_postfix_event(m_value, "construct from a pair of arithmetic types");
}
template <class D = Backend>
logged_adaptor(const Backend& i, unsigned digits10, typename std::enable_if<std::is_constructible<D, Backend const&, unsigned>::value>::type const* = nullptr)
: m_value(i, digits10)
{
log_postfix_event(m_value, "construct from arithmetic type and precision");
}
template <class D = Backend>
logged_adaptor(const logged_adaptor<Backend>& i, unsigned digits10, typename std::enable_if<std::is_constructible<D, Backend const&, unsigned>::value>::type const* = nullptr)
: m_value(i.value(), digits10)
{
log_postfix_event(m_value, "construct from arithmetic type and precision");
}
template <class T>
typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value || std::is_assignable<Backend, T>::value, logged_adaptor&>::type operator=(const T& i)
{
log_prefix_event(m_value, i, "Assignment from arithmetic type");
m_value = i;
log_postfix_event(m_value, "Assignment from arithmetic type");
return *this;
}
logged_adaptor& operator=(const char* s)
{
log_prefix_event(m_value, s, "Assignment from string type");
m_value = s;
log_postfix_event(m_value, "Assignment from string type");
return *this;
}
void swap(logged_adaptor& o)
{
log_prefix_event(m_value, o.value(), "swap");
std::swap(m_value, o.value());
log_postfix_event(m_value, "swap");
}
std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
{
log_prefix_event(m_value, "Conversion to string");
std::string s = m_value.str(digits, f);
log_postfix_event(m_value, s, "Conversion to string");
return s;
}
void negate()
{
log_prefix_event(m_value, "negate");
m_value.negate();
log_postfix_event(m_value, "negate");
}
int compare(const logged_adaptor& o) const
{
log_prefix_event(m_value, o.value(), "compare");
int r = m_value.compare(o.value());
log_postfix_event(m_value, r, "compare");
return r;
}
template <class T>
int compare(const T& i) const
{
log_prefix_event(m_value, i, "compare");
int r = m_value.compare(i);
log_postfix_event(m_value, r, "compare");
return r;
}
Backend& value()
{
return m_value;
}
const Backend& value() const
{
return m_value;
}
#ifndef BOOST_MP_STANDALONE
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/)
{
log_prefix_event(m_value, "serialize");
ar& boost::make_nvp("value", m_value);
log_postfix_event(m_value, "serialize");
}
#endif
static unsigned default_precision() noexcept
{
return Backend::default_precision();
}
static void default_precision(unsigned v) noexcept
{
Backend::default_precision(v);
}
static unsigned thread_default_precision() noexcept
{
return Backend::thread_default_precision();
}
static void thread_default_precision(unsigned v) noexcept
{
Backend::thread_default_precision(v);
}
unsigned precision() const noexcept
{
return value().precision();
}
void precision(unsigned digits10) noexcept
{
value().precision(digits10);
}
//
// Variable precision options:
//
static constexpr variable_precision_options default_variable_precision_options()noexcept
{
return Backend::default_variable_precision_options();
}
static constexpr variable_precision_options thread_default_variable_precision_options()noexcept
{
return Backend::thread_default_variable_precision_options();
}
static BOOST_MP_CXX14_CONSTEXPR void default_variable_precision_options(variable_precision_options opts)
{
Backend::default_variable_precision_options(opts);
}
static BOOST_MP_CXX14_CONSTEXPR void thread_default_variable_precision_options(variable_precision_options opts)
{
Backend::thread_default_variable_precision_options(opts);
}
};
template <class T>
inline const T& unwrap_logged_type(const T& a) { return a; }
template <class Backend>
inline const Backend& unwrap_logged_type(const logged_adaptor<Backend>& a) { return a.value(); }
#define NON_MEMBER_OP1(name, str) \
template <class Backend> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), str); \
BOOST_JOIN(eval_, name) \
(result.value()); \
log_postfix_event(result.value(), str); \
}
#define NON_MEMBER_OP2(name, str) \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const T& a) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const logged_adaptor<Backend>& a) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a)); \
log_postfix_event(result.value(), str); \
}
#define NON_MEMBER_OP3(name, str) \
template <class Backend, class T, class U> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const T& a, const U& b) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const logged_adaptor<Backend>& a, const T& b) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const T& a, const logged_adaptor<Backend>& b) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const logged_adaptor<Backend>& a, const logged_adaptor<Backend>& b) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b)); \
log_postfix_event(result.value(), str); \
}
#define NON_MEMBER_OP4(name, str) \
template <class Backend, class T, class U, class V> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const T& a, const U& b, const V& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const logged_adaptor<Backend>& a, const logged_adaptor<Backend>& b, const T& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const logged_adaptor<Backend>& a, const T& b, const logged_adaptor<Backend>& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend, class T> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const T& a, const logged_adaptor<Backend>& b, const logged_adaptor<Backend>& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const logged_adaptor<Backend>& a, const logged_adaptor<Backend>& b, const logged_adaptor<Backend>& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c)); \
log_postfix_event(result.value(), str); \
} \
template <class Backend, class T, class U> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<Backend> & result, const logged_adaptor<Backend>& a, const T& b, const U& c) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c), str); \
BOOST_JOIN(eval_, name) \
(result.value(), unwrap_logged_type(a), unwrap_logged_type(b), unwrap_logged_type(c)); \
log_postfix_event(result.value(), str); \
}
NON_MEMBER_OP2(add, "+=")
NON_MEMBER_OP2(subtract, "-=")
NON_MEMBER_OP2(multiply, "*=")
NON_MEMBER_OP2(divide, "/=")
template <class Backend, class R>
inline void eval_convert_to(R* result, const logged_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
log_prefix_event(val.value(), "convert_to");
eval_convert_to(result, val.value());
log_postfix_event(val.value(), *result, "convert_to");
}
template <class Backend, class R>
inline void eval_convert_to(logged_adaptor<R>* result, const logged_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
log_prefix_event(val.value(), "convert_to");
eval_convert_to(&result->value(), val.value());
log_postfix_event(val.value(), &result->value(), "convert_to");
}
template <class Backend, class R>
inline void eval_convert_to(logged_adaptor<R>* result, const Backend& val)
{
using default_ops::eval_convert_to;
log_prefix_event(val, "convert_to");
eval_convert_to(&result->value(), val);
log_postfix_event(val, &result->value(), "convert_to");
}
template <class Backend>
inline void eval_convert_to(std::complex<float>* result, const logged_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
log_prefix_event(val.value(), "convert_to");
eval_convert_to(result, val.value());
log_postfix_event(val.value(), *result, "convert_to");
}
template <class Backend>
inline void eval_convert_to(std::complex<double>* result, const logged_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
log_prefix_event(val.value(), "convert_to");
eval_convert_to(result, val.value());
log_postfix_event(val.value(), *result, "convert_to");
}
template <class Backend>
inline void eval_convert_to(std::complex<long double>* result, const logged_adaptor<Backend>& val)
{
using default_ops::eval_convert_to;
log_prefix_event(val.value(), "convert_to");
eval_convert_to(result, val.value());
log_postfix_event(val.value(), *result, "convert_to");
}
template <class Backend, class Exp>
inline void eval_frexp(logged_adaptor<Backend>& result, const logged_adaptor<Backend>& arg, Exp* exp)
{
log_prefix_event(arg.value(), "frexp");
eval_frexp(result.value(), arg.value(), exp);
log_postfix_event(result.value(), *exp, "frexp");
}
template <class Backend, class Exp>
inline void eval_ldexp(logged_adaptor<Backend>& result, const logged_adaptor<Backend>& arg, Exp exp)
{
log_prefix_event(arg.value(), "ldexp");
eval_ldexp(result.value(), arg.value(), exp);
log_postfix_event(result.value(), exp, "ldexp");
}
template <class Backend, class Exp>
inline void eval_scalbn(logged_adaptor<Backend>& result, const logged_adaptor<Backend>& arg, Exp exp)
{
using default_ops::eval_scalbn;
log_prefix_event(arg.value(), "scalbn");
eval_scalbn(result.value(), arg.value(), exp);
log_postfix_event(result.value(), exp, "scalbn");
}
template <class Backend>
inline typename Backend::exponent_type eval_ilogb(const logged_adaptor<Backend>& arg)
{
using default_ops::eval_ilogb;
log_prefix_event(arg.value(), "ilogb");
typename Backend::exponent_type r = eval_ilogb(arg.value());
log_postfix_event(arg.value(), "ilogb");
return r;
}
NON_MEMBER_OP2(floor, "floor")
NON_MEMBER_OP2(ceil, "ceil")
NON_MEMBER_OP2(sqrt, "sqrt")
template <class Backend>
inline int eval_fpclassify(const logged_adaptor<Backend>& arg)
{
using default_ops::eval_fpclassify;
log_prefix_event(arg.value(), "fpclassify");
int r = eval_fpclassify(arg.value());
log_postfix_event(arg.value(), r, "fpclassify");
return r;
}
/*********************************************************************
*
* Optional arithmetic operations come next:
*
*********************************************************************/
NON_MEMBER_OP3(add, "+")
NON_MEMBER_OP3(subtract, "-")
NON_MEMBER_OP3(multiply, "*")
NON_MEMBER_OP3(divide, "/")
NON_MEMBER_OP3(multiply_add, "fused-multiply-add")
NON_MEMBER_OP3(multiply_subtract, "fused-multiply-subtract")
NON_MEMBER_OP4(multiply_add, "fused-multiply-add")
NON_MEMBER_OP4(multiply_subtract, "fused-multiply-subtract")
NON_MEMBER_OP1(increment, "increment")
NON_MEMBER_OP1(decrement, "decrement")
/*********************************************************************
*
* Optional integer operations come next:
*
*********************************************************************/
NON_MEMBER_OP2(modulus, "%=")
NON_MEMBER_OP3(modulus, "%")
NON_MEMBER_OP2(bitwise_or, "|=")
NON_MEMBER_OP3(bitwise_or, "|")
NON_MEMBER_OP2(bitwise_and, "&=")
NON_MEMBER_OP3(bitwise_and, "&")
NON_MEMBER_OP2(bitwise_xor, "^=")
NON_MEMBER_OP3(bitwise_xor, "^")
NON_MEMBER_OP4(qr, "quotient-and-remainder")
NON_MEMBER_OP2(complement, "~")
template <class Backend>
inline void eval_left_shift(logged_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_left_shift;
log_prefix_event(arg.value(), a, "<<=");
eval_left_shift(arg.value(), a);
log_postfix_event(arg.value(), "<<=");
}
template <class Backend>
inline void eval_left_shift(logged_adaptor<Backend>& arg, const logged_adaptor<Backend>& a, std::size_t b)
{
using default_ops::eval_left_shift;
log_prefix_event(arg.value(), a, b, "<<");
eval_left_shift(arg.value(), a.value(), b);
log_postfix_event(arg.value(), "<<");
}
template <class Backend>
inline void eval_right_shift(logged_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_right_shift;
log_prefix_event(arg.value(), a, ">>=");
eval_right_shift(arg.value(), a);
log_postfix_event(arg.value(), ">>=");
}
template <class Backend>
inline void eval_right_shift(logged_adaptor<Backend>& arg, const logged_adaptor<Backend>& a, std::size_t b)
{
using default_ops::eval_right_shift;
log_prefix_event(arg.value(), a, b, ">>");
eval_right_shift(arg.value(), a.value(), b);
log_postfix_event(arg.value(), ">>");
}
template <class Backend, class T>
inline T eval_integer_modulus(const logged_adaptor<Backend>& arg, const T& a)
{
using default_ops::eval_integer_modulus;
log_prefix_event(arg.value(), a, "integer-modulus");
T r = eval_integer_modulus(arg.value(), a);
log_postfix_event(arg.value(), r, "integer-modulus");
return r;
}
template <class Backend>
inline std::size_t eval_lsb(const logged_adaptor<Backend>& arg)
{
using default_ops::eval_lsb;
log_prefix_event(arg.value(), "least-significant-bit");
std::size_t r = eval_lsb(arg.value());
log_postfix_event(arg.value(), r, "least-significant-bit");
return r;
}
template <class Backend>
inline std::size_t eval_msb(const logged_adaptor<Backend>& arg)
{
using default_ops::eval_msb;
log_prefix_event(arg.value(), "most-significant-bit");
std::size_t r = eval_msb(arg.value());
log_postfix_event(arg.value(), r, "most-significant-bit");
return r;
}
template <class Backend>
inline bool eval_bit_test(const logged_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_bit_test;
log_prefix_event(arg.value(), a, "bit-test");
bool r = eval_bit_test(arg.value(), a);
log_postfix_event(arg.value(), r, "bit-test");
return r;
}
template <class Backend>
inline void eval_bit_set(const logged_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_bit_set;
log_prefix_event(arg.value(), a, "bit-set");
eval_bit_set(arg.value(), a);
log_postfix_event(arg.value(), arg, "bit-set");
}
template <class Backend>
inline void eval_bit_unset(const logged_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_bit_unset;
log_prefix_event(arg.value(), a, "bit-unset");
eval_bit_unset(arg.value(), a);
log_postfix_event(arg.value(), arg, "bit-unset");
}
template <class Backend>
inline void eval_bit_flip(const logged_adaptor<Backend>& arg, std::size_t a)
{
using default_ops::eval_bit_flip;
log_prefix_event(arg.value(), a, "bit-flip");
eval_bit_flip(arg.value(), a);
log_postfix_event(arg.value(), arg, "bit-flip");
}
NON_MEMBER_OP3(gcd, "gcd")
NON_MEMBER_OP3(lcm, "lcm")
NON_MEMBER_OP4(powm, "powm")
/*********************************************************************
*
* abs/fabs:
*
*********************************************************************/
NON_MEMBER_OP2(abs, "abs")
NON_MEMBER_OP2(fabs, "fabs")
/*********************************************************************
*
* Floating point functions:
*
*********************************************************************/
NON_MEMBER_OP2(trunc, "trunc")
NON_MEMBER_OP2(round, "round")
NON_MEMBER_OP2(exp, "exp")
NON_MEMBER_OP2(log, "log")
NON_MEMBER_OP2(log10, "log10")
NON_MEMBER_OP2(sin, "sin")
NON_MEMBER_OP2(cos, "cos")
NON_MEMBER_OP2(tan, "tan")
NON_MEMBER_OP2(asin, "asin")
NON_MEMBER_OP2(acos, "acos")
NON_MEMBER_OP2(atan, "atan")
NON_MEMBER_OP2(sinh, "sinh")
NON_MEMBER_OP2(cosh, "cosh")
NON_MEMBER_OP2(tanh, "tanh")
NON_MEMBER_OP2(logb, "logb")
NON_MEMBER_OP3(fmod, "fmod")
NON_MEMBER_OP3(pow, "pow")
NON_MEMBER_OP3(atan2, "atan2")
NON_MEMBER_OP2(asinh, "asinh")
NON_MEMBER_OP2(acosh, "acosh")
NON_MEMBER_OP2(atanh, "atanh")
NON_MEMBER_OP2(conj, "conj")
template <class Backend>
int eval_signbit(const logged_adaptor<Backend>& val)
{
using default_ops::eval_signbit;
return eval_signbit(val.value());
}
template <class Backend>
std::size_t hash_value(const logged_adaptor<Backend>& val)
{
return hash_value(val.value());
}
template <class Backend, expression_template_option ExpressionTemplates>
inline typename std::enable_if<number_category<Backend>::value == number_kind_rational, typename number<logged_adaptor<Backend>, ExpressionTemplates>::value_type>::type
numerator(const number<logged_adaptor<Backend>, ExpressionTemplates>& arg)
{
number<Backend, ExpressionTemplates> t(arg.backend().value());
return numerator(t).backend();
}
template <class Backend, expression_template_option ExpressionTemplates>
inline typename std::enable_if<number_category<Backend>::value == number_kind_rational, typename number<logged_adaptor<Backend>, ExpressionTemplates>::value_type>::type
denominator(const number<logged_adaptor<Backend>, ExpressionTemplates>& arg)
{
number<Backend, ExpressionTemplates> t(arg.backend().value());
return denominator(t).backend();
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(To& to, const logged_adaptor<From>& from)
{
using default_ops::eval_set_real;
log_prefix_event(to, from.value(), "Set real part");
eval_set_real(to, from.value());
log_postfix_event(to, from.value(), "Set real part");
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(logged_adaptor<To>& to, const logged_adaptor<From>& from)
{
using default_ops::eval_set_real;
log_prefix_event(to.value(), from.value(), "Set real part");
eval_set_real(to.value(), from.value());
log_postfix_event(to.value(), from.value(), "Set real part");
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(logged_adaptor<To>& to, const From& from)
{
using default_ops::eval_set_real;
log_prefix_event(to.value(), from, "Set real part");
eval_set_real(to.value(), from);
log_postfix_event(to.value(), from, "Set real part");
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(To& to, const logged_adaptor<From>& from)
{
using default_ops::eval_set_imag;
log_prefix_event(to, from.value(), "Set imag part");
eval_set_imag(to, from.value());
log_postfix_event(to, from.value(), "Set imag part");
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(logged_adaptor<To>& to, const logged_adaptor<From>& from)
{
using default_ops::eval_set_imag;
log_prefix_event(to.value(), from.value(), "Set imag part");
eval_set_imag(to.value(), from.value());
log_postfix_event(to.value(), from.value(), "Set imag part");
}
template <class To, class From>
inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(logged_adaptor<To>& to, const From& from)
{
using default_ops::eval_set_imag;
log_prefix_event(to.value(), from, "Set imag part");
eval_set_imag(to.value(), from);
log_postfix_event(to.value(), from, "Set imag part");
}
#define NON_MEMBER_COMPLEX_TO_REAL(name, str) \
template <class B1, class B2> \
inline void BOOST_JOIN(eval_, name)(logged_adaptor<B1> & result, const logged_adaptor<B2>& a) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(a.value(), a.value(), str); \
BOOST_JOIN(eval_, name) \
(result.value(), a.value()); \
log_postfix_event(result.value(), str); \
} \
template <class B1, class B2> \
inline void BOOST_JOIN(eval_, name)(B1 & result, const logged_adaptor<B2>& a) \
{ \
using default_ops::BOOST_JOIN(eval_, name); \
log_prefix_event(a.value(), a.value(), str); \
BOOST_JOIN(eval_, name) \
(result, a.value()); \
log_postfix_event(result, str); \
}
NON_MEMBER_COMPLEX_TO_REAL(real, "real")
NON_MEMBER_COMPLEX_TO_REAL(imag, "imag")
template <class T, class V, class U>
inline void assign_components(logged_adaptor<T>& result, const V& v1, const U& v2)
{
using default_ops::assign_components;
assign_components(result.value(), unwrap_logged_type(v1), unwrap_logged_type(v2));
}
} // namespace backends
namespace detail {
template <class Backend>
struct is_variable_precision<logged_adaptor<Backend> > : public is_variable_precision<Backend>
{};
#ifdef BOOST_HAS_INT128
template <class Backend>
struct is_convertible_arithmetic<int128_type, logged_adaptor<Backend> > : public is_convertible_arithmetic<int128_type, Backend>
{};
template <class Backend>
struct is_convertible_arithmetic<uint128_type, logged_adaptor<Backend> > : public is_convertible_arithmetic<uint128_type, Backend>
{};
#endif
#ifdef BOOST_HAS_FLOAT128
template <class Backend>
struct is_convertible_arithmetic<float128_type, logged_adaptor<Backend> > : public is_convertible_arithmetic<float128_type, Backend>
{};
#endif
} // namespace detail
template <class Backend>
struct number_category<backends::logged_adaptor<Backend> > : public number_category<Backend>
{};
template <class Backend, expression_template_option ExpressionTemplates>
struct component_type<number<logged_adaptor<Backend>, ExpressionTemplates>>
{
//
// We'll make the component_type also a logged_adaptor:
//
using base_component_type = typename component_type<number<Backend, ExpressionTemplates>>::type;
using base_component_backend = typename base_component_type::backend_type;
using type = number<logged_adaptor<base_component_backend>, ExpressionTemplates>;
};
template <class Backend>
struct is_interval_number<backends::logged_adaptor<Backend> > : public is_interval_number<Backend> {};
}} // namespace boost::multiprecision
namespace std {
template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates>
class numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::logged_adaptor<Backend>, ExpressionTemplates> >
: public std::numeric_limits<boost::multiprecision::number<Backend, ExpressionTemplates> >
{
using base_type = std::numeric_limits<boost::multiprecision::number<Backend, ExpressionTemplates> > ;
using number_type = boost::multiprecision::number<boost::multiprecision::backends::logged_adaptor<Backend>, ExpressionTemplates>;
public:
static number_type(min)() noexcept { return (base_type::min)(); }
static number_type(max)() noexcept { return (base_type::max)(); }
static number_type lowest() noexcept { return -(max)(); }
static number_type epsilon() noexcept { return base_type::epsilon(); }
static number_type round_error() noexcept { return epsilon() / 2; }
static number_type infinity() noexcept { return base_type::infinity(); }
static number_type quiet_NaN() noexcept { return base_type::quiet_NaN(); }
static number_type signaling_NaN() noexcept { return base_type::signaling_NaN(); }
static number_type denorm_min() noexcept { return base_type::denorm_min(); }
};
} // namespace std
#ifdef BOOST_MP_MATH_AVAILABLE
namespace boost {
namespace math {
namespace policies {
template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
struct precision<boost::multiprecision::number<boost::multiprecision::logged_adaptor<Backend>, ExpressionTemplates>, Policy>
: public precision<boost::multiprecision::number<Backend, ExpressionTemplates>, Policy>
{};
}
}} // namespace boost::math::policies
#endif // BOOST_MP_MATH_AVAILABLE
#undef NON_MEMBER_OP1
#undef NON_MEMBER_OP2
#undef NON_MEMBER_OP3
#undef NON_MEMBER_OP4
#endif

View file

@ -0,0 +1,221 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_MR_HPP
#define BOOST_MP_MR_HPP
#include <random>
#include <cstdint>
#include <type_traits>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/integer.hpp>
#include <boost/multiprecision/detail/uniform_int_distribution.hpp>
#include <boost/multiprecision/detail/assert.hpp>
namespace boost {
namespace multiprecision {
namespace detail {
template <class I>
bool check_small_factors(const I& n)
{
constexpr std::uint32_t small_factors1[] = {
3u, 5u, 7u, 11u, 13u, 17u, 19u, 23u};
constexpr std::uint32_t pp1 = 223092870u;
std::uint32_t m1 = integer_modulus(n, pp1);
for (std::size_t i = 0; i < sizeof(small_factors1) / sizeof(small_factors1[0]); ++i)
{
BOOST_MP_ASSERT(pp1 % small_factors1[i] == 0);
if (m1 % small_factors1[i] == 0)
return false;
}
constexpr std::uint32_t small_factors2[] = {
29u, 31u, 37u, 41u, 43u, 47u};
constexpr std::uint32_t pp2 = 2756205443u;
m1 = integer_modulus(n, pp2);
for (std::size_t i = 0; i < sizeof(small_factors2) / sizeof(small_factors2[0]); ++i)
{
BOOST_MP_ASSERT(pp2 % small_factors2[i] == 0);
if (m1 % small_factors2[i] == 0)
return false;
}
constexpr std::uint32_t small_factors3[] = {
53u, 59u, 61u, 67u, 71u};
constexpr std::uint32_t pp3 = 907383479u;
m1 = integer_modulus(n, pp3);
for (std::size_t i = 0; i < sizeof(small_factors3) / sizeof(small_factors3[0]); ++i)
{
BOOST_MP_ASSERT(pp3 % small_factors3[i] == 0);
if (m1 % small_factors3[i] == 0)
return false;
}
constexpr std::uint32_t small_factors4[] = {
73u, 79u, 83u, 89u, 97u};
constexpr std::uint32_t pp4 = 4132280413u;
m1 = integer_modulus(n, pp4);
for (std::size_t i = 0; i < sizeof(small_factors4) / sizeof(small_factors4[0]); ++i)
{
BOOST_MP_ASSERT(pp4 % small_factors4[i] == 0);
if (m1 % small_factors4[i] == 0)
return false;
}
constexpr std::uint32_t small_factors5[6][4] = {
{101u, 103u, 107u, 109u},
{113u, 127u, 131u, 137u},
{139u, 149u, 151u, 157u},
{163u, 167u, 173u, 179u},
{181u, 191u, 193u, 197u},
{199u, 211u, 223u, 227u}};
constexpr std::uint32_t pp5[6] =
{
121330189u,
113u * 127u * 131u * 137u,
139u * 149u * 151u * 157u,
163u * 167u * 173u * 179u,
181u * 191u * 193u * 197u,
199u * 211u * 223u * 227u};
for (std::size_t k = 0; k < sizeof(pp5) / sizeof(*pp5); ++k)
{
m1 = integer_modulus(n, pp5[k]);
for (std::size_t i = 0; i < 4; ++i)
{
BOOST_MP_ASSERT(pp5[k] % small_factors5[k][i] == 0);
if (m1 % small_factors5[k][i] == 0)
return false;
}
}
return true;
}
inline bool is_small_prime(std::size_t n)
{
constexpr unsigned char p[] =
{
3u, 5u, 7u, 11u, 13u, 17u, 19u, 23u, 29u, 31u,
37u, 41u, 43u, 47u, 53u, 59u, 61u, 67u, 71u, 73u,
79u, 83u, 89u, 97u, 101u, 103u, 107u, 109u, 113u,
127u, 131u, 137u, 139u, 149u, 151u, 157u, 163u,
167u, 173u, 179u, 181u, 191u, 193u, 197u, 199u,
211u, 223u, 227u};
for (std::size_t i = 0; i < sizeof(p) / sizeof(*p); ++i)
{
if (n == p[i])
return true;
}
return false;
}
template <class I>
typename std::enable_if<std::is_convertible<I, unsigned>::value, unsigned>::type
cast_to_unsigned(const I& val)
{
return static_cast<unsigned>(val);
}
template <class I>
typename std::enable_if<!std::is_convertible<I, unsigned>::value, unsigned>::type
cast_to_unsigned(const I& val)
{
return val.template convert_to<unsigned>();
}
} // namespace detail
template <class I, class Engine>
typename std::enable_if<number_category<I>::value == number_kind_integer, bool>::type
miller_rabin_test(const I& n, std::size_t trials, Engine& gen)
{
using number_type = I;
if (n == 2)
return true; // Trivial special case.
if (bit_test(n, 0) == 0)
return false; // n is even
if (n <= 227)
return detail::is_small_prime(detail::cast_to_unsigned(n));
if (!detail::check_small_factors(n))
return false;
number_type nm1 = n - 1;
//
// Begin with a single Fermat test - it excludes a lot of candidates:
//
number_type q(228), x, y; // We know n is greater than this, as we've excluded small factors
x = powm(q, nm1, n);
if (x != 1u)
return false;
q = n - 1;
std::size_t k = lsb(q);
q >>= k;
// Declare our random number generator:
boost::multiprecision::uniform_int_distribution<number_type> dist(2, n - 2);
//
// Execute the trials:
//
for (std::size_t i = 0; i < trials; ++i)
{
x = dist(gen);
y = powm(x, q, n);
std::size_t j = 0;
while (true)
{
if (y == nm1)
break;
if (y == 1)
{
if (j == 0)
break;
return false; // test failed
}
if (++j == k)
return false; // failed
y = powm(y, 2, n);
}
}
return true; // Yeheh! probably prime.
}
template <class I>
typename std::enable_if<number_category<I>::value == number_kind_integer, bool>::type
miller_rabin_test(const I& x, std::size_t trials)
{
static std::mt19937 gen;
return miller_rabin_test(x, trials, gen);
}
template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class Engine>
bool miller_rabin_test(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& n, std::size_t trials, Engine& gen)
{
using number_type = typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type;
return miller_rabin_test(number_type(n), trials, gen);
}
template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
bool miller_rabin_test(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& n, std::size_t trials)
{
using number_type = typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type;
return miller_rabin_test(number_type(n), trials);
}
}} // namespace boost::multiprecision
#endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,23 @@
///////////////////////////////////////////////////////////////
// Copyright Jens Maurer 2006-2011
// Copyright Steven Watanabe 2011
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_RANDOM_HPP
#define BOOST_MP_RANDOM_HPP
#include <boost/multiprecision/detail/standalone_config.hpp>
#if defined(__GNUC__) || defined(_MSC_VER)
#pragma message("NOTE: Use of this header (boost/multiprecision/random.hpp) is deprecated: please use the random number library headers directly.")
#endif
#ifndef BOOST_MP_STANDALONE
#include <boost/random.hpp>
#else
#error "Use of this header is removed in standalone mode"
#endif
#endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,67 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Vicente J. Botet Escriba 2009-2011
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_EXPLICIT_CONVERSION_HPP
#define BOOST_MP_EXPLICIT_CONVERSION_HPP
#include <type_traits>
#include <boost/multiprecision/detail/standalone_config.hpp>
#include <boost/multiprecision/detail/number_base.hpp> // number_category
namespace boost {
namespace multiprecision {
namespace detail {
template <unsigned int N>
struct dummy_size
{};
template <typename S, typename T>
struct has_generic_interconversion
{
using type = typename std::conditional<
is_number<S>::value && is_number<T>::value,
typename std::conditional<
number_category<S>::value == number_kind_integer,
typename std::conditional<
number_category<T>::value == number_kind_integer || number_category<T>::value == number_kind_floating_point || number_category<T>::value == number_kind_rational || number_category<T>::value == number_kind_fixed_point,
std::true_type,
std::false_type >::type,
typename std::conditional<
number_category<S>::value == number_kind_rational,
typename std::conditional<
number_category<T>::value == number_kind_rational || number_category<T>::value == number_kind_rational,
std::true_type,
std::false_type >::type,
typename std::conditional<
number_category<T>::value == number_kind_floating_point,
std::true_type,
std::false_type >::type>::type>::type,
std::false_type >::type;
};
template <typename S, typename T>
struct is_explicitly_convertible_imp
{
template <typename S1, typename T1>
static int selector(dummy_size<static_cast<unsigned int>(sizeof(new T1(std::declval<S1>())))>*);
template <typename S1, typename T1>
static char selector(...);
static constexpr bool value = sizeof(selector<S, T>(nullptr)) == sizeof(int);
using type = std::integral_constant<bool, value>;
};
template <typename From, typename To>
struct is_explicitly_convertible : public is_explicitly_convertible_imp<From, To>::type
{
};
}}} // namespace boost::multiprecision::detail
#endif

View file

@ -0,0 +1,28 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_EXTRACT_EXPONENT_TYPE_HPP
#define BOOST_MP_EXTRACT_EXPONENT_TYPE_HPP
#include <boost/multiprecision/number.hpp>
namespace boost {
namespace multiprecision {
namespace backends {
template <class Backend, int cat>
struct extract_exponent_type
{
using type = int;
};
template <class Backend>
struct extract_exponent_type<Backend, number_kind_floating_point>
{
using type = typename Backend::exponent_type;
};
}}} // namespace boost::multiprecision::backends
#endif

View file

@ -0,0 +1,91 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2015 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_IS_BACKEND_HPP
#define BOOST_MP_IS_BACKEND_HPP
#include <type_traits>
#include <boost/multiprecision/detail/number_base.hpp>
namespace boost { namespace multiprecision { namespace detail {
template <class T>
struct has_signed_types
{
template <class U>
static double check(U*, typename U::signed_types* = nullptr);
static char check(...);
static T* get();
static constexpr bool value = sizeof(check(get())) == sizeof(double);
};
template <class T>
struct has_unsigned_types
{
template <class U>
static double check(U*, typename U::unsigned_types* = nullptr);
static char check(...);
static T* get();
static constexpr bool value = sizeof(check(get())) == sizeof(double);
};
template <class T>
struct has_float_types
{
template <class U>
static double check(U*, typename U::float_types* = nullptr);
static char check(...);
static T* get();
static constexpr bool value = sizeof(check(get())) == sizeof(double);
};
template <class T>
struct is_backend : public std::integral_constant<bool, has_signed_types<T>::value && has_unsigned_types<T>::value && has_float_types<T>::value> {};
template <class Backend>
struct other_backend
{
using type = typename std::conditional<
std::is_same<number<Backend>, number<Backend, et_on> >::value,
number<Backend, et_off>, number<Backend, et_on> >::type;
};
template <class B, class V>
struct number_from_backend
{
using type = typename std::conditional<
std::is_convertible<V, number<B> >::value,
number<B>,
typename other_backend<B>::type>::type;
};
template <bool b, class T, class U>
struct is_first_backend_imp : public std::false_type {};
template <class T, class U>
struct is_first_backend_imp<true, T, U> : public std::integral_constant < bool, std::is_convertible<U, number<T, et_on> >::value || std::is_convertible<U, number<T, et_off> >::value> {};
template <class T, class U>
struct is_first_backend : is_first_backend_imp<is_backend<T>::value, T, U>
{};
template <bool b, class T, class U>
struct is_second_backend_imp
{
static constexpr bool value = false;
};
template <class T, class U>
struct is_second_backend_imp<true, T, U>
{
static constexpr bool value = (std::is_convertible<T, number<U, et_on> >::value || std::is_convertible<T, number<U, et_off> >::value) && !is_first_backend<T, U>::value;
};
template <class T, class U>
struct is_second_backend : is_second_backend_imp<is_backend<U>::value, T, U>
{};
}
}
} // namespace boost::multiprecision::detail
#endif // BOOST_MP_IS_BACKEND_HPP

View file

@ -0,0 +1,51 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2015 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_IS_BYTE_CONTAINER_HPP
#define BOOST_IS_BYTE_CONTAINER_HPP
#include <iterator>
#include <type_traits>
namespace boost { namespace multiprecision { namespace detail {
template <class T>
struct has_member_const_iterator
{
template <class U>
static double check(U*, typename U::const_iterator* = nullptr);
static char check(...);
static T* get();
static constexpr bool value = sizeof(check(get())) == sizeof(double);
};
template <class C, class Iterator>
struct is_byte_container_imp_2
{
using container_value_type = typename std::remove_cv<typename std::iterator_traits<typename C::const_iterator>::value_type>::type;
static constexpr bool value = boost::multiprecision::detail::is_integral<container_value_type>::value && (sizeof(container_value_type) == 1);
};
template <class C>
struct is_byte_container_imp_2<C, void> : public std::false_type
{};
template <class C, bool b>
struct is_byte_container_imp : public is_byte_container_imp_2<C, typename C::const_iterator>
{
};
template <class C>
struct is_byte_container_imp<C, false> : public std::false_type
{};
template <class C>
struct is_byte_container : public is_byte_container_imp<C, has_member_const_iterator<C>::value>
{};
}}} // namespace boost::multiprecision::detail
#endif // BOOST_IS_BYTE_CONTAINER_HPP

View file

@ -0,0 +1,22 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2018 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_IS_COMPLEX_HPP
#define BOOST_MP_IS_COMPLEX_HPP
#include <type_traits>
#include <complex>
namespace boost { namespace multiprecision { namespace detail {
template <class T> struct is_complex : public std::integral_constant<bool, false> {};
template <class T> struct is_complex<std::complex<T> > : public std::integral_constant<bool, true> {};
}
}
} // namespace boost::multiprecision::detail
#endif // BOOST_MP_IS_BACKEND_HPP

View file

@ -0,0 +1,51 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2021 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_IS_CONVERTIBLE_ARITHMETIC_HPP
#define BOOST_IS_CONVERTIBLE_ARITHMETIC_HPP
#include <type_traits>
#include <boost/multiprecision/detail/number_base.hpp>
#include <boost/multiprecision/detail/standalone_config.hpp>
namespace boost { namespace multiprecision { namespace detail {
template <class V, class Backend>
struct is_convertible_arithmetic
{
static constexpr bool value = boost::multiprecision::detail::is_arithmetic<V>::value;
};
//
// For extension types, we don't *require* interoperability,
// so only enable it if we can convert the type to the backend
// losslessly, ie not via conversion to a narrower type.
// Note that backends with templated constructors/=operators
// will not be selected here, so these need to either specialize
// this trait, or provide a proper non-template constructor/=operator
// for the extension types it supports.
//
#ifdef BOOST_HAS_FLOAT128
template <class Backend>
struct is_convertible_arithmetic<float128_type, Backend>
{
static constexpr bool value = std::is_assignable<Backend, convertible_to<float128_type>>::value;
};
#endif
#ifdef BOOST_HAS_INT128
template <class Backend>
struct is_convertible_arithmetic<int128_type, Backend>
{
static constexpr bool value = std::is_assignable<Backend, convertible_to<int128_type>>::value;
};
template <class Backend>
struct is_convertible_arithmetic<uint128_type, Backend>
{
static constexpr bool value = std::is_assignable<Backend, convertible_to<uint128_type>>::value;
};
#endif
}}} // namespace boost::multiprecision::detail
#endif // BOOST_IS_BYTE_CONTAINER_HPP

View file

@ -0,0 +1,47 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Vicente J. Botet Escriba 2009-2011
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_IS_RESTRICTED_CONVERSION_HPP
#define BOOST_MP_IS_RESTRICTED_CONVERSION_HPP
#include <boost/multiprecision/traits/explicit_conversion.hpp>
#include <boost/multiprecision/detail/number_base.hpp>
namespace boost { namespace multiprecision { namespace detail {
template <class From, class To>
struct is_lossy_conversion
{
static constexpr bool category_conditional_is_true =
( (static_cast<boost::multiprecision::number_category_type>(number_category<From>::value) == number_kind_floating_point)
&& (static_cast<boost::multiprecision::number_category_type>(number_category<To >::value) == number_kind_integer))
|| ( (static_cast<boost::multiprecision::number_category_type>(number_category<From>::value) == number_kind_rational)
&& (static_cast<boost::multiprecision::number_category_type>(number_category<To >::value) == number_kind_integer))
|| ( (static_cast<boost::multiprecision::number_category_type>(number_category<From>::value) == number_kind_fixed_point)
&& (static_cast<boost::multiprecision::number_category_type>(number_category<To >::value) == number_kind_integer))
|| (static_cast<boost::multiprecision::number_category_type>(number_category<From>::value) == number_kind_unknown)
|| (static_cast<boost::multiprecision::number_category_type>(number_category<To >::value) == number_kind_unknown);
using type = typename std::conditional<category_conditional_is_true,
std::integral_constant<bool, true>,
std::integral_constant<bool, false>>::type;
static constexpr bool value = type::value;
};
template <typename From, typename To>
struct is_restricted_conversion
{
using type = typename std::conditional<
((is_explicitly_convertible<From, To>::value && !std::is_convertible<From, To>::value) || is_lossy_conversion<From, To>::value),
std::integral_constant<bool, true>,
std::integral_constant<bool, false>>::type;
static constexpr const bool value = type::value;
};
}}} // namespace boost::multiprecision::detail
#endif // BOOST_MP_IS_RESTRICTED_CONVERSION_HPP

View file

@ -0,0 +1,25 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2018 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_IS_VARIABLE_PRECISION_HPP
#define BOOST_MP_IS_VARIABLE_PRECISION_HPP
#include <boost/multiprecision/detail/number_base.hpp>
namespace boost { namespace multiprecision { namespace detail {
template <class Backend>
struct is_variable_precision : public std::integral_constant<bool, false>
{};
template <class Backend, expression_template_option ExpressionTemplates>
struct is_variable_precision<number<Backend, ExpressionTemplates> > : public is_variable_precision<Backend>
{};
}
}
} // namespace boost::multiprecision::detail
#endif // BOOST_MP_IS_BACKEND_HPP

View file

@ -0,0 +1,79 @@
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_MAX_DIGITS10_HPP
#define BOOST_MP_MAX_DIGITS10_HPP
namespace boost {
namespace multiprecision {
namespace detail {
template <unsigned digits>
struct calc_max_digits10
{
static constexpr unsigned max_digits_10(unsigned d)
{
//
// We need ceil(log10(2) * d) + 1 decimal places to
// guarantee round tripping, see: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
// and references therein. Since log10(2) is irrational, then d*log10(2) will
// never be exactly an integer so we can replace by trunc(log10(2) * d) + 2
// and avoid the call to ceil:
//
return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * d) + 2;
}
static constexpr unsigned value = max_digits_10(digits);
};
template <std::size_t digits>
struct calc_max_digits10_s
{
static constexpr std::size_t max_digits_10(std::size_t d)
{
//
// We need ceil(log10(2) * d) + 1 decimal places to
// guarantee round tripping, see: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
// and references therein. Since log10(2) is irrational, then d*log10(2) will
// never be exactly an integer so we can replace by trunc(log10(2) * d) + 2
// and avoid the call to ceil:
//
return static_cast<std::size_t>(static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d)) + 2u);
}
static constexpr std::size_t value = max_digits_10(digits);
};
template <unsigned digits>
struct calc_digits10
{
static constexpr unsigned digits_10(unsigned d)
{
//
// We need floor(log10(2) * (d-1)), see:
// https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
// and references therein.
//
return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d - 1u));
}
static constexpr unsigned value = digits_10(digits);
};
template <std::size_t digits>
struct calc_digits10_s
{
static constexpr std::size_t digits_10(std::size_t d)
{
//
// We need floor(log10(2) * (d-1)), see:
// https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
// and references therein.
//
return static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d - 1u));
}
static constexpr std::size_t value = digits_10(digits);
};
}}} // namespace boost::multiprecision::detail
#endif

View file

@ -0,0 +1,95 @@
///////////////////////////////////////////////////////////////
// Copyright 2012-2022 John Maddock.
// Copyright 2022 Matt Borland. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
#ifndef BOOST_MP_STD_INTEGER_TRAITS_HPP
#define BOOST_MP_STD_INTEGER_TRAITS_HPP
#include <type_traits>
#include <boost/multiprecision/detail/standalone_config.hpp>
namespace boost {
namespace multiprecision {
namespace detail {
template <class T>
struct is_signed : public std::is_signed<T> {};
template <class T>
struct is_unsigned : public std::is_unsigned<T> {};
template <class T>
struct is_integral : public std::is_integral<T> {};
template <class T>
struct is_arithmetic : public std::is_arithmetic<T> {};
template <class T>
struct make_unsigned : public std::make_unsigned<T> {};
template <class T>
struct make_signed : public std::make_signed<T> {};
#if defined(__SIZEOF_FLOAT128__)
template <>
struct is_arithmetic<__float128> : public std::true_type {};
#endif
#ifdef BOOST_HAS_INT128
template <>
struct is_signed<int128_type> : public std::true_type {};
template <>
struct is_signed<uint128_type> : public std::false_type {};
template <>
struct is_unsigned<int128_type> : public std::false_type {};
template <>
struct is_unsigned<uint128_type> : public std::true_type {};
template <>
struct is_integral<int128_type> : public std::true_type {};
template <>
struct is_integral<uint128_type> : public std::true_type {};
template <>
struct is_arithmetic<int128_type> : public std::true_type {};
template <>
struct is_arithmetic<uint128_type> : public std::true_type {};
template <>
struct make_unsigned<int128_type>
{
using type = uint128_type;
};
template <>
struct make_unsigned<uint128_type>
{
using type = uint128_type;
};
template <>
struct make_signed<int128_type>
{
using type = int128_type;
};
template <>
struct make_signed<uint128_type>
{
using type = int128_type;
};
#endif
// C++17-esque helpers
#if defined(__cpp_variable_templates) && __cpp_variable_templates >= 201304L
template <typename T>
BOOST_INLINE_CONSTEXPR bool is_signed_v = is_signed<T>::value;
template <typename T>
BOOST_INLINE_CONSTEXPR bool is_unsigned_v = is_unsigned<T>::value;
template <typename T>
BOOST_INLINE_CONSTEXPR bool is_integral_v = is_integral<T>::value;
template <typename T>
BOOST_INLINE_CONSTEXPR bool is_arithmetic_v = is_arithmetic<T>::value;
#endif
template <typename T>
using make_unsigned_t = typename make_unsigned<T>::type;
template <typename T>
using make_signed_t = typename make_signed<T>::type;
}}} // namespace boost::multiprecision::detail
#endif

View file

@ -0,0 +1,21 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2020 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MP_TRANSCENDENTAL_REDUCTION_TYPE_HPP
#define BOOST_MP_TRANSCENDENTAL_REDUCTION_TYPE_HPP
namespace boost { namespace multiprecision { namespace detail {
template <class T>
struct transcendental_reduction_type
{
using type = T;
};
}
}
} // namespace boost::multiprecision::detail
#endif // BOOST_MP_TRANSCENDENTAL_REDUCTION_TYPE_HPP