Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: ASM/Optimization

Author: Russell Reagan

Date: 21:49:53 12/06/05

Go up one level in this thread


On December 06, 2005 at 13:06:01, Zappa wrote:

>Yes, but I have to write it twice :)  That is my problem.


You don't have to write it twice if you use templates as generic function
generators.



First establish a handful of color-specific operations.

enum Color { white, black };

template <Color c>
unsigned shift_fwd (unsigned x, unsigned n) {
    return x << n;
}

template <>
unsigned shift_fwd<black> (unsigned x, unsigned n) {
    return x >> n;
}



Then write generic, color-blind code.

// You write one, compiler generates two
template <Color c>
unsigned fwd_pawn_moves (unsigned x) {
    return shift_fwd<c>(x, 8) & empty_squares;
}



Now you have two functions, but you only wrote one.

fwd_pawn_moves<white>(x);
fwd_pawn_moves<black>(x);



You can propagate the template parameters from a higher level if you want.

template <Color c>
int eval () {
    // ...

    x = fwd_pawn_moves<c>(y);

    // ...
}



For something this trivial, it may not be worth it. That's up to you, but
there's the idea he was going for. At some level you'd have to have an if-else,
switch statement, or whatever, but you'd do that anyway if you wrote two
functions, and you only have to do it once in your program and let the rest
propagate.

if (side == white)
    return eval<white>();
else
    return eval<black>();



Personally, I would just use some math and be done with it, as an extra shift
can't possibly be the bottleneck of your program.

enum { white, black }

unsigned shift_fwd (unsigned x) {
    return (x << 8) >> (side * 16);
}



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.