1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-29 16:23:09 +00:00

Assorted trivial cleanups 1/2019

To address #1862

No functional change.
This commit is contained in:
Marco Costalba 2019-01-01 14:13:08 +01:00
parent 332b90455b
commit 05f7d59a9a
9 changed files with 94 additions and 95 deletions

View file

@ -153,7 +153,7 @@ inline Bitboard file_bb(Square s) {
}
/// shift() moves a bitboard one step along direction D (mainly for pawns)
/// shift() moves a bitboard one step along direction D
template<Direction D>
constexpr Bitboard shift(Bitboard b) {
@ -165,8 +165,8 @@ constexpr Bitboard shift(Bitboard b) {
}
/// pawn_attacks_bb() returns the pawn attacks for the given color from the
/// squares in the given bitboard.
/// pawn_attacks_bb() returns the squares attacked by pawns of the given color
/// from the squares in the given bitboard.
template<Color C>
constexpr Bitboard pawn_attacks_bb(Bitboard b) {
@ -175,11 +175,11 @@ constexpr Bitboard pawn_attacks_bb(Bitboard b) {
}
/// double_pawn_attacks_bb() returns the pawn attacks for the given color
/// from the squares in the given bitboard.
/// pawn_double_attacks_bb() returns the squares doubly attacked by pawns of the
/// given color from the squares in the given bitboard.
template<Color C>
constexpr Bitboard double_pawn_attacks_bb(Bitboard b) {
constexpr Bitboard pawn_double_attacks_bb(Bitboard b) {
return C == WHITE ? shift<NORTH_WEST>(b) & shift<NORTH_EAST>(b)
: shift<SOUTH_WEST>(b) & shift<SOUTH_EAST>(b);
}

View file

@ -153,8 +153,8 @@ namespace {
// Assorted bonuses and penalties
constexpr Score BishopPawns = S( 3, 7);
constexpr Score CloseEnemies = S( 8, 0);
constexpr Score CorneredBishop = S( 50, 50);
constexpr Score FlankAttacks = S( 8, 0);
constexpr Score Hanging = S( 69, 36);
constexpr Score KingProtector = S( 7, 8);
constexpr Score KnightOnQueen = S( 16, 12);
@ -245,33 +245,37 @@ namespace {
constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH);
constexpr Bitboard LowRanks = (Us == WHITE ? Rank2BB | Rank3BB: Rank7BB | Rank6BB);
const Square ksq = pos.square<KING>(Us);
// Find our pawns that are blocked or on the first two ranks
Bitboard b = pos.pieces(Us, PAWN) & (shift<Down>(pos.pieces()) | LowRanks);
// Squares occupied by those pawns, by our king or queen, or controlled by enemy pawns
// are excluded from the mobility area.
// Squares occupied by those pawns, by our king or queen or controlled by
// enemy pawns are excluded from the mobility area.
mobilityArea[Us] = ~(b | pos.pieces(Us, KING, QUEEN) | pe->pawn_attacks(Them));
// Initialise attackedBy bitboards for kings and pawns
attackedBy[Us][KING] = pos.attacks_from<KING>(pos.square<KING>(Us));
// Initialize attackedBy[] for king and pawns
attackedBy[Us][KING] = pos.attacks_from<KING>(ksq);
attackedBy[Us][PAWN] = pe->pawn_attacks(Us);
attackedBy[Us][ALL_PIECES] = attackedBy[Us][KING] | attackedBy[Us][PAWN];
attackedBy2[Us] = attackedBy[Us][KING] & attackedBy[Us][PAWN];
// Init our king safety tables
kingRing[Us] = attackedBy[Us][KING];
if (relative_rank(Us, pos.square<KING>(Us)) == RANK_1)
if (relative_rank(Us, ksq) == RANK_1)
kingRing[Us] |= shift<Up>(kingRing[Us]);
if (file_of(pos.square<KING>(Us)) == FILE_H)
if (file_of(ksq) == FILE_H)
kingRing[Us] |= shift<WEST>(kingRing[Us]);
else if (file_of(pos.square<KING>(Us)) == FILE_A)
else if (file_of(ksq) == FILE_A)
kingRing[Us] |= shift<EAST>(kingRing[Us]);
kingAttackersCount[Them] = popcount(kingRing[Us] & pe->pawn_attacks(Them));
kingRing[Us] &= ~double_pawn_attacks_bb<Us>(pos.pieces(Us, PAWN));
kingAttacksCount[Them] = kingAttackersWeight[Them] = 0;
// Remove from kingRing[] the squares defended by two pawns
kingRing[Us] &= ~pawn_double_attacks_bb<Us>(pos.pieces(Us, PAWN));
}
@ -286,12 +290,11 @@ namespace {
const Square* pl = pos.squares<Pt>(Us);
Bitboard b, bb;
Square s;
Score score = SCORE_ZERO;
attackedBy[Us][Pt] = 0;
while ((s = *pl++) != SQ_NONE)
for (Square s = *pl; s != SQ_NONE; s = *++pl)
{
// Find attacked squares, including x-ray attacks for bishops and rooks
b = Pt == BISHOP ? attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(QUEEN))
@ -404,23 +407,12 @@ namespace {
constexpr Bitboard Camp = (Us == WHITE ? AllSquares ^ Rank6BB ^ Rank7BB ^ Rank8BB
: AllSquares ^ Rank1BB ^ Rank2BB ^ Rank3BB);
const Square ksq = pos.square<KING>(Us);
Bitboard kingFlank, weak, b, b1, b2, safe, unsafeChecks;
// King shelter and enemy pawns storm
Score score = pe->king_safety<Us>(pos);
// Find the squares that opponent attacks in our king flank, and the squares
// which are attacked twice in that flank.
kingFlank = KingFlank[file_of(ksq)];
b1 = attackedBy[Them][ALL_PIECES] & kingFlank & Camp;
b2 = b1 & attackedBy2[Them];
int tropism = popcount(b1) + popcount(b2);
// Main king safety evaluation
Bitboard weak, b, b1, b2, safe, unsafeChecks = 0;
int kingDanger = 0;
unsafeChecks = 0;
const Square ksq = pos.square<KING>(Us);
// Init the score with king shelter and enemy pawns storm
Score score = pe->king_safety<Us>(pos);
// Attacked squares defended at most once by our queen or king
weak = attackedBy[Them][ALL_PIECES]
@ -457,7 +449,7 @@ namespace {
// Enemy bishops checks: we count them only if they are from squares from
// which we can't give a queen check, because queen checks are more valuable.
Bitboard BishopCheck = b2
Bitboard BishopCheck = b2
& attackedBy[Them][BISHOP]
& safe
& ~QueenCheck;
@ -479,15 +471,22 @@ namespace {
// the square is in the attacker's mobility area.
unsafeChecks &= mobilityArea[Them];
// Find the squares that opponent attacks in our king flank, and the squares
// which are attacked twice in that flank.
b1 = attackedBy[Them][ALL_PIECES] & KingFlank[file_of(ksq)] & Camp;
b2 = b1 & attackedBy2[Them];
int kingFlankAttacks = popcount(b1) + popcount(b2);
kingDanger += kingAttackersCount[Them] * kingAttackersWeight[Them]
+ 69 * kingAttacksCount[Them]
+ 185 * popcount(kingRing[Us] & weak)
- 100 * bool(attackedBy[Us][KNIGHT] & attackedBy[Us][KING])
+ 150 * popcount(pos.blockers_for_king(Us) | unsafeChecks)
+ 5 * tropism * tropism / 16
- 873 * !pos.count<QUEEN>(Them)
- 6 * mg_value(score) / 8
+ mg_value(mobility[Them] - mobility[Us])
+ 5 * kingFlankAttacks * kingFlankAttacks / 16
- 25;
// Transform the kingDanger units into a Score, and subtract it from the evaluation
@ -495,11 +494,11 @@ namespace {
score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16);
// Penalty when our king is on a pawnless flank
if (!(pos.pieces(PAWN) & kingFlank))
if (!(pos.pieces(PAWN) & KingFlank[file_of(ksq)]))
score -= PawnlessFlank;
// King tropism bonus, to anticipate slow motion attacks on our king
score -= CloseEnemies * tropism;
// Penalty if king flank is under attack, potentially moving toward the king
score -= FlankAttacks * kingFlankAttacks;
if (T)
Trace::add(KING, Us, score);
@ -859,7 +858,7 @@ namespace {
v = mg_value(score) * int(me->game_phase())
+ eg_value(score) * int(PHASE_MIDGAME - me->game_phase()) * sf / SCALE_FACTOR_NORMAL;
v /= int(PHASE_MIDGAME);
v /= PHASE_MIDGAME;
// In case of tracing add all remaining individual evaluation terms
if (T)

View file

@ -76,9 +76,9 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHist
assert(d <= DEPTH_ZERO);
stage = pos.checkers() ? EVASION_TT : QSEARCH_TT;
ttMove = ttm
&& pos.pseudo_legal(ttm)
&& (depth > DEPTH_QS_RECAPTURES || to_sq(ttm) == recaptureSquare) ? ttm : MOVE_NONE;
ttMove = ttm
&& (depth > DEPTH_QS_RECAPTURES || to_sq(ttm) == recaptureSquare)
&& pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
stage += (ttMove == MOVE_NONE);
}
@ -91,8 +91,8 @@ MovePicker::MovePicker(const Position& p, Move ttm, Value th, const CapturePiece
stage = PROBCUT_TT;
ttMove = ttm
&& pos.pseudo_legal(ttm)
&& pos.capture(ttm)
&& pos.pseudo_legal(ttm)
&& pos.see_ge(ttm, threshold) ? ttm : MOVE_NONE;
stage += (ttMove == MOVE_NONE);
}

View file

@ -59,46 +59,46 @@ constexpr Score Bonus[][RANK_NB][int(FILE_NB) / 2] = {
{ S(-48,-51), S( -3,-40), S(-12,-39), S(-25,-20) }
},
{ // Rook
{ S(-24, -2), S(-13,-6), S( -7, -3), S( 2,-2) },
{ S(-18,-10), S(-10,-7), S( -5, 1), S( 9, 0) },
{ S(-21, 10), S( -7,-4), S( 3, 2), S(-1,-2) },
{ S(-13, -5), S( -5, 2), S( -4, -8), S(-6, 8) },
{ S(-24, -8), S(-12, 5), S( -1, 4), S( 6,-9) },
{ S(-24, 3), S( -4,-2), S( 4,-10), S(10, 7) },
{ S( -8, 1), S( 6, 2), S( 10, 17), S(12,-8) },
{ S(-22, 12), S(-24,-6), S( -6, 13), S( 4, 7) }
{ S(-24, -2), S(-13,-6), S(-7, -3), S( 2,-2) },
{ S(-18,-10), S(-10,-7), S(-5, 1), S( 9, 0) },
{ S(-21, 10), S( -7,-4), S( 3, 2), S(-1,-2) },
{ S(-13, -5), S( -5, 2), S(-4, -8), S(-6, 8) },
{ S(-24, -8), S(-12, 5), S(-1, 4), S( 6,-9) },
{ S(-24, 3), S( -4,-2), S( 4,-10), S(10, 7) },
{ S( -8, 1), S( 6, 2), S(10, 17), S(12,-8) },
{ S(-22, 12), S(-24,-6), S(-6, 13), S( 4, 7) }
},
{ // Queen
{ S( 3,-69), S(-5,-57), S(-5,-47), S( 4,-26) },
{ S( -3,-55), S( 5,-31), S( 8,-22), S(12, -4) },
{ S( -3,-39), S( 6,-18), S(13, -9), S( 7, 3) },
{ S( 4,-23), S( 5, -3), S( 9, 13), S( 8, 24) },
{ S( 0,-29), S(14, -6), S(12, 9), S( 5, 21) },
{ S( -4,-38), S(10,-18), S( 6,-12), S( 8, 1) },
{ S( -5,-50), S( 6,-27), S(10,-24), S( 8, -8) },
{ S( -2,-75), S(-2,-52), S( 1,-43), S(-2,-36) }
{ S( 3,-69), S(-5,-57), S(-5,-47), S( 4,-26) },
{ S(-3,-55), S( 5,-31), S( 8,-22), S(12, -4) },
{ S(-3,-39), S( 6,-18), S(13, -9), S( 7, 3) },
{ S( 4,-23), S( 5, -3), S( 9, 13), S( 8, 24) },
{ S( 0,-29), S(14, -6), S(12, 9), S( 5, 21) },
{ S(-4,-38), S(10,-18), S( 6,-12), S( 8, 1) },
{ S(-5,-50), S( 6,-27), S(10,-24), S( 8, -8) },
{ S(-2,-75), S(-2,-52), S( 1,-43), S(-2,-36) }
},
{ // King
{ S(272, 0), S(325, 41), S(273, 80), S(190, 93) },
{ S(277, 57), S(305, 98), S(241,138), S(183,131) },
{ S(198, 86), S(253,138), S(168,165), S(120,173) },
{ S(169,103), S(191,152), S(136,168), S(108,169) },
{ S(145, 98), S(176,166), S(112,197), S(69, 194) },
{ S(122, 87), S(159,164), S(85, 174), S(36, 189) },
{ S(87, 40), S(120, 99), S(64, 128), S(25, 141) },
{ S(64, 5), S(87, 60), S(49, 75), S(0, 75) }
{ S(145, 98), S(176,166), S(112,197), S( 69,194) },
{ S(122, 87), S(159,164), S( 85,174), S( 36,189) },
{ S( 87, 40), S(120, 99), S( 64,128), S( 25,141) },
{ S( 64, 5), S( 87, 60), S( 49, 75), S( 0, 75) }
}
};
constexpr Score PBonus[RANK_NB][FILE_NB] =
{ // Pawn (asymmetric distribution)
{ },
{ S( 0,-10), S( -5,-3), S( 10, 7), S( 13, -1), S( 21, 7), S( 17, 6), S( 6, 1), S( -3,-20) },
{ S(-11, -6), S(-10,-6), S( 15, -1), S( 22, -1), S( 26, -1), S( 28, 2), S( 4, -2), S(-24, -5) },
{ S( -9, 4), S(-18,-5), S( 8, -4), S( 22, -5), S( 33, -6), S( 25,-13), S( -4, -3), S(-16, -7) },
{ S( 6, 18), S( -3, 2), S(-10, 2), S( 1, -9), S( 12,-13), S( 6, -8), S(-12, 11), S( 1, 9) },
{ S( -6, 25), S( -8,17), S( 5, 19), S( 11, 29), S(-14, 29), S( 0, 8), S(-12, 4), S(-14, 12) },
{ S(-10, -1), S( 6,-6), S( -5, 18), S(-11, 22), S( -2, 22), S(-14, 17), S( 12, 2), S( -1, 9) }
{ S( 0,-10), S( -5,-3), S( 10, 7), S( 13,-1), S( 21, 7), S( 17, 6), S( 6, 1), S( -3,-20) },
{ S(-11, -6), S(-10,-6), S( 15,-1), S( 22,-1), S( 26, -1), S( 28, 2), S( 4,-2), S(-24, -5) },
{ S( -9, 4), S(-18,-5), S( 8,-4), S( 22,-5), S( 33, -6), S( 25,-13), S( -4,-3), S(-16, -7) },
{ S( 6, 18), S( -3, 2), S(-10, 2), S( 1,-9), S( 12,-13), S( 6, -8), S(-12,11), S( 1, 9) },
{ S( -6, 25), S( -8,17), S( 5,19), S( 11,29), S(-14, 29), S( 0, 8), S(-12, 4), S(-14, 12) },
{ S(-10, -1), S( 6,-6), S( -5,18), S(-11,22), S( -2, 22), S(-14, 17), S( 12, 2), S( -1, 9) }
};
#undef S

View file

@ -258,27 +258,23 @@ void MainThread::search() {
// Find out minimum score and reset votes for moves which can be voted
for (Thread* th: Threads)
{
minScore = std::min(minScore, th->rootMoves[0].score);
votes[th->rootMoves[0].pv[0]] = 0;
}
// Vote according to score and depth
auto square = [](int64_t x) { return x * x; };
for (Thread* th : Threads)
votes[th->rootMoves[0].pv[0]] += 200 + (square(th->rootMoves[0].score - minScore + 1)
* int64_t(th->completedDepth));
// Select best thread
int64_t bestVote = votes[this->rootMoves[0].pv[0]];
for (Thread* th : Threads)
{
int64_t s = th->rootMoves[0].score - minScore + 1;
votes[th->rootMoves[0].pv[0]] += 200 + s * s * int(th->completedDepth);
}
// Select best thread
auto bestVote = votes[this->rootMoves[0].pv[0]];
for (Thread* th : Threads)
if (votes[th->rootMoves[0].pv[0]] > bestVote)
{
bestVote = votes[th->rootMoves[0].pv[0]];
bestThread = th;
}
}
}
previousScore = bestThread->rootMoves[0].score;
@ -573,7 +569,7 @@ namespace {
Move ttMove, move, excludedMove, bestMove;
Depth extension, newDepth;
Value bestValue, value, ttValue, eval, maxValue, pureStaticEval;
bool ttHit, pvHit, inCheck, givesCheck, improving;
bool ttHit, ttPv, inCheck, givesCheck, improving;
bool captureOrPromotion, doFullDepthSearch, moveCountPruning, skipQuiets, ttCapture;
Piece movedPiece;
int moveCount, captureCount, quietCount;
@ -639,7 +635,7 @@ namespace {
ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
ttMove = rootNode ? thisThread->rootMoves[thisThread->pvIdx].pv[0]
: ttHit ? tte->move() : MOVE_NONE;
pvHit = (ttHit && tte->pv_hit()) || (PvNode && depth > 4 * ONE_PLY);
ttPv = (ttHit && tte->is_pv()) || (PvNode && depth > 4 * ONE_PLY);
// At non-PV nodes we check for an early TT cutoff
if ( !PvNode
@ -706,7 +702,7 @@ namespace {
if ( b == BOUND_EXACT
|| (b == BOUND_LOWER ? value >= beta : value <= alpha))
{
tte->save(posKey, value_to_tt(value, ss->ply), pvHit, b,
tte->save(posKey, value_to_tt(value, ss->ply), ttPv, b,
std::min(DEPTH_MAX - ONE_PLY, depth + 6 * ONE_PLY),
MOVE_NONE, VALUE_NONE);
@ -755,7 +751,7 @@ namespace {
else
ss->staticEval = eval = pureStaticEval = -(ss-1)->staticEval + 2 * Eval::Tempo;
tte->save(posKey, VALUE_NONE, pvHit, BOUND_NONE, DEPTH_NONE, MOVE_NONE, pureStaticEval);
tte->save(posKey, VALUE_NONE, ttPv, BOUND_NONE, DEPTH_NONE, MOVE_NONE, pureStaticEval);
}
// Step 7. Razoring (~2 Elo)
@ -1032,7 +1028,7 @@ moves_loop: // When in check, search starts from here
Depth r = reduction<PvNode>(improving, depth, moveCount);
// Decrease reduction if position is or has been on the PV
if (pvHit)
if (ttPv)
r -= ONE_PLY;
// Decrease reduction if opponent's move count is high (~10 Elo)
@ -1213,7 +1209,7 @@ moves_loop: // When in check, search starts from here
bestValue = std::min(bestValue, maxValue);
if (!excludedMove)
tte->save(posKey, value_to_tt(bestValue, ss->ply), pvHit,
tte->save(posKey, value_to_tt(bestValue, ss->ply), ttPv,
bestValue >= beta ? BOUND_LOWER :
PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER,
depth, bestMove, pureStaticEval);
@ -1277,7 +1273,7 @@ moves_loop: // When in check, search starts from here
tte = TT.probe(posKey, ttHit);
ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
ttMove = ttHit ? tte->move() : MOVE_NONE;
pvHit = ttHit && tte->pv_hit();
pvHit = ttHit && tte->is_pv();
if ( !PvNode
&& ttHit

View file

@ -30,8 +30,10 @@
TranspositionTable TT; // Our global transposition table
/// TTEntry::save saves a TTEntry
void TTEntry::save(Key k, Value v, bool PvNode, Bound b, Depth d, Move m, Value ev) {
/// TTEntry::save populates the TTEntry with a new node's data, possibly
/// overwriting an old position. Update is not atomic and can be racy.
void TTEntry::save(Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev) {
assert(d / ONE_PLY * ONE_PLY == d);
@ -47,7 +49,7 @@ void TTEntry::save(Key k, Value v, bool PvNode, Bound b, Depth d, Move m, Value
key16 = (uint16_t)(k >> 48);
value16 = (int16_t)v;
eval16 = (int16_t)ev;
genBound8 = (uint8_t)(TT.generation8 | PvNode << 2 | b);
genBound8 = (uint8_t)(TT.generation8 | uint8_t(pv) << 2 | b);
depth8 = (int8_t)(d / ONE_PLY);
}
}

View file

@ -31,7 +31,7 @@
/// value 16 bit
/// eval value 16 bit
/// generation 5 bit
/// PvNode 1 bit
/// pv node 1 bit
/// bound type 2 bit
/// depth 8 bit
@ -41,9 +41,9 @@ struct TTEntry {
Value value() const { return (Value)value16; }
Value eval() const { return (Value)eval16; }
Depth depth() const { return (Depth)(depth8 * int(ONE_PLY)); }
bool pv_hit() const { return (bool)(genBound8 & 0x4); }
bool is_pv() const { return (bool)(genBound8 & 0x4); }
Bound bound() const { return (Bound)(genBound8 & 0x3); }
void save(Key k, Value v, bool PvNode, Bound b, Depth d, Move m, Value ev);
void save(Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev);
private:
friend class TranspositionTable;

View file

@ -141,9 +141,11 @@ enum CastlingRight {
WHITE_OOO = WHITE_OO << 1,
BLACK_OO = WHITE_OO << 2,
BLACK_OOO = WHITE_OO << 3,
WHITE_CASTLING = WHITE_OO | WHITE_OOO,
BLACK_CASTLING = BLACK_OO | BLACK_OOO,
ANY_CASTLING = WHITE_CASTLING | BLACK_CASTLING,
ANY_CASTLING = WHITE_CASTLING | BLACK_CASTLING,
CASTLING_RIGHT_NB = 16
};

View file

@ -45,7 +45,7 @@ race:TTEntry::bound
race:TTEntry::save
race:TTEntry::value
race:TTEntry::eval
race:TTEntry::pv_hit
race:TTEntry::is_pv
race:TranspositionTable::probe
race:TranspositionTable::hashfull