Archive for the ‘Programming’ Category

Timing square root

The square root is one of those basic mathematical operations that’s totally ubiquitous in any game’s source code, and yet also has many competing implementations and performance superstitions around it. The compiler offers a sqrt() builtin function, and so do some CPUs, but some programmers insist on writing their own routines in software. And often […]

Code For Testing Virtual Function Speed

I’ve just updated my prior article on virtual function overhead with corrected timing numbers — I hadn’t noticed that my CPU cycle counts were only 32 bits wide so timings of more than 2secs would wrap back around to zero. If you want to run this test on your own hardware, I’ve put my code […]

How Slow Are Virtual Functions Really?

Whenever I work with virtual functions I find myself wondering: how much is it costing me to perform all these vtable lookups and indirect calls? The usual truism is that computers are so fast now that it doesn’t matter and that the idea of virtuals being a problem is just another myth. Our beloved Xenon […]

More About Rounding On MSVC-x86

For best float-to-int conversion speed, specify the compiler flag /arch:SSE2 to assume the availability of SSE2 opcodes.

Why You Should Never Cast Floats To Ints

The seemingly simple act of assigning an int to a float variable, or vice versa, can be astonishingly expensive. In some architectures it may cost 40 cycles or even more! The reasons for this have to do with rounding and the load-hit-store issue, but the outcome is simple: never cast a float to an int […]

Down With fcmp: Conditional Moves For Branchless Math

Branching on floating-point values can be a significant performance penalty. In some cases, using the ternary operator ?: or substituting a conditional-move operator like fsel allows you to make your function branchless and run much faster.

Sentences That Should Be Carved Into Foreheads

Non-desktop platforms don’t have paged memory. If the application exhausts memory, it dies.
The lack of paged memory means that memory fragmentation can kill an application.
Therefore, game applications cannot leak memory. Ever. Period.

December Reading List

In the realm of “things I really wish I had paid more attention to while we were crunching on Left 4 Dead“, I’ve recently been going through the talks from last June’s Gamefest. There is a lot of really great stuff in there, and I’ve only skimmed through a few of them so far, but […]

Sorting Algorithms, Animated

David R. Martin of Boston College made this great webpage demonstrating eight sorting algorithms and their performance under best-case, worst-case, and average-case conditions. It’s a clever, elegant way to show how there is no such thing as “one best sorting algorithm”, but that each has its own advantages and disadvantages depending on the expected data and […]

Clever flash game teaches embedded programming

Here’s a clever take on the program-the-robot game that does a nice job of teaching of how to think in terms of subroutines and perform instruction-space optimization — in this case the speed-space tradeoff definitely favors space. It feels a lot like programming shaders!