OSDN Git Service

2011-03-14 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / ratio
index 69c0b6e..17d28a1 100644 (file)
@@ -1,6 +1,6 @@
 // ratio -*- C++ -*-
 
-// Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
+// Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the 
@@ -177,15 +177,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct ratio_add
     {
     private:
-      static const intmax_t __gcd =
+      static constexpr intmax_t __gcd =
         __static_gcd<_R1::den, _R2::den>::value;
+      static constexpr intmax_t __n = __safe_add<
+        __safe_multiply<_R1::num, (_R2::den / __gcd)>::value,
+        __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value;
+
+      // The new numerator may have common factors with the denominator,
+      // but they have to also be factors of __gcd.
+      static constexpr intmax_t __gcd2 = __static_gcd<__n, __gcd>::value;
       
     public:
-      typedef ratio<
-        __safe_add<
-          __safe_multiply<_R1::num, (_R2::den / __gcd)>::value,
-          __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value,
-        __safe_multiply<_R1::den, (_R2::den / __gcd)>::value> type;
+      typedef ratio<__n / __gcd2,
+        __safe_multiply<_R1::den / __gcd2, _R2::den / __gcd>::value> type;
 
       static constexpr intmax_t num = type::num;
       static constexpr intmax_t den = type::den;
@@ -274,9 +278,45 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
     { };
 
-  template<typename _R1>
+  // 0 <= _Ri < 1
+  // If one is 0, conclude
+  // Otherwise, x < y iff 1/y < 1/x
+  template<typename _R1, typename _R2>
+    struct __ratio_less_impl_2;
+
+  // _Ri > 0
+  // Compare the integral parts, and remove them if they are equal
+  template<typename _R1, typename _R2, intmax_t __q1 = _R1::num / _R1::den,
+           intmax_t __q2 = _R2::num / _R2::den, bool __eq = (__q1 == __q2)>
     struct __ratio_less_impl_1
-    : integral_constant<bool, _R1::num < _R1::den>
+    : __ratio_less_impl_2<ratio<_R1::num % _R1::den, _R1::den>,
+           ratio<_R2::num % _R2::den, _R2::den> >::type
+    { }; 
+
+  template<typename _R1, typename _R2, intmax_t __q1, intmax_t __q2>
+    struct __ratio_less_impl_1<_R1, _R2, __q1, __q2, false>
+    : integral_constant<bool, (__q1 < __q2) >
+    { };
+
+  template<typename _R1, typename _R2>
+    struct __ratio_less_impl_2
+    : __ratio_less_impl_1<ratio<_R2::den, _R2::num>,
+           ratio<_R1::den, _R1::num> >::type
+    { }; 
+
+  template<intmax_t __d1, typename _R2>
+    struct __ratio_less_impl_2<ratio<0, __d1>, _R2>
+    : integral_constant<bool, true>
+    { }; 
+
+  template<typename _R1, intmax_t __d2>
+    struct __ratio_less_impl_2<_R1, ratio<0, __d2> >
+    : integral_constant<bool, false>
+    { }; 
+
+  template<intmax_t __d1, intmax_t __d2>
+    struct __ratio_less_impl_2<ratio<0, __d1>, ratio<0, __d2> >
+    : integral_constant<bool, false>
     { }; 
 
   template<typename _R1, typename _R2,
@@ -286,7 +326,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
           bool = (__static_sign<_R1::num>::value == -1
                   && __static_sign<_R2::num>::value == -1)>
     struct __ratio_less_impl
-    : __ratio_less_impl_1<typename ratio_divide<_R1, _R2>::type>::type
+    : __ratio_less_impl_1<_R1, _R2>::type
     { };
 
   template<typename _R1, typename _R2>
@@ -296,10 +336,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename _R1, typename _R2>
     struct __ratio_less_impl<_R1, _R2, false, true>
-    : __ratio_less_impl_1<typename ratio_divide<_R2, _R1>::type>::type
+    : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
+           ratio<-_R1::num, _R1::den> >::type
     { };
 
   /// ratio_less
+  // using a continued fraction expansion
   template<typename _R1, typename _R2>
     struct ratio_less
     : __ratio_less_impl<_R1, _R2>::type