/*============================================================================= Copyright (c) 2011-2017 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout 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 SPROUT_INTEGER_LIMITED_HPP #define SPROUT_INTEGER_LIMITED_HPP #include #include #include namespace sprout { namespace limited { // // plus // template inline SPROUT_CONSTEXPR typename sprout::arithmetic_promote::type plus(T const& lhs, U const& rhs) { typedef typename sprout::arithmetic_promote::type type; return lhs > 0 && rhs > 0 && sprout::numeric_limits::max() - lhs < rhs ? sprout::numeric_limits::max() : lhs < 0 && rhs < 0 && sprout::numeric_limits::min() - lhs > rhs ? sprout::numeric_limits::min() : lhs + rhs ; } // // multiplies // template inline SPROUT_CONSTEXPR typename sprout::arithmetic_promote::type multiplies(T const& lhs, U const& rhs) { typedef typename sprout::arithmetic_promote::type type; return lhs > 0 && rhs > 0 && sprout::numeric_limits::max() / lhs + (sprout::numeric_limits::max() % lhs ? 1 : 0) <= rhs ? sprout::numeric_limits::max() : lhs > 0 && rhs < 0 && sprout::numeric_limits::min() / rhs + (sprout::numeric_limits::min() % rhs ? 1 : 0) <= lhs ? sprout::numeric_limits::min() : lhs < 0 && rhs > 0 && sprout::numeric_limits::min() / lhs + (sprout::numeric_limits::min() % lhs ? 1 : 0) <= rhs ? sprout::numeric_limits::min() : lhs < 0 && rhs < 0 && -(sprout::numeric_limits::min() / rhs + (sprout::numeric_limits::min() % rhs ? 1 : 0)) >= lhs ? sprout::numeric_limits::max() : lhs * rhs ; } } // namespace limited } // namespace sprout #endif // #ifndef SPROUT_INTEGER_LIMITED_HPP