* [PATCH] libstdc++: Improve std::rot[lr] [PR99396]
@ 2021-03-05 20:37 Jakub Jelinek
2021-03-06 10:08 ` Jonathan Wakely
0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2021-03-05 20:37 UTC (permalink / raw)
To: gcc-patches, libstdc++
Hi!
As can be seen on:
#include <bit>
unsigned char f1 (unsigned char x, int y) { return std::rotl (x, y); }
unsigned char f2 (unsigned char x, int y) { return std::rotr (x, y); }
unsigned short f3 (unsigned short x, int y) { return std::rotl (x, y); }
unsigned short f4 (unsigned short x, int y) { return std::rotr (x, y); }
unsigned int f5 (unsigned int x, int y) { return std::rotl (x, y); }
unsigned int f6 (unsigned int x, int y) { return std::rotr (x, y); }
unsigned long int f7 (unsigned long int x, int y) { return std::rotl (x, y); }
unsigned long int f8 (unsigned long int x, int y) { return std::rotr (x, y); }
unsigned long long int f9 (unsigned long long int x, int y) { return std::rotl (x, y); }
unsigned long long int f10 (unsigned long long int x, int y) { return std::rotr (x, y); }
//unsigned __int128 f11 (unsigned __int128 x, int y) { return std::rotl (x, y); }
//unsigned __int128 f12 (unsigned __int128 x, int y) { return std::rotr (x, y); }
constexpr auto a = std::rotl (1234U, 0);
constexpr auto b = std::rotl (1234U, 5);
constexpr auto c = std::rotl (1234U, -5);
constexpr auto d = std::rotl (1234U, -__INT_MAX__ - 1);
the current <bit> definitions of std::__rot[lr] aren't pattern recognized
as rotates, they are too long/complex for that, starting with signed modulo,
special case for 0 and different cases for positive and negative.
For types with power of two bits the following patch adds definitions that
the compiler can pattern recognize and turn e.g. on x86_64 into ro[lr][bwlq]
instructions. For weirdo types like unsigned __int20 etc. it keeps the
current definitions.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2021-03-05 Jakub Jelinek <jakub@redhat.com>
PR libstdc++/99396
* include/std/bit (__rotl, __rotr): Add optimized variants for power of
two _Nd which the compiler can pattern match the rotates.
--- libstdc++-v3/include/std/bit.jj 2021-03-05 10:37:36.108378753 +0100
+++ libstdc++-v3/include/std/bit 2021-03-05 12:01:57.926310110 +0100
@@ -68,6 +68,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__rotl(_Tp __x, int __s) noexcept
{
constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
+ if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
+ {
+ // Variant for power of two _Nd which the compiler can
+ // easily pattern match.
+ constexpr unsigned __uNd = _Nd;
+ const unsigned __r = __s;
+ return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
+ }
const int __r = __s % _Nd;
if (__r == 0)
return __x;
@@ -82,6 +90,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__rotr(_Tp __x, int __s) noexcept
{
constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
+ if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
+ {
+ // Variant for power of two _Nd which the compiler can
+ // easily pattern match.
+ constexpr unsigned __uNd = _Nd;
+ const unsigned __r = __s;
+ return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
+ }
const int __r = __s % _Nd;
if (__r == 0)
return __x;
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] libstdc++: Improve std::rot[lr] [PR99396]
2021-03-05 20:37 [PATCH] libstdc++: Improve std::rot[lr] [PR99396] Jakub Jelinek
@ 2021-03-06 10:08 ` Jonathan Wakely
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2021-03-06 10:08 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches, libstdc++
On Fri, 5 Mar 2021, 22:32 Jakub Jelinek via Libstdc++, <
libstdc++@gcc.gnu.org> wrote:
> Hi!
>
> As can be seen on:
> #include <bit>
>
> unsigned char f1 (unsigned char x, int y) { return std::rotl (x, y); }
> unsigned char f2 (unsigned char x, int y) { return std::rotr (x, y); }
> unsigned short f3 (unsigned short x, int y) { return std::rotl (x, y); }
> unsigned short f4 (unsigned short x, int y) { return std::rotr (x, y); }
> unsigned int f5 (unsigned int x, int y) { return std::rotl (x, y); }
> unsigned int f6 (unsigned int x, int y) { return std::rotr (x, y); }
> unsigned long int f7 (unsigned long int x, int y) { return std::rotl (x,
> y); }
> unsigned long int f8 (unsigned long int x, int y) { return std::rotr (x,
> y); }
> unsigned long long int f9 (unsigned long long int x, int y) { return
> std::rotl (x, y); }
> unsigned long long int f10 (unsigned long long int x, int y) { return
> std::rotr (x, y); }
> //unsigned __int128 f11 (unsigned __int128 x, int y) { return std::rotl
> (x, y); }
> //unsigned __int128 f12 (unsigned __int128 x, int y) { return std::rotr
> (x, y); }
>
> constexpr auto a = std::rotl (1234U, 0);
> constexpr auto b = std::rotl (1234U, 5);
> constexpr auto c = std::rotl (1234U, -5);
> constexpr auto d = std::rotl (1234U, -__INT_MAX__ - 1);
> the current <bit> definitions of std::__rot[lr] aren't pattern recognized
> as rotates, they are too long/complex for that, starting with signed
> modulo,
> special case for 0 and different cases for positive and negative.
>
> For types with power of two bits the following patch adds definitions that
> the compiler can pattern recognize and turn e.g. on x86_64 into
> ro[lr][bwlq]
> instructions. For weirdo types like unsigned __int20 etc. it keeps the
> current definitions.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>
OK, thanks.
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-03-06 10:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-05 20:37 [PATCH] libstdc++: Improve std::rot[lr] [PR99396] Jakub Jelinek
2021-03-06 10:08 ` Jonathan Wakely
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).