Mail Archives: djgpp/1997/08/03/08:22:42
Jeff Weeks wrote:
>
> Yeah, yeah, I know... Fixed point math is easy. And I've used it many
> times... but 24.8, not 16.16. I just recently wrote a tiny 16.16 fixed
> point math class which doesn't work well at all.
>
> 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 must typecast the variables to a 64-bit type before multiplying
them. If you're using gcc (DJGPP), you have access to the 'long long'
integer type, which should suit your purpose. Otherwise, you've got to
play with the registers to simulate a 64-bit integer. Here's the code:
( (long long) x * (long long) y ) >> 16;
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com |
| Proud owner of what might one | http://www.cs.com/fighteer |
| day be a spectacular MUD... | Plan: To make Bill Gates suffer |
---------------------------------------------------------------------
- Raw text -