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