Debian Codegraph @

libcxx/include/ratio

raw
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
16 namespace std
17 {
18
19 template <intmax_t N, intmax_t D = 1>
20 class ratio
21 {
22 public:
23 static constexpr intmax_t num;
24 static constexpr intmax_t den;
25 typedef ratio<num, den> type;
26 };
27
28 // ratio arithmetic
29 template <class R1, class R2> using ratio_add = ...;
30 template <class R1, class R2> using ratio_subtract = ...;
31 template <class R1, class R2> using ratio_multiply = ...;
32 template <class R1, class R2> using ratio_divide = ...;
33
34 // ratio comparison
35 template <class R1, class R2> struct ratio_equal;
36 template <class R1, class R2> struct ratio_not_equal;
37 template <class R1, class R2> struct ratio_less;
38 template <class R1, class R2> struct ratio_less_equal;
39 template <class R1, class R2> struct ratio_greater;
40 template <class R1, class R2> struct ratio_greater_equal;
41
42 // convenience SI typedefs
43 using quecto = ratio <1, 1'000'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported
44 using ronto = ratio <1, 1'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported
45 typedef ratio<1, 1000000000000000000000000> yocto; // not supported
46 typedef ratio<1, 1000000000000000000000> zepto; // not supported
47 typedef ratio<1, 1000000000000000000> atto;
48 typedef ratio<1, 1000000000000000> femto;
49 typedef ratio<1, 1000000000000> pico;
50 typedef ratio<1, 1000000000> nano;
51 typedef ratio<1, 1000000> micro;
52 typedef ratio<1, 1000> milli;
53 typedef ratio<1, 100> centi;
54 typedef ratio<1, 10> deci;
55 typedef ratio< 10, 1> deca;
56 typedef ratio< 100, 1> hecto;
57 typedef ratio< 1000, 1> kilo;
58 typedef ratio< 1000000, 1> mega;
59 typedef ratio< 1000000000, 1> giga;
60 typedef ratio< 1000000000000, 1> tera;
61 typedef ratio< 1000000000000000, 1> peta;
62 typedef ratio< 1000000000000000000, 1> exa;
63 typedef ratio< 1000000000000000000000, 1> zetta; // not supported
64 typedef ratio<1000000000000000000000000, 1> yotta; // not supported
65 using ronna = ratio <1'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported
66 using 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
101 template <intmax_t _Xp, intmax_t _Yp>
102 struct __static_gcd {
103 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
104 };
105
106 template <intmax_t _Xp>
107 struct __static_gcd<_Xp, 0> {
108 static const intmax_t value = _Xp;
109 };
110
111 template <>
112 struct __static_gcd<0, 0> {
113 static const intmax_t value = 1;
114 };
115
116 // __static_lcm
117
118 template <intmax_t _Xp, intmax_t _Yp>
119 struct __static_lcm {
120 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
121 };
122
123 template <intmax_t _Xp>
124 struct __static_abs {
125 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
126 };
127
128 template <intmax_t _Xp>
129 struct __static_sign {
130 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
131 };
132
133 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
134 class __ll_add;
135
136 template <intmax_t _Xp, intmax_t _Yp>
137 class __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
143 public:
144 static const intmax_t value = _Xp + _Yp;
145 };
146
147 template <intmax_t _Xp, intmax_t _Yp>
148 class __ll_add<_Xp, _Yp, 0> {
149 public:
150 static const intmax_t value = _Xp;
151 };
152
153 template <intmax_t _Xp, intmax_t _Yp>
154 class __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
160 public:
161 static const intmax_t value = _Xp + _Yp;
162 };
163
164 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
165 class __ll_sub;
166
167 template <intmax_t _Xp, intmax_t _Yp>
168 class __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
174 public:
175 static const intmax_t value = _Xp - _Yp;
176 };
177
178 template <intmax_t _Xp, intmax_t _Yp>
179 class __ll_sub<_Xp, _Yp, 0> {
180 public:
181 static const intmax_t value = _Xp;
182 };
183
184 template <intmax_t _Xp, intmax_t _Yp>
185 class __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
191 public:
192 static const intmax_t value = _Xp - _Yp;
193 };
194
195 template <intmax_t _Xp, intmax_t _Yp>
196 class __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
205 public:
206 static const intmax_t value = _Xp * _Yp;
207 };
208
209 template <intmax_t _Yp>
210 class __ll_mul<0, _Yp> {
211 public:
212 static const intmax_t value = 0;
213 };
214
215 template <intmax_t _Xp>
216 class __ll_mul<_Xp, 0> {
217 public:
218 static const intmax_t value = 0;
219 };
220
221 template <>
222 class __ll_mul<0, 0> {
223 public:
224 static const intmax_t value = 0;
225 };
226
227 // Not actually used but left here in case needed in future maintenance
228 template <intmax_t _Xp, intmax_t _Yp>
229 class __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
236 public:
237 static const intmax_t value = _Xp / _Yp;
238 };
239
240 template <intmax_t _Num, intmax_t _Den = 1>
241 class _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
250 public:
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
257 template <intmax_t _Num, intmax_t _Den>
258 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
259
260 template <intmax_t _Num, intmax_t _Den>
261 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
262
263 template <class _Tp>
264 struct __is_ratio : false_type {};
265 template <intmax_t _Num, intmax_t _Den>
266 struct __is_ratio<ratio<_Num, _Den> > : true_type {};
267
268 typedef ratio<1LL, 1000000000000000000LL> atto;
269 typedef ratio<1LL, 1000000000000000LL> femto;
270 typedef ratio<1LL, 1000000000000LL> pico;
271 typedef ratio<1LL, 1000000000LL> nano;
272 typedef ratio<1LL, 1000000LL> micro;
273 typedef ratio<1LL, 1000LL> milli;
274 typedef ratio<1LL, 100LL> centi;
275 typedef ratio<1LL, 10LL> deci;
276 typedef ratio< 10LL, 1LL> deca;
277 typedef ratio< 100LL, 1LL> hecto;
278 typedef ratio< 1000LL, 1LL> kilo;
279 typedef ratio< 1000000LL, 1LL> mega;
280 typedef ratio< 1000000000LL, 1LL> giga;
281 typedef ratio< 1000000000000LL, 1LL> tera;
282 typedef ratio< 1000000000000000LL, 1LL> peta;
283 typedef ratio<1000000000000000000LL, 1LL> exa;
284
285 template <class _R1, class _R2>
286 struct __ratio_multiply {
287 private:
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
294 public:
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
301 template <class _R1, class _R2>
302 using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
303
304 #else // _LIBCPP_CXX03_LANG
305
306 template <class _R1, class _R2>
307 struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::type {};
308
309 #endif // _LIBCPP_CXX03_LANG
310
311 template <class _R1, class _R2>
312 struct __ratio_divide {
313 private:
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
320 public:
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
327 template <class _R1, class _R2>
328 using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
329
330 #else // _LIBCPP_CXX03_LANG
331
332 template <class _R1, class _R2>
333 struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type {};
334
335 #endif // _LIBCPP_CXX03_LANG
336
337 template <class _R1, class _R2>
338 struct __ratio_add {
339 private:
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
346 public:
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
356 template <class _R1, class _R2>
357 using ratio_add = typename __ratio_add<_R1, _R2>::type;
358
359 #else // _LIBCPP_CXX03_LANG
360
361 template <class _R1, class _R2>
362 struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {};
363
364 #endif // _LIBCPP_CXX03_LANG
365
366 template <class _R1, class _R2>
367 struct __ratio_subtract {
368 private:
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
375 public:
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
385 template <class _R1, class _R2>
386 using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
387
388 #else // _LIBCPP_CXX03_LANG
389
390 template <class _R1, class _R2>
391 struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::type {};
392
393 #endif // _LIBCPP_CXX03_LANG
394
395 // ratio_equal
396
397 template <class _R1, class _R2>
398 struct _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
403 template <class _R1, class _R2>
404 struct _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
411 template <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>
418 struct __ratio_less1 {
419 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
420 };
421
422 template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
423 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> {
424 static const bool value = false;
425 };
426
427 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
428 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> {
429 static const bool value = !_Odd;
430 };
431
432 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
433 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> {
434 static const bool value = _Odd;
435 };
436
437 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2>
438 struct __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
442 template <class _R1,
443 class _R2,
444 intmax_t _S1 = __static_sign<_R1::num>::value,
445 intmax_t _S2 = __static_sign<_R2::num>::value>
446 struct __ratio_less {
447 static const bool value = _S1 < _S2;
448 };
449
450 template <class _R1, class _R2>
451 struct __ratio_less<_R1, _R2, 1LL, 1LL> {
452 static const bool value = __ratio_less1<_R1, _R2>::value;
453 };
454
455 template <class _R1, class _R2>
456 struct __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
460 template <class _R1, class _R2>
461 struct _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
466 template <class _R1, class _R2>
467 struct _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
472 template <class _R1, class _R2>
473 struct _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
478 template <class _R1, class _R2>
479 struct _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
484 template <class _R1, class _R2>
485 struct __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
490 template <class _R1, class _R2>
491 inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
492
493 template <class _R1, class _R2>
494 inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
495
496 template <class _R1, class _R2>
497 inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
498
499 template <class _R1, class _R2>
500 inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
501
502 template <class _R1, class _R2>
503 inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
504
505 template <class _R1, class _R2>
506 inline 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