• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
GradualGames

Does anyone use fixed point math anymore?

29 posts in this topic

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
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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.

Edited by radioteeth
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

Edited by achild
0

Share this post


Link to post
Share on other sites

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.

Edited by tivolo
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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)

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0