Charge
Member
Hi,
last night I tried writing a few fixed point math routines to get started in GP32 development. I used 8 bits for the fixed part, 16 for the number, and 8 bits of padding so that when I need to multiply I dont loose most of the number.
The structs I use are as follows :
typedef struct FIXED_TAG
{
unsigned int decimal : 8;
int number : 16;
int buffer : 8;
} FIXED;
typedef struct FIXED_UNION_TAG
{
union
{
FIXED fSplit;
int nWhole;
};
} FIXED_UNION;
and any fixed point numbers are therefore of type FIXED_UNION.
eg:
FIXED_UNION first_fp_num;
FIXED_UNION second_fp_num;
FIXED_UNION result_fp;
If I multiply 2 fixed point numbers together I do it by :
result_fp.nWhole = ((first_fp_num.nWhole * second_fp_num.nWhole) >> 8);
Ththis works fine, except when I used negative numbers with a decimal part. Does anyone have the solution?
Cheers.
last night I tried writing a few fixed point math routines to get started in GP32 development. I used 8 bits for the fixed part, 16 for the number, and 8 bits of padding so that when I need to multiply I dont loose most of the number.
The structs I use are as follows :
typedef struct FIXED_TAG
{
unsigned int decimal : 8;
int number : 16;
int buffer : 8;
} FIXED;
typedef struct FIXED_UNION_TAG
{
union
{
FIXED fSplit;
int nWhole;
};
} FIXED_UNION;
and any fixed point numbers are therefore of type FIXED_UNION.
eg:
FIXED_UNION first_fp_num;
FIXED_UNION second_fp_num;
FIXED_UNION result_fp;
If I multiply 2 fixed point numbers together I do it by :
result_fp.nWhole = ((first_fp_num.nWhole * second_fp_num.nWhole) >> 8);
Ththis works fine, except when I used negative numbers with a decimal part. Does anyone have the solution?
Cheers.