home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:13590 alt.folklore.computers:13891
- Path: sparky!uunet!pmafire!news.dell.com!swrinde!sdd.hp.com!wupost!darwin.sura.net!spool.mu.edu!yale.edu!cs.yale.edu!rtnmr.chem.yale.edu!rescorla
- From: rescorla@rtnmr.chem.yale.edu (Eric Rescorla)
- Newsgroups: comp.lang.c,alt.folklore.computers
- Subject: Re: What is Duff's Device?
- Message-ID: <1992Sep12.153235.3022@cs.yale.edu>
- Date: 12 Sep 92 15:32:35 GMT
- References: <1992Sep12.091733.522@cs.umb.edu>
- Sender: news@cs.yale.edu (Usenet News)
- Distribution: na
- Organization: Rescorla for himself.
- Lines: 44
- Nntp-Posting-Host: rtnmr.chem.yale.edu
-
- In article <1992Sep12.091733.522@cs.umb.edu> mzraly@ra.cs.umb.edu (Michael S. Zraly) writes:
- >I understand that Duff's Device is a technique used to unroll a
- >critical loop, along the lines of what one might find in a routine like
- >memcpy(). Can someone please post or email an example of code using
- >Duff's Device?
- >
- >Finally, is Duff's Device named after Bell's Tom Duff?
- From the Jargon File.
- :Duff's device: n. The most dramatic use yet seen of {fall
- through} in C, invented by Tom Duff when he was at Lucasfilm.
- 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);
- }
-
- Having verified that the device is valid portable C, Duff announced
- it. 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."
-
-
- -Ekr
-
- --
- -------------------------------------------------------------------------------
- Eric Rescorla, DoD#431 (Nighthawk S) rescorla@rtnmr.chem.yale.edu
- Former chemist now CM400 mechanic ekr@eitech.com(preferred)
- I will hack Unix for food.
-