Duff's device n.
The most dramatic use yet seen of fall through in C, invented by Tom Duff when he was at L
Trying to bum all the instructions he could out of an inner
loop that copied data serially onto an output port, he decided to
unroll it. He then realized that the unrolled version could be
implemented by interlacing the structures of a switch and a
loop:
register n = (count + 7) / 8; /* count > 0 assumed */
switch (count % 8)
{
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
Shocking though it appears to all who encounter it for the first
time, the device is actually perfectly valid, legal C. C's default
fall through in case statements has long been its most
controversial single feature; Duff observed that "This code forms
some sort of argument in that debate, but I'm not sure whether it's
for or against." Duff has discussed the device in detail at
http://www.lysator.liu.se/c/duffs-device.html. Note
that the omission of postfix ++ from *to was
intentional (though confusing). Duff's device can be used to
implement memory copy, but the original aim was to copy values
serially into a magic IO register.
[For maximal obscurity, the outermost pair of braces above could be
actually be removed -- GLS]
The most dramatic use yet seen of fall through in C, invented by Tom Duff when he was at L
Trying to bum all the instructions he could out of an inner
loop that copied data serially onto an output port, he decided to
unroll it. He then realized that the unrolled version could be
implemented by interlacing the structures of a switch and a
loop:
register n = (count + 7) / 8; /* count > 0 assumed */
switch (count % 8)
{
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
Shocking though it appears to all who encounter it for the first
time, the device is actually perfectly valid, legal C. C's default
fall through in case statements has long been its most
controversial single feature; Duff observed that "This code forms
some sort of argument in that debate, but I'm not sure whether it's
for or against." Duff has discussed the device in detail at
http://www.lysator.liu.se/c/duffs-device.html. Note
that the omission of postfix ++ from *to was
intentional (though confusing). Duff's device can be used to
implement memory copy, but the original aim was to copy values
serially into a magic IO register.
[For maximal obscurity, the outermost pair of braces above could be
actually be removed -- GLS]
Related:
- fall through v.
(n. `fallthrough', var.
`fall-through') 1.
To exit a loop by exhaustion, i.e., by having fulfilled... - bug n.
An unwanted and unintended property of a program or
piece of hardware,
esp. one that causes it to malfunction. Antonym... - bit-paired keyboard n.,obs.
(alt. `bit-shift
keyboard') A non-standard keyboard layout that seems to have
originated with the Teletype ASR-33 and remained common for several
years on early computer equipment.
The ASR-33 was a mechanical device (see EOU), so... - 0475 Do you know Duff's device?
--
from THE HACKER... - ping
[from the submariners' term for a sonar pulse] 1.
n. Slang term for a small network message (ICMP... - kluge /klooj/
[from the German `klug', clever; poss.
related to Polish `klucz' (a key, a hint, a main point)]... - TECO /tee'koh/ n.,v. obs.
1. [originally an acronym for
`[paper] Tape Editor and COrrector';
later, `Text Editor and COrrector'] n. A text editor... - accumulator n. obs.
1. Archaic term for a register.
On-line use of it as a synonym for `register' is... - fencepost error n.
1. [common] A problem with the discrete
equivalent of a boundary condition,
often exhibited in programs by iterative loops....
From the same category:
- phage n.
A program that modifies other programs or
databases in unauthorized ways;
esp. one that propagates a virus or Trojan horse... - dink /dink/ adj.
Said of a machine that has the bitty box nature;
a machine too small to be worth bothering wi sometimes... - high moby /hi:' mohb'ee/ n.
The high half of a 512K
PDP-10's physical address space;
the other half was of course the low moby. This... - Zork /zork/ n.
The second of the great early experiments
in computer fantasy gaming;
see ADVENT. Originally written on MIT-DM during... - PHB /P-H-B/
[Usenet; common; rarely spoken] Abbreviation,
"Pointy-Haired Boss". From the Dilbert character,...
