🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Absolutely no floating point?

Started by
5 comments, last by Skizz 19 years, 7 months ago
For my game I need to calculate a tangens once, and sqrt often. For the moment I'm quickly converting to floating point, calculating the result, then converting back to fixed point. Today I've been purusing all documents I could find on Brew, and I seem to remember reading that floating point calculation can be done but are very slow. Is this true? Because if it isn't and floating point calculations are totally unavailable, then I need to find some fixed point trig and sqrt routines.
Advertisement
Yeah, floats are very slow. We have some fixed point math libraries internally that we're close to releasing to the public. I'll see if we can do a pre-release for the contest.
This is what I use in J2ME to do a fixed point square root:
FP_TYPE FP_Sqrt(    FP_TYPE square){    FP_TYPE        guess = square;    if (square < INT_TO_FP (1))    {        guess = FP_Sqrt (square * 256) / 16;    }    else    {        square <<= FP_SHIFT;        if (square > 0)        {            FP_TYPE                old_guess;            do            {                old_guess = guess;                guess = (old_guess + square / old_guess) >> 1;            } while ((guess < old_guess) && (guess > 0));        }    }    return guess;}

where:
FP_TYPE = a #define for the type (int, usually)
FP_SHIFT = the shift from an interger to a fixed point format
INT_TO_FP = a macro to convert an int to a FP_TYPE (i.e. #define INT_TO_FP(v) ((v)<<FP_SHIFT)

For the transendental functions, use a lookup table that contains the angles 0O to 90O and you can then work out the other three quadrants. You could use linear interpolation to get a slightly better result.

Note that the ARM chip in most phones don't have any division instructions so all division is done 'long hand'.

Skizz
Quote: Original post by Skizz
FP_TYPE FP_Sqrt(    FP_TYPE square){    FP_TYPE        guess = square;    if (square < INT_TO_FP (1))    {        guess = FP_Sqrt (square * 256) / 16;    }    else    {        square <<= FP_SHIFT;


If we get here it means that square>1. If we then shift left with 16, we lose all the bits that represent the non-fractional part don't we?

Somehow this is not working for me. If I ask FPsqrt(INT_TO_FP(82)) , square at this point equals 0.

Original post by Skizz
        if (square > 0)        {            FP_TYPE                old_guess;            do            {                old_guess = guess;                guess = (old_guess + square / old_guess) >> 1;            } while ((guess < old_guess) && (guess > 0));        }    }    return guess;}

where:
FP_TYPE = a #define for the type (int, usually)
FP_SHIFT = the shift from an interger to a fixed point format
INT_TO_FP = a macro to convert an int to a FP_TYPE (i.e. #define INT_TO_FP(v) ((v)<<FP_SHIFT)
[/qoute]
so here:
FP_TYPE = int
FP_SHIFT = 16
Dude....not to be rude, but why dont you TRY to figure out a single simple thing by yourself before you ask a million questions? Most of the things you asked about were in the openGL ES examples (in case you were wondering where the code posted in the other topic came from), or the help files and I read the stuff and figured it out without major incident. BTW google is your friend.

DMNXIA
dmnxia: You are absolutely right. I've started porting my app to the Brew simulator two days ago, and due to time limitation I have not read all the documentation. I followed the tutorial here, and when I have any problems I quickly skim over the documents I found. I did not take the time to properly puruse the entire websites of Brew and Qualcomm, indeed I did not exactly understand the distinction between Qualcomm and Brew.

After I read your post I took a bit more time too find those examples you mentioned, and they did indeed contain the code posted in the other topic. In addition, I also found the Qualcomm OpenGL ES website with additional info on it.

I would like to thank everybody who helped me with these questions, and I would also like to apologize to all of you for wasting your time because I didn't do enough research in the documentation.

Many thanks again, and thank you for pointing this out dmnxia.

PS. Don't think this is a rave. I am not upset, and I do not plan to leave here or stop posting questions. I just realize now what I've been doing, and that it isn't the best way to go around doing this kind of thing.
The following condition must be met for the algorithm to work: FP_SHIFT < number of bits in FP_TYPE / 2
So FP_SHIFT = 8 and FP_TYPE = int (32 bits) would work for the range of numbers 0 < square < 65536.
The line "square <<= FP_SHIFT;" is preshifting the numerator of this division "square / old_guess" to work in a fixed point environment (this gives you the upper limit).

Skizz

This topic is closed to new replies.

Advertisement