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