Mail Archives: djgpp/1997/08/06/15:38:06
Jeff Weeks writes:
>My multiply function overflows everytime, and it's obvious why. Here's
>what I do:
>
>(x * y) >> 16;
>
>Obviously anything over 1 * 1 (65536 * 65536) will result in an
>overflow, even in a 32-bit register. I'm wondering what the best
>technique is to multiplying in a 16.16 fixed point format?
You need to do a 64 bit multiply, to prevent the overflow. In C, just
cast your parameters to 'long long' and all will be well.
If you don't trust gcc to optimise this properly (it actually does a
pretty decent job, but not 100% perfect) you can drop down to asm and
use the 64 bit result that imul leaves in %eax:%edx. See the fmul()
function in allegro.h...
--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
Beauty is a French phonetic corruption of a short cloth neck ornament.
- Raw text -