PocketMatrix forums

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

floating point multiply == very very slow ?

Is it right? I am making a phisics simulation for my game and having only ~100 objects, I'm having terrible slowdown. Fps drops to 5-10 while it is about 30 normally.

Simulation is quite simple - only I make about 70 floating multiplies for each object, and do it three times per graphic frame. Is 70*100*3 floating point mulitplies every frame that a big deal, so I'm suffering that much?

Don't want to make it all fixed point math, never used it, don't know how to handle it :(.
If you are doing any serious 3D physics and/or rendering, you do not want to use floating point numbers on PPCs. Their processors don't have any floating point instructions and everything is emulated in software. This is why it is so slow.

I am afraid that fixed point numbers is the easiest/most well known workaround to get any performances.
Thierry wrote:I am afraid that fixed point numbers is the easiest/most well known workaround to get any performances.

And to answer you next question: http://www.pocketmatrix.com/forums/viewtopic.php?t=18346

- HM
Floating point in software is ghastly - all that normalization and stuff is costly.

Fixed point is also covered in an <a href="http://www.arm.com/pdfs/DAI0033A_fixedpoint.pdf">ARM application note</a> (covers C as well as ARM assembler), and Jacco Bikker (aka Phantom) covered it <a href="http://www.flipcode.com/articles/article_mobilegfx02.shtml">here</a>
Floating point mult and divide is bad, and integer divide is just as bad.

Basically you should try and limit yourself to integer mult, add, sub, and binary / bitwise operators, if the operator is used with any frequency.

Also, 210 mults per object per frame is a heck of a lot. I assume each object has child objects that are manipulated individually?

Fixed point math is not that hard, especially when you're only using it for a specific algorithm (as opposed to trying to globally replace floats throughout a whole game engine).

Dan East
Dan East wrote:Basically you should try and limit yourself to integer mult, add, sub, and binary / bitwise operators, if the operator is used with any frequency.

Standard eVC++ integer "/" is bad too (because you need 64 bits), that's why I put the reference in my previous reply. Also contains implementations for sin, cos, sqrt, invsqrt...

- HM
I was affraid of that :(.

Ok, I have to deal with FP math. So, did anyone actually checked how exactly slow floating operations are?

1. What about floating add and sub? How slow they are?

2. What is better - translate for example 4 floating variables to fixed, multiply them altogether, then translate them back or simply multiply them using floating multiplication?
Translation from floating point to fixed point usually involves a floating point
mul so you're better off using only fixed point whenever lots of data needs to be processed.

Converting back and forth between floating point and fixed point only makes
sense when you're doing it less frequently.

But there's no reason to be afraid of fixed point.
Just know your bits and what kind of precision you need. That's all :)

But there's no reason to be afraid of fixed point.
Just know your bits and what kind of precision you need. That's all :)


I don't afraid of them, but FP debugging is a tortune, so I'd better use floating numbers, at least on a final stage.
jaguard wrote:I don't afraid of them, but FP debugging is a tortune, so I'd better use floating numbers, at least on a final stage.

You'll get into it quickly.
For example if you're using 16:16 fixed point (which is quite common i guess) you'll learn pretty fast which fixed point number resembles which floating point value ;)

It's all about 2^n, isn't it? :D
So, did anyone actually checked how exactly slow floating operations are?


Look at it this way. In fixed point:

addition is one cycle
subtraction is one cycle
multiplication can be ~3 or 4 cycles
division is costly (but cheaper than floating point).

In floating point the branch to the floating point emulator will cost you more than the first three of the above (and that's not including the setup to the branch which will add at least another 3 or 4 cycles).

Just look at the representations, and you'll start to see why floating point in software is "messy". For fixed point you essentially just have:
1 bit sign
x bits integer
(31-x) bits precision

.. and you treat them as integers for addition, subtraction, and comparison.

For floating point, you have:
1 bit sign
an x bit signed exponent (usually 8 bits)
(31-x) bits precision

... but they can't be compared or treated as integers without normalisation. Then you've got special cases for zero, etc, etc, and it all turns into a ghastly mess.

2. What is better - translate for example 4 floating variables to fixed, multiply them altogether, then translate them back or simply multiply them using floating multiplication?


Mixing fixed and floating point is as bad (or worse) then using only floating point for speed.

As Peter said, if you know the range of the variables, fixed point is "easy" - in C you can even just wrap things in macros.

Debugging is also easy - divide by the 2^n value you set your fixed point at to get a floating point number.

E.g. in binary - "11111111.1" = 511 and there's one bit of precision so we divide by 2^1=2 = 511/2=255.5

Oh and as for division, a computer science teacher once said: he who does lots of division ... is an idiot. :)
Btw. if you have two positive floating point numbers you can compare them like integers.
refractor wrote:Oh and as for division, a computer science teacher once said: he who does lots of division ... is an idiot. :)


Altough I agree with this statement, computer science teachers in general are morons.
to evaluate how costly particular instructions are you could try the following:

1) grab the time
2) perform the instruction you want to test about 1000 times
3) grab the end time
4) the time delta you can use as a number to compare with the results of other instructions.
Would be interesting to see some benchmarks on calculations using floating point x fixed point on a pocket pc. ( made by someone with some experience with this, I have zero )
I did some benchmarks years ago, but don't have them still. If I remember correctly, floating point math is a couple hundred times slower than integer math (excluding division).

Dan East
jaguard wrote:Is it right? I am making a phisics simulation for my game and having only ~100 objects, I'm having terrible slowdown. Fps drops to 5-10 while it is about 30 normally.

Simulation is quite simple - only I make about 70 floating multiplies for each object, and do it three times per graphic frame. Is 70*100*3 floating point mulitplies every frame that a big deal, so I'm suffering that much?

Don't want to make it all fixed point math, never used it, don't know how to handle it :(.


Yes floating point is slow, but this shouldn't be enough math to slow it down that much, it might be something else, are you sure you aren't doing any divides or actually doing more ops. One thing to watch is intermediaries (the compilier might be doing more calcs than you think) especially if you use C++ overloaded operators.

I did some benches a while ago on my Axim X5, if I remember correctly a float mult was about 40x slower than an int mult, when I get my Axim back upstairs ;) I'll see if the results are on it and post them.
"Yes floating point is slow, but this shouldn't be enough math to slow it down that much,"

Oh yes it will.

He is attempting to run 70*100*3*30 float operations per second - that is 630 000 operations per second.

Enough to bring down any PPC.
I measured some matrix & vector operations years ago and posted the results here. That was back in 2000, before the site changed and lost all the previous posts.

From what I recall, the difference between float vs. fixed in my tests was ~10x on my iPaq 3670.

The Intel C compiler reportedly has a faster floating point library than what ships with eVC. The ARM development toolset has yet an even faster one.
If you're trying to do 630,000 operations per second, and getting 5-10fps, assume the worst case of 5fps and divide the number of operations by 6 to find a safe number of operations that should still get you 30fps... which is 105,000 operations maximum.

Divide 105,000 by 30fps and you should have a maximum of 3500 operations per frame. With 100 objects, that's 35 operations per object, per frame, rather than the 210 you're currently doing.

You definitely need a more simplistic approach.

-John

Divide 105,000 by 30fps and you should have a maximum of 3500 operations per frame. With 100 objects, that's 35 operations per object, per frame, rather than the 210 you're currently doing.

You definitely need a more simplistic approach.
-John


Sounds rational :). Too bad it's practically impossible. I'm currently moving to fixed point, and I already hate it :), it's affects precision - 16 bits is not enough for fractional part. But it's doing faster..

PS: Fixed point is fast and good of course, but I can't always use it. For example, I'm looking at one my function, and have no idea what to do with atan() :).

here it is:

Code:
float CalcAngle(float x1, float y1, float x2, float y2)
{
// float r = GetDist(x1,y1,x2,y2);
if (int(x1-x2) == 0)
return y1>y2 ? PI/2.0f : -PI/2.0f;
float angle = atan((y1-y2)/(x1-x2));
if (x2 < x1)
angle += PI;
return -angle;

}
16 bits should be more than enough for fractional -- what I think is happening you are overflowing somewhere during calculations.

Try to use 64 bit variables.

BTW. What the hell you are doing there that requires 70 *3 float operations per object ?
A mathematical proof of sorts ?
warmi wrote:16 bits should be more than enough for fractional -- what I think is happening you are overflowing somewhere during calculations.

It worked better with floating. I changed nothing, just moved to fixed.


Try to use 64 bit variables.

Maybe I will.

warmi wrote:
BTW. What the hell you are doing there that requires 70 *3 float operations per object ?
A mathematical proof of sorts ?

Collision detection, friction, gravity, forces and such. I didn't complete it yet, 210 is only beginning ;).
Maybe try interpolating lookup tables for trig?
mlepage wrote:Maybe try interpolating lookup tables for trig?


That should work.. Anyone did atan/acos/asin in fixed point using lookup tables? (hate inventing a wheel). EGL's fixed point lib only have cos/sin :(.
jaguard":1z2kcmx5]

Try converting a float implementation if you cannot find a fixed one (there's a good open source version of libm by Sun). All the functions in Fixed.cpp have the same structure:

Normalize range, do a table lookup, perform 1 to 6 Newton iterations.

Cheers,
HM
Speaking of re-inventing a wheel...

Your atan function can be completely replaced by atan2(dx, dy)
(it does the quadrant stuff for you)
fast_rx wrote:Speaking of re-inventing a wheel...

Your atan function can be completely replaced by atan2(dx, dy)
(it does the quadrant stuff for you)

In this case he will need a 2D LUT and some more advanced bilinear interpolation for acceptable precision :)
warmi wrote:"Yes floating point is slow, but this shouldn't be enough math to slow it down that much,"
Oh yes it will.
He is attempting to run 70*100*3*30 float operations per second - that is 630 000 operations per second.
Enough to bring down any PPC.


Yeh thats too many, after looking at my code I am doing 25k per frame or so and that is pushing it, so reduce your ops by over an order of magnitude and will be better :)

If your having problems with fixed point, one thing you could do is run the simulation at a different speed than the rest of your game. For example only update every 10 frames, so each frame calculate 1/10th of the objects, this way you ammoritize an update over a bunch of frames, a much easier alternative to converting to fixed point.
Yeh thats too many, after looking at my code I am doing 25k per frame or so and that is pushing it, so reduce your ops by over an order of magnitude and will be better :)

It's no longer the problem :). I had totally rewrite it using fixed and shifts where possible. Huge boost :).

If your having problems with fixed point, one thing you could do is run the simulation at a different speed than the rest of your game. For example only update every 10 frames, so each frame calculate 1/10th of the objects, this way you ammoritize an update over a bunch of frames, a much easier alternative to converting to fixed point.

That is bad - simulation won't be smooth. In fact, I had to make several phisics calculations per graphics frame, because my time delta should be very small - otherwise the system may become unstable.

But, its no longer the problem anyway. There's another :). Now I need to collide them. Well, it's good, I have my own collision detection function.
No good. Its too damn slow :(. Based on angles calculations. muls, divs, atan - expensive things.
I need to collide rotated rectangles, so I have to check each rectangle's point hitting each rectangle. Yes, I have trivial rejection if they are too far from each other. No use..

What do you guys do in such a case? I can't treat them as circles and I need to know intersection point..
Apply bounding spheres first and do a quick and dirty intersection test.
After you found that bounding spheres are intersecting you can do more advanced intersection tests.
xPeterx wrote:Apply bounding spheres first and do a quick and dirty intersection test.
After you found that bounding spheres are intersecting you can do more advanced intersection tests.


Thats precisely what I do :). But the point is that my "advaced intersection" sucks :).
This shouldn't be very expensive at all. Transform one rect so it is in the frame of reference to the other rect. Store your edges similar to plane equation (don't know what it is called for a line), where you have a normal and a distance from origin. Then you can just do a dot product to determine which side of the line (inside or outside) a point lies on. Test the vertices of your second rect to see if any lie on the inside of the first rect. Only if one or more verts lie inside the rect will division be required to determine the exact intersection point of the segment against the line. A dot product in 2D is just two multiplications and an addition, so you would have 32 mults and 16 adds to exactly determine if 2 rectangles collided (exluding the math to transform the frame of reference, which is also only mults and adds - matrix math).

Once all that work is done you transform the collision point back into world coords.

Dan East
Here's a better method:
http://mathforum.org/library/drmath/view/54386.html

Scroll down to the last message. That requires 24 mults and also has the advantage of working in world coordinates.

Dan East
if your only doing bounding spheres for each object, precalc and do them for each triangle as well, its all a memory vs speed tradeoff. you could also precaclulate or cache certain variables or equations that didn't change which may be specific to certain triangles or previously intersected triangles, so you don't have to calculate everything next frame if the objects didn't move or are moving very slow.