This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] P0556R3 Integral power-of-2 operations, P0553R2 Bit operations
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jonathan Wakely <jwakely at redhat dot com>
- Cc: libstdc++ at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Wed, 4 Jul 2018 10:26:15 +0200
- Subject: Re: [PATCH] P0556R3 Integral power-of-2 operations, P0553R2 Bit operations
- References: <20180703210247.GA7287@redhat.com> <20180703214003.GE7166@tucnak> <20180703222359.GL2838@redhat.com> <20180704080934.GF7166@tucnak> <20180704081404.GN2838@redhat.com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Wed, Jul 04, 2018 at 09:14:04AM +0100, Jonathan Wakely wrote:
> > Unfortunately it is not correct if _Nd is not power of two.
> > E.g. for __sN 1, -1U % 20 is 15, not 19.
> > So it would need to be
> > return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));
> > Unfortunately, our rotate pattern recognizer handles
> > return (__x << __sN) | (__x >> ((-__sN) % _Nd));
> > or
> > return (__x << __sN) | (__x >> ((-__sN) & (_Nd - 1)));
> > but doesn't handle the _Nd - __sN case.
> > Is this C++17+ only? Then perhaps
>
> The std::rotr and std::rotl functions are C++2a only, but I've added
> the __rotr and __rotl versions for our own internal use in C++14 and
> later.
>
> In practice I have no internal use for rotr and rotl, so I could
> remove the __rot[rl] forms. However, won't ((_Nd & (_Nd - 1)) optimize
> to a constant even without if-constexpr? I'll check.
It should, sure.
Anyway, I guess my C tests didn't test properly what happens in your
functions. We actually do optimize:
unsigned long long foo (unsigned long long __x, unsigned int __s)
{
constexpr int _Nd = 64;
unsigned int _sN = __s % _Nd;
return (__x << _sN) | (__x >> ((_Nd - _sN) % _Nd));
}
properly, just don't do it if it is:
unsigned long long foo (unsigned long long __x, unsigned int __s)
{
int _Nd = 64;
unsigned int _sN = __s % _Nd;
return (__x << _sN) | (__x >> ((_Nd - _sN) % _Nd));
}
Apparently, the (64 - x) & 63 optimization to -x & 63 is only done
somewhere in the FEs and not in match.pd, which is something we should fix.
But with constexpr _Nd you actually can use
return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));
unconditionally.
Jakub