public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/113811] std::rotate does 64-bit signed division Date: Thu, 08 Feb 2024 21:31:44 +0000 [thread overview] Message-ID: <bug-113811-4-qjn2cNbdGF@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-113811-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113811 --- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> --- It seems fairly easy to do: commit 12a028d76bbdf26d34d4d90a2ecdc39c6c0a4bd4 (HEAD -> master) Author: Jonathan Wakely Date: Thu Feb 8 15:40:32 2024 libstdc++: Use unsigned division in std::rotate [PR113811] 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. 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); Conditionally using 32-bit types would be a bit trickier, as it needs runtime branches, or making the type of __n and __k a template parameter, so we can call __rotate_with<unsigned> to use a smaller type than make_unsigned<_Distance> if max(n,k) < UINT_MAX.
next prev parent reply other threads:[~2024-02-08 21:31 UTC|newest] Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top 2024-02-07 16:15 [Bug libstdc++/113811] New: " terra at gnome dot org 2024-02-08 9:13 ` [Bug libstdc++/113811] " rguenth at gcc dot gnu.org 2024-02-08 10:21 ` redi at gcc dot gnu.org 2024-02-08 21:31 ` redi at gcc dot gnu.org [this message] 2024-02-15 11:44 ` cvs-commit at gcc dot gnu.org 2024-02-15 12:45 ` redi at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-113811-4-qjn2cNbdGF@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).