Mill Computing, Inc. Forums The Mill Architecture Can branch predictors perfectly predict counting loops? Reply To: Can branch predictors perfectly predict counting loops?

goldbug
Participant
Post count: 53

Could that be generalized to any arbitrary branch?

For example, consider this code:


int foo(int a) {
   // just whatever random code
   int b = a * a;
   int c = b * b;
   int d = a + b + c;

   // a branch that could be precalculated at the beginning of the function
   if (a) {
      return bar(d);
   } else {
      return baz(d);
   }
}

At the beginning of the function, we have enough information to know whether we will take that branch or not. A compiler could potentially introduce an instruction at the beginning to precalculate if the branch will be taken and notify the predictor, or there could be “delayed branch” instruction something like “branch in 4 cycles if a is non zero”.

This kind of code shows all the time in loops and precondition checking in functions.

  • This reply was modified 12 months ago by  goldbug.
  • This reply was modified 12 months ago by  goldbug.
  • This reply was modified 12 months ago by  goldbug.