sio2 wrote: This has contiguous indices so of course it can be reduced to a jump table. But key codes are not necessarily contiguous indices. [I don't know if the compiler can boil the list down to sets of contiguous indices though].
I know TIGCC had optimizations to do this, I'm almost sure that GCC has them.Halifax wrote:On another note about optimization, most compilers should optimized non-sequential switch-cases by adding padding to the jump table if warranted.
EDIT:
I got a chance to test it, and I was correct. It either pads the jump table or predicts every value prior with a byte table (for indicies [0,255]) or a word table (for indicies [0,65536]), etc.
Example #1:
Code: Select all
int func2(int a)
{
switch( a )
{
case 8: return 1;
case 3: return 2;
case 5: return 3;
case 1: return 4;
case 9: return 5;
}
return 0;
}
?func2@@YAHH@Z PROC
dec eax
cmp eax, 8
ja SHORT $LN6@func2
jmp DWORD PTR $LN10@func2[eax*4]
$LN5@func2:
mov eax, 1
ret 0
$LN4@func2:
mov eax, 2
ret 0
$LN3@func2:
mov eax, 3
ret 0
$LN2@func2:
mov eax, 4
ret 0
$LN1@func2:
mov eax, 5
ret 0
$LN6@func2:
xor eax, eax
ret 0
npad 2
$LN10@func2:
DD $LN2@func2
DD $LN6@func2
DD $LN4@func2
DD $LN6@func2
DD $LN3@func2
DD $LN6@func2
DD $LN6@func2
DD $LN5@func2
DD $LN1@func2
?func2@@YAHH@Z ENDP ; func2
Code: Select all
int func2(int a)
{
switch( a )
{
case 8: return 1;
case 3: return 2;
case 5: return 3;
case 11: return 4;
case 19: return 5;
}
return 0;
}
?func2@@YAHH@Z PROC
add eax, -3
cmp eax, 16
ja SHORT $LN6@func2
movzx eax, BYTE PTR $LN10@func2[eax]
jmp DWORD PTR $LN11@func2[eax*4]
$LN5@func2:
mov eax, 1
ret 0
$LN4@func2:
mov eax, 2
ret 0
$LN3@func2:
mov eax, 3
ret 0
$LN2@func2:
mov eax, 4
ret 0
$LN1@func2:
mov eax, 5
ret 0
$LN6@func2:
xor eax, eax
ret 0
npad 1
$LN11@func2:
DD $LN4@func2
DD $LN3@func2
DD $LN5@func2
DD $LN2@func2
DD $LN1@func2
DD $LN6@func2
$LN10@func2:
DB 0
DB 5
DB 1
DB 5
DB 5
DB 2
DB 5
DB 5
DB 3
DB 5
DB 5
DB 5
DB 5
DB 5
DB 5
DB 5
DB 4
?func2@@YAHH@Z ENDP
Secondly, I'm almost sure the indicies (for the function he's implementing) aren't even that sparsely scattered. They're almost perfectly sequential.
At any rate, back on the topic of irrConfigController!