From patchwork Sun Oct 13 18:35:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1996547 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=arqi355j; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XRTbj3VPWz1xvC for ; Mon, 14 Oct 2024 05:36:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CF662385AE55 for ; Sun, 13 Oct 2024 18:36:44 +0000 (GMT) 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 ESMTP id EE71B385AE7A for ; Sun, 13 Oct 2024 18:36:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE71B385AE7A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EE71B385AE7A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728844569; cv=none; b=b7waO2NurLVkPu4HWMFOoXVJCLLb2pBRvhSk6izLtqfAZkMYSfljQK3uktFDLNR1jV40W6ii2rtB34u5eiPX+kfgagI5SdmjqpKYDqhDc0/pmMySUErV8E+00tZHSPbn9a/DtcW9ZIUc0XxdPFQxXJrxQeKePHX3Od/IoaARXjk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728844569; c=relaxed/simple; bh=VbCF6wh86tUSAk2P8dMUEEKTCbf4xVawIOyLI++Ahgg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JovxOQC84FX6bDLjDK7O3xJUMYoDhR4x4owoNTUXTtrgTsPDG9yu/57G6rkiFMQIp9G3QSAl/wpS02IOJJ1yWndENsisWJzUlVewuNyIz3xFyzggiVHVMPvxA8tQ30nmveWggiFACTB3d07PqYCD7DQsmCz118MBqGOHIT2OprY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728844565; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=piHlnw+bxH6xmrz++CfJ6+eQsRElK0eSM3XZ8lYmDEg=; b=arqi355j5HZnP+tockBKGUUzc3h/93rr7qHiD1ovvTaq+AnBZ1pR/CTKwAozFXGtIASuSS se03TQmUZEcN/A7IqhCDTHBqbTwpMc5OGTnME2WOORDFcXOkZRWioyI6g2dOUgD6DpjvER Y3//QFd0y1HcW5MftzeQz4WKkajI3fw= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-641-Yl4ktHI1M5ivnvBa2XM0eQ-1; Sun, 13 Oct 2024 14:36:04 -0400 X-MC-Unique: Yl4ktHI1M5ivnvBa2XM0eQ-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 071A719560A6; Sun, 13 Oct 2024 18:36:03 +0000 (UTC) Received: from localhost (unknown [10.42.28.16]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 135D93000198; Sun, 13 Oct 2024 18:36:01 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix ranges::copy_backward for a single memcpyable element [PR117121] Date: Sun, 13 Oct 2024 19:35:44 +0100 Message-ID: <20241013183600.1649098-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 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_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. -- >8 -- The result iterator needs to be decremented before writing to it. Improve the PR 108846 tests for all of std::copy, std::copy_n, std::copy_backward, and the std::ranges versions. libstdc++-v3/ChangeLog: PR libstdc++/117121 * include/bits/ranges_algobase.h (copy_backward): Decrement output iterator before assigning one element through it. * testsuite/25_algorithms/copy/108846.cc: Ensure the algorithm's effects are correct for a single memcpyable element. * testsuite/25_algorithms/copy_backward/108846.cc: Likewise. * testsuite/25_algorithms/copy_n/108846.cc: Likewise. --- libstdc++-v3/include/bits/ranges_algobase.h | 5 +++-- .../testsuite/25_algorithms/copy/108846.cc | 15 +++++++++++++++ .../25_algorithms/copy_backward/108846.cc | 15 +++++++++++++++ .../testsuite/25_algorithms/copy_n/108846.cc | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h index 40c628b3818..3c8d46198c5 100644 --- a/libstdc++-v3/include/bits/ranges_algobase.h +++ b/libstdc++-v3/include/bits/ranges_algobase.h @@ -418,12 +418,13 @@ namespace ranges { using _ValueTypeI = iter_value_t<_Iter>; auto __num = __last - __first; + __result -= __num; if (__num > 1) [[likely]] - __builtin_memmove(__result - __num, __first, + __builtin_memmove(__result, __first, sizeof(_ValueTypeI) * __num); else if (__num == 1) ranges::__assign_one<_IsMove>(__first, __result); - return {__first + __num, __result - __num}; + return {__first + __num, __result}; } } diff --git a/libstdc++-v3/testsuite/25_algorithms/copy/108846.cc b/libstdc++-v3/testsuite/25_algorithms/copy/108846.cc index e3b722c068a..a283e6fcd9f 100644 --- a/libstdc++-v3/testsuite/25_algorithms/copy/108846.cc +++ b/libstdc++-v3/testsuite/25_algorithms/copy/108846.cc @@ -25,10 +25,15 @@ test_pr108846() B *src = &dsrc; // If this is optimized to memmove it will overwrite tail padding. std::copy(src, src+1, dst); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy(src, src+1, dst); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif } @@ -52,10 +57,15 @@ test_non_const_copy_assign() B2 *src = &dsrc; // Ensure the not-taken trivial copy path works for this type. std::copy(src, src+1, dst); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy(src, src+1, dst); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif } @@ -81,10 +91,15 @@ test_non_const_copy_assign_trivial() B3 *src = &dsrc; // If this is optimized to memmove it will overwrite tail padding. std::copy(src, src+1, dst); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy(src, src+1, dst); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif } diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_backward/108846.cc b/libstdc++-v3/testsuite/25_algorithms/copy_backward/108846.cc index 206748d92d3..855ee3e182f 100644 --- a/libstdc++-v3/testsuite/25_algorithms/copy_backward/108846.cc +++ b/libstdc++-v3/testsuite/25_algorithms/copy_backward/108846.cc @@ -25,10 +25,15 @@ test_pr108846() B *src = &dsrc; // If this is optimized to memmove it will overwrite tail padding. std::copy_backward(src, src+1, dst+1); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy_backward(src, src+1, dst+1); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif } @@ -52,10 +57,15 @@ test_non_const_copy_assign() B2 *src = &dsrc; // Ensure the not-taken trivial copy path works for this type. std::copy_backward(src, src+1, dst+1); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy_backward(src, src+1, dst+1); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif } @@ -81,10 +91,15 @@ test_non_const_copy_assign_trivial() B3 *src = &dsrc; // If this is optimized to memmove it will overwrite tail padding. std::copy_backward(src, src+1, dst+1); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy_backward(src, src+1, dst+1); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif } diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/108846.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/108846.cc index 50deb5dd051..d46fb9006e2 100644 --- a/libstdc++-v3/testsuite/25_algorithms/copy_n/108846.cc +++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/108846.cc @@ -25,10 +25,15 @@ test_pr108846() B *src = &dsrc; // If this is optimized to memmove it will overwrite tail padding. std::copy_n(src, 1, dst); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy_n(src, 1, dst); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif } @@ -52,10 +57,15 @@ test_non_const_copy_assign() B2 *src = &dsrc; // Ensure the not-taken trivial copy path works for this type. std::copy_n(src, 1, dst); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy_n(src, 1, dst); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif } @@ -79,10 +89,15 @@ test_non_const_copy_assign_trivial() B3 *src = &dsrc; // If this is optimized to memmove it will overwrite tail padding. std::copy_n(src, 1, dst); + // Check tail padding is unchanged: VERIFY(ddst.x == 3); + // Check B subobject was copied: + VERIFY(ddst.i == 4 && ddst.j == 5); #if __cpp_lib_ranges >= 201911L + ddst.i = ddst.j = 99; std::ranges::copy_n(src, 1, dst); VERIFY(ddst.x == 3); + VERIFY(ddst.i == 4 && ddst.j == 5); #endif }