From patchwork Wed Mar 2 15:01:15 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 85091 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id C3EA6B70DA for ; Thu, 3 Mar 2011 02:01:30 +1100 (EST) Received: (qmail 24166 invoked by alias); 2 Mar 2011 15:01:25 -0000 Received: (qmail 24150 invoked by uid 22791); 2 Mar 2011 15:01:24 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from smtp205.alice.it (HELO smtp205.alice.it) (82.57.200.101) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 02 Mar 2011 15:01:19 +0000 Received: from [192.168.1.4] (79.52.240.130) by smtp205.alice.it (8.5.124.08) id 4D0D0038064B978C; Wed, 2 Mar 2011 16:01:16 +0100 Message-ID: <4D6E5BBB.4060302@oracle.com> Date: Wed, 02 Mar 2011 16:01:15 +0100 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.16) Gecko/20101125 SUSE/3.0.11 Thunderbird/3.0.11 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: libstdc++ Subject: [v3] libstdc++/47913 X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi, some improvements from Marc. Tested x86_64-linux, committed to mainline. Paolo. ///////////////////////// 2011-03-02 Marc Glisse PR libstdc++/47913 * include/std/ratio (ratio_add): Avoid denominator overflow. * testsuite/20_util/ratio/operations/47913.cc: New. Index: include/std/ratio =================================================================== --- include/std/ratio (revision 170612) +++ include/std/ratio (working copy) @@ -177,15 +177,19 @@ 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; Index: testsuite/20_util/ratio/operations/47913.cc =================================================================== --- testsuite/20_util/ratio/operations/47913.cc (revision 0) +++ testsuite/20_util/ratio/operations/47913.cc (revision 0) @@ -0,0 +1,42 @@ +// { dg-options "-std=gnu++0x" } +// { dg-require-cstdint "" } + +// Copyright (C) 2011 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 3, 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 COPYING3. If not see +// . + +#include +#include + +// libstdc++/47913 +void test01() +{ + bool test __attribute__((unused)) = true; + using namespace std; + + const intmax_t m = (intmax_t)1 << (4 * sizeof(intmax_t) - 1); + typedef ratio_add, + ratio<1, (m - 3) * (m - 2)> > ra_type; + + VERIFY( ra_type::num == 2 ); + VERIFY( ra_type::den == (m - 1) * (m - 3) ); +} + +int main() +{ + test01(); + return 0; +}