]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/include/ratio
Update compiler-rt to release_39 branch r288513. Since this contains a
[FreeBSD/FreeBSD.git] / contrib / libc++ / include / ratio
1 // -*- C++ -*-
2 //===---------------------------- ratio -----------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_RATIO
12 #define _LIBCPP_RATIO
13
14 /*
15     ratio synopsis
16
17 namespace std
18 {
19
20 template <intmax_t N, intmax_t D = 1>
21 class ratio
22 {
23 public:
24     static constexpr intmax_t num;
25     static constexpr intmax_t den;
26     typedef ratio<num, den> type;
27 };
28
29 // ratio arithmetic
30 template <class R1, class R2> using ratio_add = ...;
31 template <class R1, class R2> using ratio_subtract = ...;
32 template <class R1, class R2> using ratio_multiply = ...;
33 template <class R1, class R2> using ratio_divide = ...;
34
35 // ratio comparison
36 template <class R1, class R2> struct ratio_equal;
37 template <class R1, class R2> struct ratio_not_equal;
38 template <class R1, class R2> struct ratio_less;
39 template <class R1, class R2> struct ratio_less_equal;
40 template <class R1, class R2> struct ratio_greater;
41 template <class R1, class R2> struct ratio_greater_equal;
42
43 // convenience SI typedefs
44 typedef ratio<1, 1000000000000000000000000> yocto;  // not supported
45 typedef ratio<1,    1000000000000000000000> zepto;  // not supported
46 typedef ratio<1,       1000000000000000000> atto;
47 typedef ratio<1,          1000000000000000> femto;
48 typedef ratio<1,             1000000000000> pico;
49 typedef ratio<1,                1000000000> nano;
50 typedef ratio<1,                   1000000> micro;
51 typedef ratio<1,                      1000> milli;
52 typedef ratio<1,                       100> centi;
53 typedef ratio<1,                        10> deci;
54 typedef ratio<                       10, 1> deca;
55 typedef ratio<                      100, 1> hecto;
56 typedef ratio<                     1000, 1> kilo;
57 typedef ratio<                  1000000, 1> mega;
58 typedef ratio<               1000000000, 1> giga;
59 typedef ratio<            1000000000000, 1> tera;
60 typedef ratio<         1000000000000000, 1> peta;
61 typedef ratio<      1000000000000000000, 1> exa;
62 typedef ratio<   1000000000000000000000, 1> zetta;  // not supported
63 typedef ratio<1000000000000000000000000, 1> yotta;  // not supported
64
65   // 20.11.5, ratio comparison
66   template <class R1, class R2> constexpr bool ratio_equal_v
67     = ratio_equal<R1, R2>::value;                                       // C++17
68   template <class R1, class R2> constexpr bool ratio_not_equal_v
69     = ratio_not_equal<R1, R2>::value;                                   // C++17
70   template <class R1, class R2> constexpr bool ratio_less_v
71     = ratio_less<R1, R2>::value;                                        // C++17
72   template <class R1, class R2> constexpr bool ratio_less_equal_v
73     = ratio_less_equal<R1, R2>::value;                                  // C++17
74   template <class R1, class R2> constexpr bool ratio_greater_v
75     = ratio_greater<R1, R2>::value;                                     // C++17
76   template <class R1, class R2> constexpr bool ratio_greater_equal_v
77     = ratio_greater_equal<R1, R2>::value;                               // C++17
78 }
79 */
80
81 #include <__config>
82 #include <cstdint>
83 #include <climits>
84 #include <type_traits>
85
86 #include <__undef_min_max>
87
88 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
89 #pragma GCC system_header
90 #endif
91
92 _LIBCPP_BEGIN_NAMESPACE_STD
93
94 // __static_gcd
95
96 template <intmax_t _Xp, intmax_t _Yp>
97 struct __static_gcd
98 {
99     static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
100 };
101
102 template <intmax_t _Xp>
103 struct __static_gcd<_Xp, 0>
104 {
105     static const intmax_t value = _Xp;
106 };
107
108 template <>
109 struct __static_gcd<0, 0>
110 {
111     static const intmax_t value = 1;
112 };
113
114 // __static_lcm
115
116 template <intmax_t _Xp, intmax_t _Yp>
117 struct __static_lcm
118 {
119     static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
120 };
121
122 template <intmax_t _Xp>
123 struct __static_abs
124 {
125     static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
126 };
127
128 template <intmax_t _Xp>
129 struct __static_sign
130 {
131     static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
132 };
133
134 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
135 class __ll_add;
136
137 template <intmax_t _Xp, intmax_t _Yp>
138 class __ll_add<_Xp, _Yp, 1>
139 {
140     static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
141     static const intmax_t max = -min;
142
143     static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
144 public:
145     static const intmax_t value = _Xp + _Yp;
146 };
147
148 template <intmax_t _Xp, intmax_t _Yp>
149 class __ll_add<_Xp, _Yp, 0>
150 {
151 public:
152     static const intmax_t value = _Xp;
153 };
154
155 template <intmax_t _Xp, intmax_t _Yp>
156 class __ll_add<_Xp, _Yp, -1>
157 {
158     static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
159     static const intmax_t max = -min;
160
161     static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
162 public:
163     static const intmax_t value = _Xp + _Yp;
164 };
165
166 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
167 class __ll_sub;
168
169 template <intmax_t _Xp, intmax_t _Yp>
170 class __ll_sub<_Xp, _Yp, 1>
171 {
172     static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
173     static const intmax_t max = -min;
174
175     static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
176 public:
177     static const intmax_t value = _Xp - _Yp;
178 };
179
180 template <intmax_t _Xp, intmax_t _Yp>
181 class __ll_sub<_Xp, _Yp, 0>
182 {
183 public:
184     static const intmax_t value = _Xp;
185 };
186
187 template <intmax_t _Xp, intmax_t _Yp>
188 class __ll_sub<_Xp, _Yp, -1>
189 {
190     static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
191     static const intmax_t max = -min;
192
193     static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
194 public:
195     static const intmax_t value = _Xp - _Yp;
196 };
197
198 template <intmax_t _Xp, intmax_t _Yp>
199 class __ll_mul
200 {
201     static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
202     static const intmax_t min = nan + 1;
203     static const intmax_t max = -min;
204     static const intmax_t __a_x = __static_abs<_Xp>::value;
205     static const intmax_t __a_y = __static_abs<_Yp>::value;
206
207     static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
208 public:
209     static const intmax_t value = _Xp * _Yp;
210 };
211
212 template <intmax_t _Yp>
213 class __ll_mul<0, _Yp>
214 {
215 public:
216     static const intmax_t value = 0;
217 };
218
219 template <intmax_t _Xp>
220 class __ll_mul<_Xp, 0>
221 {
222 public:
223     static const intmax_t value = 0;
224 };
225
226 template <>
227 class __ll_mul<0, 0>
228 {
229 public:
230     static const intmax_t value = 0;
231 };
232
233 // Not actually used but left here in case needed in future maintenance
234 template <intmax_t _Xp, intmax_t _Yp>
235 class __ll_div
236 {
237     static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
238     static const intmax_t min = nan + 1;
239     static const intmax_t max = -min;
240
241     static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
242 public:
243     static const intmax_t value = _Xp / _Yp;
244 };
245
246 template <intmax_t _Num, intmax_t _Den = 1>
247 class _LIBCPP_TYPE_VIS_ONLY ratio
248 {
249     static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
250     static_assert(_Den != 0, "ratio divide by 0");
251     static_assert(__static_abs<_Den>::value >  0, "ratio denominator is out of range");
252     static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
253     static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
254     static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
255     static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
256 public:
257     static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
258     static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
259
260     typedef ratio<num, den> type;
261 };
262
263 template <intmax_t _Num, intmax_t _Den>
264 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
265
266 template <intmax_t _Num, intmax_t _Den>
267 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
268
269 template <class _Tp>                    struct __is_ratio                     : false_type {};
270 template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type  {};
271
272 typedef ratio<1LL, 1000000000000000000LL> atto;
273 typedef ratio<1LL,    1000000000000000LL> femto;
274 typedef ratio<1LL,       1000000000000LL> pico;
275 typedef ratio<1LL,          1000000000LL> nano;
276 typedef ratio<1LL,             1000000LL> micro;
277 typedef ratio<1LL,                1000LL> milli;
278 typedef ratio<1LL,                 100LL> centi;
279 typedef ratio<1LL,                  10LL> deci;
280 typedef ratio<                 10LL, 1LL> deca;
281 typedef ratio<                100LL, 1LL> hecto;
282 typedef ratio<               1000LL, 1LL> kilo;
283 typedef ratio<            1000000LL, 1LL> mega;
284 typedef ratio<         1000000000LL, 1LL> giga;
285 typedef ratio<      1000000000000LL, 1LL> tera;
286 typedef ratio<   1000000000000000LL, 1LL> peta;
287 typedef ratio<1000000000000000000LL, 1LL> exa;
288
289 template <class _R1, class _R2>
290 struct __ratio_multiply
291 {
292 private:
293     static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
294     static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
295 public:
296     typedef typename ratio
297         <
298             __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
299             __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
300         >::type type;
301 };
302
303 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
304
305 template <class _R1, class _R2> using ratio_multiply
306                                     = typename __ratio_multiply<_R1, _R2>::type;
307
308 #else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
309
310 template <class _R1, class _R2>
311 struct _LIBCPP_TYPE_VIS_ONLY ratio_multiply
312     : public __ratio_multiply<_R1, _R2>::type {};
313
314 #endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
315
316 template <class _R1, class _R2>
317 struct __ratio_divide
318 {
319 private:
320     static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
321     static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
322 public:
323     typedef typename ratio
324         <
325             __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
326             __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
327         >::type type;
328 };
329
330 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
331
332 template <class _R1, class _R2> using ratio_divide
333                                       = typename __ratio_divide<_R1, _R2>::type;
334
335 #else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
336
337 template <class _R1, class _R2>
338 struct _LIBCPP_TYPE_VIS_ONLY ratio_divide
339     : public __ratio_divide<_R1, _R2>::type {};
340
341 #endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
342
343 template <class _R1, class _R2>
344 struct __ratio_add
345 {
346 private:
347     static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
348     static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
349 public:
350     typedef typename ratio_multiply
351         <
352             ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
353             ratio
354             <
355                 __ll_add
356                 <
357                     __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
358                     __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
359                 >::value,
360                 _R2::den
361             >
362         >::type type;
363 };
364
365 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
366
367 template <class _R1, class _R2> using ratio_add
368                                          = typename __ratio_add<_R1, _R2>::type;
369
370 #else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
371
372 template <class _R1, class _R2>
373 struct _LIBCPP_TYPE_VIS_ONLY ratio_add
374     : public __ratio_add<_R1, _R2>::type {};
375
376 #endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
377
378 template <class _R1, class _R2>
379 struct __ratio_subtract
380 {
381 private:
382     static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
383     static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
384 public:
385     typedef typename ratio_multiply
386         <
387             ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
388             ratio
389             <
390                 __ll_sub
391                 <
392                     __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
393                     __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
394                 >::value,
395                 _R2::den
396             >
397         >::type type;
398 };
399
400 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
401
402 template <class _R1, class _R2> using ratio_subtract
403                                     = typename __ratio_subtract<_R1, _R2>::type;
404
405 #else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
406
407 template <class _R1, class _R2>
408 struct _LIBCPP_TYPE_VIS_ONLY ratio_subtract
409     : public __ratio_subtract<_R1, _R2>::type {};
410
411 #endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
412
413 // ratio_equal
414
415 template <class _R1, class _R2>
416 struct _LIBCPP_TYPE_VIS_ONLY ratio_equal
417     : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};
418
419 template <class _R1, class _R2>
420 struct _LIBCPP_TYPE_VIS_ONLY ratio_not_equal
421     : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};
422
423 // ratio_less
424
425 template <class _R1, class _R2, bool _Odd = false,
426           intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
427           intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
428 struct __ratio_less1
429 {
430     static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
431 };
432
433 template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
434 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
435 {
436     static const bool value = false;
437 };
438
439 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
440 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
441 {
442     static const bool value = !_Odd;
443 };
444
445 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
446 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
447 {
448     static const bool value = _Odd;
449 };
450
451 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
452                                                         intmax_t _M2>
453 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
454 {
455     static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
456                                             ratio<_R2::den, _M2>, !_Odd>::value;
457 };
458
459 template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
460                                 intmax_t _S2 = __static_sign<_R2::num>::value>
461 struct __ratio_less
462 {
463     static const bool value = _S1 < _S2;
464 };
465
466 template <class _R1, class _R2>
467 struct __ratio_less<_R1, _R2, 1LL, 1LL>
468 {
469     static const bool value = __ratio_less1<_R1, _R2>::value;
470 };
471
472 template <class _R1, class _R2>
473 struct __ratio_less<_R1, _R2, -1LL, -1LL>
474 {
475     static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
476 };
477
478 template <class _R1, class _R2>
479 struct _LIBCPP_TYPE_VIS_ONLY ratio_less
480     : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};
481
482 template <class _R1, class _R2>
483 struct _LIBCPP_TYPE_VIS_ONLY ratio_less_equal
484     : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};
485
486 template <class _R1, class _R2>
487 struct _LIBCPP_TYPE_VIS_ONLY ratio_greater
488     : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};
489
490 template <class _R1, class _R2>
491 struct _LIBCPP_TYPE_VIS_ONLY ratio_greater_equal
492     : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};
493
494 template <class _R1, class _R2>
495 struct __ratio_gcd
496 {
497     typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
498                   __static_lcm<_R1::den, _R2::den>::value> type;
499 };
500
501 #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
502 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_equal_v
503     = ratio_equal<_R1, _R2>::value;
504
505 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_not_equal_v
506     = ratio_not_equal<_R1, _R2>::value;
507
508 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_less_v
509     = ratio_less<_R1, _R2>::value;
510
511 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_less_equal_v
512     = ratio_less_equal<_R1, _R2>::value;
513
514 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_greater_v
515     = ratio_greater<_R1, _R2>::value;
516
517 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_greater_equal_v
518     = ratio_greater_equal<_R1, _R2>::value;
519 #endif
520
521 _LIBCPP_END_NAMESPACE_STD
522
523 #endif  // _LIBCPP_RATIO