Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Reverse Bitboards

Author: Russell Reagan

Date: 19:47:42 01/13/03

Go up one level in this thread


On January 13, 2003 at 20:31:16, Dann Corbit wrote:

>Can you post the slow C routine?  I would be curious to see if it can be
>accelerated somehow.

Sure. This particular version is part of a class, hince the static and seemingly
global variable references at some points. I posted basically the same code a
while back, but in plain (maybe more generally usable) C.

Probably not the prettiest code, but if I had continued to use it I probably
would have made it a little more clean. A few things I have noticed since I did
my testing are that I didn't make very good use of these functions. For
instance, I wrote move generation functions for each piece and move type
(GenKnightCaps, GenBishopNonCaps, GenCapPromotions, etc.) and in doing so, I
call the attack generation functions in each of those functions. If I had
written my move generation function as one longer function, I would have saved a
lot of calls to the attack generation routines. I think I'll see if I can't fix
that and see if it runs significantly faster. Could that be a reason for a
drastic speed hit?

If you are interested in the whole thing I can email it to you or upload it.
It's a bitboard program that uses non-rotated bitboards, no bit index
extraction, and no supplementary squares[64] type array (just the bitboards).
It's a little different I guess. I gave up after I saw how slow it was and
realized that implementing a hash table would be difficult. Anyway, here it
is...

static bitboard_t shiftRight (bitboard_t b) {
	return (b << 1) & 0xfefefefefefefefe;
}

static bitboard_t shiftLeft (bitboard_t b) {
	return (b >> 1) & 0x7f7f7f7f7f7f7f7f;
}

static bitboard_t shiftUp (bitboard_t b) {
	return b << 8;
}

static bitboard_t shiftDown (bitboard_t b) {
	return  b >> 8;
}

static bitboard_t shiftUpRight (bitboard_t b) {
	return (b << 9) & 0xfefefefefefefefe;
}

static bitboard_t shiftUpLeft (bitboard_t b) {
	return (b << 7) & 0x7f7f7f7f7f7f7f7f;
}

static bitboard_t shiftDownRight (bitboard_t b) {
	return (b >> 7) & 0xfefefefefefefefe;
}

static bitboard_t shiftDownLeft (bitboard_t b) {
	return (b >> 9) & 0x7f7f7f7f7f7f7f7f;
}

static bitboard_t fillUpBlocked (bitboard_t source, bitboard_t clear) {
	source |= clear & (source << 8);
	clear &= clear << 8;
	source |= clear & (source << 16);
	clear &= clear << 16;
	return source |= clear & (source << 32);
}

static bitboard_t fillDownBlocked (bitboard_t source, bitboard_t clear) {
	source |= clear & (source >> 8);
	clear &= clear >> 8;
	source |= clear & (source >> 16);
	clear &= clear >> 16;
	return source |= clear & (source >> 32);
}

static bitboard_t fillLeftBlocked (bitboard_t source, bitboard_t clear) {
	clear &= 0x7f7f7f7f7f7f7f7f;
	source |= clear & (source >> 1);
	clear &= clear >> 1;
	source |= clear & (source >> 2);
	clear &= clear >> 2;
	return source |= clear & (source >> 4);
}

static bitboard_t fillRightBlocked (bitboard_t source, bitboard_t clear) {
	clear &= 0xfefefefefefefefe;
	source |= clear & (source << 1);
	clear &= clear << 1;
	source |= clear & (source << 2);
	clear &= clear << 2;
	return source |= clear & (source << 4);
}

static bitboard_t fillUpRightBlocked (bitboard_t source, bitboard_t clear) {
	clear &= 0xfefefefefefefefe;
	source |= clear & (source << 9);
	clear &= clear << 9;
	source |= clear & (source << 18);
	clear &= clear << 18;
	return source |= clear & (source << 36);
}

static bitboard_t fillUpLeftBlocked (bitboard_t source, bitboard_t clear) {
	clear &= 0x7f7f7f7f7f7f7f7f;
	source |= clear & (source << 7);
	clear &= clear << 7;
	source |= clear & (source << 14);
	clear &= clear << 14;
	return source |= clear & (source << 28);
}

static bitboard_t fillDownRightBlocked (bitboard_t source, bitboard_t clear) {
	clear &= 0xfefefefefefefefe;
	source |= clear & (source >> 7);
	clear &= clear >> 7;
	source |= clear & (source >> 14);
	clear &= clear >> 14;
	return source |= clear & (source >> 28);
}

static bitboard_t fillDownLeftBlocked (bitboard_t source, bitboard_t clear) {
	clear &= 0x7f7f7f7f7f7f7f7f;
	source |= clear & (source >> 9);
	clear &= clear >> 9;
	source |= clear & (source >> 18);
	clear &= clear >> 18;
	return source |= clear & (source >> 36);
}

static bitboard_t attacksLeft (bitboard_t source, bitboard_t clear) {
	return shiftLeft(fillLeftBlocked(source, clear));
}

static bitboard_t attacksRight (bitboard_t source, bitboard_t clear) {
	return shiftRight(fillRightBlocked(source, clear));
}

static bitboard_t attacksUp (bitboard_t source, bitboard_t clear) {
	return shiftUp(fillUpBlocked(source, clear));
}

static bitboard_t attacksDown (bitboard_t source, bitboard_t clear) {
	return shiftDown(fillDownBlocked(source, clear));
}

static bitboard_t attacksUpLeft (bitboard_t source, bitboard_t clear) {
	return shiftUpLeft(fillUpLeftBlocked(source, clear));
}

static bitboard_t attacksDownLeft (bitboard_t source, bitboard_t clear) {
	return shiftDownLeft(fillDownLeftBlocked(source, clear));
}

static bitboard_t attacksUpRight (bitboard_t source, bitboard_t clear) {
	return shiftUpRight(fillUpRightBlocked(source, clear));
}

static bitboard_t attacksDownRight (bitboard_t source, bitboard_t clear) {
	return shiftDownRight(fillDownRightBlocked(source, clear));
}

static bitboard_t pawnAttacks (bitboard_t source, color_t col) {
	return (shiftUpLeft(source) | shiftUpRight(source)) >> (col << 4);
}

static bitboard_t pawnSingleAdvances (bitboard_t source, bitboard_t clear,
color_t col) {
	return ((shiftUp(source) >> (col << 4)) & clear);
}

static bitboard_t pawnDoubleAdvances (bitboard_t source, bitboard_t clear,
color_t col) {
	return pawnSingleAdvances(pawnSingleAdvances(source & (RANK2_MASK << (40 *
col)), clear, col), clear, col);
}

static bitboard_t knightAttacks (bitboard_t source) {
	return	((source >> 17) & 0x7f7f7f7f7f7f7f7f) |
		((source << 15) & 0x7f7f7f7f7f7f7f7f) |
		((source >> 15) & 0xfefefefefefefefe) |
		((source << 17) & 0xfefefefefefefefe) |
		((source >> 10) & 0x3f3f3f3f3f3f3f3f) |
		((source <<  6) & 0x3f3f3f3f3f3f3f3f) |
		((source >>  6) & 0xfcfcfcfcfcfcfcfc) |
		((source << 10) & 0xfcfcfcfcfcfcfcfc);
}

static bitboard_t bishopAttacks (bitboard_t source, bitboard_t clear) {
	return attacksUpLeft(source, clear) | attacksUpRight(source, clear) |
		attacksDownLeft(source, clear) | attacksDownRight(source, clear);
}

static bitboard_t rookAttacks (bitboard_t source, bitboard_t clear) {
	return attacksUp(source, clear) | attacksDown(source, clear) |
		attacksLeft(source, clear) | attacksRight(source, clear);
}

static bitboard_t queenAttacks (bitboard_t source, bitboard_t clear) {
	return bishopAttacks(source, clear) | rookAttacks(source, clear);
}

static bitboard_t kingAttacks (bitboard_t source) {
	return shiftRight(source) | shiftLeft(source) |
		shiftUp(source) | shiftDown(source) |
		shiftUpLeft(source) | shiftUpRight(source) |
		shiftDownLeft(source) | shiftDownRight(source);
}

bitboard_t colorAttacks (color_t col) {
	return pawnAttacks(friendlyPawns, col) |
		knightAttacks(friendlyKnights) |
		bishopAttacks(friendlyBishops | friendlyQueens, emptySquares) |
		rookAttacks(friendlyRooks | friendlyQueens, emptySquares) |
		kingAttacks(friendlyKing);
}



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.