<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Mon, 18 Jul 1994 09:42:26 +0100 (BST)
From   : amh15@... (Alan Hart)
Subject: 256 branches in C

> I have tried to force the C compiler to do better, and have been
> trying to find a better command than switch, I have tried things
> like
> 
>     main()
>     {
>       char *opcode_table[256]; /* 256 array of labels. */
>       int   opcode;
> 
>       opcode_table[0x00] = opcode_00; /* assigning labels to the array. */
>       opcode_table[0x01] = opcode_01;
>       opcode_table[0x02] = opcode_02;
>             :
>       opcode_table[0xff] = opcode_ff;
> 
>             :
> 
>     main_cpu_loop:
>       opcode = fetch_opcode();
>       goto opcode_table[opcode]; /* jump to a label depending on opcode. */
> 
>     opcode_00 : ....  goto main_cpu_loop;
>     opcode_01 : ....  goto main_cpu_loop;
>     opcode_02 : ....  goto main_cpu_loop;
>     opcode_03 : ....  goto main_cpu_loop;
>         :
>     }
> 
> Unfortunately, I cannot find any C syntax that gets to anything
> close to the above.  Any suggestions???

Have you tried an array of functions? Not particularly fast due to stack 
manipulation, but it will work.

e.g.

typedef void vfnptr (void);

vfnptr a[] = {f1,f2,f3,f4};

void f1(void);
void f2(void);
etc.

I think I've got the syntax right for the typedef; I might have missed out a 
* in either this line or the array declaration. I don't think I'll ever 
understand this aspect of C completely (sigh).

Alan

 Alan Hart  -  amh15@...  -  University of Cambridge, UK

<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>