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