[LCP]Address of a label

Vincent Penquerc'h Vincent.Penquerch at artworks.co.uk
Fri Feb 22 03:56:33 UTC 2002


>     Remember, the behavior of "case" is more general than  a "jump
> table" (you don't HAVE to have a "break" after each) which is why it
> isn't implemented as one.

You can even wrap other control structures between a switch and its
cases. The switch is really a goto in disguise that jumps to the
various cases.
Compilers typically implement the switch statement in one of a
numer of variants (there are probably be more, but these are the
ones I can think of now):

1: if the table is compact, it's implemented as a jump table (you
fetch the address of the code to execute from a table indexed by
the switched value, then jump there)
2: a series of ifs (which is way slower, given all the prediction
trouble you run into if there are many of these
3: a combination of the above, where you can first split your
domain into subdomains which are compact enough to warrant being
implemented as method 1. Here, compactness has to be understood
as compactness of computation: the domain might not be actually
compact, but a simple computation might map it to one, as in:
switch (n)
case 16: break;
case 32: break;
case 48: break;
case 64: break;
}
n can be tested and shifted back to a compact representation
which can then be used to index a jump table.

One thing that puzzled me when I first saw it was something like:

switch (size&3) {
  while (size>0) {
      *to++=*from++;
    case 3:
      *to++=*from++;
    case 2:
      *to++=*from++;
    case 1:
      *to++=*from++;
  }
  size -= 4;
}

That makes one really see the jump table properties of switch.
(This does an unrolled memcpy like behavior, BTW).
Disclaimer: I wrote this off the top of my head, might not work as
advertised :)

-- 
Vincent Penquerc'h 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux.org.au/pipermail/linuxcprogramming/attachments/20020222/e62513ba/attachment.htm 


More information about the linuxCprogramming mailing list