From patchwork Thu Aug 1 21:11: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: 1968042 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=bjUyTSLJ; 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 4WZhW327rfz1ydv for ; Fri, 2 Aug 2024 07:12:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C6DA63858D39 for ; Thu, 1 Aug 2024 21:12: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 ESMTP id F34AA3858289 for ; Thu, 1 Aug 2024 21:11:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F34AA3858289 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 F34AA3858289 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=1722546718; cv=none; b=kypQqbV9EYp5rWQOVf6SJg6dFmr0X0F1qPREqztozuFsjoXMedZwpqHPeR4GbHibXm3u5Ykyk5D/XhiDGbsMGgIgFAc6T/bCP+t9MMxA+RtRMKxXyKsoKu1oUHu0r6ApBAnXkOi9r9S09mgnwieO9dOTxuW73Yi7QP5c1+sOHNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722546718; c=relaxed/simple; bh=eXNrkhVuMCKo4dBhTS8TohGw947+17xSlr5qD7YLhwo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lPJsXNLriFoUGjjHwWVSvziOj6nHB3Etf1DkrUaPhP/Lpyu1ISKbMTiMQp2BkphjZ7gKXBAgC8yXM/iJ+dHSFmlgvQ0qHYd4ueez7hnUFjyoLwh623n3ea/o1sMVaSscoDQvRRKpS08t+TloorKRgqIkZgiZOaa70n2o5FHX0AE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722546715; 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=ouZeHbDkygP1JAtmZPiZI2bReuuKiomW3540CTWRaXE=; b=bjUyTSLJhPb1G+XQqXY19+IVRhVzskpD0ubICzNLYtSrug5UIMCrZtPGpBYeJjqzKKznuF an0UQgeGL3s/+OBNoXDtcTh+664iyBQme3+S/ZgTvPS77UzenHOxTRkbShGF3H4yrvl9rd fQhGRFPNgB2Tk8qJFhfWNJvicJ9VfEQ= Received: from mx-prod-mc-02.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-65--I5YbQxBMY-O0bL_2teSZA-1; Thu, 01 Aug 2024 17:11:53 -0400 X-MC-Unique: -I5YbQxBMY-O0bL_2teSZA-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EC1F31955F0D; Thu, 1 Aug 2024 21:11:52 +0000 (UTC) Received: from localhost (unknown [10.42.28.21]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2A9E71955D42; Thu, 1 Aug 2024 21:11:51 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use memcmp to optimize std::bitset::_M_is_equal() [PR113807] Date: Thu, 1 Aug 2024 22:11:44 +0100 Message-ID: <20240801211150.386435-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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 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 -- As noted in the PR the compiler doesn't seem able to do this on its own, so we get better code at all optimization levels by using memcmp. libstdc++-v3/ChangeLog: PR libstdc++/113807 * include/std/bitset (bitset::_M_is_equal()): Use memcmp to optimize operator==. --- libstdc++-v3/include/std/bitset | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset index ccd6d19f7a4..e5d677ff059 100644 --- a/libstdc++-v3/include/std/bitset +++ b/libstdc++-v3/include/std/bitset @@ -205,10 +205,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _GLIBCXX14_CONSTEXPR bool _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT { - for (size_t __i = 0; __i < _Nw; ++__i) - if (_M_w[__i] != __x._M_w[__i]) - return false; - return true; +#if __cplusplus >= 201402L + if (__builtin_is_constant_evaluated()) + { + for (size_t __i = 0; __i < _Nw; ++__i) + if (_M_w[__i] != __x._M_w[__i]) + return false; + return true; + } +#endif + return !__builtin_memcmp(_M_w, __x._M_w, _Nw * sizeof(_WordT)); } template