Question about how switch statements are implemented

Hans Petter Selasky hselasky@c2i.net
Sat Nov 24 12:23:00 GMT 2007


Hi,

In simple cases like linear sequences, will the GCC compiler make a jump 
table?

switch (x) {
case 0:
case 1:
case 2:
case 3:
}

In all other cases where a jump table is not applicable, will the GCC compiler 
sort the values and do a binary search?

switch (x) {
case 1:
case 256:
case 65536:
case 512:
}

The reason for asking is that some time back I looked at the output from GCC 
3.4 in a complicated switch statement and the outputting code did nothing 
more than compare against all the values in the switch statement. This of 
course was O(N) instead of O(log2(N)).

--HPS

PS: Please CC me hence I am not on this list.



More information about the Gcc-bugs mailing list