2008-08-31 23:59:13 -06:00
|
|
|
/*
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
2021-01-08 09:04:23 -07:00
|
|
|
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
2008-08-31 23:59:13 -06:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2008-10-20 13:55:43 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
2008-08-31 23:59:13 -06:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2008-10-20 13:55:43 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-07-23 07:31:57 -06:00
|
|
|
#ifndef BITBOARD_H_INCLUDED
|
2008-08-31 23:59:13 -06:00
|
|
|
#define BITBOARD_H_INCLUDED
|
|
|
|
|
2014-08-08 23:11:36 -06:00
|
|
|
#include <string>
|
2014-09-30 01:05:20 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
#include "types.h"
|
|
|
|
|
2021-02-26 02:02:13 -07:00
|
|
|
namespace Stockfish {
|
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
namespace Bitbases {
|
2012-04-01 03:46:38 -06:00
|
|
|
|
2012-08-18 06:04:43 -06:00
|
|
|
void init();
|
2015-01-03 08:39:17 -07:00
|
|
|
bool probe(Square wksq, Square wpsq, Square bksq, Color us);
|
2012-08-18 06:04:43 -06:00
|
|
|
|
2021-02-26 02:02:13 -07:00
|
|
|
} // namespace Stockfish::Bitbases
|
2012-08-18 06:04:43 -06:00
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
namespace Bitboards {
|
2012-08-18 06:04:43 -06:00
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
void init();
|
2021-01-30 01:50:04 -07:00
|
|
|
std::string pretty(Bitboard b);
|
2012-04-01 03:46:38 -06:00
|
|
|
|
2021-02-26 02:02:13 -07:00
|
|
|
} // namespace Stockfish::Bitboards
|
2012-04-01 03:46:38 -06:00
|
|
|
|
2018-03-18 16:38:58 -06:00
|
|
|
constexpr Bitboard AllSquares = ~Bitboard(0);
|
|
|
|
constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
|
|
|
|
|
|
|
constexpr Bitboard FileABB = 0x0101010101010101ULL;
|
|
|
|
constexpr Bitboard FileBBB = FileABB << 1;
|
|
|
|
constexpr Bitboard FileCBB = FileABB << 2;
|
|
|
|
constexpr Bitboard FileDBB = FileABB << 3;
|
|
|
|
constexpr Bitboard FileEBB = FileABB << 4;
|
|
|
|
constexpr Bitboard FileFBB = FileABB << 5;
|
|
|
|
constexpr Bitboard FileGBB = FileABB << 6;
|
|
|
|
constexpr Bitboard FileHBB = FileABB << 7;
|
|
|
|
|
|
|
|
constexpr Bitboard Rank1BB = 0xFF;
|
|
|
|
constexpr Bitboard Rank2BB = Rank1BB << (8 * 1);
|
|
|
|
constexpr Bitboard Rank3BB = Rank1BB << (8 * 2);
|
|
|
|
constexpr Bitboard Rank4BB = Rank1BB << (8 * 3);
|
|
|
|
constexpr Bitboard Rank5BB = Rank1BB << (8 * 4);
|
|
|
|
constexpr Bitboard Rank6BB = Rank1BB << (8 * 5);
|
|
|
|
constexpr Bitboard Rank7BB = Rank1BB << (8 * 6);
|
|
|
|
constexpr Bitboard Rank8BB = Rank1BB << (8 * 7);
|
2013-06-23 03:15:58 -06:00
|
|
|
|
2019-02-08 02:36:03 -07:00
|
|
|
constexpr Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB;
|
|
|
|
constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
|
|
|
|
constexpr Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB;
|
|
|
|
constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
|
|
|
|
|
2019-03-31 04:02:19 -06:00
|
|
|
constexpr Bitboard KingFlank[FILE_NB] = {
|
|
|
|
QueenSide ^ FileDBB, QueenSide, QueenSide,
|
|
|
|
CenterFiles, CenterFiles,
|
|
|
|
KingSide, KingSide, KingSide ^ FileEBB
|
|
|
|
};
|
|
|
|
|
2019-02-08 02:36:03 -07:00
|
|
|
extern uint8_t PopCnt16[1 << 16];
|
|
|
|
extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
|
2015-01-03 08:39:17 -07:00
|
|
|
|
2019-03-31 04:02:19 -06:00
|
|
|
extern Bitboard SquareBB[SQUARE_NB];
|
Change definition of between_bb()
We remark that in current master, most of our use cases for between_bb() can be
optimized if the second parameter of the function is added to the segment. So we
change the definition of between_bb(s1, s2) such that it excludes s1 but includes s2.
We also use a precomputed array for between_bb() for another small speed gain
(see https://tests.stockfishchess.org/tests/view/604d09f72433018de7a389fb).
Passed STC:
LLR: 2.96 (-2.94,2.94) {-0.25,1.25}
Total: 18736 W: 1746 L: 1607 D: 15383
Ptnml(0-2): 61, 1226, 6644, 1387, 50
https://tests.stockfishchess.org/tests/view/60428c84ddcba5f0627bb6e4
Yellow LTC:
LTC:
LLR: -3.00 (-2.94,2.94) {0.25,1.25}
Total: 39144 W: 1431 L: 1413 D: 36300
Ptnml(0-2): 13, 1176, 17184, 1178, 21
https://tests.stockfishchess.org/tests/view/605128702433018de7a38ca1
Closes https://github.com/official-stockfish/Stockfish/pull/3397
---------
Verified for correctness by running perft on the following position:
./stockfish
position fen 4rrk1/1p1nq3/p7/2p1P1pp/3P2bp/3Q1Bn1/PPPB4/1K2R1NR w - - 40 21
go perft 6
Nodes searched: 6136386434
--------
No functional change
2021-03-15 13:06:42 -06:00
|
|
|
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
2013-11-08 19:06:47 -07:00
|
|
|
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
2012-10-21 02:41:23 -06:00
|
|
|
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
2017-04-28 21:33:30 -06:00
|
|
|
extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
|
2017-06-04 03:03:23 -06:00
|
|
|
/// Magic holds all magic bitboards relevant data for a single square
|
|
|
|
struct Magic {
|
|
|
|
Bitboard mask;
|
|
|
|
Bitboard magic;
|
|
|
|
Bitboard* attacks;
|
|
|
|
unsigned shift;
|
2017-06-28 18:11:17 -06:00
|
|
|
|
2017-06-30 23:58:38 -06:00
|
|
|
// Compute the attack's index using the 'magic bitboards' approach
|
2017-06-28 18:11:17 -06:00
|
|
|
unsigned index(Bitboard occupied) const {
|
|
|
|
|
2017-06-30 23:58:38 -06:00
|
|
|
if (HasPext)
|
|
|
|
return unsigned(pext(occupied, mask));
|
2017-06-28 18:11:17 -06:00
|
|
|
|
2017-06-30 23:58:38 -06:00
|
|
|
if (Is64Bit)
|
|
|
|
return unsigned(((occupied & mask) * magic) >> shift);
|
2017-06-28 18:11:17 -06:00
|
|
|
|
2017-06-30 23:58:38 -06:00
|
|
|
unsigned lo = unsigned(occupied) & unsigned(mask);
|
|
|
|
unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
|
|
|
|
return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
|
2017-06-28 18:11:17 -06:00
|
|
|
}
|
2017-06-04 03:03:23 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Magic RookMagics[SQUARE_NB];
|
|
|
|
extern Magic BishopMagics[SQUARE_NB];
|
|
|
|
|
2019-03-31 02:43:20 -06:00
|
|
|
inline Bitboard square_bb(Square s) {
|
2020-04-12 12:30:08 -06:00
|
|
|
assert(is_ok(s));
|
2019-03-31 02:43:20 -06:00
|
|
|
return SquareBB[s];
|
2015-01-03 08:39:17 -07:00
|
|
|
}
|
2019-03-31 03:47:36 -06:00
|
|
|
|
2020-06-24 14:19:58 -06:00
|
|
|
|
2019-03-31 03:47:36 -06:00
|
|
|
/// Overloads of bitwise operators between a Bitboard and a Square for testing
|
|
|
|
/// whether a given bit is set in a bitboard, and for setting and clearing bits.
|
|
|
|
|
2019-03-31 02:43:20 -06:00
|
|
|
inline Bitboard operator&( Bitboard b, Square s) { return b & square_bb(s); }
|
|
|
|
inline Bitboard operator|( Bitboard b, Square s) { return b | square_bb(s); }
|
|
|
|
inline Bitboard operator^( Bitboard b, Square s) { return b ^ square_bb(s); }
|
|
|
|
inline Bitboard& operator|=(Bitboard& b, Square s) { return b |= square_bb(s); }
|
|
|
|
inline Bitboard& operator^=(Bitboard& b, Square s) { return b ^= square_bb(s); }
|
2013-06-16 02:08:44 -06:00
|
|
|
|
Make Square and Bitboard operators commutative
As Stockfish developers, we aim to make our code as legible and as close
to simple English as possible. However, one of the more notable exceptions
to this rule concerns operations between Squares and Bitboards.
Prior to this pull request, AND, OR, and XOR were only defined when the
Bitboard was the first operand, and the Square the second. For example,
for a Bitboard b and Square s, "b & s" would be valid but "s & b" would not.
This conflicts with natural reasoning about logical operators, both
mathematically and intuitively, which says that logical operators should
commute.
More dangerously, however, both Square and Bitboard are defined as integers
"under the hood." As a result, code like "s & b" would still compile and give
reasonable bench values. This trap occasionally ensnares even experienced
Stockfish developers, but it is especially dangerous for new developers not
aware of this peculiarity. Because there is no compilation or runtime error,
and a reasonable bench, only a close review by approvers can spot this error
when a test has been submitted--and many times, these bugs have slipped past
review. This is by far the most common logical error on Fishtest, and has
wasted uncountable STC games over the years.
However, it can be fixed by adding three non-functional lines of code. In this
patch, we define the operators when the operands are provided in the opposite
order, i.e., we make AND, OR, and XOR commutative for Bitboards and Squares.
Because these are inline methods and implemented identically, the executable
does not change at all.
This patch has the small side-effect of requiring Squares to be explicitly
cast to integers before AND, OR, or XOR with integers. This is only performed
twice in Stockfish's source code, and again does not change the executable at
all (since Square is an enum defined as an integer anyway).
For demonstration purposes, this pull request also inverts the order of one AND
and one OR, to show that neither the bench nor the executable change. (This
change can be removed before merging, if preferred.)
I hope that this pull request significantly lowers the barrier-of-entry for new
developer to join the Stockfish project. I also hope that this change will improve
our efficiency in using our generous CPU donors' machines, since it will remove
one of the most common causes of buggy tests.
Following helpful review and comments by Michael Stembera (@mstembera), we add
a further clean-up by implementing OR for two Squares, to anticipate additional
traps developers may encounter and handle them cleanly.
Closes https://github.com/official-stockfish/Stockfish/pull/2387
No functional change.
2019-10-31 22:27:19 -06:00
|
|
|
inline Bitboard operator&(Square s, Bitboard b) { return b & s; }
|
|
|
|
inline Bitboard operator|(Square s, Bitboard b) { return b | s; }
|
|
|
|
inline Bitboard operator^(Square s, Bitboard b) { return b ^ s; }
|
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
inline Bitboard operator|(Square s1, Square s2) { return square_bb(s1) | s2; }
|
Make Square and Bitboard operators commutative
As Stockfish developers, we aim to make our code as legible and as close
to simple English as possible. However, one of the more notable exceptions
to this rule concerns operations between Squares and Bitboards.
Prior to this pull request, AND, OR, and XOR were only defined when the
Bitboard was the first operand, and the Square the second. For example,
for a Bitboard b and Square s, "b & s" would be valid but "s & b" would not.
This conflicts with natural reasoning about logical operators, both
mathematically and intuitively, which says that logical operators should
commute.
More dangerously, however, both Square and Bitboard are defined as integers
"under the hood." As a result, code like "s & b" would still compile and give
reasonable bench values. This trap occasionally ensnares even experienced
Stockfish developers, but it is especially dangerous for new developers not
aware of this peculiarity. Because there is no compilation or runtime error,
and a reasonable bench, only a close review by approvers can spot this error
when a test has been submitted--and many times, these bugs have slipped past
review. This is by far the most common logical error on Fishtest, and has
wasted uncountable STC games over the years.
However, it can be fixed by adding three non-functional lines of code. In this
patch, we define the operators when the operands are provided in the opposite
order, i.e., we make AND, OR, and XOR commutative for Bitboards and Squares.
Because these are inline methods and implemented identically, the executable
does not change at all.
This patch has the small side-effect of requiring Squares to be explicitly
cast to integers before AND, OR, or XOR with integers. This is only performed
twice in Stockfish's source code, and again does not change the executable at
all (since Square is an enum defined as an integer anyway).
For demonstration purposes, this pull request also inverts the order of one AND
and one OR, to show that neither the bench nor the executable change. (This
change can be removed before merging, if preferred.)
I hope that this pull request significantly lowers the barrier-of-entry for new
developer to join the Stockfish project. I also hope that this change will improve
our efficiency in using our generous CPU donors' machines, since it will remove
one of the most common causes of buggy tests.
Following helpful review and comments by Michael Stembera (@mstembera), we add
a further clean-up by implementing OR for two Squares, to anticipate additional
traps developers may encounter and handle them cleanly.
Closes https://github.com/official-stockfish/Stockfish/pull/2387
No functional change.
2019-10-31 22:27:19 -06:00
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr bool more_than_one(Bitboard b) {
|
2015-01-03 08:39:17 -07:00
|
|
|
return b & (b - 1);
|
2013-06-16 02:08:44 -06:00
|
|
|
}
|
|
|
|
|
Introduce bad outpost penalty
In some French games, Stockfish likes to bring the Knight to a bad outpost spot. This is evident in TCEC S18 Superfinal Game 63, where there is a Knight outpost on the queenside but is actually useless. Stockfish is effectively playing a piece down while holding ground against Leela's break on the kingside.
This patch turns the +56 mg bonus for a Knight outpost into a -7 mg penalty if it satisfies the following conditions:
* The outpost square is not on the CenterFiles (i.e. not on files C,D,E and F)
* The knight is not attacking non pawn enemies.
* The side where the outpost is located contains only few enemies, with a particular conditional_more_than_two() implementation
Thank you to apospa...@gmail.com for bringing this to our attention and for providing insights.
See https://groups.google.com/forum/?fromgroups=#!topic/fishcooking/dEXNzSIBgZU
Reference game: https://tcec-chess.com/#div=sf&game=63&season=18
Passed STC:
LLR: 2.93 (-2.94,2.94) {-0.50,1.50}
Total: 6960 W: 1454 L: 1247 D: 4259
Ptnml(0-2): 115, 739, 1610, 856, 160
https://tests.stockfishchess.org/tests/view/5f08221059f6f0353289477e
Passed LTC:
LLR: 2.98 (-2.94,2.94) {0.25,1.75}
Total: 21440 W: 2767 L: 2543 D: 16130
Ptnml(0-2): 122, 1904, 6462, 2092, 140
https://tests.stockfishchess.org/tests/view/5f0838ed59f6f035328947a2
various related tests show strong test results, but so far no generalizations or simplifications of conditional_more_than_two() are found. See PR for details.
closes https://github.com/official-stockfish/Stockfish/pull/2803
Bench: 4366686
2020-07-07 20:09:32 -06:00
|
|
|
|
2019-12-23 10:58:30 -07:00
|
|
|
constexpr bool opposite_colors(Square s1, Square s2) {
|
|
|
|
return (s1 + rank_of(s1) + s2 + rank_of(s2)) & 1;
|
2018-12-08 10:57:25 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 02:36:03 -07:00
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
/// rank_bb() and file_bb() return a bitboard representing all the squares on
|
|
|
|
/// the given file or rank.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard rank_bb(Rank r) {
|
2019-02-08 01:54:38 -07:00
|
|
|
return Rank1BB << (8 * r);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard rank_bb(Square s) {
|
2019-02-08 01:54:38 -07:00
|
|
|
return rank_bb(rank_of(s));
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard file_bb(File f) {
|
2019-02-08 01:54:38 -07:00
|
|
|
return FileABB << f;
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard file_bb(Square s) {
|
2019-02-08 01:54:38 -07:00
|
|
|
return file_bb(file_of(s));
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-01 01:31:17 -07:00
|
|
|
/// shift() moves a bitboard one or two steps as specified by the direction D
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2017-12-04 09:52:31 -07:00
|
|
|
template<Direction D>
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Bitboard shift(Bitboard b) {
|
2016-09-23 11:28:34 -06:00
|
|
|
return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
|
2019-03-31 04:02:19 -06:00
|
|
|
: D == NORTH+NORTH? b <<16 : D == SOUTH+SOUTH? b >>16
|
2018-03-26 01:26:50 -06:00
|
|
|
: D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1
|
2018-03-27 08:22:53 -06:00
|
|
|
: D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
|
2018-03-26 01:26:50 -06:00
|
|
|
: D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
|
2015-01-03 08:39:17 -07:00
|
|
|
: 0;
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2018-02-23 14:02:10 -07:00
|
|
|
|
2019-01-01 06:13:08 -07:00
|
|
|
/// pawn_attacks_bb() returns the squares attacked by pawns of the given color
|
|
|
|
/// from the squares in the given bitboard.
|
2018-02-21 14:31:38 -07:00
|
|
|
|
2018-02-23 14:02:10 -07:00
|
|
|
template<Color C>
|
2018-02-21 14:31:38 -07:00
|
|
|
constexpr Bitboard pawn_attacks_bb(Bitboard b) {
|
2018-02-23 14:02:10 -07:00
|
|
|
return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
|
2018-02-21 14:31:38 -07:00
|
|
|
: shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b);
|
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2020-05-28 09:48:31 -06:00
|
|
|
inline Bitboard pawn_attacks_bb(Color c, Square s) {
|
|
|
|
|
|
|
|
assert(is_ok(s));
|
|
|
|
return PawnAttacks[c][s];
|
|
|
|
}
|
|
|
|
|
2018-02-23 14:02:10 -07:00
|
|
|
|
2019-01-01 06:13:08 -07:00
|
|
|
/// pawn_double_attacks_bb() returns the squares doubly attacked by pawns of the
|
|
|
|
/// given color from the squares in the given bitboard.
|
2018-12-02 12:18:14 -07:00
|
|
|
|
|
|
|
template<Color C>
|
2019-01-01 06:13:08 -07:00
|
|
|
constexpr Bitboard pawn_double_attacks_bb(Bitboard b) {
|
2018-12-02 12:18:14 -07:00
|
|
|
return C == WHITE ? shift<NORTH_WEST>(b) & shift<NORTH_EAST>(b)
|
|
|
|
: shift<SOUTH_WEST>(b) & shift<SOUTH_EAST>(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
/// adjacent_files_bb() returns a bitboard representing all the squares on the
|
2020-07-09 14:01:06 -06:00
|
|
|
/// adjacent files of a given square.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard adjacent_files_bb(Square s) {
|
2019-06-09 06:31:16 -06:00
|
|
|
return shift<EAST>(file_bb(s)) | shift<WEST>(file_bb(s));
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:19:58 -06:00
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
/// line_bb() returns a bitboard representing an entire line (from board edge
|
|
|
|
/// to board edge) that intersects the two given squares. If the given squares
|
|
|
|
/// are not on a same file/rank/diagonal, the function returns 0. For instance,
|
2020-06-24 14:19:58 -06:00
|
|
|
/// line_bb(SQ_C4, SQ_F7) will return a bitboard with the A2-G8 diagonal.
|
2020-06-06 21:25:32 -06:00
|
|
|
|
|
|
|
inline Bitboard line_bb(Square s1, Square s2) {
|
|
|
|
|
|
|
|
assert(is_ok(s1) && is_ok(s2));
|
2021-03-24 14:55:49 -06:00
|
|
|
|
2020-06-06 21:25:32 -06:00
|
|
|
return LineBB[s1][s2];
|
|
|
|
}
|
2019-02-08 02:36:03 -07:00
|
|
|
|
2020-06-24 14:19:58 -06:00
|
|
|
|
Change definition of between_bb()
We remark that in current master, most of our use cases for between_bb() can be
optimized if the second parameter of the function is added to the segment. So we
change the definition of between_bb(s1, s2) such that it excludes s1 but includes s2.
We also use a precomputed array for between_bb() for another small speed gain
(see https://tests.stockfishchess.org/tests/view/604d09f72433018de7a389fb).
Passed STC:
LLR: 2.96 (-2.94,2.94) {-0.25,1.25}
Total: 18736 W: 1746 L: 1607 D: 15383
Ptnml(0-2): 61, 1226, 6644, 1387, 50
https://tests.stockfishchess.org/tests/view/60428c84ddcba5f0627bb6e4
Yellow LTC:
LTC:
LLR: -3.00 (-2.94,2.94) {0.25,1.25}
Total: 39144 W: 1431 L: 1413 D: 36300
Ptnml(0-2): 13, 1176, 17184, 1178, 21
https://tests.stockfishchess.org/tests/view/605128702433018de7a38ca1
Closes https://github.com/official-stockfish/Stockfish/pull/3397
---------
Verified for correctness by running perft on the following position:
./stockfish
position fen 4rrk1/1p1nq3/p7/2p1P1pp/3P2bp/3Q1Bn1/PPPB4/1K2R1NR w - - 40 21
go perft 6
Nodes searched: 6136386434
--------
No functional change
2021-03-15 13:06:42 -06:00
|
|
|
/// between_bb(s1, s2) returns a bitboard representing the squares in the semi-open
|
|
|
|
/// segment between the squares s1 and s2 (excluding s1 but including s2). If the
|
|
|
|
/// given squares are not on a same file/rank/diagonal, it returns s2. For instance,
|
|
|
|
/// between_bb(SQ_C4, SQ_F7) will return a bitboard with squares D5, E6 and F7, but
|
|
|
|
/// between_bb(SQ_E6, SQ_F8) will return a bitboard with the square F8. This trick
|
|
|
|
/// allows to generate non-king evasion moves faster: the defending piece must either
|
|
|
|
/// interpose itself to cover the check or capture the checking piece.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2012-04-14 02:16:34 -06:00
|
|
|
inline Bitboard between_bb(Square s1, Square s2) {
|
2021-03-24 14:55:49 -06:00
|
|
|
|
Change definition of between_bb()
We remark that in current master, most of our use cases for between_bb() can be
optimized if the second parameter of the function is added to the segment. So we
change the definition of between_bb(s1, s2) such that it excludes s1 but includes s2.
We also use a precomputed array for between_bb() for another small speed gain
(see https://tests.stockfishchess.org/tests/view/604d09f72433018de7a389fb).
Passed STC:
LLR: 2.96 (-2.94,2.94) {-0.25,1.25}
Total: 18736 W: 1746 L: 1607 D: 15383
Ptnml(0-2): 61, 1226, 6644, 1387, 50
https://tests.stockfishchess.org/tests/view/60428c84ddcba5f0627bb6e4
Yellow LTC:
LTC:
LLR: -3.00 (-2.94,2.94) {0.25,1.25}
Total: 39144 W: 1431 L: 1413 D: 36300
Ptnml(0-2): 13, 1176, 17184, 1178, 21
https://tests.stockfishchess.org/tests/view/605128702433018de7a38ca1
Closes https://github.com/official-stockfish/Stockfish/pull/3397
---------
Verified for correctness by running perft on the following position:
./stockfish
position fen 4rrk1/1p1nq3/p7/2p1P1pp/3P2bp/3Q1Bn1/PPPB4/1K2R1NR w - - 40 21
go perft 6
Nodes searched: 6136386434
--------
No functional change
2021-03-15 13:06:42 -06:00
|
|
|
assert(is_ok(s1) && is_ok(s2));
|
2021-03-24 14:55:49 -06:00
|
|
|
|
Change definition of between_bb()
We remark that in current master, most of our use cases for between_bb() can be
optimized if the second parameter of the function is added to the segment. So we
change the definition of between_bb(s1, s2) such that it excludes s1 but includes s2.
We also use a precomputed array for between_bb() for another small speed gain
(see https://tests.stockfishchess.org/tests/view/604d09f72433018de7a389fb).
Passed STC:
LLR: 2.96 (-2.94,2.94) {-0.25,1.25}
Total: 18736 W: 1746 L: 1607 D: 15383
Ptnml(0-2): 61, 1226, 6644, 1387, 50
https://tests.stockfishchess.org/tests/view/60428c84ddcba5f0627bb6e4
Yellow LTC:
LTC:
LLR: -3.00 (-2.94,2.94) {0.25,1.25}
Total: 39144 W: 1431 L: 1413 D: 36300
Ptnml(0-2): 13, 1176, 17184, 1178, 21
https://tests.stockfishchess.org/tests/view/605128702433018de7a38ca1
Closes https://github.com/official-stockfish/Stockfish/pull/3397
---------
Verified for correctness by running perft on the following position:
./stockfish
position fen 4rrk1/1p1nq3/p7/2p1P1pp/3P2bp/3Q1Bn1/PPPB4/1K2R1NR w - - 40 21
go perft 6
Nodes searched: 6136386434
--------
No functional change
2021-03-15 13:06:42 -06:00
|
|
|
return BetweenBB[s1][s2];
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Change definition of between_bb()
We remark that in current master, most of our use cases for between_bb() can be
optimized if the second parameter of the function is added to the segment. So we
change the definition of between_bb(s1, s2) such that it excludes s1 but includes s2.
We also use a precomputed array for between_bb() for another small speed gain
(see https://tests.stockfishchess.org/tests/view/604d09f72433018de7a389fb).
Passed STC:
LLR: 2.96 (-2.94,2.94) {-0.25,1.25}
Total: 18736 W: 1746 L: 1607 D: 15383
Ptnml(0-2): 61, 1226, 6644, 1387, 50
https://tests.stockfishchess.org/tests/view/60428c84ddcba5f0627bb6e4
Yellow LTC:
LTC:
LLR: -3.00 (-2.94,2.94) {0.25,1.25}
Total: 39144 W: 1431 L: 1413 D: 36300
Ptnml(0-2): 13, 1176, 17184, 1178, 21
https://tests.stockfishchess.org/tests/view/605128702433018de7a38ca1
Closes https://github.com/official-stockfish/Stockfish/pull/3397
---------
Verified for correctness by running perft on the following position:
./stockfish
position fen 4rrk1/1p1nq3/p7/2p1P1pp/3P2bp/3Q1Bn1/PPPB4/1K2R1NR w - - 40 21
go perft 6
Nodes searched: 6136386434
--------
No functional change
2021-03-15 13:06:42 -06:00
|
|
|
/// forward_ranks_bb() returns a bitboard representing the squares on the ranks in
|
|
|
|
/// front of the given one, from the point of view of the given color. For instance,
|
2018-02-23 14:02:10 -07:00
|
|
|
/// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
|
2015-01-03 08:39:17 -07:00
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard forward_ranks_bb(Color c, Square s) {
|
2020-04-12 12:30:08 -06:00
|
|
|
return c == WHITE ? ~Rank1BB << 8 * relative_rank(WHITE, s)
|
|
|
|
: ~Rank8BB >> 8 * relative_rank(BLACK, s);
|
2015-01-03 08:39:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-08 02:36:03 -07:00
|
|
|
/// forward_file_bb() returns a bitboard representing all the squares along the
|
|
|
|
/// line in front of the given one, from the point of view of the given color.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard forward_file_bb(Color c, Square s) {
|
2019-02-08 02:36:03 -07:00
|
|
|
return forward_ranks_bb(c, s) & file_bb(s);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-08 02:36:03 -07:00
|
|
|
/// pawn_attack_span() returns a bitboard representing all the squares that can
|
2020-06-24 14:19:58 -06:00
|
|
|
/// be attacked by a pawn of the given color when it moves along its file, starting
|
|
|
|
/// from the given square.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard pawn_attack_span(Color c, Square s) {
|
2019-06-09 06:31:16 -06:00
|
|
|
return forward_ranks_bb(c, s) & adjacent_files_bb(s);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-08 02:36:03 -07:00
|
|
|
/// passed_pawn_span() returns a bitboard which can be used to test if a pawn of
|
|
|
|
/// the given color and on the given square is a passed pawn.
|
2013-06-23 05:19:03 -06:00
|
|
|
|
2020-07-09 14:01:06 -06:00
|
|
|
constexpr Bitboard passed_pawn_span(Color c, Square s) {
|
2020-06-21 07:21:46 -06:00
|
|
|
return pawn_attack_span(c, s) | forward_file_bb(c, s);
|
2013-06-23 05:19:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
/// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
|
|
|
|
/// straight or on a diagonal line.
|
2010-12-26 08:27:58 -07:00
|
|
|
|
2013-11-10 09:14:46 -07:00
|
|
|
inline bool aligned(Square s1, Square s2, Square s3) {
|
2020-06-06 21:25:32 -06:00
|
|
|
return line_bb(s1, s2) & s3;
|
2010-12-26 08:27:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
/// distance() functions return the distance between x and y, defined as the
|
2019-04-18 08:53:52 -06:00
|
|
|
/// number of steps for a king in x to reach y.
|
2015-01-03 08:39:17 -07:00
|
|
|
|
2019-04-18 08:53:52 -06:00
|
|
|
template<typename T1 = Square> inline int distance(Square x, Square y);
|
|
|
|
template<> inline int distance<File>(Square x, Square y) { return std::abs(file_of(x) - file_of(y)); }
|
|
|
|
template<> inline int distance<Rank>(Square x, Square y) { return std::abs(rank_of(x) - rank_of(y)); }
|
2015-01-03 08:39:17 -07:00
|
|
|
template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
|
|
|
|
|
2020-04-12 12:30:08 -06:00
|
|
|
inline int edge_distance(File f) { return std::min(f, File(FILE_H - f)); }
|
|
|
|
inline int edge_distance(Rank r) { return std::min(r, Rank(RANK_8 - r)); }
|
|
|
|
|
2020-06-24 14:19:58 -06:00
|
|
|
|
2020-05-28 09:48:31 -06:00
|
|
|
/// attacks_bb(Square) returns the pseudo attacks of the give piece type
|
|
|
|
/// assuming an empty board.
|
|
|
|
|
|
|
|
template<PieceType Pt>
|
|
|
|
inline Bitboard attacks_bb(Square s) {
|
|
|
|
|
|
|
|
assert((Pt != PAWN) && (is_ok(s)));
|
|
|
|
|
|
|
|
return PseudoAttacks[Pt][s];
|
|
|
|
}
|
|
|
|
|
2020-06-24 14:19:58 -06:00
|
|
|
|
2020-05-28 09:48:31 -06:00
|
|
|
/// attacks_bb(Square, Bitboard) returns the attacks by the given piece
|
|
|
|
/// assuming the board is occupied according to the passed Bitboard.
|
|
|
|
/// Sliding piece attacks do not continue passed an occupied square.
|
2012-04-14 02:16:34 -06:00
|
|
|
|
|
|
|
template<PieceType Pt>
|
2014-12-07 16:53:33 -07:00
|
|
|
inline Bitboard attacks_bb(Square s, Bitboard occupied) {
|
2016-04-07 00:55:38 -06:00
|
|
|
|
2020-05-28 09:48:31 -06:00
|
|
|
assert((Pt != PAWN) && (is_ok(s)));
|
|
|
|
|
|
|
|
switch (Pt)
|
|
|
|
{
|
|
|
|
case BISHOP: return BishopMagics[s].attacks[BishopMagics[s].index(occupied)];
|
|
|
|
case ROOK : return RookMagics[s].attacks[ RookMagics[s].index(occupied)];
|
|
|
|
case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
|
|
|
|
default : return PseudoAttacks[Pt][s];
|
|
|
|
}
|
2012-03-04 15:26:08 -07:00
|
|
|
}
|
|
|
|
|
2017-04-28 21:33:30 -06:00
|
|
|
inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
|
2013-11-30 02:27:23 -07:00
|
|
|
|
2020-05-28 09:48:31 -06:00
|
|
|
assert((pt != PAWN) && (is_ok(s)));
|
2017-04-28 21:33:30 -06:00
|
|
|
|
|
|
|
switch (pt)
|
2013-11-30 02:27:23 -07:00
|
|
|
{
|
2014-12-07 16:53:33 -07:00
|
|
|
case BISHOP: return attacks_bb<BISHOP>(s, occupied);
|
2017-06-11 15:31:15 -06:00
|
|
|
case ROOK : return attacks_bb< ROOK>(s, occupied);
|
2014-12-07 16:53:33 -07:00
|
|
|
case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
|
2017-04-28 21:33:30 -06:00
|
|
|
default : return PseudoAttacks[pt][s];
|
2013-11-30 02:27:23 -07:00
|
|
|
}
|
|
|
|
}
|
2012-03-28 03:03:06 -06:00
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
|
2016-04-08 11:52:15 -06:00
|
|
|
/// popcount() counts the number of non-zero bits in a bitboard
|
|
|
|
|
|
|
|
inline int popcount(Bitboard b) {
|
|
|
|
|
|
|
|
#ifndef USE_POPCNT
|
|
|
|
|
|
|
|
union { Bitboard bb; uint16_t u[4]; } v = { b };
|
|
|
|
return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
|
|
|
|
|
2016-05-01 02:57:50 -06:00
|
|
|
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
2016-04-08 11:52:15 -06:00
|
|
|
|
2016-05-01 07:10:33 -06:00
|
|
|
return (int)_mm_popcnt_u64(b);
|
2016-04-08 11:52:15 -06:00
|
|
|
|
|
|
|
#else // Assumed gcc or compatible compiler
|
|
|
|
|
|
|
|
return __builtin_popcountll(b);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 08:39:17 -07:00
|
|
|
/// lsb() and msb() return the least/most significant bit in a non-zero bitboard
|
2009-07-02 08:49:41 -06:00
|
|
|
|
2018-03-11 16:56:14 -06:00
|
|
|
#if defined(__GNUC__) // GCC, Clang, ICC
|
2009-07-02 08:49:41 -06:00
|
|
|
|
2016-03-29 06:37:42 -06:00
|
|
|
inline Square lsb(Bitboard b) {
|
|
|
|
assert(b);
|
|
|
|
return Square(__builtin_ctzll(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Square msb(Bitboard b) {
|
|
|
|
assert(b);
|
2016-12-03 01:37:07 -07:00
|
|
|
return Square(63 ^ __builtin_clzll(b));
|
2016-03-29 06:37:42 -06:00
|
|
|
}
|
2016-03-28 02:08:06 -06:00
|
|
|
|
2018-03-11 16:56:14 -06:00
|
|
|
#elif defined(_MSC_VER) // MSVC
|
|
|
|
|
|
|
|
#ifdef _WIN64 // MSVC, WIN64
|
2011-03-17 06:47:15 -06:00
|
|
|
|
Retire FORCE_INLINE
No speed regression on my machine (i7-3770k, gcc 4.9.1, linux 3.16):
stat test master diff
mean 2,482,415 2,474,987 7,906
stdev 4,603 5,644 2,497
speedup 0.32%
P(speedup>0) 100.0%
Fishtest 9+0.03:
ELO: 0.26 +-1.8 (95%) LOS: 61.2%
Total: 60000 W: 12437 L: 12392 D: 35171
No functional change.
Resolves #334
2015-04-15 14:21:45 -06:00
|
|
|
inline Square lsb(Bitboard b) {
|
2016-03-29 06:37:42 -06:00
|
|
|
assert(b);
|
2014-04-06 02:50:27 -06:00
|
|
|
unsigned long idx;
|
|
|
|
_BitScanForward64(&idx, b);
|
|
|
|
return (Square) idx;
|
2012-03-26 05:52:10 -06:00
|
|
|
}
|
|
|
|
|
Retire FORCE_INLINE
No speed regression on my machine (i7-3770k, gcc 4.9.1, linux 3.16):
stat test master diff
mean 2,482,415 2,474,987 7,906
stdev 4,603 5,644 2,497
speedup 0.32%
P(speedup>0) 100.0%
Fishtest 9+0.03:
ELO: 0.26 +-1.8 (95%) LOS: 61.2%
Total: 60000 W: 12437 L: 12392 D: 35171
No functional change.
Resolves #334
2015-04-15 14:21:45 -06:00
|
|
|
inline Square msb(Bitboard b) {
|
2016-03-29 06:37:42 -06:00
|
|
|
assert(b);
|
2014-04-06 02:50:27 -06:00
|
|
|
unsigned long idx;
|
|
|
|
_BitScanReverse64(&idx, b);
|
|
|
|
return (Square) idx;
|
2011-03-17 06:47:15 -06:00
|
|
|
}
|
|
|
|
|
2018-03-11 16:56:14 -06:00
|
|
|
#else // MSVC, WIN32
|
|
|
|
|
|
|
|
inline Square lsb(Bitboard b) {
|
|
|
|
assert(b);
|
|
|
|
unsigned long idx;
|
|
|
|
|
|
|
|
if (b & 0xffffffff) {
|
|
|
|
_BitScanForward(&idx, int32_t(b));
|
|
|
|
return Square(idx);
|
|
|
|
} else {
|
|
|
|
_BitScanForward(&idx, int32_t(b >> 32));
|
|
|
|
return Square(idx + 32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Square msb(Bitboard b) {
|
|
|
|
assert(b);
|
|
|
|
unsigned long idx;
|
|
|
|
|
|
|
|
if (b >> 32) {
|
|
|
|
_BitScanReverse(&idx, int32_t(b >> 32));
|
|
|
|
return Square(idx + 32);
|
|
|
|
} else {
|
|
|
|
_BitScanReverse(&idx, int32_t(b));
|
|
|
|
return Square(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2012-07-08 01:30:37 -06:00
|
|
|
|
2018-03-11 16:56:14 -06:00
|
|
|
#else // Compiler is neither GCC nor MSVC compatible
|
2015-01-03 08:39:17 -07:00
|
|
|
|
2018-03-11 16:56:14 -06:00
|
|
|
#error "Compiler not supported."
|
2015-01-03 08:39:17 -07:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-03-16 13:51:31 -06:00
|
|
|
/// least_significant_square_bb() returns the bitboard of the least significant
|
|
|
|
/// square of a non-zero bitboard. It is equivalent to square_bb(lsb(bb)).
|
|
|
|
|
|
|
|
inline Bitboard least_significant_square_bb(Bitboard b) {
|
|
|
|
assert(b);
|
|
|
|
return b & -b;
|
|
|
|
}
|
2015-01-03 08:39:17 -07:00
|
|
|
|
|
|
|
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
|
|
|
|
|
2021-03-13 09:40:07 -07:00
|
|
|
inline Square pop_lsb(Bitboard& b) {
|
|
|
|
assert(b);
|
|
|
|
const Square s = lsb(b);
|
|
|
|
b &= b - 1;
|
2009-07-03 01:28:13 -06:00
|
|
|
return s;
|
2009-07-02 08:49:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-14 10:04:50 -06:00
|
|
|
/// frontmost_sq() returns the most advanced square for the given color,
|
|
|
|
/// requires a non-zero bitboard.
|
2019-08-14 14:15:41 -06:00
|
|
|
inline Square frontmost_sq(Color c, Bitboard b) {
|
2020-03-14 10:04:50 -06:00
|
|
|
assert(b);
|
2019-08-14 14:15:41 -06:00
|
|
|
return c == WHITE ? msb(b) : lsb(b);
|
|
|
|
}
|
2013-08-28 01:35:04 -06:00
|
|
|
|
2021-02-26 02:02:13 -07:00
|
|
|
} // namespace Stockfish
|
|
|
|
|
2013-07-23 07:31:57 -06:00
|
|
|
#endif // #ifndef BITBOARD_H_INCLUDED
|