blob: 654cb3312e94381b4883f27ffa5431aa24099239 [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
Howard Hinnantcbf51aa2011-11-01 23:13:37 +000093template <>
94struct __static_gcd<0, 0>
95{
96 static const intmax_t value = 1;
97};
98
Howard Hinnantc51e1022010-05-11 19:42:16 +000099// __static_lcm
100
101template <intmax_t _Xp, intmax_t _Yp>
102struct __static_lcm
103{
104 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
105};
106
107template <intmax_t _Xp>
108struct __static_abs
109{
110 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
111};
112
113template <intmax_t _Xp>
114struct __static_sign
115{
116 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
117};
118
119template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
120class __ll_add;
121
122template <intmax_t _Xp, intmax_t _Yp>
123class __ll_add<_Xp, _Yp, 1>
124{
125 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
126 static const intmax_t max = -min;
127
128 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
129public:
130 static const intmax_t value = _Xp + _Yp;
131};
132
133template <intmax_t _Xp, intmax_t _Yp>
134class __ll_add<_Xp, _Yp, 0>
135{
136public:
137 static const intmax_t value = _Xp;
138};
139
140template <intmax_t _Xp, intmax_t _Yp>
141class __ll_add<_Xp, _Yp, -1>
142{
143 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
144 static const intmax_t max = -min;
145
146 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
147public:
148 static const intmax_t value = _Xp + _Yp;
149};
150
151template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
152class __ll_sub;
153
154template <intmax_t _Xp, intmax_t _Yp>
155class __ll_sub<_Xp, _Yp, 1>
156{
157 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
158 static const intmax_t max = -min;
159
160 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
161public:
162 static const intmax_t value = _Xp - _Yp;
163};
164
165template <intmax_t _Xp, intmax_t _Yp>
166class __ll_sub<_Xp, _Yp, 0>
167{
168public:
169 static const intmax_t value = _Xp;
170};
171
172template <intmax_t _Xp, intmax_t _Yp>
173class __ll_sub<_Xp, _Yp, -1>
174{
175 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
176 static const intmax_t max = -min;
177
178 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
179public:
180 static const intmax_t value = _Xp - _Yp;
181};
182
183template <intmax_t _Xp, intmax_t _Yp>
184class __ll_mul
185{
186 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
187 static const intmax_t min = nan + 1;
188 static const intmax_t max = -min;
189 static const intmax_t __a_x = __static_abs<_Xp>::value;
190 static const intmax_t __a_y = __static_abs<_Yp>::value;
191
192 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
193public:
194 static const intmax_t value = _Xp * _Yp;
195};
196
197template <intmax_t _Yp>
198class __ll_mul<0, _Yp>
199{
200public:
201 static const intmax_t value = 0;
202};
203
204template <intmax_t _Xp>
205class __ll_mul<_Xp, 0>
206{
207public:
208 static const intmax_t value = 0;
209};
210
211template <>
212class __ll_mul<0, 0>
213{
214public:
215 static const intmax_t value = 0;
216};
217
218// Not actually used but left here in case needed in future maintenance
219template <intmax_t _Xp, intmax_t _Yp>
220class __ll_div
221{
222 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
223 static const intmax_t min = nan + 1;
224 static const intmax_t max = -min;
225
226 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
227public:
228 static const intmax_t value = _Xp / _Yp;
229};
230
231template <intmax_t _Num, intmax_t _Den = 1>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000232class _LIBCPP_VISIBLE ratio
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233{
234 static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
235 static_assert(_Den != 0, "ratio divide by 0");
236 static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
237 static const intmax_t __na = __static_abs<_Num>::value;
238 static const intmax_t __da = __static_abs<_Den>::value;
239 static const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
240 static const intmax_t __gcd = __static_gcd<__na, __da>::value;
241public:
242 static const intmax_t num = __s * __na / __gcd;
243 static const intmax_t den = __da / __gcd;
244
245 typedef ratio<num, den> type;
246};
247
248template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::num;
249template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::den;
250
Howard Hinnantf39463b2010-05-18 17:32:30 +0000251template <class _Tp> struct __is_ratio : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {};
253
254typedef ratio<1LL, 1000000000000000000LL> atto;
255typedef ratio<1LL, 1000000000000000LL> femto;
256typedef ratio<1LL, 1000000000000LL> pico;
257typedef ratio<1LL, 1000000000LL> nano;
258typedef ratio<1LL, 1000000LL> micro;
259typedef ratio<1LL, 1000LL> milli;
260typedef ratio<1LL, 100LL> centi;
261typedef ratio<1LL, 10LL> deci;
262typedef ratio< 10LL, 1LL> deca;
263typedef ratio< 100LL, 1LL> hecto;
264typedef ratio< 1000LL, 1LL> kilo;
265typedef ratio< 1000000LL, 1LL> mega;
266typedef ratio< 1000000000LL, 1LL> giga;
267typedef ratio< 1000000000000LL, 1LL> tera;
268typedef ratio< 1000000000000000LL, 1LL> peta;
269typedef ratio<1000000000000000000LL, 1LL> exa;
270
271template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000272struct __ratio_multiply
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273{
274private:
275 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
276 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
277public:
278 typedef typename ratio
279 <
280 __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
281 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
282 >::type type;
283};
284
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000285#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
286
287template <class _R1, class _R2> using ratio_multiply
288 = typename __ratio_multiply<_R1, _R2>::type;
289
290#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
291
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000293struct _LIBCPP_VISIBLE ratio_multiply
Howard Hinnante179d8f2010-11-28 19:41:07 +0000294 : public __ratio_multiply<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000295
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000296#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
297
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000298template <class _R1, class _R2>
299struct __ratio_divide
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300{
301private:
302 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
303 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
304public:
305 typedef typename ratio
306 <
307 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
308 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
309 >::type type;
310};
311
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000312#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
313
314template <class _R1, class _R2> using ratio_divide
315 = typename __ratio_divide<_R1, _R2>::type;
316
317#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
318
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000320struct _LIBCPP_VISIBLE ratio_divide
Howard Hinnante179d8f2010-11-28 19:41:07 +0000321 : public __ratio_divide<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000322
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000323#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
324
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000325template <class _R1, class _R2>
326struct __ratio_add
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327{
328private:
329 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
330 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
331public:
332 typedef typename ratio_multiply
333 <
334 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
335 ratio
336 <
337 __ll_add
338 <
339 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
340 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
341 >::value,
342 _R2::den
343 >
344 >::type type;
345};
346
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000347#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
348
349template <class _R1, class _R2> using ratio_add
350 = typename __ratio_add<_R1, _R2>::type;
351
352#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
353
Howard Hinnantc51e1022010-05-11 19:42:16 +0000354template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000355struct _LIBCPP_VISIBLE ratio_add
Howard Hinnante179d8f2010-11-28 19:41:07 +0000356 : public __ratio_add<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000357
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000358#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
359
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000360template <class _R1, class _R2>
361struct __ratio_subtract
Howard Hinnantc51e1022010-05-11 19:42:16 +0000362{
363private:
364 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
365 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
366public:
367 typedef typename ratio_multiply
368 <
369 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
370 ratio
371 <
372 __ll_sub
373 <
374 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
375 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
376 >::value,
377 _R2::den
378 >
379 >::type type;
380};
381
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000382#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
383
384template <class _R1, class _R2> using ratio_subtract
385 = typename __ratio_subtract<_R1, _R2>::type;
386
387#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
388
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000389template <class _R1, class _R2>
390struct _LIBCPP_VISIBLE ratio_subtract
Howard Hinnante179d8f2010-11-28 19:41:07 +0000391 : public __ratio_subtract<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000392
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000393#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
394
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395// ratio_equal
396
397template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000398struct _LIBCPP_VISIBLE ratio_equal
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000399 : public integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400
401template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000402struct _LIBCPP_VISIBLE ratio_not_equal
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000403 : public integral_constant<bool, !ratio_equal<_R1, _R2>::value> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405// ratio_less
406
407template <class _R1, class _R2, bool _Odd = false,
408 intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
409 intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
410struct __ratio_less1
411{
412 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
413};
414
415template <class _R1, class _R2, bool _Odd, intmax_t _Q>
416struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, 0>
417{
418 static const bool value = false;
419};
420
421template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M2>
422struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, _M2>
423{
424 static const bool value = !_Odd;
425};
426
427template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1>
428struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, 0>
429{
430 static const bool value = _Odd;
431};
432
433template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1,
434 intmax_t _M2>
435struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, _M2>
436{
437 static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
438 ratio<_R2::den, _M2>, !_Odd>::value;
439};
440
441template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
442 intmax_t _S2 = __static_sign<_R2::num>::value>
443struct __ratio_less
444{
445 static const bool value = _S1 < _S2;
446};
447
448template <class _R1, class _R2>
449struct __ratio_less<_R1, _R2, 1LL, 1LL>
450{
451 static const bool value = __ratio_less1<_R1, _R2>::value;
452};
453
454template <class _R1, class _R2>
455struct __ratio_less<_R1, _R2, -1LL, -1LL>
456{
457 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
458};
459
460template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000461struct _LIBCPP_VISIBLE ratio_less
Howard Hinnantc51e1022010-05-11 19:42:16 +0000462 : public integral_constant<bool, __ratio_less<_R1, _R2>::value> {};
463
464template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000465struct _LIBCPP_VISIBLE ratio_less_equal
Howard Hinnantc51e1022010-05-11 19:42:16 +0000466 : public integral_constant<bool, !ratio_less<_R2, _R1>::value> {};
467
468template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000469struct _LIBCPP_VISIBLE ratio_greater
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470 : public integral_constant<bool, ratio_less<_R2, _R1>::value> {};
471
472template <class _R1, class _R2>
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000473struct _LIBCPP_VISIBLE ratio_greater_equal
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474 : public integral_constant<bool, !ratio_less<_R1, _R2>::value> {};
475
476template <class _R1, class _R2>
477struct __ratio_gcd
478{
479 typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
480 __static_lcm<_R1::den, _R2::den>::value> type;
481};
482
483_LIBCPP_END_NAMESPACE_STD
484
485#endif // _LIBCPP_RATIO