+ // evaluate_space() computes the space evaluation for a given side. The
+ // space evaluation is a simple bonus based on the number of safe squares
+ // available for minor pieces on the central four files on ranks 2--4. Safe
+ // squares one, two or three squares behind a friendly pawn are counted
+ // twice. Finally, the space bonus is scaled by a weight taken from the
+ // material hash table.
+
+ void evaluate_space(const Position &pos, Color us, EvalInfo &ei) {
+
+ Color them = opposite_color(us);
+
+ // Find the safe squares for our pieces inside the area defined by
+ // SpaceMask[us]. A square is unsafe it is attacked by an enemy
+ // pawn, or if it is undefended and attacked by an enemy piece.
+
+ Bitboard safeSquares =
+ SpaceMask[us] & ~pos.pawns(us) & ~ei.attacked_by(them, PAWN)
+ & ~(~ei.attacked_by(us) & ei.attacked_by(them));
+
+ // Find all squares which are at most three squares behind some friendly
+ // pawn.
+ Bitboard behindFriendlyPawns = pos.pawns(us);
+ if(us == WHITE) {
+ behindFriendlyPawns |= (behindFriendlyPawns >> 8);
+ behindFriendlyPawns |= (behindFriendlyPawns >> 16);
+ }
+ else {
+ behindFriendlyPawns |= (behindFriendlyPawns << 8);
+ behindFriendlyPawns |= (behindFriendlyPawns << 16);
+ }
+
+ int space =
+ count_1s_max_15(safeSquares)
+ + count_1s_max_15(behindFriendlyPawns & safeSquares);
+
+ ei.mgValue += Sign[us] *
+ apply_weight(Value(space * ei.mi->space_weight()), WeightSpace);
+ }
+
+
+ // apply_weight() applies an evaluation weight to a value