[gameprogrammer] Re: Fixed point Math

On Wed, 2004-10-20 at 12:08, Gautam Narain wrote:
> Hi,
> 
> Kevin, you are partly right and partly wrong. I am targetting Opengl ES 
> which needs fixed point support. However I am targetting the Khronos contest 
> and not the gamedev one and a linux application(game). Anyway I do this for 
> a hobby so I figured well - I will aim for the contest.
> 
> I changed the following in the code
> long long e = FIXMUL(c, d);

My guess is that it is doing a 32 bit multiply and then converting the
result to 64 bits. It would do that because c and d are 32 bit numbers.

> 
> This gives me zero as well. However when I do it with floating point 
> numbers, I get the values after the decimal(fraction) correctly but the main 
> number is zero. Any ideas why it could be truncating ? Lastly I don't think 
> OpenGL ES has 64 bit support(so long long may not be supported). So I will 
> probably be stuck with int's. But I need to use 15.16 fixed point variables. 
> Should I do everything in 7.8 bit and the convert to 15.16 bit ? Would this 
> slow things down or make it equal to if I did it with just floats ?

Why must you use 15.16 numbers? BTW, if you multiply a 7.8 number by
another 7.8 number you get a 14.16 number. Can you get by with
converting everything to a fraction and using something like 1.15
numbers?

                Bob Pendleton

> 
> Thanks
> 
> Gautam
> 
> >------------------------------
> >
> >From: "Kevin Fields" <drunkendruid@xxxxxxxxxxx>
> >Subject: [gameprogrammer] Re: Fixed point math
> >Date: Tue, 19 Oct 2004 22:34:38 -0300
> >
> >I believe it's because the code is meant to run on a cellphone. Correct?
> >There's a contest going on at GameDev.Net for people to create 3D game 
> >demos
> >for cellphones using the BREW and OpenGLES SDKs. Unfortunately, the
> >cellphones don't have any floating point support.
> >
> >Kevin
> >
> >From: Bob Pendleton <bob@xxxxxxxxxxxxx>
> >Reply-To: gameprogrammer@xxxxxxxxxxxxx
> >To: Gameprogrammer Mailing List <gameprogrammer@xxxxxxxxxxxxx>
> >Subject: [gameprogrammer] Re: Fixed point math
> >Date: Tue, 19 Oct 2004 13:49:08 -0500
> >
> >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        +
> >+--------------------------------------+
> >
> >
> 
> _________________________________________________________________
> Movies, music, celeb news. Stay in the loop. http://www.msn.co.in/cinema/
> With MSN Entertainment!
> 
> 
> 
> ---------------------
> 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: