Hacker News new | ask | show | jobs
by praptak 4818 days ago
I did some similar experiments myself a long time ago. Something mildly surprising for me at that time was that this got optimized to a single roll instruction:

    uint32_t roll(uint32_t const x) {
        return (x << 15) | (x >> 17);
    }
I haven't managed to make GCC generate a non-constant roll (i.e. (x << c) | (x >> (32-c)) ), probably because it couldn't infer that c is in the [0,31] range.
2 comments

If c were outside that range, the result would be undefined, and the compiler would not need to consider that possibility.
What version of GCC are you using? The non-constant case compiles to "roll" as far back as GCC 4.4.
It was a really long time ago - around the time of the NIST contest for the AES, so around 2001. And thanks for the "roll" update :)