In GCC, branch prediction macros are used to provide hints to the compiler about the likelihood of a branch being taken. The two macros are __builtin_expect and _ _builtin_expect_with_probability.
__builtin_expect is used to indicate that a certain branch is likely or unlikely to be taken. The macro takes two arguments: the value being tested and the expected value. For example, the following code indicates that the branch is likely to be taken:
__builtin_expect_with_probability is a more advanced macro that allows for the specification of a probability for a branch to be taken. This macro takes three arguments: the value being tested, the expected value, and the probability of the branch being taken, represented as a floating-point value between 0 and 1. For example, the following code indicates that the branch has a probability of 0.7 of being taken:
It's worth noting that these macros are only hints to the compiler and the actual behavior of the code might not match the hints provided.
In this example, we are trying to get the user's home directory by calling the getenv() function. We know that in most cases, the home directory will be set and the branch where the home directory is printed will be taken. So we use the likely() macro to indicate to the compiler that the branch where home_dir != NULL is true is more likely to be taken.
In this way, the compiler will place the code for the true branch immediately after the branch instruction, reducing the number of cycles required to jump to the correct code path.
In contrast, if we expect that the home directory is unlikely to be set, we can use the unlikely() macro:
In this way, the compiler will place the code for the false branch immediately after the branch instruction, reducing the number of cycles required to jump to the correct code path.