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#pragma GCC system_header 74 75_LIBCPP_BEGIN_NAMESPACE_STD 76 77// __static_gcd 78 79template <intmax_t _Xp, intmax_t _Yp> 80struct __static_gcd 81{ 82 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value; 83}; 84 85template <intmax_t _Xp> 86struct __static_gcd<_Xp, 0> 87{ 88 static const intmax_t value = _Xp; 89}; 90 91// __static_lcm 92 93template <intmax_t _Xp, intmax_t _Yp> 94struct __static_lcm 95{ 96 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp; 97}; 98 99template <intmax_t _Xp> 100struct __static_abs 101{ 102 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp; 103}; 104 105template <intmax_t _Xp> 106struct __static_sign 107{ 108 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1); 109}; 110 111template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value> 112class __ll_add; 113 114template <intmax_t _Xp, intmax_t _Yp> 115class __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"); 121public: 122 static const intmax_t value = _Xp + _Yp; 123}; 124 125template <intmax_t _Xp, intmax_t _Yp> 126class __ll_add<_Xp, _Yp, 0> 127{ 128public: 129 static const intmax_t value = _Xp; 130}; 131 132template <intmax_t _Xp, intmax_t _Yp> 133class __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"); 139public: 140 static const intmax_t value = _Xp + _Yp; 141}; 142 143template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value> 144class __ll_sub; 145 146template <intmax_t _Xp, intmax_t _Yp> 147class __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"); 153public: 154 static const intmax_t value = _Xp - _Yp; 155}; 156 157template <intmax_t _Xp, intmax_t _Yp> 158class __ll_sub<_Xp, _Yp, 0> 159{ 160public: 161 static const intmax_t value = _Xp; 162}; 163 164template <intmax_t _Xp, intmax_t _Yp> 165class __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"); 171public: 172 static const intmax_t value = _Xp - _Yp; 173}; 174 175template <intmax_t _Xp, intmax_t _Yp> 176class __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"); 185public: 186 static const intmax_t value = _Xp * _Yp; 187}; 188 189template <intmax_t _Yp> 190class __ll_mul<0, _Yp> 191{ 192public: 193 static const intmax_t value = 0; 194}; 195 196template <intmax_t _Xp> 197class __ll_mul<_Xp, 0> 198{ 199public: 200 static const intmax_t value = 0; 201}; 202 203template <> 204class __ll_mul<0, 0> 205{ 206public: 207 static const intmax_t value = 0; 208}; 209 210// Not actually used but left here in case needed in future maintenance 211template <intmax_t _Xp, intmax_t _Yp> 212class __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"); 219public: 220 static const intmax_t value = _Xp / _Yp; 221}; 222 223template <intmax_t _Num, intmax_t _Den = 1> 224class _LIBCPP_VISIBLE ratio 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; 233public: 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 240template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::num; 241template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::den; 242 243template <class _Tp> struct __is_ratio : false_type {}; 244template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {}; 245 246typedef ratio<1LL, 1000000000000000000LL> atto; 247typedef ratio<1LL, 1000000000000000LL> femto; 248typedef ratio<1LL, 1000000000000LL> pico; 249typedef ratio<1LL, 1000000000LL> nano; 250typedef ratio<1LL, 1000000LL> micro; 251typedef ratio<1LL, 1000LL> milli; 252typedef ratio<1LL, 100LL> centi; 253typedef ratio<1LL, 10LL> deci; 254typedef ratio< 10LL, 1LL> deca; 255typedef ratio< 100LL, 1LL> hecto; 256typedef ratio< 1000LL, 1LL> kilo; 257typedef ratio< 1000000LL, 1LL> mega; 258typedef ratio< 1000000000LL, 1LL> giga; 259typedef ratio< 1000000000000LL, 1LL> tera; 260typedef ratio< 1000000000000000LL, 1LL> peta; 261typedef ratio<1000000000000000000LL, 1LL> exa; 262 263template <class _R1, class _R2> 264struct __ratio_multiply 265{ 266private: 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; 269public: 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 277template <class _R1, class _R2> 278struct _LIBCPP_VISIBLE ratio_multiply 279 : public __ratio_multiply<_R1, _R2>::type {}; 280 281template <class _R1, class _R2> 282struct __ratio_divide 283{ 284private: 285 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value; 286 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value; 287public: 288 typedef typename ratio 289 < 290 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value, 291 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value 292 >::type type; 293}; 294 295template <class _R1, class _R2> 296struct _LIBCPP_VISIBLE ratio_divide 297 : public __ratio_divide<_R1, _R2>::type {}; 298 299template <class _R1, class _R2> 300struct __ratio_add 301{ 302private: 303 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value; 304 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value; 305public: 306 typedef typename ratio_multiply 307 < 308 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>, 309 ratio 310 < 311 __ll_add 312 < 313 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value, 314 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value 315 >::value, 316 _R2::den 317 > 318 >::type type; 319}; 320 321template <class _R1, class _R2> 322struct _LIBCPP_VISIBLE ratio_add 323 : public __ratio_add<_R1, _R2>::type {}; 324 325template <class _R1, class _R2> 326struct __ratio_subtract 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_sub 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 347template <class _R1, class _R2> 348struct _LIBCPP_VISIBLE ratio_subtract 349 : public __ratio_subtract<_R1, _R2>::type {}; 350 351// ratio_equal 352 353template <class _R1, class _R2> 354struct _LIBCPP_VISIBLE ratio_equal 355 : public integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den> {}; 356 357template <class _R1, class _R2> 358struct _LIBCPP_VISIBLE ratio_not_equal 359 : public integral_constant<bool, !ratio_equal<_R1, _R2>::value> {}; 360 361// ratio_less 362 363template <class _R1, class _R2, bool _Odd = false, 364 intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den, 365 intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den> 366struct __ratio_less1 367{ 368 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2; 369}; 370 371template <class _R1, class _R2, bool _Odd, intmax_t _Q> 372struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, 0> 373{ 374 static const bool value = false; 375}; 376 377template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M2> 378struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, _M2> 379{ 380 static const bool value = !_Odd; 381}; 382 383template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1> 384struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, 0> 385{ 386 static const bool value = _Odd; 387}; 388 389template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1, 390 intmax_t _M2> 391struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, _M2> 392{ 393 static const bool value = __ratio_less1<ratio<_R1::den, _M1>, 394 ratio<_R2::den, _M2>, !_Odd>::value; 395}; 396 397template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value, 398 intmax_t _S2 = __static_sign<_R2::num>::value> 399struct __ratio_less 400{ 401 static const bool value = _S1 < _S2; 402}; 403 404template <class _R1, class _R2> 405struct __ratio_less<_R1, _R2, 1LL, 1LL> 406{ 407 static const bool value = __ratio_less1<_R1, _R2>::value; 408}; 409 410template <class _R1, class _R2> 411struct __ratio_less<_R1, _R2, -1LL, -1LL> 412{ 413 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value; 414}; 415 416template <class _R1, class _R2> 417struct _LIBCPP_VISIBLE ratio_less 418 : public integral_constant<bool, __ratio_less<_R1, _R2>::value> {}; 419 420template <class _R1, class _R2> 421struct _LIBCPP_VISIBLE ratio_less_equal 422 : public integral_constant<bool, !ratio_less<_R2, _R1>::value> {}; 423 424template <class _R1, class _R2> 425struct _LIBCPP_VISIBLE ratio_greater 426 : public integral_constant<bool, ratio_less<_R2, _R1>::value> {}; 427 428template <class _R1, class _R2> 429struct _LIBCPP_VISIBLE ratio_greater_equal 430 : public integral_constant<bool, !ratio_less<_R1, _R2>::value> {}; 431 432template <class _R1, class _R2> 433struct __ratio_gcd 434{ 435 typedef ratio<__static_gcd<_R1::num, _R2::num>::value, 436 __static_lcm<_R1::den, _R2::den>::value> type; 437}; 438 439_LIBCPP_END_NAMESPACE_STD 440 441#endif // _LIBCPP_RATIO 442