4

I am learning about branch prediction in C++ and have a fairly straightforward question.

It seems the __builtin_expect term tells the compiler which clause to put first in the assembly code.

However if this is added manually by the programmer how is it any different than simply switching the if/else clauses in the C++ code?

Newd
  • 2,174
  • 2
  • 17
  • 31
  • 1
    possible duplicate of [Why do we use \_\_builtin\_expect when a straightforward way is to use if-else](http://stackoverflow.com/questions/7346929/why-do-we-use-builtin-expect-when-a-straightforward-way-is-to-use-if-else) – NathanOliver Jul 02 '15 at 15:14

1 Answers1

1

The compiler's optimiser is allowed to reorder branches. The __builtin_expect is useful if the compiler gets (or is likely) to get it wrong.

Alexander Balabin
  • 2,055
  • 11
  • 13