public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [patch] std::experimental::gcd and std::experimental::lcd
Date: Sat, 02 May 2015 16:47:00 -0000	[thread overview]
Message-ID: <20150502164728.GA3618@redhat.com> (raw)
In-Reply-To: <alpine.DEB.2.11.1505021813010.4001@laptop-mg.saclay.inria.fr>

[-- Attachment #1: Type: text/plain, Size: 1504 bytes --]

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 <cmath> for
integral types just uses __builtin_fabs. Maybe it would be better for
gcd() to just use that directly instead of including <cmath> (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 <experimental/numeric> would probably allow <ratio> 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...

<ratio> was added to libstdc++ in 2008 so I think the constexpr
support was not good enough at the time.


[-- Attachment #2: abs.txt --]
[-- Type: text/plain, Size: 1542 bytes --]

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 <experimental/type_traits>
-#include <cmath>
 
 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<typename _Mn, typename _Nn>
     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<typename _Mn, typename _Nn>
     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;
     }
 

  reply	other threads:[~2015-05-02 16:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-02 15:18 Jonathan Wakely
2015-05-02 16:27 ` Marc Glisse
2015-05-02 16:47   ` Jonathan Wakely [this message]
2015-05-02 17:07     ` Marc Glisse
2015-05-02 17:16       ` Jonathan Wakely
2015-05-02 17:14     ` Marc Glisse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150502164728.GA3618@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).