Skip to content

Commit

Permalink
Use pre-computed fixed shift magics with a densely packed table, remo…
Browse files Browse the repository at this point in the history
…ve specialized 32-bit magics and magics generation altogether.
  • Loading branch information
GediminasMasaitis committed Apr 26, 2021
1 parent 3dfda1b commit ba5d989
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 86 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -65,6 +65,7 @@ Fauzi Akram Dabat (FauziAkram)
Felix Wittmann
gamander
Gary Heckman (gheckman)
Gediminas Masaitis
George Sobala (gsobala)
gguliash
Gian-Carlo Pascutto (gcp)
Expand Down
204 changes: 129 additions & 75 deletions src/bitboard.cpp
Expand Up @@ -18,6 +18,7 @@

#include <algorithm>
#include <bitset>
#include <array>

#include "bitboard.h"
#include "misc.h"
Expand All @@ -33,16 +34,92 @@ Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];

Magic RookMagics[SQUARE_NB];
Magic BishopMagics[SQUARE_NB];
Magic< ROOK> RookMagics[SQUARE_NB];
Magic<BISHOP> BishopMagics[SQUARE_NB];

namespace {

Bitboard RookTable[0x19000]; // To store rook attacks
Bitboard BishopTable[0x1480]; // To store bishop attacks

void init_magics(PieceType pt, Bitboard table[], Magic magics[]);

using KnownMagicArray = std::array<KnownMagic, SQUARE_NB>;

// If using magic bitboards, it's possible to reduce the size of the
// attack table (~694 kB instead of 841 kB) by using specific offsets
// in the table for each piece, to allow overlaps wherever possible.
// We use magics and offsets originally found by Volker Annuss:
// www.talkchess.com/forum3/viewtopic.php?p=727500#p727500
constexpr KnownMagicArray KnownRookMagics = {
KnownMagic { 0x00280077ffebfffeu, 26304 }, KnownMagic { 0x2004010201097fffu, 35520 },
KnownMagic { 0x0010020010053fffu, 38592 }, KnownMagic { 0x0040040008004002u, 8026 },
KnownMagic { 0x7fd00441ffffd003u, 22196 }, KnownMagic { 0x4020008887dffffeu, 80870 },
KnownMagic { 0x004000888847ffffu, 76747 }, KnownMagic { 0x006800fbff75fffdu, 30400 },
KnownMagic { 0x000028010113ffffu, 11115 }, KnownMagic { 0x0020040201fcffffu, 18205 },
KnownMagic { 0x007fe80042ffffe8u, 53577 }, KnownMagic { 0x00001800217fffe8u, 62724 },
KnownMagic { 0x00001800073fffe8u, 34282 }, KnownMagic { 0x00001800e05fffe8u, 29196 },
KnownMagic { 0x00001800602fffe8u, 23806 }, KnownMagic { 0x000030002fffffa0u, 49481 },
KnownMagic { 0x00300018010bffffu, 2410 }, KnownMagic { 0x0003000c0085fffbu, 36498 },
KnownMagic { 0x0004000802010008u, 24478 }, KnownMagic { 0x0004002020020004u, 10074 },
KnownMagic { 0x0001002002002001u, 79315 }, KnownMagic { 0x0001001000801040u, 51779 },
KnownMagic { 0x0000004040008001u, 13586 }, KnownMagic { 0x0000006800cdfff4u, 19323 },
KnownMagic { 0x0040200010080010u, 70612 }, KnownMagic { 0x0000080010040010u, 83652 },
KnownMagic { 0x0004010008020008u, 63110 }, KnownMagic { 0x0000040020200200u, 34496 },
KnownMagic { 0x0002008010100100u, 84966 }, KnownMagic { 0x0000008020010020u, 54341 },
KnownMagic { 0x0000008020200040u, 60421 }, KnownMagic { 0x0000820020004020u, 86402 },
KnownMagic { 0x00fffd1800300030u, 50245 }, KnownMagic { 0x007fff7fbfd40020u, 76622 },
KnownMagic { 0x003fffbd00180018u, 84676 }, KnownMagic { 0x001fffde80180018u, 78757 },
KnownMagic { 0x000fffe0bfe80018u, 37346 }, KnownMagic { 0x0001000080202001u, 370 },
KnownMagic { 0x0003fffbff980180u, 42182 }, KnownMagic { 0x0001fffdff9000e0u, 45385 },
KnownMagic { 0x00fffefeebffd800u, 61659 }, KnownMagic { 0x007ffff7ffc01400u, 12790 },
KnownMagic { 0x003fffbfe4ffe800u, 16762 }, KnownMagic { 0x001ffff01fc03000u, 0 },
KnownMagic { 0x000fffe7f8bfe800u, 38380 }, KnownMagic { 0x0007ffdfdf3ff808u, 11098 },
KnownMagic { 0x0003fff85fffa804u, 21803 }, KnownMagic { 0x0001fffd75ffa802u, 39189 },
KnownMagic { 0x00ffffd7ffebffd8u, 58628 }, KnownMagic { 0x007fff75ff7fbfd8u, 44116 },
KnownMagic { 0x003fff863fbf7fd8u, 78357 }, KnownMagic { 0x001fffbfdfd7ffd8u, 44481 },
KnownMagic { 0x000ffff810280028u, 64134 }, KnownMagic { 0x0007ffd7f7feffd8u, 41759 },
KnownMagic { 0x0003fffc0c480048u, 1394 }, KnownMagic { 0x0001ffffafd7ffd8u, 40910 },
KnownMagic { 0x00ffffe4ffdfa3bau, 66516 }, KnownMagic { 0x007fffef7ff3d3dau, 3897 },
KnownMagic { 0x003fffbfdfeff7fau, 3930 }, KnownMagic { 0x001fffeff7fbfc22u, 72934 },
KnownMagic { 0x0000020408001001u, 72662 }, KnownMagic { 0x0007fffeffff77fdu, 56325 },
KnownMagic { 0x0003ffffbf7dfeecu, 66501 }, KnownMagic { 0x0001ffff9dffa333u, 14826 }
};

constexpr KnownMagicArray KnownBishopMagics = {
KnownMagic { 0x007fbfbfbfbfbfffu, 5378 }, KnownMagic { 0x0000a060401007fcu, 4093 },
KnownMagic { 0x0001004008020000u, 4314 }, KnownMagic { 0x0000806004000000u, 6587 },
KnownMagic { 0x0000100400000000u, 6491 }, KnownMagic { 0x000021c100b20000u, 6330 },
KnownMagic { 0x0000040041008000u, 5609 }, KnownMagic { 0x00000fb0203fff80u, 22236 },
KnownMagic { 0x0000040100401004u, 6106 }, KnownMagic { 0x0000020080200802u, 5625 },
KnownMagic { 0x0000004010202000u, 16785 }, KnownMagic { 0x0000008060040000u, 16817 },
KnownMagic { 0x0000004402000000u, 6842 }, KnownMagic { 0x0000000801008000u, 7003 },
KnownMagic { 0x000007efe0bfff80u, 4197 }, KnownMagic { 0x0000000820820020u, 7356 },
KnownMagic { 0x0000400080808080u, 4602 }, KnownMagic { 0x00021f0100400808u, 4538 },
KnownMagic { 0x00018000c06f3fffu, 29531 }, KnownMagic { 0x0000258200801000u, 45393 },
KnownMagic { 0x0000240080840000u, 12420 }, KnownMagic { 0x000018000c03fff8u, 15763 },
KnownMagic { 0x00000a5840208020u, 5050 }, KnownMagic { 0x0000020008208020u, 4346 },
KnownMagic { 0x0000804000810100u, 6074 }, KnownMagic { 0x0001011900802008u, 7866 },
KnownMagic { 0x0000804000810100u, 32139 }, KnownMagic { 0x000100403c0403ffu, 57673 },
KnownMagic { 0x00078402a8802000u, 55365 }, KnownMagic { 0x0000101000804400u, 15818 },
KnownMagic { 0x0000080800104100u, 5562 }, KnownMagic { 0x00004004c0082008u, 6390 },
KnownMagic { 0x0001010120008020u, 7930 }, KnownMagic { 0x000080809a004010u, 13329 },
KnownMagic { 0x0007fefe08810010u, 7170 }, KnownMagic { 0x0003ff0f833fc080u, 27267 },
KnownMagic { 0x007fe08019003042u, 53787 }, KnownMagic { 0x003fffefea003000u, 5097 },
KnownMagic { 0x0000101010002080u, 6643 }, KnownMagic { 0x0000802005080804u, 6138 },
KnownMagic { 0x0000808080a80040u, 7418 }, KnownMagic { 0x0000104100200040u, 7898 },
KnownMagic { 0x0003ffdf7f833fc0u, 42012 }, KnownMagic { 0x0000008840450020u, 57350 },
KnownMagic { 0x00007ffc80180030u, 22813 }, KnownMagic { 0x007fffdd80140028u, 56693 },
KnownMagic { 0x00020080200a0004u, 5818 }, KnownMagic { 0x0000101010100020u, 7098 },
KnownMagic { 0x0007ffdfc1805000u, 4451 }, KnownMagic { 0x0003ffefe0c02200u, 4709 },
KnownMagic { 0x0000000820806000u, 4794 }, KnownMagic { 0x0000000008403000u, 13364 },
KnownMagic { 0x0000000100202000u, 4570 }, KnownMagic { 0x0000004040802000u, 4282 },
KnownMagic { 0x0004010040100400u, 14964 }, KnownMagic { 0x00006020601803f4u, 4026 },
KnownMagic { 0x0003ffdfdfc28048u, 4826 }, KnownMagic { 0x0000000820820020u, 7354 },
KnownMagic { 0x0000000008208060u, 4848 }, KnownMagic { 0x0000000000808020u, 15946 },
KnownMagic { 0x0000000001002020u, 14932 }, KnownMagic { 0x0000000401002008u, 16588 },
KnownMagic { 0x0000004040404040u, 6905 }, KnownMagic { 0x007fff9fdf7ff813u, 16076 }
};

// If using PEXT indexing, do not use reduced table size.
Bitboard SlideAttackTable[HasPext ? 0x19000 + 0x1480 : 88772] {};

template<PieceType Pt>
void init_magics(Bitboard table[], Magic<Pt> magics[]);
}

/// safe_destination() returns the bitboard of target square for the given step
Expand Down Expand Up @@ -89,8 +166,8 @@ void Bitboards::init() {
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));

init_magics(ROOK, RookTable, RookMagics);
init_magics(BISHOP, BishopTable, BishopMagics);
init_magics< ROOK>(SlideAttackTable, RookMagics);
init_magics<BISHOP>(SlideAttackTable, BishopMagics);

for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
{
Expand Down Expand Up @@ -138,83 +215,60 @@ namespace {
}


// init_magics() computes all rook and bishop attacks at startup. Magic
// bitboards are used to look up attacks of sliding pieces. As a reference see
// www.chessprogramming.org/Magic_Bitboards. In particular, here we use the so
// called "fancy" approach.

void init_magics(PieceType pt, Bitboard table[], Magic magics[]) {

// Optimal PRNG seeds to pick the correct magics in the shortest time
int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020 },
{ 728, 10316, 55013, 32803, 12281, 15100, 16645, 255 } };

Bitboard occupancy[4096], reference[4096], edges, b;
int epoch[4096] = {}, cnt = 0, size = 0;
// init_magics() computes all rook and bishop attacks at startup. Either magic
// bitboards or PEXT indexing are used to look up attacks of sliding pieces.
// As a reference see www.chessprogramming.org/Magic_Bitboards. In particular,
// here we use the so called "fixed shift fancy magic bitboards" approach.
template<PieceType Pt>
void init_magics(Bitboard table[], Magic<Pt> magics[]) {
int size = 0;

for (Square s = SQ_A1; s <= SQ_H8; ++s)
{
// Board edges are not considered in the relevant occupancies
edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
Bitboard edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));

Magic<Pt>& m = magics[s];
KnownMagic knownMagic = Pt == ROOK ? KnownRookMagics[s] : KnownBishopMagics[s];

// Given a square 's', the mask is the bitboard of sliding attacks from
// 's' computed on an empty board. The index must be big enough to contain
// all the attacks for each possible subset of the mask and so is 2 power
// the number of 1s of the mask. Hence we deduce the size of the shift to
// apply to the 64 or 32 bits word to get the index.
Magic& m = magics[s];
m.mask = sliding_attack(pt, s, 0) & ~edges;
m.shift = (Is64Bit ? 64 : 32) - popcount(m.mask);

// Set the offset for the attacks table of the square. We have individual
// table sizes for each square with "Fancy Magic Bitboards".
m.attacks = s == SQ_A1 ? table : magics[s - 1].attacks + size;
// 's' computed on an empty board.
m.mask = sliding_attack(Pt, s, 0) & ~edges;

if constexpr (HasPext)
{
// For PEXT, use the starting offset if on the first square, and use the
// previous square's end offset as the current square's starting offset.
// Rooks are stored in entries 0 through 0x18FFFF,
// bishops are stored in entries 0x190000 through 0x19147F.
constexpr int startOffset = Pt == ROOK ? 0 : 0x19000;
m.attacks = s == SQ_A1 ? table + startOffset : magics[s - 1].attacks + size;
}
else
{
// For magic bitboards indexing we use pre-computed magics values and
// offsets. Since we are using the "fixed shift" approach we do not need
// to calculate the shift, because the magic product will always resolve
// a unique index using a 64-12 bit shift for rooks and 64-9 bit shift
// for bishops.
m.magic = knownMagic.magic;
m.attacks = table + knownMagic.offset;
}

// Use Carry-Rippler trick to enumerate all subsets of masks[s] and
// store the corresponding sliding attack bitboard in reference[].
b = size = 0;
// store the corresponding sliding attack bitboard in the attack table.
Bitboard occupied = 0;
size = 0;
do {
occupancy[size] = b;
reference[size] = sliding_attack(pt, s, b);
Bitboard reference = sliding_attack(Pt, s, occupied);

if (HasPext)
m.attacks[pext(b, m.mask)] = reference[size];
unsigned index = m.index(occupied);
assert(m.attacks[index] == 0 || m.attacks[index] == reference);
m.attacks[index] = reference;

size++;
b = (b - m.mask) & m.mask;
} while (b);

if (HasPext)
continue;

PRNG rng(seeds[Is64Bit][rank_of(s)]);

// Find a magic for square 's' picking up an (almost) random number
// until we find the one that passes the verification test.
for (int i = 0; i < size; )
{
for (m.magic = 0; popcount((m.magic * m.mask) >> 56) < 6; )
m.magic = rng.sparse_rand<Bitboard>();

// A good magic must map every possible occupancy to an index that
// looks up the correct sliding attack in the attacks[s] database.
// Note that we build up the database for square 's' as a side
// effect of verifying the magic. Keep track of the attempt count
// and save it in epoch[], little speed-up trick to avoid resetting
// m.attacks[] after every failed attempt.
for (++cnt, i = 0; i < size; ++i)
{
unsigned idx = m.index(occupancy[i]);

if (epoch[idx] < cnt)
{
epoch[idx] = cnt;
m.attacks[idx] = reference[i];
}
else if (m.attacks[idx] != reference[i])
break;
}
}
occupied = (occupied - m.mask) & m.mask;
} while (occupied);
}
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/bitboard.h
Expand Up @@ -81,30 +81,37 @@ extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];


/// Magic holds all magic bitboards relevant data for a single square
// Used for storing pre-computed magics and their positions in
// an attack table.
struct KnownMagic
{
Bitboard magic;
unsigned offset;
};

// Magic holds all magic bitboards relevant data for a single square
template<PieceType Pt>
struct Magic {
Bitboard mask;
Bitboard magic;
Bitboard* attacks;
unsigned shift;

// Compute the attack's index using the 'magic bitboards' approach
// Compute the attack's index using either the BMI2 PEXT instruction,
// or by the 'fixed shift fancy magic bitboards' approach.
unsigned index(Bitboard occupied) const {

if (HasPext)
if constexpr (HasPext)
return unsigned(pext(occupied, mask));

if (Is64Bit)
return unsigned(((occupied & mask) * magic) >> shift);

unsigned lo = unsigned(occupied) & unsigned(mask);
unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
// Fixed shift - leave 12 bits for rooks, and 9 bits for bishops.
constexpr unsigned fixedShift = 64 - (Pt == ROOK ? 12 : 9);
return unsigned(((occupied & mask) * magic) >> fixedShift);
}
};

extern Magic RookMagics[SQUARE_NB];
extern Magic BishopMagics[SQUARE_NB];
extern Magic< ROOK> RookMagics[SQUARE_NB];
extern Magic<BISHOP> BishopMagics[SQUARE_NB];

inline Bitboard square_bb(Square s) {
assert(is_ok(s));
Expand Down

0 comments on commit ba5d989

Please sign in to comment.