Advertisement

Does anyone use fixed point math anymore?

Started by March 11, 2013 01:28 PM
28 comments, last by Hodgman 11 years, 11 months ago

Fixed-point is more natural for certain situations, like representing the position of an object in a huge world: It doesn't make much sense to have more resolution near some arbitrary point we take as origin. However, using floating-point numbers for everything is less of a headache, so that's what I expect most people to use.

You dont even need fixed point for this. Just never store vertices in world space.

Keep your models each in their own coordinate system near 0,0,0.

Combine model and view matrix(possibly with doubles), this cancels out those huge distances.

Apply that single matrix in one step to all vertices of the model and everything near the camera will have the best resolution.

Slight twist:

For very simple operations, such as scaling an image in software, it is actually faster to use fixed point. Not because it is faster per se, but because converting to float, doing a simple add (or whatever), and converting back to integer, 10s or 100s of millions of times is slower than using fixed point and keeping everything in the integer registers in the first place.

Advertisement

[edit] It is much better with SIMD, but even then you still might need a solution for the general case, and this would be important to realize.

[edit again] Oops, meant for this post to be edited into the above post.

I've always wondered about this question, and decided to spent the past 30 min using what I know to write a test in C, compiled using gcc with all optimization levels and reached the conclusion that fixed point is actually much slower.. again, this test was executed using only what I already know about doing fixed-point, and could be erroneous, but I feel confident in my work..

here's a screenie

http://dl.dropbox.com/u/62846912/FILE/fixedvsfloat.jpg

and here's the source + exe

http://dl.dropbox.com/u/62846912/FILE/fixedvsfloat.zip

.. and for those who are having problems downloading an exe in a zip off dropbox

http://dl.dropbox.com/u/62846912/FILE/fixedpoint.c

I'm glad that I have settled this for myself, at least. The slowness does not seem to be a product of any of the conversion to/from floating to fixed (I moved it around, in and out of loops, etc) but instead seems to be a result of merely performing arithmetic operations using integers themselves.. therefore, it is almost invariably wiser to always do everything using floating point, so far as the pursuit of speed is concerned.

Your test isn't entirely accurate because it causes a load-hit-store each time you convert from fixed-point to float, and vice versa. That alone causes a performance penalty on certain CPUs (Wii/Xbox360/PS3).

Apart from that, yes, on most CPUs floating-point math will be slower, but it's still good to know fixed-point math (see the above comment about ARM processors).

Furthermore, I encourage everybody to really understand all the subtle intricacies of floating-point math (and god, there are many, check Bruce's list) before just blindly putting in floats everywhere in their program. I still consider knowing fixed-point math useful.

So for fixed point arithmetic to be an optimization again it would have to be when targeting a strange platform that doesn't give you floating point operations for free. I can't think of one, but maybe some hobbyist embedded project or something like that.

C# on XBox360. The processor in the 360 clearly supports FP calculations, but it appears that C# emulates it anyway. I looked around, but no official reason why turned up--a couple people I talked to averred that it was one way Microsoft keeps indie developers in line.

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

I've always wondered about this question, and decided to spent the past 30 min using what I know to write a test in C, compiled using gcc with all optimization levels and reached the conclusion that fixed point is actually much slower.. again, this test was executed using only what I already know about doing fixed-point, and could be erroneous, but I feel confident in my work..

here's a screenie

http://dl.dropbox.com/u/62846912/FILE/fixedvsfloat.jpg

Those weren't exactly the result I would expect from quick test code, so I dove down into the code and made some changes to make it closer to a fair comparison.
The test still has the issue that it assumes that +,-,* and / are equally common.

Modified code: http://pastebin.com/mvPR2snF
(Note: some changes are purely for preventing the optimizer from removing entire sections.)

I tried it with multiple compiler flags:

Compiler: gcc 4.6.2 (mingw) Flags: (none)
Typical result: 2.3s for floating point, 2.75s for fixed point (1.2 times slower)

Compiler: gcc 4.6.2 (mingw) Flags: -O2
Typical result: 1.3s for floating point, 1.65s for fixed point (1.27 times slower)

Compiler: gcc 4.6.2 (mingw) Flags: -O2 -mfpmath=sse -march=native

Typical result: 0.9s for floating point, 2.0s for fixed point (2.2 times slower)

Stop twiddling your bits and use them already!
Advertisement

Your test isn't entirely accurate because it causes a load-hit-store each time you convert from fixed-point to float, and vice versa.

The test is between the use of fixed and floating point arithmetic, conversion is a part of the test. I decoupled the conversion by using an inner loop that excludes the conversion, and played with the loop ratios to draw the same conclusion.

The test still has the issue that it assumes that +,-,* and / are equally common.

I thought of it as simulating that they were all necessitated by some unknown imaginary application. Your results are much more what I had expected, but equally surprising and conclusive, at least for my purposes and intents. Thanks for sharing.

[ deftware.org ]

In addition to the uses for fixed point already stated (e.g. large worlds,) it is also used to make code deterministic across machines/platforms/etc.

It is possible to do this with floating point code but your milleage may vary (in my experience it is challenging.)

One example of this is RTS games where inputs are broadcast to all clients and each client must update their state and stay in sync.

I'm using fixed point in the parts of my current project that need to be deterministic for this reason. I think saying that floating point determinism is challenging is a bit of an understatement. With floating point, determinism can and will break across different compilers, different optimisation settings, and different CPUs (and seeing as iOS, Android and PCs all support multiple CPUs, that's some significant platforms where fp determinism can't be 100% relied upon).

Of course, doing lock-step multiplayer which relies on perfect sync is pretty much out of fashion, but if you're going to do it, my advice is to avoid floating point numbers in your simulation.

Modified code: http://pastebin.com/mvPR2snF
(Note: some changes are purely for preventing the optimizer from removing entire sections.)

I think you still have a number of issues in your code that skew the result.

One thing is that you do an float->int conversion for each int operation, this will likely slow down the int operations more then they should.

The other is that you use base 10 for the precision, you should use base 2, and then a lot of muls and divs will become shifts.

Fixed point is nice, and I find it pretty intuitive when you get the hang of it, have had to use it a lot on different ARM processors.

But you can write bad fixed point code too, probably easier then you write bad float code, and that can hurt performance.

Nice thing with ARM is that you can get shifts more or less for free, speeds up the fixed point code even more.

Also has a 32bit * 32bit -> 64 bit instruction so you can do muls without losing precision, very nice :)


Fixed-point is more natural for certain situations, like representing the position of an object in a huge world

You dont even need fixed point for this. Just never store vertices in world space.
Keep your models each in their own coordinate system near 0,0,0.
No, that will not solve the issue. The reason why using floating point is "wrong" and fixed point is "correct" is not that vertices within a model are in world space, but that entire objects are (necessarily) in world space. Though floating point will still "work" in many situations.

An object (person, car, box, whatever) near the origin might have a position accurate to 5 nanometers, which seems "cool" but is not really necessary -- nobody can tell. On the other hand, an object on the far end of the world might only have a position accurate to 15 meters.

Bang. It is now entirely impossible for a person to walk, as you can only move in steps of 15 meters, or not at all (and people just don't leap 15 meters). It is also impossible to distinguish 10 people standing in a group, since they all have the exact same position. It is further impossible to properly distinguish between objects moving at different speeds. A bicycle moving at 25km/h stands still. A car moving at 53.9 km/h stands still, but a car moving at 54km/h moves at 54km/h.

This is inherent to how floating point math works (it is a "feature", not so much a "bug"). Fixed point does not have that feature. A person can walk the same speed and be positioned the same at the origin or at the far end of the world.

It doesn't matter that the vertices you render are in their own coordinate system if the entire object cannot be simulated properly or if the entire object is culled away.

This topic is closed to new replies.

Advertisement