The Linux kernel uses several special capabilities of the GNU Compiler Collection (GCC) suite. These capabilities range from giving you shortcuts and simplifications to providing the compiler with hints for optimization. Discover some of these special GCC features and learn how to use them in the Linux kernel.
The article introduces some obscure yet useful GCC extensions with sample code from the linux kernel. The extensions are categorized into functionality and optimization. Some of my favorite extensions from the list are:
- Type Discovery. This is a simplified version of templates found in C++. But its pretty cool to write a generic min() or max() function using this extension.
- Range Extension. Now this is an awesome extension that can save some typing (or copy/pasting). It lets you use a range in Switch … Case statements. You can also initialize an array range with a value.
- Branch Prediction and Pre-fetching. These extensions are optimization hints that help the compiler to produce efficient machine code. Branch Prediction is used in if-else conditions to notify which condition is more likely to be true. Pre-fetching is manually populating the L1 and L2 cache with appropriate data which results in reduced latency, thus improving the performance.