/////////////////////////////////////////////////////////////////////////////// // 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 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator-(const number& v) { static_assert(is_signed_number::value, "Negating an unsigned type results in ill-defined behavior."); detail::scoped_default_precision > precision_guard(v); number result(v); result.backend().negate(); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator~(const number& v) { detail::scoped_default_precision > precision_guard(v); number result; eval_complement(result.backend(), v.backend()); return result; } // // Addition: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator+(const number& a, const number& b) { detail::scoped_default_precision > precision_guard(a, b); number result; using default_ops::eval_add; eval_add(result.backend(), a.backend(), b.backend()); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value, number >::type operator+(const number& a, const V& b) { detail::scoped_default_precision > precision_guard(a); number result; using default_ops::eval_add; eval_add(result.backend(), a.backend(), number::canonical_value(b)); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && !is_equivalent_number_type::value, number >::type operator+(const V& a, const number& b) { detail::scoped_default_precision > precision_guard(b); number result; using default_ops::eval_add; eval_add(result.backend(), b.backend(), number::canonical_value(a)); return result; } // // Subtraction: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator-(const number& a, const number& b) { detail::scoped_default_precision > precision_guard(a, b); number result; using default_ops::eval_subtract; eval_subtract(result.backend(), a.backend(), b.backend()); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value, number >::type operator-(const number& a, const V& b) { detail::scoped_default_precision > precision_guard(a); number result; using default_ops::eval_subtract; eval_subtract(result.backend(), a.backend(), number::canonical_value(b)); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && !is_equivalent_number_type::value, number >::type operator-(const V& a, const number& b) { detail::scoped_default_precision > precision_guard(b); number result; using default_ops::eval_subtract; eval_subtract(result.backend(), number::canonical_value(a), b.backend()); return result; } // // Multiply: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator*(const number& a, const number& b) { detail::scoped_default_precision > precision_guard(a, b); number result; using default_ops::eval_multiply; eval_multiply(result.backend(), a.backend(), b.backend()); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value, number >::type operator*(const number& a, const V& b) { detail::scoped_default_precision > precision_guard(a); number result; using default_ops::eval_multiply; eval_multiply(result.backend(), a.backend(), number::canonical_value(b)); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && !is_equivalent_number_type::value, number >::type operator*(const V& a, const number& b) { detail::scoped_default_precision > precision_guard(b); number result; using default_ops::eval_multiply; eval_multiply(result.backend(), b.backend(), number::canonical_value(a)); return result; } // // divide: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator/(const number& a, const number& b) { detail::scoped_default_precision > precision_guard(a, b); number result; using default_ops::eval_divide; eval_divide(result.backend(), a.backend(), b.backend()); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value, number >::type operator/(const number& a, const V& b) { detail::scoped_default_precision > precision_guard(a); number result; using default_ops::eval_divide; eval_divide(result.backend(), a.backend(), number::canonical_value(b)); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && !is_equivalent_number_type::value, number >::type operator/(const V& a, const number& b) { detail::scoped_default_precision > precision_guard(b); number result; using default_ops::eval_divide; eval_divide(result.backend(), number::canonical_value(a), b.backend()); return result; } // // modulus: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator%(const number& a, const number& b) { detail::scoped_default_precision > precision_guard(a, b); number result; using default_ops::eval_modulus; eval_modulus(result.backend(), a.backend(), b.backend()); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer), number >::type operator%(const number& a, const V& b) { detail::scoped_default_precision > precision_guard(a); number result; using default_ops::eval_modulus; eval_modulus(result.backend(), a.backend(), number::canonical_value(b)); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer) && !is_equivalent_number_type::value, number >::type operator%(const V& a, const number& b) { detail::scoped_default_precision > precision_guard(b); number result; using default_ops::eval_modulus; eval_modulus(result.backend(), number::canonical_value(a), b.backend()); return result; } // // Bitwise or: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator|(const number& a, const number& b) { number result; using default_ops::eval_bitwise_or; eval_bitwise_or(result.backend(), a.backend(), b.backend()); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer), number >::type operator|(const number& a, const V& b) { number result; using default_ops::eval_bitwise_or; eval_bitwise_or(result.backend(), a.backend(), number::canonical_value(b)); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer) && !is_equivalent_number_type::value, number >::type operator|(const V& a, const number& b) { number result; using default_ops::eval_bitwise_or; eval_bitwise_or(result.backend(), b.backend(), number::canonical_value(a)); return result; } // // Bitwise xor: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator^(const number& a, const number& b) { number result; using default_ops::eval_bitwise_xor; eval_bitwise_xor(result.backend(), a.backend(), b.backend()); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer), number >::type operator^(const number& a, const V& b) { number result; using default_ops::eval_bitwise_xor; eval_bitwise_xor(result.backend(), a.backend(), number::canonical_value(b)); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer) && !is_equivalent_number_type::value, number >::type operator^(const V& a, const number& b) { number result; using default_ops::eval_bitwise_xor; eval_bitwise_xor(result.backend(), b.backend(), number::canonical_value(a)); return result; } // // Bitwise and: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator&(const number& a, const number& b) { number result; using default_ops::eval_bitwise_and; eval_bitwise_and(result.backend(), a.backend(), b.backend()); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer), number >::type operator&(const number& a, const V& b) { number result; using default_ops::eval_bitwise_and; eval_bitwise_and(result.backend(), a.backend(), number::canonical_value(b)); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer) && !is_equivalent_number_type::value, number >::type operator&(const V& a, const number& b) { number result; using default_ops::eval_bitwise_and; eval_bitwise_and(result.backend(), b.backend(), number::canonical_value(a)); return result; } // // shifts: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value && (number_category::value == number_kind_integer), number >::type operator<<(const number& a, const I& b) { number result(a); using default_ops::eval_left_shift; detail::check_shift_range(b, std::integral_constant sizeof(std::size_t))>(), std::integral_constant::value>()); eval_left_shift(result.backend(), b); return result; } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value && (number_category::value == number_kind_integer), number >::type operator>>(const number& a, const I& b) { number result(a); using default_ops::eval_right_shift; detail::check_shift_range(b, std::integral_constant sizeof(std::size_t))>(), std::integral_constant::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 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator-(number&& v) { static_assert(is_signed_number::value, "Negating an unsigned type results in ill-defined behavior."); v.backend().negate(); return static_cast&&>(v); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator~(number&& v) { eval_complement(v.backend(), v.backend()); return static_cast&&>(v); } // // Addition: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator+(number&& a, const number& b) { using default_ops::eval_add; detail::scoped_default_precision > precision_guard(a, b); eval_add(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator+(const number& a, number&& b) { using default_ops::eval_add; detail::scoped_default_precision > precision_guard(a, b); eval_add(b.backend(), a.backend()); return static_cast&&>(b); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator+(number&& a, number&& b) { using default_ops::eval_add; detail::scoped_default_precision > precision_guard(a, b); eval_add(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value, number >::type operator+(number&& a, const V& b) { using default_ops::eval_add; detail::scoped_default_precision > precision_guard(a, b); eval_add(a.backend(), number::canonical_value(b)); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && !is_equivalent_number_type::value, number >::type operator+(const V& a, number&& b) { using default_ops::eval_add; detail::scoped_default_precision > precision_guard(a, b); eval_add(b.backend(), number::canonical_value(a)); return static_cast&&>(b); } // // Subtraction: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator-(number&& a, const number& b) { using default_ops::eval_subtract; detail::scoped_default_precision > precision_guard(a, b); eval_subtract(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value, number >::type operator-(const number& a, number&& b) { using default_ops::eval_subtract; detail::scoped_default_precision > precision_guard(a, b); eval_subtract(b.backend(), a.backend()); b.backend().negate(); return static_cast&&>(b); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator-(number&& a, number&& b) { using default_ops::eval_subtract; detail::scoped_default_precision > precision_guard(a, b); eval_subtract(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value, number >::type operator-(number&& a, const V& b) { using default_ops::eval_subtract; detail::scoped_default_precision > precision_guard(a, b); eval_subtract(a.backend(), number::canonical_value(b)); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(is_compatible_arithmetic_type >::value && is_signed_number::value) && !is_equivalent_number_type::value, number >::type operator-(const V& a, number&& b) { using default_ops::eval_subtract; detail::scoped_default_precision > precision_guard(a, b); eval_subtract(b.backend(), number::canonical_value(a)); b.backend().negate(); return static_cast&&>(b); } // // Multiply: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator*(number&& a, const number& b) { using default_ops::eval_multiply; detail::scoped_default_precision > precision_guard(a, b); eval_multiply(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator*(const number& a, number&& b) { using default_ops::eval_multiply; detail::scoped_default_precision > precision_guard(a, b); eval_multiply(b.backend(), a.backend()); return static_cast&&>(b); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator*(number&& a, number&& b) { using default_ops::eval_multiply; detail::scoped_default_precision > precision_guard(a, b); eval_multiply(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value, number >::type operator*(number&& a, const V& b) { using default_ops::eval_multiply; detail::scoped_default_precision > precision_guard(a, b); eval_multiply(a.backend(), number::canonical_value(b)); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && !is_equivalent_number_type::value, number >::type operator*(const V& a, number&& b) { using default_ops::eval_multiply; detail::scoped_default_precision > precision_guard(a, b); eval_multiply(b.backend(), number::canonical_value(a)); return static_cast&&>(b); } // // divide: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number operator/(number&& a, const number& b) { using default_ops::eval_divide; detail::scoped_default_precision > precision_guard(a, b); eval_divide(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value, number >::type operator/(number&& a, const V& b) { using default_ops::eval_divide; detail::scoped_default_precision > precision_guard(a, b); eval_divide(a.backend(), number::canonical_value(b)); return static_cast&&>(a); } // // modulus: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator%(number&& a, const number& b) { using default_ops::eval_modulus; detail::scoped_default_precision > precision_guard(a, b); eval_modulus(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer), number >::type operator%(number&& a, const V& b) { using default_ops::eval_modulus; detail::scoped_default_precision > precision_guard(a, b); eval_modulus(a.backend(), number::canonical_value(b)); return static_cast&&>(a); } // // Bitwise or: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator|(number&& a, const number& b) { using default_ops::eval_bitwise_or; eval_bitwise_or(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator|(const number& a, number&& b) { using default_ops::eval_bitwise_or; eval_bitwise_or(b.backend(), a.backend()); return static_cast&&>(b); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator|(number&& a, number&& b) { using default_ops::eval_bitwise_or; eval_bitwise_or(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer), number >::type operator|(number&& a, const V& b) { using default_ops::eval_bitwise_or; eval_bitwise_or(a.backend(), number::canonical_value(b)); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer) && !is_equivalent_number_type::value, number >::type operator|(const V& a, number&& b) { using default_ops::eval_bitwise_or; eval_bitwise_or(b.backend(), number::canonical_value(a)); return static_cast&&>(b); } // // Bitwise xor: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator^(number&& a, const number& b) { using default_ops::eval_bitwise_xor; eval_bitwise_xor(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator^(const number& a, number&& b) { using default_ops::eval_bitwise_xor; eval_bitwise_xor(b.backend(), a.backend()); return static_cast&&>(b); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator^(number&& a, number&& b) { using default_ops::eval_bitwise_xor; eval_bitwise_xor(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer), number >::type operator^(number&& a, const V& b) { using default_ops::eval_bitwise_xor; eval_bitwise_xor(a.backend(), number::canonical_value(b)); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer) && !is_equivalent_number_type::value, number >::type operator^(const V& a, number&& b) { using default_ops::eval_bitwise_xor; eval_bitwise_xor(b.backend(), number::canonical_value(a)); return static_cast&&>(b); } // // Bitwise and: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator&(number&& a, const number& b) { using default_ops::eval_bitwise_and; eval_bitwise_and(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator&(const number& a, number&& b) { using default_ops::eval_bitwise_and; eval_bitwise_and(b.backend(), a.backend()); return static_cast&&>(b); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value == number_kind_integer, number >::type operator&(number&& a, number&& b) { using default_ops::eval_bitwise_and; eval_bitwise_and(a.backend(), b.backend()); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer), number >::type operator&(number&& a, const V& b) { using default_ops::eval_bitwise_and; eval_bitwise_and(a.backend(), number::canonical_value(b)); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if >::value && (number_category::value == number_kind_integer) && !is_equivalent_number_type::value, number >::type operator&(const V& a, number&& b) { using default_ops::eval_bitwise_and; eval_bitwise_and(b.backend(), number::canonical_value(a)); return static_cast&&>(b); } // // shifts: // template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value && (number_category::value == number_kind_integer), number >::type operator<<(number&& a, const I& b) { using default_ops::eval_left_shift; eval_left_shift(a.backend(), b); return static_cast&&>(a); } template BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if::value && (number_category::value == number_kind_integer), number >::type operator>>(number&& a, const I& b) { using default_ops::eval_right_shift; eval_right_shift(a.backend(), b); return static_cast&&>(a); } } } // namespace boost::multiprecision #ifdef BOOST_MSVC #pragma warning(pop) #endif #endif // BOOST_MP_NO_ET_OPS_HPP