What's the purpose of the rotate instructions (ROL, RCL on x86)?

35.6k Views Asked by At

I always wondered what's the purpose of the rotate instructions some CPUs have (ROL, RCL on x86, for example). What kind of software makes use of these instructions?

I first thought they may be used for encryption/computing hash codes, but these libraries are written usually in C, which doesn't have operators that map to these instructions. (Editor's note: see Best practices for circular shift (rotate) operations in C++ for how to write C or C++ that will compile to a rotate instruction. Also, optimized crypto libraries often do have asm for specific platforms.)

Has anybody found an use for them? Why where they added to the instructions set?

6

There are 6 best solutions below

6
On BEST ANSWER

Rotates are required for bit shifts across multiple words. When you SHL the lower word, the high-order bit spills out into the carry. To complete the operation, you need to shift the higher word(s) while bringing in the carry to the low-order bit. RCL is the instruction that accomplishes this.

                      High word             Low word         CF
Initial          0110 1001 1011 1001   1100 0010 0000 1101    ?
SHL low word     0110 1001 1011 1001   1000 0100 0001 1010    1
RCL high word    1101 0011 0111 0011   1000 0100 0001 1010    0

ROL and ROR are useful for examining a value bit-by-bit in a way that is (ultimately) non-destructive. They can also be used to shunt a bitmask around without bringing in garbage bits.

0
On

If I understand you correctly, your question is this:

"Given the fact that rotation instructions seem to be very special-purpose and not emitted by compilers, when are they actually used and why are they included in CPUs?".

The answer is twofold:

  1. CPU's are not designed specifically to execute C programs. Rather, they are designed as general purpose machines, intended to solve a wide array of problems using a wide variety of different tools and languages.

  2. The designers of a language are under no obligation to use every opcode in the CPU. In fact, most of the time, they do not, because some CPU instructions are highly specialized, and the language designer has no pressing need to use them.

More information about bitwise operators (and how they relate to C programming) can be found here: http://en.wikipedia.org/wiki/Bitwise_operation

6
On

The rotate shift opcodes ROL, RCL, ROR, RCR) are used almost exclusively for hashing and CRC computations. They are pretty arcane and very rarely used.

The shift opcodes (SHL, SHR) are used for fast multiplication by powers of 2, or to move a low byte into a high byte of a large register.

The difference between ROL and SHL is ROL takes the high bit and rolls it around into the low bit position. SHL throws the high bit away and fills the low bit position with zero.

0
On

Back when microprocessors were first created, most programs were written in assembly, not compiled. The majority of CPU instructions are probably not emitted by compilers (which is the impetus for creating RISC), but are often relatively easy to implement in hardware.

Many algorithms in graphics and cryptography use rotation, and their inclusion in CPUs makes it possible to write very fast algorithms in assembly.

3
On

ROR ROL are "historic" but still useful in a number of ways.

Before the 80386 (and opcode BT), ROL would be used a lot to test a bit (SHL doesn't propagate to the carry flag) - actually in 8088, ROR/ROL would only shift by 1 bit at a time !!!!

Also if you want to shift one way and then the other way without loosing the bits that have been shifted out of scope, you'd use ROR/ROL instead of SHR/SHL

2
On

I think many answers here got it somewhat backwards, including the currently accepted one. The biggest application is in shifting data across byte/word boundaries, which is extensively used in

  • extracting and inserting bit patterns
    • protocols (insert 5 bits starting from bit 6)
    • compression schemes (LZW77 and more)
    • data transfer (300 baud modems anyone? 7-bit data + parity)
  • arbitrary precision arithmetic
    • multiplying/dividing by 2 utilises rotations-through-carry
    • multiplying/dividing by other powers of two need the ROL (or ROR)
    • scrolling 1-bit graphics horizontally

And the niche applications:

  • crc16/32
  • ciphers
  • non-destructive moving bits to sign bit or to carry for testing

The historical perspective is that shifting was expensive: when one needs to shift say 16 bits left by 3, in chunks of 8 bits (or 128 bits left in chunks of 64 bits), a ROL performs two expensive shifts at the cost of one:

rotate all bits left by 3
      hi       lo
src = fedcba98|76543210
dst = cba98765|43210---

Notice, that the bits "765" need to be shifted right by 5, while bits "43210" need to be shifted left by 3. This is all accomplished by a single rotation, which put all the right bits to the correct position, even if they are accompanied by the wrong bits, which are recombined by masking, which is an inexpensive operation:

dst_lo = ((src_lo ROL 3) & 0b11111000)
dst_hi = ((src_lo ROL 3) & 0b00000111) | (src_hi << 3)

This extends to bignum shifting, or scrolling a monochrome graphics plane horizontally by arbitrary number of pixels.

This algorithm is so essential, that 80386 included a double-rotate instruction for it.