This adds constexpr to 11 algorithms defined in <numeric> as per P1645R1.
Tested on x86_64-pc-linux-gnu, OK to commit? libstdc++-v3/ChangeLog: P1645R1 constexpr for <numeric> algorithms * include/bits/stl_numeric.h (iota, accumulate, inner_product, partial_sum, adjacent_difference): Make conditionally constexpr for C++20. * include/std/numeric (reduce, transform_reduce, exclusive_scan, inclusive_scan, transform_exclusive_scan, transform_inclusive_scan): Likewise. Define the feature test macro __cpp_lib_constexpr_numeric. * testsuite/26_numerics/accumulate/constexpr.cc: New test. * testsuite/26_numerics/adjacent_difference/constexpr.cc: Likewise. * testsuite/26_numerics/exclusive_scan/constexpr.cc: Likewise. * testsuite/26_numerics/inclusive_scan/constexpr.cc: Likewise. * testsuite/26_numerics/inner_product/constexpr.cc: Likewise. * testsuite/26_numerics/iota/constexpr.cc: Likewise. * testsuite/26_numerics/partial_sum/constexpr.cc: Likewise. * testsuite/26_numerics/reduce/constexpr.cc: Likewise. * testsuite/26_numerics/transform_exclusive_scan/constexpr.cc: Likewise. * testsuite/26_numerics/transform_inclusive_scan/constexpr.cc: Likewise. * testsuite/26_numerics/transform_reduce/constexpr.cc: Likewise. --- libstdc++-v3/include/bits/stl_numeric.h | 9 +++ libstdc++-v3/include/std/numeric | 18 ++++++ .../26_numerics/accumulate/constexpr.cc | 42 ++++++++++++++ .../adjacent_difference/constexpr.cc | 44 +++++++++++++++ .../26_numerics/exclusive_scan/constexpr.cc | 44 +++++++++++++++ .../26_numerics/inclusive_scan/constexpr.cc | 55 +++++++++++++++++++ .../26_numerics/inner_product/constexpr.cc | 45 +++++++++++++++ .../testsuite/26_numerics/iota/constexpr.cc | 31 +++++++++++ .../26_numerics/partial_sum/constexpr.cc | 44 +++++++++++++++ .../testsuite/26_numerics/reduce/constexpr.cc | 52 ++++++++++++++++++ .../transform_exclusive_scan/constexpr.cc | 33 +++++++++++ .../transform_inclusive_scan/constexpr.cc | 44 +++++++++++++++ .../26_numerics/transform_reduce/constexpr.cc | 55 +++++++++++++++++++ 13 files changed, 516 insertions(+) create mode 100644 libstdc++-v3/testsuite/26_numerics/accumulate/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/adjacent_difference/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/exclusive_scan/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/inclusive_scan/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/inner_product/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/iota/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/partial_sum/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/reduce/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/transform_exclusive_scan/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/transform_inclusive_scan/constexpr.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/transform_reduce/constexpr.cc diff --git a/libstdc++-v3/include/bits/stl_numeric.h b/libstdc++-v3/include/bits/stl_numeric.h index dd4683fe92e..f95c86a0d48 100644 --- a/libstdc++-v3/include/bits/stl_numeric.h +++ b/libstdc++-v3/include/bits/stl_numeric.h @@ -83,6 +83,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @ingroup numeric_ops */ template<typename _ForwardIterator, typename _Tp> + _GLIBCXX20_CONSTEXPR void iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value) { @@ -128,6 +129,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO * @return The final sum. */ template<typename _InputIterator, typename _Tp> + _GLIBCXX20_CONSTEXPR inline _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) { @@ -154,6 +156,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO * @return The final sum. */ template<typename _InputIterator, typename _Tp, typename _BinaryOperation> + _GLIBCXX20_CONSTEXPR inline _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) @@ -182,6 +185,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO * @return The final inner product. */ template<typename _InputIterator1, typename _InputIterator2, typename _Tp> + _GLIBCXX20_CONSTEXPR inline _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) @@ -214,6 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO */ template<typename _InputIterator1, typename _InputIterator2, typename _Tp, typename _BinaryOperation1, typename _BinaryOperation2> + _GLIBCXX20_CONSTEXPR inline _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init, @@ -246,6 +251,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO * @return Iterator pointing just beyond the values written to __result. */ template<typename _InputIterator, typename _OutputIterator> + _GLIBCXX20_CONSTEXPR _OutputIterator partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) @@ -287,6 +293,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO */ template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation> + _GLIBCXX20_CONSTEXPR _OutputIterator partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op) @@ -326,6 +333,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO * DR 539. partial_sum and adjacent_difference should mention requirements */ template<typename _InputIterator, typename _OutputIterator> + _GLIBCXX20_CONSTEXPR _OutputIterator adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) @@ -369,6 +377,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO */ template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation> + _GLIBCXX20_CONSTEXPR _OutputIterator adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op) diff --git a/libstdc++-v3/include/std/numeric b/libstdc++-v3/include/std/numeric index cf35191cb47..8a7830ffd85 100644 --- a/libstdc++-v3/include/std/numeric +++ b/libstdc++-v3/include/std/numeric @@ -249,6 +249,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * arithmetic) the result can be different. */ template<typename _InputIterator, typename _Tp, typename _BinaryOperation> + _GLIBCXX20_CONSTEXPR _Tp reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) @@ -284,6 +285,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * Equivalent to calling `std::reduce(first, last, init, std::plus<>())`. */ template<typename _InputIterator, typename _Tp> + _GLIBCXX20_CONSTEXPR inline _Tp reduce(_InputIterator __first, _InputIterator __last, _Tp __init) { return std::reduce(__first, __last, std::move(__init), plus<>()); } @@ -300,6 +302,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * Equivalent to calling `std::reduce(first, last, T{}, std::plus<>())`. */ template<typename _InputIterator> + _GLIBCXX20_CONSTEXPR inline typename iterator_traits<_InputIterator>::value_type reduce(_InputIterator __first, _InputIterator __last) { @@ -327,6 +330,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template<typename _InputIterator1, typename _InputIterator2, typename _Tp, typename _BinaryOperation1, typename _BinaryOperation2> + _GLIBCXX20_CONSTEXPR _Tp transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init, @@ -369,6 +373,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * elements. */ template<typename _InputIterator1, typename _InputIterator2, typename _Tp> + _GLIBCXX20_CONSTEXPR inline _Tp transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) @@ -394,6 +399,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template<typename _InputIterator, typename _Tp, typename _BinaryOperation, typename _UnaryOperation> + _GLIBCXX20_CONSTEXPR _Tp transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op, _UnaryOperation __unary_op) @@ -436,6 +442,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template<typename _InputIterator, typename _OutputIterator, typename _Tp, typename _BinaryOperation> + _GLIBCXX20_CONSTEXPR _OutputIterator exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, @@ -469,6 +476,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * so the Nth input element is not included. */ template<typename _InputIterator, typename _OutputIterator, typename _Tp> + _GLIBCXX20_CONSTEXPR inline _OutputIterator exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init) @@ -497,6 +505,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _Tp> + _GLIBCXX20_CONSTEXPR _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op, @@ -525,6 +534,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation> + _GLIBCXX20_CONSTEXPR _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op) @@ -557,6 +567,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * so the Nth input element is included. */ template<typename _InputIterator, typename _OutputIterator> + _GLIBCXX20_CONSTEXPR inline _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result) @@ -584,6 +595,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template<typename _InputIterator, typename _OutputIterator, typename _Tp, typename _BinaryOperation, typename _UnaryOperation> + _GLIBCXX20_CONSTEXPR _OutputIterator transform_exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, @@ -622,6 +634,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _UnaryOperation, typename _Tp> + _GLIBCXX20_CONSTEXPR _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, @@ -655,6 +668,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _UnaryOperation> + _GLIBCXX20_CONSTEXPR _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, @@ -676,6 +690,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // @} group numeric_ops +#if __cplusplus > 201703L +#define __cpp_lib_constexpr_numeric 201911L +#endif + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/testsuite/26_numerics/accumulate/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/accumulate/constexpr.cc new file mode 100644 index 00000000000..6fefd5a3f98 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/accumulate/constexpr.cc @@ -0,0 +1,42 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[5] = {1,2,3,4,5}; + auto sum = std::accumulate(x, x+5, 0); + return sum == 15; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[5] = {1,2,3,4,5}; + auto prod = std::accumulate(x, x+5, 1, std::multiplies{}); + return prod == 120; +} + +static_assert(test02()); diff --git a/libstdc++-v3/testsuite/26_numerics/adjacent_difference/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/adjacent_difference/constexpr.cc new file mode 100644 index 00000000000..5f79284e268 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/adjacent_difference/constexpr.cc @@ -0,0 +1,44 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[5] = {1,2,3,4,5}; + int y[5]; + std::adjacent_difference(x, x+5, y); + return y[0] == 1 && y[1] == 1 && y[2] == 1 && y[3] == 1 && y[4] == 1; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[5] = {1,2,3,4,5}; + int y[5]; + std::adjacent_difference(x, x+5, y, std::multiplies{}); + return y[0] == 1 && y[1] == 2 && y[2] == 6 && y[3] == 12 && y[4] == 20; +} + +static_assert(test02()); diff --git a/libstdc++-v3/testsuite/26_numerics/exclusive_scan/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/exclusive_scan/constexpr.cc new file mode 100644 index 00000000000..c885ba5dbd1 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/exclusive_scan/constexpr.cc @@ -0,0 +1,44 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[3] = {1,2,3}; + int y[3]; + std::exclusive_scan(x, x+3, y, 0); + return y[0] == 0 && y[1] == 1 && y[2] == 3; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[3] = {1,2,3}; + int y[3]; + std::exclusive_scan(x, x+3, y, 1, std::multiplies{}); + return y[0] == 1 && y[1] == 1 && y[2] == 2; +} + +static_assert(test02()); diff --git a/libstdc++-v3/testsuite/26_numerics/inclusive_scan/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/inclusive_scan/constexpr.cc new file mode 100644 index 00000000000..a50a70e9fc2 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/inclusive_scan/constexpr.cc @@ -0,0 +1,55 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[3] = {1,2,3}; + int y[3]; + std::inclusive_scan(x, x+3, y); + return y[0] == 1 && y[1] == 3 && y[2] == 6; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[3] = {1,2,3}; + int y[3]; + std::inclusive_scan(x, x+3, y, std::multiplies{}); + return y[0] == 1 && y[1] == 2 && y[2] == 6; +} + +static_assert(test02()); + +constexpr bool +test03() +{ + int x[3] = {1,2,3}; + int y[3]; + std::inclusive_scan(x, x+3, y, std::multiplies{}, 2); + return y[0] == 2 && y[1] == 4 && y[2] == 12; +} + +static_assert(test03()); diff --git a/libstdc++-v3/testsuite/26_numerics/inner_product/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/inner_product/constexpr.cc new file mode 100644 index 00000000000..a6aaf351560 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/inner_product/constexpr.cc @@ -0,0 +1,45 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[5] = {1,2,3,4,5}; + int y[5] = {2,4,6,8,10}; + auto ret = std::inner_product(x, x+5, y, 0); + return ret == 110; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[5] = {1,2,3,4,5}; + int y[5] = {2,4,6,8,10}; + auto ret = std::inner_product(x, x+5, y, 1, + std::multiplies{}, std::plus{}); + return ret == 3*6*9*12*15; +} + +static_assert(test02()); diff --git a/libstdc++-v3/testsuite/26_numerics/iota/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/iota/constexpr.cc new file mode 100644 index 00000000000..50eecf8b605 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/iota/constexpr.cc @@ -0,0 +1,31 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <numeric> + +constexpr bool +test01() +{ + int x[3] = {0}; + std::iota(x, x+3, 0); + return x[0] == 0 && x[1] == 1 && x[2] == 2; +} + +static_assert(test01()); diff --git a/libstdc++-v3/testsuite/26_numerics/partial_sum/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/partial_sum/constexpr.cc new file mode 100644 index 00000000000..c2e758492e4 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/partial_sum/constexpr.cc @@ -0,0 +1,44 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[3] = {1,2,3}; + int y[3]; + std::partial_sum(x, x+3, y); + return y[0] == 1 && y[1] == 3 && y[2] == 6; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[3] = {1,2,3}; + int y[3]; + std::partial_sum(x, x+3, y, std::multiplies{}); + return y[0] == 1 && y[1] == 2 && y[2] == 6; +} + +static_assert(test02()); diff --git a/libstdc++-v3/testsuite/26_numerics/reduce/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/reduce/constexpr.cc new file mode 100644 index 00000000000..5ed48892df2 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/reduce/constexpr.cc @@ -0,0 +1,52 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[5] = {1,2,3,4,5}; + auto sum = std::reduce(x, x+5); + return sum == 15; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[5] = {1,2,3,4,5}; + auto sum = std::reduce(x, x+5, 0); + return sum == 15; +} + +static_assert(test02()); + +constexpr bool +test03() +{ + int x[5] = {1,2,3,4,5}; + auto prod = std::reduce(x, x+5, 1, std::multiplies{}); + return prod == 120; +} + +static_assert(test03()); diff --git a/libstdc++-v3/testsuite/26_numerics/transform_exclusive_scan/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/transform_exclusive_scan/constexpr.cc new file mode 100644 index 00000000000..f01fd241a8a --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/transform_exclusive_scan/constexpr.cc @@ -0,0 +1,33 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[3] = {1,2,3}; + int y[3]; + std::transform_exclusive_scan(x, x+3, y, 0, std::plus{}, std::negate{}); + return y[0] == 0 && y[1] == -1 && y[2] == -3; +} + +static_assert(test01()); diff --git a/libstdc++-v3/testsuite/26_numerics/transform_inclusive_scan/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/transform_inclusive_scan/constexpr.cc new file mode 100644 index 00000000000..6f072b74569 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/transform_inclusive_scan/constexpr.cc @@ -0,0 +1,44 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[3] = {1,2,3}; + int y[3]; + std::transform_inclusive_scan(x, x+3, y, std::plus{}, std::negate{}, 0); + return y[0] == -1 && y[1] == -3 && y[2] == -6; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[3] = {1,2,3}; + int y[3]; + std::transform_inclusive_scan(x, x+3, y, std::plus{}, std::negate{}); + return y[0] == -1 && y[1] == -3 && y[2] == -6; +} + +static_assert(test02()); diff --git a/libstdc++-v3/testsuite/26_numerics/transform_reduce/constexpr.cc b/libstdc++-v3/testsuite/26_numerics/transform_reduce/constexpr.cc new file mode 100644 index 00000000000..eb054c1f8af --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/transform_reduce/constexpr.cc @@ -0,0 +1,55 @@ +// Copyright (C) 2020 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++2a" } +// { dg-do compile { target c++2a } } + +#include <functional> +#include <numeric> + +constexpr bool +test01() +{ + int x[5] = {1,2,3,4,5}; + int y[5] = {2,4,6,8,10}; + auto ret = std::transform_reduce(x, x+5, y, 0); + return ret == 110; +} + +static_assert(test01()); + +constexpr bool +test02() +{ + int x[5] = {1,2,3,4,5}; + int y[5] = {2,4,6,8,10}; + auto ret = std::transform_reduce(x, x+5, y, 1, + std::multiplies{}, std::plus{}); + return ret == 3*6*9*12*15; +} + +static_assert(test02()); + +constexpr bool +test03() +{ + int x[5] = {1,2,3,4,5}; + auto ret = std::transform_reduce(x, x+5, 0, std::plus{}, std::negate{}); + return ret == -15; +} + +static_assert(test03()); -- 2.25.1.291.ge68e29171c