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?
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.
I've found The microarchitecture of Intel, AMD and VIA CPUs to be a good source for questions like yours.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With