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