xref: /minix3/external/bsd/libc++/dist/libcxx/include/ratio (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
14684ddb6SLionel Sambuc// -*- C++ -*-
24684ddb6SLionel Sambuc//===---------------------------- ratio -----------------------------------===//
34684ddb6SLionel Sambuc//
44684ddb6SLionel Sambuc//                     The LLVM Compiler Infrastructure
54684ddb6SLionel Sambuc//
64684ddb6SLionel Sambuc// This file is dual licensed under the MIT and the University of Illinois Open
74684ddb6SLionel Sambuc// Source Licenses. See LICENSE.TXT for details.
84684ddb6SLionel Sambuc//
94684ddb6SLionel Sambuc//===----------------------------------------------------------------------===//
104684ddb6SLionel Sambuc
114684ddb6SLionel Sambuc#ifndef _LIBCPP_RATIO
124684ddb6SLionel Sambuc#define _LIBCPP_RATIO
134684ddb6SLionel Sambuc
144684ddb6SLionel Sambuc/*
154684ddb6SLionel Sambuc    ratio synopsis
164684ddb6SLionel Sambuc
174684ddb6SLionel Sambucnamespace std
184684ddb6SLionel Sambuc{
194684ddb6SLionel Sambuc
204684ddb6SLionel Sambuctemplate <intmax_t N, intmax_t D = 1>
214684ddb6SLionel Sambucclass ratio
224684ddb6SLionel Sambuc{
234684ddb6SLionel Sambucpublic:
24*0a6a1f1dSLionel Sambuc    static constexpr intmax_t num;
25*0a6a1f1dSLionel Sambuc    static constexpr intmax_t den;
264684ddb6SLionel Sambuc    typedef ratio<num, den> type;
274684ddb6SLionel Sambuc};
284684ddb6SLionel Sambuc
294684ddb6SLionel Sambuc// ratio arithmetic
304684ddb6SLionel Sambuctemplate <class R1, class R2> using ratio_add = ...;
314684ddb6SLionel Sambuctemplate <class R1, class R2> using ratio_subtract = ...;
324684ddb6SLionel Sambuctemplate <class R1, class R2> using ratio_multiply = ...;
334684ddb6SLionel Sambuctemplate <class R1, class R2> using ratio_divide = ...;
344684ddb6SLionel Sambuc
354684ddb6SLionel Sambuc// ratio comparison
364684ddb6SLionel Sambuctemplate <class R1, class R2> struct ratio_equal;
374684ddb6SLionel Sambuctemplate <class R1, class R2> struct ratio_not_equal;
384684ddb6SLionel Sambuctemplate <class R1, class R2> struct ratio_less;
394684ddb6SLionel Sambuctemplate <class R1, class R2> struct ratio_less_equal;
404684ddb6SLionel Sambuctemplate <class R1, class R2> struct ratio_greater;
414684ddb6SLionel Sambuctemplate <class R1, class R2> struct ratio_greater_equal;
424684ddb6SLionel Sambuc
434684ddb6SLionel Sambuc// convenience SI typedefs
444684ddb6SLionel Sambuctypedef ratio<1, 1000000000000000000000000> yocto;  // not supported
454684ddb6SLionel Sambuctypedef ratio<1,    1000000000000000000000> zepto;  // not supported
464684ddb6SLionel Sambuctypedef ratio<1,       1000000000000000000> atto;
474684ddb6SLionel Sambuctypedef ratio<1,          1000000000000000> femto;
484684ddb6SLionel Sambuctypedef ratio<1,             1000000000000> pico;
494684ddb6SLionel Sambuctypedef ratio<1,                1000000000> nano;
504684ddb6SLionel Sambuctypedef ratio<1,                   1000000> micro;
514684ddb6SLionel Sambuctypedef ratio<1,                      1000> milli;
524684ddb6SLionel Sambuctypedef ratio<1,                       100> centi;
534684ddb6SLionel Sambuctypedef ratio<1,                        10> deci;
544684ddb6SLionel Sambuctypedef ratio<                       10, 1> deca;
554684ddb6SLionel Sambuctypedef ratio<                      100, 1> hecto;
564684ddb6SLionel Sambuctypedef ratio<                     1000, 1> kilo;
574684ddb6SLionel Sambuctypedef ratio<                  1000000, 1> mega;
584684ddb6SLionel Sambuctypedef ratio<               1000000000, 1> giga;
594684ddb6SLionel Sambuctypedef ratio<            1000000000000, 1> tera;
604684ddb6SLionel Sambuctypedef ratio<         1000000000000000, 1> peta;
614684ddb6SLionel Sambuctypedef ratio<      1000000000000000000, 1> exa;
624684ddb6SLionel Sambuctypedef ratio<   1000000000000000000000, 1> zetta;  // not supported
634684ddb6SLionel Sambuctypedef ratio<1000000000000000000000000, 1> yotta;  // not supported
644684ddb6SLionel Sambuc
654684ddb6SLionel Sambuc}
664684ddb6SLionel Sambuc*/
674684ddb6SLionel Sambuc
684684ddb6SLionel Sambuc#include <__config>
694684ddb6SLionel Sambuc#include <cstdint>
704684ddb6SLionel Sambuc#include <climits>
714684ddb6SLionel Sambuc#include <type_traits>
724684ddb6SLionel Sambuc
734684ddb6SLionel Sambuc#include <__undef_min_max>
744684ddb6SLionel Sambuc
754684ddb6SLionel Sambuc#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
764684ddb6SLionel Sambuc#pragma GCC system_header
774684ddb6SLionel Sambuc#endif
784684ddb6SLionel Sambuc
794684ddb6SLionel Sambuc_LIBCPP_BEGIN_NAMESPACE_STD
804684ddb6SLionel Sambuc
814684ddb6SLionel Sambuc// __static_gcd
824684ddb6SLionel Sambuc
834684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
844684ddb6SLionel Sambucstruct __static_gcd
854684ddb6SLionel Sambuc{
864684ddb6SLionel Sambuc    static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
874684ddb6SLionel Sambuc};
884684ddb6SLionel Sambuc
894684ddb6SLionel Sambuctemplate <intmax_t _Xp>
904684ddb6SLionel Sambucstruct __static_gcd<_Xp, 0>
914684ddb6SLionel Sambuc{
924684ddb6SLionel Sambuc    static const intmax_t value = _Xp;
934684ddb6SLionel Sambuc};
944684ddb6SLionel Sambuc
954684ddb6SLionel Sambuctemplate <>
964684ddb6SLionel Sambucstruct __static_gcd<0, 0>
974684ddb6SLionel Sambuc{
984684ddb6SLionel Sambuc    static const intmax_t value = 1;
994684ddb6SLionel Sambuc};
1004684ddb6SLionel Sambuc
1014684ddb6SLionel Sambuc// __static_lcm
1024684ddb6SLionel Sambuc
1034684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
1044684ddb6SLionel Sambucstruct __static_lcm
1054684ddb6SLionel Sambuc{
1064684ddb6SLionel Sambuc    static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
1074684ddb6SLionel Sambuc};
1084684ddb6SLionel Sambuc
1094684ddb6SLionel Sambuctemplate <intmax_t _Xp>
1104684ddb6SLionel Sambucstruct __static_abs
1114684ddb6SLionel Sambuc{
1124684ddb6SLionel Sambuc    static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
1134684ddb6SLionel Sambuc};
1144684ddb6SLionel Sambuc
1154684ddb6SLionel Sambuctemplate <intmax_t _Xp>
1164684ddb6SLionel Sambucstruct __static_sign
1174684ddb6SLionel Sambuc{
1184684ddb6SLionel Sambuc    static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
1194684ddb6SLionel Sambuc};
1204684ddb6SLionel Sambuc
1214684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
1224684ddb6SLionel Sambucclass __ll_add;
1234684ddb6SLionel Sambuc
1244684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
1254684ddb6SLionel Sambucclass __ll_add<_Xp, _Yp, 1>
1264684ddb6SLionel Sambuc{
1274684ddb6SLionel Sambuc    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
1284684ddb6SLionel Sambuc    static const intmax_t max = -min;
1294684ddb6SLionel Sambuc
1304684ddb6SLionel Sambuc    static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
1314684ddb6SLionel Sambucpublic:
1324684ddb6SLionel Sambuc    static const intmax_t value = _Xp + _Yp;
1334684ddb6SLionel Sambuc};
1344684ddb6SLionel Sambuc
1354684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
1364684ddb6SLionel Sambucclass __ll_add<_Xp, _Yp, 0>
1374684ddb6SLionel Sambuc{
1384684ddb6SLionel Sambucpublic:
1394684ddb6SLionel Sambuc    static const intmax_t value = _Xp;
1404684ddb6SLionel Sambuc};
1414684ddb6SLionel Sambuc
1424684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
1434684ddb6SLionel Sambucclass __ll_add<_Xp, _Yp, -1>
1444684ddb6SLionel Sambuc{
1454684ddb6SLionel Sambuc    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
1464684ddb6SLionel Sambuc    static const intmax_t max = -min;
1474684ddb6SLionel Sambuc
1484684ddb6SLionel Sambuc    static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
1494684ddb6SLionel Sambucpublic:
1504684ddb6SLionel Sambuc    static const intmax_t value = _Xp + _Yp;
1514684ddb6SLionel Sambuc};
1524684ddb6SLionel Sambuc
1534684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
1544684ddb6SLionel Sambucclass __ll_sub;
1554684ddb6SLionel Sambuc
1564684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
1574684ddb6SLionel Sambucclass __ll_sub<_Xp, _Yp, 1>
1584684ddb6SLionel Sambuc{
1594684ddb6SLionel Sambuc    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
1604684ddb6SLionel Sambuc    static const intmax_t max = -min;
1614684ddb6SLionel Sambuc
1624684ddb6SLionel Sambuc    static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
1634684ddb6SLionel Sambucpublic:
1644684ddb6SLionel Sambuc    static const intmax_t value = _Xp - _Yp;
1654684ddb6SLionel Sambuc};
1664684ddb6SLionel Sambuc
1674684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
1684684ddb6SLionel Sambucclass __ll_sub<_Xp, _Yp, 0>
1694684ddb6SLionel Sambuc{
1704684ddb6SLionel Sambucpublic:
1714684ddb6SLionel Sambuc    static const intmax_t value = _Xp;
1724684ddb6SLionel Sambuc};
1734684ddb6SLionel Sambuc
1744684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
1754684ddb6SLionel Sambucclass __ll_sub<_Xp, _Yp, -1>
1764684ddb6SLionel Sambuc{
1774684ddb6SLionel Sambuc    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
1784684ddb6SLionel Sambuc    static const intmax_t max = -min;
1794684ddb6SLionel Sambuc
1804684ddb6SLionel Sambuc    static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
1814684ddb6SLionel Sambucpublic:
1824684ddb6SLionel Sambuc    static const intmax_t value = _Xp - _Yp;
1834684ddb6SLionel Sambuc};
1844684ddb6SLionel Sambuc
1854684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
1864684ddb6SLionel Sambucclass __ll_mul
1874684ddb6SLionel Sambuc{
1884684ddb6SLionel Sambuc    static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
1894684ddb6SLionel Sambuc    static const intmax_t min = nan + 1;
1904684ddb6SLionel Sambuc    static const intmax_t max = -min;
1914684ddb6SLionel Sambuc    static const intmax_t __a_x = __static_abs<_Xp>::value;
1924684ddb6SLionel Sambuc    static const intmax_t __a_y = __static_abs<_Yp>::value;
1934684ddb6SLionel Sambuc
1944684ddb6SLionel Sambuc    static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
1954684ddb6SLionel Sambucpublic:
1964684ddb6SLionel Sambuc    static const intmax_t value = _Xp * _Yp;
1974684ddb6SLionel Sambuc};
1984684ddb6SLionel Sambuc
1994684ddb6SLionel Sambuctemplate <intmax_t _Yp>
2004684ddb6SLionel Sambucclass __ll_mul<0, _Yp>
2014684ddb6SLionel Sambuc{
2024684ddb6SLionel Sambucpublic:
2034684ddb6SLionel Sambuc    static const intmax_t value = 0;
2044684ddb6SLionel Sambuc};
2054684ddb6SLionel Sambuc
2064684ddb6SLionel Sambuctemplate <intmax_t _Xp>
2074684ddb6SLionel Sambucclass __ll_mul<_Xp, 0>
2084684ddb6SLionel Sambuc{
2094684ddb6SLionel Sambucpublic:
2104684ddb6SLionel Sambuc    static const intmax_t value = 0;
2114684ddb6SLionel Sambuc};
2124684ddb6SLionel Sambuc
2134684ddb6SLionel Sambuctemplate <>
2144684ddb6SLionel Sambucclass __ll_mul<0, 0>
2154684ddb6SLionel Sambuc{
2164684ddb6SLionel Sambucpublic:
2174684ddb6SLionel Sambuc    static const intmax_t value = 0;
2184684ddb6SLionel Sambuc};
2194684ddb6SLionel Sambuc
2204684ddb6SLionel Sambuc// Not actually used but left here in case needed in future maintenance
2214684ddb6SLionel Sambuctemplate <intmax_t _Xp, intmax_t _Yp>
2224684ddb6SLionel Sambucclass __ll_div
2234684ddb6SLionel Sambuc{
2244684ddb6SLionel Sambuc    static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
2254684ddb6SLionel Sambuc    static const intmax_t min = nan + 1;
2264684ddb6SLionel Sambuc    static const intmax_t max = -min;
2274684ddb6SLionel Sambuc
2284684ddb6SLionel Sambuc    static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
2294684ddb6SLionel Sambucpublic:
2304684ddb6SLionel Sambuc    static const intmax_t value = _Xp / _Yp;
2314684ddb6SLionel Sambuc};
2324684ddb6SLionel Sambuc
2334684ddb6SLionel Sambuctemplate <intmax_t _Num, intmax_t _Den = 1>
2344684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS_ONLY ratio
2354684ddb6SLionel Sambuc{
2364684ddb6SLionel Sambuc    static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
2374684ddb6SLionel Sambuc    static_assert(_Den != 0, "ratio divide by 0");
2384684ddb6SLionel Sambuc    static_assert(__static_abs<_Den>::value >  0, "ratio denominator is out of range");
239*0a6a1f1dSLionel Sambuc    static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
240*0a6a1f1dSLionel Sambuc    static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
241*0a6a1f1dSLionel Sambuc    static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
242*0a6a1f1dSLionel Sambuc    static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
2434684ddb6SLionel Sambucpublic:
244*0a6a1f1dSLionel Sambuc    static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
245*0a6a1f1dSLionel Sambuc    static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
2464684ddb6SLionel Sambuc
2474684ddb6SLionel Sambuc    typedef ratio<num, den> type;
2484684ddb6SLionel Sambuc};
2494684ddb6SLionel Sambuc
250*0a6a1f1dSLionel Sambuctemplate <intmax_t _Num, intmax_t _Den>
251*0a6a1f1dSLionel Sambuc_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
252*0a6a1f1dSLionel Sambuc
253*0a6a1f1dSLionel Sambuctemplate <intmax_t _Num, intmax_t _Den>
254*0a6a1f1dSLionel Sambuc_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
2554684ddb6SLionel Sambuc
2564684ddb6SLionel Sambuctemplate <class _Tp>                    struct __is_ratio                     : false_type {};
2574684ddb6SLionel Sambuctemplate <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type  {};
2584684ddb6SLionel Sambuc
2594684ddb6SLionel Sambuctypedef ratio<1LL, 1000000000000000000LL> atto;
2604684ddb6SLionel Sambuctypedef ratio<1LL,    1000000000000000LL> femto;
2614684ddb6SLionel Sambuctypedef ratio<1LL,       1000000000000LL> pico;
2624684ddb6SLionel Sambuctypedef ratio<1LL,          1000000000LL> nano;
2634684ddb6SLionel Sambuctypedef ratio<1LL,             1000000LL> micro;
2644684ddb6SLionel Sambuctypedef ratio<1LL,                1000LL> milli;
2654684ddb6SLionel Sambuctypedef ratio<1LL,                 100LL> centi;
2664684ddb6SLionel Sambuctypedef ratio<1LL,                  10LL> deci;
2674684ddb6SLionel Sambuctypedef ratio<                 10LL, 1LL> deca;
2684684ddb6SLionel Sambuctypedef ratio<                100LL, 1LL> hecto;
2694684ddb6SLionel Sambuctypedef ratio<               1000LL, 1LL> kilo;
2704684ddb6SLionel Sambuctypedef ratio<            1000000LL, 1LL> mega;
2714684ddb6SLionel Sambuctypedef ratio<         1000000000LL, 1LL> giga;
2724684ddb6SLionel Sambuctypedef ratio<      1000000000000LL, 1LL> tera;
2734684ddb6SLionel Sambuctypedef ratio<   1000000000000000LL, 1LL> peta;
2744684ddb6SLionel Sambuctypedef ratio<1000000000000000000LL, 1LL> exa;
2754684ddb6SLionel Sambuc
2764684ddb6SLionel Sambuctemplate <class _R1, class _R2>
2774684ddb6SLionel Sambucstruct __ratio_multiply
2784684ddb6SLionel Sambuc{
2794684ddb6SLionel Sambucprivate:
2804684ddb6SLionel Sambuc    static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
2814684ddb6SLionel Sambuc    static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
2824684ddb6SLionel Sambucpublic:
2834684ddb6SLionel Sambuc    typedef typename ratio
2844684ddb6SLionel Sambuc        <
2854684ddb6SLionel Sambuc            __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
2864684ddb6SLionel Sambuc            __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
2874684ddb6SLionel Sambuc        >::type type;
2884684ddb6SLionel Sambuc};
2894684ddb6SLionel Sambuc
2904684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2914684ddb6SLionel Sambuc
2924684ddb6SLionel Sambuctemplate <class _R1, class _R2> using ratio_multiply
2934684ddb6SLionel Sambuc                                    = typename __ratio_multiply<_R1, _R2>::type;
2944684ddb6SLionel Sambuc
2954684ddb6SLionel Sambuc#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2964684ddb6SLionel Sambuc
2974684ddb6SLionel Sambuctemplate <class _R1, class _R2>
2984684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_multiply
2994684ddb6SLionel Sambuc    : public __ratio_multiply<_R1, _R2>::type {};
3004684ddb6SLionel Sambuc
3014684ddb6SLionel Sambuc#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3024684ddb6SLionel Sambuc
3034684ddb6SLionel Sambuctemplate <class _R1, class _R2>
3044684ddb6SLionel Sambucstruct __ratio_divide
3054684ddb6SLionel Sambuc{
3064684ddb6SLionel Sambucprivate:
3074684ddb6SLionel Sambuc    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
3084684ddb6SLionel Sambuc    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
3094684ddb6SLionel Sambucpublic:
3104684ddb6SLionel Sambuc    typedef typename ratio
3114684ddb6SLionel Sambuc        <
3124684ddb6SLionel Sambuc            __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
3134684ddb6SLionel Sambuc            __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
3144684ddb6SLionel Sambuc        >::type type;
3154684ddb6SLionel Sambuc};
3164684ddb6SLionel Sambuc
3174684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3184684ddb6SLionel Sambuc
3194684ddb6SLionel Sambuctemplate <class _R1, class _R2> using ratio_divide
3204684ddb6SLionel Sambuc                                      = typename __ratio_divide<_R1, _R2>::type;
3214684ddb6SLionel Sambuc
3224684ddb6SLionel Sambuc#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3234684ddb6SLionel Sambuc
3244684ddb6SLionel Sambuctemplate <class _R1, class _R2>
3254684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_divide
3264684ddb6SLionel Sambuc    : public __ratio_divide<_R1, _R2>::type {};
3274684ddb6SLionel Sambuc
3284684ddb6SLionel Sambuc#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3294684ddb6SLionel Sambuc
3304684ddb6SLionel Sambuctemplate <class _R1, class _R2>
3314684ddb6SLionel Sambucstruct __ratio_add
3324684ddb6SLionel Sambuc{
3334684ddb6SLionel Sambucprivate:
3344684ddb6SLionel Sambuc    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
3354684ddb6SLionel Sambuc    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
3364684ddb6SLionel Sambucpublic:
3374684ddb6SLionel Sambuc    typedef typename ratio_multiply
3384684ddb6SLionel Sambuc        <
3394684ddb6SLionel Sambuc            ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
3404684ddb6SLionel Sambuc            ratio
3414684ddb6SLionel Sambuc            <
3424684ddb6SLionel Sambuc                __ll_add
3434684ddb6SLionel Sambuc                <
3444684ddb6SLionel Sambuc                    __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
3454684ddb6SLionel Sambuc                    __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
3464684ddb6SLionel Sambuc                >::value,
3474684ddb6SLionel Sambuc                _R2::den
3484684ddb6SLionel Sambuc            >
3494684ddb6SLionel Sambuc        >::type type;
3504684ddb6SLionel Sambuc};
3514684ddb6SLionel Sambuc
3524684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3534684ddb6SLionel Sambuc
3544684ddb6SLionel Sambuctemplate <class _R1, class _R2> using ratio_add
3554684ddb6SLionel Sambuc                                         = typename __ratio_add<_R1, _R2>::type;
3564684ddb6SLionel Sambuc
3574684ddb6SLionel Sambuc#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3584684ddb6SLionel Sambuc
3594684ddb6SLionel Sambuctemplate <class _R1, class _R2>
3604684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_add
3614684ddb6SLionel Sambuc    : public __ratio_add<_R1, _R2>::type {};
3624684ddb6SLionel Sambuc
3634684ddb6SLionel Sambuc#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3644684ddb6SLionel Sambuc
3654684ddb6SLionel Sambuctemplate <class _R1, class _R2>
3664684ddb6SLionel Sambucstruct __ratio_subtract
3674684ddb6SLionel Sambuc{
3684684ddb6SLionel Sambucprivate:
3694684ddb6SLionel Sambuc    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
3704684ddb6SLionel Sambuc    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
3714684ddb6SLionel Sambucpublic:
3724684ddb6SLionel Sambuc    typedef typename ratio_multiply
3734684ddb6SLionel Sambuc        <
3744684ddb6SLionel Sambuc            ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
3754684ddb6SLionel Sambuc            ratio
3764684ddb6SLionel Sambuc            <
3774684ddb6SLionel Sambuc                __ll_sub
3784684ddb6SLionel Sambuc                <
3794684ddb6SLionel Sambuc                    __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
3804684ddb6SLionel Sambuc                    __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
3814684ddb6SLionel Sambuc                >::value,
3824684ddb6SLionel Sambuc                _R2::den
3834684ddb6SLionel Sambuc            >
3844684ddb6SLionel Sambuc        >::type type;
3854684ddb6SLionel Sambuc};
3864684ddb6SLionel Sambuc
3874684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3884684ddb6SLionel Sambuc
3894684ddb6SLionel Sambuctemplate <class _R1, class _R2> using ratio_subtract
3904684ddb6SLionel Sambuc                                    = typename __ratio_subtract<_R1, _R2>::type;
3914684ddb6SLionel Sambuc
3924684ddb6SLionel Sambuc#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3934684ddb6SLionel Sambuc
3944684ddb6SLionel Sambuctemplate <class _R1, class _R2>
3954684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_subtract
3964684ddb6SLionel Sambuc    : public __ratio_subtract<_R1, _R2>::type {};
3974684ddb6SLionel Sambuc
3984684ddb6SLionel Sambuc#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
3994684ddb6SLionel Sambuc
4004684ddb6SLionel Sambuc// ratio_equal
4014684ddb6SLionel Sambuc
4024684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4034684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_equal
404*0a6a1f1dSLionel Sambuc    : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};
4054684ddb6SLionel Sambuc
4064684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4074684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_not_equal
408*0a6a1f1dSLionel Sambuc    : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};
4094684ddb6SLionel Sambuc
4104684ddb6SLionel Sambuc// ratio_less
4114684ddb6SLionel Sambuc
4124684ddb6SLionel Sambuctemplate <class _R1, class _R2, bool _Odd = false,
4134684ddb6SLionel Sambuc          intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
4144684ddb6SLionel Sambuc          intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
4154684ddb6SLionel Sambucstruct __ratio_less1
4164684ddb6SLionel Sambuc{
4174684ddb6SLionel Sambuc    static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
4184684ddb6SLionel Sambuc};
4194684ddb6SLionel Sambuc
4204684ddb6SLionel Sambuctemplate <class _R1, class _R2, bool _Odd, intmax_t _Qp>
4214684ddb6SLionel Sambucstruct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
4224684ddb6SLionel Sambuc{
4234684ddb6SLionel Sambuc    static const bool value = false;
4244684ddb6SLionel Sambuc};
4254684ddb6SLionel Sambuc
4264684ddb6SLionel Sambuctemplate <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
4274684ddb6SLionel Sambucstruct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
4284684ddb6SLionel Sambuc{
4294684ddb6SLionel Sambuc    static const bool value = !_Odd;
4304684ddb6SLionel Sambuc};
4314684ddb6SLionel Sambuc
4324684ddb6SLionel Sambuctemplate <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
4334684ddb6SLionel Sambucstruct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
4344684ddb6SLionel Sambuc{
4354684ddb6SLionel Sambuc    static const bool value = _Odd;
4364684ddb6SLionel Sambuc};
4374684ddb6SLionel Sambuc
4384684ddb6SLionel Sambuctemplate <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
4394684ddb6SLionel Sambuc                                                        intmax_t _M2>
4404684ddb6SLionel Sambucstruct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
4414684ddb6SLionel Sambuc{
4424684ddb6SLionel Sambuc    static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
4434684ddb6SLionel Sambuc                                            ratio<_R2::den, _M2>, !_Odd>::value;
4444684ddb6SLionel Sambuc};
4454684ddb6SLionel Sambuc
4464684ddb6SLionel Sambuctemplate <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
4474684ddb6SLionel Sambuc                                intmax_t _S2 = __static_sign<_R2::num>::value>
4484684ddb6SLionel Sambucstruct __ratio_less
4494684ddb6SLionel Sambuc{
4504684ddb6SLionel Sambuc    static const bool value = _S1 < _S2;
4514684ddb6SLionel Sambuc};
4524684ddb6SLionel Sambuc
4534684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4544684ddb6SLionel Sambucstruct __ratio_less<_R1, _R2, 1LL, 1LL>
4554684ddb6SLionel Sambuc{
4564684ddb6SLionel Sambuc    static const bool value = __ratio_less1<_R1, _R2>::value;
4574684ddb6SLionel Sambuc};
4584684ddb6SLionel Sambuc
4594684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4604684ddb6SLionel Sambucstruct __ratio_less<_R1, _R2, -1LL, -1LL>
4614684ddb6SLionel Sambuc{
4624684ddb6SLionel Sambuc    static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
4634684ddb6SLionel Sambuc};
4644684ddb6SLionel Sambuc
4654684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4664684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_less
467*0a6a1f1dSLionel Sambuc    : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};
4684684ddb6SLionel Sambuc
4694684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4704684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_less_equal
471*0a6a1f1dSLionel Sambuc    : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};
4724684ddb6SLionel Sambuc
4734684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4744684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_greater
475*0a6a1f1dSLionel Sambuc    : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};
4764684ddb6SLionel Sambuc
4774684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4784684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY ratio_greater_equal
479*0a6a1f1dSLionel Sambuc    : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};
4804684ddb6SLionel Sambuc
4814684ddb6SLionel Sambuctemplate <class _R1, class _R2>
4824684ddb6SLionel Sambucstruct __ratio_gcd
4834684ddb6SLionel Sambuc{
4844684ddb6SLionel Sambuc    typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
4854684ddb6SLionel Sambuc                  __static_lcm<_R1::den, _R2::den>::value> type;
4864684ddb6SLionel Sambuc};
4874684ddb6SLionel Sambuc
4884684ddb6SLionel Sambuc_LIBCPP_END_NAMESPACE_STD
4894684ddb6SLionel Sambuc
4904684ddb6SLionel Sambuc#endif  // _LIBCPP_RATIO
491