0

I have the following code;

for ( int i = 0; i < 100 ; i++) {
    for( int j = 0; j < 100 ; j++) {
        System.out.println(j+"");

In this code I am gonna predict the branches by the method of one bit branch prediction. Let us say the first preference is "branch not taken". According to these, what is the branch prediction accuracy?

In this context, should I calculate the two loops seperately? I mean are there two different -one bit prediction scheme- for the two loop? If so, it this the correct way of calculating accuracy?

for second loop:

  1. taken (wrong prediction as first preference is branch not taken)
  2. taken (correct)
  3. taken (correct)

and when j = 99 taken (correct) and when j = 100 not taken (wrong prediction)

So there are 2 wrong prediction when j = 0 and 100 so accuracy is 99/101 for second loop? or should I calculate the loops together?

Davis
  • 188
  • 1
  • 13
  • 1
    So you're assuming that `System.out.println` doesn't have any branches that alias with either of your loop branches, and doesn't otherwise disturb branch-prediction state for your loop? (e.g. Spectre v2 mitigation by flushing BTB if it makes a system call). Sounds unlikely, but let's pretend it's just an ALU instruction or an empty loop other than the loop overhead. – Peter Cordes Feb 28 '19 at 09:30
  • 1
    Are you asking if each loop branch will have its own history bit? Yes that would be the normal case. Using a single 1-bit shared global history would probably be worse for a lot of code than static prediction (assume all backward branches are taken, forward branches not-taken). – Peter Cordes Feb 28 '19 at 09:35

0 Answers0