+ // No pawns makes it difficult to win, even with a material advantage
+ if (pos.piece_count(WHITE, PAWN) == 0 && npm_w - npm_b <= BishopValueMg)
+ {
+ e->factor[WHITE] = (uint8_t)
+ (npm_w == npm_b || npm_w < RookValueMg ? 0 : NoPawnsSF[std::min(pos.piece_count(WHITE, BISHOP), 2)]);
+ }
+
+ if (pos.piece_count(BLACK, PAWN) == 0 && npm_b - npm_w <= BishopValueMg)
+ {
+ e->factor[BLACK] = (uint8_t)
+ (npm_w == npm_b || npm_b < RookValueMg ? 0 : NoPawnsSF[std::min(pos.piece_count(BLACK, BISHOP), 2)]);
+ }
+
+ // Compute the space weight
+ if (npm_w + npm_b >= 2 * QueenValueMg + 4 * RookValueMg + 2 * KnightValueMg)
+ {
+ int minorPieceCount = pos.piece_count(WHITE, KNIGHT) + pos.piece_count(WHITE, BISHOP)
+ + pos.piece_count(BLACK, KNIGHT) + pos.piece_count(BLACK, BISHOP);
+
+ e->spaceWeight = minorPieceCount * minorPieceCount;
+ }
+
+ // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
+ // for the bishop pair "extended piece", this allow us to be more flexible
+ // in defining bishop pair bonuses.
+ const int pieceCount[2][8] = {
+ { pos.piece_count(WHITE, BISHOP) > 1, pos.piece_count(WHITE, PAWN), pos.piece_count(WHITE, KNIGHT),
+ pos.piece_count(WHITE, BISHOP) , pos.piece_count(WHITE, ROOK), pos.piece_count(WHITE, QUEEN) },
+ { pos.piece_count(BLACK, BISHOP) > 1, pos.piece_count(BLACK, PAWN), pos.piece_count(BLACK, KNIGHT),
+ pos.piece_count(BLACK, BISHOP) , pos.piece_count(BLACK, ROOK), pos.piece_count(BLACK, QUEEN) } };
+
+ e->value = (int16_t)((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
+ return e;
+}
+
+
+/// MaterialTable::imbalance() calculates imbalance comparing piece count of each
+/// piece type for both colors.
+
+template<Color Us>
+int MaterialTable::imbalance(const int pieceCount[][8]) {
+
+ const Color Them = (Us == WHITE ? BLACK : WHITE);