29 #ifndef _GLIBCXX_RATIO
30 #define _GLIBCXX_RATIO 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
41 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 template<
intmax_t _Pn>
57 : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
60 template<intmax_t _Pn>
62 : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
65 template<intmax_t _Pn, intmax_t _Qn>
68 template<intmax_t _Pn, intmax_t _Qn>
70 : __static_gcd<_Qn, (_Pn % _Qn)>
73 template<intmax_t _Pn>
74 struct __static_gcd<_Pn, 0>
75 : integral_constant<intmax_t, __static_abs<_Pn>::value>
78 template<intmax_t _Qn>
79 struct __static_gcd<0, _Qn>
80 : integral_constant<intmax_t, __static_abs<_Qn>::value>
89 template<intmax_t _Pn, intmax_t _Qn>
90 struct __safe_multiply
93 static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
95 static const uintmax_t __a0 = __static_abs<_Pn>::value % __c;
96 static const uintmax_t __a1 = __static_abs<_Pn>::value / __c;
97 static const uintmax_t __b0 = __static_abs<_Qn>::value % __c;
98 static const uintmax_t __b1 = __static_abs<_Qn>::value / __c;
100 static_assert(__a1 == 0 || __b1 == 0,
101 "overflow in multiplication");
102 static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1),
103 "overflow in multiplication");
104 static_assert(__b0 * __a0 <= __INTMAX_MAX__,
105 "overflow in multiplication");
106 static_assert((__a0 * __b1 + __b0 * __a1) * __c
107 <= __INTMAX_MAX__ - __b0 * __a0,
108 "overflow in multiplication");
111 static const intmax_t value = _Pn * _Qn;
116 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
118 : integral_constant<bool, (__hi1 < __hi2
119 || (__hi1 == __hi2 && __lo1 < __lo2))>
122 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
125 static constexpr uintmax_t __lo = __lo1 + __lo2;
126 static constexpr uintmax_t __hi = (__hi1 + __hi2 +
127 (__lo1 + __lo2 < __lo1));
131 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
134 static_assert(!__big_less<__hi1, __lo1, __hi2, __lo2>::value,
135 "Internal library error");
136 static constexpr uintmax_t __lo = __lo1 - __lo2;
137 static constexpr uintmax_t __hi = (__hi1 - __hi2 -
142 template<uintmax_t __x, uintmax_t __y>
146 static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
147 static constexpr uintmax_t __x0 = __x % __c;
148 static constexpr uintmax_t __x1 = __x / __c;
149 static constexpr uintmax_t __y0 = __y % __c;
150 static constexpr uintmax_t __y1 = __y / __c;
151 static constexpr uintmax_t __x0y0 = __x0 * __y0;
152 static constexpr uintmax_t __x0y1 = __x0 * __y1;
153 static constexpr uintmax_t __x1y0 = __x1 * __y0;
154 static constexpr uintmax_t __x1y1 = __x1 * __y1;
155 static constexpr uintmax_t __mix = __x0y1 + __x1y0;
156 static constexpr uintmax_t __mix_lo = __mix * __c;
157 static constexpr uintmax_t __mix_hi
158 = __mix / __c + ((__mix < __x0y1) ? __c : 0);
159 typedef __big_add<__mix_hi, __mix_lo, __x1y1, __x0y0> _Res;
161 static constexpr uintmax_t __hi = _Res::__hi;
162 static constexpr uintmax_t __lo = _Res::__lo;
167 template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
168 struct __big_div_impl
171 static_assert(__d >= (uintmax_t(1) << (sizeof(intmax_t) * 8 - 1)),
172 "Internal library error");
173 static_assert(__n1 < __d, "Internal library error");
174 static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
175 static constexpr uintmax_t __d1 = __d / __c;
176 static constexpr uintmax_t __d0 = __d % __c;
178 static constexpr uintmax_t __q1x = __n1 / __d1;
179 static constexpr uintmax_t __r1x = __n1 % __d1;
180 static constexpr uintmax_t __m = __q1x * __d0;
181 static constexpr uintmax_t __r1y = __r1x * __c + __n0 / __c;
182 static constexpr uintmax_t __r1z = __r1y + __d;
183 static constexpr uintmax_t __r1
184 = ((__r1y < __m) ? ((__r1z >= __d) && (__r1z < __m))
185 ? (__r1z + __d) : __r1z : __r1y) - __m;
186 static constexpr uintmax_t __q1
187 = __q1x - ((__r1y < __m)
188 ? ((__r1z >= __d) && (__r1z < __m)) ? 2 : 1 : 0);
189 static constexpr uintmax_t __q0x = __r1 / __d1;
190 static constexpr uintmax_t __r0x = __r1 % __d1;
191 static constexpr uintmax_t __n = __q0x * __d0;
192 static constexpr uintmax_t __r0y = __r0x * __c + __n0 % __c;
193 static constexpr uintmax_t __r0z = __r0y + __d;
194 static constexpr uintmax_t __r0
195 = ((__r0y < __n) ? ((__r0z >= __d) && (__r0z < __n))
196 ? (__r0z + __d) : __r0z : __r0y) - __n;
197 static constexpr uintmax_t __q0
198 = __q0x - ((__r0y < __n) ? ((__r0z >= __d)
199 && (__r0z < __n)) ? 2 : 1 : 0);
202 static constexpr uintmax_t __quot = __q1 * __c + __q0;
203 static constexpr uintmax_t __rem = __r0;
206 typedef __big_mul<__quot, __d> _Prod;
207 typedef __big_add<_Prod::__hi, _Prod::__lo, 0, __rem> _Sum;
208 static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
209 "Internal library error");
212 template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
216 static_assert(__d != 0, "Internal library error");
217 static_assert(sizeof (uintmax_t) == sizeof (unsigned long long),
218 "This library calls __builtin_clzll on uintmax_t, which "
219 "is unsafe on your platform. Please complain to "
221 static constexpr int __shift = __builtin_clzll(__d);
222 static constexpr int __coshift_ = sizeof(uintmax_t) * 8 - __shift;
223 static constexpr int __coshift = (__shift != 0) ? __coshift_ : 0;
224 static constexpr uintmax_t __c1 = uintmax_t(1) << __shift;
225 static constexpr uintmax_t __c2 = uintmax_t(1) << __coshift;
226 static constexpr uintmax_t __new_d = __d * __c1;
227 static constexpr uintmax_t __new_n0 = __n0 * __c1;
228 static constexpr uintmax_t __n1_shifted = (__n1 % __d) * __c1;
229 static constexpr uintmax_t __n0_top = (__shift != 0) ? (__n0 / __c2) : 0;
230 static constexpr uintmax_t __new_n1 = __n1_shifted + __n0_top;
231 typedef __big_div_impl<__new_n1, __new_n0, __new_d> _Res;
234 static constexpr uintmax_t __quot_hi = __n1 / __d;
235 static constexpr uintmax_t __quot_lo = _Res::__quot;
236 static constexpr uintmax_t __rem = _Res::__rem / __c1;
239 typedef __big_mul<__quot_lo, __d> _P0;
240 typedef __big_mul<__quot_hi, __d> _P1;
241 typedef __big_add<_P0::__hi, _P0::__lo, _P1::__lo, __rem> _Sum;
243 static_assert(_P1::__hi == 0, "Internal library error");
244 static_assert(_Sum::__hi >= _P0::__hi, "Internal library error");
246 static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
247 "Internal library error");
248 static_assert(__rem < __d, "Internal library error");
265 template<intmax_t _Num, intmax_t _Den = 1>
268 static_assert(_Den != 0, "denominator cannot be zero");
269 static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
273 static constexpr intmax_t num =
274 _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
276 static constexpr intmax_t den =
277 __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
279 typedef ratio<num, den> type;
282 template<intmax_t _Num, intmax_t _Den>
283 constexpr intmax_t ratio<_Num, _Den>::num;
285 template<intmax_t _Num, intmax_t _Den>
286 constexpr intmax_t ratio<_Num, _Den>::den;
289 template<typename _R1, typename _R2>
290 struct ratio_multiply
293 static const intmax_t __gcd1 =
294 __static_gcd<_R1::num, _R2::den>::value;
295 static const intmax_t __gcd2 =
296 __static_gcd<_R2::num, _R1::den>::value;
300 __safe_multiply<(_R1::num / __gcd1),
301 (_R2::num / __gcd2)>::value,
302 __safe_multiply<(_R1::den / __gcd2),
303 (_R2::den / __gcd1)>::value> type;
305 static constexpr intmax_t num = type::num;
306 static constexpr intmax_t den = type::den;
309 template<typename _R1, typename _R2>
310 constexpr intmax_t ratio_multiply<_R1, _R2>::num;
312 template<typename _R1, typename _R2>
313 constexpr intmax_t ratio_multiply<_R1, _R2>::den;
316 template<typename _R1, typename _R2>
319 static_assert(_R2::num != 0, "division by 0");
321 typedef typename ratio_multiply<
323 ratio<_R2::den, _R2::num>>::type type;
325 static constexpr intmax_t num = type::num;
326 static constexpr intmax_t den = type::den;
329 template<typename _R1, typename _R2>
330 constexpr intmax_t ratio_divide<_R1, _R2>::num;
332 template<typename _R1, typename _R2>
333 constexpr intmax_t ratio_divide<_R1, _R2>::den;
336 template<typename _R1, typename _R2>
338 : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
342 template<typename _R1, typename _R2>
343 struct ratio_not_equal
344 : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
348 template<typename _R1, typename _R2,
349 typename _Left = __big_mul<_R1::num,_R2::den>,
350 typename _Right = __big_mul<_R2::num,_R1::den> >
351 struct __ratio_less_impl_1
352 : integral_constant<bool, __big_less<_Left::__hi, _Left::__lo,
353 _Right::__hi, _Right::__lo>::value>
356 template<typename _R1, typename _R2,
357 bool = (_R1::num == 0 || _R2::num == 0
358 || (__static_sign<_R1::num>::value
359 != __static_sign<_R2::num>::value)),
360 bool = (__static_sign<_R1::num>::value == -1
361 && __static_sign<_R2::num>::value == -1)>
362 struct __ratio_less_impl
363 : __ratio_less_impl_1<_R1, _R2>::type
366 template<typename _R1, typename _R2>
367 struct __ratio_less_impl<_R1, _R2, true, false>
368 : integral_constant<bool, _R1::num < _R2::num>
371 template<typename _R1, typename _R2>
372 struct __ratio_less_impl<_R1, _R2, false, true>
373 : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
374 ratio<-_R1::num, _R1::den> >::type
378 template<typename _R1, typename _R2>
380 : __ratio_less_impl<_R1, _R2>::type
384 template<typename _R1, typename _R2>
385 struct ratio_less_equal
386 : integral_constant<bool, !ratio_less<_R2, _R1>::value>
390 template<typename _R1, typename _R2>
392 : integral_constant<bool, ratio_less<_R2, _R1>::value>
396 template<typename _R1, typename _R2>
397 struct ratio_greater_equal
398 : integral_constant<bool, !ratio_less<_R1, _R2>::value>
401 template<typename _R1, typename _R2,
402 bool = (_R1::num >= 0),
403 bool = (_R2::num >= 0),
404 bool = ratio_less<ratio<__static_abs<_R1::num>::value, _R1::den>,
405 ratio<__static_abs<_R2::num>::value, _R2::den> >::value>
406 struct __ratio_add_impl
409 typedef typename __ratio_add_impl<
410 ratio<-_R1::num, _R1::den>,
411 ratio<-_R2::num, _R2::den> >::type __t;
413 typedef ratio<-__t::num, __t::den> type;
417 template<typename _R1, typename _R2, bool __b>
418 struct __ratio_add_impl<_R1, _R2, true, true, __b>
421 static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
422 static constexpr uintmax_t __d2 = _R2::den / __g;
423 typedef __big_mul<_R1::den, __d2> __d;
424 typedef __big_mul<_R1::num, _R2::den / __g> __x;
425 typedef __big_mul<_R2::num, _R1::den / __g> __y;
426 typedef __big_add<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
427 static_assert(__n::__hi >= __x::__hi, "Internal library error");
428 typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
429 static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
430 typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
431 static_assert(__n_final::__rem == 0, "Internal library error");
432 static_assert(__n_final::__quot_hi == 0 &&
433 __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
434 typedef __big_mul<_R1::den / __g2, __d2> __d_final;
435 static_assert(__d_final::__hi == 0 &&
436 __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
438 typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
441 template<typename _R1, typename _R2>
442 struct __ratio_add_impl<_R1, _R2, false, true, true>
443 : __ratio_add_impl<_R2, _R1>
447 template<typename _R1, typename _R2>
448 struct __ratio_add_impl<_R1, _R2, true, false, false>
451 static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
452 static constexpr uintmax_t __d2 = _R2::den / __g;
453 typedef __big_mul<_R1::den, __d2> __d;
454 typedef __big_mul<_R1::num, _R2::den / __g> __x;
455 typedef __big_mul<-_R2::num, _R1::den / __g> __y;
456 typedef __big_sub<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
457 typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
458 static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
459 typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
460 static_assert(__n_final::__rem == 0, "Internal library error");
461 static_assert(__n_final::__quot_hi == 0 &&
462 __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
463 typedef __big_mul<_R1::den / __g2, __d2> __d_final;
464 static_assert(__d_final::__hi == 0 &&
465 __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
467 typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
471 template<typename _R1, typename _R2>
474 typedef typename __ratio_add_impl<_R1, _R2>::type type;
475 static constexpr intmax_t num = type::num;
476 static constexpr intmax_t den = type::den;
479 template<typename _R1, typename _R2>
480 constexpr intmax_t ratio_add<_R1, _R2>::num;
482 template<typename _R1, typename _R2>
483 constexpr intmax_t ratio_add<_R1, _R2>::den;
486 template<typename _R1, typename _R2>
487 struct ratio_subtract
489 typedef typename ratio_add<
491 ratio<-_R2::num, _R2::den>>::type type;
493 static constexpr intmax_t num = type::num;
494 static constexpr intmax_t den = type::den;
497 template<typename _R1, typename _R2>
498 constexpr intmax_t ratio_subtract<_R1, _R2>::num;
500 template<typename _R1, typename _R2>
501 constexpr intmax_t ratio_subtract<_R1, _R2>::den;
505 typedef ratio<1, 1000000000000000000> atto;
506 typedef ratio<1, 1000000000000000> femto;
507 typedef ratio<1, 1000000000000> pico;
508 typedef ratio<1, 1000000000> nano;
509 typedef ratio<1, 1000000> micro;
510 typedef ratio<1, 1000> milli;
511 typedef ratio<1, 100> centi;
512 typedef ratio<1, 10> deci;
513 typedef ratio< 10, 1> deca;
514 typedef ratio< 100, 1> hecto;
515 typedef ratio< 1000, 1> kilo;
516 typedef ratio< 1000000, 1> mega;
517 typedef ratio< 1000000000, 1> giga;
518 typedef ratio< 1000000000000, 1> tera;
519 typedef ratio< 1000000000000000, 1> peta;
520 typedef ratio< 1000000000000000000, 1> exa;
523 _GLIBCXX_END_NAMESPACE_VERSION