diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7753c85d585..3e5828154b6 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,17 @@ 2016-10-03 Jonathan Wakely + * doc/xml/manual/status_cxx2017.xml: Update gcd/lcm status. + * doc/html/*: Regenerate. + * include/experimental/numeric (__abs): Move to . + (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 ecc93a947c7..a0a68b10033 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++. P0025R0 - 7 __cpp_lib_clamp >= 201603 Adopt Selected Library Fundamentals V2 Components for C++17 + 7 __cpp_lib_clamp >= 201603 Adopt Selected Library Fundamentals V2 Components for C++17 P0295R0 - No __cpp_lib_gcd >= 201606 , + 7 __cpp_lib_gcd >= 201606 , __cpp_lib_lcm >= 201606 Proposal to Introduce a 3-Argument Overload to std::hypot diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml index feed08514ce..9f47b349834 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++. - Adopt Selected Library Fundamentals V2 Components for C++17 P0295R0 - No + 7 __cpp_lib_gcd >= 201606 , __cpp_lib_lcm >= 201606 diff --git a/libstdc++-v3/include/experimental/numeric b/libstdc++-v3/include/experimental/numeric index 50897722682..6d1dc21ff4d 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 - constexpr - enable_if_t<__and_, is_signed<_Tp>>::value, _Tp> - __abs(_Tp __val) - { return __val < 0 ? -__val : __val; } - - template - constexpr - enable_if_t<__and_, is_unsigned<_Tp>>::value, _Tp> - __abs(_Tp __val) - { return __val; } - - // Greatest common divisor + /// Greatest common divisor template 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 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 47a7cb8e19c..7b1ab986879 100644 --- a/libstdc++-v3/include/std/numeric +++ b/libstdc++-v3/include/std/numeric @@ -74,4 +74,83 @@ * math functions. */ +#if __cplusplus >= 201402L +#include + +namespace std _GLIBCXX_VISIBILITY(default) +{ +namespace __detail +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + // std::abs is not constexpr and doesn't support unsigned integers. + template + constexpr + enable_if_t<__and_, is_signed<_Tp>>::value, _Tp> + __abs_integral(_Tp __val) + { return __val < 0 ? -__val : __val; } + + template + constexpr + enable_if_t<__and_, is_unsigned<_Tp>>::value, _Tp> + __abs_integral(_Tp __val) + { return __val; } + + template + 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 + 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 + 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 + 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 00000000000..8e162571fb3 --- /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 +// . + +// { dg-options "-std=gnu++1z" } +// { dg-do compile { target c++1z } } + +#include + +#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 ); +static_assert( std::is_same_v ); 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 00000000000..caa2032bd66 --- /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 +// . + +// { dg-options "-std=gnu++1z" } +// { dg-do compile { target c++1z } } + +#include + +#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 ); +static_assert( std::is_same_v ); diff --git a/libstdc++-v3/testsuite/experimental/numeric/gcd.cc b/libstdc++-v3/testsuite/experimental/numeric/gcd.cc index b3345dc5e6d..d90c1527541 100644 --- a/libstdc++-v3/testsuite/experimental/numeric/gcd.cc +++ b/libstdc++-v3/testsuite/experimental/numeric/gcd.cc @@ -19,13 +19,16 @@ #include -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 d90c1527541..b3345dc5e6d 100644 --- a/libstdc++-v3/testsuite/experimental/numeric/lcm.cc +++ b/libstdc++-v3/testsuite/experimental/numeric/lcm.cc @@ -19,16 +19,13 @@ #include -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");