From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 2E51E3864802 for ; Thu, 15 Feb 2024 11:46:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2E51E3864802 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 2E51E3864802 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707997595; cv=none; b=MyqoaxNd8H+wYE+jVDI4LKJ6qgB1SG2+fsp93k/QShSNZnkG4u2c+1RdLjYWg0BlBIeajouOWBT9i2940aM/G/cX6PM+zfxciQC2b7VpDZEECoFRDYwK83dDOvvp1Xb/Shw3Xq+xkTy2Ix+VuSW/a3JIFwD/NtLnlrm53GozO/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707997595; c=relaxed/simple; bh=gpu4v7piXu25Fk8lU11Uk91Xa16oY7b3ayw6/+KgTqI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Ax9xWPtI2VBM/eVMXo+LxHV0BkCI49kxBsChiCps1/iHi1V7Q9Ubpeg+R4GibV0v93wwbzxnEPEfos/J0mtQR8F//AxxIHisQAVHxpFoMesxMhHVlG2yOhxugYEiTglt+q6PEazxVI+kV7td5p64fE6nTNhnXJ82nimsWpoBBTQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707997590; 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=m/aQow4lA0rBFMylxF0wPIGFTHNnqPc/GTnzcd/NK8k=; b=NFKOU2H+tFiX4FjQ/di9j/6yee8S96zxAzh16kWfa0mcBusTtGKF9T/XE3r6nE4vuVmH0c F8n/KzrkINQFzccA2ehOgV854dIDCgQAVXd12fKtPPtLT0qWdrF4juHzbzbKAxJlcYS/nu +C/YVZLMk8vFhcAysraukTCqXBZZUt8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-148-7FHDP04WMQCbEqf75J6uDQ-1; Thu, 15 Feb 2024 06:46:29 -0500 X-MC-Unique: 7FHDP04WMQCbEqf75J6uDQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id EB33D10651EA; Thu, 15 Feb 2024 11:46:28 +0000 (UTC) Received: from localhost (unknown [10.42.28.206]) by smtp.corp.redhat.com (Postfix) with ESMTP id B8906112131D; Thu, 15 Feb 2024 11:46:28 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use unsigned division in std::rotate [PR113811] Date: Thu, 15 Feb 2024 11:46:16 +0000 Message-ID: <20240215114628.50110-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 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,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 List-Id: Tested aarch64-linux and x86_64-linux. Pushed to trunk. -- >8 -- Signed 64-bit division is much slower than unsigned, so cast the n and k values to unsigned before doing n %= k. We know this is safe because neither value can be negative. libstdc++-v3/ChangeLog: PR libstdc++/113811 * include/bits/stl_algo.h (__rotate): Use unsigned values for division. --- libstdc++-v3/include/bits/stl_algo.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 9496b53f887..7a0cf6b6737 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -1251,6 +1251,12 @@ _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2) typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; +#if __cplusplus >= 201103L + typedef typename make_unsigned<_Distance>::type _UDistance; +#else + typedef _Distance _UDistance; +#endif + _Distance __n = __last - __first; _Distance __k = __middle - __first; @@ -1281,7 +1287,7 @@ _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2) ++__p; ++__q; } - __n %= __k; + __n = static_cast<_UDistance>(__n) % static_cast<_UDistance>(__k); if (__n == 0) return __ret; std::swap(__n, __k); @@ -1305,7 +1311,7 @@ _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2) --__q; std::iter_swap(__p, __q); } - __n %= __k; + __n = static_cast<_UDistance>(__n) % static_cast<_UDistance>(__k); if (__n == 0) return __ret; std::swap(__n, __k); -- 2.43.0