From patchwork Thu Nov 25 20:04:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 48160 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC6293858406 for ; Thu, 25 Nov 2021 20:06:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC6293858406 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1637870798; bh=CyyLL5EguCptAMAukxPYULYK4ML6PQF6MjtyOVmvNcU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=EZj1RryCHanWpSIo+VaFtZo7xQrEyAQzFOp4uzEre4LIEU0Eq0s4TgGKAFvDMRA7i sAYkLKnTRFJaoxYoCFMuzWoV0+8DYLid9GnRLT2ICayclDvVvOKJaWM6XJZIujezLj 2M2ZTTafhfEOFza3v2MZgaguwQvQ5+3BCdcALJeY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8B3663858416 for ; Thu, 25 Nov 2021 20:04:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8B3663858416 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-23-N6ELixOUOxSp1tiZm68Bqw-1; Thu, 25 Nov 2021 15:04:12 -0500 X-MC-Unique: N6ELixOUOxSp1tiZm68Bqw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0029E80BCAD; Thu, 25 Nov 2021 20:04:11 +0000 (UTC) Received: from localhost (unknown [10.33.36.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id 93A3C19C46; Thu, 25 Nov 2021 20:04:10 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Do not use memset in constexpr calls to ranges::fill_n [PR101608] Date: Thu, 25 Nov 2021 20:04:09 +0000 Message-Id: <20211125200409.1448002-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux, pushed to trunk. libstdc++-v3/ChangeLog: PR libstdc++/101608 * include/bits/ranges_algobase.h (__fill_n_fn): Check for constant evaluation before using memset. * testsuite/25_algorithms/fill_n/constrained.cc: Check byte-sized values as well. --- libstdc++-v3/include/bits/ranges_algobase.h | 28 ++++++++++++------- .../25_algorithms/fill_n/constrained.cc | 6 ++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h index c8c4d032983..9929e5e828b 100644 --- a/libstdc++-v3/include/bits/ranges_algobase.h +++ b/libstdc++-v3/include/bits/ranges_algobase.h @@ -527,17 +527,25 @@ namespace ranges if (__n <= 0) return __first; - // TODO: Generalize this optimization to contiguous iterators. - if constexpr (is_pointer_v<_Out> - // Note that __is_byte already implies !is_volatile. - && __is_byte>::__value - && integral<_Tp>) - { - __builtin_memset(__first, static_cast(__value), __n); - return __first + __n; - } - else if constexpr (is_scalar_v<_Tp>) + if constexpr (is_scalar_v<_Tp>) { + // TODO: Generalize this optimization to contiguous iterators. + if constexpr (is_pointer_v<_Out> + // Note that __is_byte already implies !is_volatile. + && __is_byte>::__value + && integral<_Tp>) + { +#ifdef __cpp_lib_is_constant_evaluated + if (!std::is_constant_evaluated()) +#endif + { + __builtin_memset(__first, + static_cast(__value), + __n); + return __first + __n; + } + } + const auto __tmp = __value; for (; __n > 0; --__n, (void)++__first) *__first = __tmp; diff --git a/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc index 6a015d34a89..1d1e1c104d4 100644 --- a/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc +++ b/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc @@ -73,11 +73,12 @@ test01() } } +template constexpr bool test02() { bool ok = true; - int x[6] = { 1, 2, 3, 4, 5, 6 }; + T x[6] = { 1, 2, 3, 4, 5, 6 }; const int y[6] = { 1, 2, 3, 4, 5, 6 }; const int z[6] = { 17, 17, 17, 4, 5, 6 }; @@ -94,5 +95,6 @@ int main() { test01(); - static_assert(test02()); + static_assert(test02()); + static_assert(test02()); // PR libstdc++/101608 }