From cd6b4a4bca97657af9e08583ac92f1833f6191eb Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 6 Aug 2010 08:58:27 +0000 Subject: ratio (ratio_less): Improve, use ratio_divide to avoid more overflows. 2010-08-06 Paolo Carlini * include/std/ratio (ratio_less): Improve, use ratio_divide to avoid more overflows. * testsuite/20_util/ratio/comparisons/comp1.cc: Extend. * testsuite/20_util/ratio/comparisons/comp2.cc: Likewise. From-SVN: r162941 --- libstdc++-v3/ChangeLog | 7 +++++ libstdc++-v3/include/std/ratio | 33 +++++++++++++--------- .../testsuite/20_util/ratio/comparisons/comp1.cc | 9 ++++-- .../testsuite/20_util/ratio/comparisons/comp2.cc | 9 +++++- 4 files changed, 42 insertions(+), 16 deletions(-) (limited to 'libstdc++-v3') diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 42db596..8cd8bb2 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2010-08-06 Paolo Carlini + + * include/std/ratio (ratio_less): Improve, use ratio_divide to avoid + more overflows. + * testsuite/20_util/ratio/comparisons/comp1.cc: Extend. + * testsuite/20_util/ratio/comparisons/comp2.cc: Likewise. + 2010-08-05 Paolo Carlini * include/tr1_impl/utility (begin, end): Remove per GB 85. diff --git a/libstdc++-v3/include/std/ratio b/libstdc++-v3/include/std/ratio index e169d79..74806c4 100644 --- a/libstdc++-v3/include/std/ratio +++ b/libstdc++-v3/include/std/ratio @@ -233,23 +233,30 @@ namespace std struct ratio_not_equal : integral_constant::value> { }; - + + template + struct __ratio_less_impl_1 + : integral_constant + { }; + + template::value + != __static_sign<_R2::num>::value)), + bool = (__static_sign<_R1::num>::value == -1 + && __static_sign<_R2::num>::value == -1)> + struct __ratio_less_impl + : __ratio_less_impl_1::type>::type + { }; + template - struct __ratio_less_simple_impl - : integral_constant::value - < __safe_multiply<_R2::num, _R1::den>::value)> + struct __ratio_less_impl<_R1, _R2, true, false> + : integral_constant { }; - // If the denominators are equal or the signs differ, we can just compare - // numerators, otherwise fallback to the simple cross-multiply method. template - struct __ratio_less_impl - : conditional<(_R1::den == _R2::den - || (__static_sign<_R1::num>::value - != __static_sign<_R2::num>::value)), - integral_constant, - __ratio_less_simple_impl<_R1, _R2>>::type + struct __ratio_less_impl<_R1, _R2, false, true> + : __ratio_less_impl_1::type>::type { }; /// ratio_less diff --git a/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp1.cc b/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp1.cc index f4354fe..eb1bd21 100644 --- a/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp1.cc +++ b/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp1.cc @@ -1,7 +1,7 @@ // { dg-options "-std=gnu++0x" } // { dg-require-cstdint "" } -// Copyright (C) 2008, 2009 Free Software Foundation +// Copyright (C) 2008, 2009, 2010 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 @@ -45,7 +45,12 @@ test02() VERIFY( (std::ratio_less, std::ratio<1,4>>::value == 0) ); VERIFY( (std::ratio_less, std::ratio<-1,3>>::value == 0) ); - + + VERIFY( (std::ratio_less, std::ratio<-1,4>>::value == 1) ); + VERIFY( (std::ratio_less, std::ratio<0,3>>::value == 0) ); + VERIFY( (std::ratio_less, std::ratio<0,3>>::value == 0) ); + VERIFY( (std::ratio_less, std::ratio<-1,4>>::value == 0) ); + VERIFY( (std::ratio_less_equal, std::ratio<-1,3>>::value == 1) ); VERIFY( ( std::ratio_less_equal, diff --git a/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp2.cc b/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp2.cc index e741c14..151dc64 100644 --- a/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp2.cc +++ b/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp2.cc @@ -1,7 +1,7 @@ // { dg-options "-std=gnu++0x" } // { dg-require-cstdint "" } -// Copyright (C) 2008, 2009 Free Software Foundation +// Copyright (C) 2008, 2009, 2010 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 @@ -41,6 +41,13 @@ test01() VERIFY( (std::ratio_less, std::ratio<-M, M - 1>>::value == 0) ); + + // No overflow + VERIFY( (std::ratio_less, + std::ratio>::value == 1) ); + + VERIFY( (std::ratio_less, + std::ratio<-M, M - 2>>::value == 0) ); } int main() -- cgit v1.1