/*1Stockfish, a UCI chess playing engine derived from Glaurung 2.12Copyright (C) 2004-2026 The Stockfish developers (see AUTHORS file)34Stockfish is free software: you can redistribute it and/or modify5it under the terms of the GNU General Public License as published by6the Free Software Foundation, either version 3 of the License, or7(at your option) any later version.89Stockfish is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12GNU General Public License for more details.1314You should have received a copy of the GNU General Public License15along with this program. If not, see <http://www.gnu.org/licenses/>.16*/1718#include "bitboard.h"1920#include <algorithm>21#include <bitset>22#include <initializer_list>2324#include "misc.h"2526namespace Stockfish {2728uint8_t PopCnt16[1 << 16];29uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];3031Bitboard LineBB[SQUARE_NB][SQUARE_NB];32Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];33Bitboard RayPassBB[SQUARE_NB][SQUARE_NB];3435alignas(64) Magic Magics[SQUARE_NB][2];3637namespace {3839Bitboard RookTable[0x19000]; // To store rook attacks40Bitboard BishopTable[0x1480]; // To store bishop attacks4142void init_magics(PieceType pt, Bitboard table[], Magic magics[][2]);43}4445// Returns an ASCII representation of a bitboard suitable46// to be printed to standard output. Useful for debugging.47std::string Bitboards::pretty(Bitboard b) {4849std::string s = "+---+---+---+---+---+---+---+---+\n";5051for (Rank r = RANK_8;; --r)52{53for (File f = FILE_A; f <= FILE_H; ++f)54s += b & make_square(f, r) ? "| X " : "| ";5556s += "| " + std::to_string(1 + r) + "\n+---+---+---+---+---+---+---+---+\n";5758if (r == RANK_1)59break;60}61s += " a b c d e f g h\n";6263return s;64}656667// Initializes various bitboard tables. It is called at68// startup and relies on global objects to be already zero-initialized.69void Bitboards::init() {7071for (unsigned i = 0; i < (1 << 16); ++i)72PopCnt16[i] = uint8_t(std::bitset<16>(i).count());7374for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)75for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)76SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));7778init_magics(ROOK, RookTable, Magics);79init_magics(BISHOP, BishopTable, Magics);8081for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)82{83for (PieceType pt : {BISHOP, ROOK})84for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)85{86if (PseudoAttacks[pt][s1] & s2)87{88LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;89BetweenBB[s1][s2] =90(attacks_bb(pt, s1, square_bb(s2)) & attacks_bb(pt, s2, square_bb(s1)));91RayPassBB[s1][s2] =92attacks_bb(pt, s1, 0) & (attacks_bb(pt, s2, square_bb(s1)) | s2);93}94BetweenBB[s1][s2] |= s2;95}96}97}9899namespace {100// Computes all rook and bishop attacks at startup. Magic101// bitboards are used to look up attacks of sliding pieces. As a reference see102// https://www.chessprogramming.org/Magic_Bitboards. In particular, here we use103// the so called "fancy" approach.104void init_magics(PieceType pt, Bitboard table[], Magic magics[][2]) {105106#ifndef USE_PEXT107// Optimal PRNG seeds to pick the correct magics in the shortest time108int seeds[][RANK_NB] = {{8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020},109{728, 10316, 55013, 32803, 12281, 15100, 16645, 255}};110111Bitboard occupancy[4096];112int epoch[4096] = {}, cnt = 0;113#endif114Bitboard reference[4096];115int size = 0;116117for (Square s = SQ_A1; s <= SQ_H8; ++s)118{119// Board edges are not considered in the relevant occupancies120Bitboard edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));121122// Given a square 's', the mask is the bitboard of sliding attacks from123// 's' computed on an empty board. The index must be big enough to contain124// all the attacks for each possible subset of the mask and so is 2 power125// the number of 1s of the mask. Hence we deduce the size of the shift to126// apply to the 64 or 32 bits word to get the index.127Magic& m = magics[s][pt - BISHOP];128m.mask = Bitboards::sliding_attack(pt, s, 0) & ~edges;129#ifndef USE_PEXT130m.shift = (Is64Bit ? 64 : 32) - popcount(m.mask);131#endif132// Set the offset for the attacks table of the square. We have individual133// table sizes for each square with "Fancy Magic Bitboards".134m.attacks = s == SQ_A1 ? table : magics[s - 1][pt - BISHOP].attacks + size;135size = 0;136137// Use Carry-Rippler trick to enumerate all subsets of masks[s] and138// store the corresponding sliding attack bitboard in reference[].139Bitboard b = 0;140do141{142#ifndef USE_PEXT143occupancy[size] = b;144#endif145reference[size] = Bitboards::sliding_attack(pt, s, b);146147if (HasPext)148m.attacks[pext(b, m.mask)] = reference[size];149150size++;151b = (b - m.mask) & m.mask;152} while (b);153154#ifndef USE_PEXT155PRNG rng(seeds[Is64Bit][rank_of(s)]);156157// Find a magic for square 's' picking up an (almost) random number158// until we find the one that passes the verification test.159for (int i = 0; i < size;)160{161for (m.magic = 0; popcount((m.magic * m.mask) >> 56) < 6;)162m.magic = rng.sparse_rand<Bitboard>();163164// A good magic must map every possible occupancy to an index that165// looks up the correct sliding attack in the attacks[s] database.166// Note that we build up the database for square 's' as a side167// effect of verifying the magic. Keep track of the attempt count168// and save it in epoch[], little speed-up trick to avoid resetting169// m.attacks[] after every failed attempt.170for (++cnt, i = 0; i < size; ++i)171{172unsigned idx = m.index(occupancy[i]);173174if (epoch[idx] < cnt)175{176epoch[idx] = cnt;177m.attacks[idx] = reference[i];178}179else if (m.attacks[idx] != reference[i])180break;181}182}183#endif184}185}186}187188} // namespace Stockfish189190191