From patchwork Wed Jun 12 15:03:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1947000 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=H3IxhVJL; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4VzpkY513xz20Pb for ; Thu, 13 Jun 2024 01:05:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7BFFB3882123 for ; Wed, 12 Jun 2024 15:05:23 +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 ESMTPS id 293023882061 for ; Wed, 12 Jun 2024 15:03:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 293023882061 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 293023882061 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=1718204640; cv=none; b=hz1AF6eSUMXLWbGcEACGPzvHw/S16DrQiQ0l7VnqPlMqXZ7bs17Aijg0FawLh6hVpFThg0DUWyA6u1SJv9UiYyqEmRMrqhUopcfHNMWqsfSbaka3CJIAaDTRcATj2Z1kPpBMGgDfHl+N6TWyFwS32wNp/ZTW3qDAlpUjBIYKpsU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718204640; c=relaxed/simple; bh=9PoJgrbDEjnd2gZJ0tzufKFny+Aw8TKf96Qe3xNAVg0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UCuj3HMih75flUeKJnlYP1nKbJJ6WMz7nHma8o+IXnv+ll7JX8Ol8eovuWuVlJDu09t4rueUJ9FnisgxE1U263LGrRECEyNP/TPXTEhC2u6vOl7icjECH2Bc36MkdR2gMgxzN2mlOSFv5QUThYokOa4QD8ST3Dg6RHzfHNGyvHQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718204637; 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=kjLLppQLFXMH/uc2DjK0CqX0Mp3Jb7cZQR7mDgKZ8Es=; b=H3IxhVJL/LrUsbPEyifkaA1iJWcuu2aNeUFx9H71wtp8fB8EyrhMmzqfsgzOPSvwBtDbOW dRtMdskDfq2la179RfqOOJyJFb9+C35b5VBkpLsWa4byxxIktOislkwi+HCLcabkkrvW2M wdIlewXhLFcCWWefwo9Vtu/8Z7YaC+Q= Received: from mx-prod-mc-05.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-597-YAQv1_4vPLWdxOm0G_Izaw-1; Wed, 12 Jun 2024 11:03:55 -0400 X-MC-Unique: YAQv1_4vPLWdxOm0G_Izaw-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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 40E841955F01; Wed, 12 Jun 2024 15:03:53 +0000 (UTC) Received: from localhost (unknown [10.39.192.128]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A3D1F30000C4; Wed, 12 Jun 2024 15:03:51 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::tr2::dynamic_bitset shift operations [PR115399] Date: Wed, 12 Jun 2024 16:03:46 +0100 Message-ID: <20240612150350.812187-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=-10.7 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_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 shift operations for dynamic_bitset fail to zero out words where the non-zero bits were shifted to a completely different word. For a right shift we don't need to sanitize the unused bits in the high word, because we know they were already clear and a right shift doesn't change that. libstdc++-v3/ChangeLog: PR libstdc++/115399 * include/tr2/dynamic_bitset (operator>>=): Remove redundant call to _M_do_sanitize. * include/tr2/dynamic_bitset.tcc (_M_do_left_shift): Zero out low bits in words that should no longer be populated. (_M_do_right_shift): Likewise for high bits. * testsuite/tr2/dynamic_bitset/pr115399.cc: New test. --- libstdc++-v3/include/tr2/dynamic_bitset | 5 +-- libstdc++-v3/include/tr2/dynamic_bitset.tcc | 6 +-- .../testsuite/tr2/dynamic_bitset/pr115399.cc | 37 +++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 libstdc++-v3/testsuite/tr2/dynamic_bitset/pr115399.cc diff --git a/libstdc++-v3/include/tr2/dynamic_bitset b/libstdc++-v3/include/tr2/dynamic_bitset index 0e4e8894287..274c4f6a138 100644 --- a/libstdc++-v3/include/tr2/dynamic_bitset +++ b/libstdc++-v3/include/tr2/dynamic_bitset @@ -815,10 +815,7 @@ namespace tr2 operator>>=(size_type __pos) { if (__builtin_expect(__pos < this->_M_Nb, 1)) - { - this->_M_do_right_shift(__pos); - this->_M_do_sanitize(); - } + this->_M_do_right_shift(__pos); else this->_M_do_reset(); return *this; diff --git a/libstdc++-v3/include/tr2/dynamic_bitset.tcc b/libstdc++-v3/include/tr2/dynamic_bitset.tcc index 63ba6f285c7..5aac7d88ee3 100644 --- a/libstdc++-v3/include/tr2/dynamic_bitset.tcc +++ b/libstdc++-v3/include/tr2/dynamic_bitset.tcc @@ -60,8 +60,7 @@ namespace tr2 this->_M_w[__wshift] = this->_M_w[0] << __offset; } - //// std::fill(this->_M_w.begin(), this->_M_w.begin() + __wshift, - //// static_cast<_WordT>(0)); + std::fill_n(this->_M_w.begin(), __wshift, _WordT(0)); } } @@ -88,8 +87,7 @@ namespace tr2 this->_M_w[__limit] = this->_M_w[_M_w.size()-1] >> __offset; } - ////std::fill(this->_M_w.begin() + __limit + 1, this->_M_w.end(), - //// static_cast<_WordT>(0)); + std::fill_n(this->_M_w.end() - __wshift, __wshift, _WordT(0)); } } diff --git a/libstdc++-v3/testsuite/tr2/dynamic_bitset/pr115399.cc b/libstdc++-v3/testsuite/tr2/dynamic_bitset/pr115399.cc new file mode 100644 index 00000000000..e626e4a5d15 --- /dev/null +++ b/libstdc++-v3/testsuite/tr2/dynamic_bitset/pr115399.cc @@ -0,0 +1,37 @@ +// { dg-do run { target c++11 } } + +// PR libstdc++/115399 +// std::tr2::dynamic_bitset shift behaves differently from std::bitset + +#include +#include + +void +test_left_shift() +{ + std::tr2::dynamic_bitset<> b(65); + b[0] = 1; + auto b2 = b << 64; + VERIFY(b2[64] == 1); + VERIFY(b2[0] == 0); + b <<= 64; + VERIFY( b2 == b ); +} + +void +test_right_shift() +{ + std::tr2::dynamic_bitset<> b(65); + b[64] = 1; + auto b2 = b >> 64; + VERIFY(b2[64] == 0); + VERIFY(b2[0] == 1); + b >>= 64; + VERIFY( b2 == b ); +} + +int main() +{ + test_left_shift(); + test_right_shift(); +}