<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2448.0">
<TITLE>RE: [LCP]Address of a label</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>> Remember, the behavior of "case" is more general than a "jump</FONT>
<BR><FONT SIZE=2>> table" (you don't HAVE to have a "break" after each) which is why it</FONT>
<BR><FONT SIZE=2>> isn't implemented as one.</FONT>
</P>
<P><FONT SIZE=2>You can even wrap other control structures between a switch and its</FONT>
<BR><FONT SIZE=2>cases. The switch is really a goto in disguise that jumps to the</FONT>
<BR><FONT SIZE=2>various cases.</FONT>
<BR><FONT SIZE=2>Compilers typically implement the switch statement in one of a</FONT>
<BR><FONT SIZE=2>numer of variants (there are probably be more, but these are the</FONT>
<BR><FONT SIZE=2>ones I can think of now):</FONT>
</P>
<P><FONT SIZE=2>1: if the table is compact, it's implemented as a jump table (you</FONT>
<BR><FONT SIZE=2>fetch the address of the code to execute from a table indexed by</FONT>
<BR><FONT SIZE=2>the switched value, then jump there)</FONT>
<BR><FONT SIZE=2>2: a series of ifs (which is way slower, given all the prediction</FONT>
<BR><FONT SIZE=2>trouble you run into if there are many of these</FONT>
<BR><FONT SIZE=2>3: a combination of the above, where you can first split your</FONT>
<BR><FONT SIZE=2>domain into subdomains which are compact enough to warrant being</FONT>
<BR><FONT SIZE=2>implemented as method 1. Here, compactness has to be understood</FONT>
<BR><FONT SIZE=2>as compactness of computation: the domain might not be actually</FONT>
<BR><FONT SIZE=2>compact, but a simple computation might map it to one, as in:</FONT>
<BR><FONT SIZE=2>switch (n)</FONT>
<BR><FONT SIZE=2>case 16: break;</FONT>
<BR><FONT SIZE=2>case 32: break;</FONT>
<BR><FONT SIZE=2>case 48: break;</FONT>
<BR><FONT SIZE=2>case 64: break;</FONT>
<BR><FONT SIZE=2>}</FONT>
<BR><FONT SIZE=2>n can be tested and shifted back to a compact representation</FONT>
<BR><FONT SIZE=2>which can then be used to index a jump table.</FONT>
</P>
<P><FONT SIZE=2>One thing that puzzled me when I first saw it was something like:</FONT>
</P>
<P><FONT SIZE=2>switch (size&3) {</FONT>
<BR><FONT SIZE=2> while (size>0) {</FONT>
<BR><FONT SIZE=2> *to++=*from++;</FONT>
<BR><FONT SIZE=2> case 3:</FONT>
<BR><FONT SIZE=2> *to++=*from++;</FONT>
<BR><FONT SIZE=2> case 2:</FONT>
<BR><FONT SIZE=2> *to++=*from++;</FONT>
<BR><FONT SIZE=2> case 1:</FONT>
<BR><FONT SIZE=2> *to++=*from++;</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> size -= 4;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>That makes one really see the jump table properties of switch.</FONT>
<BR><FONT SIZE=2>(This does an unrolled memcpy like behavior, BTW).</FONT>
<BR><FONT SIZE=2>Disclaimer: I wrote this off the top of my head, might not work as</FONT>
<BR><FONT SIZE=2>advertised :)</FONT>
</P>
<P><FONT SIZE=2>-- </FONT>
<BR><FONT SIZE=2>Vincent Penquerc'h </FONT>
</P>
</BODY>
</HTML>