2-16 Double-Length Shifts

Let (x1, x0) be a pair of 32-bit words to be shifted left or right as if they were a single 64-bit quantity, with x1 being the most significant half. Let (y1, y0) be the result, interpreted similarly. Assume the shift amount n is a variable ranging from 0 to 63. Assume further that the machine's shift instructions are modulo 64 or greater. That is, a shift amount in the range 32 to 63 or -32 to -1 results in an all-0 word, unless the shift is a signed right shift, in which case the result is 32 sign bits from the word shifted. (This code will not work on the Intel x86 machines, which have mod-32 shifts.)

Under these assumptions the shift left double operation may be accomplished as follows (eight instructions):

graphics/02icon107.gif

 

The main connective in the first assignment must be or, not plus, to give the correct result when n = 32. If it is known that 0 n 32, the last term of the first assignment may be omitted, giving a six-instruction solution.

Similarly, a shift right double unsigned operation may be done with

graphics/02icon108.gif

 

Shift right double signed is more difficult, because of an unwanted sign propagation in one of the terms. Straightforward code follows:

graphics/02icon109.gif

 

If your machine has the conditional move instructions, it is a simple matter to express this in branch-free code, in which form it takes eight instructions. If the conditional move instructions are not available, the operation may be done in ten instructions by using the familiar device of constructing a mask with the shift right signed 31 instruction to mask the unwanted sign propagating term:

graphics/02icon110.gif