TAOCP 7.1.3 Exercise 206
Let the input word $z$ be split into two halves $x$ and $y$, each consisting of 32 bits, so that $z = (x,y)$ in concatenated form.
Section 7.1.3: Bitwise Tricks and Techniques
Exercise 206. [20] [20] The perfect shuffle (175) is sometimes called an “outshuffle,” by comparison with the “inshuffle” that takes z → y ‡ x = (y31x31 . . . y1x1y0x0)2; the outshuffle preserves the leftmost and rightmost bits of z, but the inshuffle has no fixed points. Can an inshuffle be performed as efficiently as an outshuffle?
Verified: yes
Solve time: 1m51s
Solution
Let the input word $z$ be split into two halves $x$ and $y$, each consisting of 32 bits, so that $z = (x,y)$ in concatenated form. The outshuffle of Section 7.1.3, equations (175)–(178), is a fixed bit permutation $P$ that interleaves bits of $x$ and $y$ in one specified order, implemented by a constant number of MMIX instructions using masking, shifting, and recombination.
The inshuffle maps
$z = (x_{31}\cdots x_0, y_{31}\cdots y_0)$
to
$P'(z) = (y_{31}x_{31}\cdots y_0x_0).$
This transformation differs from the outshuffle only in the role of the two halves: each position where the outshuffle places a bit of $x$ is occupied by the corresponding bit of $y$, and vice versa.
Let $S$ denote the instruction sequence implementing the outshuffle $P$. Each instruction in $S$ is a linear combination of bitwise operations $&$, $\mid$, $\oplus$, and shifts, hence it computes a fixed coordinatewise Boolean transformation on the 64-bit vector. Swapping the initial decomposition $(x,y)$ to $(y,x)$ produces the transformed input $(y,x)$ on which the same sequence $S$ computes $P(x,y)$, which is exactly the inshuffle output.
Formally, if $\sigma$ denotes the permutation exchanging the two 32-bit halves, then the inshuffle satisfies
$P' = \sigma \circ P \circ \sigma.$
Conjugation by $\sigma$ does not change the number of MMIX instructions required, since it corresponds only to relabeling of registers before and after execution, with no additional bit operations inside the chain.
Therefore the inshuffle is implementable by the same instruction sequence as the outshuffle, up to initial and final register swapping, which contributes no arithmetic or logical operations.
The instruction count is identical to that of the outshuffle construction in equations (175)–(178), hence the inshuffle has the same asymptotic and constant-factor efficiency.
This completes the proof. ∎