Does anyone use fixed point math anymore?

Started by
28 comments, last by Hodgman 11 years, 1 month ago

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?

Advertisement

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.

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.

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.

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

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.
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.
Stop twiddling your bits and use them already!

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.

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.

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.

This topic is closed to new replies.

Advertisement