Uh, I don’t know how I may introduce this one. I don’t know if I should call it a horrific failure or just hilarious. This is what comes out of VS2008 for a signed short division:
symSend = mergedNGram % numCases; mergedNGram /= numCases;
The produced code:
00024 0f bf 86 2c 04
00 00 movsx eax, WORD PTR [esi+1068]
0002b 99 cdq
0002c b9 03 00 00 00 mov ecx, 3
00031 f7 f9 idiv ecx
00033 0f b7 ca movzx ecx, dx
00036 66 89 86 2c 04
00 00 mov WORD PTR [esi+1068], ax
This little bugger knows exactly we have shorts (he sign extends them to longs), and he also knows, that the results must be shorts (he does not consider results over 65535)! I got courious and tried everything to make it produce non-long division, especially a char division. Nada, njiet.
I would forgive it it’s behaviour if it would be just a plain simple non-optimizing compiler, but this one is complex and it does have the entire boundary-check done right! And anyway he damn doesn’t produce a 6-7 clocks faster 8bit division!?