title: How far does GCC's __builtin_expect go?
body: <p>While answering another question I got curious about this. I'm well aware that</p>
  
  <pre><code>if( __builtin_expect( !!a, 0 ) ) {
      // not likely
  } else {
      // quite likely
  }
  </code></pre>
  
  <p>will make the "quite likely" branch faster (in general) by doing something along the lines of hinting to the processor / changing the assembly code order / some kind of magic. (if anyone can clarify that magic that would also be great).</p>
  
  <p>But does this work for a) inline ifs, b) variables and c) values other than 0 and 1? i.e. will</p>
  
  <pre><code>__builtin_expect( !!a, 0 ) ? /* unlikely */ : /* likely */;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>int x = __builtin_expect( t / 10, 7 );
  if( x == 7 ) {
      // likely
  } else {
      // unlikely
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>if( __builtin_expect( a, 3 ) ) {
      // likely
      // uh-oh, what happens if a is 2?
  } else {
      // unlikely
  }
  </code></pre>
  
  <p>have any effect? And does all of this depend on the architecture being targeted?</p>