HELP!: Anyone remember good old ON_GOTO? 
Author Message
 HELP!: Anyone remember good old ON_GOTO?

In a function that must be very efficient, I have to do one of many
different things according to the value of a variable.  It is a classic
case of where an ON GOTO construction would be ideal in assembler or BASIC.
Is there any way I can mimic the action of an ON GOTO in C while retaining
the efficiency of ON GOTO?

If only something like this would work...

/* DOES NOT WORK.  :-(  */

int main() {
  void *ongoto[3] = {jump0, jump1, jump2};
  int i;

  <blah blah blah ...>

  /* This is the ON GOTO */

  goto ongoto[i];

 jump0:
  <blah blah blah ...>
  goto endongoto;

 jump1:
  <blah blah blah ...>
  goto endongoto;

 jump2:
  <blah blah blah ...>
  goto endongoto;

 endongoto:

  <blah blah blah ...>

Quote:
}

but alas you can't use labels as rvalues.


Sat, 31 May 1997 02:00:36 GMT  
 HELP!: Anyone remember good old ON_GOTO?
: In a function that must be very efficient, I have to do one of many
: different things according to the value of a variable.  It is a classic
: case of where an ON GOTO construction would be ideal in assembler or BASIC.
: Is there any way I can mimic the action of an ON GOTO in C while retaining
: the efficiency of ON GOTO?

: If only something like this would work...

< something awful >

It looks like you are looking for the switch statement:

int i = GetSomeValue();
switch( i )
{
   case 1  : DoJob1();
             break;
   case 2  : Job2();
             break;
   case 3  : BlahBlah();
             break;
   default : TheDefaultAction();
             break;

Quote:
}

/* Execution continues here */

This construct has a default value too, ever heard about 'ON GOTO
DEFAULT' int BASIC :)

Saku



Sat, 31 May 1997 03:23:44 GMT  
 HELP!: Anyone remember good old ON_GOTO?

Quote:

>In a function that must be very efficient, I have to do one of many
>different things according to the value of a variable.  It is a classic
>case of where an ON GOTO construction would be ideal in assembler or BASIC.
>Is there any way I can mimic the action of an ON GOTO in C while retaining
>the efficiency of ON GOTO?

In C, this is called 'switch'...

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Sat, 31 May 1997 03:47:06 GMT  
 HELP!: Anyone remember good old ON_GOTO?
|> In a function that must be very efficient, I have to do one of many
|> different things according to the value of a variable.  It is a classic
|> case of where an ON GOTO construction would be ideal in assembler or BASIC.
|> Is there any way I can mimic the action of an ON GOTO in C while retaining
|> the efficiency of ON GOTO?
|>

Look at switch...case statement.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545

<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Sat, 31 May 1997 04:36:34 GMT  
 HELP!: Anyone remember good old ON_GOTO?
Why not use switch?
int foo(int blech) {
        switch(blech){
                0:
                        [...]
                        break;
                1:
                        [...]
                        break;
                2:
                        [...]
                        break;
                3:
                        [...]
                        break;
                [...]
        }
Quote:
}

--
____ "Always modulate your shield frequency" -- Me, after seeing Generations
\BI/   "And if I smile, please tell me some bad news
 \/     before I laugh, and act like a fool"  -The Who "Behind Blue Eyes"
grep -vi obik       Running Linux 1.1 -- Free Unix for 386+ machines


Sat, 31 May 1997 03:59:40 GMT  
 HELP!: Anyone remember good old ON_GOTO?

Quote:
> In a function that must be very efficient, I have to do one of many
> different things according to the value of a variable.  It is a classic
> case of where an ON GOTO construction would be ideal in assembler or BASIC.

    The switch statement in C may be of value. It depends on your
    compiler, but some compilers are capable of setting up a jump
    table to optimise switch statements.

    If you do not have such a compiler, and you really need to squeeze
    every last cycle out of the routine, you may want to consider re-
    writing it in assembler and then interfacing that with your main C
    code.

    Another option, if the code is something that gets executed often
    within a loop, is to make several copies of the loop, each of
    which handles a separate case of your switch variable. Essentially
    you are pushing the split of flow control up a level, increasing
    your code size but removing the decision from within the loop. For
    example, instead of doing...

        for (i = 0; i < 10000; i++) {
            switch (j) {
                case (0):
                    ...

    you'd be doing...

        switch (j) {
            case (0):
                for (i = 0; i < 10000; i++) {
                    ...
                break;
            case (1):
                for (i = 0; i < 10000; i++) {
                    ...

    Another option, if you can reduce the function call overhead for
    each case to some small amount, is to use a function table. It's
    system-specific, but you may be able to set up functions to expect
    their arguments in registers (if your system has registers), turn
    off stack checking (if your system has a stack), etc. For
    example...

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>

        static void func1(void);
        static void func2(void);

        void    (* table[])(void) = {
            func1,
            func2
        };

        static void func1() /*======================================*/
        {
            printf("1\n");
        }

        static void func2() /*======================================*/
        {
            printf("2\n");
        }

        int     main(argc,argv) /*==================================*/
        int     argc;
        char    *argv[];
        {
        int     i;
            if (argc > 1) {
                i = atoi(argv[1]);
                if (i == 0 || i == 1) (table[i])();
            }
            exit(0);
        }

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***



Sat, 31 May 1997 05:16:26 GMT  
 HELP!: Anyone remember good old ON_GOTO?

[}>In a function that must be very efficient, I have to do one of many
[}>different things according to the value of a variable.  It is a classic
[}>case of where an ON GOTO construction would be ideal in assembler or BASIC.
[}>Is there any way I can mimic the action of an ON GOTO in C while retaining
[}>the efficiency of ON GOTO?
[}>

Short answer: switch / case.
(Too busy right now...your C manual should have the details.)


Amaze your friends! Stupefy your neighbors! Dazzle your detractors!
...........THINK!
These are MY opinions. AT&T Global Information Solutions can speak for itself.



Sat, 31 May 1997 06:03:53 GMT  
 HELP!: Anyone remember good old ON_GOTO?

Quote:

>In a function that must be very efficient, I have to do one of many
>different things according to the value of a variable.

Have you tried using a switch statement?  If your compiler doesn't
generate reasonably fast code for a switch statement, it's probably
not doing a good job elsewhere either.

-Dave



Sat, 31 May 1997 09:22:15 GMT  
 HELP!: Anyone remember good old ON_GOTO?

Quote:

>In a function that must be very efficient, I have to do one of many
>different things according to the value of a variable.  It is a classic
>case of where an ON GOTO construction would be ideal in assembler or BASIC.
>Is there any way I can mimic the action of an ON GOTO in C while retaining
>the efficiency of ON GOTO?

This thread shows one of the problems why c.l.c is as crowded as
it is. First I wonder why Mark did not like/use switch/case. But
thats not the problem.

Until now there have been seven follow-ups to this question from
which are six only contain a hit to switch/case. Only one delivers
are longer article about the topic and its surrounds.

(FLAME ON)
So why do people who have not more to say then "look at switch/case"
do a follow-up but not the (IMHO) more appropriate (spelling?) reply
by mail?
(FLAME OFF)

Hans Friedrich Steffani
--
Hans Friedrich Steffani
Institut fuer Elektrische Maschinen und Antriebe
TU Chemnitz-Zwickau



Sat, 31 May 1997 15:23:45 GMT  
 HELP!: Anyone remember good old ON_GOTO?
Maybe I wasn't clear. I _do_ already know about the switch statement,
but the point is that I want to force the compiler to produce a jump
table because efficiency is of prime importance.

I would hope that an optimising compiler would produce a jump table
by itself, but in this particular case, I have 0x600 cases to match in
all, and although a certain number of those cases have individual code
to match, there are also ranges of values that all go to the same case.

Sorry if my first post seemed a bit dorkish!

Mark



Sat, 31 May 1997 19:50:20 GMT  
 HELP!: Anyone remember good old ON_GOTO?

Quote:

> In a function that must be very efficient, I have to do one of many
> different things according to the value of a variable.  It is a classic
> case of where an ON GOTO construction would be ideal in assembler or BASIC.
> Is there any way I can mimic the action of an ON GOTO in C while retaining
> the efficiency of ON GOTO?

I think you can go two ways:

1)  Use a switch-statment.

2)  Use pointers to functions.

                                                               Arne

Arne Vajh?j                             local DECNET:  KO::ARNE
Computer Department                     PSI:           PSI%238310013040::ARNE

                WWW URL: http://www.hhs.dk/~arne/arne.html



Sun, 01 Jun 1997 00:40:00 GMT  
 HELP!: Anyone remember good old ON_GOTO?
|> Maybe I wasn't clear. I _do_ already know about the switch statement,
|> but the point is that I want to force the compiler to produce a jump
|> table because efficiency is of prime importance.

Unfortunately, a language almost _never_ specifies issues of efficiency. A
`language' describes the `meanings' of `conformant' programs: not how they are
implemented. If your compiler documentation does not describe specific
optimization hints: I would guess it does not allow such things.

In addition, when I absolutely needed one particular optimization (and, yes, my
work regularly brings me to such problems), I usually have to resort to assembly
coding after trying the obvious things.

|>
|> I would hope that an optimising compiler would produce a jump table
|> by itself, but in this particular case, I have 0x600 cases to match in
|> all, and although a certain number of those cases have individual code
|> to match, there are also ranges of values that all go to the same case.

What about

int jmptarget[0x600];

/* Initialize jmptarget by giving a distinct value to every range which
   leads to the same action */

switch (jumptarget[action]) {
  ...

Quote:
}

Any optimizing compiler should do something nice with it.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545

<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Sun, 01 Jun 1997 00:23:50 GMT  
 HELP!: Anyone remember good old ON_GOTO?

Quote:

> Maybe I wasn't clear. I _do_ already know about the switch statement,
> but the point is that I want to force the compiler to produce a jump
> table because efficiency is of prime importance.

There is nothing in the C standard that say an implementation *has* to
do so, but any sensible compiler will indirect the jump through a hash
table when a switch statement has a large number of cases.

Read your compiler documentation.

--
Gareth Rees



Sun, 01 Jun 1997 05:27:06 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. does anyone remember...

2. Anyone Remember Centurion??????

3. Does anyone remember VBX ?

4. Does anyone remember GWBASIC?

5. Remember, remember - All Configurations!!

6. Disassemble old DOS App into new .DLL Anyone?

7. The good old Hashing function

8. The good old ctrl-c problem...

9. Where is my good old printf?

10. Help me remember a macro trick

11. Good old Quake 2 question

12. anyone know good book on assembler?

 

 
Powered by phpBB® Forum Software