X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fevaluate.cpp;h=0bf64631c9d4a8686554619b43eb813c4c94de91;hp=a5868f504b0b827af8e7320b780f57e6a2c2f656;hb=cff9ff21985edcf1d15e7df6c0e0039f550706ad;hpb=fa0bffeafaa3554cb914249b4d032526e543359a diff --git a/src/evaluate.cpp b/src/evaluate.cpp index a5868f50..0bf64631 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -207,8 +207,8 @@ namespace { ((1ULL << SQ_A8) | (1ULL << SQ_H8)) }; - // The SpaceMask[color] contains area of the board which is consdered by - // the space evaluation. In the middle game, each side is given a bonus + // The SpaceMask[color] contains the area of the board which is consdered + // by the space evaluation. In the middle game, each side is given a bonus // based on how many squares inside this area are safe and available for // friendly minor pieces. const Bitboard SpaceMask[2] = { @@ -344,8 +344,8 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) { ei.kingZone[BLACK] = ei.attackedBy[WHITE][KING] | (ei.attackedBy[WHITE][KING] << 8); // Initialize pawn attack bitboards for both sides - ei.attackedBy[WHITE][PAWN] = ((pos.pieces(PAWN, WHITE) << 9) & ~FileABB) | ((pos.pieces(PAWN, WHITE) << 7) & ~FileHBB); - ei.attackedBy[BLACK][PAWN] = ((pos.pieces(PAWN, BLACK) >> 7) & ~FileABB) | ((pos.pieces(PAWN, BLACK) >> 9) & ~FileHBB); + ei.attackedBy[WHITE][PAWN] = ei.pi->pawn_attacks(WHITE); + ei.attackedBy[BLACK][PAWN] = ei.pi->pawn_attacks(BLACK); Bitboard b1 = ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING]; Bitboard b2 = ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING]; if (b1) @@ -529,31 +529,43 @@ namespace { // evaluate_mobility() computes mobility and attacks for every piece template - int evaluate_mobility(const Position& pos, const Bitboard& mob_bb, EvalInfo& ei) { + int evaluate_mobility(const Position& pos, Bitboard b, EvalInfo& ei) { const Color Them = (Us == WHITE ? BLACK : WHITE); static const int AttackWeight[] = { 0, 0, KnightAttackWeight, BishopAttackWeight, RookAttackWeight, QueenAttackWeight }; static const Value* MgBonus[] = { 0, 0, MidgameKnightMobilityBonus, MidgameBishopMobilityBonus, MidgameRookMobilityBonus, MidgameQueenMobilityBonus }; static const Value* EgBonus[] = { 0, 0, EndgameKnightMobilityBonus, EndgameBishopMobilityBonus, EndgameRookMobilityBonus, EndgameQueenMobilityBonus }; + static const int lastIndex[] = { 0, 0, 8, 15, 15, 31 }; // Update attack info - ei.attackedBy[Us][Piece] |= mob_bb; + ei.attackedBy[Us][Piece] |= b; // King attacks - if (mob_bb & ei.kingZone[Us]) + if (b & ei.kingZone[Us]) { ei.kingAttackersCount[Us]++; ei.kingAttackersWeight[Us] += AttackWeight[Piece]; - Bitboard b = (mob_bb & ei.attackedBy[Them][KING]); - if (b) - ei.kingAdjacentZoneAttacksCount[Us] += count_1s_max_15(b); + Bitboard bb = (b & ei.attackedBy[Them][KING]); + if (bb) + ei.kingAdjacentZoneAttacksCount[Us] += count_1s_max_15(bb); } + // The squares occupied by enemy pieces will be counted two times instead + // of one. The shift (almost) guarantees that intersection with b is zero + // so when we 'or' the two bitboards togheter and count we get the correct + // sum of '1' in b and attacked bitboards. + Bitboard attacked = Us == WHITE ? ((b & pos.pieces_of_color(Them)) >> 1) + : ((b & pos.pieces_of_color(Them)) << 1); + // Remove squares protected by enemy pawns or occupied by our pieces - Bitboard b = mob_bb & ~ei.attackedBy[Them][PAWN] & ~pos.pieces_of_color(Us); + b &= ~(ei.attackedBy[Them][PAWN] | pos.pieces_of_color(Us)); // Mobility - int mob = (Piece != QUEEN ? count_1s_max_15(b) : count_1s(b)); + int mob = (Piece != QUEEN ? count_1s_max_15(b | attacked) + : count_1s(b | attacked)); + + if (mob > lastIndex[Piece]) + mob = lastIndex[Piece]; ei.mgMobility += Sign[Us] * MgBonus[Piece][mob]; ei.egMobility += Sign[Us] * EgBonus[Piece][mob]; @@ -592,7 +604,7 @@ namespace { template void evaluate_pieces(const Position& pos, EvalInfo& ei) { - Bitboard mob_bb; + Bitboard b; Square s, ksq; int mob; File f; @@ -603,16 +615,16 @@ namespace { while ((s = *ptr++) != SQ_NONE) { if (Piece == KNIGHT || Piece == QUEEN) - mob_bb = pos.attacks_from(s); + b = pos.attacks_from(s); else if (Piece == BISHOP) - mob_bb = bishop_attacks_bb(s, pos.occupied_squares() & ~pos.pieces(QUEEN, Us)); + b = bishop_attacks_bb(s, pos.occupied_squares() & ~pos.pieces(QUEEN, Us)); else if (Piece == ROOK) - mob_bb = rook_attacks_bb(s, pos.occupied_squares() & ~pos.pieces(ROOK, QUEEN, Us)); + b = rook_attacks_bb(s, pos.occupied_squares() & ~pos.pieces(ROOK, QUEEN, Us)); else assert(false); // Attacks and mobility - mob = evaluate_mobility(pos, mob_bb, ei); + mob = evaluate_mobility(pos, b, ei); // Bishop and knight outposts squares if ((Piece == BISHOP || Piece == KNIGHT) && pos.square_is_weak(s, Them))