From patchwork Tue Feb 25 20:36:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1244505 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-520110-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=J3hqeG58; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=d6rjikY3; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48RrLx2sQ5z9sPK for ; Wed, 26 Feb 2020 07:36:39 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=inLgNhu3BfqWCFSg pETwzklJ2Ya8DgRG3yr5bHP16Ia67XodmnDNMB51sJdMMwmbMnZyUQfAYpdTdRBL yDFSmx0GB2eqO3MAj5yejxt0DmAh7vrrivg76dV3jFbgE7AbNr7qa9vxVEVox0zA uRUqtxCJvnn8+fddI9xLEWhAdkg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=wVO1iwCad18hMq6AYXD1b3 BXej8=; b=J3hqeG58qT1DM44pSWZF3gCdS6QvFf3b/bT/+B5wDOAHN6JrnDTNau t3/XBrQlYujkqhi3tjijdNnYLucqMCbSIV0BqMmBGv/kcwI7L5d9CR7NP729U1wO Lmse91Yn0YXsAHt2chd5RtaPFVcXBBD16m23GVK8khm7bwieyPXNY= Received: (qmail 119264 invoked by alias); 25 Feb 2020 20:36:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 119250 invoked by uid 89); 25 Feb 2020 20:36:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=26_numerics X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (207.211.31.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 25 Feb 2020 20:36:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582662983; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FDNiCCIQs70qmzy6Fb51PYJnKiiZ3lVHgPjHPD+W7DI=; b=d6rjikY3Qhg7wz/abBWn2y3CEzMvBk2Bm90PjiLj5KdRvx/fcZgLDC9x/P2vwlQLbmJZVI oKqpI3auf4JUUZPe/XmWIlvQPXMrKk2sPiuVfGO/Alxy+CzmTqkir89zyLabbJFqZ5mRLi dn26TUb4c6ILVcigMJMsXtzAidnJ82Y= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-375-fHr3l3mSNSuifkUbi-4RHA-1; Tue, 25 Feb 2020 15:36:16 -0500 Received: by mail-qt1-f198.google.com with SMTP id c22so898707qtn.23 for ; Tue, 25 Feb 2020 12:36:16 -0800 (PST) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id g11sm8136856qtc.48.2020.02.25.12.36.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2020 12:36:13 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: P1645R1 constexpr for algorithms Date: Tue, 25 Feb 2020 15:36:11 -0500 Message-Id: <20200225203611.1207978-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-IsSubscribed: yes This adds constexpr to 11 algorithms defined in as per P1645R1. Tested on x86_64-pc-linux-gnu, OK to commit? libstdc++-v3/ChangeLog: P1645R1 constexpr for 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 + _GLIBCXX20_CONSTEXPR void iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value) { @@ -128,6 +129,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO * @return The final sum. */ template + _GLIBCXX20_CONSTEXPR inline _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) { @@ -154,6 +156,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO * @return The final sum. */ template + _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 + _GLIBCXX20_CONSTEXPR inline _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) @@ -214,6 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO */ template + _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 + _GLIBCXX20_CONSTEXPR _OutputIterator partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) @@ -287,6 +293,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO */ template + _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 + _GLIBCXX20_CONSTEXPR _OutputIterator adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) @@ -369,6 +377,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO */ template + _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 + _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 + _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 + _GLIBCXX20_CONSTEXPR inline typename iterator_traits<_InputIterator>::value_type reduce(_InputIterator __first, _InputIterator __last) { @@ -327,6 +330,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template + _GLIBCXX20_CONSTEXPR _Tp transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init, @@ -369,6 +373,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * elements. */ template + _GLIBCXX20_CONSTEXPR inline _Tp transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) @@ -394,6 +399,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template + _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 + _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 + _GLIBCXX20_CONSTEXPR inline _OutputIterator exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init) @@ -497,6 +505,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template + _GLIBCXX20_CONSTEXPR _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op, @@ -525,6 +534,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template + _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 + _GLIBCXX20_CONSTEXPR inline _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result) @@ -584,6 +595,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template + _GLIBCXX20_CONSTEXPR _OutputIterator transform_exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, @@ -622,6 +634,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template + _GLIBCXX20_CONSTEXPR _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, @@ -655,6 +668,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template + _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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +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());