Duff's device

In the C programming language, Duff's device is a way of manually implementing loop unrolling by interleaving two syntactic constructs of C: the do-while loop and a switch statement. Its discovery is credited to Tom Duff in November 1983, when Duff was working for Lucasfilm and used it to speed up a real-time animation program.

Loop unrolling attempts to reduce the overhead of conditional branching needed to check whether a loop is done, by executing a batch of loop bodies per iteration. To handle cases where the number of iterations is not divisible by the unrolled-loop increments, a common technique among assembly language programmers is to jump directly into the middle of the unrolled loop body to handle the remainder.[1] Duff implemented this technique in C by using C's case label fall-through feature to jump into the unrolled body.[2]

  1. ^ Holly, Ralf (August 1, 2005). "A Reusable Duff Device". Dr. Dobb's. Dr. Dobb's Journal. Retrieved September 18, 2015.
  2. ^ Duff, Tom (August 29, 1988). "Subject: Re: Explanation, please!". Lysator. Retrieved November 3, 2015.