blob: b9b43d8388217ff216a8fa967742e5197847c0ab [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
277template <class _R1, class _R2>
Howard Hinnantd397d032010-11-24 17:05:06 +0000278struct _LIBCPP_VISIBLE ratio_multiply
279 : public __ratio_multiply<_R1, _R2>::type
280{
281 typedef typename __ratio_multiply<_R1, _R2>::type type;
282};
283
284template <class _R1, class _R2>
285struct __ratio_divide
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286{
287private:
288 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
289 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
290public:
291 typedef typename ratio
292 <
293 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
294 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
295 >::type type;
296};
297
298template <class _R1, class _R2>
Howard Hinnantd397d032010-11-24 17:05:06 +0000299struct _LIBCPP_VISIBLE ratio_divide
300 : public __ratio_divide<_R1, _R2>::type
301{
302 typedef typename __ratio_divide<_R1, _R2>::type type;
303};
304
305template <class _R1, class _R2>
306struct __ratio_add
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000307{
308private:
309 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
310 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
311public:
312 typedef typename ratio_multiply
313 <
314 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
315 ratio
316 <
317 __ll_add
318 <
319 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
320 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
321 >::value,
322 _R2::den
323 >
324 >::type type;
325};
326
327template <class _R1, class _R2>
Howard Hinnantd397d032010-11-24 17:05:06 +0000328struct _LIBCPP_VISIBLE ratio_add
329 : public __ratio_add<_R1, _R2>::type
330{
331 typedef typename __ratio_add<_R1, _R2>::type type;
332};
333
334template <class _R1, class _R2>
335struct __ratio_subtract
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000336{
337private:
338 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
339 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
340public:
341 typedef typename ratio_multiply
342 <
343 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
344 ratio
345 <
346 __ll_sub
347 <
348 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
349 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
350 >::value,
351 _R2::den
352 >
353 >::type type;
354};
355
Howard Hinnantd397d032010-11-24 17:05:06 +0000356template <class _R1, class _R2>
357struct _LIBCPP_VISIBLE ratio_subtract
358 : public __ratio_subtract<_R1, _R2>::type
359{
360 typedef typename __ratio_subtract<_R1, _R2>::type type;
361};
362
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000363// ratio_equal
364
365template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000366struct _LIBCPP_VISIBLE ratio_equal
Howard Hinnant324bb032010-08-22 00:02:43 +0000367 : public integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368
369template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000370struct _LIBCPP_VISIBLE ratio_not_equal
Howard Hinnant324bb032010-08-22 00:02:43 +0000371 : public integral_constant<bool, !ratio_equal<_R1, _R2>::value> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372
373// ratio_less
374
375template <class _R1, class _R2, bool _Odd = false,
376 intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
377 intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
378struct __ratio_less1
379{
380 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
381};
382
383template <class _R1, class _R2, bool _Odd, intmax_t _Q>
384struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, 0>
385{
386 static const bool value = false;
387};
388
389template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M2>
390struct __ratio_less1<_R1, _R2, _Odd, _Q, 0, _Q, _M2>
391{
392 static const bool value = !_Odd;
393};
394
395template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1>
396struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, 0>
397{
398 static const bool value = _Odd;
399};
400
401template <class _R1, class _R2, bool _Odd, intmax_t _Q, intmax_t _M1,
402 intmax_t _M2>
403struct __ratio_less1<_R1, _R2, _Odd, _Q, _M1, _Q, _M2>
404{
405 static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
406 ratio<_R2::den, _M2>, !_Odd>::value;
407};
408
409template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
410 intmax_t _S2 = __static_sign<_R2::num>::value>
411struct __ratio_less
412{
413 static const bool value = _S1 < _S2;
414};
415
416template <class _R1, class _R2>
417struct __ratio_less<_R1, _R2, 1LL, 1LL>
418{
419 static const bool value = __ratio_less1<_R1, _R2>::value;
420};
421
422template <class _R1, class _R2>
423struct __ratio_less<_R1, _R2, -1LL, -1LL>
424{
425 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
426};
427
428template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000429struct _LIBCPP_VISIBLE ratio_less
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 : public integral_constant<bool, __ratio_less<_R1, _R2>::value> {};
431
432template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000433struct _LIBCPP_VISIBLE ratio_less_equal
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434 : public integral_constant<bool, !ratio_less<_R2, _R1>::value> {};
435
436template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000437struct _LIBCPP_VISIBLE ratio_greater
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438 : public integral_constant<bool, ratio_less<_R2, _R1>::value> {};
439
440template <class _R1, class _R2>
Howard Hinnantaef07cb2010-09-23 15:13:20 +0000441struct _LIBCPP_VISIBLE ratio_greater_equal
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442 : public integral_constant<bool, !ratio_less<_R1, _R2>::value> {};
443
444template <class _R1, class _R2>
445struct __ratio_gcd
446{
447 typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
448 __static_lcm<_R1::den, _R2::den>::value> type;
449};
450
451_LIBCPP_END_NAMESPACE_STD
452
453#endif // _LIBCPP_RATIO