Branch prediction macros in GCC Last Updated : 28 May, 2017 Summarize Comments Improve Suggest changes Share Like Article Like Report One of the most used optimization techniques in the Linux kernel is " __builtin_expect". When working with conditional code (if-else statements), we often know which branch is true and which is not. If compiler knows this information in advance, it can generate most optimized code. Let us see macro definition of "likely()" and "unlikely()" macros from linux kernel code "http://lxr.linux.no/linux+v3.6.5/include/linux/compiler.h" [line no 146 and 147]. C #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) In the following example, we are marking branch as likely true: C const char *home_dir ; home_dir = getenv("HOME"); if (likely(home_dir)) printf("home directory: %s\n", home_dir); else perror("getenv"); For above example, we have marked "if" condition as "likely()" true, so compiler will put true code immediately after branch, and false code within the branch instruction. In this way compiler can achieve optimization. But don't use "likely()" and "unlikely()" macros blindly. If prediction is correct, it means there is zero cycle of jump instruction, but if prediction is wrong, then it will take several cycles, because processor needs to flush it's pipeline which is worst than no prediction. Accessing memory is the slowest CPU operation as compared to other CPU operations. To avoid this limitation, CPU uses "CPU caches" e.g L1-cache, L2-cache etc. The idea behind cache is, copy some part of memory into CPU itself. We can access cache memory much faster than any other memory. But the problem is, limited size of "cache memory", we can't copy entire memory into cache. So, the CPU has to guess which memory is going to be used in the near future and load that memory into the CPU cache and above macros are hint to load memory into the CPU cache. This article is compiled by Narendra Kangralkar. Comment More infoAdvertise with us K kartik Follow Improve Article Tags : C Language c-puzzle GCC AdvanceC C-Macro & Preprocessor +1 More Similar Reads C Preprocessors Preprocessors are programs that process the source code before the actual compilation begins. They are not part of the compilation process but operate separately, allowing programmers to modify the code before compilation. It is the first step that the C source code goes through when being converted 8 min read C Preprocessor Directives In C programming, the preprocessor is a program that process the source code before the actual compilation begins. It uses preprocessor directives are commands that instruct the preprocessor to perform specific actions. These directives start with the # symbol.List of Preprocessor DirectivesThe foll 6 min read How a Preprocessor works in C? Compiling a C program - Behind the Scene A Preprocessor is a system software (a computer program that is designed to run on computer's hardware and application programs). It performs preprocessing of the High Level Language(HLL). Preprocessing is the first step of the language processing system. Lan 3 min read Header Files in C In C programming, a header file is a file that ends with the .h extension and contains features like functions, data types, macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor.C language uses header files to provide the standard 5 min read Whatâs difference between header files "stdio.h" and "stdlib.h" ? In C programming, standard header files provide various inbuilt functionalities and two of the most commonly used standard header files are stdio.h and stdlib.h. The <stdio.h> provides Standard Input Output tools such as printf(), scanf(), etc while <stdlib.h> provides some commonly used 4 min read How to write your own header file in C? As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question ar 4 min read Macros and its types in C In C programming, a macro is a symbolic name or constant that represents a value, expression, or code snippet. They are defined using the #define directive, and when encountered, the preprocessor substitutes it with its defined content.ExampleC#include <stdio.h> // Macro definition #define LIM 4 min read Interesting Facts about Macros and Preprocessors in C In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. by this we mean to say that the â#â symbol is used to process the functionality prior than other statements in the program, that is, which means it processes some code before 5 min read # and ## Operators in C In C, # and ## operators are preprocessor operators using in macros for token manipulation. They are known as stringizing and token pasting operators and are used in macro definition with #define preprocessor. In this article, we will learn about these operators and how to use them in C programs.Str 3 min read How to print a variable name in C? Printing a variable name means printing the identifier that is assigned to the variable. To print it, it should be in the form of string. This can be done by using stringification.Stringification in C is the method to convert the argument of a function like macro to a string. This can be done with t 1 min read Like