gigachess 0.4.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +94 -39
  2. package/dist/attacks.d.ts +12 -4
  3. package/dist/attacks.d.ts.map +1 -1
  4. package/dist/attacks.js +161 -74
  5. package/dist/bishopSplit32Blob.d.ts +2 -0
  6. package/dist/bishopSplit32Blob.d.ts.map +1 -0
  7. package/dist/bishopSplit32Blob.js +2 -0
  8. package/dist/board.d.ts +27 -1
  9. package/dist/board.d.ts.map +1 -1
  10. package/dist/board.js +351 -36
  11. package/dist/castling.d.ts +2 -0
  12. package/dist/castling.d.ts.map +1 -1
  13. package/dist/castling.js +27 -12
  14. package/dist/chess.d.ts +11 -4
  15. package/dist/chess.d.ts.map +1 -1
  16. package/dist/chess.js +521 -137
  17. package/dist/chesstree.d.ts +9 -0
  18. package/dist/chesstree.d.ts.map +1 -1
  19. package/dist/chesstree.js +85 -7
  20. package/dist/core.d.ts +1 -1
  21. package/dist/core.d.ts.map +1 -1
  22. package/dist/core.js +1 -1
  23. package/dist/fast/bishopSplit32Blob-NOHWZEWR.js +1 -0
  24. package/dist/fast/index.js +1 -0
  25. package/dist/fast/rookSplit32Blob-C3ANUNTJ.js +1 -0
  26. package/dist/fast/zobristBlob-HIMMLZLJ.js +1 -0
  27. package/dist/fast.d.ts +12 -0
  28. package/dist/fast.d.ts.map +1 -0
  29. package/dist/fast.js +11 -0
  30. package/dist/fen.d.ts.map +1 -1
  31. package/dist/fen.js +139 -124
  32. package/dist/index.d.ts +2 -2
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +2 -2
  35. package/dist/perftCompact.d.ts +3 -0
  36. package/dist/perftCompact.d.ts.map +1 -0
  37. package/dist/perftCompact.js +358 -0
  38. package/dist/pgn.d.ts +4 -0
  39. package/dist/pgn.d.ts.map +1 -1
  40. package/dist/pgn.js +7 -2
  41. package/dist/rookSplit32Blob.d.ts +2 -0
  42. package/dist/rookSplit32Blob.d.ts.map +1 -0
  43. package/dist/rookSplit32Blob.js +2 -0
  44. package/dist/san.d.ts.map +1 -1
  45. package/dist/san.js +14 -40
  46. package/dist/squareSet.d.ts +2 -0
  47. package/dist/squareSet.d.ts.map +1 -1
  48. package/dist/squareSet.js +6 -1
  49. package/dist/standardMovegen.d.ts +3 -0
  50. package/dist/standardMovegen.d.ts.map +1 -0
  51. package/dist/standardMovegen.js +253 -0
  52. package/dist/zobrist.d.ts.map +1 -1
  53. package/dist/zobrist.js +17 -15
  54. package/package.json +15 -6
  55. package/dist/bishopMagicBlob.d.ts +0 -2
  56. package/dist/bishopMagicBlob.d.ts.map +0 -1
  57. package/dist/bishopMagicBlob.js +0 -11
  58. package/dist/rookMagicBlob.d.ts +0 -2
  59. package/dist/rookMagicBlob.d.ts.map +0 -1
  60. package/dist/rookMagicBlob.js +0 -11
package/dist/chess.js CHANGED
@@ -1,5 +1,3 @@
1
- // src/chess.ts — core chess rules, Position/Setup, dests, isLegal, isCheck, perft etc
2
- // MIT gigachess, clean-room from specs + FIDE notes (no G P L)
3
1
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
4
2
  if (kind === "m") throw new TypeError("Private method is not writable");
5
3
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -12,13 +10,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
12
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
13
11
  };
14
12
  var _Chess_instances, _Chess_board, _Chess_startFen, _Chess_history, _Chess_movetext, _Chess_positions, _Chess_describe, _Chess_fenOf;
13
+ import { standardMovesInto } from "./standardMovegen.js";
14
+ // src/chess.ts — core chess rules, Position/Setup, dests, isLegal, isCheck, perft etc
15
+ // MIT gigachess, clean-room from specs + FIDE notes (no G P L)
15
16
  import * as sq from "./squareSet.js";
16
17
  import * as board from "./board.js";
17
18
  import { Board } from "./board.js";
18
19
  import * as attacks from "./attacks.js";
19
20
  import { Color, Role } from "./types.js";
20
21
  import { opposite, squareFile, squareRank, parseSquare, squareName } from "./util.js";
21
- import { zobristTablesLoaded, zobristAfterMove, zobristAfterNullMove } from "./zobrist.js";
22
+ import { zobristTablesLoaded, zobristAfterMove, zobristAfterNullMove, calculateZobrist, epIsHashable, keyLo, keyHi, IDX_EP } from "./zobrist.js";
22
23
  import { parseFen, makeFen } from "./fen.js";
23
24
  import { parseSan, makeSan, parseUci, makeUci } from "./san.js";
24
25
  import { packOf, packedToMoves, isNullMove, PROMO_NONE, PROMO_KNIGHT, PROMO_BISHOP, PROMO_ROOK, PROMO_QUEEN, } from "./packedMove.js";
@@ -138,11 +139,31 @@ export function isFivefoldRepetition(history) {
138
139
  count++;
139
140
  return count >= 5;
140
141
  }
142
+ /**
143
+ * FIDE repetition key, matching gigachess-rs `Board::repetition_key()`.
144
+ * Polyglot Zobrist hashing deliberately remains pseudo-legal EP-based;
145
+ * this key excludes the EP contribution when there is no LEGAL EP capture.
146
+ * Requires `ensureZobristLoaded()` to have completed, like other key APIs.
147
+ */
148
+ export function repetitionKey(pos) {
149
+ if (!zobristTablesLoaded() || !keyLo || !keyHi) {
150
+ throw new Error("zobrist tables not loaded — call ensureZobristLoaded() first");
151
+ }
152
+ // Match Rust's cached-hash fast path for stateful Boards, while still
153
+ // supporting immutable parsed Setups without incremental hash fields.
154
+ const key = pos.zobristLo !== undefined && pos.zobristHi !== undefined
155
+ ? { lo: pos.zobristLo, hi: pos.zobristHi }
156
+ : calculateZobrist(pos);
157
+ const ep = pos.epSquare;
158
+ if (ep === null || !epIsHashable(pos, ep, pos.turn) || legalRepetitionEp(pos) !== null) {
159
+ return key;
160
+ }
161
+ const idx = IDX_EP + (ep & 7);
162
+ return { lo: (key.lo ^ keyLo[idx]) >>> 0, hi: (key.hi ^ keyHi[idx]) >>> 0 };
163
+ }
141
164
  function positionsEqualForRepetition(a, b) {
142
165
  if (a.turn !== b.turn)
143
166
  return false;
144
- if (a.epSquare !== b.epSquare)
145
- return false;
146
167
  if (!board.boardEquals(a.board, b.board))
147
168
  return false;
148
169
  // castling sets equality
@@ -156,7 +177,25 @@ function positionsEqualForRepetition(a, b) {
156
177
  for (const s of a.castling.black)
157
178
  if (!b.castling.black.has(s))
158
179
  return false;
159
- return true;
180
+ // FIDE repetition requires identical LEGAL moves. Polyglot's hash instead
181
+ // includes a geometrically capturable EP file, and must stay unchanged.
182
+ return a.epSquare === b.epSquare || legalRepetitionEp(a) === legalRepetitionEp(b);
183
+ }
184
+ function legalRepetitionEp(pos) {
185
+ const to = pos.epSquare;
186
+ if (to === null)
187
+ return null;
188
+ const white = pos.turn === Color.White;
189
+ // A white pawn captures from ep-7/ep-9; a black pawn from ep+7/ep+9.
190
+ const candidates = white ? [to - 9, to - 7] : [to + 7, to + 9];
191
+ for (const from of candidates) {
192
+ if (from < 0 || from >= 64 || Math.abs((from & 7) - (to & 7)) !== 1)
193
+ continue;
194
+ if (sq.has(pos.board.pawn, from) && sq.has(white ? pos.board.white : pos.board.black, from)
195
+ && isLegal(pos, { from, to }))
196
+ return to;
197
+ }
198
+ return null;
160
199
  }
161
200
  // ---------- castling: one detect/apply path (ADR-013 as amended) ----------
162
201
  // ADR-013 bake-off result (change purechess-gates-green, task 1.1): the
@@ -702,88 +741,145 @@ isPromotion, promotion) {
702
741
  return false;
703
742
  return !attacks.isAttacked(destScratch, ksq, opposite(pos.turn));
704
743
  }
705
- // Reused castling plan scratch (same ownership discipline): avoids allocating
706
- // two plan objects per analyzed position in the perft/movegen hot loop. The
707
- // referenced position data is read-only and consumed within the same call.
708
- const scratchCastlingPlans = [];
709
- const scratchCastlingPlanA = { side: "king", kingFrom: 0, kingTo: 0, rookFrom: 0, rookTo: 0 };
710
- const scratchCastlingPlanB = { side: "king", kingFrom: 0, kingTo: 0, rookFrom: 0, rookTo: 0 };
711
- export function analyzeCheckContext(pos) {
744
+ /**
745
+ * Per-position, caller-owned reusable analysis context. A global scratch
746
+ * castling-plan array is unsafe: a nested analysis or retained CheckContext
747
+ * silently changed rights for an earlier position, especially in Chess960.
748
+ * A caller may pass its OWN context to reuse allocations (e.g. perft ply),
749
+ * never a shared global singleton.
750
+ */
751
+ // Precompute empty-board rank/file/diagonal king rays once. If no enemy
752
+ // slider shares a line with the king, there cannot be a pin; skip expensive
753
+ // magic-indexed sniper queries entirely.
754
+ const ROOK_KING_RAYS = Array.from({ length: 64 }, (_, i) => attacks.rookAttacksForTest(i, sq.EMPTY));
755
+ const BISHOP_KING_RAYS = Array.from({ length: 64 }, (_, i) => attacks.bishopAttacksForTest(i, sq.EMPTY));
756
+ export function analyzeCheckContext(pos, reuse, perftOnly = false) {
757
+ return analyzeCheckContextInternal(pos, reuse, perftOnly, false);
758
+ }
759
+ function analyzeCheckContextInternal(pos, reuse, perftOnly = false, nonKingOnly = false) {
712
760
  const us = pos.turn;
713
761
  const them = opposite(us);
714
762
  const ksq = board.kingSquare(pos.board, us);
715
- if (ksq === undefined) {
716
- // degenerate (kingless) position: no pins, no masks
717
- return { us, ksq: -1, doubleCheck: false, checkMask: sq.FULL, kingSafe: sq.EMPTY, pinned: sq.EMPTY, castlingPlans: [] };
718
- }
719
- const ctx = { us, ksq, doubleCheck: false, checkMask: sq.FULL, kingSafe: sq.EMPTY, pinned: sq.EMPTY, castlingPlans: [] };
763
+ const ctx = reuse ?? {
764
+ us, ksq: -1, doubleCheck: false, checkMask: sq.FULL,
765
+ kingSafe: sq.EMPTY, pinned: sq.EMPTY, castlingPlans: [],
766
+ };
767
+ ctx.us = us;
768
+ ctx.ksq = ksq ?? -1;
769
+ ctx.doubleCheck = false;
770
+ ctx.checkMask = sq.FULL;
771
+ ctx.kingSafe = sq.EMPTY;
772
+ ctx.pinned = sq.EMPTY;
773
+ ctx.castlingPlans.length = 0;
774
+ if (ksq === undefined)
775
+ return ctx;
720
776
  // Precompute the position's castling plans once (design D2): the hot loops
721
777
  // (destsFast king branch, genLegalMoves) look them up instead of re-running
722
- // the full detectCastling per king move. Plans are written into the reused
723
- // scratch array — no allocation in the hot loop.
724
- ctx.castlingPlans = scratchCastlingPlans;
725
- ctx.castlingPlans.length = 0;
726
- if (!pos.isChess960) {
778
+ // detectCastling per king move. The plans belong to THIS context only.
779
+ if (!nonKingOnly && !pos.isChess960) {
727
780
  const mask = pos.castlingMask ?? getCastlingMask(pos);
728
781
  if (us === Color.White) {
729
782
  if ((mask & CASTLE_WK) !== 0 && ksq === 4) {
730
- const rp = board.pieceAt(pos.board, 7);
731
- if (rp && rp.color === Color.White && rp.role === Role.Rook)
732
- ctx.castlingPlans.push(PLAN_WHITE_K);
783
+ if (perftOnly) {
784
+ if ((pos.board.occupied.lo & 0x60) === 0 &&
785
+ (pos.board.white.lo & pos.board.rook.lo & 0x80) !== 0)
786
+ ctx.castlingPlans.push(PLAN_WHITE_K);
787
+ }
788
+ else {
789
+ const rp = board.pieceAt(pos.board, 7);
790
+ if (rp && rp.color === Color.White && rp.role === Role.Rook)
791
+ ctx.castlingPlans.push(PLAN_WHITE_K);
792
+ }
733
793
  }
734
794
  if ((mask & CASTLE_WQ) !== 0 && ksq === 4) {
735
- const rp = board.pieceAt(pos.board, 0);
736
- if (rp && rp.color === Color.White && rp.role === Role.Rook)
737
- ctx.castlingPlans.push(PLAN_WHITE_Q);
795
+ if (perftOnly) {
796
+ if ((pos.board.occupied.lo & 0x0e) === 0 &&
797
+ (pos.board.white.lo & pos.board.rook.lo & 0x01) !== 0)
798
+ ctx.castlingPlans.push(PLAN_WHITE_Q);
799
+ }
800
+ else {
801
+ const rp = board.pieceAt(pos.board, 0);
802
+ if (rp && rp.color === Color.White && rp.role === Role.Rook)
803
+ ctx.castlingPlans.push(PLAN_WHITE_Q);
804
+ }
738
805
  }
739
806
  }
740
807
  else {
741
808
  if ((mask & CASTLE_BK) !== 0 && ksq === 60) {
742
- const rp = board.pieceAt(pos.board, 63);
743
- if (rp && rp.color === Color.Black && rp.role === Role.Rook)
744
- ctx.castlingPlans.push(PLAN_BLACK_K);
809
+ if (perftOnly) {
810
+ if ((pos.board.occupied.hi & 0x60000000) === 0 &&
811
+ (pos.board.black.hi & pos.board.rook.hi & 0x80000000) !== 0)
812
+ ctx.castlingPlans.push(PLAN_BLACK_K);
813
+ }
814
+ else {
815
+ const rp = board.pieceAt(pos.board, 63);
816
+ if (rp && rp.color === Color.Black && rp.role === Role.Rook)
817
+ ctx.castlingPlans.push(PLAN_BLACK_K);
818
+ }
745
819
  }
746
820
  if ((mask & CASTLE_BQ) !== 0 && ksq === 60) {
747
- const rp = board.pieceAt(pos.board, 56);
748
- if (rp && rp.color === Color.Black && rp.role === Role.Rook)
749
- ctx.castlingPlans.push(PLAN_BLACK_Q);
821
+ if (perftOnly) {
822
+ if ((pos.board.occupied.hi & 0x0e000000) === 0 &&
823
+ (pos.board.black.hi & pos.board.rook.hi & 0x01000000) !== 0)
824
+ ctx.castlingPlans.push(PLAN_BLACK_Q);
825
+ }
826
+ else {
827
+ const rp = board.pieceAt(pos.board, 56);
828
+ if (rp && rp.color === Color.Black && rp.role === Role.Rook)
829
+ ctx.castlingPlans.push(PLAN_BLACK_Q);
830
+ }
750
831
  }
751
832
  }
752
833
  }
753
- else {
834
+ else if (!nonKingOnly) {
754
835
  const rights = us === Color.White ? pos.castling.white : pos.castling.black;
755
836
  if (rights.size > 0) {
756
837
  for (const rs of rights) {
757
838
  const rp = board.pieceAt(pos.board, rs);
758
839
  if (rp && rp.color === us && rp.role === Role.Rook) {
759
- const slot = ctx.castlingPlans.length === 0 ? scratchCastlingPlanA : scratchCastlingPlanB;
760
- const next = castlingPlanFor(us, ksq, rs);
761
- slot.side = next.side;
762
- slot.kingFrom = next.kingFrom;
763
- slot.kingTo = next.kingTo;
764
- slot.rookFrom = next.rookFrom;
765
- slot.rookTo = next.rookTo;
766
- ctx.castlingPlans.push(slot);
840
+ // Each analysis context owns its plans; the returned object is fresh
841
+ // and can never overwrite a previously returned context.
842
+ ctx.castlingPlans.push(castlingPlanFor(us, ksq, rs));
767
843
  }
768
844
  }
769
845
  }
770
846
  }
771
847
  const checkers = pos.checkers ?? attacks.kingAttackers(pos.board, us);
772
- const nCheckers = sq.popcount(checkers);
773
- if (nCheckers === 1) {
774
- const c = sq.first(checkers);
775
- ctx.checkMask = sq.or(attacks.between(ksq, c), sq.singleton(c));
776
- }
777
- else if (nCheckers >= 2) {
778
- ctx.doubleCheck = true;
848
+ // Most positions are not in check. Avoid two popcounts for zero checkers;
849
+ // distinguish single from double checks with just bit identity tests.
850
+ if ((checkers.lo | checkers.hi) !== 0) {
851
+ if (sq.moreThanOne(checkers))
852
+ ctx.doubleCheck = true;
853
+ else {
854
+ const c = sq.first(checkers);
855
+ ctx.checkMask = sq.or(attacks.between(ksq, c), sq.singleton(c));
856
+ }
779
857
  }
780
858
  // Pinned pieces: enemy slider "snipers" x-raying our king with exactly one
781
859
  // blocker between; if the blocker is ours, it is pinned to the sniper's ray.
782
860
  const themOcc = them === Color.White ? pos.board.white : pos.board.black;
783
- const themBQ = sq.or(sq.and(themOcc, pos.board.bishop), sq.and(themOcc, pos.board.queen));
784
- const themRQ = sq.or(sq.and(themOcc, pos.board.rook), sq.and(themOcc, pos.board.queen));
785
- const noOcc = sq.EMPTY;
786
- const snipers = sq.or(sq.and(themRQ, attacks.rookAttacks(ksq, noOcc)), sq.and(themBQ, attacks.bishopAttacks(ksq, noOcc)));
861
+ const themBQ = {
862
+ lo: (themOcc.lo & (pos.board.bishop.lo | pos.board.queen.lo)) >>> 0,
863
+ hi: (themOcc.hi & (pos.board.bishop.hi | pos.board.queen.hi)) >>> 0,
864
+ };
865
+ const themRQ = {
866
+ lo: (themOcc.lo & (pos.board.rook.lo | pos.board.queen.lo)) >>> 0,
867
+ hi: (themOcc.hi & (pos.board.rook.hi | pos.board.queen.hi)) >>> 0,
868
+ };
869
+ // Enemy occupancy stops sniper rays beyond an intervening enemy piece.
870
+ // A farther slider behind another opponent piece cannot pin a friendly
871
+ // blocker, so filtering these rays avoids redundant BETWEEN work.
872
+ // Mirror gigachess-rs compute_pinned_split's enemy-occupancy geometry.
873
+ const rRay = ROOK_KING_RAYS[ksq];
874
+ const bRay = BISHOP_KING_RAYS[ksq];
875
+ const hasRookLine = (((themRQ.lo & rRay.lo) | (themRQ.hi & rRay.hi)) >>> 0) !== 0;
876
+ const hasBishopLine = (((themBQ.lo & bRay.lo) | (themBQ.hi & bRay.hi)) >>> 0) !== 0;
877
+ const rookSniperRays = hasRookLine ? attacks.rookAttacks(ksq, themOcc) : sq.EMPTY;
878
+ const bishopSniperRays = hasBishopLine ? attacks.bishopAttacks(ksq, themOcc) : sq.EMPTY;
879
+ const snipers = {
880
+ lo: ((themRQ.lo & rookSniperRays.lo) | (themBQ.lo & bishopSniperRays.lo)) >>> 0,
881
+ hi: ((themRQ.hi & rookSniperRays.hi) | (themBQ.hi & bishopSniperRays.hi)) >>> 0,
882
+ };
787
883
  let pinnedLo = 0, pinnedHi = 0;
788
884
  let sLo = snipers.lo >>> 0, sHi = snipers.hi >>> 0;
789
885
  while (sLo !== 0) {
@@ -793,15 +889,12 @@ export function analyzeCheckContext(pos) {
793
889
  const b = attacks.between(ksq, sniper);
794
890
  const bLo = (b.lo & pos.board.occupied.lo) >>> 0;
795
891
  const bHi = (b.hi & pos.board.occupied.hi) >>> 0;
796
- const nBlockers = sq.popcount({ lo: bLo, hi: bHi });
797
- if (nBlockers === 1) {
798
- const blockerSq = bLo !== 0 ? (Math.clz32((bLo & -bLo) >>> 0) ^ 31) : (32 + (Math.clz32((bHi & -bHi) >>> 0) ^ 31));
799
- if (!sq.has(themOcc, blockerSq)) {
800
- if (blockerSq < 32)
801
- pinnedLo |= (1 << blockerSq) >>> 0;
802
- else
803
- pinnedHi |= (1 << (blockerSq - 32)) >>> 0;
804
- }
892
+ // One blocker iff exactly one halfboard is nonzero and power-of-two.
893
+ const blockerBits = (bLo | bHi) >>> 0;
894
+ if (blockerBits !== 0 && (bLo === 0 || bHi === 0)
895
+ && (blockerBits & (blockerBits - 1)) === 0) {
896
+ pinnedLo |= bLo & ~themOcc.lo;
897
+ pinnedHi |= bHi & ~themOcc.hi;
805
898
  }
806
899
  }
807
900
  while (sHi !== 0) {
@@ -811,34 +904,81 @@ export function analyzeCheckContext(pos) {
811
904
  const b = attacks.between(ksq, sniper);
812
905
  const bLo = (b.lo & pos.board.occupied.lo) >>> 0;
813
906
  const bHi = (b.hi & pos.board.occupied.hi) >>> 0;
814
- const nBlockers = sq.popcount({ lo: bLo, hi: bHi });
815
- if (nBlockers === 1) {
816
- const blockerSq = bLo !== 0 ? (Math.clz32((bLo & -bLo) >>> 0) ^ 31) : (32 + (Math.clz32((bHi & -bHi) >>> 0) ^ 31));
817
- if (!sq.has(themOcc, blockerSq)) {
818
- if (blockerSq < 32)
819
- pinnedLo |= (1 << blockerSq) >>> 0;
820
- else
821
- pinnedHi |= (1 << (blockerSq - 32)) >>> 0;
822
- }
907
+ // One blocker iff exactly one halfboard is nonzero and power-of-two.
908
+ const blockerBits = (bLo | bHi) >>> 0;
909
+ if (blockerBits !== 0 && (bLo === 0 || bHi === 0)
910
+ && (blockerBits & (blockerBits - 1)) === 0) {
911
+ pinnedLo |= bLo & ~themOcc.lo;
912
+ pinnedHi |= bHi & ~themOcc.hi;
823
913
  }
824
914
  }
825
915
  ctx.pinned = { lo: pinnedLo >>> 0, hi: pinnedHi >>> 0 };
826
- // King-safe mask: attackedness evaluated with our king removed from the
827
- // occupancy, so sliders x-raying the king keep it attacked on the far side.
828
- const occWithoutKing = sq.minus(pos.board.occupied, sq.singleton(ksq));
916
+ if (nonKingOnly)
917
+ return ctx;
918
+ // Only reachable king destinations require attack checks. King surrounded
919
+ // by its own pieces is an especially common leaf: no attack map necessary.
829
920
  const usOcc = us === Color.White ? pos.board.white : pos.board.black;
830
- let kingLo = 0, kingHi = 0;
831
- sq.forEachSquare(attacks.kingAttacks(ksq), (d) => {
832
- if (sq.has(usOcc, d))
833
- return;
834
- if (sq.isEmpty(attacks.attackersTo(pos.board, d, them, occWithoutKing))) {
835
- if (d < 32)
836
- kingLo |= (1 << d) >>> 0;
837
- else
838
- kingHi |= (1 << (d - 32)) >>> 0;
921
+ const adjacent = attacks.kingAttacks(ksq);
922
+ const candidate = {
923
+ lo: (adjacent.lo & ~usOcc.lo) >>> 0,
924
+ hi: (adjacent.hi & ~usOcc.hi) >>> 0,
925
+ };
926
+ if ((candidate.lo | candidate.hi) === 0) {
927
+ ctx.kingSafe = sq.EMPTY;
928
+ }
929
+ else {
930
+ const occWithoutKing = sq.minus(pos.board.occupied, sq.singleton(ksq));
931
+ // A single-pass attack map is best for sparse material or king mobility.
932
+ // For cramped kings with many attackers, direct king-target probes win.
933
+ if (sq.popcount(themOcc) <= 8 || sq.popcount(candidate) >= 4) {
934
+ const danger = attacks.attackedSquares(pos.board, them, occWithoutKing);
935
+ ctx.kingSafe = {
936
+ lo: (candidate.lo & ~danger.lo) >>> 0,
937
+ hi: (candidate.hi & ~danger.hi) >>> 0,
938
+ };
839
939
  }
840
- });
841
- ctx.kingSafe = { lo: kingLo >>> 0, hi: kingHi >>> 0 };
940
+ else {
941
+ // Reuse the enemy slider masks already obtained for pin analysis.
942
+ // Filter non-sliders before magic probes; never rebuild the enemy
943
+ // role partition for each target or allocate an attackers bitboard.
944
+ const b = pos.board;
945
+ const pLo = themOcc.lo & b.pawn.lo, pHi = themOcc.hi & b.pawn.hi;
946
+ const nLo = themOcc.lo & b.knight.lo, nHi = themOcc.hi & b.knight.hi;
947
+ const kLo = themOcc.lo & b.king.lo, kHi = themOcc.hi & b.king.hi;
948
+ let kingLo = 0, kingHi = 0;
949
+ for (let half = 0; half < 2; half++) {
950
+ let bits = half === 0 ? candidate.lo : candidate.hi;
951
+ while (bits !== 0) {
952
+ const lsb = bits & -bits;
953
+ const d = (half << 5) + 31 - Math.clz32(lsb);
954
+ bits = (bits & (bits - 1)) >>> 0;
955
+ const p = attacks.pawnAttacks(us, d);
956
+ const n = attacks.knightAttacks(d);
957
+ const k = attacks.kingAttacks(d);
958
+ if (((p.lo & pLo) | (p.hi & pHi) | (n.lo & nLo) |
959
+ (n.hi & nHi) | (k.lo & kLo) | (k.hi & kHi)) !== 0)
960
+ continue;
961
+ const br = BISHOP_KING_RAYS[d];
962
+ if (((br.lo & themBQ.lo) | (br.hi & themBQ.hi)) !== 0) {
963
+ const at = attacks.bishopAttacks(d, occWithoutKing);
964
+ if (((at.lo & themBQ.lo) | (at.hi & themBQ.hi)) !== 0)
965
+ continue;
966
+ }
967
+ const rr = ROOK_KING_RAYS[d];
968
+ if (((rr.lo & themRQ.lo) | (rr.hi & themRQ.hi)) !== 0) {
969
+ const at = attacks.rookAttacks(d, occWithoutKing);
970
+ if (((at.lo & themRQ.lo) | (at.hi & themRQ.hi)) !== 0)
971
+ continue;
972
+ }
973
+ if (half === 0)
974
+ kingLo |= lsb;
975
+ else
976
+ kingHi |= lsb;
977
+ }
978
+ }
979
+ ctx.kingSafe = { lo: kingLo >>> 0, hi: kingHi >>> 0 };
980
+ }
981
+ }
842
982
  return ctx;
843
983
  }
844
984
  /**
@@ -846,7 +986,53 @@ export function analyzeCheckContext(pos) {
846
986
  * pure set intersection for all cases except the trap cases listed above.
847
987
  */
848
988
  export function destsFast(pos, from, piece, ctx) {
849
- const pseudo = genPseudoDests(pos, from, piece);
989
+ // Piece-specialized hot path: retain exact check/pin constraints while
990
+ // avoiding generic pseudo generation, own-occupancy subtraction and
991
+ // temporary SquareSet objects for every non-pawn non-king piece.
992
+ if (piece.role !== Role.Pawn && piece.role !== Role.King) {
993
+ if (piece.color !== ctx.us || ctx.doubleCheck)
994
+ return sq.EMPTY;
995
+ const own = piece.color === Color.White ? pos.board.white : pos.board.black;
996
+ let attacksBB;
997
+ switch (piece.role) {
998
+ case Role.Knight:
999
+ attacksBB = attacks.knightAttacks(from);
1000
+ break;
1001
+ case Role.Bishop:
1002
+ attacksBB = attacks.bishopAttacks(from, pos.board.occupied);
1003
+ break;
1004
+ case Role.Rook:
1005
+ attacksBB = attacks.rookAttacks(from, pos.board.occupied);
1006
+ break;
1007
+ case Role.Queen:
1008
+ attacksBB = attacks.queenAttacks(from, pos.board.occupied);
1009
+ break;
1010
+ default: return sq.EMPTY;
1011
+ }
1012
+ let lo = (attacksBB.lo & ~own.lo & ctx.checkMask.lo) >>> 0;
1013
+ let hi = (attacksBB.hi & ~own.hi & ctx.checkMask.hi) >>> 0;
1014
+ if (sq.has(ctx.pinned, from)) {
1015
+ const idx = (ctx.ksq << 6) | from;
1016
+ lo = (lo & attacks.LINE_RAY_LO[idx]) >>> 0;
1017
+ hi = (hi & attacks.LINE_RAY_HI[idx]) >>> 0;
1018
+ }
1019
+ return { lo, hi };
1020
+ }
1021
+ // In most positions there are no usable castling rights. Normal king
1022
+ // destinations are already masked by the per-position kingSafe bitboard;
1023
+ // bypass the king's castling/check/traversal and per-destination callbacks.
1024
+ if (piece.role === Role.King && ctx.castlingPlans.length === 0) {
1025
+ const adjacent = attacks.kingAttacks(from);
1026
+ const own = piece.color === Color.White ? pos.board.white : pos.board.black;
1027
+ return {
1028
+ lo: (adjacent.lo & ctx.kingSafe.lo & ~own.lo) >>> 0,
1029
+ hi: (adjacent.hi & ctx.kingSafe.hi & ~own.hi) >>> 0,
1030
+ };
1031
+ }
1032
+ const pseudo = piece.role === Role.Pawn
1033
+ ? (piece.color !== pos.turn ? sq.EMPTY : (piece.color === Color.White
1034
+ ? whitePawnPseudoDests(pos, from) : blackPawnPseudoDests(pos, from)))
1035
+ : genPseudoDests(pos, from, piece);
850
1036
  if (sq.isEmpty(pseudo))
851
1037
  return pseudo;
852
1038
  if (piece.role === Role.King) {
@@ -902,11 +1088,63 @@ export function destsFast(pos, from, piece, ctx) {
902
1088
  result = sq.or(result, sq.singleton(epSquare));
903
1089
  return result;
904
1090
  }
1091
+ /** For one non-king origin, only that origin's pin can affect its legal
1092
+ * destinations. Use exactly the same check mask as full-context analysis. */
1093
+ function singlePieceContext(pos, from) {
1094
+ const us = pos.turn, them = opposite(us), b = pos.board;
1095
+ const ksq = board.kingSquare(b, us);
1096
+ if (ksq === undefined || ksq > 63 || from < 0 || from > 63 || !Number.isInteger(from))
1097
+ return analyzeCheckContextInternal(pos, undefined, false, true);
1098
+ const ctx = { us, ksq, doubleCheck: false, checkMask: sq.FULL,
1099
+ kingSafe: sq.EMPTY, pinned: sq.EMPTY, castlingPlans: [] };
1100
+ const checkers = pos.checkers ?? attacks.kingAttackers(b, us);
1101
+ if ((checkers.lo | checkers.hi) !== 0) {
1102
+ if (sq.moreThanOne(checkers)) {
1103
+ ctx.doubleCheck = true;
1104
+ return ctx;
1105
+ }
1106
+ const c = sq.first(checkers);
1107
+ ctx.checkMask = sq.or(attacks.between(ksq, c), sq.singleton(c));
1108
+ }
1109
+ // A nonaligned origin cannot be the sole blocker on a king-slider line.
1110
+ const i = (ksq << 6) | from;
1111
+ const lineLo = attacks.LINE_RAY_LO[i], lineHi = attacks.LINE_RAY_HI[i];
1112
+ if ((lineLo | lineHi) === 0)
1113
+ return ctx;
1114
+ const diagonal = (ksq & 7) !== (from & 7) && (ksq >> 3) !== (from >> 3);
1115
+ const role = diagonal ? b.bishop : b.rook;
1116
+ const enemy = them === Color.White ? b.white : b.black;
1117
+ if ((enemy.lo & ~b.occupied.lo) || (enemy.hi & ~b.occupied.hi))
1118
+ return analyzeCheckContextInternal(pos, undefined, false, true);
1119
+ let lo = enemy.lo & (role.lo | b.queen.lo) & lineLo;
1120
+ let hi = enemy.hi & (role.hi | b.queen.hi) & lineHi;
1121
+ const fl = from < 32 ? 1 << from : 0, fh = from >= 32 ? 1 << (from - 32) : 0;
1122
+ // Full analysis never marks an enemy square as pinned.
1123
+ if ((enemy.lo & fl) || (enemy.hi & fh))
1124
+ return ctx;
1125
+ while (lo || hi) {
1126
+ let slider;
1127
+ if (lo) {
1128
+ slider = 31 - Math.clz32(lo & -lo);
1129
+ lo &= lo - 1;
1130
+ }
1131
+ else {
1132
+ slider = 63 - Math.clz32(hi & -hi);
1133
+ hi &= hi - 1;
1134
+ }
1135
+ const bt = attacks.between(ksq, slider);
1136
+ if ((bt.lo & b.occupied.lo) === fl && (bt.hi & b.occupied.hi) === fh) {
1137
+ ctx.pinned = { lo: fl >>> 0, hi: fh >>> 0 };
1138
+ break;
1139
+ }
1140
+ }
1141
+ return ctx;
1142
+ }
905
1143
  export function dests(pos, from) {
906
1144
  const piece = board.pieceAt(pos.board, from);
907
1145
  if (!piece)
908
1146
  return { lo: 0, hi: 0 };
909
- return destsFast(pos, from, piece, analyzeCheckContext(pos));
1147
+ return destsFast(pos, from, piece, piece.role === Role.King ? analyzeCheckContext(pos) : singlePieceContext(pos, from));
910
1148
  }
911
1149
  export function allDests(pos) {
912
1150
  const m = new Map();
@@ -953,7 +1191,9 @@ export function isLegal(pos, move) {
953
1191
  }
954
1192
  const piece = board.pieceAt(pos.board, move.from);
955
1193
  const to = move.to;
956
- const d = dests(pos, move.from);
1194
+ if (!piece)
1195
+ return false;
1196
+ const d = destsFast(pos, move.from, piece, piece.role === Role.King ? analyzeCheckContext(pos) : singlePieceContext(pos, move.from));
957
1197
  if (!sq.has(d, to))
958
1198
  return false;
959
1199
  if (piece && piece.role === Role.Pawn) {
@@ -1039,8 +1279,8 @@ export class MoveCounter {
1039
1279
  this.count = 0;
1040
1280
  }
1041
1281
  }
1042
- export function countLegalMoves(pos) {
1043
- const ctx = analyzeCheckContext(pos);
1282
+ export function countLegalMoves(pos, existingContext) {
1283
+ const ctx = existingContext ?? analyzeCheckContext(pos);
1044
1284
  const us = pos.turn;
1045
1285
  const isWhite = us === Color.White;
1046
1286
  const own = isWhite ? pos.board.white : pos.board.black;
@@ -1053,58 +1293,138 @@ export function countLegalMoves(pos) {
1053
1293
  }
1054
1294
  if (ctx.doubleCheck)
1055
1295
  return count;
1056
- // 2. Pawns (bulk popcount without inner destination loops)
1296
+ // 2. Pawn mobility in parallel by 32-bit halfboard shifts.
1297
+ // When not in check, no pawn pinned, and no EP square, each pawn move is
1298
+ // independently legal. Popcount the four pawn categories directly; this
1299
+ // avoids 8–16 separate per-pawn destination and safety operations.
1057
1300
  const pawnPiece = isWhite ? P_W_P : P_B_P;
1058
1301
  let pLo = (own.lo & pos.board.pawn.lo) >>> 0;
1059
1302
  let pHi = (own.hi & pos.board.pawn.hi) >>> 0;
1060
- while (pLo !== 0) {
1061
- const lsb = (pLo & -pLo) >>> 0;
1062
- const from = Math.clz32(lsb) ^ 31;
1063
- pLo = (pLo ^ lsb) >>> 0;
1064
- const legal = destsFast(pos, from, pawnPiece, ctx);
1065
- const total = sq.popcount(legal);
1066
- const promos = isWhite
1067
- ? sq.popcount({ lo: 0, hi: (legal.hi & 0xff000000) >>> 0 })
1068
- : sq.popcount({ lo: (legal.lo & 0x000000ff) >>> 0, hi: 0 });
1069
- count += total + promos * 3;
1070
- }
1071
- while (pHi !== 0) {
1072
- const lsb = (pHi & -pHi) >>> 0;
1073
- const from = 32 + (Math.clz32(lsb) ^ 31);
1074
- pHi = (pHi ^ lsb) >>> 0;
1075
- const legal = destsFast(pos, from, pawnPiece, ctx);
1076
- const total = sq.popcount(legal);
1077
- const promos = isWhite
1078
- ? sq.popcount({ lo: 0, hi: (legal.hi & 0xff000000) >>> 0 })
1079
- : sq.popcount({ lo: (legal.lo & 0x000000ff) >>> 0, hi: 0 });
1080
- count += total + promos * 3;
1303
+ if (pos.epSquare === null && ctx.checkMask === sq.FULL
1304
+ && (((ctx.pinned.lo & pLo) | (ctx.pinned.hi & pHi)) === 0)) {
1305
+ const occ = pos.board.occupied;
1306
+ const them = isWhite ? pos.board.black : pos.board.white;
1307
+ const emptyLo = ~occ.lo, emptyHi = ~occ.hi;
1308
+ let oneLo, oneHi, twoLo, twoHi;
1309
+ let leftLo, leftHi, rightLo, rightHi;
1310
+ const westLo = (pLo & 0xfefefefe) >>> 0;
1311
+ const westHi = (pHi & 0xfefefefe) >>> 0;
1312
+ const eastLo = (pLo & 0x7f7f7f7f) >>> 0;
1313
+ const eastHi = (pHi & 0x7f7f7f7f) >>> 0;
1314
+ if (isWhite) {
1315
+ oneLo = ((pLo << 8) & emptyLo) >>> 0;
1316
+ oneHi = (((pHi << 8) | (pLo >>> 24)) & emptyHi) >>> 0;
1317
+ twoLo = (((oneLo & 0x00ff0000) << 8) & emptyLo) >>> 0;
1318
+ twoHi = 0;
1319
+ leftLo = ((westLo << 7) & them.lo) >>> 0;
1320
+ leftHi = (((westHi << 7) | (westLo >>> 25)) & them.hi) >>> 0;
1321
+ rightLo = ((eastLo << 9) & them.lo) >>> 0;
1322
+ rightHi = (((eastHi << 9) | (eastLo >>> 23)) & them.hi) >>> 0;
1323
+ // Only white pawns on rank 7 can promote in one move. Avoid three
1324
+ // expensive popcounts in almost every ordinary leaf position.
1325
+ if ((pHi & 0x00ff0000) !== 0) {
1326
+ count += 3 * (sq.popcountParts(oneHi & 0xff000000, 0) +
1327
+ sq.popcountParts(leftHi & 0xff000000, 0) +
1328
+ sq.popcountParts(rightHi & 0xff000000, 0));
1329
+ }
1330
+ }
1331
+ else {
1332
+ oneLo = (((pLo >>> 8) | (pHi << 24)) & emptyLo) >>> 0;
1333
+ oneHi = ((pHi >>> 8) & emptyHi) >>> 0;
1334
+ twoLo = 0;
1335
+ twoHi = (((oneHi & 0x0000ff00) >>> 8) & emptyHi) >>> 0;
1336
+ leftLo = (((westLo >>> 9) | (westHi << 23)) & them.lo) >>> 0;
1337
+ leftHi = ((westHi >>> 9) & them.hi) >>> 0;
1338
+ rightLo = (((eastLo >>> 7) | (eastHi << 25)) & them.lo) >>> 0;
1339
+ rightHi = ((eastHi >>> 7) & them.hi) >>> 0;
1340
+ if ((pLo & 0x0000ff00) !== 0) {
1341
+ count += 3 * (sq.popcountParts(oneLo & 0x000000ff, 0) +
1342
+ sq.popcountParts(leftLo & 0x000000ff, 0) +
1343
+ sq.popcountParts(rightLo & 0x000000ff, 0));
1344
+ }
1345
+ }
1346
+ count += sq.popcountParts(oneLo, oneHi);
1347
+ count += sq.popcountParts(twoLo, twoHi);
1348
+ count += sq.popcountParts(leftLo, leftHi);
1349
+ count += sq.popcountParts(rightLo, rightHi);
1081
1350
  }
1082
- // 3. Knights, Bishops, Rooks, Queens (bulk popcount on legal dests)
1083
- const countPieces = (roleBB, piece) => {
1084
- let rLo = (own.lo & roleBB.lo) >>> 0;
1085
- let rHi = (own.hi & roleBB.hi) >>> 0;
1086
- while (rLo !== 0) {
1087
- const lsb = (rLo & -rLo) >>> 0;
1351
+ else {
1352
+ // Fallback handles checks, pins and en-passant discovered-check traps.
1353
+ while (pLo !== 0) {
1354
+ const lsb = (pLo & -pLo) >>> 0;
1088
1355
  const from = Math.clz32(lsb) ^ 31;
1089
- rLo = (rLo ^ lsb) >>> 0;
1090
- count += sq.popcount(destsFast(pos, from, piece, ctx));
1356
+ pLo = (pLo ^ lsb) >>> 0;
1357
+ const legal = destsFast(pos, from, pawnPiece, ctx);
1358
+ const total = sq.popcountParts(legal.lo, legal.hi);
1359
+ const promos = isWhite
1360
+ ? ((from >= 48 && from < 56) ? sq.popcountParts(legal.hi & 0xff000000, 0) : 0)
1361
+ : ((from >= 8 && from < 16) ? sq.popcountParts(legal.lo & 0x000000ff, 0) : 0);
1362
+ count += total + promos * 3;
1091
1363
  }
1092
- while (rHi !== 0) {
1093
- const lsb = (rHi & -rHi) >>> 0;
1364
+ while (pHi !== 0) {
1365
+ const lsb = (pHi & -pHi) >>> 0;
1094
1366
  const from = 32 + (Math.clz32(lsb) ^ 31);
1095
- rHi = (rHi ^ lsb) >>> 0;
1096
- count += sq.popcount(destsFast(pos, from, piece, ctx));
1367
+ pHi = (pHi ^ lsb) >>> 0;
1368
+ const legal = destsFast(pos, from, pawnPiece, ctx);
1369
+ const total = sq.popcountParts(legal.lo, legal.hi);
1370
+ const promos = isWhite
1371
+ ? ((from >= 48 && from < 56) ? sq.popcountParts(legal.hi & 0xff000000, 0) : 0)
1372
+ : ((from >= 8 && from < 16) ? sq.popcountParts(legal.lo & 0x000000ff, 0) : 0);
1373
+ count += total + promos * 3;
1097
1374
  }
1098
- };
1099
- countPieces(pos.board.knight, isWhite ? P_W_N : P_B_N);
1100
- countPieces(pos.board.bishop, isWhite ? P_W_B : P_B_B);
1101
- countPieces(pos.board.rook, isWhite ? P_W_R : P_B_R);
1102
- countPieces(pos.board.queen, isWhite ? P_W_Q : P_B_Q);
1375
+ }
1376
+ // 3. All non-pawn, non-king pieces in one bit traversal. Unlike four
1377
+ // per-role calls this performs one two-half scan and chooses role from
1378
+ // disjoint piece role bitboards. Still applies the same pin and check masks.
1379
+ const allRoles = pos.board;
1380
+ let piecesLo = (own.lo & (allRoles.knight.lo | allRoles.bishop.lo
1381
+ | allRoles.rook.lo | allRoles.queen.lo)) >>> 0;
1382
+ let piecesHi = (own.hi & (allRoles.knight.hi | allRoles.bishop.hi
1383
+ | allRoles.rook.hi | allRoles.queen.hi)) >>> 0;
1384
+ const pinnedLo = ctx.pinned.lo;
1385
+ const pinnedHi = ctx.pinned.hi;
1386
+ const occ = allRoles.occupied;
1387
+ const cLo = (ctx.checkMask.lo & ~own.lo) >>> 0;
1388
+ const cHi = (ctx.checkMask.hi & ~own.hi) >>> 0;
1389
+ for (let half = 0; half < 2; half++) {
1390
+ let bits = half === 0 ? piecesLo : piecesHi;
1391
+ const knightBits = half === 0 ? allRoles.knight.lo : allRoles.knight.hi;
1392
+ const bishopBits = half === 0 ? allRoles.bishop.lo : allRoles.bishop.hi;
1393
+ const rookBits = half === 0 ? allRoles.rook.lo : allRoles.rook.hi;
1394
+ const pins = half === 0 ? pinnedLo : pinnedHi;
1395
+ while (bits !== 0) {
1396
+ const lsb = (bits & -bits) >>> 0;
1397
+ const from = (half << 5) + (31 - Math.clz32(lsb));
1398
+ bits = (bits ^ lsb) >>> 0;
1399
+ let target;
1400
+ if ((lsb & knightBits) !== 0)
1401
+ target = attacks.knightAttacks(from);
1402
+ else if ((lsb & bishopBits) !== 0)
1403
+ target = attacks.bishopAttacks(from, occ);
1404
+ else if ((lsb & rookBits) !== 0)
1405
+ target = attacks.rookAttacks(from, occ);
1406
+ else
1407
+ target = attacks.queenAttacks(from, occ);
1408
+ let lo = (target.lo & cLo) >>> 0;
1409
+ let hi = (target.hi & cHi) >>> 0;
1410
+ if ((lsb & pins) !== 0) {
1411
+ const idx = (ctx.ksq << 6) | from;
1412
+ lo &= attacks.LINE_RAY_LO[idx];
1413
+ hi &= attacks.LINE_RAY_HI[idx];
1414
+ }
1415
+ count += sq.popcountParts(lo, hi);
1416
+ }
1417
+ }
1103
1418
  return count;
1104
1419
  }
1105
- export function legalMovesInto(pos, out) {
1420
+ export function legalMovesInto(pos, out, existingContext) {
1421
+ if (existingContext === undefined) {
1422
+ const direct = standardMovesInto(pos, out);
1423
+ if (direct !== undefined)
1424
+ return direct;
1425
+ }
1106
1426
  let count = 0;
1107
- const ctx = analyzeCheckContext(pos);
1427
+ const ctx = existingContext ?? analyzeCheckContext(pos);
1108
1428
  const us = pos.turn;
1109
1429
  const isWhite = us === Color.White;
1110
1430
  const own = isWhite ? pos.board.white : pos.board.black;
@@ -1134,9 +1454,73 @@ export function legalMovesInto(pos, out) {
1134
1454
  let pLo = (own.lo & pos.board.pawn.lo) >>> 0;
1135
1455
  let pHi = (own.hi & pos.board.pawn.hi) >>> 0;
1136
1456
  const promoRank = isWhite ? 7 : 0;
1457
+ const fastPawns = pos.epSquare === null && ctx.checkMask === sq.FULL
1458
+ && (((ctx.pinned.lo & pLo) | (ctx.pinned.hi & pHi)) === 0);
1459
+ const occ = pos.board.occupied;
1460
+ const enemy = isWhite ? pos.board.black : pos.board.white;
1137
1461
  const emitPawnDests = (from) => {
1138
- const legal = destsFast(pos, from, pawnPiece, ctx);
1139
- let dLo = legal.lo >>> 0, dHi = legal.hi >>> 0;
1462
+ let dLo = 0, dHi = 0;
1463
+ if (fastPawns) {
1464
+ // Exactly the standard pawn pseudo-destination set: when there is no
1465
+ // check, pawn pin or en-passant trap, this is already the legal set.
1466
+ if (isWhite) {
1467
+ const one = from + 8;
1468
+ if (!sq.has(occ, one)) {
1469
+ if (one < 32)
1470
+ dLo |= 1 << one;
1471
+ else
1472
+ dHi |= 1 << (one - 32);
1473
+ if (from >= 8 && from <= 15 && !sq.has(occ, from + 16))
1474
+ dLo |= 1 << (from + 16);
1475
+ }
1476
+ if ((from & 7) !== 0 && sq.has(enemy, from + 7)) {
1477
+ const to = from + 7;
1478
+ if (to < 32)
1479
+ dLo |= 1 << to;
1480
+ else
1481
+ dHi |= 1 << (to - 32);
1482
+ }
1483
+ if ((from & 7) !== 7 && sq.has(enemy, from + 9)) {
1484
+ const to = from + 9;
1485
+ if (to < 32)
1486
+ dLo |= 1 << to;
1487
+ else
1488
+ dHi |= 1 << (to - 32);
1489
+ }
1490
+ }
1491
+ else {
1492
+ const one = from - 8;
1493
+ if (!sq.has(occ, one)) {
1494
+ if (one < 32)
1495
+ dLo |= 1 << one;
1496
+ else
1497
+ dHi |= 1 << (one - 32);
1498
+ if (from >= 48 && from <= 55 && !sq.has(occ, from - 16))
1499
+ dHi |= 1 << (from - 48);
1500
+ }
1501
+ if ((from & 7) !== 0 && sq.has(enemy, from - 9)) {
1502
+ const to = from - 9;
1503
+ if (to < 32)
1504
+ dLo |= 1 << to;
1505
+ else
1506
+ dHi |= 1 << (to - 32);
1507
+ }
1508
+ if ((from & 7) !== 7 && sq.has(enemy, from - 7)) {
1509
+ const to = from - 7;
1510
+ if (to < 32)
1511
+ dLo |= 1 << to;
1512
+ else
1513
+ dHi |= 1 << (to - 32);
1514
+ }
1515
+ }
1516
+ dLo >>>= 0;
1517
+ dHi >>>= 0;
1518
+ }
1519
+ else {
1520
+ const legal = destsFast(pos, from, pawnPiece, ctx);
1521
+ dLo = legal.lo >>> 0;
1522
+ dHi = legal.hi >>> 0;
1523
+ }
1140
1524
  while (dLo !== 0) {
1141
1525
  const lsb = (dLo & -dLo) >>> 0;
1142
1526
  const to = Math.clz32(lsb) ^ 31;
@@ -1668,12 +2052,12 @@ export class Chess {
1668
2052
  }
1669
2053
  return rows;
1670
2054
  }
1671
- /** "light" | "dark" square color ("a1" is light, like the consumer baseline). */
2055
+ /** "light" | "dark" square color (a1 is dark, h1 is light). */
1672
2056
  squareColor(square) {
1673
2057
  const sqIdx = parseSquare(square);
1674
2058
  if (sqIdx === undefined)
1675
2059
  return undefined;
1676
- return (((sqIdx & 7) + (sqIdx >> 3)) & 1) === 0 ? "light" : "dark";
2060
+ return (((sqIdx & 7) + (sqIdx >> 3)) & 1) === 0 ? "dark" : "light";
1677
2061
  }
1678
2062
  /** PGN: Seven Tag Roster defaults, FEN/SetUp for non-default starts, numbered movetext. */
1679
2063
  pgn() {