xref: /llvm-project/libcxx/include/ratio (revision f69585235ec85d54e0f3fc41b2d5700430907f99)
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#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
85#  include <__cxx03/ratio>
86#else
87#  include <__config>
88#  include <__type_traits/integral_constant.h>
89#  include <climits>
90#  include <cstdint>
91#  include <version>
92
93#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
94#    pragma GCC system_header
95#  endif
96
97_LIBCPP_PUSH_MACROS
98#  include <__undef_macros>
99
100_LIBCPP_BEGIN_NAMESPACE_STD
101
102// __static_gcd
103
104template <intmax_t _Xp, intmax_t _Yp>
105inline const intmax_t __static_gcd = __static_gcd<_Yp, _Xp % _Yp>;
106
107template <intmax_t _Xp>
108inline const intmax_t __static_gcd<_Xp, 0> = _Xp;
109
110template <>
111inline const intmax_t __static_gcd<0, 0> = 1;
112
113// __static_lcm
114
115template <intmax_t _Xp, intmax_t _Yp>
116inline const intmax_t __static_lcm = _Xp / __static_gcd<_Xp, _Yp> * _Yp;
117
118template <intmax_t _Xp>
119inline const intmax_t __static_abs = _Xp < 0 ? -_Xp : _Xp;
120
121template <intmax_t _Xp>
122inline const intmax_t __static_sign = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
123
124template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
125class __ll_add;
126
127template <intmax_t _Xp, intmax_t _Yp>
128class __ll_add<_Xp, _Yp, 1> {
129  static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
130  static const intmax_t max = -min;
131
132  static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
133
134public:
135  static const intmax_t value = _Xp + _Yp;
136};
137
138template <intmax_t _Xp, intmax_t _Yp>
139class __ll_add<_Xp, _Yp, 0> {
140public:
141  static const intmax_t value = _Xp;
142};
143
144template <intmax_t _Xp, intmax_t _Yp>
145class __ll_add<_Xp, _Yp, -1> {
146  static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
147  static const intmax_t max = -min;
148
149  static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
150
151public:
152  static const intmax_t value = _Xp + _Yp;
153};
154
155template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
156class __ll_sub;
157
158template <intmax_t _Xp, intmax_t _Yp>
159class __ll_sub<_Xp, _Yp, 1> {
160  static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
161  static const intmax_t max = -min;
162
163  static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
164
165public:
166  static const intmax_t value = _Xp - _Yp;
167};
168
169template <intmax_t _Xp, intmax_t _Yp>
170class __ll_sub<_Xp, _Yp, 0> {
171public:
172  static const intmax_t value = _Xp;
173};
174
175template <intmax_t _Xp, intmax_t _Yp>
176class __ll_sub<_Xp, _Yp, -1> {
177  static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
178  static const intmax_t max = -min;
179
180  static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
181
182public:
183  static const intmax_t value = _Xp - _Yp;
184};
185
186template <intmax_t _Xp, intmax_t _Yp>
187class __ll_mul {
188  static const intmax_t nan   = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
189  static const intmax_t min   = nan + 1;
190  static const intmax_t max   = -min;
191  static const intmax_t __a_x = __static_abs<_Xp>;
192  static const intmax_t __a_y = __static_abs<_Yp>;
193
194  static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
195
196public:
197  static const intmax_t value = _Xp * _Yp;
198};
199
200template <intmax_t _Yp>
201class __ll_mul<0, _Yp> {
202public:
203  static const intmax_t value = 0;
204};
205
206template <intmax_t _Xp>
207class __ll_mul<_Xp, 0> {
208public:
209  static const intmax_t value = 0;
210};
211
212template <>
213class __ll_mul<0, 0> {
214public:
215  static const intmax_t value = 0;
216};
217
218// Not actually used but left here in case needed in future maintenance
219template <intmax_t _Xp, intmax_t _Yp>
220class __ll_div {
221  static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
222  static const intmax_t min = nan + 1;
223  static const intmax_t max = -min;
224
225  static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
226
227public:
228  static const intmax_t value = _Xp / _Yp;
229};
230
231template <intmax_t _Num, intmax_t _Den = 1>
232class _LIBCPP_TEMPLATE_VIS ratio {
233  static_assert(__static_abs<_Num> >= 0, "ratio numerator is out of range");
234  static_assert(_Den != 0, "ratio divide by 0");
235  static_assert(__static_abs<_Den> > 0, "ratio denominator is out of range");
236  static _LIBCPP_CONSTEXPR const intmax_t __na  = __static_abs<_Num>;
237  static _LIBCPP_CONSTEXPR const intmax_t __da  = __static_abs<_Den>;
238  static _LIBCPP_CONSTEXPR const intmax_t __s   = __static_sign<_Num> * __static_sign<_Den>;
239  static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>;
240
241public:
242  static inline _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
243  static inline _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
244
245  typedef ratio<num, den> type;
246};
247
248template <class _Tp>
249inline const bool __is_ratio_v = false;
250
251template <intmax_t _Num, intmax_t _Den>
252inline const bool __is_ratio_v<ratio<_Num, _Den> > = true;
253
254typedef ratio<1LL, 1000000000000000000LL> atto;
255typedef ratio<1LL, 1000000000000000LL> femto;
256typedef ratio<1LL, 1000000000000LL> pico;
257typedef ratio<1LL, 1000000000LL> nano;
258typedef ratio<1LL, 1000000LL> micro;
259typedef ratio<1LL, 1000LL> milli;
260typedef ratio<1LL, 100LL> centi;
261typedef ratio<1LL, 10LL> deci;
262typedef ratio< 10LL, 1LL> deca;
263typedef ratio< 100LL, 1LL> hecto;
264typedef ratio< 1000LL, 1LL> kilo;
265typedef ratio< 1000000LL, 1LL> mega;
266typedef ratio< 1000000000LL, 1LL> giga;
267typedef ratio< 1000000000000LL, 1LL> tera;
268typedef ratio< 1000000000000000LL, 1LL> peta;
269typedef ratio<1000000000000000000LL, 1LL> exa;
270
271template <class _R1, class _R2>
272struct __ratio_multiply {
273private:
274  static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>;
275  static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>;
276
277  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
278  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
279
280public:
281  typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
282                          __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value >::type type;
283};
284
285#  ifndef _LIBCPP_CXX03_LANG
286
287template <class _R1, class _R2>
288using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
289
290#  else // _LIBCPP_CXX03_LANG
291
292template <class _R1, class _R2>
293struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::type {};
294
295#  endif // _LIBCPP_CXX03_LANG
296
297template <class _R1, class _R2>
298struct __ratio_divide {
299private:
300  static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
301  static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
302
303  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
304  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
305
306public:
307  typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
308                          __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::type type;
309};
310
311#  ifndef _LIBCPP_CXX03_LANG
312
313template <class _R1, class _R2>
314using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
315
316#  else // _LIBCPP_CXX03_LANG
317
318template <class _R1, class _R2>
319struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type {};
320
321#  endif // _LIBCPP_CXX03_LANG
322
323template <class _R1, class _R2>
324struct __ratio_add {
325private:
326  static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
327  static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
328
329  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
330  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
331
332public:
333  typedef typename ratio_multiply<
334      ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
335      ratio< __ll_add< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
336                       __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
337             _R2::den > >::type type;
338};
339
340#  ifndef _LIBCPP_CXX03_LANG
341
342template <class _R1, class _R2>
343using ratio_add = typename __ratio_add<_R1, _R2>::type;
344
345#  else // _LIBCPP_CXX03_LANG
346
347template <class _R1, class _R2>
348struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {};
349
350#  endif // _LIBCPP_CXX03_LANG
351
352template <class _R1, class _R2>
353struct __ratio_subtract {
354private:
355  static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
356  static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
357
358  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
359  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
360
361public:
362  typedef typename ratio_multiply<
363      ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
364      ratio< __ll_sub< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
365                       __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
366             _R2::den > >::type type;
367};
368
369#  ifndef _LIBCPP_CXX03_LANG
370
371template <class _R1, class _R2>
372using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
373
374#  else // _LIBCPP_CXX03_LANG
375
376template <class _R1, class _R2>
377struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::type {};
378
379#  endif // _LIBCPP_CXX03_LANG
380
381// ratio_equal
382
383template <class _R1, class _R2>
384struct _LIBCPP_TEMPLATE_VIS ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {
385  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
386  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
387};
388
389template <class _R1, class _R2>
390struct _LIBCPP_TEMPLATE_VIS ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> {
391  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
392  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
393};
394
395// ratio_less
396
397template <class _R1,
398          class _R2,
399          bool _Odd    = false,
400          intmax_t _Q1 = _R1::num / _R1::den,
401          intmax_t _M1 = _R1::num % _R1::den,
402          intmax_t _Q2 = _R2::num / _R2::den,
403          intmax_t _M2 = _R2::num % _R2::den>
404struct __ratio_less1 {
405  static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
406};
407
408template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
409struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> {
410  static const bool value = false;
411};
412
413template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
414struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> {
415  static const bool value = !_Odd;
416};
417
418template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
419struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> {
420  static const bool value = _Odd;
421};
422
423template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2>
424struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> {
425  static const bool value = __ratio_less1<ratio<_R1::den, _M1>, ratio<_R2::den, _M2>, !_Odd>::value;
426};
427
428template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>, intmax_t _S2 = __static_sign<_R2::num> >
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  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
446  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
447};
448
449template <class _R1, class _R2>
450struct _LIBCPP_TEMPLATE_VIS ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> {
451  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
452  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
453};
454
455template <class _R1, class _R2>
456struct _LIBCPP_TEMPLATE_VIS ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> {
457  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
458  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
459};
460
461template <class _R1, class _R2>
462struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> {
463  static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
464  static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
465};
466
467template <class _R1, class _R2>
468using __ratio_gcd _LIBCPP_NODEBUG = ratio<__static_gcd<_R1::num, _R2::num>, __static_lcm<_R1::den, _R2::den> >;
469
470#  if _LIBCPP_STD_VER >= 17
471template <class _R1, class _R2>
472inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
473
474template <class _R1, class _R2>
475inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
476
477template <class _R1, class _R2>
478inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
479
480template <class _R1, class _R2>
481inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
482
483template <class _R1, class _R2>
484inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
485
486template <class _R1, class _R2>
487inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value;
488#  endif
489
490_LIBCPP_END_NAMESPACE_STD
491
492_LIBCPP_POP_MACROS
493
494#  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
495#    include <type_traits>
496#  endif
497#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
498
499#endif // _LIBCPP_RATIO
500