Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- ratio -----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_RATIO |
| 11 | #define _LIBCPP_RATIO |
| 12 | |
| 13 | /* |
| 14 | ratio synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | template <intmax_t N, intmax_t D = 1> |
| 20 | class ratio |
| 21 | { |
| 22 | public: |
Marshall Clow | 0735e4e | 2015-04-16 21:36:54 +0000 | [diff] [blame] | 23 | static constexpr intmax_t num; |
| 24 | static constexpr intmax_t den; |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 25 | typedef ratio<num, den> type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | // ratio arithmetic |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 29 | template <class R1, class R2> using ratio_add = ...; |
| 30 | template <class R1, class R2> using ratio_subtract = ...; |
| 31 | template <class R1, class R2> using ratio_multiply = ...; |
| 32 | template <class R1, class R2> using ratio_divide = ...; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | |
| 34 | // ratio comparison |
| 35 | template <class R1, class R2> struct ratio_equal; |
| 36 | template <class R1, class R2> struct ratio_not_equal; |
| 37 | template <class R1, class R2> struct ratio_less; |
| 38 | template <class R1, class R2> struct ratio_less_equal; |
| 39 | template <class R1, class R2> struct ratio_greater; |
| 40 | template <class R1, class R2> struct ratio_greater_equal; |
| 41 | |
| 42 | // convenience SI typedefs |
| 43 | typedef ratio<1, 1000000000000000000000000> yocto; // not supported |
| 44 | typedef ratio<1, 1000000000000000000000> zepto; // not supported |
| 45 | typedef ratio<1, 1000000000000000000> atto; |
| 46 | typedef ratio<1, 1000000000000000> femto; |
| 47 | typedef ratio<1, 1000000000000> pico; |
| 48 | typedef ratio<1, 1000000000> nano; |
| 49 | typedef ratio<1, 1000000> micro; |
| 50 | typedef ratio<1, 1000> milli; |
| 51 | typedef ratio<1, 100> centi; |
| 52 | typedef ratio<1, 10> deci; |
| 53 | typedef ratio< 10, 1> deca; |
| 54 | typedef ratio< 100, 1> hecto; |
| 55 | typedef ratio< 1000, 1> kilo; |
| 56 | typedef ratio< 1000000, 1> mega; |
| 57 | typedef ratio< 1000000000, 1> giga; |
| 58 | typedef ratio< 1000000000000, 1> tera; |
| 59 | typedef ratio< 1000000000000000, 1> peta; |
| 60 | typedef ratio< 1000000000000000000, 1> exa; |
| 61 | typedef ratio< 1000000000000000000000, 1> zetta; // not supported |
| 62 | typedef ratio<1000000000000000000000000, 1> yotta; // not supported |
| 63 | |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 64 | // 20.11.5, ratio comparison |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 65 | template <class R1, class R2> inline constexpr bool ratio_equal_v |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 66 | = ratio_equal<R1, R2>::value; // C++17 |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 67 | template <class R1, class R2> inline constexpr bool ratio_not_equal_v |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 68 | = ratio_not_equal<R1, R2>::value; // C++17 |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 69 | template <class R1, class R2> inline constexpr bool ratio_less_v |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 70 | = ratio_less<R1, R2>::value; // C++17 |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 71 | template <class R1, class R2> inline constexpr bool ratio_less_equal_v |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 72 | = ratio_less_equal<R1, R2>::value; // C++17 |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 73 | template <class R1, class R2> inline constexpr bool ratio_greater_v |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 74 | = ratio_greater<R1, R2>::value; // C++17 |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 75 | template <class R1, class R2> inline constexpr bool ratio_greater_equal_v |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 76 | = ratio_greater_equal<R1, R2>::value; // C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | } |
| 78 | */ |
| 79 | |
| 80 | #include <__config> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | #include <climits> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 82 | #include <cstdint> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 83 | #include <type_traits> |
| 84 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 85 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 86 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 87 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 89 | _LIBCPP_PUSH_MACROS |
| 90 | #include <__undef_macros> |
| 91 | |
| 92 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 94 | |
| 95 | // __static_gcd |
| 96 | |
| 97 | template <intmax_t _Xp, intmax_t _Yp> |
| 98 | struct __static_gcd |
| 99 | { |
| 100 | static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value; |
| 101 | }; |
| 102 | |
| 103 | template <intmax_t _Xp> |
| 104 | struct __static_gcd<_Xp, 0> |
| 105 | { |
| 106 | static const intmax_t value = _Xp; |
| 107 | }; |
| 108 | |
Howard Hinnant | cbf51aa | 2011-11-01 23:13:37 +0000 | [diff] [blame] | 109 | template <> |
| 110 | struct __static_gcd<0, 0> |
| 111 | { |
| 112 | static const intmax_t value = 1; |
| 113 | }; |
| 114 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | // __static_lcm |
| 116 | |
| 117 | template <intmax_t _Xp, intmax_t _Yp> |
| 118 | struct __static_lcm |
| 119 | { |
| 120 | static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp; |
| 121 | }; |
| 122 | |
| 123 | template <intmax_t _Xp> |
| 124 | struct __static_abs |
| 125 | { |
| 126 | static const intmax_t value = _Xp < 0 ? -_Xp : _Xp; |
| 127 | }; |
| 128 | |
| 129 | template <intmax_t _Xp> |
| 130 | struct __static_sign |
| 131 | { |
| 132 | static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1); |
| 133 | }; |
| 134 | |
| 135 | template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value> |
| 136 | class __ll_add; |
| 137 | |
| 138 | template <intmax_t _Xp, intmax_t _Yp> |
| 139 | class __ll_add<_Xp, _Yp, 1> |
| 140 | { |
| 141 | static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1; |
| 142 | static const intmax_t max = -min; |
| 143 | |
| 144 | static_assert(_Xp <= max - _Yp, "overflow in __ll_add"); |
| 145 | public: |
| 146 | static const intmax_t value = _Xp + _Yp; |
| 147 | }; |
| 148 | |
| 149 | template <intmax_t _Xp, intmax_t _Yp> |
| 150 | class __ll_add<_Xp, _Yp, 0> |
| 151 | { |
| 152 | public: |
| 153 | static const intmax_t value = _Xp; |
| 154 | }; |
| 155 | |
| 156 | template <intmax_t _Xp, intmax_t _Yp> |
| 157 | class __ll_add<_Xp, _Yp, -1> |
| 158 | { |
| 159 | static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1; |
| 160 | static const intmax_t max = -min; |
| 161 | |
| 162 | static_assert(min - _Yp <= _Xp, "overflow in __ll_add"); |
| 163 | public: |
| 164 | static const intmax_t value = _Xp + _Yp; |
| 165 | }; |
| 166 | |
| 167 | template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value> |
| 168 | class __ll_sub; |
| 169 | |
| 170 | template <intmax_t _Xp, intmax_t _Yp> |
| 171 | class __ll_sub<_Xp, _Yp, 1> |
| 172 | { |
| 173 | static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1; |
| 174 | static const intmax_t max = -min; |
| 175 | |
| 176 | static_assert(min + _Yp <= _Xp, "overflow in __ll_sub"); |
| 177 | public: |
| 178 | static const intmax_t value = _Xp - _Yp; |
| 179 | }; |
| 180 | |
| 181 | template <intmax_t _Xp, intmax_t _Yp> |
| 182 | class __ll_sub<_Xp, _Yp, 0> |
| 183 | { |
| 184 | public: |
| 185 | static const intmax_t value = _Xp; |
| 186 | }; |
| 187 | |
| 188 | template <intmax_t _Xp, intmax_t _Yp> |
| 189 | class __ll_sub<_Xp, _Yp, -1> |
| 190 | { |
| 191 | static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1; |
| 192 | static const intmax_t max = -min; |
| 193 | |
| 194 | static_assert(_Xp <= max + _Yp, "overflow in __ll_sub"); |
| 195 | public: |
| 196 | static const intmax_t value = _Xp - _Yp; |
| 197 | }; |
| 198 | |
| 199 | template <intmax_t _Xp, intmax_t _Yp> |
| 200 | class __ll_mul |
| 201 | { |
| 202 | static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)); |
| 203 | static const intmax_t min = nan + 1; |
| 204 | static const intmax_t max = -min; |
| 205 | static const intmax_t __a_x = __static_abs<_Xp>::value; |
| 206 | static const intmax_t __a_y = __static_abs<_Yp>::value; |
| 207 | |
| 208 | static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul"); |
| 209 | public: |
| 210 | static const intmax_t value = _Xp * _Yp; |
| 211 | }; |
| 212 | |
| 213 | template <intmax_t _Yp> |
| 214 | class __ll_mul<0, _Yp> |
| 215 | { |
| 216 | public: |
| 217 | static const intmax_t value = 0; |
| 218 | }; |
| 219 | |
| 220 | template <intmax_t _Xp> |
| 221 | class __ll_mul<_Xp, 0> |
| 222 | { |
| 223 | public: |
| 224 | static const intmax_t value = 0; |
| 225 | }; |
| 226 | |
| 227 | template <> |
| 228 | class __ll_mul<0, 0> |
| 229 | { |
| 230 | public: |
| 231 | static const intmax_t value = 0; |
| 232 | }; |
| 233 | |
| 234 | // Not actually used but left here in case needed in future maintenance |
| 235 | template <intmax_t _Xp, intmax_t _Yp> |
| 236 | class __ll_div |
| 237 | { |
| 238 | static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)); |
| 239 | static const intmax_t min = nan + 1; |
| 240 | static const intmax_t max = -min; |
| 241 | |
| 242 | static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div"); |
| 243 | public: |
| 244 | static const intmax_t value = _Xp / _Yp; |
| 245 | }; |
| 246 | |
| 247 | template <intmax_t _Num, intmax_t _Den = 1> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 248 | class _LIBCPP_TEMPLATE_VIS ratio |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | { |
| 250 | static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range"); |
| 251 | static_assert(_Den != 0, "ratio divide by 0"); |
| 252 | static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range"); |
Marshall Clow | 0735e4e | 2015-04-16 21:36:54 +0000 | [diff] [blame] | 253 | static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value; |
| 254 | static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value; |
| 255 | static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value; |
| 256 | static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | public: |
Marshall Clow | 0735e4e | 2015-04-16 21:36:54 +0000 | [diff] [blame] | 258 | static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd; |
| 259 | static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 260 | |
| 261 | typedef ratio<num, den> type; |
| 262 | }; |
| 263 | |
Eric Fiselier | bd716a4 | 2015-05-20 03:15:01 +0000 | [diff] [blame] | 264 | template <intmax_t _Num, intmax_t _Den> |
| 265 | _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num; |
| 266 | |
| 267 | template <intmax_t _Num, intmax_t _Den> |
| 268 | _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 269 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 270 | template <class _Tp> struct __is_ratio : false_type {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 271 | template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {}; |
| 272 | |
| 273 | typedef ratio<1LL, 1000000000000000000LL> atto; |
| 274 | typedef ratio<1LL, 1000000000000000LL> femto; |
| 275 | typedef ratio<1LL, 1000000000000LL> pico; |
| 276 | typedef ratio<1LL, 1000000000LL> nano; |
| 277 | typedef ratio<1LL, 1000000LL> micro; |
| 278 | typedef ratio<1LL, 1000LL> milli; |
| 279 | typedef ratio<1LL, 100LL> centi; |
| 280 | typedef ratio<1LL, 10LL> deci; |
| 281 | typedef ratio< 10LL, 1LL> deca; |
| 282 | typedef ratio< 100LL, 1LL> hecto; |
| 283 | typedef ratio< 1000LL, 1LL> kilo; |
| 284 | typedef ratio< 1000000LL, 1LL> mega; |
| 285 | typedef ratio< 1000000000LL, 1LL> giga; |
| 286 | typedef ratio< 1000000000000LL, 1LL> tera; |
| 287 | typedef ratio< 1000000000000000LL, 1LL> peta; |
| 288 | typedef ratio<1000000000000000000LL, 1LL> exa; |
| 289 | |
| 290 | template <class _R1, class _R2> |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 291 | struct __ratio_multiply |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 292 | { |
| 293 | private: |
| 294 | static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value; |
| 295 | static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value; |
| 296 | public: |
| 297 | typedef typename ratio |
| 298 | < |
| 299 | __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value, |
| 300 | __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value |
| 301 | >::type type; |
| 302 | }; |
| 303 | |
Eric Fiselier | c6f17cc | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 304 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 305 | |
| 306 | template <class _R1, class _R2> using ratio_multiply |
| 307 | = typename __ratio_multiply<_R1, _R2>::type; |
| 308 | |
Eric Fiselier | c6f17cc | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 309 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 310 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 311 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 312 | struct _LIBCPP_TEMPLATE_VIS ratio_multiply |
Howard Hinnant | e179d8f | 2010-11-28 19:41:07 +0000 | [diff] [blame] | 313 | : public __ratio_multiply<_R1, _R2>::type {}; |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 314 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 315 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 316 | |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 317 | template <class _R1, class _R2> |
| 318 | struct __ratio_divide |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 319 | { |
| 320 | private: |
| 321 | static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value; |
| 322 | static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value; |
| 323 | public: |
| 324 | typedef typename ratio |
| 325 | < |
| 326 | __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value, |
| 327 | __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value |
| 328 | >::type type; |
| 329 | }; |
| 330 | |
Eric Fiselier | c6f17cc | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 331 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 332 | |
| 333 | template <class _R1, class _R2> using ratio_divide |
| 334 | = typename __ratio_divide<_R1, _R2>::type; |
| 335 | |
Eric Fiselier | c6f17cc | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 336 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 337 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 338 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 339 | struct _LIBCPP_TEMPLATE_VIS ratio_divide |
Howard Hinnant | e179d8f | 2010-11-28 19:41:07 +0000 | [diff] [blame] | 340 | : public __ratio_divide<_R1, _R2>::type {}; |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 341 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 342 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 343 | |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 344 | template <class _R1, class _R2> |
| 345 | struct __ratio_add |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 346 | { |
| 347 | private: |
| 348 | static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value; |
| 349 | static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value; |
| 350 | public: |
| 351 | typedef typename ratio_multiply |
| 352 | < |
| 353 | ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>, |
| 354 | ratio |
| 355 | < |
| 356 | __ll_add |
| 357 | < |
| 358 | __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value, |
| 359 | __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value |
| 360 | >::value, |
| 361 | _R2::den |
| 362 | > |
| 363 | >::type type; |
| 364 | }; |
| 365 | |
Eric Fiselier | c6f17cc | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 366 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 367 | |
| 368 | template <class _R1, class _R2> using ratio_add |
| 369 | = typename __ratio_add<_R1, _R2>::type; |
| 370 | |
Eric Fiselier | c6f17cc | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 371 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 372 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 373 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 374 | struct _LIBCPP_TEMPLATE_VIS ratio_add |
Howard Hinnant | e179d8f | 2010-11-28 19:41:07 +0000 | [diff] [blame] | 375 | : public __ratio_add<_R1, _R2>::type {}; |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 376 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 377 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 378 | |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 379 | template <class _R1, class _R2> |
| 380 | struct __ratio_subtract |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 381 | { |
| 382 | private: |
| 383 | static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value; |
| 384 | static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value; |
| 385 | public: |
| 386 | typedef typename ratio_multiply |
| 387 | < |
| 388 | ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>, |
| 389 | ratio |
| 390 | < |
| 391 | __ll_sub |
| 392 | < |
| 393 | __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value, |
| 394 | __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value |
| 395 | >::value, |
| 396 | _R2::den |
| 397 | > |
| 398 | >::type type; |
| 399 | }; |
| 400 | |
Eric Fiselier | c6f17cc | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 401 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 402 | |
| 403 | template <class _R1, class _R2> using ratio_subtract |
| 404 | = typename __ratio_subtract<_R1, _R2>::type; |
| 405 | |
Eric Fiselier | c6f17cc | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 406 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 407 | |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 408 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 409 | struct _LIBCPP_TEMPLATE_VIS ratio_subtract |
Howard Hinnant | e179d8f | 2010-11-28 19:41:07 +0000 | [diff] [blame] | 410 | : public __ratio_subtract<_R1, _R2>::type {}; |
Howard Hinnant | bc2b5ac | 2010-11-24 17:05:06 +0000 | [diff] [blame] | 411 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 412 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 0b8647d | 2011-05-31 16:55:36 +0000 | [diff] [blame] | 413 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 414 | // ratio_equal |
| 415 | |
| 416 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 417 | struct _LIBCPP_TEMPLATE_VIS ratio_equal |
Marshall Clow | 5460b36 | 2015-05-18 23:21:06 +0000 | [diff] [blame] | 418 | : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 419 | |
| 420 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 421 | struct _LIBCPP_TEMPLATE_VIS ratio_not_equal |
Marshall Clow | 5460b36 | 2015-05-18 23:21:06 +0000 | [diff] [blame] | 422 | : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 423 | |
| 424 | // ratio_less |
| 425 | |
| 426 | template <class _R1, class _R2, bool _Odd = false, |
| 427 | intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den, |
| 428 | intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den> |
| 429 | struct __ratio_less1 |
| 430 | { |
| 431 | static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2; |
| 432 | }; |
| 433 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 434 | template <class _R1, class _R2, bool _Odd, intmax_t _Qp> |
| 435 | struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 436 | { |
| 437 | static const bool value = false; |
| 438 | }; |
| 439 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 440 | template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2> |
| 441 | struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 442 | { |
| 443 | static const bool value = !_Odd; |
| 444 | }; |
| 445 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 446 | template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1> |
| 447 | struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 448 | { |
| 449 | static const bool value = _Odd; |
| 450 | }; |
| 451 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 452 | template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 453 | intmax_t _M2> |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 454 | struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 455 | { |
| 456 | static const bool value = __ratio_less1<ratio<_R1::den, _M1>, |
| 457 | ratio<_R2::den, _M2>, !_Odd>::value; |
| 458 | }; |
| 459 | |
| 460 | template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value, |
| 461 | intmax_t _S2 = __static_sign<_R2::num>::value> |
| 462 | struct __ratio_less |
| 463 | { |
| 464 | static const bool value = _S1 < _S2; |
| 465 | }; |
| 466 | |
| 467 | template <class _R1, class _R2> |
| 468 | struct __ratio_less<_R1, _R2, 1LL, 1LL> |
| 469 | { |
| 470 | static const bool value = __ratio_less1<_R1, _R2>::value; |
| 471 | }; |
| 472 | |
| 473 | template <class _R1, class _R2> |
| 474 | struct __ratio_less<_R1, _R2, -1LL, -1LL> |
| 475 | { |
| 476 | static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value; |
| 477 | }; |
| 478 | |
| 479 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 480 | struct _LIBCPP_TEMPLATE_VIS ratio_less |
Marshall Clow | 5460b36 | 2015-05-18 23:21:06 +0000 | [diff] [blame] | 481 | : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 482 | |
| 483 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 484 | struct _LIBCPP_TEMPLATE_VIS ratio_less_equal |
Marshall Clow | 5460b36 | 2015-05-18 23:21:06 +0000 | [diff] [blame] | 485 | : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 486 | |
| 487 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 488 | struct _LIBCPP_TEMPLATE_VIS ratio_greater |
Marshall Clow | 5460b36 | 2015-05-18 23:21:06 +0000 | [diff] [blame] | 489 | : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 490 | |
| 491 | template <class _R1, class _R2> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 492 | struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal |
Marshall Clow | 5460b36 | 2015-05-18 23:21:06 +0000 | [diff] [blame] | 493 | : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 494 | |
| 495 | template <class _R1, class _R2> |
| 496 | struct __ratio_gcd |
| 497 | { |
| 498 | typedef ratio<__static_gcd<_R1::num, _R2::num>::value, |
| 499 | __static_lcm<_R1::den, _R2::den>::value> type; |
| 500 | }; |
| 501 | |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 502 | #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES) |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 503 | template <class _R1, class _R2> |
Louis Dionne | 559be10 | 2021-09-22 09:35:32 -0400 | [diff] [blame^] | 504 | inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value; |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 505 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 506 | template <class _R1, class _R2> |
Louis Dionne | 559be10 | 2021-09-22 09:35:32 -0400 | [diff] [blame^] | 507 | inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value; |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 508 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 509 | template <class _R1, class _R2> |
Louis Dionne | 559be10 | 2021-09-22 09:35:32 -0400 | [diff] [blame^] | 510 | inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value; |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 511 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 512 | template <class _R1, class _R2> |
Louis Dionne | 559be10 | 2021-09-22 09:35:32 -0400 | [diff] [blame^] | 513 | inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value; |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 514 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 515 | template <class _R1, class _R2> |
Louis Dionne | 559be10 | 2021-09-22 09:35:32 -0400 | [diff] [blame^] | 516 | inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value; |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 517 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 518 | template <class _R1, class _R2> |
Louis Dionne | 559be10 | 2021-09-22 09:35:32 -0400 | [diff] [blame^] | 519 | inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value; |
Marshall Clow | cec5d83 | 2015-11-30 05:04:48 +0000 | [diff] [blame] | 520 | #endif |
| 521 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 522 | _LIBCPP_END_NAMESPACE_STD |
| 523 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 524 | _LIBCPP_POP_MACROS |
| 525 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 526 | #endif // _LIBCPP_RATIO |