Best practices for circular shift (rotate) operations in C++
See also an earlier version of this answer on another rotate question with some more details about what asm gcc/clang produce for x86. The most compiler-friendly way to express a rotate in C and C++ that avoids any Undefined Behaviour seems to be John Regehr’s implementation. I’ve adapted it to rotate by the width of … Read more