optimization of switch statements on i386
Godmar Back
godmar@gmail.com
Sat Feb 9 00:31:00 GMT 2008
Hi,
I have a question regarding the compilation of switch statements for
i386 targets. What heuristics or algorithm does gcc use to decide
whether to use a binary search or a jump table? I'm specifically
interested in switch statements for a dense range of values and in
which the default: branch is unreachable.
I've tried a number of approaches in order to obtain a jump table, but
it seems gcc 4.x always ends up creating binary searches. For
instance, I've tried casting the switch value to a limited range enum
and placing a gcc_unreachable() in the default: case. A cursory
inspection of the source code also didn't reveal any documentation of
the strategy used. Does gcc have reason to believe that a binary
search is always faster? If so, is this true for all variants of i386?
Would it depend on the number of case arms?
Thanks for any insights you could provide.
- Godmar
More information about the Gcc-help
mailing list