[Coco] Re: CoCo gcc project

Theodore A. Evans alxevans at concentric.net
Mon Nov 3 20:18:01 EST 2003


On 11/03/03, KnudsenMJ at aol.com wrote:

> I've written a lot of C code where I used shifts to divide or multiply a
> value by powers of 2. I've also been told that in fancy modern compilers,
> you're better off to write it as a multiply or divide by 2, 4, 8,
> whatever, and let the compiler figure out whether, on the target CPU, a
> shift is faster than a MUL or DIV instruction.

If the compiler is that smart, it will use the optimum of the two
either way, and it becomes a matter of readability in your code, and a
simple one line comment can take care of that.  Of course it is
possible to run into a portablity problem.  Not all computers use raw
binary for unsigned ints (bytes, longs, long longs) (for instance a
few machines use BCD representation for numbers), and there are always
some more that don't use two's complement for signed (one's complement
is not unknown, nor is simple sign bit).

> If the value is floating point, then you *have* to use honest arithmetic.

Well, if you want to be portable, yes, but you can also cheat.  For
example, under Microware C, if you cast a float to a struct consisting
of four bytes, then increment or decrement the last byte (the
mantissa) to multiply or divide by two.  Of course, this is not
generally portable, though on many platforms you can use similar
tricks.  On top of that, a really good optomizing compiler will
probably figure out to do this.

-- 
"Everything's burnt here." - "Even the milk." -- Col. Potter
Theodore (Alex) Evans            | alxevans at concentric.net
94-1071 Kepakepa St. Unit #C-1   | http://www.concentric.net/~alxevans
Waipahu, HI 96797                | ICQ 78089262
x                                | (808) 676-0123         2B v ~2B = ?




More information about the Coco mailing list