// The MIT License (MIT) // // Copyright (c) 2018 Mateusz Pusz // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #pragma once #include "hacks.h" #include #include #include namespace units { namespace detail { template [[nodiscard]] constexpr T abs(T v) noexcept { return v < 0 ? -v : v; } } template struct ratio { static_assert(Den != 0, "zero denominator"); static_assert(-INTMAX_MAX <= Num, "numerator too negative"); static_assert(-INTMAX_MAX <= Den, "denominator too negative"); static constexpr std::intmax_t num = Num * (Den < 0 ? -1 : 1) / std::gcd(Num, Den); static constexpr std::intmax_t den = detail::abs(Den) / std::gcd(Num, Den); using type = ratio; }; // is_ratio namespace detail { template inline constexpr bool is_ratio = false; template inline constexpr bool is_ratio> = true; } // namespace detail template concept Ratio = detail::is_ratio; // ratio_multiply namespace detail { static constexpr std::intmax_t safe_multiply(std::intmax_t lhs, std::intmax_t rhs) { constexpr std::uintmax_t c = std::uintmax_t(1) << (sizeof(std::intmax_t) * 4); const std::uintmax_t a0 = detail::abs(lhs) % c; const std::uintmax_t a1 = detail::abs(lhs) / c; const std::uintmax_t b0 = detail::abs(rhs) % c; const std::uintmax_t b1 = detail::abs(rhs) / c; Expects(a1 == 0 || b1 == 0); // overflow in multiplication Expects(a0 * b1 + b0 * a1 < (c >> 1)); // overflow in multiplication Expects(b0 * a0 <= INTMAX_MAX); // overflow in multiplication Expects((a0 * b1 + b0 * a1) * c <= INTMAX_MAX - b0 * a0); // overflow in multiplication return lhs * rhs; } template struct ratio_multiply_impl { private: static constexpr std::intmax_t gcd1 = std::gcd(R1::num, R2::den); static constexpr std::intmax_t gcd2 = std::gcd(R2::num, R1::den); public: using type = ratio; static constexpr std::intmax_t num = type::num; static constexpr std::intmax_t den = type::den; }; } template using ratio_multiply = detail::ratio_multiply_impl::type; // ratio_divide namespace detail { template struct ratio_divide_impl { static_assert(R2::num != 0, "division by 0"); using type = ratio_multiply>; static constexpr std::intmax_t num = type::num; static constexpr std::intmax_t den = type::den; }; } template using ratio_divide = detail::ratio_divide_impl::type; // common_ratio namespace detail { template struct common_ratio_impl { static constexpr std::intmax_t gcd_num = std::gcd(R1::num, R2::num); static constexpr std::intmax_t gcd_den = std::gcd(R1::den, R2::den); using type = ratio; }; } template using common_ratio = detail::common_ratio_impl::type; } // namespace units