June 30th, 2024

Duff's Device and Coroutines (2008)

Duff's Device, a unique C code by Tom Duff in 1983, features an 8x-unrolled while loop with a switch statement for efficient data processing. Despite limited reuse, it sparked adaptations and discussions. Modern alternatives like threads and coroutines exist.

Read original articleLink Icon
Duff's Device and Coroutines (2008)

The article discusses Duff's Device, a unique piece of C code created by Tom Duff in 1983. It is an 8x-unrolled while loop combined with a switch statement, allowing for efficient processing of data. Despite its complexity, Duff's Device is considered a rare and specific tool, with limited reuse value. The code is valid in C due to the nature of switch statements and labels. Over the years, variations and discussions about its usage have emerged, including Bjarne Stroustrup's adaptation in The C++ Programming Language and Simon Tatham's C coroutine implementation for event-driven programming. Duff himself confirmed the use of a similar concept for specific applications in the past. The article also mentions modern alternatives like threads and coroutines with real stacks for similar functionalities. Various comments from readers provide additional insights and references to related implementations like protothreads in Contiki OS.

Link Icon 2 comments
By @kamikaz1k - 4 months
(I guess I could ask ChatGPT) Can someone explain the code? The article didn’t really help me understand.