View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Does anyone use fixed point math anymore?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

31 replies to this topic

Posted 11 March 2013 - 07:28 AM

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?

Edited by GradualGames, 11 March 2013 - 09:39 AM.

### #2Burnt_Fyr  Members

Posted 11 March 2013 - 08:36 AM

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.

### #3jwezorek  Members

Posted 11 March 2013 - 11:55 AM

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, 11 March 2013 - 11:57 AM.

### #4Álvaro  Members

Posted 11 March 2013 - 12:20 PM

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, 11 March 2013 - 12:20 PM.

### #5tivolo  Members

Posted 11 March 2013 - 12:24 PM

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 .

### #6dave j  Members

Posted 11 March 2013 - 12:29 PM

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.


### #7scniton  Members

Posted 11 March 2013 - 02:01 PM

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.

### #8deftware  Prime Members

Posted 11 March 2013 - 02:04 PM

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.

Edited by radioteeth, 11 March 2013 - 02:05 PM.

### #9deftware  Prime Members

Posted 11 March 2013 - 02:13 PM

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.

### #10jwezorek  Members

Posted 11 March 2013 - 02:20 PM

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, 11 March 2013 - 02:23 PM.

### #11wintertime  Members

Posted 11 March 2013 - 02:22 PM

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, 11 March 2013 - 02:25 PM.

### #12achild  Members

Posted 11 March 2013 - 02:29 PM

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, 11 March 2013 - 02:31 PM.

### #13achild  Members

Posted 11 March 2013 - 02:30 PM

 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, 11 March 2013 - 02:31 PM.

### #14tivolo  Members

Posted 11 March 2013 - 02:56 PM

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, 11 March 2013 - 02:57 PM.

### #15Geometrian  Members

Posted 11 March 2013 - 04:31 PM

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.

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.

### #16scniton  Members

Posted 11 March 2013 - 05:25 PM

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)

### #17deftware  Prime Members

Posted 11 March 2013 - 05:47 PM

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.

### #18C0lumbo  Members

Posted 12 March 2013 - 02:21 AM

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.

### #19Olof Hedman  Members

Posted 12 March 2013 - 03:09 AM

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, 12 March 2013 - 03:13 AM.

### #20samoth  Members

Posted 12 March 2013 - 04:58 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.