On 10/17/2011 4:45 PM, bearophile wrote:
Manu:
*Roll/Rotate:* I'm loving the '>>>' operator, but I could often really do
with a rotate operator useful in many situations... '>>|' perhaps...
something like that? This is ugly: a = (a<< x) | ((unsigned)a>>
(sizeof(a)/8 - x));
I have asked for a rotate intrinsic in Phobos, but Walter has added a rewrite
rule instead, that turns D code to a rot. Personal experience has shown me
that it's easy to write the operation in a slightly different way (like with
signed instead of unsigned values) that causes a missed optimization. So I
prefer still something specific, like a Phobos intrinsic, to explicitly ask
for this operation to every present and future D compiler, with no risk of
mistakes.
There's no need for a compiler intrinsic. Just write a function that does do the
optimization, and call it.
The signed versions "don't work" because a signed right shift is not the same
thing as an unsigned right shift.
For reference:
void test236()
{
uint a;
int shift;
a = 7;
shift = 1;
int r;
r = (a >> shift) | (a << (int.sizeof * 8 - shift));
assert(r == 0x8000_0003);
r = (a << shift) | (a >> (int.sizeof * 8 - shift));
assert(a == 7);
}