Duff's Device - Why IT Works

Why It Works

Based on an algorithm used widely by programmers coding in assembly for minimizing the number of tests and branches during a copy, Duff's device appears out of place when implemented in C. The device is valid C by virtue of two attributes in C:

  1. Relaxed specification of the switch statement in the language's definition. At the time of the device's invention this was the first edition of The C Programming Language which requires only that the controlled statement of the switch be a syntactically valid (compound) statement within which case labels can appear prefixing any sub-statement. In conjunction with the fact that, in the absence of a break statement, the flow of control will fall through from a statement controlled by one case label to that controlled by the next, this means that the code specifies a succession of count copies from sequential source addresses to the memory-mapped output port.
  2. The ability to jump into the middle of a loop in C.

Note that, as documented in the comment appearing in Duff's un-optimized version, the code assumes that count is strictly positive.

Read more about this topic:  Duff's Device

Famous quotes containing the word works:

    The works of women are symbolical.
    We sew, sew, prick our fingers, dull our sight,
    Producing what? A pair of slippers, sir,
    To put on when you’re weary or a stool
    To stumble over and vex you ... “curse that stool!”
    Or else at best, a cushion, where you lean
    And sleep, and dream of something we are not,
    But would be for your sake. Alas, alas!
    This hurts most, this ... that, after all, we are paid
    The worth of our work, perhaps.
    Elizabeth Barrett Browning (1806–1861)