1// ratio -*- C++ -*-
2
3// Copyright (C) 2008-2019 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/ratio
26 *  This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_RATIO
30#define _GLIBCXX_RATIO 1
31
32#pragma GCC system_header
33
34#if __cplusplus < 201103L
35# include <bits/c++0x_warning.h>
36#else
37
38#include <type_traits>
39#include <cstdint>		// intmax_t, uintmax_t
40
41namespace std _GLIBCXX_VISIBILITY(default)
42{
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
44
45  /**
46   * @defgroup ratio Rational Arithmetic
47   * @ingroup utilities
48   *
49   * Compile time representation of finite rational numbers.
50   * @{
51   */
52
53  template<intmax_t _Pn>
54    struct __static_sign
55    : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
56    { };
57
58  template<intmax_t _Pn>
59    struct __static_abs
60    : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
61    { };
62
63  template<intmax_t _Pn, intmax_t _Qn>
64    struct __static_gcd
65    : __static_gcd<_Qn, (_Pn % _Qn)>
66    { };
67
68  template<intmax_t _Pn>
69    struct __static_gcd<_Pn, 0>
70    : integral_constant<intmax_t, __static_abs<_Pn>::value>
71    { };
72
73  template<intmax_t _Qn>
74    struct __static_gcd<0, _Qn>
75    : integral_constant<intmax_t, __static_abs<_Qn>::value>
76    { };
77
78  // Let c = 2^(half # of bits in an intmax_t)
79  // then we find a1, a0, b1, b0 s.t. N = a1*c + a0, M = b1*c + b0
80  // The multiplication of N and M becomes,
81  // N * M = (a1 * b1)c^2 + (a0 * b1 + b0 * a1)c + a0 * b0
82  // Multiplication is safe if each term and the sum of the terms
83  // is representable by intmax_t.
84  template<intmax_t _Pn, intmax_t _Qn>
85    struct __safe_multiply
86    {
87    private:
88      static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
89
90      static const uintmax_t __a0 = __static_abs<_Pn>::value % __c;
91      static const uintmax_t __a1 = __static_abs<_Pn>::value / __c;
92      static const uintmax_t __b0 = __static_abs<_Qn>::value % __c;
93      static const uintmax_t __b1 = __static_abs<_Qn>::value / __c;
94
95      static_assert(__a1 == 0 || __b1 == 0,
96		    "overflow in multiplication");
97      static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1),
98		    "overflow in multiplication");
99      static_assert(__b0 * __a0 <= __INTMAX_MAX__,
100		    "overflow in multiplication");
101      static_assert((__a0 * __b1 + __b0 * __a1) * __c
102		    <= __INTMAX_MAX__ -  __b0 * __a0,
103		    "overflow in multiplication");
104
105    public:
106      static const intmax_t value = _Pn * _Qn;
107    };
108
109  // Some double-precision utilities, where numbers are represented as
110  // __hi*2^(8*sizeof(uintmax_t)) + __lo.
111  template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
112    struct __big_less
113    : integral_constant<bool, (__hi1 < __hi2
114			       || (__hi1 == __hi2 && __lo1 < __lo2))>
115    { };
116
117  template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
118    struct __big_add
119    {
120      static constexpr uintmax_t __lo = __lo1 + __lo2;
121      static constexpr uintmax_t __hi = (__hi1 + __hi2 +
122					 (__lo1 + __lo2 < __lo1)); // carry
123    };
124
125  // Subtract a number from a bigger one.
126  template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
127    struct __big_sub
128    {
129      static_assert(!__big_less<__hi1, __lo1, __hi2, __lo2>::value,
130		    "Internal library error");
131      static constexpr uintmax_t __lo = __lo1 - __lo2;
132      static constexpr uintmax_t __hi = (__hi1 - __hi2 -
133					 (__lo1 < __lo2)); // carry
134    };
135
136  // Same principle as __safe_multiply.
137  template<uintmax_t __x, uintmax_t __y>
138    struct __big_mul
139    {
140    private:
141      static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
142      static constexpr uintmax_t __x0 = __x % __c;
143      static constexpr uintmax_t __x1 = __x / __c;
144      static constexpr uintmax_t __y0 = __y % __c;
145      static constexpr uintmax_t __y1 = __y / __c;
146      static constexpr uintmax_t __x0y0 = __x0 * __y0;
147      static constexpr uintmax_t __x0y1 = __x0 * __y1;
148      static constexpr uintmax_t __x1y0 = __x1 * __y0;
149      static constexpr uintmax_t __x1y1 = __x1 * __y1;
150      static constexpr uintmax_t __mix = __x0y1 + __x1y0; // possible carry...
151      static constexpr uintmax_t __mix_lo = __mix * __c;
152      static constexpr uintmax_t __mix_hi
153      = __mix / __c + ((__mix < __x0y1) ? __c : 0); // ... added here
154      typedef __big_add<__mix_hi, __mix_lo, __x1y1, __x0y0> _Res;
155    public:
156      static constexpr uintmax_t __hi = _Res::__hi;
157      static constexpr uintmax_t __lo = _Res::__lo;
158    };
159
160  // Adapted from __udiv_qrnnd_c in longlong.h
161  // This version assumes that the high bit of __d is 1.
162  template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
163    struct __big_div_impl
164    {
165    private:
166      static_assert(__d >= (uintmax_t(1) << (sizeof(intmax_t) * 8 - 1)),
167		    "Internal library error");
168      static_assert(__n1 < __d, "Internal library error");
169      static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
170      static constexpr uintmax_t __d1 = __d / __c;
171      static constexpr uintmax_t __d0 = __d % __c;
172
173      static constexpr uintmax_t __q1x = __n1 / __d1;
174      static constexpr uintmax_t __r1x = __n1 % __d1;
175      static constexpr uintmax_t __m = __q1x * __d0;
176      static constexpr uintmax_t __r1y = __r1x * __c + __n0 / __c;
177      static constexpr uintmax_t __r1z = __r1y + __d;
178      static constexpr uintmax_t __r1
179      = ((__r1y < __m) ? ((__r1z >= __d) && (__r1z < __m))
180	 ? (__r1z + __d) : __r1z : __r1y) - __m;
181      static constexpr uintmax_t __q1
182      = __q1x - ((__r1y < __m)
183		 ? ((__r1z >= __d) && (__r1z < __m)) ? 2 : 1 : 0);
184      static constexpr uintmax_t __q0x = __r1 / __d1;
185      static constexpr uintmax_t __r0x = __r1 % __d1;
186      static constexpr uintmax_t __n = __q0x * __d0;
187      static constexpr uintmax_t __r0y = __r0x * __c + __n0 % __c;
188      static constexpr uintmax_t __r0z = __r0y + __d;
189      static constexpr uintmax_t __r0
190      = ((__r0y < __n) ? ((__r0z >= __d) && (__r0z < __n))
191	 ? (__r0z + __d) : __r0z : __r0y) - __n;
192      static constexpr uintmax_t __q0
193      = __q0x - ((__r0y < __n) ? ((__r0z >= __d)
194				  && (__r0z < __n)) ? 2 : 1 : 0);
195
196    public:
197      static constexpr uintmax_t __quot = __q1 * __c + __q0;
198      static constexpr uintmax_t __rem = __r0;
199
200    private:
201      typedef __big_mul<__quot, __d> _Prod;
202      typedef __big_add<_Prod::__hi, _Prod::__lo, 0, __rem> _Sum;
203      static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
204		    "Internal library error");
205  };
206
207  template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
208    struct __big_div
209    {
210    private:
211      static_assert(__d != 0, "Internal library error");
212      static_assert(sizeof (uintmax_t) == sizeof (unsigned long long),
213		    "This library calls __builtin_clzll on uintmax_t, which "
214		    "is unsafe on your platform. Please complain to "
215		    "http://gcc.gnu.org/bugzilla/");
216      static constexpr int __shift = __builtin_clzll(__d);
217      static constexpr int __coshift_ = sizeof(uintmax_t) * 8 - __shift;
218      static constexpr int __coshift = (__shift != 0) ? __coshift_ : 0;
219      static constexpr uintmax_t __c1 = uintmax_t(1) << __shift;
220      static constexpr uintmax_t __c2 = uintmax_t(1) << __coshift;
221      static constexpr uintmax_t __new_d = __d * __c1;
222      static constexpr uintmax_t __new_n0 = __n0 * __c1;
223      static constexpr uintmax_t __n1_shifted = (__n1 % __d) * __c1;
224      static constexpr uintmax_t __n0_top = (__shift != 0) ? (__n0 / __c2) : 0;
225      static constexpr uintmax_t __new_n1 = __n1_shifted + __n0_top;
226      typedef __big_div_impl<__new_n1, __new_n0, __new_d> _Res;
227
228    public:
229      static constexpr uintmax_t __quot_hi = __n1 / __d;
230      static constexpr uintmax_t __quot_lo = _Res::__quot;
231      static constexpr uintmax_t __rem = _Res::__rem / __c1;
232
233    private:
234      typedef __big_mul<__quot_lo, __d> _P0;
235      typedef __big_mul<__quot_hi, __d> _P1;
236      typedef __big_add<_P0::__hi, _P0::__lo, _P1::__lo, __rem> _Sum;
237      // No overflow.
238      static_assert(_P1::__hi == 0, "Internal library error");
239      static_assert(_Sum::__hi >= _P0::__hi, "Internal library error");
240      // Matches the input data.
241      static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
242		    "Internal library error");
243      static_assert(__rem < __d, "Internal library error");
244    };
245
246  /**
247   *  @brief Provides compile-time rational arithmetic.
248   *
249   *  This class template represents any finite rational number with a
250   *  numerator and denominator representable by compile-time constants of
251   *  type intmax_t. The ratio is simplified when instantiated.
252   *
253   *  For example:
254   *  @code
255   *    std::ratio<7,-21>::num == -1;
256   *    std::ratio<7,-21>::den == 3;
257   *  @endcode
258   *
259  */
260  template<intmax_t _Num, intmax_t _Den = 1>
261    struct ratio
262    {
263      static_assert(_Den != 0, "denominator cannot be zero");
264      static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
265		    "out of range");
266
267      // Note: sign(N) * abs(N) == N
268      static constexpr intmax_t num =
269        _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
270
271      static constexpr intmax_t den =
272        __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
273
274      typedef ratio<num, den> type;
275    };
276
277  template<intmax_t _Num, intmax_t _Den>
278    constexpr intmax_t ratio<_Num, _Den>::num;
279
280  template<intmax_t _Num, intmax_t _Den>
281    constexpr intmax_t ratio<_Num, _Den>::den;
282
283  template<typename _R1, typename _R2>
284    struct __ratio_multiply
285    {
286    private:
287      static const intmax_t __gcd1 =
288        __static_gcd<_R1::num, _R2::den>::value;
289      static const intmax_t __gcd2 =
290        __static_gcd<_R2::num, _R1::den>::value;
291
292    public:
293      typedef ratio<
294        __safe_multiply<(_R1::num / __gcd1),
295                        (_R2::num / __gcd2)>::value,
296        __safe_multiply<(_R1::den / __gcd2),
297                        (_R2::den / __gcd1)>::value> type;
298
299      static constexpr intmax_t num = type::num;
300      static constexpr intmax_t den = type::den;
301    };
302
303  template<typename _R1, typename _R2>
304    constexpr intmax_t __ratio_multiply<_R1, _R2>::num;
305
306  template<typename _R1, typename _R2>
307    constexpr intmax_t __ratio_multiply<_R1, _R2>::den;
308
309  /// ratio_multiply
310  template<typename _R1, typename _R2>
311    using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
312
313  template<typename _R1, typename _R2>
314    struct __ratio_divide
315    {
316      static_assert(_R2::num != 0, "division by 0");
317
318      typedef typename __ratio_multiply<
319        _R1,
320        ratio<_R2::den, _R2::num>>::type type;
321
322      static constexpr intmax_t num = type::num;
323      static constexpr intmax_t den = type::den;
324    };
325
326  template<typename _R1, typename _R2>
327    constexpr intmax_t __ratio_divide<_R1, _R2>::num;
328
329  template<typename _R1, typename _R2>
330    constexpr intmax_t __ratio_divide<_R1, _R2>::den;
331
332  /// ratio_divide
333  template<typename _R1, typename _R2>
334    using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
335
336  /// ratio_equal
337  template<typename _R1, typename _R2>
338    struct ratio_equal
339    : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
340    { };
341
342  /// ratio_not_equal
343  template<typename _R1, typename _R2>
344    struct ratio_not_equal
345    : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
346    { };
347
348  // Both numbers are positive.
349  template<typename _R1, typename _R2,
350           typename _Left = __big_mul<_R1::num,_R2::den>,
351           typename _Right = __big_mul<_R2::num,_R1::den> >
352    struct __ratio_less_impl_1
353    : integral_constant<bool, __big_less<_Left::__hi, _Left::__lo,
354           _Right::__hi, _Right::__lo>::value>
355    { };
356
357  template<typename _R1, typename _R2,
358	   bool = (_R1::num == 0 || _R2::num == 0
359		   || (__static_sign<_R1::num>::value
360		       != __static_sign<_R2::num>::value)),
361	   bool = (__static_sign<_R1::num>::value == -1
362		   && __static_sign<_R2::num>::value == -1)>
363    struct __ratio_less_impl
364    : __ratio_less_impl_1<_R1, _R2>::type
365    { };
366
367  template<typename _R1, typename _R2>
368    struct __ratio_less_impl<_R1, _R2, true, false>
369    : integral_constant<bool, _R1::num < _R2::num>
370    { };
371
372  template<typename _R1, typename _R2>
373    struct __ratio_less_impl<_R1, _R2, false, true>
374    : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
375           ratio<-_R1::num, _R1::den> >::type
376    { };
377
378  /// ratio_less
379  template<typename _R1, typename _R2>
380    struct ratio_less
381    : __ratio_less_impl<_R1, _R2>::type
382    { };
383
384  /// ratio_less_equal
385  template<typename _R1, typename _R2>
386    struct ratio_less_equal
387    : integral_constant<bool, !ratio_less<_R2, _R1>::value>
388    { };
389
390  /// ratio_greater
391  template<typename _R1, typename _R2>
392    struct ratio_greater
393    : integral_constant<bool, ratio_less<_R2, _R1>::value>
394    { };
395
396  /// ratio_greater_equal
397  template<typename _R1, typename _R2>
398    struct ratio_greater_equal
399    : integral_constant<bool, !ratio_less<_R1, _R2>::value>
400    { };
401
402#if __cplusplus > 201402L
403  template <typename _R1, typename _R2>
404    inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
405  template <typename _R1, typename _R2>
406    inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
407  template <typename _R1, typename _R2>
408    inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
409  template <typename _R1, typename _R2>
410    inline constexpr bool ratio_less_equal_v =
411      ratio_less_equal<_R1, _R2>::value;
412  template <typename _R1, typename _R2>
413    inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
414  template <typename _R1, typename _R2>
415    inline constexpr bool ratio_greater_equal_v
416    = ratio_greater_equal<_R1, _R2>::value;
417#endif // C++17
418
419  template<typename _R1, typename _R2,
420      bool = (_R1::num >= 0),
421      bool = (_R2::num >= 0),
422      bool = ratio_less<ratio<__static_abs<_R1::num>::value, _R1::den>,
423        ratio<__static_abs<_R2::num>::value, _R2::den> >::value>
424    struct __ratio_add_impl
425    {
426    private:
427      typedef typename __ratio_add_impl<
428        ratio<-_R1::num, _R1::den>,
429        ratio<-_R2::num, _R2::den> >::type __t;
430    public:
431      typedef ratio<-__t::num, __t::den> type;
432    };
433
434  // True addition of nonnegative numbers.
435  template<typename _R1, typename _R2, bool __b>
436    struct __ratio_add_impl<_R1, _R2, true, true, __b>
437    {
438    private:
439      static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
440      static constexpr uintmax_t __d2 = _R2::den / __g;
441      typedef __big_mul<_R1::den, __d2> __d;
442      typedef __big_mul<_R1::num, _R2::den / __g> __x;
443      typedef __big_mul<_R2::num, _R1::den / __g> __y;
444      typedef __big_add<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
445      static_assert(__n::__hi >= __x::__hi, "Internal library error");
446      typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
447      static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
448      typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
449      static_assert(__n_final::__rem == 0, "Internal library error");
450      static_assert(__n_final::__quot_hi == 0 &&
451        __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
452      typedef __big_mul<_R1::den / __g2, __d2> __d_final;
453      static_assert(__d_final::__hi == 0 &&
454        __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
455    public:
456      typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
457    };
458
459  template<typename _R1, typename _R2>
460    struct __ratio_add_impl<_R1, _R2, false, true, true>
461    : __ratio_add_impl<_R2, _R1>
462    { };
463
464  // True subtraction of nonnegative numbers yielding a nonnegative result.
465  template<typename _R1, typename _R2>
466    struct __ratio_add_impl<_R1, _R2, true, false, false>
467    {
468    private:
469      static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
470      static constexpr uintmax_t __d2 = _R2::den / __g;
471      typedef __big_mul<_R1::den, __d2> __d;
472      typedef __big_mul<_R1::num, _R2::den / __g> __x;
473      typedef __big_mul<-_R2::num, _R1::den / __g> __y;
474      typedef __big_sub<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
475      typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
476      static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
477      typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
478      static_assert(__n_final::__rem == 0, "Internal library error");
479      static_assert(__n_final::__quot_hi == 0 &&
480        __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
481      typedef __big_mul<_R1::den / __g2, __d2> __d_final;
482      static_assert(__d_final::__hi == 0 &&
483        __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
484    public:
485      typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
486    };
487
488  template<typename _R1, typename _R2>
489    struct __ratio_add
490    {
491      typedef typename __ratio_add_impl<_R1, _R2>::type type;
492      static constexpr intmax_t num = type::num;
493      static constexpr intmax_t den = type::den;
494    };
495
496  template<typename _R1, typename _R2>
497    constexpr intmax_t __ratio_add<_R1, _R2>::num;
498
499  template<typename _R1, typename _R2>
500    constexpr intmax_t __ratio_add<_R1, _R2>::den;
501
502  /// ratio_add
503  template<typename _R1, typename _R2>
504    using ratio_add = typename __ratio_add<_R1, _R2>::type;
505
506  template<typename _R1, typename _R2>
507    struct __ratio_subtract
508    {
509      typedef typename __ratio_add<
510        _R1,
511        ratio<-_R2::num, _R2::den>>::type type;
512
513      static constexpr intmax_t num = type::num;
514      static constexpr intmax_t den = type::den;
515    };
516
517  template<typename _R1, typename _R2>
518    constexpr intmax_t __ratio_subtract<_R1, _R2>::num;
519
520  template<typename _R1, typename _R2>
521    constexpr intmax_t __ratio_subtract<_R1, _R2>::den;
522
523  /// ratio_subtract
524  template<typename _R1, typename _R2>
525    using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
526
527
528  typedef ratio<1,       1000000000000000000> atto;
529  typedef ratio<1,          1000000000000000> femto;
530  typedef ratio<1,             1000000000000> pico;
531  typedef ratio<1,                1000000000> nano;
532  typedef ratio<1,                   1000000> micro;
533  typedef ratio<1,                      1000> milli;
534  typedef ratio<1,                       100> centi;
535  typedef ratio<1,                        10> deci;
536  typedef ratio<                       10, 1> deca;
537  typedef ratio<                      100, 1> hecto;
538  typedef ratio<                     1000, 1> kilo;
539  typedef ratio<                  1000000, 1> mega;
540  typedef ratio<               1000000000, 1> giga;
541  typedef ratio<            1000000000000, 1> tera;
542  typedef ratio<         1000000000000000, 1> peta;
543  typedef ratio<      1000000000000000000, 1> exa;
544
545  /// @} group ratio
546_GLIBCXX_END_NAMESPACE_VERSION
547} // namespace
548
549#endif // C++11
550
551#endif //_GLIBCXX_RATIO
552