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