ratio revision 288943
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===---------------------------- ratio -----------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_RATIO
12227825Stheraven#define _LIBCPP_RATIO
13227825Stheraven
14227825Stheraven/*
15227825Stheraven    ratio synopsis
16227825Stheraven
17227825Stheravennamespace std
18227825Stheraven{
19227825Stheraven
20227825Stheraventemplate <intmax_t N, intmax_t D = 1>
21227825Stheravenclass ratio
22227825Stheraven{
23227825Stheravenpublic:
24288943Sdim    static constexpr intmax_t num;
25288943Sdim    static constexpr intmax_t den;
26227825Stheraven    typedef ratio<num, den> type;
27227825Stheraven};
28227825Stheraven
29227825Stheraven// ratio arithmetic
30227825Stheraventemplate <class R1, class R2> using ratio_add = ...;
31227825Stheraventemplate <class R1, class R2> using ratio_subtract = ...;
32227825Stheraventemplate <class R1, class R2> using ratio_multiply = ...;
33227825Stheraventemplate <class R1, class R2> using ratio_divide = ...;
34227825Stheraven
35227825Stheraven// ratio comparison
36227825Stheraventemplate <class R1, class R2> struct ratio_equal;
37227825Stheraventemplate <class R1, class R2> struct ratio_not_equal;
38227825Stheraventemplate <class R1, class R2> struct ratio_less;
39227825Stheraventemplate <class R1, class R2> struct ratio_less_equal;
40227825Stheraventemplate <class R1, class R2> struct ratio_greater;
41227825Stheraventemplate <class R1, class R2> struct ratio_greater_equal;
42227825Stheraven
43227825Stheraven// convenience SI typedefs
44227825Stheraventypedef ratio<1, 1000000000000000000000000> yocto;  // not supported
45227825Stheraventypedef ratio<1,    1000000000000000000000> zepto;  // not supported
46227825Stheraventypedef ratio<1,       1000000000000000000> atto;
47227825Stheraventypedef ratio<1,          1000000000000000> femto;
48227825Stheraventypedef ratio<1,             1000000000000> pico;
49227825Stheraventypedef ratio<1,                1000000000> nano;
50227825Stheraventypedef ratio<1,                   1000000> micro;
51227825Stheraventypedef ratio<1,                      1000> milli;
52227825Stheraventypedef ratio<1,                       100> centi;
53227825Stheraventypedef ratio<1,                        10> deci;
54227825Stheraventypedef ratio<                       10, 1> deca;
55227825Stheraventypedef ratio<                      100, 1> hecto;
56227825Stheraventypedef ratio<                     1000, 1> kilo;
57227825Stheraventypedef ratio<                  1000000, 1> mega;
58227825Stheraventypedef ratio<               1000000000, 1> giga;
59227825Stheraventypedef ratio<            1000000000000, 1> tera;
60227825Stheraventypedef ratio<         1000000000000000, 1> peta;
61227825Stheraventypedef ratio<      1000000000000000000, 1> exa;
62227825Stheraventypedef ratio<   1000000000000000000000, 1> zetta;  // not supported
63227825Stheraventypedef ratio<1000000000000000000000000, 1> yotta;  // not supported
64227825Stheraven
65227825Stheraven}
66227825Stheraven*/
67227825Stheraven
68227825Stheraven#include <__config>
69227825Stheraven#include <cstdint>
70227825Stheraven#include <climits>
71227825Stheraven#include <type_traits>
72227825Stheraven
73232924Stheraven#include <__undef_min_max>
74232924Stheraven
75227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
76227825Stheraven#pragma GCC system_header
77227825Stheraven#endif
78227825Stheraven
79227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
80227825Stheraven
81227825Stheraven// __static_gcd
82227825Stheraven
83227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
84227825Stheravenstruct __static_gcd
85227825Stheraven{
86227825Stheraven    static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
87227825Stheraven};
88227825Stheraven
89227825Stheraventemplate <intmax_t _Xp>
90227825Stheravenstruct __static_gcd<_Xp, 0>
91227825Stheraven{
92227825Stheraven    static const intmax_t value = _Xp;
93227825Stheraven};
94227825Stheraven
95227825Stheraventemplate <>
96227825Stheravenstruct __static_gcd<0, 0>
97227825Stheraven{
98227825Stheraven    static const intmax_t value = 1;
99227825Stheraven};
100227825Stheraven
101227825Stheraven// __static_lcm
102227825Stheraven
103227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
104227825Stheravenstruct __static_lcm
105227825Stheraven{
106227825Stheraven    static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
107227825Stheraven};
108227825Stheraven
109227825Stheraventemplate <intmax_t _Xp>
110227825Stheravenstruct __static_abs
111227825Stheraven{
112227825Stheraven    static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
113227825Stheraven};
114227825Stheraven
115227825Stheraventemplate <intmax_t _Xp>
116227825Stheravenstruct __static_sign
117227825Stheraven{
118227825Stheraven    static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
119227825Stheraven};
120227825Stheraven
121227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
122227825Stheravenclass __ll_add;
123227825Stheraven
124227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
125227825Stheravenclass __ll_add<_Xp, _Yp, 1>
126227825Stheraven{
127227825Stheraven    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
128227825Stheraven    static const intmax_t max = -min;
129227825Stheraven
130227825Stheraven    static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
131227825Stheravenpublic:
132227825Stheraven    static const intmax_t value = _Xp + _Yp;
133227825Stheraven};
134227825Stheraven
135227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
136227825Stheravenclass __ll_add<_Xp, _Yp, 0>
137227825Stheraven{
138227825Stheravenpublic:
139227825Stheraven    static const intmax_t value = _Xp;
140227825Stheraven};
141227825Stheraven
142227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
143227825Stheravenclass __ll_add<_Xp, _Yp, -1>
144227825Stheraven{
145227825Stheraven    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
146227825Stheraven    static const intmax_t max = -min;
147227825Stheraven
148227825Stheraven    static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
149227825Stheravenpublic:
150227825Stheraven    static const intmax_t value = _Xp + _Yp;
151227825Stheraven};
152227825Stheraven
153227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
154227825Stheravenclass __ll_sub;
155227825Stheraven
156227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
157227825Stheravenclass __ll_sub<_Xp, _Yp, 1>
158227825Stheraven{
159227825Stheraven    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
160227825Stheraven    static const intmax_t max = -min;
161227825Stheraven
162227825Stheraven    static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
163227825Stheravenpublic:
164227825Stheraven    static const intmax_t value = _Xp - _Yp;
165227825Stheraven};
166227825Stheraven
167227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
168227825Stheravenclass __ll_sub<_Xp, _Yp, 0>
169227825Stheraven{
170227825Stheravenpublic:
171227825Stheraven    static const intmax_t value = _Xp;
172227825Stheraven};
173227825Stheraven
174227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
175227825Stheravenclass __ll_sub<_Xp, _Yp, -1>
176227825Stheraven{
177227825Stheraven    static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
178227825Stheraven    static const intmax_t max = -min;
179227825Stheraven
180227825Stheraven    static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
181227825Stheravenpublic:
182227825Stheraven    static const intmax_t value = _Xp - _Yp;
183227825Stheraven};
184227825Stheraven
185227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
186227825Stheravenclass __ll_mul
187227825Stheraven{
188227825Stheraven    static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
189227825Stheraven    static const intmax_t min = nan + 1;
190227825Stheraven    static const intmax_t max = -min;
191227825Stheraven    static const intmax_t __a_x = __static_abs<_Xp>::value;
192227825Stheraven    static const intmax_t __a_y = __static_abs<_Yp>::value;
193227825Stheraven
194227825Stheraven    static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
195227825Stheravenpublic:
196227825Stheraven    static const intmax_t value = _Xp * _Yp;
197227825Stheraven};
198227825Stheraven
199227825Stheraventemplate <intmax_t _Yp>
200227825Stheravenclass __ll_mul<0, _Yp>
201227825Stheraven{
202227825Stheravenpublic:
203227825Stheraven    static const intmax_t value = 0;
204227825Stheraven};
205227825Stheraven
206227825Stheraventemplate <intmax_t _Xp>
207227825Stheravenclass __ll_mul<_Xp, 0>
208227825Stheraven{
209227825Stheravenpublic:
210227825Stheraven    static const intmax_t value = 0;
211227825Stheraven};
212227825Stheraven
213227825Stheraventemplate <>
214227825Stheravenclass __ll_mul<0, 0>
215227825Stheraven{
216227825Stheravenpublic:
217227825Stheraven    static const intmax_t value = 0;
218227825Stheraven};
219227825Stheraven
220227825Stheraven// Not actually used but left here in case needed in future maintenance
221227825Stheraventemplate <intmax_t _Xp, intmax_t _Yp>
222227825Stheravenclass __ll_div
223227825Stheraven{
224227825Stheraven    static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
225227825Stheraven    static const intmax_t min = nan + 1;
226227825Stheraven    static const intmax_t max = -min;
227227825Stheraven
228227825Stheraven    static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
229227825Stheravenpublic:
230227825Stheraven    static const intmax_t value = _Xp / _Yp;
231227825Stheraven};
232227825Stheraven
233227825Stheraventemplate <intmax_t _Num, intmax_t _Den = 1>
234261272Sdimclass _LIBCPP_TYPE_VIS_ONLY ratio
235227825Stheraven{
236227825Stheraven    static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
237227825Stheraven    static_assert(_Den != 0, "ratio divide by 0");
238227825Stheraven    static_assert(__static_abs<_Den>::value >  0, "ratio denominator is out of range");
239288943Sdim    static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
240288943Sdim    static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
241288943Sdim    static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
242288943Sdim    static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
243227825Stheravenpublic:
244288943Sdim    static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
245288943Sdim    static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
246227825Stheraven
247227825Stheraven    typedef ratio<num, den> type;
248227825Stheraven};
249227825Stheraven
250288943Sdimtemplate <intmax_t _Num, intmax_t _Den>
251288943Sdim_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
252227825Stheraven
253288943Sdimtemplate <intmax_t _Num, intmax_t _Den>
254288943Sdim_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
255288943Sdim
256227825Stheraventemplate <class _Tp>                    struct __is_ratio                     : false_type {};
257227825Stheraventemplate <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type  {};
258227825Stheraven
259227825Stheraventypedef ratio<1LL, 1000000000000000000LL> atto;
260227825Stheraventypedef ratio<1LL,    1000000000000000LL> femto;
261227825Stheraventypedef ratio<1LL,       1000000000000LL> pico;
262227825Stheraventypedef ratio<1LL,          1000000000LL> nano;
263227825Stheraventypedef ratio<1LL,             1000000LL> micro;
264227825Stheraventypedef ratio<1LL,                1000LL> milli;
265227825Stheraventypedef ratio<1LL,                 100LL> centi;
266227825Stheraventypedef ratio<1LL,                  10LL> deci;
267227825Stheraventypedef ratio<                 10LL, 1LL> deca;
268227825Stheraventypedef ratio<                100LL, 1LL> hecto;
269227825Stheraventypedef ratio<               1000LL, 1LL> kilo;
270227825Stheraventypedef ratio<            1000000LL, 1LL> mega;
271227825Stheraventypedef ratio<         1000000000LL, 1LL> giga;
272227825Stheraventypedef ratio<      1000000000000LL, 1LL> tera;
273227825Stheraventypedef ratio<   1000000000000000LL, 1LL> peta;
274227825Stheraventypedef ratio<1000000000000000000LL, 1LL> exa;
275227825Stheraven
276227825Stheraventemplate <class _R1, class _R2>
277227825Stheravenstruct __ratio_multiply
278227825Stheraven{
279227825Stheravenprivate:
280227825Stheraven    static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
281227825Stheraven    static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
282227825Stheravenpublic:
283227825Stheraven    typedef typename ratio
284227825Stheraven        <
285227825Stheraven            __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
286227825Stheraven            __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
287227825Stheraven        >::type type;
288227825Stheraven};
289227825Stheraven
290227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
291227825Stheraven
292227825Stheraventemplate <class _R1, class _R2> using ratio_multiply
293227825Stheraven                                    = typename __ratio_multiply<_R1, _R2>::type;
294227825Stheraven
295227825Stheraven#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
296227825Stheraven
297227825Stheraventemplate <class _R1, class _R2>
298261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_multiply
299227825Stheraven    : public __ratio_multiply<_R1, _R2>::type {};
300227825Stheraven
301227825Stheraven#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
302227825Stheraven
303227825Stheraventemplate <class _R1, class _R2>
304227825Stheravenstruct __ratio_divide
305227825Stheraven{
306227825Stheravenprivate:
307227825Stheraven    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
308227825Stheraven    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
309227825Stheravenpublic:
310227825Stheraven    typedef typename ratio
311227825Stheraven        <
312227825Stheraven            __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
313227825Stheraven            __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
314227825Stheraven        >::type type;
315227825Stheraven};
316227825Stheraven
317227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
318227825Stheraven
319227825Stheraventemplate <class _R1, class _R2> using ratio_divide
320227825Stheraven                                      = typename __ratio_divide<_R1, _R2>::type;
321227825Stheraven
322227825Stheraven#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
323227825Stheraven
324227825Stheraventemplate <class _R1, class _R2>
325261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_divide
326227825Stheraven    : public __ratio_divide<_R1, _R2>::type {};
327227825Stheraven
328227825Stheraven#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
329227825Stheraven
330227825Stheraventemplate <class _R1, class _R2>
331227825Stheravenstruct __ratio_add
332227825Stheraven{
333227825Stheravenprivate:
334227825Stheraven    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
335227825Stheraven    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
336227825Stheravenpublic:
337227825Stheraven    typedef typename ratio_multiply
338227825Stheraven        <
339227825Stheraven            ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
340227825Stheraven            ratio
341227825Stheraven            <
342227825Stheraven                __ll_add
343227825Stheraven                <
344227825Stheraven                    __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
345227825Stheraven                    __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
346227825Stheraven                >::value,
347227825Stheraven                _R2::den
348227825Stheraven            >
349227825Stheraven        >::type type;
350227825Stheraven};
351227825Stheraven
352227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
353227825Stheraven
354227825Stheraventemplate <class _R1, class _R2> using ratio_add
355227825Stheraven                                         = typename __ratio_add<_R1, _R2>::type;
356227825Stheraven
357227825Stheraven#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
358227825Stheraven
359227825Stheraventemplate <class _R1, class _R2>
360261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_add
361227825Stheraven    : public __ratio_add<_R1, _R2>::type {};
362227825Stheraven
363227825Stheraven#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
364227825Stheraven
365227825Stheraventemplate <class _R1, class _R2>
366227825Stheravenstruct __ratio_subtract
367227825Stheraven{
368227825Stheravenprivate:
369227825Stheraven    static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
370227825Stheraven    static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
371227825Stheravenpublic:
372227825Stheraven    typedef typename ratio_multiply
373227825Stheraven        <
374227825Stheraven            ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
375227825Stheraven            ratio
376227825Stheraven            <
377227825Stheraven                __ll_sub
378227825Stheraven                <
379227825Stheraven                    __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
380227825Stheraven                    __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
381227825Stheraven                >::value,
382227825Stheraven                _R2::den
383227825Stheraven            >
384227825Stheraven        >::type type;
385227825Stheraven};
386227825Stheraven
387227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
388227825Stheraven
389227825Stheraventemplate <class _R1, class _R2> using ratio_subtract
390227825Stheraven                                    = typename __ratio_subtract<_R1, _R2>::type;
391227825Stheraven
392227825Stheraven#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
393227825Stheraven
394227825Stheraventemplate <class _R1, class _R2>
395261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_subtract
396227825Stheraven    : public __ratio_subtract<_R1, _R2>::type {};
397227825Stheraven
398227825Stheraven#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
399227825Stheraven
400227825Stheraven// ratio_equal
401227825Stheraven
402227825Stheraventemplate <class _R1, class _R2>
403261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_equal
404288943Sdim    : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};
405227825Stheraven
406227825Stheraventemplate <class _R1, class _R2>
407261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_not_equal
408288943Sdim    : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};
409227825Stheraven
410227825Stheraven// ratio_less
411227825Stheraven
412227825Stheraventemplate <class _R1, class _R2, bool _Odd = false,
413227825Stheraven          intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
414227825Stheraven          intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
415227825Stheravenstruct __ratio_less1
416227825Stheraven{
417227825Stheraven    static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
418227825Stheraven};
419227825Stheraven
420232924Stheraventemplate <class _R1, class _R2, bool _Odd, intmax_t _Qp>
421232924Stheravenstruct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
422227825Stheraven{
423227825Stheraven    static const bool value = false;
424227825Stheraven};
425227825Stheraven
426232924Stheraventemplate <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
427232924Stheravenstruct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
428227825Stheraven{
429227825Stheraven    static const bool value = !_Odd;
430227825Stheraven};
431227825Stheraven
432232924Stheraventemplate <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
433232924Stheravenstruct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
434227825Stheraven{
435227825Stheraven    static const bool value = _Odd;
436227825Stheraven};
437227825Stheraven
438232924Stheraventemplate <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
439227825Stheraven                                                        intmax_t _M2>
440232924Stheravenstruct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
441227825Stheraven{
442227825Stheraven    static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
443227825Stheraven                                            ratio<_R2::den, _M2>, !_Odd>::value;
444227825Stheraven};
445227825Stheraven
446227825Stheraventemplate <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
447227825Stheraven                                intmax_t _S2 = __static_sign<_R2::num>::value>
448227825Stheravenstruct __ratio_less
449227825Stheraven{
450227825Stheraven    static const bool value = _S1 < _S2;
451227825Stheraven};
452227825Stheraven
453227825Stheraventemplate <class _R1, class _R2>
454227825Stheravenstruct __ratio_less<_R1, _R2, 1LL, 1LL>
455227825Stheraven{
456227825Stheraven    static const bool value = __ratio_less1<_R1, _R2>::value;
457227825Stheraven};
458227825Stheraven
459227825Stheraventemplate <class _R1, class _R2>
460227825Stheravenstruct __ratio_less<_R1, _R2, -1LL, -1LL>
461227825Stheraven{
462227825Stheraven    static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
463227825Stheraven};
464227825Stheraven
465227825Stheraventemplate <class _R1, class _R2>
466261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_less
467288943Sdim    : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};
468227825Stheraven
469227825Stheraventemplate <class _R1, class _R2>
470261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_less_equal
471288943Sdim    : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};
472227825Stheraven
473227825Stheraventemplate <class _R1, class _R2>
474261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_greater
475288943Sdim    : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};
476227825Stheraven
477227825Stheraventemplate <class _R1, class _R2>
478261272Sdimstruct _LIBCPP_TYPE_VIS_ONLY ratio_greater_equal
479288943Sdim    : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};
480227825Stheraven
481227825Stheraventemplate <class _R1, class _R2>
482227825Stheravenstruct __ratio_gcd
483227825Stheraven{
484227825Stheraven    typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
485227825Stheraven                  __static_lcm<_R1::den, _R2::den>::value> type;
486227825Stheraven};
487227825Stheraven
488227825Stheraven_LIBCPP_END_NAMESPACE_STD
489227825Stheraven
490227825Stheraven#endif  // _LIBCPP_RATIO
491