gigachess 0.2.1 β 0.3.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.
- package/README.md +37 -19
- package/dist/attacks.d.ts +3 -0
- package/dist/attacks.d.ts.map +1 -1
- package/dist/attacks.js +44 -0
- package/dist/board.d.ts +12 -1
- package/dist/board.d.ts.map +1 -1
- package/dist/board.js +22 -0
- package/dist/castling.d.ts +31 -0
- package/dist/castling.d.ts.map +1 -0
- package/dist/castling.js +145 -0
- package/dist/chess.d.ts +21 -7
- package/dist/chess.d.ts.map +1 -1
- package/dist/chess.js +813 -339
- package/dist/chessops/convert.d.ts.map +1 -1
- package/dist/chessops/convert.js +1 -6
- package/dist/fen.d.ts.map +1 -1
- package/dist/fen.js +83 -47
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/packedMove.js +1 -1
- package/dist/san.d.ts.map +1 -1
- package/dist/san.js +148 -172
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/zobrist.d.ts.map +1 -1
- package/dist/zobrist.js +34 -19
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
8
|
<strong>The fastest chess engine in JavaScript.</strong><br>
|
|
9
|
-
A 1-line drop-in upgrade for <code>chess.js</code> and <code>chessops</code> delivering
|
|
10
|
-
120,000 games/sec PGN parsing, and interactive variation trees.
|
|
9
|
+
A 1-line drop-in upgrade for <code>chess.js</code> and <code>chessops</code> delivering up to 7.4x faster move validation,
|
|
10
|
+
18.5M nodes/s perft throughput, 120,000 games/sec PGN parsing, and interactive variation trees.
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
13
|
<p align="center">
|
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
|
|
21
21
|
---
|
|
22
22
|
|
|
23
|
+
> π¦ **Looking for maximum native backend performance?** Check out [**`gigachess` (Rust)**](https://github.com/itshak/gigachess-rs) β the fastest chess move generator in Rust, featuring 540M nodes/sec perft throughput, hardware PEXT / Fancy Magic bitboards, 16-bit binary replay (1.4M games/sec), and zero heap allocations for database workstations and search engines. Available on [crates.io](https://crates.io/crates/gigachess).
|
|
24
|
+
|
|
23
25
|
## β‘ Why GigaChess?
|
|
24
26
|
|
|
25
27
|
Until today, chess developers had to choose between two compromises:
|
|
@@ -27,8 +29,9 @@ Until today, chess developers had to choose between two compromises:
|
|
|
27
29
|
2. **`chessops`**: Fast bitboards, but restrictive GPL licensing, functional-only syntax with no single class, and lack of variation trees or transposition hashing.
|
|
28
30
|
|
|
29
31
|
**GigaChess eliminates this compromise.** It gives you:
|
|
30
|
-
- π **
|
|
31
|
-
- β‘ **Up to 3.
|
|
32
|
+
- π **Up to 7.4x Faster than `chess.js`** on SAN move generation and 7.0x on PGN streaming.
|
|
33
|
+
- β‘ **Up to 3.8x Faster than `chessops`** on real-world workstation workloads (`chessground` dests).
|
|
34
|
+
- ποΈ **3.8xβ5.3x Faster Movegen than Rust/WASM** (`ultrachess`) due to zero boundary serialization overhead.
|
|
32
35
|
- π **1-Line Drop-in Replacement** for `chess.js` (`import { Chess } from 'gigachess'`) and `chessops` (`import * as chessops from 'gigachess/chessops'`).
|
|
33
36
|
- π³ **Built-in `chesstree` Variation Trees** (`game.toTree()` and `Chess.loadTree(pgn)`).
|
|
34
37
|
- π **Instant $O(1)$ 64-bit Polyglot Zobrist Hashing** (`game.zobrist()`).
|
|
@@ -43,17 +46,17 @@ Until today, chess developers had to choose between two compromises:
|
|
|
43
46
|
|---|---|---|---|---|
|
|
44
47
|
| **License** | β
**100% MIT** | β **GPL-3.0** | β
**BSD-2-Clause** | **Free for commercial use** |
|
|
45
48
|
| **Sliding Piece Attacks** | **35.5 MAttacks/s** | 10.6 MAttacks/s | N/A *(array scan)* | **3.36x faster (+236%)** vs chessops |
|
|
46
|
-
| **Perft Movegen Throughput** | **
|
|
47
|
-
| **Chessground UI Dests** | **
|
|
48
|
-
| **FEN Parse + Make** | **285,000 ops/s** |
|
|
49
|
-
| **SAN Make + Legal Dests** | **
|
|
50
|
-
| **PGN Game Streaming** | **
|
|
51
|
-
| **Repertoire Tree Build** | **
|
|
49
|
+
| **Perft Movegen Throughput** | **18.5 Million nodes/s** | 8.5 Million nodes/s | β *(No perft API)* | **2.17x faster (+117%)** vs chessops |
|
|
50
|
+
| **Chessground UI Dests** | **212,675 pos/s** | 56,000 pos/s | N/A | **3.80x faster (+280%)** vs chessops |
|
|
51
|
+
| **FEN Parse + Make** | **285,000 ops/s** | 125,000 ops/s | 94,000 ops/s | **2.26x vs chessops \| 3.03x vs chess.js** |
|
|
52
|
+
| **SAN Make + Legal Dests** | **51,700 ops/s** | 18,200 ops/s | 6,947 ops/s | **2.84x vs chessops \| 7.44x vs chess.js** |
|
|
53
|
+
| **PGN Game Streaming** | **120,000 games/s** (84 MB/s) | 41,500 games/s (29 MB/s) | 1,710 games/s (1.2 MB/s) | **2.89x vs chessops \| 7.01x vs chess.js** |
|
|
54
|
+
| **Repertoire Tree Build** | **37,200 lines/s** | 20,400 lines/s | β *(No Tree API)* | **1.84x faster (+84%)** vs chessops |
|
|
55
|
+
| **isCheck Detection** | **0.5 ns / op** (2.1B ops/s) | 14.5 ns / op | 85.0 ns / op | **29x vs chessops \| 170x vs chess.js** |
|
|
52
56
|
| **Polyglot Zobrist ($O(1)$)** | β
**Native (`{lo,hi}`)** | β None | β None | **Zero-allocation transposition match** |
|
|
53
57
|
| **16-bit Packed Moves (`moves2`)** | β
**Native (`Uint16Array`)** | β None | β None | **25x smaller memory (160 B/game)** |
|
|
54
|
-
| **Bundle Size (gzipped)** | **17.5 KB gz** | 28.0 KB gz *(full)* | **12.6 KB gz** | **Complete workstation in 17 KB** |
|
|
55
58
|
|
|
56
|
-
*Benchmarks measured on Node.js
|
|
59
|
+
*Benchmarks measured on Node.js v24 on Apple Silicon. Run locally via `node --expose-gc bench/bench-real.mjs`.*
|
|
57
60
|
|
|
58
61
|
---
|
|
59
62
|
|
|
@@ -68,7 +71,7 @@ npm install gigachess
|
|
|
68
71
|
## ποΈ 1-Line Drop-in Migrations
|
|
69
72
|
|
|
70
73
|
### From `chess.js`
|
|
71
|
-
Replace your import statement. Every method, property, and type signature works out of the box with
|
|
74
|
+
Replace your import statement. Every method, property, and type signature works out of the box with up to 7.4x faster move validation:
|
|
72
75
|
```ts
|
|
73
76
|
// Before:
|
|
74
77
|
// import { Chess } from 'chess.js';
|
|
@@ -169,12 +172,27 @@ console.log(replayedGame.fen() === game.fen()); // true
|
|
|
169
172
|
|
|
170
173
|
GigaChess incorporates the same hardware-efficient architectural principles found in Stockfish and modern master engines:
|
|
171
174
|
|
|
172
|
-
1. **Zero-BigInt 32-bit Pair Bitboards (`{ lo, hi }`)**: JavaScript V8 optimizes 32-bit unsigned integers into native CPU registers. BigInt forces heap boxing and garbage collector churn. All bitboards in GigaChess are split into low/high 32-bit unsigned integers.
|
|
173
|
-
2. **
|
|
174
|
-
3. **
|
|
175
|
-
4. **
|
|
176
|
-
5. **
|
|
177
|
-
6. **
|
|
175
|
+
1. **Zero-BigInt 32-bit Pair Bitboards (`{ lo, hi }`)**: JavaScript V8 optimizes 32-bit unsigned integers into native CPU registers. BigInt forces heap boxing and garbage collector churn. All bitboards in GigaChess are split into low/high 32-bit unsigned integers with `>>> 0` bitwise arithmetic.
|
|
176
|
+
2. **Early Standard Chess Fast-Path Separation**: Standard Chess castling rights are stored as a 4-bit integer mask (`WK=1, WQ=2, BK=4, BQ=8`) with constant clearance bitmasks (`0x60`, `0x0E`), eliminating `Set<number>` allocations and dynamic loop overhead.
|
|
177
|
+
3. **Targeted Reverse-Attacker SAN Parser**: Querying destination attackers directly (`attackersTo & pieceRoleBB`) cuts SAN parse latency from ~6.5 Β΅s down to **~493 ns/op** (>8x speedup).
|
|
178
|
+
4. **Branchless `isCheck` Detection**: Cached `checkers: SquareSet` reduces check detection to 2 bitwise operations (`(lo | hi) !== 0`), clocking in at **0.5 ns/op** (>2 Billion checks/sec).
|
|
179
|
+
5. **Precomputed $64 \times 64$ Flat Ray & Between Tables**: Single-cycle `Uint32Array(4096)` array index lookups replace dynamic loops during pin and check resolution.
|
|
180
|
+
6. **Stockfish `CheckContext` (Single-Pass Pin Analysis)**: Pins, check rays, and king-safe destination masks are analyzed **once per position**, turning move validation into fast bitwise intersections ($\text{Pseudo} \cap \text{CheckMask} \cap \text{PinRay}$).
|
|
181
|
+
7. **Black Magic Sliding Bitboards**: $O(1)$ Bishop, Rook, and Queen ray generation delivering over **35.5 Million attacks/sec**.
|
|
182
|
+
8. **Piece-Centric Movegen & Vectorized Pawn Shifts**: Direct iteration over piece bitboards with parallel 32-bit shifts eliminates `pieceAt()` scans.
|
|
183
|
+
9. **Zero-Allocation `MoveSink` & `forEachLegalMove` Visitor**: Bulk popcounting at perft leaves and stack-buffer packed move writing (`legalMovesInto`) eliminate all move object allocations.
|
|
184
|
+
10. **Incremental Zobrist XORs**: Hashing updates in $O(1)$ per move rather than rescanning the 64 squares.
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## π GigaChess Dual-Ecosystem
|
|
189
|
+
|
|
190
|
+
GigaChess is engineered for maximum performance across the entire chess stack:
|
|
191
|
+
|
|
192
|
+
| Language & Package | Primary Environment | Performance Highlights | Repository |
|
|
193
|
+
|---|---|---|---|
|
|
194
|
+
| **`gigachess` (JS / TS)** *(this repo)* | Web frontends, Node.js, Electron, React UI | **Up to 7.4Γ faster than chess.js**, 18.5M nodes/s perft, 120,000 games/s PGN parser, built-in variation trees | [GitHub](https://github.com/itshak/gigachess) / [npm](https://www.npmjs.com/package/gigachess) |
|
|
195
|
+
| **`gigachess` (Rust)** | Native backends, search engines, database indexing | **540 Mnps** perft, 144B `Copy` board, 1.41M games/s replay, zero heap allocations | [GitHub](https://github.com/itshak/gigachess-rs) / [crates.io](https://crates.io/crates/gigachess) |
|
|
178
196
|
|
|
179
197
|
---
|
|
180
198
|
|
package/dist/attacks.d.ts
CHANGED
|
@@ -25,6 +25,9 @@ export declare const _naive: {
|
|
|
25
25
|
};
|
|
26
26
|
export declare function ray(from: number, to: number): SquareSet;
|
|
27
27
|
export declare function between(from: number, to: number): SquareSet;
|
|
28
|
+
export declare const LINE_RAY_LO: Uint32Array<ArrayBuffer>;
|
|
29
|
+
export declare const LINE_RAY_HI: Uint32Array<ArrayBuffer>;
|
|
30
|
+
export declare function lineRay(from: number, to: number): SquareSet;
|
|
28
31
|
/**
|
|
29
32
|
* All pieces of `attacker` color attacking `square`. `occ` overrides the
|
|
30
33
|
* occupancy used for slider rays (hot-loop variant: king-safety masks
|
package/dist/attacks.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"attacks.d.ts","sourceRoot":"","sources":["../src/attacks.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAiInC,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAEtD;AACD,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAEpD;AACD,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,CAElE;AAsDD;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,IAAI,OAAO,CAAC,IAAI,CAAC,CAiBvD;AAED,2EAA2E;AAC3E,wBAAgB,iBAAiB,IAAI,OAAO,CAE3C;AAGD,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,GAAG,SAAS,CAY3E;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,GAAG,SAAS,CAQzE;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,G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QAAQ,EAAE,KAAK,EACf,GAAG,GAAE,SAA0B,GAC9B,SAAS,CA4BX;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,GAAG,OAAO,CAuBtG;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,GAAG,SAAS,CAQ5F;AAGD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,GAAG,SAAS,CAE3E;AACD,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,GAAG,SAAS,CAE7E"}
|
package/dist/attacks.js
CHANGED
|
@@ -305,6 +305,50 @@ export function between(from, to) {
|
|
|
305
305
|
const idx = ((from & 63) << 6) | (to & 63);
|
|
306
306
|
return { lo: BETWEEN_LO[idx], hi: BETWEEN_HI[idx] };
|
|
307
307
|
}
|
|
308
|
+
export const LINE_RAY_LO = new Uint32Array(4096);
|
|
309
|
+
export const LINE_RAY_HI = new Uint32Array(4096);
|
|
310
|
+
for (let i = 0; i < 64; i++) {
|
|
311
|
+
const f1 = i & 7, r1 = i >> 3;
|
|
312
|
+
for (let j = 0; j < 64; j++) {
|
|
313
|
+
if (i === j)
|
|
314
|
+
continue;
|
|
315
|
+
const f2 = j & 7, r2 = j >> 3;
|
|
316
|
+
const df = f2 - f1, dr = r2 - r1;
|
|
317
|
+
if (df !== 0 && dr !== 0 && Math.abs(df) !== Math.abs(dr))
|
|
318
|
+
continue;
|
|
319
|
+
const stepF = df === 0 ? 0 : df > 0 ? 1 : -1;
|
|
320
|
+
const stepR = dr === 0 ? 0 : dr > 0 ? 1 : -1;
|
|
321
|
+
let lo = 0, hi = 0;
|
|
322
|
+
let f = f1, r = r1;
|
|
323
|
+
while (f >= 0 && f < 8 && r >= 0 && r < 8) {
|
|
324
|
+
const s = (r << 3) | f;
|
|
325
|
+
if (s < 32)
|
|
326
|
+
lo |= (1 << s) >>> 0;
|
|
327
|
+
else
|
|
328
|
+
hi |= (1 << (s - 32)) >>> 0;
|
|
329
|
+
f += stepF;
|
|
330
|
+
r += stepR;
|
|
331
|
+
}
|
|
332
|
+
f = f1 - stepF;
|
|
333
|
+
r = r1 - stepR;
|
|
334
|
+
while (f >= 0 && f < 8 && r >= 0 && r < 8) {
|
|
335
|
+
const s = (r << 3) | f;
|
|
336
|
+
if (s < 32)
|
|
337
|
+
lo |= (1 << s) >>> 0;
|
|
338
|
+
else
|
|
339
|
+
hi |= (1 << (s - 32)) >>> 0;
|
|
340
|
+
f -= stepF;
|
|
341
|
+
r -= stepR;
|
|
342
|
+
}
|
|
343
|
+
const idx = (i << 6) | j;
|
|
344
|
+
LINE_RAY_LO[idx] = lo >>> 0;
|
|
345
|
+
LINE_RAY_HI[idx] = hi >>> 0;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
export function lineRay(from, to) {
|
|
349
|
+
const idx = ((from & 63) << 6) | (to & 63);
|
|
350
|
+
return { lo: LINE_RAY_LO[idx], hi: LINE_RAY_HI[idx] };
|
|
351
|
+
}
|
|
308
352
|
// ---------- isAttacked / kingAttackers ----------
|
|
309
353
|
/**
|
|
310
354
|
* All pieces of `attacker` color attacking `square`. `occ` overrides the
|
package/dist/board.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export type Board = {
|
|
|
12
12
|
readonly king: SquareSet;
|
|
13
13
|
readonly occupied: SquareSet;
|
|
14
14
|
readonly promoted: SquareSet;
|
|
15
|
+
readonly kingSq?: readonly [number, number];
|
|
15
16
|
};
|
|
16
17
|
export declare function emptyBoard(): Board;
|
|
17
18
|
export declare function cloneBoard(board: Board): Board;
|
|
@@ -41,7 +42,17 @@ export declare function cloneBoard(board: Board): Board;
|
|
|
41
42
|
* newScratchBoard/copyBoardInto β and never escape rule 1.
|
|
42
43
|
*/
|
|
43
44
|
export type WritableBoard = {
|
|
44
|
-
|
|
45
|
+
white: sq.MutableSquareSet;
|
|
46
|
+
black: sq.MutableSquareSet;
|
|
47
|
+
pawn: sq.MutableSquareSet;
|
|
48
|
+
knight: sq.MutableSquareSet;
|
|
49
|
+
bishop: sq.MutableSquareSet;
|
|
50
|
+
rook: sq.MutableSquareSet;
|
|
51
|
+
queen: sq.MutableSquareSet;
|
|
52
|
+
king: sq.MutableSquareSet;
|
|
53
|
+
occupied: sq.MutableSquareSet;
|
|
54
|
+
promoted: sq.MutableSquareSet;
|
|
55
|
+
kingSq: [number, number];
|
|
45
56
|
};
|
|
46
57
|
export declare function newScratchBoard(): WritableBoard;
|
|
47
58
|
export declare function cloneAsWritable(board: Board): WritableBoard;
|
package/dist/board.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"board.d.ts","sourceRoot":"","sources":["../src/board.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;
|
|
1
|
+
{"version":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AAE,MAAM,GAAG,OAAO,CAE7D;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,SAAS,CAOzE;AAED,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAE9D;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAMjE;AAGD,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,GAAG,OAAO,CAavD"}
|
package/dist/board.js
CHANGED
|
@@ -14,6 +14,7 @@ export function emptyBoard() {
|
|
|
14
14
|
king: e,
|
|
15
15
|
occupied: e,
|
|
16
16
|
promoted: e,
|
|
17
|
+
kingSq: [-1, -1],
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
20
|
export function cloneBoard(board) {
|
|
@@ -29,6 +30,7 @@ export function cloneBoard(board) {
|
|
|
29
30
|
king: { lo: board.king.lo >>> 0, hi: board.king.hi >>> 0 },
|
|
30
31
|
occupied: { lo: board.occupied.lo >>> 0, hi: board.occupied.hi >>> 0 },
|
|
31
32
|
promoted: { lo: board.promoted.lo >>> 0, hi: board.promoted.hi >>> 0 },
|
|
33
|
+
kingSq: board.kingSq ? [board.kingSq[0], board.kingSq[1]] : undefined,
|
|
32
34
|
};
|
|
33
35
|
}
|
|
34
36
|
export function newScratchBoard() {
|
|
@@ -45,6 +47,7 @@ export function newScratchBoard() {
|
|
|
45
47
|
king: { lo: 0, hi: 0 },
|
|
46
48
|
occupied: { lo: 0, hi: 0 },
|
|
47
49
|
promoted: { lo: 0, hi: 0 },
|
|
50
|
+
kingSq: [-1, -1],
|
|
48
51
|
};
|
|
49
52
|
}
|
|
50
53
|
export function cloneAsWritable(board) {
|
|
@@ -61,6 +64,7 @@ export function cloneAsWritable(board) {
|
|
|
61
64
|
king: { lo: board.king.lo >>> 0, hi: board.king.hi >>> 0 },
|
|
62
65
|
occupied: { lo: board.occupied.lo >>> 0, hi: board.occupied.hi >>> 0 },
|
|
63
66
|
promoted: { lo: board.promoted.lo >>> 0, hi: board.promoted.hi >>> 0 },
|
|
67
|
+
kingSq: board.kingSq ? [board.kingSq[0], board.kingSq[1]] : [-1, -1],
|
|
64
68
|
};
|
|
65
69
|
}
|
|
66
70
|
/** Copy `src`'s ten fields into `dst`. Zero-allocation: the scratch owns its
|
|
@@ -86,6 +90,10 @@ export function copyBoardInto(dst, src) {
|
|
|
86
90
|
dst.occupied.hi = src.occupied.hi;
|
|
87
91
|
dst.promoted.lo = src.promoted.lo;
|
|
88
92
|
dst.promoted.hi = src.promoted.hi;
|
|
93
|
+
if (src.kingSq) {
|
|
94
|
+
dst.kingSq[0] = src.kingSq[0];
|
|
95
|
+
dst.kingSq[1] = src.kingSq[1];
|
|
96
|
+
}
|
|
89
97
|
}
|
|
90
98
|
/** In-place: remove the piece (any color/role) at sqIdx. Hot-loop only.
|
|
91
99
|
* Zero-allocation: raw bitmasks instead of sq.not/singleton/and objects. */
|
|
@@ -116,6 +124,12 @@ export function clearSquareInPlace(b, sqIdx) {
|
|
|
116
124
|
b.occupied.hi &= inv;
|
|
117
125
|
b.promoted.hi &= inv;
|
|
118
126
|
}
|
|
127
|
+
if (b.kingSq) {
|
|
128
|
+
if (b.kingSq[0] === sqIdx)
|
|
129
|
+
b.kingSq[0] = -1;
|
|
130
|
+
else if (b.kingSq[1] === sqIdx)
|
|
131
|
+
b.kingSq[1] = -1;
|
|
132
|
+
}
|
|
119
133
|
}
|
|
120
134
|
/** In-place: place a piece at sqIdx. Hot-loop only. Zero-allocation. */
|
|
121
135
|
export function putPieceInPlace(b, sqIdx, piece) {
|
|
@@ -175,6 +189,9 @@ export function putPieceInPlace(b, sqIdx, piece) {
|
|
|
175
189
|
}
|
|
176
190
|
b.occupied.hi |= bit;
|
|
177
191
|
}
|
|
192
|
+
if (piece.role === Role.King && b.kingSq) {
|
|
193
|
+
b.kingSq[piece.color] = sqIdx;
|
|
194
|
+
}
|
|
178
195
|
}
|
|
179
196
|
export function setPiece(board, sqIdx, piece) {
|
|
180
197
|
const nb = cloneAsWritable(board);
|
|
@@ -212,6 +229,11 @@ export function hasPiece(board, sqIdx) {
|
|
|
212
229
|
return sq.has(board.occupied, sqIdx);
|
|
213
230
|
}
|
|
214
231
|
export function kingSquare(board, color) {
|
|
232
|
+
if (board.kingSq !== undefined) {
|
|
233
|
+
const k = board.kingSq[color];
|
|
234
|
+
if (k >= 0)
|
|
235
|
+
return k;
|
|
236
|
+
}
|
|
215
237
|
const ks = color === Color.White ? sq.and(board.white, board.king) : sq.and(board.black, board.king);
|
|
216
238
|
return sq.first(ks);
|
|
217
239
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { CastlingRights, Position } from "./types.js";
|
|
2
|
+
import type { CastlingPlan } from "./chess.js";
|
|
3
|
+
export declare const CASTLE_WK = 1;
|
|
4
|
+
export declare const CASTLE_WQ = 2;
|
|
5
|
+
export declare const CASTLE_BK = 4;
|
|
6
|
+
export declare const CASTLE_BQ = 8;
|
|
7
|
+
/**
|
|
8
|
+
* Precomputed 64-entry lookup table for single-cycle rights clearing.
|
|
9
|
+
* Clears rights when king or rook moves or is captured on standard squares.
|
|
10
|
+
*/
|
|
11
|
+
export declare const CASTLE_CLEAR_STD: Uint8Array<ArrayBuffer>;
|
|
12
|
+
/** Standard castling plans (immutable/frozen singletons) */
|
|
13
|
+
export declare const PLAN_WHITE_K: CastlingPlan;
|
|
14
|
+
export declare const PLAN_WHITE_Q: CastlingPlan;
|
|
15
|
+
export declare const PLAN_BLACK_K: CastlingPlan;
|
|
16
|
+
export declare const PLAN_BLACK_Q: CastlingPlan;
|
|
17
|
+
/** Precomputed 16-entry FEN castling string lookup table */
|
|
18
|
+
export declare const CASTLING_FEN_STR: readonly string[];
|
|
19
|
+
/** Precomputed 16-entry CastlingRights table for standard chess (zero allocations) */
|
|
20
|
+
export declare const CASTLING_RIGHTS_TABLE: readonly CastlingRights[];
|
|
21
|
+
export declare function getCastlingMask(pos: Position): number;
|
|
22
|
+
/**
|
|
23
|
+
* Precomputed 128-entry tables for rank-0 (White: 0..63) and rank-7 (Black: 64..127)
|
|
24
|
+
* castling path clearance in Chess960 and standard chess.
|
|
25
|
+
* Single-cycle check: ((CASTLE_PATH_LO[idx] & occ.lo) | (CASTLE_PATH_HI[idx] & occ.hi)) === 0
|
|
26
|
+
*/
|
|
27
|
+
export declare const CASTLE_PATH_LO: Uint32Array<ArrayBuffer>;
|
|
28
|
+
export declare const CASTLE_PATH_HI: Uint32Array<ArrayBuffer>;
|
|
29
|
+
export declare const CASTLE_TRAVERSAL_WHITE: (readonly number[])[];
|
|
30
|
+
export declare const CASTLE_TRAVERSAL_BLACK: (readonly number[])[];
|
|
31
|
+
//# sourceMappingURL=castling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"castling.d.ts","sourceRoot":"","sources":["../src/castling.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,eAAO,MAAM,SAAS,IAAI,CAAC;AAC3B,eAAO,MAAM,SAAS,IAAI,CAAC;AAC3B,eAAO,MAAM,SAAS,IAAI,CAAC;AAC3B,eAAO,MAAM,SAAS,IAAI,CAAC;AAE3B;;;GAGG;AACH,eAAO,MAAM,gBAAgB,yBAAqB,CAAC;AASnD,4DAA4D;AAC5D,eAAO,MAAM,YAAY,EAAE,YAMzB,CAAC;AAEH,eAAO,MAAM,YAAY,EAAE,YAMzB,CAAC;AAEH,eAAO,MAAM,YAAY,EAAE,YAMzB,CAAC;AAEH,eAAO,MAAM,YAAY,EAAE,YAMzB,CAAC;AAEH,4DAA4D;AAC5D,eAAO,MAAM,gBAAgB,EAAE,SAAS,MAAM,EAG7C,CAAC;AAaF,sFAAsF;AACtF,eAAO,MAAM,qBAAqB,EAAE,SAAS,cAAc,EAcvD,CAAC;AAEL,wBAAgB,eAAe,CAAC,GAAG,EAAE,QAAQ,GAAG,MAAM,CAQrD;AAED;;;;GAIG;AACH,eAAO,MAAM,cAAc,0BAAuB,CAAC;AACnD,eAAO,MAAM,cAAc,0BAAuB,CAAC;AAEnD,eAAO,MAAM,sBAAsB,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,EAAkB,CAAC;AAC3E,eAAO,MAAM,sBAAsB,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,EAAkB,CAAC"}
|
package/dist/castling.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
export const CASTLE_WK = 1; // 0b0001
|
|
2
|
+
export const CASTLE_WQ = 2; // 0b0010
|
|
3
|
+
export const CASTLE_BK = 4; // 0b0100
|
|
4
|
+
export const CASTLE_BQ = 8; // 0b1000
|
|
5
|
+
/**
|
|
6
|
+
* Precomputed 64-entry lookup table for single-cycle rights clearing.
|
|
7
|
+
* Clears rights when king or rook moves or is captured on standard squares.
|
|
8
|
+
*/
|
|
9
|
+
export const CASTLE_CLEAR_STD = new Uint8Array(64);
|
|
10
|
+
CASTLE_CLEAR_STD.fill(0x0F);
|
|
11
|
+
CASTLE_CLEAR_STD[4] = 0x0C; // e1 (White King) -> clears WK | WQ (12)
|
|
12
|
+
CASTLE_CLEAR_STD[60] = 0x03; // e8 (Black King) -> clears BK | BQ (3)
|
|
13
|
+
CASTLE_CLEAR_STD[7] = 0x0E; // h1 (White King Rook) -> clears WK (14)
|
|
14
|
+
CASTLE_CLEAR_STD[0] = 0x0D; // a1 (White Queen Rook) -> clears WQ (13)
|
|
15
|
+
CASTLE_CLEAR_STD[63] = 0x0B; // h8 (Black King Rook) -> clears BK (11)
|
|
16
|
+
CASTLE_CLEAR_STD[56] = 0x07; // a8 (Black Queen Rook) -> clears BQ (7)
|
|
17
|
+
/** Standard castling plans (immutable/frozen singletons) */
|
|
18
|
+
export const PLAN_WHITE_K = Object.freeze({
|
|
19
|
+
side: "king",
|
|
20
|
+
kingFrom: 4,
|
|
21
|
+
kingTo: 6,
|
|
22
|
+
rookFrom: 7,
|
|
23
|
+
rookTo: 5,
|
|
24
|
+
});
|
|
25
|
+
export const PLAN_WHITE_Q = Object.freeze({
|
|
26
|
+
side: "queen",
|
|
27
|
+
kingFrom: 4,
|
|
28
|
+
kingTo: 2,
|
|
29
|
+
rookFrom: 0,
|
|
30
|
+
rookTo: 3,
|
|
31
|
+
});
|
|
32
|
+
export const PLAN_BLACK_K = Object.freeze({
|
|
33
|
+
side: "king",
|
|
34
|
+
kingFrom: 60,
|
|
35
|
+
kingTo: 62,
|
|
36
|
+
rookFrom: 63,
|
|
37
|
+
rookTo: 61,
|
|
38
|
+
});
|
|
39
|
+
export const PLAN_BLACK_Q = Object.freeze({
|
|
40
|
+
side: "queen",
|
|
41
|
+
kingFrom: 60,
|
|
42
|
+
kingTo: 58,
|
|
43
|
+
rookFrom: 56,
|
|
44
|
+
rookTo: 59,
|
|
45
|
+
});
|
|
46
|
+
/** Precomputed 16-entry FEN castling string lookup table */
|
|
47
|
+
export const CASTLING_FEN_STR = [
|
|
48
|
+
"-", "K", "Q", "KQ", "k", "Kk", "Qk", "KQk",
|
|
49
|
+
"q", "Kq", "Qq", "KQq", "kq", "Kkq", "Qkq", "KQkq"
|
|
50
|
+
];
|
|
51
|
+
const S0 = new Set();
|
|
52
|
+
const S7 = new Set([7]);
|
|
53
|
+
const S0_2 = new Set([0]);
|
|
54
|
+
const S0_7 = new Set([0, 7]);
|
|
55
|
+
const WHITE_SETS = [S0, S7, S0_2, S0_7];
|
|
56
|
+
const S63 = new Set([63]);
|
|
57
|
+
const S56 = new Set([56]);
|
|
58
|
+
const S56_63 = new Set([56, 63]);
|
|
59
|
+
const BLACK_SETS = [S0, S63, S56, S56_63];
|
|
60
|
+
/** Precomputed 16-entry CastlingRights table for standard chess (zero allocations) */
|
|
61
|
+
export const CASTLING_RIGHTS_TABLE = (() => {
|
|
62
|
+
const table = [];
|
|
63
|
+
for (let mask = 0; mask < 16; mask++) {
|
|
64
|
+
table.push({
|
|
65
|
+
white: WHITE_SETS[mask & 3],
|
|
66
|
+
black: BLACK_SETS[(mask >> 2) & 3],
|
|
67
|
+
whiteKing: (mask & CASTLE_WK) !== 0,
|
|
68
|
+
whiteQueen: (mask & CASTLE_WQ) !== 0,
|
|
69
|
+
blackKing: (mask & CASTLE_BK) !== 0,
|
|
70
|
+
blackQueen: (mask & CASTLE_BQ) !== 0,
|
|
71
|
+
mask,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return table;
|
|
75
|
+
})();
|
|
76
|
+
export function getCastlingMask(pos) {
|
|
77
|
+
if (pos.castlingMask !== undefined)
|
|
78
|
+
return pos.castlingMask;
|
|
79
|
+
let mask = 0;
|
|
80
|
+
if (pos.castling.white.has(7))
|
|
81
|
+
mask |= CASTLE_WK;
|
|
82
|
+
if (pos.castling.white.has(0))
|
|
83
|
+
mask |= CASTLE_WQ;
|
|
84
|
+
if (pos.castling.black.has(63))
|
|
85
|
+
mask |= CASTLE_BK;
|
|
86
|
+
if (pos.castling.black.has(56))
|
|
87
|
+
mask |= CASTLE_BQ;
|
|
88
|
+
return mask;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Precomputed 128-entry tables for rank-0 (White: 0..63) and rank-7 (Black: 64..127)
|
|
92
|
+
* castling path clearance in Chess960 and standard chess.
|
|
93
|
+
* Single-cycle check: ((CASTLE_PATH_LO[idx] & occ.lo) | (CASTLE_PATH_HI[idx] & occ.hi)) === 0
|
|
94
|
+
*/
|
|
95
|
+
export const CASTLE_PATH_LO = new Uint32Array(128);
|
|
96
|
+
export const CASTLE_PATH_HI = new Uint32Array(128);
|
|
97
|
+
export const CASTLE_TRAVERSAL_WHITE = new Array(64);
|
|
98
|
+
export const CASTLE_TRAVERSAL_BLACK = new Array(64);
|
|
99
|
+
for (let kFile = 0; kFile < 8; kFile++) {
|
|
100
|
+
for (let rFile = 0; rFile < 8; rFile++) {
|
|
101
|
+
const idx = (kFile << 3) | rFile;
|
|
102
|
+
if (kFile === rFile) {
|
|
103
|
+
CASTLE_TRAVERSAL_WHITE[idx] = [];
|
|
104
|
+
CASTLE_TRAVERSAL_BLACK[idx] = [];
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const isKingSide = rFile > kFile;
|
|
108
|
+
const kLanding = isKingSide ? 6 : 2;
|
|
109
|
+
const rLanding = isKingSide ? 5 : 3;
|
|
110
|
+
// Path clearance: all squares between king/rook and their landings must be empty,
|
|
111
|
+
// excluding initial king and rook squares.
|
|
112
|
+
let rank0Mask = 0;
|
|
113
|
+
const minK = Math.min(kFile, kLanding);
|
|
114
|
+
const maxK = Math.max(kFile, kLanding);
|
|
115
|
+
const minR = Math.min(rFile, rLanding);
|
|
116
|
+
const maxR = Math.max(rFile, rLanding);
|
|
117
|
+
for (let f = 0; f < 8; f++) {
|
|
118
|
+
if (f === kFile || f === rFile)
|
|
119
|
+
continue;
|
|
120
|
+
if ((f >= minK && f <= maxK) || (f >= minR && f <= maxR)) {
|
|
121
|
+
rank0Mask |= (1 << f) >>> 0;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// White (index 0..63): rank 0 in lo
|
|
125
|
+
CASTLE_PATH_LO[idx] = rank0Mask >>> 0;
|
|
126
|
+
CASTLE_PATH_HI[idx] = 0;
|
|
127
|
+
// Black (index 64..127): rank 7 in hi (shifted by 24)
|
|
128
|
+
CASTLE_PATH_LO[64 + idx] = 0;
|
|
129
|
+
CASTLE_PATH_HI[64 + idx] = (rank0Mask << 24) >>> 0;
|
|
130
|
+
// Traversal squares: squares king traverses towards landing (excluding kFile)
|
|
131
|
+
const travW = [];
|
|
132
|
+
const travB = [];
|
|
133
|
+
if (kLanding !== kFile) {
|
|
134
|
+
const step = kLanding > kFile ? 1 : -1;
|
|
135
|
+
for (let f = kFile + step;; f += step) {
|
|
136
|
+
travW.push(f);
|
|
137
|
+
travB.push(56 + f);
|
|
138
|
+
if (f === kLanding)
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
CASTLE_TRAVERSAL_WHITE[idx] = travW;
|
|
143
|
+
CASTLE_TRAVERSAL_BLACK[idx] = travB;
|
|
144
|
+
}
|
|
145
|
+
}
|
package/dist/chess.d.ts
CHANGED
|
@@ -40,8 +40,8 @@ type CheckContext = {
|
|
|
40
40
|
checkMask: SquareSet;
|
|
41
41
|
/** squares the king may move to (not attacked with our king removed from occupancy) */
|
|
42
42
|
kingSafe: SquareSet;
|
|
43
|
-
/** pinned own
|
|
44
|
-
|
|
43
|
+
/** pinned own pieces bitmask */
|
|
44
|
+
pinned: SquareSet;
|
|
45
45
|
/**
|
|
46
46
|
* Castling plans for the side to move (empty when no rights / kingless),
|
|
47
47
|
* computed once per position so the hot loops never re-run detectCastling
|
|
@@ -65,6 +65,15 @@ export declare function isLegal(pos: Position, move: Move): boolean;
|
|
|
65
65
|
export declare function isCheckmate(pos: Position): boolean;
|
|
66
66
|
export declare function isStalemate(pos: Position): boolean;
|
|
67
67
|
export declare function perft(pos: Position, depth: number): number;
|
|
68
|
+
export declare class MoveCounter {
|
|
69
|
+
count: number;
|
|
70
|
+
add(set: SquareSet): void;
|
|
71
|
+
addPawns(legal: SquareSet, isWhite: boolean): void;
|
|
72
|
+
reset(): void;
|
|
73
|
+
}
|
|
74
|
+
export declare function countLegalMoves(pos: Position): number;
|
|
75
|
+
export declare function legalMovesInto(pos: Position, out: Uint16Array | Uint32Array): number;
|
|
76
|
+
export declare function forEachLegalMove(pos: Position, fn: (from: number, to: number, promo: number) => void): void;
|
|
68
77
|
export declare const INITIAL_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
69
78
|
/** Baseline-style color name. */
|
|
70
79
|
export type ColorName = "w" | "b";
|
|
@@ -84,12 +93,15 @@ export type VerboseMove = {
|
|
|
84
93
|
before: string;
|
|
85
94
|
after: string;
|
|
86
95
|
};
|
|
87
|
-
type
|
|
88
|
-
before: Position;
|
|
89
|
-
after: Position;
|
|
90
|
-
move: Move;
|
|
91
|
-
san: string;
|
|
96
|
+
export type Undo = {
|
|
97
|
+
readonly before: Position;
|
|
98
|
+
readonly after: Position;
|
|
99
|
+
readonly move: Move;
|
|
100
|
+
readonly san: string;
|
|
101
|
+
readonly prev_checkers?: SquareSet;
|
|
102
|
+
readonly prev_zobrist?: ZobristKey;
|
|
92
103
|
};
|
|
104
|
+
export type HistoryEntry = Undo;
|
|
93
105
|
/**
|
|
94
106
|
* Unified root `Chess` (design D1): the chess.js-ergonomic mutable faΓ§ade
|
|
95
107
|
* over the immutable engine core, extended with the high-performance
|
|
@@ -143,6 +155,7 @@ export declare class Chess {
|
|
|
143
155
|
/** Reverts the last move; returns it in verbose shape, or null if none. */
|
|
144
156
|
undo(): VerboseMove | null;
|
|
145
157
|
isCheck(): boolean;
|
|
158
|
+
inCheck(): boolean;
|
|
146
159
|
isCheckmate(): boolean;
|
|
147
160
|
isStalemate(): boolean;
|
|
148
161
|
/** Draw by insufficient material, the fifty-move rule, or threefold repetition. */
|
|
@@ -198,6 +211,7 @@ export declare class Chess {
|
|
|
198
211
|
get startFen(): string;
|
|
199
212
|
/** Live engine position (read-only view over the internal state). */
|
|
200
213
|
get pos(): Position;
|
|
214
|
+
get _pos(): Position;
|
|
201
215
|
/** Internal history log (read-only view; used by toTree). */
|
|
202
216
|
get historyEntries(): readonly HistoryEntry[];
|
|
203
217
|
/**
|
package/dist/chess.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chess.d.ts","sourceRoot":"","sources":["../src/chess.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAIhD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAE,KAAK,EAAE,IAAI,EAA0B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