PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

s15.16 fixed point overflows

Hi there,

Using s15.16 fixed point numbers, is there a simple way to catch over/underflow errors without having to replicate the equation being tested in floats and using a break point?


Ie

a * b will result in an overflow

Code:

EGL_Fixed a = EGL_FixedFromFloat(300);
EGL_Fixed b = EGL_FixedFromFloat(300);
EGL_Fixed c = EGL_Mul(a,b);

#if DEBUG

float af = EGL_FloatFromFixed(a);
float bf = EGL_FloatFromFixed(b);
float cf = af*bf;

if(cf > EGL_FloatFromFixed(EGL_PINF))
{
//over flow - break here

}

#endif


my mind is running a blank on how best to do this without having to replicate the equation each time
(I have quite a few)

any help would be appreciated.
Structure wrote:

my mind is running a blank on how best to do this without having to replicate the equation each time
(I have quite a few)

any help would be appreciated.

Since I know those macros/inlines:

The intermediate result is a 64-bit number, so you could test if the upper 16 bits are different from 0xffff and 0. (Shift them signed to the right, add one, unsigned result is <= 1).

- HM
Firstly I cant thank you enough for your EGL_Fixed implementation, im using it extensively throughout my project.

Yeah I didn’t think of that, I was taking a bit of a black box approach to your code.


Just out of interest, im not really up on this but are 64 bit ints slow on pocket pc hardware? I was shying away from using them as intermediate values, but I think I really need them for normalisation of vectors as the s15.16 format is placing quite a restriction on the vector size.
Structure wrote:Firstly I cant thank you enough for your EGL_Fixed implementation, im using it extensively throughout my project.

Yeah I didn’t think of that, I was taking a bit of a black box approach to your code.


Just out of interest, im not really up on this but are 64 bit ints slow on pocket pc hardware? I was shying away from using them as intermediate values, but I think I really need them for normalisation of vectors as the s15.16 format is placing quite a restriction on the vector size.

They are not really bad, it depends a little on what you are doing. From what I understand, you want to accumulate the adds in 64 bit before taking the root. That should be fine. Taking the root is the place where you would need to add some logic in: In case your intermediate result would overflow, explicitly shift to the right before going into the root calculation, and adjust the result by an appropriate shift to the left.

Vincent does a couple of these 'tricks' to get the necessary precision range required by the conformance tests.

- HM
Ok thanks ill investigate that tomorrow,

Im making a 2dimentional rigid body physics engine for a game im making, this uses a polygon collision system, because of the numerical restrictions I cant have 2 vertices more than ~180 pixels away from each other if I want to be able to normalise the corresponding vector. This is problematic if i want to create say a large box to prevent objects falling off the bottom of the screen. I was thinking of a method to subdivide the vertices into smaller gaps but this creates a lot of extra collision tests. So the method you mentioned would be useful. No doubt ill create overflows somewhere else but if I get the multiply test working I should be able to catch some of those


Sorry im rambling………


Well I’ll probably need some help tomorrow so thanks
:D
Structure wrote:

Im making a 2dimentional rigid body physics engine for a game im making, this uses a polygon collision system, because of the numerical restrictions I cant have 2 vertices more than ~180 pixels away from each other if I want to be able to normalise the corresponding vector.


That was pretty much where I was before sending out this post http://www.pocketmatrix.com/forums/viewtopic.php?t=20204
Well, actually I was considering to turn it into a full 3D physics engine.

How 'closed' is your code? Any interest in any form of collaboration?

- HM
That raises quite an interesting debate that ive been having with myself over whether to release the software I’m writing to the public domain. I’m in favour of open source projects for their educational value, I learnt most of what I know about game programming today from them.

But as I’m writing this engine as a commercial venture I am worried that I would be release a fundamental asset to possible competition.

A solution to this would be to release the code under GLP licence or some form of non-commercial restrictive licence, but this would require observation of the industry to insure conformity, and I don’t really have the resources to peruse licence infringements.


My physics engine uses an 2d implementation of this paper

http://graphics.stanford.edu/papers/rigid_bodies-sig03/

But with restricted collision handling that uses an optimised separation of axis test of convex objects.

The engine is essentially finished, im just trying to make it more robust from overflows , and more flexible in terms of object configurations.



Out of interest, what sort of collaboration were you suggesting?
I didn't necessarily think OSS. I'll send you a private message later today...

- HM