home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / mac / programm / 15513 < prev    next >
Encoding:
Text File  |  1992-09-15  |  2.4 KB  |  87 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!unlinfo.unl.edu!news
  3. From: mgleason@cse.unl.edu (Mike Gleason)
  4. Subject: BlockZero.c snippet
  5. Message-ID: <1992Sep15.185019.22483@unlinfo.unl.edu>
  6. Sender: news@unlinfo.unl.edu
  7. Nntp-Posting-Host: cse.unl.edu
  8. Organization: NCEMRSoft
  9. Date: Tue, 15 Sep 1992 18:50:19 GMT
  10. Lines: 75
  11.  
  12. I think J. McCarthy & M. Mora started a good idea by posting code snippets here,
  13. so here's a freebie function that clears an area of memory (which can start
  14. on an odd boundary).  It's much faster than using memset(ptr,0,n) because
  15. this function uses move.l's for the majority of the block.  It compiles
  16. under Think C, but the asm {} blocks may break other compilers.
  17.  
  18. I use this function all the time, especially when dealing with parameter
  19. blocks. e.g.:
  20.  
  21. #define ZERO(a) BlockZero(&(a), sizeof (a))
  22.  
  23. {
  24.     HParamBlockRec  pb;
  25.  
  26.     ZERO(pb);
  27. }
  28.  
  29.  
  30. Code follows.  Further optimizations, your cool code snippets, bug fixes
  31. <gulp> welcome....
  32.  
  33. ----------------
  34.  
  35. void BlockZero(void *area, register unsigned long n);
  36.  
  37. void BlockZero(void *area, register unsigned long n)
  38. {
  39.     register char               *p = (char *)area;
  40.     register unsigned long      fours;
  41.     register unsigned long      remainder, zero;
  42.  
  43.     if (n) {
  44.         if ((long) p & 01) {
  45.             /* Ptr on an odd boundary... */
  46.             asm {
  47.                 clr.b       (p)+
  48.             }
  49.             --n;
  50.             /*
  51.              * Ptr is now on an even boundary,
  52.              * and it's first byte is zeroed.
  53.              */
  54.         }
  55.         
  56.         fours = n / 4L;     /* Can be zero. */
  57.         
  58.         /* Do the meat of the block using longwords for speed. */
  59.  
  60.         asm {
  61.                 clr.l       zero
  62.                 bra         @2
  63.         @1:     move.l      zero, (p)+
  64.         @2:     subq.l      #1, fours
  65.                 tst.l       fours
  66.                 bge         @1
  67.         }
  68.  
  69.         /*
  70.          * If the size of the block isn't divisible by 4, we'll
  71.          * have to do the remaining bytes (up to 3) by hand.
  72.          */
  73.  
  74.         remainder = n & 03;     /* same as remainder = n % 4. */
  75.             
  76.         asm {
  77.             bra             @4
  78.         @3: clr.b           (p)+
  79.         @4: dbra            remainder, @3
  80.         }
  81.     }
  82. }   /* BlockZero */
  83.  
  84. /* eof BlockZero.c */
  85. --
  86. --mg                                                      mgleason@cse.unl.edu
  87.