
On Thu, 10 Dec 2009 05:50:43 -0800, Steven Watanabe <watanabesj@gmail.com> wrote:
There are many ways that a compiler can handle a switch statement. a) sequential if/else b) binary search c) jump table
And probably other ways, as the cases number rise I've seen only jump tables in binaries. if/else are hidden with the rest of the code making it not trivial to see it's not a switch. I'm not sure I understand what you're referring to when you talk about binary search? What kind of implementation is this and what are the benefits?
Also, a jump table does not necessarily prevent the processor from predicting the branch.
Probably, I guess there is much more intelligence in the branch prediction algorithms than I can imagine. However, AFAIK when the branch prediction fails it needs to wait for all the registers to be loaded and the computation to be done to get the good address. That's a serious stall. Well perhaps it's not a major issue if you consider the penalty you get for any branch fail, but I think this penalty has been greatly reduced since Core 2 (compared to P4). Main point : switches are slower for the same reason a if/then/else maze is slow. -- EA