/////////////////////////////////////////////////////////////////////////////// // 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_PRECISION_HPP #define BOOST_MP_PRECISION_HPP #include #include #include namespace boost { namespace multiprecision { namespace detail { template inline constexpr unsigned current_precision_of_last_chance_imp(const boost::multiprecision::number&, const std::integral_constant&) { return std::numeric_limits >::digits10; } template inline BOOST_MP_CXX14_CONSTEXPR unsigned current_precision_of_last_chance_imp(const boost::multiprecision::number& val, const std::integral_constant&) { // // 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 val.is_zero() ? 1 : 1 + digits2_2_10(msb(abs(val)) - lsb(abs(val)) + 1); } template inline BOOST_MP_CXX14_CONSTEXPR unsigned current_precision_of_last_chance_imp(const boost::multiprecision::number& val, const std::integral_constant&) { // // 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()), current_precision_of_last_chance_imp(denominator(val), std::integral_constant())); } template inline BOOST_MP_CXX14_CONSTEXPR unsigned current_precision_of_imp(const boost::multiprecision::number& n, const std::integral_constant&) { return n.precision(); } template inline constexpr unsigned current_precision_of_imp(const boost::multiprecision::number& val, const std::integral_constant&) { using tag = std::integral_constant >::is_specialized && std::numeric_limits >::is_integer && std::numeric_limits >::is_exact && !std::numeric_limits >::is_modulo ? 1 : boost::multiprecision::number_category >::value == boost::multiprecision::number_kind_rational ? 2 : 0>; return current_precision_of_last_chance_imp(val, tag()); } template 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::min_exponent ? std::numeric_limits::digits10 : 1 + std::numeric_limits::digits10) : 0; } template inline constexpr unsigned current_precision_of(const Terminal& r) { return current_precision_of_terminal(R::canonical_value(r)); } template 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::digits10 : 0; } template inline constexpr unsigned current_precision_of(const Terminal (&)[N]) { // For string literals: return 0; } template inline constexpr unsigned current_precision_of_imp(const boost::multiprecision::number& n, const std::true_type&) { return std::is_same >::value || (std::is_same >::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 >()) : 0; } template inline constexpr unsigned current_precision_of_imp(const boost::multiprecision::number& n, const std::false_type&) { return std::is_same >::value || std::is_same >::value ? current_precision_of_imp(n, boost::multiprecision::detail::is_variable_precision >()) : 0; } template inline constexpr unsigned current_precision_of(const boost::multiprecision::number& n) { return current_precision_of_imp(n, boost::multiprecision::detail::is_variable_precision()); } template inline constexpr unsigned current_precision_of(const expression& expr) { return current_precision_of(expr.left_ref()); } template inline constexpr unsigned current_precision_of(const expression& expr) { return current_precision_of(expr.value()); } template inline constexpr unsigned current_precision_of(const expression& expr) { return (std::max)(current_precision_of(expr.left_ref()), current_precision_of(expr.right_ref())); } template inline constexpr unsigned current_precision_of(const expression& expr) { return (std::max)((std::max)(current_precision_of(expr.left_ref()), current_precision_of(expr.right_ref())), current_precision_of(expr.middle_ref())); } #ifdef BOOST_MSVC #pragma warning(push) #pragma warning(disable : 4130) #endif template ::value> struct scoped_default_precision { template constexpr scoped_default_precision(const T&) {} template constexpr scoped_default_precision(const T&, const U&) {} template 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_ASSERT("This function should never be called!!" == 0); return 0; } }; #ifdef BOOST_MSVC #pragma warning(pop) #endif template struct scoped_default_precision { template 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(a))); } template 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(a), current_precision_of(b)))); } template 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(a), current_precision_of(b)), (std::max)(R::thread_default_precision(), current_precision_of(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 inline BOOST_MP_CXX14_CONSTEXPR void maybe_promote_precision(T*, const std::integral_constant&) {} template inline BOOST_MP_CXX14_CONSTEXPR void maybe_promote_precision(T* obj, const std::integral_constant&) { if (obj->precision() != T::thread_default_precision()) { obj->precision(T::thread_default_precision()); } } template inline BOOST_MP_CXX14_CONSTEXPR void maybe_promote_precision(T* obj) { maybe_promote_precision(obj, std::integral_constant::value>()); } #ifndef BOOST_NO_CXX17_IF_CONSTEXPR #define BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(T) \ if \ constexpr(boost::multiprecision::detail::is_variable_precision::value) #else #define BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(T) if (boost::multiprecision::detail::is_variable_precision::value) #endif template ::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 struct scoped_target_precision {}; template ::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 struct scoped_source_precision {}; template ::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 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 struct scoped_precision_options { scoped_precision_options(unsigned) {} scoped_precision_options(unsigned, variable_precision_options) {} template scoped_precision_options(const U&) {} ~scoped_precision_options() {} }; } } } // namespace boost::multiprecision::detail #endif // BOOST_MP_IS_BACKEND_HPP