[gameprogrammer] Fixed point math

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)

/* 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 ?

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


Other related posts: