diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2016-10-03 18:01:10 +0100 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2016-10-03 18:01:10 +0100 |
commit | 606dda21c8bcd4c2574e5b28f8125f01e38955c6 (patch) | |
tree | fbbf846d1547d0889eb640b9c224bef9cb11882b | |
parent | ec9d5ad13bf6a965007d727d0dcbc77155e8132a (diff) | |
download | gcc-606dda21c8bcd4c2574e5b28f8125f01e38955c6.zip gcc-606dda21c8bcd4c2574e5b28f8125f01e38955c6.tar.gz gcc-606dda21c8bcd4c2574e5b28f8125f01e38955c6.tar.bz2 |
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
-rw-r--r-- | libstdc++-v3/ChangeLog | 12 | ||||
-rw-r--r-- | libstdc++-v3/doc/html/manual/status.html | 4 | ||||
-rw-r--r-- | libstdc++-v3/doc/xml/manual/status_cxx2017.xml | 3 | ||||
-rw-r--r-- | libstdc++-v3/include/experimental/numeric | 28 | ||||
-rw-r--r-- | libstdc++-v3/include/std/numeric | 79 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/26_numerics/gcd/1.cc | 44 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/26_numerics/lcm/1.cc | 41 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/experimental/numeric/gcd.cc | 19 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/experimental/numeric/lcm.cc | 19 |
9 files changed, 202 insertions, 47 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7753c85..3e58281 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -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 diff --git a/libstdc++-v3/doc/html/manual/status.html b/libstdc++-v3/doc/html/manual/status.html index ecc93a9..a0a68b1 100644 --- a/libstdc++-v3/doc/html/manual/status.html +++ b/libstdc++-v3/doc/html/manual/status.html @@ -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"> 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"); |