home *** CD-ROM | disk | FTP | other *** search
- 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
- From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
- Newsgroups: comp.lang.c
- Subject: Re: Question to test general C knowledge
- Message-ID: <1992Dec20.234400.11271@thunder.mcrcim.mcgill.edu>
- Date: 20 Dec 92 23:44:00 GMT
- References: <1992Dec14.164400.59891@ns1.cc.lehigh.edu> <1992Dec17.190025.11325@athena.mit.edu>
- Organization: McGill Research Centre for Intelligent Machines
- Lines: 30
-
- In article <1992Dec17.190025.11325@athena.mit.edu>, scs@adam.mit.edu (Steve Summit) writes:
- > In article <1992Dec14.222413.28898@newshost.lanl.gov>, jlg@cochiti.lanl.gov (J. Giles) writes:
-
- >> varname%4
- >> varname&03
- > Whether or not the AND is faster is immaterial, as any C compiler
- > worthy of the name can and will replace a modulus operation by a mask
- > if it is in fact correct and better to do so.
-
- The compiler often cannot tell whether it is correct to do so. If the
- variable is declared signed (probably implicitly), but cannot in fact
- be negative at that point (for reasons the compiler cannot determine,
- eg, it's the return value from a separately compiled function which is
- called with arguments which make it always return a positive value),
- then &3 is correct but the compiler can't determine this. (Unless it
- rounds divison by four towards minus infinity, in which case they *are*
- equivalent.)
-
- > There are a few "source code" optimizations which it may still be
- > worth making, but replacing % by & (or / by >>) isn't one of them.
-
- And I agree, except for special circumstances (eg, divison/modulus is
- slower than and/shift, and cycles are very precious, or kernel code for
- a machine where / and % are normally done by the kernel on behalf of
- user processes).
-
- der Mouse
-
- mouse@larry.mcrcim.mcgill.edu
-