Line data Source code
1 : #pragma once
2 :
3 : #include "bitboard.hpp"
4 : #include "definition.hpp"
5 : #include "pieceTools.hpp"
6 :
7 : namespace BBTools {
8 :
9 : /*!
10 : * Many little helper for evaluation (pawn structure mostly)
11 : * Most of them are taken from Topple chess engine by Vincent Tang (a.k.a Konsolas)
12 : */
13 :
14 1744098 : [[nodiscard]] constexpr BitBoard _shiftSouth (const BitBoard b) { return b >> 8; }
15 1744098 : [[nodiscard]] constexpr BitBoard _shiftNorth (const BitBoard b) { return b << 8; }
16 34582420 : [[nodiscard]] constexpr BitBoard _shiftWest (const BitBoard b) { return b >> 1 & ~BB::fileH; }
17 34582420 : [[nodiscard]] constexpr BitBoard _shiftEast (const BitBoard b) { return b << 1 & ~BB::fileA; }
18 3580936 : [[nodiscard]] constexpr BitBoard _shiftNorthEast(const BitBoard b) { return b << 9 & ~BB::fileA; }
19 796968 : [[nodiscard]] constexpr BitBoard _shiftNorthWest(const BitBoard b) { return b << 7 & ~BB::fileH; }
20 3580936 : [[nodiscard]] constexpr BitBoard _shiftSouthEast(const BitBoard b) { return b >> 7 & ~BB::fileA; }
21 1742690 : [[nodiscard]] constexpr BitBoard _shiftSouthWest(const BitBoard b) { return b >> 9 & ~BB::fileH; }
22 :
23 34134750 : [[nodiscard]] constexpr BitBoard adjacent(const BitBoard b) { return _shiftWest(b) | _shiftEast(b); }
24 :
25 : template<Color C> [[nodiscard]] constexpr BitBoard shiftN(const BitBoard b) {
26 : return C == Co_White ? BBTools::_shiftNorth(b) : BBTools::_shiftSouth(b);
27 : }
28 : template<Color C> [[nodiscard]] constexpr BitBoard shiftS(const BitBoard b) {
29 : return C != Co_White ? BBTools::_shiftNorth(b) : BBTools::_shiftSouth(b);
30 : }
31 : template<Color C> [[nodiscard]] constexpr BitBoard shiftSW(const BitBoard b) {
32 : return C == Co_White ? BBTools::_shiftSouthWest(b) : BBTools::_shiftNorthWest(b);
33 : }
34 : template<Color C> [[nodiscard]] constexpr BitBoard shiftSE(const BitBoard b) {
35 : return C == Co_White ? BBTools::_shiftSouthEast(b) : BBTools::_shiftNorthEast(b);
36 : }
37 : template<Color C> [[nodiscard]] constexpr BitBoard shiftNW(const BitBoard b) {
38 : return C != Co_White ? BBTools::_shiftSouthWest(b) : BBTools::_shiftNorthWest(b);
39 : }
40 : template<Color C> [[nodiscard]] constexpr BitBoard shiftNE(const BitBoard b) {
41 : return C != Co_White ? BBTools::_shiftSouthEast(b) : BBTools::_shiftNorthEast(b);
42 : }
43 :
44 : template<Color> [[nodiscard]] constexpr BitBoard fillForward(BitBoard b);
45 : template<> [[nodiscard]] constexpr BitBoard fillForward<Co_White>(BitBoard b) {
46 10077780 : b |= (b << 8u);
47 10077780 : b |= (b << 16u);
48 7591320 : b |= (b << 32u);
49 : return b;
50 : }
51 : template<> [[nodiscard]] constexpr BitBoard fillForward<Co_Black>(BitBoard b) {
52 9863923 : b |= (b >> 8u);
53 9863923 : b |= (b >> 16u);
54 7377463 : b |= (b >> 32u);
55 : return b;
56 : }
57 :
58 : template<Color> [[nodiscard]] constexpr BitBoard fillForwardOccluded(BitBoard b, BitBoard open);
59 : template<> [[nodiscard]] constexpr BitBoard fillForwardOccluded<Co_White>(BitBoard b, BitBoard open) {
60 148754 : b |= open & (b << 8u);
61 148754 : open &= (open << 8u);
62 148754 : b |= open & (b << 16u);
63 148754 : open &= (open << 16u);
64 148754 : b |= open & (b << 32u);
65 : return b;
66 : }
67 : template<> [[nodiscard]] constexpr BitBoard fillForwardOccluded<Co_Black>(BitBoard b, BitBoard open) {
68 148754 : b |= open & (b >> 8u);
69 148754 : open &= (open >> 8u);
70 148754 : b |= open & (b >> 16u);
71 148754 : open &= (open >> 16u);
72 148754 : b |= open & (b >> 32u);
73 : return b;
74 : }
75 :
76 : template<Color C> [[nodiscard]] constexpr BitBoard frontSpan(const BitBoard b) { return fillForward<C>(shiftN<C>(b)); }
77 : template<Color C> [[nodiscard]] constexpr BitBoard rearSpan(const BitBoard b) { return frontSpan<~C>(b); }
78 148754 : template<Color C> [[nodiscard]] constexpr BitBoard pawnSemiOpen(const BitBoard own, const BitBoard opp) { return own & ~frontSpan<~C>(opp); }
79 :
80 4109553 : [[nodiscard]] constexpr BitBoard fillFile(const BitBoard b) { return fillForward<Co_White>(b) | fillForward<Co_Black>(b); }
81 2515617 : [[nodiscard]] constexpr BitBoard openFiles(const BitBoard w, const BitBoard b) { return ~fillFile(w) & ~fillFile(b); }
82 :
83 2539658 : template<Color C> [[nodiscard]] constexpr BitBoard pawnAttacks(const BitBoard b) { return shiftNW<C>(b) | shiftNE<C>(b); }
84 297508 : template<Color C> [[nodiscard]] constexpr BitBoard pawnDoubleAttacks(const BitBoard b) { return shiftNW<C>(b) & shiftNE<C>(b); }
85 297508 : template<Color C> [[nodiscard]] constexpr BitBoard pawnSingleAttacks(const BitBoard b) { return shiftNW<C>(b) ^ shiftNE<C>(b); }
86 :
87 148754 : template<Color C> [[nodiscard]] constexpr BitBoard pawnHoles(const BitBoard b) { return ~fillForward<C>(pawnAttacks<C>(b)); }
88 148754 : template<Color C> [[nodiscard]] constexpr BitBoard pawnDoubled(const BitBoard b) { return frontSpan<C>(b) & b; }
89 :
90 297508 : [[nodiscard]] constexpr BitBoard pawnIsolated(const BitBoard b) { return b & ~fillFile(_shiftEast(b)) & ~fillFile(_shiftWest(b)); }
91 :
92 297508 : template<Color C> [[nodiscard]] constexpr BitBoard pawnBackward(const BitBoard own, const BitBoard opp) {
93 297508 : return shiftN<~C>((shiftN<C>(own) & ~opp) & ~fillForward<C>(pawnAttacks<C>(own)) & (pawnAttacks<~C>(opp)));
94 : }
95 :
96 : template<Color C> [[nodiscard]] constexpr BitBoard pawnForwardCoverage(const BitBoard bb) {
97 : const BitBoard spans = frontSpan<C>(bb);
98 148754 : return spans | _shiftEast(spans) | _shiftWest(spans);
99 : }
100 :
101 148754 : template<Color C> [[nodiscard]] constexpr BitBoard pawnPassed(const BitBoard own, const BitBoard opp) { return own & ~pawnForwardCoverage<~C>(opp); }
102 :
103 297508 : template<Color C> [[nodiscard]] constexpr BitBoard pawnCandidates(const BitBoard own, const BitBoard opp) {
104 : return pawnSemiOpen<C>(own, opp) &
105 297508 : shiftN<~C>((pawnSingleAttacks<C>(own) & pawnSingleAttacks<~C>(opp)) | (pawnDoubleAttacks<C>(own) & pawnDoubleAttacks<~C>(opp)));
106 : }
107 :
108 297508 : template<Color C> [[nodiscard]] constexpr BitBoard pawnUndefendable(const BitBoard own, const BitBoard other) {
109 297508 : return own & ~pawnAttacks<C>(fillForwardOccluded<C>(own, ~other)) & BB::advancedRanks;
110 : }
111 :
112 297508 : [[nodiscard]] constexpr BitBoard pawnAlone(const BitBoard b) { return b & ~(adjacent(b) | pawnAttacks<Co_White>(b) | pawnAttacks<Co_Black>(b)); }
113 :
114 : template<Color C> [[nodiscard]] constexpr BitBoard pawnDetached(const BitBoard own, const BitBoard other) {
115 148754 : return pawnUndefendable<C>(own, other) & pawnAlone(own);
116 : }
117 :
118 : // return first square of the bitboard only
119 : // beware emptyness of the bitboard is only checked in debug mode !
120 : [[nodiscard]] Square SquareFromBitBoard(const BitBoard& b);
121 :
122 : // Position relative helpers
123 : FORCE_FINLINE void unSetBit(Position& p, const Square k) {
124 279406 : assert(isValidSquare(k));
125 558812 : BB::_unSetBit(p._pieces(p.board_const(k)), k);
126 : }
127 : FORCE_FINLINE void unSetBit(Position& p, const Square k, const Piece pp) {
128 80230 : assert(isValidSquare(k));
129 190223058 : BB::_unSetBit(p._pieces(pp), k);
130 : }
131 : FORCE_FINLINE void setBit(Position& p, const Square k, const Piece pp) {
132 80230 : assert(isValidSquare(k));
133 179498734 : BB::_setBit(p._pieces(pp), k);
134 : }
135 : void clearBitBoards(Position& p);
136 : void setBitBoards(Position& p);
137 :
138 : } // namespace BBTools
|