So I had some fun toying with C++ templates and this generator is a result of my effort. I do not know how it compares with another ones in terms of speed but I have been using it for a long time. I think that on 32 bits it could be somewhat competitive, at least the queen attack. I hope that the code is self-explanatory as well.I agree it is more fun to rely on own ideas and code, and if your code and memory balance is sufficient, there are more important issues to improve engine strength
Gerd
Filip
Code: Select all
#include <cstdio>
#include <cstdlib>
#define R 1
#define B 2
#define Q (R|B)
enum SQ64 {
A1, A2, A3, A4, A5, A6, A7, A8,
B1, B2, B3, B4, B5, B6, B7, B8,
C1, C2, C3, C4, C5, C6, C7, C8,
D1, D2, D3, D4, D5, D6, D7, D8,
E1, E2, E3, E4, E5, E6, E7, E8,
F1, F2, F3, F4, F5, F6, F7, F8,
G1, G2, G3, G4, G5, G6, G7, G8,
H1, H2, H3, H4, H5, H6, H7, H8
};
inline int File(const SQ64 x) { return (x >> 3) & 7; };
inline int Rank(const SQ64 x) { return x & 7; };
typedef unsigned char U_8;
typedef unsigned long long U_64;
union BITBOARD
{
U_64 board;
U_8 byte[8];
BITBOARD(const U_64 x) : board(x) { };
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
#define PrintBitboard(board, ...) PrintBitboardFunc(board, #board, __LINE__, ## __VA_ARGS__)
int PrintBitboardFunc(char* pointer, const BITBOARD& board, int line, const char* name)
{
const char* start = pointer;
*pointer++ = '\n';
*pointer++ = ' ';
for (int k = 0; k < 31; k++) *pointer++ = '_';
*pointer++ = '\n';
U_8* uk = (U_8*)&board;
for (unsigned char j = 0x80; j ; j >>= 1)
{
if (j & 0x55) pointer += sprintf(pointer, "|///| |///| |///| |///| |\n");
else pointer += sprintf(pointer, "| |///| |///| |///| |///|\n");
for (int i = 0; i < 8; i++)
{
*pointer++ = '|';
if (!(j & 0x55) ^ (i & 1)) *pointer++ = ' '; else *pointer++ = ' ';
if (uk[i] & j) *pointer++ = '1';
else *pointer++ = ' ';
if (!(j & 0x55) ^ (i & 1)) *pointer++ = ' '; else *pointer++ = ' ';
}
*pointer++ = '|';
if (j == 0x80) pointer += sprintf(pointer, " %s on line %d", name, line);
*pointer++ = '\n';
pointer += sprintf(pointer, "|___|___|___|___|___|___|___|___|\n");
}
return pointer - start;
}
void PrintBitboardFunc(const BITBOARD& board, const char *name, int line, FILE* file = stdout)
{
char buf[1000];
PrintBitboardFunc(buf, board, line, name);
fputs(buf, file);
system("pause");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
U_8 SlidingArray_1[64][8];
U_8 SlidingArray_2[8][256] = {
0,1,4,5,8,9,0,0,16,17,0,0,0,0,0,0,32,33,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,128,129,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,8,8,10,10,16,0,18,0,0,0,0,0,32,0,34,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,66,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,0,130,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,128,0,129,0,130,0,0,0,132,0,0,0,0,0,0,0,136,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,144,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,160, };
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <int file, int piece> inline void InnerAttackLoop(BITBOARD* const target, const BITBOARD* const free_squares, const int rank)
{
PrintBitboard(*target);
target->byte[file] = (piece & R) ? SlidingArray_1[(free_squares->byte[file] & 0x7e) >> 1][rank] : 0;
PrintBitboard(*target);
const U_8 start = ((((piece & R) ? 0x100 : 0)|((piece & B) ? 0x280 : 0)) << rank) >> 8;
if (file > 0) target->byte[file-1] = start;
if (file < 7) target->byte[file+1] = start;
PrintBitboard(*target);
for (int i = file + 2; i < 8; i++)
{
target->byte[i] = (piece & B) ? SlidingArray_2[rank][target->byte[i-1] & free_squares->byte[i-1]] : target->byte[i-1] & free_squares->byte[i-1];
PrintBitboard(*target);
}
for (int i = file - 2; i >= 0; i--)
{
target->byte[i] = (piece & B) ? SlidingArray_2[rank][target->byte[i+1] & free_squares->byte[i+1]] : target->byte[i+1] & free_squares->byte[i+1];
PrintBitboard(*target);
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <const int piece> void AttackFromSquare(const SQ64 square, BITBOARD* const target, const BITBOARD* const free_squares)
{
#define CASE(file) case file: InnerAttackLoop<file,piece>(target, free_squares, Rank(square)); break;
switch (File(square)) { CASE(0) CASE(1) CASE(2) CASE(3) CASE(4) CASE(5) CASE(6) CASE(7) }
#undef CASE
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Initialise_SlidingArray_1()
{
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 8; j++)
{
U_8 result = 0;
U_8 square = 1<<j;
do
{
square <<= 1;
result |= square;
} while (square && !(i & square));
square = 1<<j;
do
{
square >>= 1;
result |= square;
} while (square && !(i & square));
SlidingArray_1[((~i)&0x7e)>>1][j] = result;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
Initialise_SlidingArray_1();
BITBOARD free_squares = 0xaaaf9cffcffac94fULL;
PrintBitboard(free_squares);
BITBOARD target = 0xffffffffffffffffULL;
AttackFromSquare<Q>(E6, &target, &free_squares);
return 0;
}