shubhxho commited on
Commit
223490f
·
verified ·
1 Parent(s): 989e42a

Sync reference/net.rs: king-attacker count kept in a register

Browse files
Files changed (1) hide show
  1. reference/net.rs +13 -6
reference/net.rs CHANGED
@@ -197,10 +197,11 @@ pub fn features_both(pos: &Position, persp: usize, a: &mut [u16; MAX_F], b: &mut
197
  // filled in during the mobility walk and emitted after both colours are
198
  // done, because the pieces that bear on white's king are black's, and they
199
  // are not seen until the second pass.
200
- let zone = [
201
- king_attacks(pos.king_sq(WHITE)) | bit(pos.king_sq(WHITE)),
202
- king_attacks(pos.king_sq(BLACK)) | bit(pos.king_sq(BLACK)),
203
- ];
 
204
  let mut attackers = [0usize; 2];
205
 
206
  for c in 0..2 {
@@ -211,6 +212,10 @@ pub fn features_both(pos: &Position, persp: usize, a: &mut [u16; MAX_F], b: &mut
211
  let them = c ^ 1;
212
  let our_pawns = pos.pieces(c, PAWN_P);
213
  let their_pawns = pos.pieces(them, PAWN_P);
 
 
 
 
214
 
215
  // A fact whose index depends only on relative colour.
216
  macro_rules! put {
@@ -250,11 +255,13 @@ pub fn features_both(pos: &Position, persp: usize, a: &mut [u16; MAX_F], b: &mut
250
  };
251
  let m = popcount(att & !pos.color[c]) as usize;
252
  put!(MOB, 4 * 12, (pt - 1) * 12 + m.min(11));
253
- if att & zone[them] != 0 {
254
- attackers[them] += 1;
255
  }
256
  }
257
  }
 
 
258
 
259
  // --- pawn structure, asked of the whole board instead of pawn by pawn
260
  //
 
197
  // filled in during the mobility walk and emitted after both colours are
198
  // done, because the pieces that bear on white's king are black's, and they
199
  // are not seen until the second pass.
200
+ // The king bitboard already *is* `bit(king_sq)`, so reuse it rather than
201
+ // recovering a square from it and shifting a one back up.
202
+ let wk = pos.piece[KING_P] & pos.color[WHITE];
203
+ let bk = pos.piece[KING_P] & pos.color[BLACK];
204
+ let zone = [king_attacks(lsb(wk)) | wk, king_attacks(lsb(bk)) | bk];
205
  let mut attackers = [0usize; 2];
206
 
207
  for c in 0..2 {
 
212
  let them = c ^ 1;
213
  let our_pawns = pos.pieces(c, PAWN_P);
214
  let their_pawns = pos.pieces(them, PAWN_P);
215
+ // Hoisted out of the mobility walk: a stack array indexed by a value
216
+ // the compiler cannot fold sits in memory unless the loop is unrolled.
217
+ let their_zone = zone[them];
218
+ let mut their_attackers = 0usize;
219
 
220
  // A fact whose index depends only on relative colour.
221
  macro_rules! put {
 
255
  };
256
  let m = popcount(att & !pos.color[c]) as usize;
257
  put!(MOB, 4 * 12, (pt - 1) * 12 + m.min(11));
258
+ if att & their_zone != 0 {
259
+ their_attackers += 1;
260
  }
261
  }
262
  }
263
+ // Each entry is written by exactly one pass, since `them` is `c ^ 1`.
264
+ attackers[them] = their_attackers;
265
 
266
  // --- pawn structure, asked of the whole board instead of pawn by pawn
267
  //