Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Various compiler questions

Author: Andrew Dados

Date: 12:32:19 01/14/02

Go up one level in this thread


On January 14, 2002 at 14:57:29, Robert Hyatt wrote:

>On January 14, 2002 at 12:11:52, James Robertson wrote:
>
>>Here are several questions I've been wondering about:
>>
>>1) How does one insert inline assembler statements into your code such that it
>>can be compiled by gcc (obviously __asm doesn't work)?
>>
>>2) How is switch() code generated? For instance, if the compiler encounters:
>>switch (var) {
>>case 0: do_stuff0(); break;
>>case 1: do_stuff1(); break;
>>case 2: do_stuff2(); break;
>>}
>>will more efficient code be generated than if I write:
>>switch (var) {
>>case 297: do_stuff287; break;
>>case 0: do_stuff0; break;
>>case 9000: do_stuff9000; break;
>>}
>
>
>Note that in the latter case, you have oddball numbers.  That makes the jump
>table idea not so good and it will generally try to turn that switch into a
>series of if-then-elses instead.  Which will be slower.
>
>>If so, what does the compiler do to take advantage of the fact that 0, 1, and 2
>>are consecutive numbers?
>>
>
>It generates something like this (I will use a pseudo-asm for simplicity):
>
>        load       var,R1
>        cmp        R1,0
>        blt        skip                  ;  if var < 0, exit
>        cmp        R1,2
>        bgt        skip                  ;  ditto for var > 2
>        load       R2,#table             ;  address of branch table
>        mult       R1,4                  ;  scale to word index
>        add        R1,R2                 ;  R2 = table, table+4 or table+8
>        branch     R2
>table:  branch     case0
>        branch     case1
>        branch     case2
>case0:  do stuff
>        branch skip
>case1:  do stuff
>        branch skip
>case2:  do stuff
>
>skip:
>
>The above is the best way to handle a switch (C) or case (pascal)
>type structure.  The problem is that if the case variable values are
>scattered around, the branch table becomes huge.  The first pascal compiler
>I used didn't understand this and a case 0:  case 88000: would cause it to
>blow up as it produced an _enormous_ jump table that was too large to fit in
>the machine it ran on (a xerox sigma-9 with 128KW of memory total).
>
>The alternative is to use a series of if-then-else clauses but as you add
>cases, you add branches and slow things down.  Most common case _must_ appear
>first for performance.  While with the jump table, the order of the cases
>is totally irrelevant.

There is another technique requiring max k jumps, where 2^k>=number of switches
(minimum of k-1 jumps). It works with no jump table, but adding new cases is
tedious.

e.g for 4 n values (1,30,120,500):

if n>30 goto upper;
if n==1 {...; goto end;}
 else {...; goto end;}  //n==30 here;
label upper:
if n==120 {...; goto end;}
 else {...; goto end;}  //n==500 here;

So you have max 2 jumps here, min one.
I doubt anyone would want to hand-code huge switch in such a way, but trick is
worth mentioning, imo.
Sure you may want to validate all cases btw...

-Andrew-




>
>
>
>
>
>
>
>
>
>
>>3) How much overhead is added to a function call from a function pointer? Maybe
>>none? How about if you index an array of function pointers?
>
>A memory reference at most...
>
>>
>>4) What are the syntax differences between console code written for Linux versus
>>Unix (I don't have easy access to a Unix machine)?
>
>
>
>absolutely nothing.  Linux _is_ UNIX....
>
>
>
>>
>>5) Just out of curiousity, when is VC7 being released?
>>
>>Ok, If anyone could help me with these I'd be grateful.
>>Thanks!
>>James



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.