home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13590 < prev    next >
Encoding:
Internet Message Format  |  1992-09-09  |  2.4 KB

  1. Xref: sparky comp.lang.c:13590 alt.folklore.computers:13891
  2. 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
  3. From: rescorla@rtnmr.chem.yale.edu (Eric Rescorla)
  4. Newsgroups: comp.lang.c,alt.folklore.computers
  5. Subject: Re: What is Duff's Device?
  6. Message-ID: <1992Sep12.153235.3022@cs.yale.edu>
  7. Date: 12 Sep 92 15:32:35 GMT
  8. References: <1992Sep12.091733.522@cs.umb.edu>
  9. Sender: news@cs.yale.edu (Usenet News)
  10. Distribution: na
  11. Organization: Rescorla for himself.
  12. Lines: 44
  13. Nntp-Posting-Host: rtnmr.chem.yale.edu
  14.  
  15. In article <1992Sep12.091733.522@cs.umb.edu> mzraly@ra.cs.umb.edu (Michael S. Zraly) writes:
  16. >I understand that Duff's Device is a technique used to unroll a
  17. >critical loop, along the lines of what one might find in a routine like
  18. >memcpy().  Can someone please post or email an example of code using
  19. >Duff's Device?
  20. >
  21. >Finally, is Duff's Device named after Bell's Tom Duff?
  22. From the Jargon File.
  23. :Duff's device: n. The most dramatic use yet seen of {fall
  24. through} in C, invented by Tom Duff when he was at Lucasfilm.
  25. Trying to {bum} all the instructions he could out of an inner
  26. loop that copied data serially onto an output port, he decided to
  27. {unroll} it.  He then realized that the unrolled version could
  28. be implemented by *interlacing* the structures of a switch and
  29. a loop:
  30.  
  31.        register n = (count + 7) / 8;       /* count > 0 assumed */
  32.        switch (count % 8)
  33.         {
  34.         case 0: do {    *to = *from++;
  35.         case 7:         *to = *from++;
  36.         case 6:         *to = *from++;
  37.         case 5:         *to = *from++;
  38.         case 4:         *to = *from++;
  39.         case 3:         *to = *from++;
  40.         case 2:         *to = *from++;
  41.         case 1:         *to = *from++;
  42.         } while (--n > 0);
  43.         }
  44.  
  45.    Having verified that the device is valid portable C, Duff announced
  46.    it.  C's default {fall through} in case statements has long been
  47.    its most controversial single feature; Duff observed that "This
  48.    code forms some sort of argument in that debate, but I'm not sure
  49.    whether it's for or against."
  50.  
  51.                                   
  52. -Ekr
  53.  
  54. -- 
  55. -------------------------------------------------------------------------------
  56. Eric Rescorla, DoD#431 (Nighthawk S)              rescorla@rtnmr.chem.yale.edu
  57. Former chemist now CM400 mechanic                 ekr@eitech.com(preferred)
  58.                        I will hack Unix for food.
  59.