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. From-SVN: r240723
This commit is contained in:
parent
ec9d5ad13b
commit
606dda21c8
@ -1,5 +1,17 @@
|
||||
2016-10-03 Jonathan Wakely <jwakely@redhat.com>
|
||||
|
||||
* 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.
|
||||
|
||||
PR libstdc++/68323
|
||||
PR libstdc++/77794
|
||||
* config/abi/pre/gnu-versioned-namespace.ver: Add exports for
|
||||
|
@ -687,11 +687,11 @@ Feature-testing recommendations for C++</a>.
|
||||
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0025r0.html" target="_top">
|
||||
P0025R0
|
||||
</a>
|
||||
</td><td align="center"> 7 </td><td align="left"><code class="code"> __cpp_lib_clamp >= 201603 </code></td></tr><tr bgcolor="#C8B0B0"><td align="left"> Adopt Selected Library Fundamentals V2 Components for C++17 </td><td align="left">
|
||||
</td><td align="center"> 7 </td><td align="left"><code class="code"> __cpp_lib_clamp >= 201603 </code></td></tr><tr><td align="left"> Adopt Selected Library Fundamentals V2 Components for C++17 </td><td align="left">
|
||||
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0295r0.pdf" target="_top">
|
||||
P0295R0
|
||||
</a>
|
||||
</td><td align="center"> No </td><td align="left"><code class="code"> __cpp_lib_gcd >= 201606 </code>,
|
||||
</td><td align="center"> 7 </td><td align="left"><code class="code"> __cpp_lib_gcd >= 201606 </code>,
|
||||
<code class="code"> __cpp_lib_lcm >= 201606 </code>
|
||||
</td></tr><tr><td align="left"> Proposal to Introduce a 3-Argument Overload to <code class="code">std::hypot</code> </td><td align="left">
|
||||
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0030r1.pdf" target="_top">
|
||||
|
@ -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>
|
||||
|
@ -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
|
||||
|
@ -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 */
|
||||
|
44
libstdc++-v3/testsuite/26_numerics/gcd/1.cc
Normal file
44
libstdc++-v3/testsuite/26_numerics/gcd/1.cc
Normal file
@ -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> );
|
41
libstdc++-v3/testsuite/26_numerics/lcm/1.cc
Normal file
41
libstdc++-v3/testsuite/26_numerics/lcm/1.cc
Normal file
@ -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> );
|
@ -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");
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user