Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
ratio504 linesDownload Raw Back to include
1// -*- C++ -*-2//===---------------------------- ratio -----------------------------------===//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 _LIBCUDACXX_RATIO11#define _LIBCUDACXX_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 typedefs43typedef ratio<1, 1000000000000000000000000> yocto;  // not supported44typedef ratio<1,    1000000000000000000000> zepto;  // not supported45typedef ratio<1,       1000000000000000000> atto;46typedef ratio<1,          1000000000000000> femto;47typedef ratio<1,             1000000000000> pico;48typedef ratio<1,                1000000000> nano;49typedef ratio<1,                   1000000> micro;50typedef ratio<1,                      1000> milli;51typedef ratio<1,                       100> centi;52typedef ratio<1,                        10> deci;53typedef ratio<                       10, 1> deca;54typedef ratio<                      100, 1> hecto;55typedef ratio<                     1000, 1> kilo;56typedef ratio<                  1000000, 1> mega;57typedef ratio<               1000000000, 1> giga;58typedef ratio<            1000000000000, 1> tera;59typedef ratio<         1000000000000000, 1> peta;60typedef ratio<      1000000000000000000, 1> exa;61typedef ratio<   1000000000000000000000, 1> zetta;  // not supported62typedef ratio<1000000000000000000000000, 1> yotta;  // not supported63 64  // 20.11.5, ratio comparison65  template <class R1, class R2> inline constexpr bool ratio_equal_v66    = ratio_equal<R1, R2>::value;                                       // C++1767  template <class R1, class R2> inline constexpr bool ratio_not_equal_v68    = ratio_not_equal<R1, R2>::value;                                   // C++1769  template <class R1, class R2> inline constexpr bool ratio_less_v70    = ratio_less<R1, R2>::value;                                        // C++1771  template <class R1, class R2> inline constexpr bool ratio_less_equal_v72    = ratio_less_equal<R1, R2>::value;                                  // C++1773  template <class R1, class R2> inline constexpr bool ratio_greater_v74    = ratio_greater<R1, R2>::value;                                     // C++1775  template <class R1, class R2> inline constexpr bool ratio_greater_equal_v76    = ratio_greater_equal<R1, R2>::value;                               // C++1777}78*/79 80#ifndef __cuda_std__81#include <__config>82#endif // __cuda_std__83 84#include "__assert" // all public C++ headers provide the assertion handler85#include "__type_traits/integral_constant.h"86#include "climits"87#include "cstdint"88#include "type_traits"89 90#ifndef __cuda_std__91#include <__pragma_push>92#endif // __cuda_std__93 94#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)95#  pragma GCC system_header96#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)97#  pragma clang system_header98#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)99#  pragma system_header100#endif // no system header101 102_LIBCUDACXX_BEGIN_NAMESPACE_STD103 104// __static_gcd105 106template <intmax_t _Xp, intmax_t _Yp>107struct __static_gcd108{109    static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;110};111 112template <intmax_t _Xp>113struct __static_gcd<_Xp, 0>114{115    static const intmax_t value = _Xp;116};117 118template <>119struct __static_gcd<0, 0>120{121    static const intmax_t value = 1;122};123 124// __static_lcm125 126template <intmax_t _Xp, intmax_t _Yp>127struct __static_lcm128{129    static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;130};131 132template <intmax_t _Xp>133struct __static_abs134{135    static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;136};137 138template <intmax_t _Xp>139struct __static_sign140{141    static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);142};143 144template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>145class __ll_add;146 147template <intmax_t _Xp, intmax_t _Yp>148class __ll_add<_Xp, _Yp, 1>149{150    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;151    static const intmax_t max = -min;152 153    static_assert(_Xp <= max - _Yp, "overflow in __ll_add");154public:155    static const intmax_t value = _Xp + _Yp;156};157 158template <intmax_t _Xp, intmax_t _Yp>159class __ll_add<_Xp, _Yp, 0>160{161public:162    static const intmax_t value = _Xp;163};164 165template <intmax_t _Xp, intmax_t _Yp>166class __ll_add<_Xp, _Yp, -1>167{168    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;169    static const intmax_t max = -min;170 171    static_assert(min - _Yp <= _Xp, "overflow in __ll_add");172public:173    static const intmax_t value = _Xp + _Yp;174};175 176template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>177class __ll_sub;178 179template <intmax_t _Xp, intmax_t _Yp>180class __ll_sub<_Xp, _Yp, 1>181{182    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;183    static const intmax_t max = -min;184 185    static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");186public:187    static const intmax_t value = _Xp - _Yp;188};189 190template <intmax_t _Xp, intmax_t _Yp>191class __ll_sub<_Xp, _Yp, 0>192{193public:194    static const intmax_t value = _Xp;195};196 197template <intmax_t _Xp, intmax_t _Yp>198class __ll_sub<_Xp, _Yp, -1>199{200    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;201    static const intmax_t max = -min;202 203    static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");204public:205    static const intmax_t value = _Xp - _Yp;206};207 208template <intmax_t _Xp, intmax_t _Yp>209class __ll_mul210{211    static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));212    static const intmax_t min = nan + 1;213    static const intmax_t max = -min;214    static const intmax_t __a_x = __static_abs<_Xp>::value;215    static const intmax_t __a_y = __static_abs<_Yp>::value;216 217    static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");218public:219    static const intmax_t value = _Xp * _Yp;220};221 222template <intmax_t _Yp>223class __ll_mul<0, _Yp>224{225public:226    static const intmax_t value = 0;227};228 229template <intmax_t _Xp>230class __ll_mul<_Xp, 0>231{232public:233    static const intmax_t value = 0;234};235 236template <>237class __ll_mul<0, 0>238{239public:240    static const intmax_t value = 0;241};242 243// Not actually used but left here in case needed in future maintenance244template <intmax_t _Xp, intmax_t _Yp>245class __ll_div246{247    static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));248    static const intmax_t min = nan + 1;249    static const intmax_t max = -min;250 251    static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");252public:253    static const intmax_t value = _Xp / _Yp;254};255 256template <intmax_t _Num, intmax_t _Den = 1>257class _LIBCUDACXX_TEMPLATE_VIS ratio258{259    static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");260    static_assert(_Den != 0, "ratio divide by 0");261    static_assert(__static_abs<_Den>::value >  0, "ratio denominator is out of range");262    static constexpr intmax_t __na = __static_abs<_Num>::value;263    static constexpr intmax_t __da = __static_abs<_Den>::value;264    static constexpr intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;265    static constexpr intmax_t __gcd = __static_gcd<__na, __da>::value;266public:267    static constexpr intmax_t num = __s * __na / __gcd;268    static constexpr intmax_t den = __da / __gcd;269 270    typedef ratio<num, den> type;271};272 273template <intmax_t _Num, intmax_t _Den>274constexpr intmax_t ratio<_Num, _Den>::num;275 276template <intmax_t _Num, intmax_t _Den>277constexpr intmax_t ratio<_Num, _Den>::den;278 279template <class _Tp>                    struct __is_ratio                     : false_type {};280template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type  {};281 282typedef ratio<1LL, 1000000000000000000LL> atto;283typedef ratio<1LL,    1000000000000000LL> femto;284typedef ratio<1LL,       1000000000000LL> pico;285typedef ratio<1LL,          1000000000LL> nano;286typedef ratio<1LL,             1000000LL> micro;287typedef ratio<1LL,                1000LL> milli;288typedef ratio<1LL,                 100LL> centi;289typedef ratio<1LL,                  10LL> deci;290typedef ratio<                 10LL, 1LL> deca;291typedef ratio<                100LL, 1LL> hecto;292typedef ratio<               1000LL, 1LL> kilo;293typedef ratio<            1000000LL, 1LL> mega;294typedef ratio<         1000000000LL, 1LL> giga;295typedef ratio<      1000000000000LL, 1LL> tera;296typedef ratio<   1000000000000000LL, 1LL> peta;297typedef ratio<1000000000000000000LL, 1LL> exa;298 299template <class _R1, class _R2>300struct __ratio_multiply301{302//private:303    static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;304    static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;305public:306    typedef typename ratio307        <308            __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,309            __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value310        >::type type;311};312 313template <class _R1, class _R2> using ratio_multiply314                                    = typename __ratio_multiply<_R1, _R2>::type;315 316template <class _R1, class _R2>317struct __ratio_divide318{319//private:320    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;321    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;322public:323    typedef typename ratio324        <325            __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,326            __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value327        >::type type;328};329 330template <class _R1, class _R2> using ratio_divide331                                      = typename __ratio_divide<_R1, _R2>::type;332 333template <class _R1, class _R2>334struct __ratio_add335{336//private:337    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;338    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;339public:340    typedef typename ratio_multiply341        <342            ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,343            ratio344            <345                __ll_add346                <347                    __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,348                    __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value349                >::value,350                _R2::den351            >352        >::type type;353};354 355template <class _R1, class _R2> using ratio_add356                                         = typename __ratio_add<_R1, _R2>::type;357 358template <class _R1, class _R2>359struct __ratio_subtract360{361//private:362    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;363    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;364public:365    typedef typename ratio_multiply366        <367            ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,368            ratio369            <370                __ll_sub371                <372                    __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,373                    __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value374                >::value,375                _R2::den376            >377        >::type type;378};379 380template <class _R1, class _R2> using ratio_subtract381                                    = typename __ratio_subtract<_R1, _R2>::type;382 383// ratio_equal384 385template <class _R1, class _R2>386struct _LIBCUDACXX_TEMPLATE_VIS ratio_equal387    : public _LIBCUDACXX_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};388 389template <class _R1, class _R2>390struct _LIBCUDACXX_TEMPLATE_VIS ratio_not_equal391    : public _LIBCUDACXX_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};392 393// ratio_less394 395template <class _R1, class _R2, bool _Odd = false,396          intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,397          intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>398struct __ratio_less1399{400    static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;401};402 403template <class _R1, class _R2, bool _Odd, intmax_t _Qp>404struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>405{406    static const bool value = false;407};408 409template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>410struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>411{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{418    static const bool value = _Odd;419};420 421template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,422                                                        intmax_t _M2>423struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>424{425    static const bool value = __ratio_less1<ratio<_R1::den, _M1>,426                                            ratio<_R2::den, _M2>, !_Odd>::value;427};428 429template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,430                                intmax_t _S2 = __static_sign<_R2::num>::value>431struct __ratio_less432{433    static const bool value = _S1 < _S2;434};435 436template <class _R1, class _R2>437struct __ratio_less<_R1, _R2, 1LL, 1LL>438{439    static const bool value = __ratio_less1<_R1, _R2>::value;440};441 442template <class _R1, class _R2>443struct __ratio_less<_R1, _R2, -1LL, -1LL>444{445    static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;446};447 448template <class _R1, class _R2>449struct _LIBCUDACXX_TEMPLATE_VIS ratio_less450    : public _LIBCUDACXX_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};451 452template <class _R1, class _R2>453struct _LIBCUDACXX_TEMPLATE_VIS ratio_less_equal454    : public _LIBCUDACXX_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};455 456template <class _R1, class _R2>457struct _LIBCUDACXX_TEMPLATE_VIS ratio_greater458    : public _LIBCUDACXX_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};459 460template <class _R1, class _R2>461struct _LIBCUDACXX_TEMPLATE_VIS ratio_greater_equal462    : public _LIBCUDACXX_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};463 464template <class _R1, class _R2>465struct __ratio_gcd466{467    typedef ratio<__static_gcd<_R1::num, _R2::num>::value,468                  __static_lcm<_R1::den, _R2::den>::value> type;469};470 471#if _LIBCUDACXX_STD_VER > 14 && !defined(_LIBCUDACXX_HAS_NO_VARIABLE_TEMPLATES)472template <class _R1, class _R2>473_LIBCUDACXX_INLINE_VAR constexpr bool ratio_equal_v474    = ratio_equal<_R1, _R2>::value;475 476template <class _R1, class _R2>477_LIBCUDACXX_INLINE_VAR constexpr bool ratio_not_equal_v478    = ratio_not_equal<_R1, _R2>::value;479 480template <class _R1, class _R2>481_LIBCUDACXX_INLINE_VAR constexpr bool ratio_less_v482    = ratio_less<_R1, _R2>::value;483 484template <class _R1, class _R2>485_LIBCUDACXX_INLINE_VAR constexpr bool ratio_less_equal_v486    = ratio_less_equal<_R1, _R2>::value;487 488template <class _R1, class _R2>489_LIBCUDACXX_INLINE_VAR constexpr bool ratio_greater_v490    = ratio_greater<_R1, _R2>::value;491 492template <class _R1, class _R2>493_LIBCUDACXX_INLINE_VAR constexpr bool ratio_greater_equal_v494    = ratio_greater_equal<_R1, _R2>::value;495#endif496 497_LIBCUDACXX_END_NAMESPACE_STD498 499#ifndef __cuda_std__500#include <__pragma_pop>501#endif //__cuda_std__502 503#endif  // _LIBCUDACXX_RATIO504 
codekingpro/portable-devtools · Team Ai