Compile Time Assertions

Standard preprocessor checks can be very useful to catch programming errors. Nevertheless, these checks can be very limited, as they can only evaluate preprocessor expressions, rather than compile time values like sizeof. Presented here is a small piece of code to create compile time checks

Posted by Ami Chayun | | No comments

Variadic arguments in C/C++

Very few programming languages allow the user to define functions with variable number of parameters. Although this method is very powerful, it should be used carefully. In this article we will explore variadic functions and macros, as well as ways to better utilize this capability.

Posted by Ami Chayun | | No comments

Shared Memory In Linux

Linux, like most POSIX / System V compatible operating systems prefer processes over threads, as a matter of fact, a POSIX thread is nothing but a process with a layer of abstraction.

Posted by Ami Chayun | | [3] comments

Optimizing Singletons With Double Checks

Writing a thread safe singleton has its difficulties. One of them is making sure that the dynamic creation of the inner singleton object is thread safe, while locking as few mutexes as possible.

Posted by Ofer Kapota | | [7] comments

Magical Square Root Implementation In Quake III

Any 3D engine draws it’s power and speed from the mathematical models and implementations within, and trust John Carmack of ID software for using really good hacks. As it turns out, a very interesting hack is used in Quake III to calculate an inverse square root.

Posted by Tomer Margolin | | [27] comments

Fast Atomic Counters With the x86 LOCK Prefix

Atomic counters are the foundation of any thread safe reference counter. They are also common in daily use for thread counting and many other uses. Regularly, implementing thread safe atomic counters can be done in several ways, but always with a performance cost. In this review we will examine a method to implement a reference counter with a single assembly instruction.

Posted by Ami Chayun | | No comments

Controlling the Packing Alignment to Minimize Memory Consumption in C/C++

Do you know how much memory your structs and classes require? Unless you plan carefully, structs and classes may require as much as twice memory than actually needed, due to the compiler’s default packing alignment. The pack directive can be used in order to define the best packing alignment for your code.

Posted by Tomer Margolin | | One comment

Loop Unrolling with Duff’s Device

Loop unrolling, or unwinding, is simply reducing the number of overhead instructions that the CPU has to execute in a loop, thus improving the cache hit rate and the loop’s run time.

Posted by Tomer Margolin | | No comments

Journey into CPUID

Some programs rely on specific CPU instructions, with features like MMX, 3DNow!, Hyper Threading, etc. Some of them even rely on the specific CPU version. How would they know for sure that the CPU has what it takes to operate?

Posted by Tomer Margolin | | No comments