1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_RATIO 11#define _LIBCPP_RATIO 12 13/* 14 ratio synopsis 15 16namespace std 17{ 18 19template <intmax_t N, intmax_t D = 1> 20class ratio 21{ 22public: 23 static constexpr intmax_t num; 24 static constexpr intmax_t den; 25 typedef ratio<num, den> type; 26}; 27 28// ratio arithmetic 29template <class R1, class R2> using ratio_add = ...; 30template <class R1, class R2> using ratio_subtract = ...; 31template <class R1, class R2> using ratio_multiply = ...; 32template <class R1, class R2> using ratio_divide = ...; 33 34// ratio comparison 35template <class R1, class R2> struct ratio_equal; 36template <class R1, class R2> struct ratio_not_equal; 37template <class R1, class R2> struct ratio_less; 38template <class R1, class R2> struct ratio_less_equal; 39template <class R1, class R2> struct ratio_greater; 40template <class R1, class R2> struct ratio_greater_equal; 41 42// convenience SI typedefs 43using quecto = ratio <1, 1'000'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported 44using ronto = ratio <1, 1'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported 45typedef ratio<1, 1000000000000000000000000> yocto; // not supported 46typedef ratio<1, 1000000000000000000000> zepto; // not supported 47typedef ratio<1, 1000000000000000000> atto; 48typedef ratio<1, 1000000000000000> femto; 49typedef ratio<1, 1000000000000> pico; 50typedef ratio<1, 1000000000> nano; 51typedef ratio<1, 1000000> micro; 52typedef ratio<1, 1000> milli; 53typedef ratio<1, 100> centi; 54typedef ratio<1, 10> deci; 55typedef ratio< 10, 1> deca; 56typedef ratio< 100, 1> hecto; 57typedef ratio< 1000, 1> kilo; 58typedef ratio< 1000000, 1> mega; 59typedef ratio< 1000000000, 1> giga; 60typedef ratio< 1000000000000, 1> tera; 61typedef ratio< 1000000000000000, 1> peta; 62typedef ratio< 1000000000000000000, 1> exa; 63typedef ratio< 1000000000000000000000, 1> zetta; // not supported 64typedef ratio<1000000000000000000000000, 1> yotta; // not supported 65using ronna = ratio <1'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported 66using quetta = ratio <1'000'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported 67 68 // 20.11.5, ratio comparison 69 template <class R1, class R2> inline constexpr bool ratio_equal_v 70 = ratio_equal<R1, R2>::value; // C++17 71 template <class R1, class R2> inline constexpr bool ratio_not_equal_v 72 = ratio_not_equal<R1, R2>::value; // C++17 73 template <class R1, class R2> inline constexpr bool ratio_less_v 74 = ratio_less<R1, R2>::value; // C++17 75 template <class R1, class R2> inline constexpr bool ratio_less_equal_v 76 = ratio_less_equal<R1, R2>::value; // C++17 77 template <class R1, class R2> inline constexpr bool ratio_greater_v 78 = ratio_greater<R1, R2>::value; // C++17 79 template <class R1, class R2> inline constexpr bool ratio_greater_equal_v 80 = ratio_greater_equal<R1, R2>::value; // C++17 81} 82*/ 83 84#include <__config> 85#include <__type_traits/integral_constant.h> 86#include <climits> 87#include <cstdint> 88#include <version> 89 90#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 91# pragma GCC system_header 92#endif 93 94_LIBCPP_PUSH_MACROS 95#include <__undef_macros> 96 97_LIBCPP_BEGIN_NAMESPACE_STD 98 99// __static_gcd 100 101template <intmax_t _Xp, intmax_t _Yp> 102inline const intmax_t __static_gcd = __static_gcd<_Yp, _Xp % _Yp>; 103 104template <intmax_t _Xp> 105inline const intmax_t __static_gcd<_Xp, 0> = _Xp; 106 107template <> 108inline const intmax_t __static_gcd<0, 0> = 1; 109 110// __static_lcm 111 112template <intmax_t _Xp, intmax_t _Yp> 113inline const intmax_t __static_lcm = _Xp / __static_gcd<_Xp, _Yp> * _Yp; 114 115template <intmax_t _Xp> 116inline const intmax_t __static_abs = _Xp < 0 ? -_Xp : _Xp; 117 118template <intmax_t _Xp> 119inline const intmax_t __static_sign = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1); 120 121template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> > 122class __ll_add; 123 124template <intmax_t _Xp, intmax_t _Yp> 125class __ll_add<_Xp, _Yp, 1> { 126 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1; 127 static const intmax_t max = -min; 128 129 static_assert(_Xp <= max - _Yp, "overflow in __ll_add"); 130 131public: 132 static const intmax_t value = _Xp + _Yp; 133}; 134 135template <intmax_t _Xp, intmax_t _Yp> 136class __ll_add<_Xp, _Yp, 0> { 137public: 138 static const intmax_t value = _Xp; 139}; 140 141template <intmax_t _Xp, intmax_t _Yp> 142class __ll_add<_Xp, _Yp, -1> { 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"); 147 148public: 149 static const intmax_t value = _Xp + _Yp; 150}; 151 152template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> > 153class __ll_sub; 154 155template <intmax_t _Xp, intmax_t _Yp> 156class __ll_sub<_Xp, _Yp, 1> { 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"); 161 162public: 163 static const intmax_t value = _Xp - _Yp; 164}; 165 166template <intmax_t _Xp, intmax_t _Yp> 167class __ll_sub<_Xp, _Yp, 0> { 168public: 169 static const intmax_t value = _Xp; 170}; 171 172template <intmax_t _Xp, intmax_t _Yp> 173class __ll_sub<_Xp, _Yp, -1> { 174 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1; 175 static const intmax_t max = -min; 176 177 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub"); 178 179public: 180 static const intmax_t value = _Xp - _Yp; 181}; 182 183template <intmax_t _Xp, intmax_t _Yp> 184class __ll_mul { 185 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)); 186 static const intmax_t min = nan + 1; 187 static const intmax_t max = -min; 188 static const intmax_t __a_x = __static_abs<_Xp>; 189 static const intmax_t __a_y = __static_abs<_Yp>; 190 191 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul"); 192 193public: 194 static const intmax_t value = _Xp * _Yp; 195}; 196 197template <intmax_t _Yp> 198class __ll_mul<0, _Yp> { 199public: 200 static const intmax_t value = 0; 201}; 202 203template <intmax_t _Xp> 204class __ll_mul<_Xp, 0> { 205public: 206 static const intmax_t value = 0; 207}; 208 209template <> 210class __ll_mul<0, 0> { 211public: 212 static const intmax_t value = 0; 213}; 214 215// Not actually used but left here in case needed in future maintenance 216template <intmax_t _Xp, intmax_t _Yp> 217class __ll_div { 218 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)); 219 static const intmax_t min = nan + 1; 220 static const intmax_t max = -min; 221 222 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div"); 223 224public: 225 static const intmax_t value = _Xp / _Yp; 226}; 227 228template <intmax_t _Num, intmax_t _Den = 1> 229class _LIBCPP_TEMPLATE_VIS ratio { 230 static_assert(__static_abs<_Num> >= 0, "ratio numerator is out of range"); 231 static_assert(_Den != 0, "ratio divide by 0"); 232 static_assert(__static_abs<_Den> > 0, "ratio denominator is out of range"); 233 static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>; 234 static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>; 235 static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num> * __static_sign<_Den>; 236 static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>; 237 238public: 239 static inline _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd; 240 static inline _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd; 241 242 typedef ratio<num, den> type; 243}; 244 245template <class _Tp> 246inline const bool __is_ratio_v = false; 247 248template <intmax_t _Num, intmax_t _Den> 249inline const bool __is_ratio_v<ratio<_Num, _Den> > = true; 250 251typedef ratio<1LL, 1000000000000000000LL> atto; 252typedef ratio<1LL, 1000000000000000LL> femto; 253typedef ratio<1LL, 1000000000000LL> pico; 254typedef ratio<1LL, 1000000000LL> nano; 255typedef ratio<1LL, 1000000LL> micro; 256typedef ratio<1LL, 1000LL> milli; 257typedef ratio<1LL, 100LL> centi; 258typedef ratio<1LL, 10LL> deci; 259typedef ratio< 10LL, 1LL> deca; 260typedef ratio< 100LL, 1LL> hecto; 261typedef ratio< 1000LL, 1LL> kilo; 262typedef ratio< 1000000LL, 1LL> mega; 263typedef ratio< 1000000000LL, 1LL> giga; 264typedef ratio< 1000000000000LL, 1LL> tera; 265typedef ratio< 1000000000000000LL, 1LL> peta; 266typedef ratio<1000000000000000000LL, 1LL> exa; 267 268template <class _R1, class _R2> 269struct __ratio_multiply { 270private: 271 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>; 272 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>; 273 274 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 275 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 276 277public: 278 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value, 279 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value >::type type; 280}; 281 282#ifndef _LIBCPP_CXX03_LANG 283 284template <class _R1, class _R2> 285using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type; 286 287#else // _LIBCPP_CXX03_LANG 288 289template <class _R1, class _R2> 290struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::type {}; 291 292#endif // _LIBCPP_CXX03_LANG 293 294template <class _R1, class _R2> 295struct __ratio_divide { 296private: 297 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>; 298 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>; 299 300 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 301 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 302 303public: 304 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value, 305 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::type type; 306}; 307 308#ifndef _LIBCPP_CXX03_LANG 309 310template <class _R1, class _R2> 311using ratio_divide = typename __ratio_divide<_R1, _R2>::type; 312 313#else // _LIBCPP_CXX03_LANG 314 315template <class _R1, class _R2> 316struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type {}; 317 318#endif // _LIBCPP_CXX03_LANG 319 320template <class _R1, class _R2> 321struct __ratio_add { 322private: 323 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>; 324 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>; 325 326 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 327 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 328 329public: 330 typedef typename ratio_multiply< 331 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>, 332 ratio< __ll_add< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value, 333 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value, 334 _R2::den > >::type type; 335}; 336 337#ifndef _LIBCPP_CXX03_LANG 338 339template <class _R1, class _R2> 340using ratio_add = typename __ratio_add<_R1, _R2>::type; 341 342#else // _LIBCPP_CXX03_LANG 343 344template <class _R1, class _R2> 345struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {}; 346 347#endif // _LIBCPP_CXX03_LANG 348 349template <class _R1, class _R2> 350struct __ratio_subtract { 351private: 352 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>; 353 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>; 354 355 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 356 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 357 358public: 359 typedef typename ratio_multiply< 360 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>, 361 ratio< __ll_sub< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value, 362 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value, 363 _R2::den > >::type type; 364}; 365 366#ifndef _LIBCPP_CXX03_LANG 367 368template <class _R1, class _R2> 369using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type; 370 371#else // _LIBCPP_CXX03_LANG 372 373template <class _R1, class _R2> 374struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::type {}; 375 376#endif // _LIBCPP_CXX03_LANG 377 378// ratio_equal 379 380template <class _R1, class _R2> 381struct _LIBCPP_TEMPLATE_VIS ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> { 382 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 383 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 384}; 385 386template <class _R1, class _R2> 387struct _LIBCPP_TEMPLATE_VIS ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> { 388 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 389 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 390}; 391 392// ratio_less 393 394template <class _R1, 395 class _R2, 396 bool _Odd = false, 397 intmax_t _Q1 = _R1::num / _R1::den, 398 intmax_t _M1 = _R1::num % _R1::den, 399 intmax_t _Q2 = _R2::num / _R2::den, 400 intmax_t _M2 = _R2::num % _R2::den> 401struct __ratio_less1 { 402 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2; 403}; 404 405template <class _R1, class _R2, bool _Odd, intmax_t _Qp> 406struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> { 407 static const bool value = false; 408}; 409 410template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2> 411struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> { 412 static const bool value = !_Odd; 413}; 414 415template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1> 416struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> { 417 static const bool value = _Odd; 418}; 419 420template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2> 421struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> { 422 static const bool value = __ratio_less1<ratio<_R1::den, _M1>, ratio<_R2::den, _M2>, !_Odd>::value; 423}; 424 425template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>, intmax_t _S2 = __static_sign<_R2::num> > 426struct __ratio_less { 427 static const bool value = _S1 < _S2; 428}; 429 430template <class _R1, class _R2> 431struct __ratio_less<_R1, _R2, 1LL, 1LL> { 432 static const bool value = __ratio_less1<_R1, _R2>::value; 433}; 434 435template <class _R1, class _R2> 436struct __ratio_less<_R1, _R2, -1LL, -1LL> { 437 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value; 438}; 439 440template <class _R1, class _R2> 441struct _LIBCPP_TEMPLATE_VIS ratio_less : _BoolConstant<__ratio_less<_R1, _R2>::value> { 442 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 443 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 444}; 445 446template <class _R1, class _R2> 447struct _LIBCPP_TEMPLATE_VIS ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> { 448 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 449 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 450}; 451 452template <class _R1, class _R2> 453struct _LIBCPP_TEMPLATE_VIS ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> { 454 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 455 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 456}; 457 458template <class _R1, class _R2> 459struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> { 460 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template"); 461 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template"); 462}; 463 464template <class _R1, class _R2> 465using __ratio_gcd = ratio<__static_gcd<_R1::num, _R2::num>, __static_lcm<_R1::den, _R2::den> >; 466 467#if _LIBCPP_STD_VER >= 17 468template <class _R1, class _R2> 469inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value; 470 471template <class _R1, class _R2> 472inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value; 473 474template <class _R1, class _R2> 475inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value; 476 477template <class _R1, class _R2> 478inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value; 479 480template <class _R1, class _R2> 481inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value; 482 483template <class _R1, class _R2> 484inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value; 485#endif 486 487_LIBCPP_END_NAMESPACE_STD 488 489_LIBCPP_POP_MACROS 490 491#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 492# include <type_traits> 493#endif 494 495#endif // _LIBCPP_RATIO 496