Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Various compiler questions

Author: Ed Schröder

Date: 15:48:32 01/14/02

Go up one level in this thread


On January 14, 2002 at 17:28:25, James Robertson wrote:

>On January 14, 2002 at 13:31:39, Ed Schröder 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)?
>>
>>James, welcome back.
>
>Thanks. =) I hope to post here periodically as my school schedule allows.
>
>>
>>
>>>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;
>>}
>>
>>A good compiler will make use of the "indirect call", that is it will generate a
>>table, store the memory address of the functions, and call the right routine
>>from the generated table. The advantage, it takes 1 instruction.
>>
>>
>>>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;
>>>}
>>>
>>>If so, what does the compiler do to take advantage of the fact that 0, 1, and 2
>>>are consecutive numbers?
>>
>>See above.
>>
>>If you are using values that are in a reasonable range then create the table
>>yourself. I recently posted an example of that for generating moves, you will
>>have to search for that in the archive.
>>
>>Ed
>
>Ok, this looks interesting. Assuming I create this table that contains locations
>of other pieces of code (not other functions), then how would I be able to jump
>to these other locations in the code based on some sort of array reference? I
>don't think I'm familiar with the syntax. I'll see what I can do about searching
>for stuff in the archive, but that might take more time than I can spare.
>
>Thanks for you help. =)

It works like this, instead of doing a "switch / case",

switch (var) {
case 0: do_stuff0(); break;
case 1: do_stuff1(); break;
case 2: do_stuff2(); break;
}

you do this,

void (*jump[])() = { do_stuff0, do_stuff1, do_stuff2 };

        (jump[var])();

That is actually what any good compiler will do in this case.

Ed





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.