# Does anyone use fixed point math anymore?

## Recommended Posts

Does anyone use fixed point math anymore for coordinates in games? I've gotten rather familiar with how to use them for various smooth movement effects, by programming an oldschool console game. I'm thinking of trying to use the same techniques for a modern game. Would this make most of you die laughing and wonder what planet I've been on for the last 13 years or is it still fairly common?

##### Share on other sites
Burnt_Fyr    1665

AFAIK fixed point as an optimization is dead. Both CPU and FPU can handle floating point math fast enough that they are unlikely to become bottle necks for the program, and working with fixed point for this purpose is premature optimization.

That being said, for large worlds where floating point precision can become well unprecise, fixed point math still has it's purpose.

##### Share on other sites
jwezorek    2663

Doing fixed point arithmetic was an optimization back when CPUs didn't  have "math co-processors", either as a separate thing or built-in. It used to be much slower to do floating point operations than to do integer operations. This hasn't been an issue since about 1994.

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.

Edited by jwezorek

##### Share on other sites
alvaro    21247
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. Edited by Álvaro

##### Share on other sites
tivolo    1367

Doing fixed point arithmetic was an optimization back when CPUs didn't  have "math co-processors", either as a separate thing or built-in. It used to be much slower to do floating point operations than to do integer operations. This hasn't been an issue since about 1994.

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.

On the Nintendo DS, all floating-point operations where emulated in software, because neither processor in there had an FPU. You absolutely had to work with fixed-point arithmetic instead of floating-point. Granted, the Nintendo DS came out in 2004, but I still think it's useful to know your way around fixed-point math even today. There's occasions where using a floating-point number just isn't a good choice, and fixed-point is a better alternative.

Unfortunately, it's not something people get taught at universities or similar :(.

##### Share on other sites
dave j    681
It's worth noting that not all ARM processors have FP hardware. e.g. Android's native development kit only supports hardware FP on ARMv7 and above processors. Many low end Android phones use earlier architectures and the NDK will emulate FP on those systems.

From the CPU-ARCH-ABIS.html file in the NDK:

 I.1. 'armeabi'
--------------

This is the name of an ABI for ARM-based CPUs that support *at* *least*
the ARMv5TE instruction set. Please refer to following documentation for
more details:

- ARM Architecture Reference manual                (a.k.a  ARMARM)

--snip--

This ABI does *not* support hardware-assisted floating point computations.
Instead, all FP operations are performed through software helper functions
that come from the compiler's libgcc.a static library.


##### Share on other sites
scniton    252
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.

##### Share on other sites
deftware    1778

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

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.

##### Share on other sites
deftware    1778

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 actually experienced floating-point determinism being varied across machines in an engine which 'planted' trees around terrain at game load time using a procedural method of creating potential points all over the terrain and checking the slope of the ground at those points one at a time before generating a new point to test, and in some cases it would throw off the scene generation algorithm so wildly that the two machines in the same game would be in two geometrically different worlds due to the PRN code becoming out of sync when some trees would get planted and others wouldn't based on floating point precision nuances in the slope check.

##### Share on other sites
jwezorek    2663

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..

I'm confident in your work too. I wouldn't be if it came out the other way ... Look fellas, the thing you need to understand is that computers used to be really slow and game programming used to be, actually, a lot harder.

In those days you would use fixed point because you had to if you were writing a game that involved a lot of math (like, say, anything 3D as there were no GPUs). This wasn't some thing done by crazy programmers with time on their hands to achieve a slight speed-up; it was the way you wrote math intensive games. Those days are over but there is still lingering advice sprinkled around the internet because there once was a different era in which fixed point programming was fundamental knowledge that was necessary to be a game programmer.

Edited by jwezorek

##### Share on other sites
wintertime    4108

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.

Edited by wintertime

##### Share on other sites
popsoftheyear    2194

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.

Edited by achild

##### Share on other sites
popsoftheyear    2194

 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.

Edited by achild

##### Share on other sites
tivolo    1367

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

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.

Edited by tivolo

##### Share on other sites
Geometrian    1810

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.

##### Share on other sites
scniton    252

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)

##### Share on other sites
deftware    1778

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.

##### Share on other sites
C0lumbo    4411

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.

##### Share on other sites
alh420    5995

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 :)

Edited by Olof Hedman

##### Share on other sites

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.

##### Share on other sites
scniton    252

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.

The old code had these issues, I believe you're quoting the wrong person.

##### Share on other sites
RobTheBloke    2553

Does anyone use fixed point math anymore for coordinates in games?

Depends if you think accounting makes for a fun game?

I'm thinking of trying to use the same techniques for a modern game. Would this make most of you die laughing and wonder what planet I've been on for the last 13 years or is it still fairly common?

Outside of the world of microcontrollers, or financial applications, there isn't a lot of sense to using fixed point anymore. There may be a reason it could still come in useful (e.g. compressing game assets using 16bit floats for example), but that's not really fixed point, and most sane people would simply convert to a 32bit float a.s.a.p. There simply isn't a case where a floating point op is any slower than the equivalent integer op (mostly, on most architectures, but there are exceptions)

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 can see some shifts disguised as mult / div here:

                iv = ((int64_t)iv*ivalue[2])/PRECISION;
iv = ((int64_t)iv*PRECISION)/ivalue[3];

But you'd the compiler would already spot that....

                iv = (iv*ivalue[2]) >> 10;
iv = (int64_t(iv) << 10)/ivalue[3];
Edited by RobTheBloke

##### Share on other sites
Hodgman    51234

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).

Assuming you're working in metres and need 1mm of accuracy, this isn't much of a problem unless your game world is a few thousand kilometers across. Fixed point grants you a few more orders of magnitude, but at those scales you'd soon have to switch to a hierarchy of coordinate systems anyway.

##### Share on other sites
EWClay    659
I had precision problems with a world size of 10 kilometres, though it was only noticeable from some instability in the physics simulation.

The solution was not to switch to fixed point, but to store world positions in double precision. Everything thing else - relative positions, velocities, rotations - stayed in single precision, so it was practically a one line fix and required very little extra storage.

It's no slower, either: unless you are using SIMD, the CPU uses higher precision in registers anyway. It's only when you store to memory that a float becomes 32 bit.

##### Share on other sites
quasar3d    814

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 actually experienced floating-point determinism being varied across machines in an engine which 'planted' trees around terrain at game load time using a procedural method of creating potential points all over the terrain and checking the slope of the ground at those points one at a time before generating a new point to test, and in some cases it would throw off the scene generation algorithm so wildly that the two machines in the same game would be in two geometrically different worlds due to the PRN code becoming out of sync when some trees would get planted and others wouldn't based on floating point precision nuances in the slope check.

If both were running the same executable, then this really can't happen. The result of a floating point instruction is defined exactly (the rule is that it should return the nearest floating point number to the exact result, and round to even on ties). Also, if you don't have options like fast math enabled, then the same thing is true for C/C++ code (at least if it's IEEE 754 compliant), so it's likely that this difference was caused by something else.