diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2024-02-08 15:40:32 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2024-02-15 11:43:21 +0000 |
commit | 4d819db7f229a23cb15ef68f310e0bb51d201c45 (patch) | |
tree | 0f8e096528c7512cb79d029a578eb46793ab5dae /libstdc++-v3 | |
parent | b58f0e5216a3053486e7f1aa96c3f2443b14d630 (diff) | |
download | gcc-4d819db7f229a23cb15ef68f310e0bb51d201c45.zip gcc-4d819db7f229a23cb15ef68f310e0bb51d201c45.tar.gz gcc-4d819db7f229a23cb15ef68f310e0bb51d201c45.tar.bz2 |
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.
Diffstat (limited to 'libstdc++-v3')
-rw-r--r-- | libstdc++-v3/include/bits/stl_algo.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 9496b53..7a0cf6b 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); |