From patchwork Fri Jun 28 19:24:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1954153 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=FCuB1UFQ; 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 4W9llf2B6Kz20Xf for ; Sat, 29 Jun 2024 05:25:50 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E5E53898C6C for ; Fri, 28 Jun 2024 19:25:48 +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 884AB3826DF0 for ; Fri, 28 Jun 2024 19:25:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 884AB3826DF0 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 884AB3826DF0 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=1719602714; cv=none; b=MC4P9EXgzpWVBu6M6ngOc4vLZXtWkv26JJmOW1fhIZTNdJqoYReT+Y4Tc9SGbH0eXwz1gmh4pAdEtBD54apY1s7V51+XXU5Rnx4Kh1ac/gJMsDnV9lfQpyr0kiyqZ89kYQnCVwamAvvjIti/j+ggXeUinkXV99ZNJmQvfUg3vPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719602714; c=relaxed/simple; bh=CSs+gln3R5f/crLEBGzy3Gwa8MkH8JmZwYixzwId19E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kfU79L6Ott4cHdPjNbq1KnHadIUTH6/gVBWUrO6hgWxDj/DM+yTqI4Yj15wBU1R+Ymf1TVxrCziWqjuqm1oI1SsVC/KUM38qfX09eApDsN6/Rz9RxVlzcg65Fc40n1tefGboavoTSOGzfBj1bYUkp3gZSRN01U0ECLkcSvnfz3I= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719602711; 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=rIsfH/iKGbb2ogpITFP/o4BOBh5kBbb0w02VFPuOods=; b=FCuB1UFQNpJPeKpmZsc/icYHd/QtLiYLkpnBjKDFhIUL0HCIv2IJaWKeHXuNNF3Eb0VMpz agl+qk0bxLJWUUTqZxcrWSBsva3Eg3sjfLVD5WLzqzyohvdUSkb3uJeartgCE7j48xWmQ5 Onm+d0MhZ2KOS9bpTVKUqdM9AZrGLiA= 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-443-jOG5wd3wNoSaUwu5HoOcyg-1; Fri, 28 Jun 2024 15:25:08 -0400 X-MC-Unique: jOG5wd3wNoSaUwu5HoOcyg-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 124D01956089; Fri, 28 Jun 2024 19:25:06 +0000 (UTC) Received: from localhost (unknown [10.42.28.171]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2E6E53000225; Fri, 28 Jun 2024 19:25:03 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Extend std::equal memcmp optimization to std::byte [PR101485] Date: Fri, 28 Jun 2024 20:24:30 +0100 Message-ID: <20240628192502.342983-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=-12.3 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, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=unavailable 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 -- We optimize std::equal to memcmp for integers and pointers, which means that std::byte comparisons generate bigger code than char comparisons. We can't use memcmp for arbitrary enum types, because they could have an overloaded operator== that has custom semantics, but we know that std::byte doesn't do that. libstdc++-v3/ChangeLog: PR libstdc++/101485 * include/bits/stl_algobase.h (__equal_aux1): Check for std::byte as well. * testsuite/25_algorithms/equal/101485.cc: New test. --- libstdc++-v3/include/bits/stl_algobase.h | 6 +++++- libstdc++-v3/testsuite/25_algorithms/equal/101485.cc | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/equal/101485.cc diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index 57ff2f7cb08..dec1e4c79d8 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -1257,7 +1257,11 @@ _GLIBCXX_END_NAMESPACE_CONTAINER typedef typename iterator_traits<_II1>::value_type _ValueType1; const bool __simple = ((__is_integer<_ValueType1>::__value #if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer) - || __is_pointer(_ValueType1) + || __is_pointer(_ValueType1) +#endif +#if __glibcxx_byte && __glibcxx_type_trait_variable_templates + // bits/cpp_type_traits.h declares std::byte + || is_same_v<_ValueType1, byte> #endif ) && __memcmpable<_II1, _II2>::__value); return std::__equal<__simple>::equal(__first1, __last1, __first2); diff --git a/libstdc++-v3/testsuite/25_algorithms/equal/101485.cc b/libstdc++-v3/testsuite/25_algorithms/equal/101485.cc new file mode 100644 index 00000000000..1fbb40acae9 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/equal/101485.cc @@ -0,0 +1,11 @@ +// { dg-options "-O0" } +// { dg-do compile { target c++17 } } +// { dg-final { scan-assembler "memcmp" } } + +#include +#include + +bool eq(std::byte const* p, std::byte const* q, unsigned n) +{ + return std::equal(p, p + n, q); +}