Improve mobility performance by bitboard

And fix mobilityDiff value is wrong when moving.
This commit is contained in:
Calcitem 2021-06-14 21:26:11 +08:00
parent c0f92c2dcd
commit d719c70c5a
1 changed files with 34 additions and 49 deletions

View File

@ -719,28 +719,26 @@ bool Position::put_piece(Square s, bool updateRecord)
const Piece pc = board[currentSquare]; const Piece pc = board[currentSquare];
board[s] = pc;
update_key(s);
revert_key(currentSquare);
updateMobility(MOVETYPE_PLACE, s);
board[currentSquare] = NO_PIECE;
updateMobility(MOVETYPE_REMOVE, currentSquare);
CLEAR_BIT(byTypeBB[ALL_PIECES], currentSquare); CLEAR_BIT(byTypeBB[ALL_PIECES], currentSquare);
CLEAR_BIT(byTypeBB[type_of(pc)], currentSquare); CLEAR_BIT(byTypeBB[type_of(pc)], currentSquare);
CLEAR_BIT(byColorBB[color_of(pc)], currentSquare); CLEAR_BIT(byColorBB[color_of(pc)], currentSquare);
updateMobility(MOVETYPE_REMOVE, currentSquare);
SET_BIT(byTypeBB[ALL_PIECES], s); SET_BIT(byTypeBB[ALL_PIECES], s);
SET_BIT(byTypeBB[type_of(pc)], s); SET_BIT(byTypeBB[type_of(pc)], s);
SET_BIT(byColorBB[color_of(pc)], s); SET_BIT(byColorBB[color_of(pc)], s);
updateMobility(MOVETYPE_PLACE, s);
board[s] = pc;
update_key(s);
revert_key(currentSquare);
board[currentSquare] = NO_PIECE;
currentSquare = s; currentSquare = s;
const int n = mills_count(currentSquare); const int n = mills_count(currentSquare);
if (n == 0 if (n == 0
@ -792,8 +790,6 @@ bool Position::remove_piece(Square s, bool updateRecord)
revert_key(s); revert_key(s);
updateMobility(MOVETYPE_REMOVE, s);
if (rule.hasBannedLocations && phase == Phase::placing) { if (rule.hasBannedLocations && phase == Phase::placing) {
// Remove and put ban // Remove and put ban
Piece pc = board[s]; Piece pc = board[s];
@ -801,6 +797,8 @@ bool Position::remove_piece(Square s, bool updateRecord)
CLEAR_BIT(byTypeBB[type_of(pc)], s); // TODO CLEAR_BIT(byTypeBB[type_of(pc)], s); // TODO
CLEAR_BIT(byColorBB[color_of(pc)], s); CLEAR_BIT(byColorBB[color_of(pc)], s);
updateMobility(MOVETYPE_REMOVE, s);
pc = board[s] = BAN_STONE; pc = board[s] = BAN_STONE;
update_key(s); update_key(s);
@ -813,6 +811,8 @@ bool Position::remove_piece(Square s, bool updateRecord)
CLEAR_BIT(byTypeBB[type_of(pc)], s); CLEAR_BIT(byTypeBB[type_of(pc)], s);
CLEAR_BIT(byColorBB[color_of(pc)], s); CLEAR_BIT(byColorBB[color_of(pc)], s);
updateMobility(MOVETYPE_REMOVE, s);
board[s] = NO_PIECE; board[s] = NO_PIECE;
} }
@ -1356,45 +1356,30 @@ void Position::reset_bb()
void Position::updateMobility(MoveType mt, Square s) void Position::updateMobility(MoveType mt, Square s)
{ {
Bitboard adjacentWhiteBB = byColorBB[WHITE] & MoveList<LEGAL>::adjacentSquaresBB[s];
Bitboard adjacentBlackBB = byColorBB[BLACK] & MoveList<LEGAL>::adjacentSquaresBB[s];
Bitboard adjacentNoColorBB = (~(byColorBB[BLACK] | byColorBB[WHITE])) & MoveList<LEGAL>::adjacentSquaresBB[s];
int adjacentWhiteBBCount = popcount(adjacentWhiteBB);
int adjacentBlackBBCount = popcount(adjacentBlackBB);
int adjacentNoColorBBCount = popcount(adjacentNoColorBB);
if (mt == MOVETYPE_PLACE) { if (mt == MOVETYPE_PLACE) {
for (MoveDirection d = MD_BEGIN; d < MD_NB; ++d) { mobilityDiff -= adjacentWhiteBBCount;
auto adjacentSquare = static_cast<Square>(MoveList<LEGAL>::adjacentSquares[s][d]); mobilityDiff += adjacentBlackBBCount;
if (!adjacentSquare) {
break;;
}
if (board[adjacentSquare] & W_STONE) {
mobilityDiff--;
} else if (board[adjacentSquare] & B_STONE) {
mobilityDiff++;
} else {
if (side_to_move() == WHITE) { if (side_to_move() == WHITE) {
mobilityDiff++; mobilityDiff += adjacentNoColorBBCount;
} else { } else {
mobilityDiff--; mobilityDiff -= adjacentNoColorBBCount;
}
}
} }
} else if (mt == MOVETYPE_REMOVE) { } else if (mt == MOVETYPE_REMOVE) {
for (MoveDirection d = MD_BEGIN; d < MD_NB; ++d) { mobilityDiff += adjacentWhiteBBCount;
auto adjacentSquare = static_cast<Square>(MoveList<LEGAL>::adjacentSquares[s][d]); mobilityDiff -= adjacentBlackBBCount;
if (!adjacentSquare) { if ( color_of(board[s]) == WHITE) {
break; mobilityDiff -= adjacentNoColorBBCount;
}
if (board[adjacentSquare] & W_STONE) {
mobilityDiff++;
} else if (board[adjacentSquare] & B_STONE) {
mobilityDiff--;
} else { } else {
if (side_to_move() == WHITE) { mobilityDiff += adjacentNoColorBBCount;
mobilityDiff++;
} else {
mobilityDiff--;
}
}
} }
} else { } else {
assert(0); assert(0);