delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/06/15:38:06

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
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: <g0eLJAAowh5zEwrE@talula.demon.co.uk>
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

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 -


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