From: Shawn Hargreaves Newsgroups: comp.os.msdos.djgpp Subject: Re: 16.16 fixed point math multiply (overflow problems) Date: Mon, 4 Aug 1997 19:15:04 +0100 Organization: None Distribution: world Message-ID: References: <33DF8F01 DOT 58BCC37C AT execulink DOT com> NNTP-Posting-Host: talula.demon.co.uk MIME-Version: 1.0 Lines: 22 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk 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.