Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do switch statements in C empty the x86 pipeline?

Does hitting a switch statement in C (assuming that it uses a jump table) empty the pipeline of an x86 processor? I'm thinking that it might because it would need the result of the table lookup to know what instruction to execute next. Can it forward that result back early enough that the pipeline doesn't get totally emptied?

like image 928
Jeremy Avatar asked Dec 12 '22 09:12

Jeremy


2 Answers

A jump table does not necessarily empty the pipeline. Indirect branches are predicted on modern processors, and the branch predictors do a better job than you might expect. Obviously, a correctly-predicted indirect branch does not cause a stall.

Not branching at all is preferable, but often impossible (or introduces so much overhead as to be a net loss). Replacing jump tables with a sequence of conditional branches is sometimes beneficial, but only if the number of branches in the replacement sequence is reasonably small.

like image 138
Stephen Canon Avatar answered Dec 29 '22 16:12

Stephen Canon


I've found The microarchitecture of Intel, AMD and VIA CPUs to be a good source for questions like yours.

like image 32
Pete Wilson Avatar answered Dec 29 '22 16:12

Pete Wilson