A dart into the dark abstract landed on the most infamous loop in C — a switch statement threaded straight through a do-while, so the loop body falls THROUGH the case labels and copies N items in ⌈N/8⌉ passes. We run it, count the branches it deletes, credit who actually wrote it, and ask what I-13 should learn. Three prongs, one dart.
Unroll the copy 8×, then handle the remainder not with a separate cleanup loop but by jumping into the middle of the unrolled body via the switch. Case labels sit between statements; execution enters at case N % 8 and falls through the rest, then the do-while wraps it. live demo
Both copiers below actually run in your browser on a real array of length N. We instrument every branch and prove the outputs are byte-identical.
No myth to bust on the author — the name is honest. It really was Tom Duff, and he really did devise it. What people forget is why and how legal it is. cited
On the fall-through that makes it work, Duff himself hedged: “this code forms some sort of argument in that debate, but I’m not sure whether it’s for or against.” It is valid C (relaxed switch + fall-through), but it violates MISRA C and strict compilers like CompCert reject it. cited
Still open: whether it’s a win today. When it was removed from the XFree86 server (v4.0) the binary shrank and throughput rose — modern CPUs and compilers often unroll better on their own. The technique is timeless; the payoff is hardware-dependent. open
Duff’s Device is built from exactly the two things I-13 does not have: a switch and fall-through into the middle of a loop. I asked the real I-13 compiler. It refuses both — proven, not asserted:
“Control is depth-indexed, not address-indexed. With a saved entry height the target is always a number already in hand, so validation is one linear pass.”
Add a computed jump and that guarantee dies: the stack height at the landing point now depends on how you arrived, so there is no single number to check and single-pass validation is gone. So the honest recommendation is not to add switch/computed-goto. Mark Duff’s Device as structurally inexpressible by design — a technique that reveals, precisely, the line I-13 chose to draw.