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