Invented by programmer Tom Duff while he was at Lucasfilm Ltd. in 1983, Duff's Device generalizes the loop unrolling process. You can unroll loops to your heart's content using this technique, without knowing the number of iterations beforehand. The original algorithm combined a
do-while and a
switch statement. The technique combines loop unrolling, loop reversal, and loop flipping.
Note: The first printing of Speed Up Your Site assumed for the faster Duff's Device that n is greater than the degree you unroll the loop. A safer, more universal method is to check for a positive n in each loop, not just the first one in Duff's device.