See the program fragment below.
I have tried it on VC8 x64.
//--------------------------------------------
extern unsigned char fetch_byte();
void dispatch() {
while(true) {
switch(fetch_byte()) {
case 0: ...; break;
case 1: ...; break;
...
case 255: ...; break;
}
}
}
//---------------------------------------------
VC generates a jump table for the switch statement. The case is
exhaustive with respect to the unsigned char type. However, VC generates
a bound check before indexing in the jump table:
movzx r11d, al
cmp r11d, 255 ; 000000ffH
ja SHORT $LL260@dispatch_c
mov edx, DWORD PTR $LN776@dispatch_c[rdi+r11*4]
add rdx, rdi
jmp rdx
The first three lines here can be ommitted.
The question is: how do I make the compiler to omit the checks?
Note, that the same problem appears with an exhausinve switch for an
enum type.
Thanks in advance,
Artem