[multiple changes]
authorPaolo Carlini <paolo@gcc.gnu.org>
Sat, 5 Jul 2008 10:24:08 +0000 (10:24 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Sat, 5 Jul 2008 10:24:08 +0000 (10:24 +0000)
2008-07-05  Paolo Carlini  <paolo.carlini@oracle.com>

* include/std/ratio: Prefer __INTMAX_MAX__ to INTMAX_MAX (INTMAX_MIN).

2008-07-05  Chris Fairles  <chris.fairles@gmail.com>

        * include/std/ratio: Documentation for std::ratio class. Add conditions
        to ratio_less to prevent overflow.
        * testsuite/20_util/ratio/comparisons/comp2.cc: New.
        * testsuite/20_util/ratio/cons/cons_overflow.cc: Update dg-error line
        numbers.

From-SVN: r137504

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/ratio
libstdc++-v3/testsuite/20_util/ratio/comparisons/comp2.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow.cc

index cae6060..55b9389 100644 (file)
@@ -1,3 +1,15 @@
+2008-07-05  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       * include/std/ratio: Prefer __INTMAX_MAX__ to INTMAX_MAX (INTMAX_MIN).
+
+2008-07-05  Chris Fairles  <chris.fairles@gmail.com>
+
+        * include/std/ratio: Documentation for std::ratio class. Add conditions 
+        to ratio_less to prevent overflow.
+        * testsuite/20_util/ratio/comparisons/comp2.cc: New.
+        * testsuite/20_util/ratio/cons/cons_overflow.cc: Update dg-error line
+        numbers.
+
 2008-07-04  Chris Fairles  <chris.fairles@gmail.com>
 
        * include/std/ratio: New, per N2661.
index 6dcefa5..9980571 100644 (file)
@@ -96,10 +96,10 @@ namespace std
         "overflow in multiplication");
       static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1), 
         "overflow in multiplication");
-      static_assert(__b0 * __a0 <= INTMAX_MAX
+      static_assert(__b0 * __a0 <= __INTMAX_MAX__
         "overflow in multiplication");
       static_assert((__a0 * __b1 + __b0 * __a1) * __c <= 
-        INTMAX_MAX -  __b0 * __a0, "overflow in multiplication");
+        __INTMAX_MAX__ -  __b0 * __a0, "overflow in multiplication");
 
     public:
       static const intmax_t value = _Pn * _Qn;
@@ -108,12 +108,12 @@ namespace std
   // Helpers for __safe_add
   template<intmax_t _Pn, intmax_t _Qn, bool>
     struct __add_overflow_check_impl
-    : integral_constant<bool, (_Pn <= INTMAX_MAX - _Qn)>
+    : integral_constant<bool, (_Pn <= __INTMAX_MAX__ - _Qn)>
     { };
 
   template<intmax_t _Pn, intmax_t _Qn>
     struct __add_overflow_check_impl<_Pn, _Qn, false>
-    : integral_constant<bool, (_Pn >= -INTMAX_MAX - _Qn)>
+    : integral_constant<bool, (_Pn >= -__INTMAX_MAX__ - _Qn)>
     { };
 
   template<intmax_t _Pn, intmax_t _Qn>
@@ -130,12 +130,27 @@ namespace std
       static const intmax_t value = _Pn + _Qn;
     };
 
+  /**
+   *  @brief Provides compile-time rational arithmetic.
+   * 
+   *  This class template represents any finite rational number with a
+   *  numerator and denominator representable by compile-time constants of
+   *  type intmax_t. The ratio is simplified when instantiated.
+   *
+   *  For example:
+   *  @code
+   *    std::ratio<7,-21>::num == -1;
+   *    std::ratio<7,-21>::den == 3;
+   *  @endcode
+   *  
+  */
   template<intmax_t _Num, intmax_t _Den = 1>
     struct ratio
     {
       static_assert(_Den != 0, "denominator cannot be zero");
-      static_assert(_Num > INTMAX_MIN && _Den > INTMAX_MIN, "out of range");
-      
+      static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
+                   "out of range");
+
       // Note: sign(N) * abs(N) == N
       static const intmax_t num =
         _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
@@ -211,10 +226,26 @@ namespace std
     { };
   
   template<typename _R1, typename _R2>
-    struct ratio_less
+    struct __ratio_less_simple_impl
     : integral_constant<bool,
-        (__safe_multiply<_R1::num, _R2::den>::value < 
-         __safe_multiply<_R2::num, _R1::den>::value)>        
+                       (__safe_multiply<_R1::num, _R2::den>::value
+                        < __safe_multiply<_R2::num, _R1::den>::value)>
+    { };
+
+  // If the denominators are equal or the signs differ, we can just compare
+  // numerators, otherwise fallback to the simple cross-multiply method.
+  template<typename _R1, typename _R2>
+    struct __ratio_less_impl
+    : conditional<(_R1::den == _R2::den
+                  || (__static_sign<_R1::num>::value
+                      != __static_sign<_R2::num>::value)),
+      integral_constant<bool, (_R1::num < _R2::num)>,
+      __ratio_less_simple_impl<_R1, _R2>>::type
+    { };
+
+  template<typename _R1, typename _R2>
+    struct ratio_less
+    : __ratio_less_impl<_R1, _R2>::type
     { };
     
   template<typename _R1, typename _R2>
diff --git a/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp2.cc b/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp2.cc
new file mode 100644 (file)
index 0000000..1d378d5
--- /dev/null
@@ -0,0 +1,56 @@
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2008 Free Software Foundation
+//
+// 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
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING.  If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+#include <ratio>
+#include <testsuite_hooks.h>
+
+#ifdef _GLIBCXX_USE_C99_STDINT_TR1
+
+static const std::intmax_t M = INTMAX_MAX;
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+  //no overflow with same denominator
+  VERIFY(( std::ratio_less<std::ratio<M - 2, M>,
+           std::ratio<M - 1, M>>::value == 1 ) );
+  
+  VERIFY(( std::ratio_less<std::ratio<M - 1, M>,
+           std::ratio<M - 2, M>>::value == 0 ) );
+           
+  //no overflow if signs differ
+  VERIFY(( std::ratio_less<std::ratio<-M, M - 1>,
+           std::ratio<M - 1, M - 2>>::value == 1 ) );
+  
+  VERIFY(( std::ratio_less<std::ratio<M - 1, M - 2>,
+           std::ratio<-M, M - 1>>::value == 0 ) );
+}
+
+#endif //_GLIBCXX_USE_C99_STDINT_TR1
+
+int main()
+{
+#ifdef _GLIBCXX_USE_C99_STDINT_TR1
+  test01();
+#endif
+  return 0;
+}
index 26bbfcd..05daee5 100644 (file)
@@ -51,8 +51,8 @@ test04()
 // { dg-error "instantiated from here" "" { target *-*-* } 36 }
 // { dg-error "instantiated from here" "" { target *-*-* } 42 }
 // { dg-error "instantiated from here" "" { target *-*-* } 48 }
-// { dg-error "denominator cannot be zero" "" { target *-*-* } 136 }
-// { dg-error "out of range" "" { target *-*-* } 137 }
+// { dg-error "denominator cannot be zero" "" { target *-*-* } 150 }
+// { dg-error "out of range" "" { target *-*-* } 151 }
 // { dg-excess-errors "In instantiation of" }
 
 #endif //_GLIBCXX_USE_C99_STDINT_TR1