This shares the code for std::gcd and std::experimental::gcd, and
similarly for lcm.

I realised I'd mixed up the gcd.cc and lcm.cc tests so this patch also
swaps them around.

        * doc/xml/manual/status_cxx2017.xml: Update gcd/lcm status.
        * doc/html/*: Regenerate.
        * include/experimental/numeric (__abs): Move to <numeric>.
        (gcd, lcm): Use __detail::gcd and __detail::lcm.
        * include/std/numeric (__detail::__abs_integral)
        (__detail::__gcd, __detail::__lcm): Define.
        (gcd, lcm): Define for C++17.
        * testsuite/26_numerics/gcd/1.cc: New test.
        * testsuite/26_numerics/lcm/1.cc: New test.
        * testsuite/experimental/numeric/gcd.cc: Swap contents with ...
        * testsuite/experimental/numeric/lcd.cc: ... this.

Tested powerpc64le-linux, committed to trunk.

commit 56efd86de7a7bbcfe43fe1c20979e06eb5e49802
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Mon Oct 3 17:09:10 2016 +0100

    Define std::gcd and std::lcm for C++17
    
        * doc/xml/manual/status_cxx2017.xml: Update gcd/lcm status.
        * doc/html/*: Regenerate.
        * include/experimental/numeric (__abs): Move to <numeric>.
        (gcd, lcm): Use __detail::gcd and __detail::lcm.
        * include/std/numeric (__detail::__abs_integral)
        (__detail::__gcd, __detail::__lcm): Define.
        (gcd, lcm): Define for C++17.
        * testsuite/26_numerics/gcd/1.cc: New test.
        * testsuite/26_numerics/lcm/1.cc: New test.
        * testsuite/experimental/numeric/gcd.cc: Swap contents with ...
        * testsuite/experimental/numeric/lcd.cc: ... this.

diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml 
b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
index feed085..9f47b349 100644
--- a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
+++ b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
@@ -615,14 +615,13 @@ Feature-testing recommendations for C++</link>.
     </row>
 
     <row>
-      <?dbhtml bgcolor="#C8B0B0" ?>
       <entry> Adopt Selected Library Fundamentals V2 Components for C++17 
</entry>
       <entry>
        <link xmlns:xlink="http://www.w3.org/1999/xlink"; 
xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0295r0.pdf";>
        P0295R0
        </link>
       </entry>
-      <entry align="center"> No </entry>
+      <entry align="center"> 7 </entry>
       <entry><code> __cpp_lib_gcd >= 201606 </code>,
             <code> __cpp_lib_lcm >= 201606 </code>
       </entry>
diff --git a/libstdc++-v3/include/experimental/numeric 
b/libstdc++-v3/include/experimental/numeric
index 5089772..6d1dc21 100644
--- a/libstdc++-v3/include/experimental/numeric
+++ b/libstdc++-v3/include/experimental/numeric
@@ -52,44 +52,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #define __cpp_lib_experimental_gcd_lcm 201411
 
-  // std::abs is not constexpr and doesn't support unsigned integers.
-  template<typename _Tp>
-    constexpr
-    enable_if_t<__and_<is_integral<_Tp>, is_signed<_Tp>>::value, _Tp>
-    __abs(_Tp __val)
-    { return __val < 0 ? -__val : __val; }
-
-  template<typename _Tp>
-    constexpr
-    enable_if_t<__and_<is_integral<_Tp>, is_unsigned<_Tp>>::value, _Tp>
-    __abs(_Tp __val)
-    { return __val; }
-
-  // Greatest common divisor
+  /// Greatest common divisor
   template<typename _Mn, typename _Nn>
     constexpr common_type_t<_Mn, _Nn>
     gcd(_Mn __m, _Nn __n)
     {
       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 ? fundamentals_v2::__abs(__n)
-       : __n == 0 ? fundamentals_v2::__abs(__m)
-       : fundamentals_v2::gcd(__n, __m % __n);
+      return std::__detail::__gcd(__m, __n);
     }
 
-  // Least common multiple
+  /// Least common multiple
   template<typename _Mn, typename _Nn>
     constexpr common_type_t<_Mn, _Nn>
     lcm(_Mn __m, _Nn __n)
     {
       static_assert(is_integral<_Mn>::value, "arguments to lcm are integers");
       static_assert(is_integral<_Nn>::value, "arguments to lcm are integers");
-
-      return (__m != 0 && __n != 0)
-       ? (fundamentals_v2::__abs(__m) / fundamentals_v2::gcd(__m, __n))
-         * fundamentals_v2::__abs(__n)
-       : 0;
+      return std::__detail::__lcm(__m, __n);
     }
 
 _GLIBCXX_END_NAMESPACE_VERSION
diff --git a/libstdc++-v3/include/std/numeric b/libstdc++-v3/include/std/numeric
index 47a7cb8..7b1ab98 100644
--- a/libstdc++-v3/include/std/numeric
+++ b/libstdc++-v3/include/std/numeric
@@ -74,4 +74,83 @@
  * math functions.
  */
 
+#if __cplusplus >= 201402L
+#include <type_traits>
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+namespace __detail
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  // std::abs is not constexpr and doesn't support unsigned integers.
+  template<typename _Tp>
+    constexpr
+    enable_if_t<__and_<is_integral<_Tp>, is_signed<_Tp>>::value, _Tp>
+    __abs_integral(_Tp __val)
+    { return __val < 0 ? -__val : __val; }
+
+  template<typename _Tp>
+    constexpr
+    enable_if_t<__and_<is_integral<_Tp>, is_unsigned<_Tp>>::value, _Tp>
+    __abs_integral(_Tp __val)
+    { return __val; }
+
+  template<typename _Mn, typename _Nn>
+    constexpr common_type_t<_Mn, _Nn>
+    __gcd(_Mn __m, _Nn __n)
+    {
+      return __m == 0 ? __detail::__abs_integral(__n)
+       : __n == 0 ? __detail::__abs_integral(__m)
+       : __detail::__gcd(__n, __m % __n);
+    }
+
+  /// Least common multiple
+  template<typename _Mn, typename _Nn>
+    constexpr common_type_t<_Mn, _Nn>
+    __lcm(_Mn __m, _Nn __n)
+    {
+      return (__m != 0 && __n != 0)
+       ? (__detail::__abs_integral(__m) / __detail::__gcd(__m, __n))
+         * __detail::__abs_integral(__n)
+       : 0;
+    }
+
+_GLIBCXX_END_NAMESPACE_VERSION
+}
+
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+#if __cplusplus > 201402L
+
+#define __cpp_lib_gcd 201606
+  /// Greatest common divisor
+  template<typename _Mn, typename _Nn>
+    constexpr common_type_t<_Mn, _Nn>
+    gcd(_Mn __m, _Nn __n)
+    {
+      static_assert(is_integral<_Mn>::value, "arguments to gcd are integers");
+      static_assert(is_integral<_Nn>::value, "arguments to gcd are integers");
+      return __detail::__gcd(__m, __n);
+    }
+
+#define __cpp_lib_lcm 201606
+  /// Least common multiple
+  template<typename _Mn, typename _Nn>
+    constexpr common_type_t<_Mn, _Nn>
+    lcm(_Mn __m, _Nn __n)
+    {
+      static_assert(is_integral<_Mn>::value, "arguments to lcm are integers");
+      static_assert(is_integral<_Nn>::value, "arguments to lcm are integers");
+      return __detail::__lcm(__m, __n);
+    }
+
+#endif // C++17
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
+
+#endif // C++14
+
+
 #endif /* _GLIBCXX_NUMERIC */
diff --git a/libstdc++-v3/testsuite/26_numerics/gcd/1.cc 
b/libstdc++-v3/testsuite/26_numerics/gcd/1.cc
new file mode 100644
index 0000000..8e16257
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/gcd/1.cc
@@ -0,0 +1,44 @@
+// Copyright (C) 2015-2016 Free Software Foundation, Inc.
+//
+// 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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++1z" }
+// { dg-do compile { target c++1z } }
+
+#include <numeric>
+
+#ifndef __cpp_lib_gcd
+# error "Feature-test macro for gcd missing"
+#elif __cpp_lib_gcd != 201606
+# error "Feature-test macro for gcd has wrong value"
+#endif
+
+using std::gcd;
+
+static_assert( gcd(1071, 462) == 21, "" );
+static_assert( gcd(2000, 20) == 20, "" );
+static_assert( gcd(2011, 17) == 1, "GCD of two primes is 1" );
+static_assert( gcd(200, 200) == 200, "GCD of equal numbers is that number" );
+static_assert( gcd(0, 13) == 13, "GCD of any number and 0 is that number" );
+static_assert( gcd(29, 0) == 29, "GCD of any number and 0 is that number" );
+static_assert( gcd(0, 0) == 0, "" );
+
+static_assert(gcd(1u, 2) == 1, "unsigned and signed");
+static_assert(gcd(3, 4u) == 1, "signed and unsigned");
+static_assert(gcd(5u, 6u) == 1, "unsigned and unsigned");
+
+static_assert( std::is_same_v<decltype(gcd(1l, 1)), long> );
+static_assert( std::is_same_v<decltype(gcd(1ul, 1ull)), unsigned long long> );
diff --git a/libstdc++-v3/testsuite/26_numerics/lcm/1.cc 
b/libstdc++-v3/testsuite/26_numerics/lcm/1.cc
new file mode 100644
index 0000000..caa2032
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/lcm/1.cc
@@ -0,0 +1,41 @@
+// Copyright (C) 2015-2016 Free Software Foundation, Inc.
+//
+// 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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++1z" }
+// { dg-do compile { target c++1z } }
+
+#include <numeric>
+
+#ifndef __cpp_lib_lcm
+# error "Feature-test macro for lcm missing"
+#elif __cpp_lib_lcm != 201606
+# error "Feature-test macro for lcm has wrong value"
+#endif
+
+using std::lcm;
+
+static_assert(lcm(21, 6) == 42, "");
+static_assert(lcm(41, 0) == 0, "LCD with zero is zero");
+static_assert(lcm(0, 7) == 0, "LCD with zero is zero");
+static_assert(lcm(0, 0) == 0, "no division by zero");
+
+static_assert(lcm(1u, 2) == 2, "unsigned and signed");
+static_assert(lcm(3, 4u) == 12, "signed and unsigned");
+static_assert(lcm(5u, 6u) == 30, "unsigned and unsigned");
+
+static_assert( std::is_same_v<decltype(lcm(1l, 1)), long> );
+static_assert( std::is_same_v<decltype(lcm(1ul, 1ull)), unsigned long long> );
diff --git a/libstdc++-v3/testsuite/experimental/numeric/gcd.cc 
b/libstdc++-v3/testsuite/experimental/numeric/gcd.cc
index b3345dc..d90c152 100644
--- a/libstdc++-v3/testsuite/experimental/numeric/gcd.cc
+++ b/libstdc++-v3/testsuite/experimental/numeric/gcd.cc
@@ -19,13 +19,16 @@
 
 #include <experimental/numeric>
 
-using std::experimental::fundamentals_v2::lcm;
+using std::experimental::fundamentals_v2::gcd;
 
-static_assert(lcm(21, 6) == 42, "");
-static_assert(lcm(41, 0) == 0, "LCD with zero is zero");
-static_assert(lcm(0, 7) == 0, "LCD with zero is zero");
-static_assert(lcm(0, 0) == 0, "no division by zero");
+static_assert( gcd(1071, 462) == 21, "" );
+static_assert( gcd(2000, 20) == 20, "" );
+static_assert( gcd(2011, 17) == 1, "GCD of two primes is 1" );
+static_assert( gcd(200, 200) == 200, "GCD of equal numbers is that number" );
+static_assert( gcd(0, 13) == 13, "GCD of any number and 0 is that number" );
+static_assert( gcd(29, 0) == 29, "GCD of any number and 0 is that number" );
+static_assert( gcd(0, 0) == 0, "" );
 
-static_assert(lcm(1u, 2) == 2, "unsigned and signed");
-static_assert(lcm(3, 4u) == 12, "signed and unsigned");
-static_assert(lcm(5u, 6u) == 30, "unsigned and unsigned");
+static_assert(gcd(1u, 2) == 1, "unsigned and signed");
+static_assert(gcd(3, 4u) == 1, "signed and unsigned");
+static_assert(gcd(5u, 6u) == 1, "unsigned and unsigned");
diff --git a/libstdc++-v3/testsuite/experimental/numeric/lcm.cc 
b/libstdc++-v3/testsuite/experimental/numeric/lcm.cc
index d90c152..b3345dc 100644
--- a/libstdc++-v3/testsuite/experimental/numeric/lcm.cc
+++ b/libstdc++-v3/testsuite/experimental/numeric/lcm.cc
@@ -19,16 +19,13 @@
 
 #include <experimental/numeric>
 
-using std::experimental::fundamentals_v2::gcd;
+using std::experimental::fundamentals_v2::lcm;
 
-static_assert( gcd(1071, 462) == 21, "" );
-static_assert( gcd(2000, 20) == 20, "" );
-static_assert( gcd(2011, 17) == 1, "GCD of two primes is 1" );
-static_assert( gcd(200, 200) == 200, "GCD of equal numbers is that number" );
-static_assert( gcd(0, 13) == 13, "GCD of any number and 0 is that number" );
-static_assert( gcd(29, 0) == 29, "GCD of any number and 0 is that number" );
-static_assert( gcd(0, 0) == 0, "" );
+static_assert(lcm(21, 6) == 42, "");
+static_assert(lcm(41, 0) == 0, "LCD with zero is zero");
+static_assert(lcm(0, 7) == 0, "LCD with zero is zero");
+static_assert(lcm(0, 0) == 0, "no division by zero");
 
-static_assert(gcd(1u, 2) == 1, "unsigned and signed");
-static_assert(gcd(3, 4u) == 1, "signed and unsigned");
-static_assert(gcd(5u, 6u) == 1, "unsigned and unsigned");
+static_assert(lcm(1u, 2) == 2, "unsigned and signed");
+static_assert(lcm(3, 4u) == 12, "signed and unsigned");
+static_assert(lcm(5u, 6u) == 30, "unsigned and unsigned");

Reply via email to