Early Outs and Floating-Point Performance

One of the most fascinating things about Xbox 360 is how it turns traditional floating-point programming on its heels. Consider the CRT function double exp(double x), which returns the exponential of a number (e^x).
On older processors, math is fairly expensive, so you’d do as much as you could to avoid actually invoking mathematical instructions on the CPU (or math co-processor). For instance, if you were implementing exp you might do the following (I’ve ignored negative inputs for simplicity):
// Save some time by avoiding unnecessary math
if( x == 0.0 )
   return 1.0;
if( x == 1.0 )
   return e;
// Check for overflow/underflow
if( x > ReallyBigNum )
   return INF;
if( x < ReallySmallNum )
   return 0.0;
// OK, now actually do some real math here to calculate e^x
Xbox 360 is a mathematical monster. It’s much happier doing raw math than doing floating-point comparisons and branches. On Xbox 360, all those initial early-out comparisons could be more expensive than actually just doing the math!
One of the great things about updating the C runtime libraries for Xbox 360 has been eliminating this unnecessary code. Not only do you end up with a smaller library footprint (and better instruction cache utilization), but improved runtime performance as well. Naturally, exp() was optimized in other ways, too: floating-point exceptions were removed (not very important on a game console) and necessary branches were replaced with the faster fsel instruction. The result: exp() runs about 4X faster than the original. Yes — 400%. That’s an optimization well worth making.
The important thing: understanding the underlying architecture of your platform(s) is critical when it comes to performance. In general, modern processors are really, really good at floating-point math. Some of the tricks of the past no longer make sense. Make sure to measure your own early-out code and determine whether it’s providing value or not. You might be surprised. 
This entry was posted in Game Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s