Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Ed's "indirect addressing"

Author: Dann Corbit

Date: 21:05:35 01/29/03

Go up one level in this thread


On January 29, 2003 at 23:29:36, Russell Reagan wrote:

>http://members.home.nl/matador/chess840.htm#INTRO
>
>From Ed's page...
>
>switch (piece_type) { case  0 : goto empty;
>                      case  1 : goto white_pawn;    // evaluate white pawn
>                      case  2 : goto white_knight;  // evaluate white knight
>                      case  3 : goto white_bishop;
>                      case  4 : goto white_rook;
>                      case  5 : goto white_queen;
>                      case  6 : goto white_king;
>                      case  7 : goto black_pawn;    // evaluate black pawn
>                      case  8 : goto black_knight;
>                      case  9 : goto black_bishop;
>                      case 10 : goto black_rook;
>                      case 11 : goto black_queen;
>                      case 12 : goto black_king; }
>
>
>On one portion of Ed's discussion of Rebel (see above), he talks about using
>"indirect addressing". I get the impression from Ed's words that this method is
>supposed to fast. I understand his discussion to mean that if you create a
>switch statement like he does, you create a jump table and avoid a bunch of
>conditionals.
>
>However, in past discussions, I recall hearing that using a function pointer is
>going to be at least as slow as conditional, so I asked someone, and was told
>that Ed's example should be no different than using a function pointer or
>virtual functions.

Direct jumps (like the above) or an array of function pointers will be faster
than virtual functions.

>Ed talks about this method as if it is a good thing to use. So what is the
>advantage of it? Either someone is mistaken, or Ed and the guy I talked to are
>talking about different things.

If you use the Intel compiler, a switch statement gets translated into a jump
table (if the switch is reasonable).  So the trick loses a lot of its value.

Try the following with your compiler inside of your profiler:

#include <math.h>
#include <stdio.h>

typedef double  (*f_t) (double);
static f_t      f[] = {log, log10, sqrt, cos, cosh, exp, sin, sinh, tan, tanh,
0};

static double   accum0 = 0;
static double   accum1 = 0;
static double   accum2 = 0;


void            arr(void)
{
    int             i;
    double          d = 0;
    for (i = 0; f[i]; i++) {
        d += f[i] (0.5);
    }
    accum0 += d;
}

void            poi(void)
{
    f_t            *flist = f;
    double          d = 0;
    while (*flist) {
        f_t             ff = *flist;
        d += ff(0.5);
        flist++;
    }
    accum1 += d;
}

void            swi(void)
{
    int             i;
    double          d = 0;
    for (i = 0; f[i]; i++) {
        switch (i) {
        case 0:
            d += f[0] (0.5);
            break;
        case 1:
            d += f[1] (0.5);
            break;
        case 2:
            d += f[2] (0.5);
            break;
        case 3:
            d += f[3] (0.5);
            break;
        case 4:
            d += f[4] (0.5);
            break;
        case 5:
            d += f[5] (0.5);
            break;
        case 6:
            d += f[6] (0.5);
            break;
        case 7:
            d += f[7] (0.5);
            break;
        case 8:
            d += f[8] (0.5);
            break;
        case 9:
            d += f[9] (0.5);
            break;
        default:
            break;
        }
    }
    accum2 += d;
}

int             main(void)
{
    long            i;
    for (i = 0; i < 1000000; i++) {
        arr();
        poi();
        swi();
    }
    printf("%.20g, %.20g, %.20g\n", accum0, accum1, accum2);
    return 0;
}



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.