On Thursday, 18 October 2012 at 02:36:59 UTC, bearophile wrote:
uint foo(in uint x) pure nothrow {
    return (x << 11) | (x >> (32 - 11));
}

Why would you write that instead of using rol(x, 11) today?

uint rol(in uint x, in uint y) pure nothrow {
    return (x << y) | (x >> (32 - y));
}

uint foo(in uint x) pure nothrow {
        return rol(x, 11);
}

the rest is the same. Compile it and see a rol instruction, inlined, in the main function.

Though there is a bit of extra spam around it, some movs that don't seem necessary to me.

Here's the top function so you can see the movs:

00000000 <_D5test43rolFNaNbxkxkZk>:
   0:   55                      push   ebp
   1:   8b ec                   mov    ebp,esp
   3:   50                      push   eax
   4:   8b 45 08                mov    eax,DWORD PTR [ebp+0x8]
   7:   8b 4d fc                mov    ecx,DWORD PTR [ebp-0x4]
   a:   8b e5                   mov    esp,ebp
   c:   d3 c0                   rol    eax,cl
   e:   5d                      pop    ebp
   f:   c2 04 00                ret    0x4
  12:   90                      nop
  13:   90                      nop



Perhaps we should add that rol function to the stdlib to save people from quickly doing it themselves, but there's no need to do anything beyond this simple function.

Reply via email to