1

Just using the dissembly window in VS2012:

if(p == 7){
00344408  cmp         dword ptr [p],7  
0034440C  jne         main+57h (0344417h)  
    j = 2;
0034440E  mov         dword ptr [j],2  
}
else{
00344415  jmp         main+5Eh (034441Eh)  
    j = 3;
00344417  mov         dword ptr [j],3  
}

Am I correct in saying a jump table has been implemented? If so, does this still cause CPU branching problems because the assembly still has to execute the cmp command?

I am looking at the performance costs of IF statements and was wondering if the compiler optimizing to a jump-table means no more CPU branching problems.

user997112
  • 29,025
  • 43
  • 182
  • 361

3 Answers3

2

There is no jump table here: the two jump instruction are on some absolute address:

jne         main+57h (0344417h) 
jmp         main+5Eh (034441Eh)

There is no indirection. Using a jump table doesn't solve at all the "CPU branching problems". The branch prediction cost with or without jump table should be similar.

hivert
  • 10,579
  • 3
  • 31
  • 56
  • Ah ok- so correct me if I am wrong but the only thing a branch table saves is multiple comparison instructions? So jump table means one comparison, if, else if, else if requires multiple comparisons- both causing CPU branch misprediction? – user997112 Feb 02 '14 at 17:55
  • There is no comparison for a jump table. The address of the jump is computed using some array access. You should read http://stackoverflow.com/questions/48017/what-is-a-jump-table or http://en.wikipedia.org/wiki/Branch_table – hivert Feb 02 '14 at 17:57
  • So the branch misprediction simply arises because option 1 was chosen, then option 3, then option 2, then option 3 again etc.... and there's no.... predicting! – user997112 Feb 02 '14 at 22:17
2

I wouldn't call that a jump table. A jump table is an array of destination addresses into which the index is computed dynamically from the user data on which you're switching. The code you showed is just a simple control flow with two alternative branches, with entirely statically coded control flow.

As a typical example, if (X) foo() else bar() becomes (in pseudo-code):

jump_if(!X, Label),  foo(),  jump(End),  Label: bar(),  End:
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

The closest way to express a jump table in pure C or C++ is using an array of function pointers.

switch constructs often become jump tables, although unlike the array of function pointers, those are indirect branch within a function instead of indirect call to a new function.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720