Advertisement

fixed point math

Started by January 13, 2000 11:49 PM
2 comments, last by hugg 24 years, 11 months ago
Who still has opinions on fixed vs floating point numbers in games? I started in the pre-Pentium world, where fixed-point and "imul" were your friends in getting speedy performance ... but then Quake and Abrash broke my little world apart by insinuating that the FPU was now spiffy enough for games -- in some cases, more spiffy than integer math. I believe there''s still a case for both, however, and here''s why: * Fixed point numbers have the same precision across their entire range -- which prevents problems like physics being slightly different in two different parts of the world. * You can easily define your own fixed point number, choosing the # of bits for fractional and mantissa parts. Not so easy with floating point, you''re limited to whatever the hardware supports unless you write your own routines. * Fixed point numbers have truncation behavior which may be advantageous -- for instance, if you have a wrap-around universe where the world is a size that''s a power of 2. * Addition, subtraction, and multiplying by powers of 2 is usually faster than floating point. * Fixed-point numbers don''t have incompatibility problems across platforms (this may not be as much of a problem anymore, right?) FIXED POINT ADVANTAGES: And the cons for fixed point: * Floating point handles a much bigger range for a given # of bits. * For performance, most fixed-point implementations silently truncate overflow results, which can cause problems (of course, floating point can overflow and generate exceptions) * Fixed-point numbers are not as well integrated into computer languages as floating-point is. C++ templates can help even the gap, but it is not part of the standard library. Does this sound reasonable? Any other comparison points? My alterior motive for this (besides spurring good numerical conversation) is to ask if there is a good fixed-point C++ template available, or should I use a home-grown one. I plan to use both for my game -- fixed-point for world coordinates, and floating-point for quantities that are more sensitive like velocity & force vectors.
I couldn''t have agreed with you more if I had wrote the message. As far as which method to use, I don''t bother with templates since a well defined class can easily be used with multiple projects.

Slightly off the subject, a new technique I started using recently with fixed point is using preprocessor commands to mask the fixed point at will. This helps me figure out if fixed point or floating point is better in a certain circumstance.
Advertisement
I too started in a world where fixed point was the way to go, but I don''t think it''s applicable anymore. Here''s why:

Modern FPU''s have shortened the gap between integer multiplies and floating point multiplies. Whereas the idiv instruction seems to be still taking the same number of cycles.

SIMD instructions on floating point are now available on desktop platforms. In two to three years they will be on all computers sold.

Amdahl''s law. Floating point calculation take significantly less time that blits or other memory operations. Especially on today''s superscalar pipelines. Compare the speed that processors have increased over the memory speed increase.

The floating point calculations just aren''t much of a bottleneck anymore.

I don''t like templates that much, but they come in handy in this case -- I have a C++ fixed-point template, and a 2D vector template. Now I can define a 2D fixed-point vector class like this:

typedef TVec2< TFixed<15,int> > CFixed2Vec;

The <15,int> means use 15 fractional bits, and store it in an int. And if I want to use floats:

typedef TVec2 CFloat2Vec;

Not that it''s that hard to write two different vector classes, but we want to save our fingers ;-)

I am interested in floating-vs-fixed point performance tho -- know any good links?

This topic is closed to new replies.

Advertisement