]> git.sesse.net Git - stockfish/commitdiff
Simplify BlockedByKing in pawn storms
authorprotonspring <mike@whiteley.org>
Wed, 18 Apr 2018 18:03:37 +0000 (20:03 +0200)
committerStéphane Nicolet <cassio@free.fr>
Wed, 18 Apr 2018 18:03:52 +0000 (20:03 +0200)
This patch is non-functional. Current master does four operations to determine
whether an enemy pawn on this file is blocked by the king or not

```
f == file_of(ksq) && rkThem == relative_rank(Us, ksq) + 1 )
```

By adding a direction (based on the template color), this is reduced to two
operations. This works because b is limited to enemy pawns that are ahead of
the king and on the current file.

```
shift<Down>(b) & ksq
```

I've added a line of code, but the number of executing instructions is reduced
(I think). I'm not sure if this counts as a simplification, but it should
theoretically be a little faster (barely). The code line length is also reduced
making it a little easier to read.

Closes https://github.com/official-stockfish/Stockfish/pull/1552

No functional change.

src/pawns.cpp

index 7b3dd8181ef9fc602b6868a4eba4a1d1a81bef77..d2ef33e61e499cef4a466d91a1d076de7d36830d 100644 (file)
@@ -236,7 +236,8 @@ Entry* probe(const Position& pos) {
 template<Color Us>
 Value Entry::shelter_storm(const Position& pos, Square ksq) {
 
 template<Color Us>
 Value Entry::shelter_storm(const Position& pos, Square ksq) {
 
-  constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
+  constexpr Color     Them = (Us == WHITE ? BLACK : WHITE);
+  constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH);
 
   enum { BlockedByKing, Unopposed, BlockedByPawn, Unblocked };
 
 
   enum { BlockedByKing, Unopposed, BlockedByPawn, Unblocked };
 
@@ -257,9 +258,9 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) {
       int d = std::min(f, ~f);
       safety -=  ShelterWeakness[f == file_of(ksq)][d][rkUs]
                + StormDanger
       int d = std::min(f, ~f);
       safety -=  ShelterWeakness[f == file_of(ksq)][d][rkUs]
                + StormDanger
-                 [f == file_of(ksq) && rkThem == relative_rank(Us, ksq) + 1 ? BlockedByKing  :
-                  rkUs   == RANK_1                                          ? Unopposed :
-                  rkThem == rkUs + 1                                        ? BlockedByPawn  : Unblocked]
+                 [(shift<Down>(b) & ksq) ? BlockedByKing :
+                  rkUs   == RANK_1       ? Unopposed     :
+                  rkThem == (rkUs + 1)   ? BlockedByPawn : Unblocked]
                  [d][rkThem];
   }
 
                  [d][rkThem];
   }