Skip to content

Commit 46cc1ad

Browse files
authored
Merge pull request official-stockfish#1017 from IIvec/master
nm
2 parents 7a35202 + 1aab5b4 commit 46cc1ad

File tree

12 files changed

+114
-118
lines changed

12 files changed

+114
-118
lines changed

src/bitboard.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "misc.h"
2525

2626
uint8_t PopCnt16[1 << 16];
27-
int8_t SquareDistance[SQUARE_NB][SQUARE_NB];
27+
uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
2828

2929
Bitboard SquareBB[SQUARE_NB];
3030
Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
@@ -34,6 +34,12 @@ Bitboard DistanceRingBB[SQUARE_NB][8];
3434
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
3535
Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
3636

37+
Bitboard KingFlank[FILE_NB] = {
38+
QueenSide ^ FileDBB, QueenSide, QueenSide,
39+
CenterFiles, CenterFiles,
40+
KingSide, KingSide, KingSide ^ FileEBB
41+
};
42+
3743
Magic RookMagics[SQUARE_NB];
3844
Magic BishopMagics[SQUARE_NB];
3945

@@ -45,7 +51,6 @@ namespace {
4551
void init_magics(Bitboard table[], Magic magics[], Direction directions[]);
4652

4753
// popcount16() counts the non-zero bits using SWAR-Popcount algorithm
48-
4954
unsigned popcount16(unsigned u) {
5055
u -= (u >> 1) & 0x5555U;
5156
u = ((u >> 2) & 0x3333U) + (u & 0x3333U);
@@ -80,17 +85,13 @@ const std::string Bitboards::pretty(Bitboard b) {
8085
void Bitboards::init() {
8186

8287
for (unsigned i = 0; i < (1 << 16); ++i)
83-
PopCnt16[i] = (uint8_t) popcount16(i);
88+
PopCnt16[i] = (uint8_t)popcount16(i);
8489

8590
for (Square s = SQ_A1; s <= SQ_H8; ++s)
8691
SquareBB[s] = (1ULL << s);
8792

88-
for (Rank r = RANK_1; r < RANK_8; ++r)
89-
ForwardRanksBB[WHITE][r] = ~(ForwardRanksBB[BLACK][r + 1] = ForwardRanksBB[BLACK][r] | rank_bb(r));
90-
9193
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
9294
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
93-
if (s1 != s2)
9495
{
9596
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
9697
DistanceRingBB[s1][SquareDistance[s1][s2]] |= s2;
@@ -127,13 +128,11 @@ void Bitboards::init() {
127128

128129
for (PieceType pt : { BISHOP, ROOK })
129130
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
130-
{
131-
if (!(PseudoAttacks[pt][s1] & s2))
132-
continue;
133-
134-
LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;
135-
BetweenBB[s1][s2] = attacks_bb(pt, s1, SquareBB[s2]) & attacks_bb(pt, s2, SquareBB[s1]);
136-
}
131+
if (PseudoAttacks[pt][s1] & s2)
132+
{
133+
LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;
134+
BetweenBB[s1][s2] = attacks_bb(pt, s1, SquareBB[s2]) & attacks_bb(pt, s2, SquareBB[s1]);
135+
}
137136
}
138137
}
139138

src/bitboard.h

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,21 @@ constexpr Bitboard Rank6BB = Rank1BB << (8 * 5);
6060
constexpr Bitboard Rank7BB = Rank1BB << (8 * 6);
6161
constexpr Bitboard Rank8BB = Rank1BB << (8 * 7);
6262

63-
extern int8_t SquareDistance[SQUARE_NB][SQUARE_NB];
63+
constexpr Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB;
64+
constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
65+
constexpr Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB;
66+
constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
67+
68+
extern uint8_t PopCnt16[1 << 16];
69+
extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
6470

6571
extern Bitboard SquareBB[SQUARE_NB];
66-
extern Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
6772
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
6873
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
6974
extern Bitboard DistanceRingBB[SQUARE_NB][8];
7075
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
7176
extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
77+
extern Bitboard KingFlank[FILE_NB];
7278

7379

7480
/// Magic holds all magic bitboards relevant data for a single square
@@ -133,6 +139,7 @@ inline bool opposite_colors(Square s1, Square s2) {
133139
return bool(DarkSquares & s1) != bool(DarkSquares & s2);
134140
}
135141

142+
136143
/// rank_bb() and file_bb() return a bitboard representing all the squares on
137144
/// the given file or rank.
138145

@@ -192,48 +199,49 @@ inline Bitboard adjacent_files_bb(File f) {
192199
return shift<EAST>(file_bb(f)) | shift<WEST>(file_bb(f));
193200
}
194201

202+
195203
/// between_bb() returns a bitboard representing all the squares between the two
196204
/// given ones. For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with
197-
/// the bits for square d5 and e6 set. If s1 and s2 are not on the same rank, file
198-
/// or diagonal, 0 is returned.
205+
/// the bits for square d5 and e6 set. If s1 and s2 are not on the same rank,
206+
/// file or diagonal, 0 is returned.
199207

200208
inline Bitboard between_bb(Square s1, Square s2) {
201209
return BetweenBB[s1][s2];
202210
}
203211

204212

205-
/// forward_ranks_bb() returns a bitboard representing the squares on all the ranks
213+
/// forward_ranks_bb() returns a bitboard representing the squares on the ranks
206214
/// in front of the given one, from the point of view of the given color. For instance,
207215
/// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
208216

209217
inline Bitboard forward_ranks_bb(Color c, Square s) {
210-
return ForwardRanksBB[c][rank_of(s)];
218+
return c == WHITE ? ~Rank1BB << 8 * (rank_of(s) - RANK_1)
219+
: ~Rank8BB >> 8 * (RANK_8 - rank_of(s));
211220
}
212221

213222

214-
/// forward_file_bb() returns a bitboard representing all the squares along the line
215-
/// in front of the given one, from the point of view of the given color:
216-
/// ForwardFileBB[c][s] = forward_ranks_bb(c, s) & file_bb(s)
223+
/// forward_file_bb() returns a bitboard representing all the squares along the
224+
/// line in front of the given one, from the point of view of the given color.
217225

218226
inline Bitboard forward_file_bb(Color c, Square s) {
219-
return ForwardRanksBB[c][rank_of(s)] & file_bb(s);
227+
return forward_ranks_bb(c, s) & file_bb(s);
220228
}
221229

222230

223-
/// pawn_attack_span() returns a bitboard representing all the squares that can be
224-
/// attacked by a pawn of the given color when it moves along its file, starting
225-
/// from the given square:
231+
/// pawn_attack_span() returns a bitboard representing all the squares that can
232+
/// be attacked by a pawn of the given color when it moves along its file,
233+
/// starting from the given square.
226234

227235
inline Bitboard pawn_attack_span(Color c, Square s) {
228236
return forward_ranks_bb(c, s) & adjacent_files_bb(file_of(s));
229237
}
230238

231239

232-
/// passed_pawn_mask() returns a bitboard mask which can be used to test if a
233-
/// pawn of the given color and on the given square is a passed pawn:
240+
/// passed_pawn_span() returns a bitboard which can be used to test if a pawn of
241+
/// the given color and on the given square is a passed pawn.
234242

235-
inline Bitboard passed_pawn_mask(Color c, Square s) {
236-
return pawn_attack_span(c, s) | forward_file_bb(c, s);
243+
inline Bitboard passed_pawn_span(Color c, Square s) {
244+
return forward_ranks_bb(c, s) & (adjacent_files_bb(file_of(s)) | file_bb(s));
237245
}
238246

239247

@@ -248,7 +256,7 @@ inline bool aligned(Square s1, Square s2, Square s3) {
248256
/// distance() functions return the distance between x and y, defined as the
249257
/// number of steps for a king in x to reach y. Works with squares, ranks, files.
250258

251-
template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
259+
template<typename T> inline int distance(T x, T y) { return std::abs(x - y); }
252260
template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
253261

254262
template<typename T1, typename T2> inline int distance(T2 x, T2 y);
@@ -286,7 +294,6 @@ inline int popcount(Bitboard b) {
286294

287295
#ifndef USE_POPCNT
288296

289-
extern uint8_t PopCnt16[1 << 16];
290297
union { Bitboard bb; uint16_t u[4]; } v = { b };
291298
return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
292299

src/evaluate.cpp

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,6 @@ using namespace Trace;
7373

7474
namespace {
7575

76-
constexpr Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB;
77-
constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
78-
constexpr Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB;
79-
constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
80-
81-
constexpr Bitboard KingFlank[FILE_NB] = {
82-
QueenSide ^ FileDBB, QueenSide, QueenSide,
83-
CenterFiles, CenterFiles,
84-
KingSide, KingSide, KingSide ^ FileEBB
85-
};
86-
8776
// Threshold for lazy and space evaluation
8877
constexpr Value LazyThreshold = Value(1500);
8978
constexpr Value SpaceThreshold = Value(12222);
@@ -152,6 +141,7 @@ namespace {
152141
constexpr Score KnightOnQueen = S( 16, 12);
153142
constexpr Score LongDiagonalBishop = S( 45, 0);
154143
constexpr Score MinorBehindPawn = S( 18, 3);
144+
constexpr Score Outpost = S( 9, 3);
155145
constexpr Score PawnlessFlank = S( 17, 95);
156146
constexpr Score RestrictedPiece = S( 7, 7);
157147
constexpr Score RookOnPawn = S( 10, 32);
@@ -163,7 +153,6 @@ namespace {
163153
constexpr Score TrappedRook = S( 47, 4);
164154
constexpr Score WeakQueen = S( 49, 15);
165155
constexpr Score WeakUnopposedPawn = S( 12, 23);
166-
constexpr Score Outpost = S( 9, 3);
167156

168157
#undef S
169158

@@ -402,7 +391,8 @@ namespace {
402391
constexpr Bitboard Camp = (Us == WHITE ? AllSquares ^ Rank6BB ^ Rank7BB ^ Rank8BB
403392
: AllSquares ^ Rank1BB ^ Rank2BB ^ Rank3BB);
404393

405-
Bitboard weak, b, b1, b2, safe, unsafeChecks = 0;
394+
Bitboard weak, b1, b2, safe, unsafeChecks = 0;
395+
Bitboard rookChecks, queenChecks, bishopChecks, knightChecks;
406396
int kingDanger = 0;
407397
const Square ksq = pos.square<KING>(Us);
408398

@@ -422,45 +412,43 @@ namespace {
422412
b2 = attacks_bb<BISHOP>(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN));
423413

424414
// Enemy rooks checks
425-
Bitboard RookCheck = b1
426-
& safe
427-
& attackedBy[Them][ROOK];
415+
rookChecks = b1 & safe & attackedBy[Them][ROOK];
428416

429-
if (RookCheck)
417+
if (rookChecks)
430418
kingDanger += RookSafeCheck;
431419
else
432420
unsafeChecks |= b1 & attackedBy[Them][ROOK];
433421

434422
// Enemy queen safe checks: we count them only if they are from squares from
435423
// which we can't give a rook check, because rook checks are more valuable.
436-
Bitboard QueenCheck = (b1 | b2)
437-
& attackedBy[Them][QUEEN]
438-
& safe
439-
& ~attackedBy[Us][QUEEN]
440-
& ~RookCheck;
424+
queenChecks = (b1 | b2)
425+
& attackedBy[Them][QUEEN]
426+
& safe
427+
& ~attackedBy[Us][QUEEN]
428+
& ~rookChecks;
441429

442-
if (QueenCheck)
430+
if (queenChecks)
443431
kingDanger += QueenSafeCheck;
444432

445433
// Enemy bishops checks: we count them only if they are from squares from
446434
// which we can't give a queen check, because queen checks are more valuable.
447-
Bitboard BishopCheck = b2
448-
& attackedBy[Them][BISHOP]
449-
& safe
450-
& ~QueenCheck;
435+
bishopChecks = b2
436+
& attackedBy[Them][BISHOP]
437+
& safe
438+
& ~queenChecks;
451439

452-
if (BishopCheck)
440+
if (bishopChecks)
453441
kingDanger += BishopSafeCheck;
454442
else
455443
unsafeChecks |= b2 & attackedBy[Them][BISHOP];
456444

457445
// Enemy knights checks
458-
b = pos.attacks_from<KNIGHT>(ksq) & attackedBy[Them][KNIGHT];
446+
knightChecks = pos.attacks_from<KNIGHT>(ksq) & attackedBy[Them][KNIGHT];
459447

460-
if (b & safe)
448+
if (knightChecks & safe)
461449
kingDanger += KnightSafeCheck;
462450
else
463-
unsafeChecks |= b;
451+
unsafeChecks |= knightChecks;
464452

465453
// Unsafe or occupied checking squares will also be considered, as long as
466454
// the square is in the attacker's mobility area.
@@ -511,7 +499,7 @@ namespace {
511499
constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH);
512500
constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
513501

514-
Bitboard b, weak, defended, nonPawnEnemies, stronglyProtected, safe, restricted;
502+
Bitboard b, weak, defended, nonPawnEnemies, stronglyProtected, safe;
515503
Score score = SCORE_ZERO;
516504

517505
// Non-pawn enemies
@@ -561,10 +549,11 @@ namespace {
561549
}
562550

563551
// Bonus for restricting their piece moves
564-
restricted = attackedBy[Them][ALL_PIECES]
565-
& ~stronglyProtected
566-
& attackedBy[Us][ALL_PIECES];
567-
score += RestrictedPiece * popcount(restricted);
552+
b = attackedBy[Them][ALL_PIECES]
553+
& ~stronglyProtected
554+
& attackedBy[Us][ALL_PIECES];
555+
556+
score += RestrictedPiece * popcount(b);
568557

569558
// Bonus for enemy unopposed weak pawns
570559
if (pos.pieces(Us, ROOK, QUEEN))
@@ -901,7 +890,6 @@ std::string Eval::trace(const Position& pos) {
901890
<< " ------------+-------------+-------------+------------\n"
902891
<< " Material | " << Term(MATERIAL)
903892
<< " Imbalance | " << Term(IMBALANCE)
904-
<< " Initiative | " << Term(INITIATIVE)
905893
<< " Pawns | " << Term(PAWN)
906894
<< " Knights | " << Term(KNIGHT)
907895
<< " Bishops | " << Term(BISHOP)
@@ -912,6 +900,7 @@ std::string Eval::trace(const Position& pos) {
912900
<< " Threats | " << Term(THREAT)
913901
<< " Passed | " << Term(PASSED)
914902
<< " Space | " << Term(SPACE)
903+
<< " Initiative | " << Term(INITIATIVE)
915904
<< " ------------+-------------+-------------+------------\n"
916905
<< " Total | " << Term(TOTAL);
917906

src/movegen.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,11 @@ namespace {
252252
} // namespace
253253

254254

255-
/// generate<CAPTURES> generates all pseudo-legal captures and queen
256-
/// promotions. Returns a pointer to the end of the move list.
255+
/// <CAPTURES> Generates all pseudo-legal captures and queen promotions
256+
/// <QUIETS> Generates all pseudo-legal non-captures and underpromotions
257+
/// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
257258
///
258-
/// generate<QUIETS> generates all pseudo-legal non-captures and
259-
/// underpromotions. Returns a pointer to the end of the move list.
260-
///
261-
/// generate<NON_EVASIONS> generates all pseudo-legal captures and
262-
/// non-captures. Returns a pointer to the end of the move list.
259+
/// Returns a pointer to the end of the move list.
263260

264261
template<GenType Type>
265262
ExtMove* generate(const Position& pos, ExtMove* moveList) {

src/movepick.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ void MovePicker::score() {
114114
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
115115
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
116116
+ (*continuationHistory[1])[pos.moved_piece(m)][to_sq(m)]
117-
+ (*continuationHistory[3])[pos.moved_piece(m)][to_sq(m)];
117+
+ (*continuationHistory[3])[pos.moved_piece(m)][to_sq(m)]
118+
+ (*continuationHistory[5])[pos.moved_piece(m)][to_sq(m)] / 2;
118119

119120
else // Type == EVASIONS
120121
{

src/pawns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace {
9696

9797
// Flag the pawn
9898
opposed = theirPawns & forward_file_bb(Us, s);
99-
stoppers = theirPawns & passed_pawn_mask(Us, s);
99+
stoppers = theirPawns & passed_pawn_span(Us, s);
100100
lever = theirPawns & PawnAttacks[Us][s];
101101
leverPush = theirPawns & PawnAttacks[Us][s + Up];
102102
doubled = ourPawns & (s - Up);

src/position.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ inline Bitboard Position::check_squares(PieceType pt) const {
310310
}
311311

312312
inline bool Position::pawn_passed(Color c, Square s) const {
313-
return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
313+
return !(pieces(~c, PAWN) & passed_pawn_span(c, s));
314314
}
315315

316316
inline bool Position::advanced_pawn_push(Move m) const {

0 commit comments

Comments
 (0)