blob: 4ef66c5bf1973cd1acd0e149c124ffad71eb7311 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- ratio -----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_RATIO
12#define _LIBCPP_RATIO
13
14/*
15 ratio synopsis
16
17namespace std
18{
19
20template <intmax_t N, intmax_t D = 1>
21class ratio
22{
23public:
24 static const intmax_t num;
25 static const intmax_t den;
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +000026 typedef ratio<num, den> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000027};
28
29// ratio arithmetic
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +000030template <class R1, class R2> using ratio_add = ...;
31template <class R1, class R2> using ratio_subtract = ...;
32template <class R1, class R2> using ratio_multiply = ...;
33template <class R1, class R2> using ratio_divide = ...;
Howard Hinnantc51e1022010-05-11 19:42:16 +000034
35// ratio comparison
36template <class R1, class R2> struct ratio_equal;
37template <class R1, class R2> struct ratio_not_equal;
38template <class R1, class R2> struct ratio_less;
39template <class R1, class R2> struct ratio_less_equal;
40template <class R1, class R2> struct ratio_greater;
41template <class R1, class R2> struct ratio_greater_equal;
42
43// convenience SI typedefs
44typedef ratio<1, 1000000000000000000000000> yocto; // not supported
45typedef ratio<1, 1000000000000000000000> zepto; // not supported
46typedef ratio<1, 1000000000000000000> atto;
47typedef ratio<1, 1000000000000000> femto;
48typedef ratio<1, 1000000000000> pico;
49typedef ratio<1, 1000000000> nano;
50typedef ratio<1, 1000000> micro;
51typedef ratio<1, 1000> milli;
52typedef ratio<1, 100> centi;
53typedef ratio<1, 10> deci;
54typedef ratio< 10, 1> deca;
55typedef ratio< 100, 1> hecto;
56typedef ratio< 1000, 1> kilo;
57typedef ratio< 1000000, 1> mega;
58typedef ratio< 1000000000, 1> giga;
59typedef ratio< 1000000000000, 1> tera;
60typedef ratio< 1000000000000000, 1> peta;
61typedef ratio< 1000000000000000000, 1> exa;
62typedef ratio< 1000000000000000000000, 1> zetta; // not supported
63typedef ratio<1000000000000000000000000, 1> yotta; // not supported
64
65}
66*/
67
68#include <__config>
69#include <cstdint>
70#include <climits>
71#include <type_traits>
72
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000073#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000074#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000075#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000076
77_LIBCPP_BEGIN_NAMESPACE_STD
78
79// __static_gcd
80
81template <intmax_t _Xp, intmax_t _Yp>
82struct __static_gcd
83{
84 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
85};
86
87template <intmax_t _Xp>
88struct __static_gcd<_Xp, 0>
89{
90 static const intmax_t value = _Xp;
91};
92
93// __static_lcm
94
95template <intmax_t _Xp, intmax_t _Yp>
96struct __static_lcm
97{
98 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
99};
100
101template <intmax_t _Xp>
102struct __static_abs
103{
104 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
105};
106
107template <intmax_t _Xp>
108struct __static_sign
109{
110 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
111};
112
113template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
114class __ll_add;
115
116template <intmax_t _Xp, intmax_t _Yp>
117class __ll_add<_Xp, _Yp, 1>
118{
119 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
120 static const intmax_t max = -min;
121
122 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
123public:
124 static const intmax_t value = _Xp + _Yp;
125};
126
127template <intmax_t _Xp, intmax_t _Yp>
128class __ll_add<_Xp, _Yp, 0>
129{
130public:
131 static const intmax_t value = _Xp;
132};
133
134template <intmax_t _Xp, intmax_t _Yp>
135class __ll_add<_Xp, _Yp, -1>
136{
137 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
138 static const intmax_t max = -min;
139
140 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
141public:
142 static const intmax_t value = _Xp + _Yp;
143};
144
145template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
146class __ll_sub;
147
148template <intmax_t _Xp, intmax_t _Yp>
149class __ll_sub<_Xp, _Yp, 1>
150{
151 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
152 static const intmax_t max = -min;
153
154 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
155public:
156 static const intmax_t value = _Xp - _Yp;
157};
158
159template <intmax_t _Xp, intmax_t _Yp>
160class __ll_sub<_Xp, _Yp, 0>
161{
162public:
163 static const intmax_t value = _Xp;
164};
165
166template <intmax_t _Xp, intmax_t _Yp>
167class __ll_sub<_Xp, _Yp, -1>
168{
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(_Xp <= max + _Yp, "overflow in __ll_sub");
173public:
174 static const intmax_t value = _Xp - _Yp;
175};
176
177template <intmax_t _Xp, intmax_t _Yp>
178class __ll_mul
179{
180 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
181 static const intmax_t min = nan + 1;
182 static const intmax_t max = -min;
183 static const intmax_t __a_x = __static_abs<_Xp>::value;
184 static const intmax_t __a_y = __static_abs<_Yp>::value;
185
186 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
187public:
188 static const intmax_t value = _Xp * _Yp;
189};
190
191template <intmax_t _Yp>
192class __ll_mul<0, _Yp>
193{
194public:
195 static const intmax_t value = 0;
196};
197
198template <intmax_t _Xp>
199class __ll_mul<_Xp, 0>
200{
201public:
202 static const intmax_t value = 0;
203};
204
205template <>
206class __ll_mul<0, 0>
207{
208public:
209 static const intmax_t value = 0;
210};
211
212// Not actually used but left here in case needed in future maintenance
213template <intmax_t _Xp, intmax_t _Yp>
214class __ll_div
215{
216 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
217 static const intmax_t min = nan + 1;
218 static const intmax_t max = -min;
219
220 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
221public:
222 static const intmax_t value = _Xp / _Yp;
223};
224
225template <intmax_t _Num, intmax_t _Den = 1>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000226class _LIBCPP_VISIBLE ratio
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227{
228 static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
229 static_assert(_Den != 0, "ratio divide by 0");
230 static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
231 static const intmax_t __na = __static_abs<_Num>::value;
232 static const intmax_t __da = __static_abs<_Den>::value;
233 static const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
234 static const intmax_t __gcd = __static_gcd<__na, __da>::value;
235public:
236 static const intmax_t num = __s * __na / __gcd;
237 static const intmax_t den = __da / __gcd;
238
239 typedef ratio<num, den> type;
240};
241
242template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::num;
243template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::den;
244
Howard Hinnantf39463b2010-05-18 17:32:30 +0000245template <class _Tp> struct __is_ratio : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {};
247
248typedef ratio<1LL, 1000000000000000000LL> atto;
249typedef ratio<1LL, 1000000000000000LL> femto;
250typedef ratio<1LL, 1000000000000LL> pico;
251typedef ratio<1LL, 1000000000LL> nano;
252typedef ratio<1LL, 1000000LL> micro;
253typedef ratio<1LL, 1000LL> milli;
254typedef ratio<1LL, 100LL> centi;
255typedef ratio<1LL, 10LL> deci;
256typedef ratio< 10LL, 1LL> deca;
257typedef ratio< 100LL, 1LL> hecto;
258typedef ratio< 1000LL, 1LL> kilo;
259typedef ratio< 1000000LL, 1LL> mega;
260typedef ratio< 1000000000LL, 1LL> giga;
261typedef ratio< 1000000000000LL, 1LL> tera;
262typedef ratio< 1000000000000000LL, 1LL> peta;
263typedef ratio<1000000000000000000LL, 1LL> exa;
264
265template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000266struct __ratio_multiply
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267{
268private:
269 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
270 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
271public:
272 typedef typename ratio
273 <
274 __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
275 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
276 >::type type;
277};
278
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000279#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
280
281template <class _R1, class _R2> using ratio_multiply
282 = typename __ratio_multiply<_R1, _R2>::type;
283
284#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
285
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000287struct _LIBCPP_VISIBLE ratio_multiply
Howard Hinnante179d8f2010-11-28 19:41:07 +0000288 : public __ratio_multiply<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000289
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000290#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
291
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000292template <class _R1, class _R2>
293struct __ratio_divide
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294{
295private:
296 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
297 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
298public:
299 typedef typename ratio
300 <
301 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
302 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
303 >::type type;
304};
305
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000306#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
307
308template <class _R1, class _R2> using ratio_divide
309 = typename __ratio_divide<_R1, _R2>::type;
310
311#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
312
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000314struct _LIBCPP_VISIBLE ratio_divide
Howard Hinnante179d8f2010-11-28 19:41:07 +0000315 : public __ratio_divide<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000316
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000317#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
318
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000319template <class _R1, class _R2>
320struct __ratio_add
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321{
322private:
323 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
324 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
325public:
326 typedef typename ratio_multiply
327 <
328 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
329 ratio
330 <
331 __ll_add
332 <
333 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
334 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
335 >::value,
336 _R2::den
337 >
338 >::type type;
339};
340
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000341#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
342
343template <class _R1, class _R2> using ratio_add
344 = typename __ratio_add<_R1, _R2>::type;
345
346#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
347
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000349struct _LIBCPP_VISIBLE ratio_add
Howard Hinnante179d8f2010-11-28 19:41:07 +0000350 : public __ratio_add<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000351
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000352#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
353
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000354template <class _R1, class _R2>
355struct __ratio_subtract
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356{
357private:
358 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
359 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
360public:
361 typedef typename ratio_multiply
362 <
363 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
364 ratio
365 <
366 __ll_sub
367 <
368 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
369 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
370 >::value,
371 _R2::den
372 >
373 >::type type;
374};
375
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000376#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
377
378template <class _R1, class _R2> using ratio_subtract
379 = typename __ratio_subtract<_R1, _R2>::type;
380
381#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
382
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000383template <class _R1, class _R2>
384struct _LIBCPP_VISIBLE ratio_subtract
Howard Hinnante179d8f2010-11-28 19:41:07 +0000385 : public __ratio_subtract<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000386
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000387#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
388
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389// ratio_equal
390
391template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000392struct _LIBCPP_VISIBLE ratio_equal
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000393 : public integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000396struct _LIBCPP_VISIBLE ratio_not_equal
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000397 : public integral_constant<bool, !ratio_equal<_R1, _R2>::value> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398
399// ratio_less
400
401template <class _R1, class _R2, bool _Odd = false,
402 intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
403 intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
404struct __ratio_less1
405{
406 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
407};
408
409template <class _R1, class _R2, bool _Odd, intmax_t _Q>
410struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, 0>
411{
412 static const bool value = false;
413};
414
415template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M2>
416struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, _M2>
417{
418 static const bool value = !_Odd;
419};
420
421template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1>
422struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, 0>
423{
424 static const bool value = _Odd;
425};
426
427template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1,
428 intmax_t _M2>
429struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, _M2>
430{
431 static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
432 ratio<_R2::den, _M2>, !_Odd>::value;
433};
434
435template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
436 intmax_t _S2 = __static_sign<_R2::num>::value>
437struct __ratio_less
438{
439 static const bool value = _S1 < _S2;
440};
441
442template <class _R1, class _R2>
443struct __ratio_less<_R1, _R2, 1LL, 1LL>
444{
445 static const bool value = __ratio_less1<_R1, _R2>::value;
446};
447
448template <class _R1, class _R2>
449struct __ratio_less<_R1, _R2, -1LL, -1LL>
450{
451 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
452};
453
454template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000455struct _LIBCPP_VISIBLE ratio_less
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456 : public integral_constant<bool, __ratio_less<_R1, _R2>::value> {};
457
458template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000459struct _LIBCPP_VISIBLE ratio_less_equal
Howard Hinnantc51e1022010-05-11 19:42:16 +0000460 : public integral_constant<bool, !ratio_less<_R2, _R1>::value> {};
461
462template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000463struct _LIBCPP_VISIBLE ratio_greater
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464 : public integral_constant<bool, ratio_less<_R2, _R1>::value> {};
465
466template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000467struct _LIBCPP_VISIBLE ratio_greater_equal
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468 : public integral_constant<bool, !ratio_less<_R1, _R2>::value> {};
469
470template <class _R1, class _R2>
471struct __ratio_gcd
472{
473 typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
474 __static_lcm<_R1::den, _R2::den>::value> type;
475};
476
477_LIBCPP_END_NAMESPACE_STD
478
479#endif // _LIBCPP_RATIO