Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Various compiler questions

Author: Robert Hyatt

Date: 11:57:29 01/14/02

Go up one level in this thread


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.










>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.