[gameprogrammer] Re: Fixed point math

On Tue, 2004-10-19 at 13:28, Gautam Narain wrote:
> Hi,
> 
> I am trying to do fixed point maths. Currently I have the following code.
> 
> #ifndef FPMATH_H
> #define FPMATH_H
> 
> #include <GLES/gl.h>
> 
> /* convert an integer to a fixed value */
> #define INTTOFIX(x)   ((x)<<16)
> 
> /* convert a float to a fixed value */
> #define FLOATTOFIX(x) (x*(1<<16))
> 
> /* convert a fixed point variable to an int */
> #define FIXTOINT(x)   ((x)<<16)

The shift is backward here.

> 
> /* convert a fixed point variable to a float */
> #define FIXTOFLOAT(x) ((float)(x)/65536)
> 
> /* Add 2 fixed variables */
> #define FIXADD(x, y) ((x)+(y))
> 
> /* Subtract 2 fixed variables */
> #define FIXSUB(x, y) ((x)-(y))
> 
> /* multiply 2 fixed variables */
> #define FIXMUL(x, y) (((y)*(x))>>16)
> 
> /* divide 2 fixed variables */
> #define FIXDIV(x, y) ((y  << 16)/(x))
> 
> #endif
> 
> However when I do the following I get e as 0 and printf also shows zero.
> 
> #include <stdio.h>
> #include "fpmath.h"
> 
> int main()
> {
>     int a = 1;
>     int b = 2;
> 
>     int c = INTTOFIX(a);
>     int d = INTTOFIX(b);
> 
>     int e = FIXMUL(c, d);  // e seems to be zero
> 
>     printf("%ld\n", c);
>     printf("%ld\n", d);
>     printf("%ld\n", FIXMUL(c, d));  // result comes as 0
> }
> 
> I am using gcc 3.3.4 if it helps. I am at a loss as to where I am going 
> wrong. Surely this couldn;t be overflowing ?

Yes, as a matter of fact it is overflowing. When you multiply two fixed
point numbers with 16 bits of fraction the result has 32 bits of
fraction. Since your numbers only have 32 bits that means you get
nothing but fraction in the result. To do what you are trying to do you
need to use a 64 bit intermediate value. Which you can get at by using
assembly language. If you want to avoid assembly language you need 64
bit numbers, in gcc you can get those by declaring them as "long long
int" and do all you arithmetic and shifting on 64 bits values.

You might want to read: http://gameprogrammer.com/4-fixed.html which has
a nice section on fixed point arithmetic.

BTW, why are you working with fixed point? Floating point arithmetic is
much faster than integer arithmetic. Unless, of course, you are working
on a machine with no floating point unit or are using a very old
computer, something like a 486 or older.

                Bob Pendleton

> 
> Thanks.
> 
> Gautam
> 
> _________________________________________________________________
> Protect your PC from viruses. Enrich your online experience. 
> http://www.msn.co.in/security/ Get more from the internet.
> 
> 
> 
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
> 
-- 
+--------------------------------------+
+ Bob Pendleton: writer and programmer +
+ email: Bob@xxxxxxxxxxxxx             +
+ blog:  www.Stonewolf.net             +
+ web:   www.GameProgrammer.com        +
+--------------------------------------+



---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: