From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6186 invoked by alias); 2 May 2015 16:47:34 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 6074 invoked by uid 89); 2 May 2015 16:47:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Sat, 02 May 2015 16:47:31 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t42GlU8g003796 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Sat, 2 May 2015 12:47:30 -0400 Received: from localhost (ovpn-116-62.ams2.redhat.com [10.36.116.62]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t42GlTHr010845; Sat, 2 May 2015 12:47:29 -0400 Date: Sat, 02 May 2015 16:47:00 -0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org Cc: gcc-patches@gcc.gnu.org Subject: Re: [patch] std::experimental::gcd and std::experimental::lcd Message-ID: <20150502164728.GA3618@redhat.com> References: <20150502151849.GZ3618@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Iv4H2WyLgK50POYH" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-SW-Source: 2015-05/txt/msg00140.txt.bz2 --Iv4H2WyLgK50POYH Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Content-length: 1504 On 02/05/15 18:27 +0200, Marc Glisse wrote: >On Sat, 2 May 2015, Jonathan Wakely wrote: > >>These where simple to implement (almost too simple ... I probably >>got something wrong!) > >I didn't remember that std::abs works for unsigned. It will need more >work for performance, but that can certainly be done later (I didn't >look at the code beyond checking what you meant by "simple"). std::abs seems to work fine for unsigned, the overload in for integral types just uses __builtin_fabs. Maybe it would be better for gcd() to just use that directly instead of including (as attached, which also removes the qualification on the call to gcd because the functions only work for integral types which have no associated namespaces anyway). >>(Apart from using common_type_t, which is easy to change, these >>functions meet the simpler rules for C++11 constexpr, so moving them >>out of would probably allow to be >>greatly simplified. I don't plan on doing that myself any time soon, >>but it would make sense to do it some day.) > >gcd is not really the hard part in ratio. But constexpr should help, >it made sense not to have it in tr1, but I don't remember why we >didn't use it in the more recent changes (2011, the compiler probably >already supported constexpr). Maybe the interesting functions were too >hard to write as one-liners... was added to libstdc++ in 2008 so I think the constexpr support was not good enough at the time. --Iv4H2WyLgK50POYH Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="abs.txt" Content-length: 1542 diff --git a/libstdc++-v3/include/experimental/numeric b/libstdc++-v3/include/experimental/numeric index a11516b..b284110 100644 --- a/libstdc++-v3/include/experimental/numeric +++ b/libstdc++-v3/include/experimental/numeric @@ -40,7 +40,6 @@ #else #include -#include namespace std _GLIBCXX_VISIBILITY(default) { @@ -52,7 +51,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #define __cpp_lib_experimental_gcd_lcm 201411 - // Greatest common divisor + /// Greatest common divisor template constexpr common_type_t<_Mn, _Nn> gcd(_Mn __m, _Nn __n) @@ -60,12 +59,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_integral<_Mn>::value, "arguments to gcd are integers"); static_assert(is_integral<_Nn>::value, "arguments to gcd are integers"); - return __m == 0 ? std::abs(__n) - : __n == 0 ? std::abs(__m) - : fundamentals_v2::gcd(__n, __m % __n); + return __m == 0 ? __builtin_abs(__n) + : __n == 0 ? __builtin_abs(__m) + : gcd(__n, __m % __n); } - // Least common multiple + /// Least common multiple template constexpr common_type_t<_Mn, _Nn> lcm(_Mn __m, _Nn __n) @@ -74,7 +73,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_integral<_Nn>::value, "arguments to lcm are integers"); return (__m != 0 && __n != 0) - ? (std::abs(__m) / fundamentals_v2::gcd(__m, __n)) * std::abs(__n) + ? (__builtin_abs(__m) / gcd(__m, __n)) * __builtin_abs(__n) : 0; } --Iv4H2WyLgK50POYH--