1 module nxt.bitwise_rotate;
2 
3 /** Rotate `x` left by `n` bits.
4  *
5  * Should compile to a single CPU instruction (ROL).
6  *
7  * TODO `core.bitop.rol` instead.
8  */
9 ulong rotateLeft(const scope ulong x,
10                  const scope uint n) @safe pure nothrow @nogc
11 {
12     version(D_Coverage) {} else pragma(inline, true);
13     return (x << n) | (x >> (8*typeof(x).sizeof - n));
14 }
15 
16 ///
17 @safe pure nothrow @nogc unittest
18 {
19     assert(rotateLeft(ulong.max, 1) == ulong.max);
20     assert(rotateLeft(1UL, 1) == 2);
21     assert(rotateLeft(2UL, 2) == 8);
22     assert(rotateLeft(3UL, 3) == 24);
23     assert(rotateLeft(2UL^^63, 1) == 1);
24 }
25 
26 /** Rotate `x` right by `n` bits.
27  *
28  * Should compile to a single CPU instruction (ROR).
29  *
30  * TODO `core.bitop.ror` instead.
31  */
32 ulong rotateRight(const scope ulong x,
33                   const scope uint n) @safe pure nothrow @nogc
34 {
35     version(D_Coverage) {} else pragma(inline, true);
36     return (x >> n) | (x << (8*typeof(x).sizeof - n));
37 }
38 
39 ///
40 @safe pure nothrow @nogc unittest
41 {
42     assert(rotateRight(ulong.max, 1) == ulong.max);
43     assert(rotateRight(1UL, 1) == 2UL^^63);
44 }