home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18723 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  1.8 KB

  1. Path: sparky!uunet!europa.asd.contel.com!howland.reston.ans.net!usc!zaphod.mps.ohio-state.edu!uwm.edu!spool.mu.edu!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
  2. From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question to test general C knowledge
  5. Message-ID: <1992Dec20.234400.11271@thunder.mcrcim.mcgill.edu>
  6. Date: 20 Dec 92 23:44:00 GMT
  7. References: <1992Dec14.164400.59891@ns1.cc.lehigh.edu> <1992Dec17.190025.11325@athena.mit.edu>
  8. Organization: McGill Research Centre for Intelligent Machines
  9. Lines: 30
  10.  
  11. In article <1992Dec17.190025.11325@athena.mit.edu>, scs@adam.mit.edu (Steve Summit) writes:
  12. > In article <1992Dec14.222413.28898@newshost.lanl.gov>, jlg@cochiti.lanl.gov (J. Giles) writes:
  13.  
  14. >>    varname%4
  15. >>    varname&03
  16. > Whether or not the AND is faster is immaterial, as any C compiler
  17. > worthy of the name can and will replace a modulus operation by a mask
  18. > if it is in fact correct and better to do so.
  19.  
  20. The compiler often cannot tell whether it is correct to do so.  If the
  21. variable is declared signed (probably implicitly), but cannot in fact
  22. be negative at that point (for reasons the compiler cannot determine,
  23. eg, it's the return value from a separately compiled function which is
  24. called with arguments which make it always return a positive value),
  25. then &3 is correct but the compiler can't determine this.  (Unless it
  26. rounds divison by four towards minus infinity, in which case they *are*
  27. equivalent.)
  28.  
  29. > There are a few "source code" optimizations which it may still be
  30. > worth making, but replacing % by & (or / by >>) isn't one of them.
  31.  
  32. And I agree, except for special circumstances (eg, divison/modulus is
  33. slower than and/shift, and cycles are very precious, or kernel code for
  34. a machine where / and % are normally done by the kernel on behalf of
  35. user processes).
  36.  
  37.                     der Mouse
  38.  
  39.                 mouse@larry.mcrcim.mcgill.edu
  40.