Debian Codegraph @

libcxx/include/__cxx03/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___CXX03_RATIO
11 #define _LIBCPP___CXX03_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 <__cxx03/__config>
85 #include <__cxx03/__type_traits/integral_constant.h>
86 #include <__cxx03/climits>
87 #include <__cxx03/cstdint>
88 #include <__cxx03/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 <__cxx03/__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 const intmax_t __na = __static_abs<_Num>::value;
246 static const intmax_t __da = __static_abs<_Den>::value;
247 static const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
248 static const intmax_t __gcd = __static_gcd<__na, __da>::value;
249
250 public:
251 static const intmax_t num = __s * __na / __gcd;
252 static const intmax_t den = __da / __gcd;
253
254 typedef ratio<num, den> type;
255 };
256
257 template <intmax_t _Num, intmax_t _Den>
258 const intmax_t ratio<_Num, _Den>::num;
259
260 template <intmax_t _Num, intmax_t _Den>
261 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 template <class _R1, class _R2>
300 struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::type {};
301
302 template <class _R1, class _R2>
303 struct __ratio_divide {
304 private:
305 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
306 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
307
308 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
309 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
310
311 public:
312 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
313 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::type type;
314 };
315
316 template <class _R1, class _R2>
317 struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type {};
318
319 template <class _R1, class _R2>
320 struct __ratio_add {
321 private:
322 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
323 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
324
325 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
326 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
327
328 public:
329 typedef typename ratio_multiply<
330 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
331 ratio< __ll_add< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
332 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
333 _R2::den > >::type type;
334 };
335
336 template <class _R1, class _R2>
337 struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {};
338
339 template <class _R1, class _R2>
340 struct __ratio_subtract {
341 private:
342 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
343 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
344
345 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
346 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
347
348 public:
349 typedef typename ratio_multiply<
350 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
351 ratio< __ll_sub< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
352 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
353 _R2::den > >::type type;
354 };
355
356 template <class _R1, class _R2>
357 struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::type {};
358
359 // ratio_equal
360
361 template <class _R1, class _R2>
362 struct _LIBCPP_TEMPLATE_VIS ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {
363 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
364 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
365 };
366
367 template <class _R1, class _R2>
368 struct _LIBCPP_TEMPLATE_VIS ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> {
369 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
370 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
371 };
372
373 // ratio_less
374
375 template <class _R1,
376 class _R2,
377 bool _Odd = false,
378 intmax_t _Q1 = _R1::num / _R1::den,
379 intmax_t _M1 = _R1::num % _R1::den,
380 intmax_t _Q2 = _R2::num / _R2::den,
381 intmax_t _M2 = _R2::num % _R2::den>
382 struct __ratio_less1 {
383 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
384 };
385
386 template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
387 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> {
388 static const bool value = false;
389 };
390
391 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
392 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> {
393 static const bool value = !_Odd;
394 };
395
396 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
397 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> {
398 static const bool value = _Odd;
399 };
400
401 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2>
402 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> {
403 static const bool value = __ratio_less1<ratio<_R1::den, _M1>, ratio<_R2::den, _M2>, !_Odd>::value;
404 };
405
406 template <class _R1,
407 class _R2,
408 intmax_t _S1 = __static_sign<_R1::num>::value,
409 intmax_t _S2 = __static_sign<_R2::num>::value>
410 struct __ratio_less {
411 static const bool value = _S1 < _S2;
412 };
413
414 template <class _R1, class _R2>
415 struct __ratio_less<_R1, _R2, 1LL, 1LL> {
416 static const bool value = __ratio_less1<_R1, _R2>::value;
417 };
418
419 template <class _R1, class _R2>
420 struct __ratio_less<_R1, _R2, -1LL, -1LL> {
421 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
422 };
423
424 template <class _R1, class _R2>
425 struct _LIBCPP_TEMPLATE_VIS ratio_less : _BoolConstant<__ratio_less<_R1, _R2>::value> {
426 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
427 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
428 };
429
430 template <class _R1, class _R2>
431 struct _LIBCPP_TEMPLATE_VIS ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> {
432 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
433 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
434 };
435
436 template <class _R1, class _R2>
437 struct _LIBCPP_TEMPLATE_VIS ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> {
438 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
439 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
440 };
441
442 template <class _R1, class _R2>
443 struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> {
444 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
445 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
446 };
447
448 template <class _R1, class _R2>
449 struct __ratio_gcd {
450 typedef ratio<__static_gcd<_R1::num, _R2::num>::value, __static_lcm<_R1::den, _R2::den>::value> type;
451 };
452
453 _LIBCPP_END_NAMESPACE_STD
454
455 _LIBCPP_POP_MACROS
456
457 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)
458 # include <__cxx03/type_traits>
459 #endif
460
461 #endif // _LIBCPP___CXX03_RATIO