1// -*- C++ -*- 2//===---------------------------- ratio -----------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_RATIO 12#define _LIBCPP_RATIO 13 14/* 15 ratio synopsis 16 17namespace std 18{ 19 20template <intmax_t N, intmax_t D = 1> 21class ratio 22{ 23public: 24 static const intmax_t num; 25 static const intmax_t den; 26 typedef ratio<num, den> type; 27}; 28 29// ratio arithmetic 30template <class R1, class R2> using ratio_add = ...; 31template <class R1, class R2> using ratio_subtract = ...; 32template <class R1, class R2> using ratio_multiply = ...; 33template <class R1, class R2> using ratio_divide = ...; 34 35// ratio comparison 36template <class R1, class R2> struct ratio_equal; 37template <class R1, class R2> struct ratio_not_equal; 38template <class R1, class R2> struct ratio_less; 39template <class R1, class R2> struct ratio_less_equal; 40template <class R1, class R2> struct ratio_greater; 41template <class R1, class R2> struct ratio_greater_equal; 42 43// convenience SI typedefs 44typedef ratio<1, 1000000000000000000000000> yocto; // not supported 45typedef ratio<1, 1000000000000000000000> zepto; // not supported 46typedef ratio<1, 1000000000000000000> atto; 47typedef ratio<1, 1000000000000000> femto; 48typedef ratio<1, 1000000000000> pico; 49typedef ratio<1, 1000000000> nano; 50typedef ratio<1, 1000000> micro; 51typedef ratio<1, 1000> milli; 52typedef ratio<1, 100> centi; 53typedef ratio<1, 10> deci; 54typedef ratio< 10, 1> deca; 55typedef ratio< 100, 1> hecto; 56typedef ratio< 1000, 1> kilo; 57typedef ratio< 1000000, 1> mega; 58typedef ratio< 1000000000, 1> giga; 59typedef ratio< 1000000000000, 1> tera; 60typedef ratio< 1000000000000000, 1> peta; 61typedef ratio< 1000000000000000000, 1> exa; 62typedef ratio< 1000000000000000000000, 1> zetta; // not supported 63typedef 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#include <__undef_min_max> 74 75#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 76#pragma GCC system_header 77#endif 78 79_LIBCPP_BEGIN_NAMESPACE_STD 80 81// __static_gcd 82 83template <intmax_t _Xp, intmax_t _Yp> 84struct __static_gcd 85{ 86 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value; 87}; 88 89template <intmax_t _Xp> 90struct __static_gcd<_Xp, 0> 91{ 92 static const intmax_t value = _Xp; 93}; 94 95template <> 96struct __static_gcd<0, 0> 97{ 98 static const intmax_t value = 1; 99}; 100 101// __static_lcm 102 103template <intmax_t _Xp, intmax_t _Yp> 104struct __static_lcm 105{ 106 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp; 107}; 108 109template <intmax_t _Xp> 110struct __static_abs 111{ 112 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp; 113}; 114 115template <intmax_t _Xp> 116struct __static_sign 117{ 118 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1); 119}; 120 121template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value> 122class __ll_add; 123 124template <intmax_t _Xp, intmax_t _Yp> 125class __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"); 131public: 132 static const intmax_t value = _Xp + _Yp; 133}; 134 135template <intmax_t _Xp, intmax_t _Yp> 136class __ll_add<_Xp, _Yp, 0> 137{ 138public: 139 static const intmax_t value = _Xp; 140}; 141 142template <intmax_t _Xp, intmax_t _Yp> 143class __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"); 149public: 150 static const intmax_t value = _Xp + _Yp; 151}; 152 153template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value> 154class __ll_sub; 155 156template <intmax_t _Xp, intmax_t _Yp> 157class __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"); 163public: 164 static const intmax_t value = _Xp - _Yp; 165}; 166 167template <intmax_t _Xp, intmax_t _Yp> 168class __ll_sub<_Xp, _Yp, 0> 169{ 170public: 171 static const intmax_t value = _Xp; 172}; 173 174template <intmax_t _Xp, intmax_t _Yp> 175class __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"); 181public: 182 static const intmax_t value = _Xp - _Yp; 183}; 184 185template <intmax_t _Xp, intmax_t _Yp> 186class __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"); 195public: 196 static const intmax_t value = _Xp * _Yp; 197}; 198 199template <intmax_t _Yp> 200class __ll_mul<0, _Yp> 201{ 202public: 203 static const intmax_t value = 0; 204}; 205 206template <intmax_t _Xp> 207class __ll_mul<_Xp, 0> 208{ 209public: 210 static const intmax_t value = 0; 211}; 212 213template <> 214class __ll_mul<0, 0> 215{ 216public: 217 static const intmax_t value = 0; 218}; 219 220// Not actually used but left here in case needed in future maintenance 221template <intmax_t _Xp, intmax_t _Yp> 222class __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"); 229public: 230 static const intmax_t value = _Xp / _Yp; 231}; 232 233template <intmax_t _Num, intmax_t _Den = 1> 234class _LIBCPP_TYPE_VIS_ONLY ratio 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; 243public: 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 250template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::num; 251template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::den; 252 253template <class _Tp> struct __is_ratio : false_type {}; 254template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {}; 255 256typedef ratio<1LL, 1000000000000000000LL> atto; 257typedef ratio<1LL, 1000000000000000LL> femto; 258typedef ratio<1LL, 1000000000000LL> pico; 259typedef ratio<1LL, 1000000000LL> nano; 260typedef ratio<1LL, 1000000LL> micro; 261typedef ratio<1LL, 1000LL> milli; 262typedef ratio<1LL, 100LL> centi; 263typedef ratio<1LL, 10LL> deci; 264typedef ratio< 10LL, 1LL> deca; 265typedef ratio< 100LL, 1LL> hecto; 266typedef ratio< 1000LL, 1LL> kilo; 267typedef ratio< 1000000LL, 1LL> mega; 268typedef ratio< 1000000000LL, 1LL> giga; 269typedef ratio< 1000000000000LL, 1LL> tera; 270typedef ratio< 1000000000000000LL, 1LL> peta; 271typedef ratio<1000000000000000000LL, 1LL> exa; 272 273template <class _R1, class _R2> 274struct __ratio_multiply 275{ 276private: 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; 279public: 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 287#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES 288 289template <class _R1, class _R2> using ratio_multiply 290 = typename __ratio_multiply<_R1, _R2>::type; 291 292#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES 293 294template <class _R1, class _R2> 295struct _LIBCPP_TYPE_VIS_ONLY ratio_multiply 296 : public __ratio_multiply<_R1, _R2>::type {}; 297 298#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES 299 300template <class _R1, class _R2> 301struct __ratio_divide 302{ 303private: 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; 306public: 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 314#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES 315 316template <class _R1, class _R2> using ratio_divide 317 = typename __ratio_divide<_R1, _R2>::type; 318 319#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES 320 321template <class _R1, class _R2> 322struct _LIBCPP_TYPE_VIS_ONLY ratio_divide 323 : public __ratio_divide<_R1, _R2>::type {}; 324 325#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES 326 327template <class _R1, class _R2> 328struct __ratio_add 329{ 330private: 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; 333public: 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 349#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES 350 351template <class _R1, class _R2> using ratio_add 352 = typename __ratio_add<_R1, _R2>::type; 353 354#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES 355 356template <class _R1, class _R2> 357struct _LIBCPP_TYPE_VIS_ONLY ratio_add 358 : public __ratio_add<_R1, _R2>::type {}; 359 360#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES 361 362template <class _R1, class _R2> 363struct __ratio_subtract 364{ 365private: 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; 368public: 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 384#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES 385 386template <class _R1, class _R2> using ratio_subtract 387 = typename __ratio_subtract<_R1, _R2>::type; 388 389#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES 390 391template <class _R1, class _R2> 392struct _LIBCPP_TYPE_VIS_ONLY ratio_subtract 393 : public __ratio_subtract<_R1, _R2>::type {}; 394 395#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES 396 397// ratio_equal 398 399template <class _R1, class _R2> 400struct _LIBCPP_TYPE_VIS_ONLY ratio_equal 401 : public integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den> {}; 402 403template <class _R1, class _R2> 404struct _LIBCPP_TYPE_VIS_ONLY ratio_not_equal 405 : public integral_constant<bool, !ratio_equal<_R1, _R2>::value> {}; 406 407// ratio_less 408 409template <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> 412struct __ratio_less1 413{ 414 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2; 415}; 416 417template <class _R1, class _R2, bool _Odd, intmax_t _Qp> 418struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> 419{ 420 static const bool value = false; 421}; 422 423template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2> 424struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> 425{ 426 static const bool value = !_Odd; 427}; 428 429template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1> 430struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> 431{ 432 static const bool value = _Odd; 433}; 434 435template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, 436 intmax_t _M2> 437struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> 438{ 439 static const bool value = __ratio_less1<ratio<_R1::den, _M1>, 440 ratio<_R2::den, _M2>, !_Odd>::value; 441}; 442 443template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value, 444 intmax_t _S2 = __static_sign<_R2::num>::value> 445struct __ratio_less 446{ 447 static const bool value = _S1 < _S2; 448}; 449 450template <class _R1, class _R2> 451struct __ratio_less<_R1, _R2, 1LL, 1LL> 452{ 453 static const bool value = __ratio_less1<_R1, _R2>::value; 454}; 455 456template <class _R1, class _R2> 457struct __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 462template <class _R1, class _R2> 463struct _LIBCPP_TYPE_VIS_ONLY ratio_less 464 : public integral_constant<bool, __ratio_less<_R1, _R2>::value> {}; 465 466template <class _R1, class _R2> 467struct _LIBCPP_TYPE_VIS_ONLY ratio_less_equal 468 : public integral_constant<bool, !ratio_less<_R2, _R1>::value> {}; 469 470template <class _R1, class _R2> 471struct _LIBCPP_TYPE_VIS_ONLY ratio_greater 472 : public integral_constant<bool, ratio_less<_R2, _R1>::value> {}; 473 474template <class _R1, class _R2> 475struct _LIBCPP_TYPE_VIS_ONLY ratio_greater_equal 476 : public integral_constant<bool, !ratio_less<_R1, _R2>::value> {}; 477 478template <class _R1, class _R2> 479struct __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 488