<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Tue, 19 Jul 1994 08:50:58 EST
From   : Stephen Quan <quan@...>
Subject: 256 branches in C (fwd)

Alan writes :

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

Thanks Alan, I did sort it out as

void do_0x00(void);
void do_0x01(void);
:
void do_0xff(void);

void (*do[256])(void);

main()
{
  do[opcode]();
}

And it works!  But it is definitely slower.  I look at the assembler
that my C compiler generated, and there is a lot of messing about
with the saving of registers and restoring of registers per procedure.
If I could tell I didn't care less of any register except and the
condition codes this method would have been nearly ideal.
-- 
Stephen Quan (quan@...                 ), SysAdmin, Analyst/Programmer.
Centre for Spatial Information Studies, University of Tasmania, Hobart.
GPO BOX 252C, Australia, 7001.  Local Tel: (002) 202898 Fax: (002) 240282
International Callers use +6102 instead of (002).
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>