blob: 9764014a1e3a471fca1f7a871c213c9333d0700e [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- ratio -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnantd397d032010-11-24 17:05:06 +000026 typedef ratio<num, den> type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000027};
28
29// ratio arithmetic
Howard Hinnantd397d032010-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 Hinnantbc8d3f92010-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
73#pragma GCC system_header
74
75_LIBCPP_BEGIN_NAMESPACE_STD
76
77// __static_gcd
78
79template <intmax_t _Xp, intmax_t _Yp>
80struct __static_gcd
81{
82 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
83};
84
85template <intmax_t _Xp>
86struct __static_gcd<_Xp, 0>
87{
88 static const intmax_t value = _Xp;
89};
90
91// __static_lcm
92
93template <intmax_t _Xp, intmax_t _Yp>
94struct __static_lcm
95{
96 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
97};
98
99template <intmax_t _Xp>
100struct __static_abs
101{
102 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
103};
104
105template <intmax_t _Xp>
106struct __static_sign
107{
108 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
109};
110
111template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
112class __ll_add;
113
114template <intmax_t _Xp, intmax_t _Yp>
115class __ll_add<_Xp, _Yp, 1>
116{
117 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
118 static const intmax_t max = -min;
119
120 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
121public:
122 static const intmax_t value = _Xp + _Yp;
123};
124
125template <intmax_t _Xp, intmax_t _Yp>
126class __ll_add<_Xp, _Yp, 0>
127{
128public:
129 static const intmax_t value = _Xp;
130};
131
132template <intmax_t _Xp, intmax_t _Yp>
133class __ll_add<_Xp, _Yp, -1>
134{
135 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
136 static const intmax_t max = -min;
137
138 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
139public:
140 static const intmax_t value = _Xp + _Yp;
141};
142
143template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
144class __ll_sub;
145
146template <intmax_t _Xp, intmax_t _Yp>
147class __ll_sub<_Xp, _Yp, 1>
148{
149 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
150 static const intmax_t max = -min;
151
152 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
153public:
154 static const intmax_t value = _Xp - _Yp;
155};
156
157template <intmax_t _Xp, intmax_t _Yp>
158class __ll_sub<_Xp, _Yp, 0>
159{
160public:
161 static const intmax_t value = _Xp;
162};
163
164template <intmax_t _Xp, intmax_t _Yp>
165class __ll_sub<_Xp, _Yp, -1>
166{
167 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
168 static const intmax_t max = -min;
169
170 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
171public:
172 static const intmax_t value = _Xp - _Yp;
173};
174
175template <intmax_t _Xp, intmax_t _Yp>
176class __ll_mul
177{
178 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
179 static const intmax_t min = nan + 1;
180 static const intmax_t max = -min;
181 static const intmax_t __a_x = __static_abs<_Xp>::value;
182 static const intmax_t __a_y = __static_abs<_Yp>::value;
183
184 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
185public:
186 static const intmax_t value = _Xp * _Yp;
187};
188
189template <intmax_t _Yp>
190class __ll_mul<0, _Yp>
191{
192public:
193 static const intmax_t value = 0;
194};
195
196template <intmax_t _Xp>
197class __ll_mul<_Xp, 0>
198{
199public:
200 static const intmax_t value = 0;
201};
202
203template <>
204class __ll_mul<0, 0>
205{
206public:
207 static const intmax_t value = 0;
208};
209
210// Not actually used but left here in case needed in future maintenance
211template <intmax_t _Xp, intmax_t _Yp>
212class __ll_div
213{
214 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
215 static const intmax_t min = nan + 1;
216 static const intmax_t max = -min;
217
218 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
219public:
220 static const intmax_t value = _Xp / _Yp;
221};
222
223template <intmax_t _Num, intmax_t _Den = 1>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000224class _LIBCPP_VISIBLE ratio
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225{
226 static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
227 static_assert(_Den != 0, "ratio divide by 0");
228 static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
229 static const intmax_t __na = __static_abs<_Num>::value;
230 static const intmax_t __da = __static_abs<_Den>::value;
231 static const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
232 static const intmax_t __gcd = __static_gcd<__na, __da>::value;
233public:
234 static const intmax_t num = __s * __na / __gcd;
235 static const intmax_t den = __da / __gcd;
236
237 typedef ratio<num, den> type;
238};
239
240template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::num;
241template <intmax_t _Num, intmax_t _Den> const intmax_t ratio<_Num, _Den>::den;
242
Howard Hinnantd8bc09b2010-05-18 17:32:30 +0000243template <class _Tp> struct __is_ratio : false_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {};
245
246typedef ratio<1LL, 1000000000000000000LL> atto;
247typedef ratio<1LL, 1000000000000000LL> femto;
248typedef ratio<1LL, 1000000000000LL> pico;
249typedef ratio<1LL, 1000000000LL> nano;
250typedef ratio<1LL, 1000000LL> micro;
251typedef ratio<1LL, 1000LL> milli;
252typedef ratio<1LL, 100LL> centi;
253typedef ratio<1LL, 10LL> deci;
254typedef ratio< 10LL, 1LL> deca;
255typedef ratio< 100LL, 1LL> hecto;
256typedef ratio< 1000LL, 1LL> kilo;
257typedef ratio< 1000000LL, 1LL> mega;
258typedef ratio< 1000000000LL, 1LL> giga;
259typedef ratio< 1000000000000LL, 1LL> tera;
260typedef ratio< 1000000000000000LL, 1LL> peta;
261typedef ratio<1000000000000000000LL, 1LL> exa;
262
263template <class _R1, class _R2>
Howard Hinnantd397d032010-11-24 17:05:06 +0000264struct __ratio_multiply
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265{
266private:
267 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
268 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
269public:
270 typedef typename ratio
271 <
272 __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
273 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
274 >::type type;
275};
276
Howard Hinnant20eda8b2011-05-31 16:55:36 +0000277#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
278
279template <class _R1, class _R2> using ratio_multiply
280 = typename __ratio_multiply<_R1, _R2>::type;
281
282#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
283
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284template <class _R1, class _R2>
Howard Hinnantd397d032010-11-24 17:05:06 +0000285struct _LIBCPP_VISIBLE ratio_multiply
Howard Hinnantac417fa2010-11-28 19:41:07 +0000286 : public __ratio_multiply<_R1, _R2>::type {};
Howard Hinnantd397d032010-11-24 17:05:06 +0000287
Howard Hinnant20eda8b2011-05-31 16:55:36 +0000288#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
289
Howard Hinnantd397d032010-11-24 17:05:06 +0000290template <class _R1, class _R2>
291struct __ratio_divide
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292{
293private:
294 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
295 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
296public:
297 typedef typename ratio
298 <
299 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
300 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
301 >::type type;
302};
303
Howard Hinnant20eda8b2011-05-31 16:55:36 +0000304#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
305
306template <class _R1, class _R2> using ratio_divide
307 = typename __ratio_divide<_R1, _R2>::type;
308
309#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
310
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311template <class _R1, class _R2>
Howard Hinnantd397d032010-11-24 17:05:06 +0000312struct _LIBCPP_VISIBLE ratio_divide
Howard Hinnantac417fa2010-11-28 19:41:07 +0000313 : public __ratio_divide<_R1, _R2>::type {};
Howard Hinnantd397d032010-11-24 17:05:06 +0000314
Howard Hinnant20eda8b2011-05-31 16:55:36 +0000315#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
316
Howard Hinnantd397d032010-11-24 17:05:06 +0000317template <class _R1, class _R2>
318struct __ratio_add
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319{
320private:
321 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
322 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
323public:
324 typedef typename ratio_multiply
325 <
326 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
327 ratio
328 <
329 __ll_add
330 <
331 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
332 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
333 >::value,
334 _R2::den
335 >
336 >::type type;
337};
338
Howard Hinnant20eda8b2011-05-31 16:55:36 +0000339#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
340
341template <class _R1, class _R2> using ratio_add
342 = typename __ratio_add<_R1, _R2>::type;
343
344#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
345
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346template <class _R1, class _R2>
Howard Hinnantd397d032010-11-24 17:05:06 +0000347struct _LIBCPP_VISIBLE ratio_add
Howard Hinnantac417fa2010-11-28 19:41:07 +0000348 : public __ratio_add<_R1, _R2>::type {};
Howard Hinnantd397d032010-11-24 17:05:06 +0000349
Howard Hinnant20eda8b2011-05-31 16:55:36 +0000350#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
351
Howard Hinnantd397d032010-11-24 17:05:06 +0000352template <class _R1, class _R2>
353struct __ratio_subtract
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354{
355private:
356 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
357 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
358public:
359 typedef typename ratio_multiply
360 <
361 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
362 ratio
363 <
364 __ll_sub
365 <
366 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
367 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
368 >::value,
369 _R2::den
370 >
371 >::type type;
372};
373
Howard Hinnant20eda8b2011-05-31 16:55:36 +0000374#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
375
376template <class _R1, class _R2> using ratio_subtract
377 = typename __ratio_subtract<_R1, _R2>::type;
378
379#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
380
Howard Hinnantd397d032010-11-24 17:05:06 +0000381template <class _R1, class _R2>
382struct _LIBCPP_VISIBLE ratio_subtract
Howard Hinnantac417fa2010-11-28 19:41:07 +0000383 : public __ratio_subtract<_R1, _R2>::type {};
Howard Hinnantd397d032010-11-24 17:05:06 +0000384
Howard Hinnant20eda8b2011-05-31 16:55:36 +0000385#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
386
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387// ratio_equal
388
389template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000390struct _LIBCPP_VISIBLE ratio_equal
Howard Hinnant324bb032010-08-22 00:02:43 +0000391 : public integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000394struct _LIBCPP_VISIBLE ratio_not_equal
Howard Hinnant324bb032010-08-22 00:02:43 +0000395 : public integral_constant<bool, !ratio_equal<_R1, _R2>::value> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396
397// ratio_less
398
399template <class _R1, class _R2, bool _Odd = false,
400 intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
401 intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
402struct __ratio_less1
403{
404 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
405};
406
407template <class _R1, class _R2, bool _Odd, intmax_t _Q>
408struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, 0>
409{
410 static const bool value = false;
411};
412
413template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M2>
414struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, _M2>
415{
416 static const bool value = !_Odd;
417};
418
419template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1>
420struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, 0>
421{
422 static const bool value = _Odd;
423};
424
425template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1,
426 intmax_t _M2>
427struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, _M2>
428{
429 static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
430 ratio<_R2::den, _M2>, !_Odd>::value;
431};
432
433template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
434 intmax_t _S2 = __static_sign<_R2::num>::value>
435struct __ratio_less
436{
437 static const bool value = _S1 < _S2;
438};
439
440template <class _R1, class _R2>
441struct __ratio_less<_R1, _R2, 1LL, 1LL>
442{
443 static const bool value = __ratio_less1<_R1, _R2>::value;
444};
445
446template <class _R1, class _R2>
447struct __ratio_less<_R1, _R2, -1LL, -1LL>
448{
449 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
450};
451
452template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000453struct _LIBCPP_VISIBLE ratio_less
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454 : public integral_constant<bool, __ratio_less<_R1, _R2>::value> {};
455
456template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000457struct _LIBCPP_VISIBLE ratio_less_equal
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458 : public integral_constant<bool, !ratio_less<_R2, _R1>::value> {};
459
460template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000461struct _LIBCPP_VISIBLE ratio_greater
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462 : public integral_constant<bool, ratio_less<_R2, _R1>::value> {};
463
464template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000465struct _LIBCPP_VISIBLE ratio_greater_equal
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466 : public integral_constant<bool, !ratio_less<_R1, _R2>::value> {};
467
468template <class _R1, class _R2>
469struct __ratio_gcd
470{
471 typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
472 __static_lcm<_R1::den, _R2::den>::value> type;
473};
474
475_LIBCPP_END_NAMESPACE_STD
476
477#endif // _LIBCPP_RATIO