delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/17/11:43:03

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Allegro ex11.c
Date: Mon, 16 Feb 1998 19:19:42 +0000
Organization: None
Distribution: world
Message-ID: <U8UGrGAOFJ60EwJq@talula.demon.co.uk>
References: <34DF7294 DOT 21D0 AT home DOT com> <34e39746 DOT 1884750 AT news DOT compusmart DOT ab DOT ca>
<34E7EFA0 DOT 6DF9E92 AT alcyone DOT com> <6c908d$1mj AT freenet-news DOT carleton DOT ca>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 23
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Paul Derbyshire writes:
>I sincerely hope GCC's optimizer is smart enough to silently use a
>bitshift when optimizing
>
>int x;
>...
>y = x % 8;
>
>for instance.

The modulus operator and a bitmask are not exactly the same thing, so it
cannot do that. The result of a%b has a range from -b+1 to b-1, wheras
a&(b-1), where b is a power of two, ranges from 0 to b-1.

If you are using unsigned data, gcc is able to do this optimisation. But
in many cases when you are using signed values, it is both easier and
less error prone to write an explicit & operation than to keep casting
back and forth between signed and unsigned variables.


--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
"Pigs use it for a tambourine" - Frank Zappa

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019