delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/14/04:18:03

Sender: crough45 AT amc DOT de
Date: Mon, 14 Apr 1997 09:03:44 +0100
From: Chris Croughton <crough45 AT amc DOT de>
Mime-Version: 1.0
To: fighteer AT cs DOT com
Cc: djgpp AT delorie DOT com
Subject: Re: Help! Random is not working right
Message-Id: <97Apr14.100108gmt+0100.21894@internet01.amc.de>

John M. Aldrich wrote:

>It should also be noted that gcc is very smart when it comes to
>optimizations, and will probably convert modulus operators into masks
>when you're modding with powers of two.  It's okay to trust the
>compiler!  ;)

This has been true with all recent compilers I've used (Borland,
Microsoft and a couple of Unix ones).  They seem to do that piece
of optimisation even with optimisation turned off.  The same for
multiplication and division by constant powers of 2.  I find it
clearer to write:

  lsb = word % 256;
  msb = word / 256;

than the equivalent lowlevel operations:

  lsb = word & 0xFF;
  msb = word >> 8;

(assuming that 'word' is unsigned short, of course, for clarity).
I've never had a speed or size penalty because of it...

Chris

- Raw text -


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