462 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03_RATIO11#define _LIBCPP___CXX03_RATIO12 13/*14 ratio synopsis15 16namespace std17{18 19template <intmax_t N, intmax_t D = 1>20class ratio21{22public:23 static constexpr intmax_t num;24 static constexpr intmax_t den;25 typedef ratio<num, den> type;26};27 28// ratio arithmetic29template <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 comparison35template <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 typedefs43using quecto = ratio <1, 1'000'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported44using ronto = ratio <1, 1'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported45typedef ratio<1, 1000000000000000000000000> yocto; // not supported46typedef ratio<1, 1000000000000000000000> zepto; // not supported47typedef 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 supported64typedef ratio<1000000000000000000000000, 1> yotta; // not supported65using ronna = ratio <1'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported66using quetta = ratio <1'000'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported67 68 // 20.11.5, ratio comparison69 template <class R1, class R2> inline constexpr bool ratio_equal_v70 = ratio_equal<R1, R2>::value; // C++1771 template <class R1, class R2> inline constexpr bool ratio_not_equal_v72 = ratio_not_equal<R1, R2>::value; // C++1773 template <class R1, class R2> inline constexpr bool ratio_less_v74 = ratio_less<R1, R2>::value; // C++1775 template <class R1, class R2> inline constexpr bool ratio_less_equal_v76 = ratio_less_equal<R1, R2>::value; // C++1777 template <class R1, class R2> inline constexpr bool ratio_greater_v78 = ratio_greater<R1, R2>::value; // C++1779 template <class R1, class R2> inline constexpr bool ratio_greater_equal_v80 = ratio_greater_equal<R1, R2>::value; // C++1781}82*/83 84#include <__cxx03/__config>85#include <__cxx03/__type_traits/integral_constant.h>86#include <__cxx03/climits>87#include <__cxx03/cstdint>88#include <__cxx03/version>89 90#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)91# pragma GCC system_header92#endif93 94_LIBCPP_PUSH_MACROS95#include <__cxx03/__undef_macros>96 97_LIBCPP_BEGIN_NAMESPACE_STD98 99// __static_gcd100 101template <intmax_t _Xp, intmax_t _Yp>102struct __static_gcd {103 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;104};105 106template <intmax_t _Xp>107struct __static_gcd<_Xp, 0> {108 static const intmax_t value = _Xp;109};110 111template <>112struct __static_gcd<0, 0> {113 static const intmax_t value = 1;114};115 116// __static_lcm117 118template <intmax_t _Xp, intmax_t _Yp>119struct __static_lcm {120 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;121};122 123template <intmax_t _Xp>124struct __static_abs {125 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;126};127 128template <intmax_t _Xp>129struct __static_sign {130 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);131};132 133template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>134class __ll_add;135 136template <intmax_t _Xp, intmax_t _Yp>137class __ll_add<_Xp, _Yp, 1> {138 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;139 static const intmax_t max = -min;140 141 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");142 143public:144 static const intmax_t value = _Xp + _Yp;145};146 147template <intmax_t _Xp, intmax_t _Yp>148class __ll_add<_Xp, _Yp, 0> {149public:150 static const intmax_t value = _Xp;151};152 153template <intmax_t _Xp, intmax_t _Yp>154class __ll_add<_Xp, _Yp, -1> {155 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;156 static const intmax_t max = -min;157 158 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");159 160public:161 static const intmax_t value = _Xp + _Yp;162};163 164template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>165class __ll_sub;166 167template <intmax_t _Xp, intmax_t _Yp>168class __ll_sub<_Xp, _Yp, 1> {169 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;170 static const intmax_t max = -min;171 172 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");173 174public:175 static const intmax_t value = _Xp - _Yp;176};177 178template <intmax_t _Xp, intmax_t _Yp>179class __ll_sub<_Xp, _Yp, 0> {180public:181 static const intmax_t value = _Xp;182};183 184template <intmax_t _Xp, intmax_t _Yp>185class __ll_sub<_Xp, _Yp, -1> {186 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;187 static const intmax_t max = -min;188 189 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");190 191public:192 static const intmax_t value = _Xp - _Yp;193};194 195template <intmax_t _Xp, intmax_t _Yp>196class __ll_mul {197 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));198 static const intmax_t min = nan + 1;199 static const intmax_t max = -min;200 static const intmax_t __a_x = __static_abs<_Xp>::value;201 static const intmax_t __a_y = __static_abs<_Yp>::value;202 203 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");204 205public:206 static const intmax_t value = _Xp * _Yp;207};208 209template <intmax_t _Yp>210class __ll_mul<0, _Yp> {211public:212 static const intmax_t value = 0;213};214 215template <intmax_t _Xp>216class __ll_mul<_Xp, 0> {217public:218 static const intmax_t value = 0;219};220 221template <>222class __ll_mul<0, 0> {223public:224 static const intmax_t value = 0;225};226 227// Not actually used but left here in case needed in future maintenance228template <intmax_t _Xp, intmax_t _Yp>229class __ll_div {230 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));231 static const intmax_t min = nan + 1;232 static const intmax_t max = -min;233 234 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");235 236public:237 static const intmax_t value = _Xp / _Yp;238};239 240template <intmax_t _Num, intmax_t _Den = 1>241class _LIBCPP_TEMPLATE_VIS ratio {242 static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");243 static_assert(_Den != 0, "ratio divide by 0");244 static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");245 static const intmax_t __na = __static_abs<_Num>::value;246 static const intmax_t __da = __static_abs<_Den>::value;247 static const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;248 static const intmax_t __gcd = __static_gcd<__na, __da>::value;249 250public:251 static const intmax_t num = __s * __na / __gcd;252 static const intmax_t den = __da / __gcd;253 254 typedef ratio<num, den> type;255};256 257template <intmax_t _Num, intmax_t _Den>258const intmax_t ratio<_Num, _Den>::num;259 260template <intmax_t _Num, intmax_t _Den>261const intmax_t ratio<_Num, _Den>::den;262 263template <class _Tp>264struct __is_ratio : false_type {};265template <intmax_t _Num, intmax_t _Den>266struct __is_ratio<ratio<_Num, _Den> > : true_type {};267 268typedef ratio<1LL, 1000000000000000000LL> atto;269typedef ratio<1LL, 1000000000000000LL> femto;270typedef ratio<1LL, 1000000000000LL> pico;271typedef ratio<1LL, 1000000000LL> nano;272typedef ratio<1LL, 1000000LL> micro;273typedef ratio<1LL, 1000LL> milli;274typedef ratio<1LL, 100LL> centi;275typedef ratio<1LL, 10LL> deci;276typedef ratio< 10LL, 1LL> deca;277typedef ratio< 100LL, 1LL> hecto;278typedef ratio< 1000LL, 1LL> kilo;279typedef ratio< 1000000LL, 1LL> mega;280typedef ratio< 1000000000LL, 1LL> giga;281typedef ratio< 1000000000000LL, 1LL> tera;282typedef ratio< 1000000000000000LL, 1LL> peta;283typedef ratio<1000000000000000000LL, 1LL> exa;284 285template <class _R1, class _R2>286struct __ratio_multiply {287private:288 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;289 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;290 291 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");292 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");293 294public:295 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,296 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value >::type type;297};298 299template <class _R1, class _R2>300struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::type {};301 302template <class _R1, class _R2>303struct __ratio_divide {304private:305 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;306 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;307 308 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");309 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");310 311public:312 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,313 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::type type;314};315 316template <class _R1, class _R2>317struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type {};318 319template <class _R1, class _R2>320struct __ratio_add {321private:322 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;323 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;324 325 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");326 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");327 328public:329 typedef typename ratio_multiply<330 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,331 ratio< __ll_add< __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 >::value,333 _R2::den > >::type type;334};335 336template <class _R1, class _R2>337struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {};338 339template <class _R1, class _R2>340struct __ratio_subtract {341private:342 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;343 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;344 345 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");346 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");347 348public:349 typedef typename ratio_multiply<350 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,351 ratio< __ll_sub< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,352 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,353 _R2::den > >::type type;354};355 356template <class _R1, class _R2>357struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::type {};358 359// ratio_equal360 361template <class _R1, class _R2>362struct _LIBCPP_TEMPLATE_VIS ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {363 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");364 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");365};366 367template <class _R1, class _R2>368struct _LIBCPP_TEMPLATE_VIS ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> {369 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");370 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");371};372 373// ratio_less374 375template <class _R1,376 class _R2,377 bool _Odd = false,378 intmax_t _Q1 = _R1::num / _R1::den,379 intmax_t _M1 = _R1::num % _R1::den,380 intmax_t _Q2 = _R2::num / _R2::den,381 intmax_t _M2 = _R2::num % _R2::den>382struct __ratio_less1 {383 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;384};385 386template <class _R1, class _R2, bool _Odd, intmax_t _Qp>387struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> {388 static const bool value = false;389};390 391template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>392struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> {393 static const bool value = !_Odd;394};395 396template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>397struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> {398 static const bool value = _Odd;399};400 401template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2>402struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> {403 static const bool value = __ratio_less1<ratio<_R1::den, _M1>, ratio<_R2::den, _M2>, !_Odd>::value;404};405 406template <class _R1,407 class _R2,408 intmax_t _S1 = __static_sign<_R1::num>::value,409 intmax_t _S2 = __static_sign<_R2::num>::value>410struct __ratio_less {411 static const bool value = _S1 < _S2;412};413 414template <class _R1, class _R2>415struct __ratio_less<_R1, _R2, 1LL, 1LL> {416 static const bool value = __ratio_less1<_R1, _R2>::value;417};418 419template <class _R1, class _R2>420struct __ratio_less<_R1, _R2, -1LL, -1LL> {421 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;422};423 424template <class _R1, class _R2>425struct _LIBCPP_TEMPLATE_VIS ratio_less : _BoolConstant<__ratio_less<_R1, _R2>::value> {426 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");427 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");428};429 430template <class _R1, class _R2>431struct _LIBCPP_TEMPLATE_VIS ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> {432 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");433 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");434};435 436template <class _R1, class _R2>437struct _LIBCPP_TEMPLATE_VIS ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> {438 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");439 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");440};441 442template <class _R1, class _R2>443struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> {444 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");445 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");446};447 448template <class _R1, class _R2>449struct __ratio_gcd {450 typedef ratio<__static_gcd<_R1::num, _R2::num>::value, __static_lcm<_R1::den, _R2::den>::value> type;451};452 453_LIBCPP_END_NAMESPACE_STD454 455_LIBCPP_POP_MACROS456 457#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)458# include <__cxx03/type_traits>459#endif460 461#endif // _LIBCPP___CXX03_RATIO462