diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index cae60608575..55b93899b0d 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2008-07-05 Paolo Carlini + + * include/std/ratio: Prefer __INTMAX_MAX__ to INTMAX_MAX (INTMAX_MIN). + +2008-07-05 Chris Fairles + + * 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 * include/std/ratio: New, per N2661. diff --git a/libstdc++-v3/include/std/ratio b/libstdc++-v3/include/std/ratio index 6dcefa5b931..9980571f160 100644 --- a/libstdc++-v3/include/std/ratio +++ b/libstdc++-v3/include/std/ratio @@ -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 struct __add_overflow_check_impl - : integral_constant + : integral_constant { }; template struct __add_overflow_check_impl<_Pn, _Qn, false> - : integral_constant= -INTMAX_MAX - _Qn)> + : integral_constant= -__INTMAX_MAX__ - _Qn)> { }; template @@ -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 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 - struct ratio_less + struct __ratio_less_simple_impl : integral_constant::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 + 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 + { }; + + template + struct ratio_less + : __ratio_less_impl<_R1, _R2>::type { }; template 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 index 00000000000..1d378d52880 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/ratio/comparisons/comp2.cc @@ -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 +#include + +#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>::value == 1 ) ); + + VERIFY(( std::ratio_less, + std::ratio>::value == 0 ) ); + + //no overflow if signs differ + VERIFY(( std::ratio_less, + std::ratio>::value == 1 ) ); + + VERIFY(( std::ratio_less, + 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; +} diff --git a/libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow.cc b/libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow.cc index 26bbfcdec53..05daee5ea32 100644 --- a/libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow.cc +++ b/libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow.cc @@ -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