
On Jan 4, 2008 3:31 AM, Alexander Nasonov <alnsn@yandex.ru> wrote:
Tobias Schwinger <tschwinger <at> isonews2.com> writes:
We actually very much want a preprocessor-generated 'switch' statement because it is a special hint for optimization and most compilers generate very efficient code for it...
BTW, switch_ doesn't implement fall-though and I was worried about performance of this important case (bzero with Duff's device optimization):
switch(n % 8) { case 7: buf[6] = 0; case 6: buf[5] = 0; case 5: buf[4] = 0; case 4: buf[3] = 0; case 3: buf[2] = 0; case 2: buf[1] = 0; case 1: buf[0] = 0; }
switch_ would generate this code:
switch(n % 8) { case 7: buf[6] = 0; buf[5] = 0; buf[4] = 0; buf[3] = 0; buf[2] = 0; buf[1] = 0; buf[0] = 0; break; case 6: buf[5] = 0; buf[4] = 0; buf[3] = 0; buf[2] = 0; buf[1] = 0; buf[0] = 0; break; case 5: buf[4] = 0; buf[3] = 0; buf[2] = 0; buf[1] = 0; buf[0] = 0; break; case 4: buf[3] = 0; buf[2] = 0; buf[1] = 0; buf[0] = 0; break; case 3: buf[2] = 0; buf[1] = 0; buf[0] = 0; break; case 2: buf[1] = 0; buf[0] = 0; break; case 1: buf[0] = 0; break; default: break; }
Below is a program that demonstates a difference of assembly code between hand- crafted switch and the switch_. The are identical on gcc 3.4.6 x86_64.
[snip program]
Hi Alexander, Thanks for the example and the assembly analysis. Off the top of my head, it doesn't seem like it would be too difficult to add support for fall-through directly (e.g., by taking an (optional?) sequence of MPL bool constants to specify whether a case should fall through or break/return). Although, the difficult part might be deciding how to deal with return values in this case. Steven, what do you think? Stjepan