delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/03/08:22:42

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: 16.16 fixed point math multiply (overflow problems)
Date: Wed, 30 Jul 1997 20:17:57 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 28
Message-ID: <33DFA175.3543@cs.com>
References: <33DF8F01 DOT 58BCC37C AT execulink DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp106.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


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