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