-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathnnue_training_data_formats.h
More file actions
781 lines (638 loc) · 23.4 KB
/
nnue_training_data_formats.h
File metadata and controls
781 lines (638 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
Copyright 2020 Tomasz Sobczyk
Permission is hereby granted, free of charge,
to any person obtaining a copy of this software
and associated documentation files (the "Software"),
to deal in the Software without restriction,
including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall
be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <bitset>
#include <cstdio>
#include <cassert>
#include <string>
#include <string_view>
#include <vector>
#include <memory>
#include <fstream>
#include <cstring>
#include <iostream>
#include <set>
#include <cstdio>
#include <cassert>
#include <array>
#include <limits>
#include <climits>
#include <optional>
#include <thread>
#include <mutex>
#include <random>
#include "rng.h"
#if (defined(_MSC_VER) || defined(__INTEL_COMPILER)) && !defined(__clang__)
#include <intrin.h>
#endif
#include "../variant.h"
static_assert(DATA_SIZE % 8 == 0);
namespace chess
{
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#define FORCEINLINE __attribute__((always_inline))
#elif defined(_MSC_VER)
// NOTE: for some reason it breaks the profiler a little
// keep it on only when not profiling.
//#define FORCEINLINE __forceinline
#define FORCEINLINE
#else
#define FORCEINLINE inline
#endif
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#define NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define NOINLINE __declspec(noinline)
#else
#define NOINLINE
#endif
enum struct Color : std::uint8_t
{
White,
Black,
NB
};
enum struct PieceType : std::uint8_t
{
Pawn,
Knight,
Bishop,
Rook,
Queen,
MaxPiece = PIECE_TYPES - 1,
King = MaxPiece + (KING_SQUARES == 1),
None,
NB = King + 1
};
enum struct Piece : std::uint8_t
{
White = 0,
Black = static_cast<uint8_t>(PieceType::King) + 1,
Max = Black + static_cast<uint8_t>(PieceType::King),
None,
NB
};
constexpr Color color_of(Piece p) {
return p >= Piece::Black ? Color::Black : Color::White;
}
constexpr PieceType type_of(Piece p) {
return static_cast<PieceType>(p >= Piece::Black ? static_cast<uint8_t>(p) - static_cast<uint8_t>(Piece::Black) : static_cast<uint8_t>(p));
}
constexpr Piece make_piece(PieceType pt, Color c) {
return static_cast<Piece>(static_cast<uint8_t>(c == Color::Black) * static_cast<uint8_t>(Piece::Black) + static_cast<uint8_t>(pt));
}
enum struct Rank : std::int8_t // make sure that it does not underflow in loop
{
RANK_1,
RANK_2,
RANK_3,
RANK_4,
RANK_5,
RANK_6,
RANK_7,
RANK_8,
RANK_MAX = RANKS - 1,
RANK_NB,
};
enum struct File : std::uint8_t
{
FILE_A,
FILE_B,
FILE_C,
FILE_D,
FILE_E,
FILE_F,
FILE_G,
FILE_H,
FILE_MAX = FILES - 1,
FILE_NB
};
enum struct Square : std::uint8_t
{
MIN = 0,
NB = std::uint8_t(Rank::RANK_NB) * std::uint8_t(File::FILE_NB),
MAX = NB - 1,
KNB = KING_SQUARES,
};
inline Square make_square(File f, Rank r) {
return Square(std::uint8_t(File::FILE_NB) * std::uint8_t(r) + std::uint8_t(f));
}
inline Rank file_of(Square s) {
return Rank(std::uint8_t(s) % std::uint8_t(File::FILE_NB));
}
inline Rank rank_of(Square s) {
return Rank(std::uint8_t(s) / std::uint8_t(File::FILE_NB));
}
inline Square flip_horizontally(Square s) {
return Square(std::uint8_t(s) + std::uint8_t(File::FILE_MAX) - 2 * std::uint8_t(file_of(s)));
}
inline Square flip_vertically(Square s) {
return Square(std::int8_t(s) + (std::int8_t(Rank::RANK_MAX) - 2 * std::int8_t(rank_of(s))) * std::int8_t(File::FILE_NB));
}
#define ENABLE_INCR_OPERATORS_ON(T) \
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
inline T& operator--(T& d) { return d = T(int(d) - 1); }
ENABLE_INCR_OPERATORS_ON(Square);
ENABLE_INCR_OPERATORS_ON(File);
ENABLE_INCR_OPERATORS_ON(Rank);
ENABLE_INCR_OPERATORS_ON(PieceType);
enum struct MoveType : std::uint8_t
{
Normal,
Promotion,
Castle,
EnPassant
};
enum struct CastleType : std::uint8_t
{
Short,
Long
};
[[nodiscard]] constexpr CastleType operator!(CastleType ct)
{
return static_cast<CastleType>(static_cast<std::uint8_t>(ct) ^ 1);
}
// castling is encoded as a king capturing rook
// ep is encoded as a normal pawn capture (move.to is empty on the board)
struct Move
{
Square from;
Square to;
MoveType type = MoveType::Normal;
Piece promotedPiece = Piece::None;
[[nodiscard]] constexpr friend bool operator==(const Move& lhs, const Move& rhs) noexcept
{
return lhs.from == rhs.from
&& lhs.to == rhs.to
&& lhs.type == rhs.type
&& lhs.promotedPiece == rhs.promotedPiece;
}
[[nodiscard]] constexpr friend bool operator!=(const Move& lhs, const Move& rhs) noexcept
{
return !(lhs == rhs);
}
};
static_assert(sizeof(Move) == 4);
enum struct CastlingRights : std::uint8_t
{
None = 0x0,
WhiteKingSide = 0x1,
WhiteQueenSide = 0x2,
BlackKingSide = 0x4,
BlackQueenSide = 0x8,
White = WhiteKingSide | WhiteQueenSide,
Black = BlackKingSide | BlackQueenSide,
All = WhiteKingSide | WhiteQueenSide | BlackKingSide | BlackQueenSide
};
[[nodiscard]] constexpr CastlingRights operator|(CastlingRights lhs, CastlingRights rhs)
{
return static_cast<CastlingRights>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
}
[[nodiscard]] constexpr CastlingRights operator&(CastlingRights lhs, CastlingRights rhs)
{
return static_cast<CastlingRights>(static_cast<std::uint8_t>(lhs) & static_cast<std::uint8_t>(rhs));
}
[[nodiscard]] constexpr CastlingRights operator~(CastlingRights lhs)
{
return static_cast<CastlingRights>(~static_cast<std::uint8_t>(lhs) & static_cast<std::uint8_t>(CastlingRights::All));
}
constexpr CastlingRights& operator|=(CastlingRights& lhs, CastlingRights rhs)
{
lhs = static_cast<CastlingRights>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
return lhs;
}
constexpr CastlingRights& operator&=(CastlingRights& lhs, CastlingRights rhs)
{
lhs = static_cast<CastlingRights>(static_cast<std::uint8_t>(lhs) & static_cast<std::uint8_t>(rhs));
return lhs;
}
// checks whether lhs contains rhs
[[nodiscard]] constexpr bool contains(CastlingRights lhs, CastlingRights rhs)
{
return (lhs & rhs) == rhs;
}
struct Board
{
Board() noexcept :
m_pieces{},
m_pocketCount{},
m_pieceCount{},
m_kings{}
{
std::fill_n(m_pieces, static_cast<uint8_t>(Square::NB), Piece::None);
}
constexpr void place(Piece piece, Square sq)
{
if (type_of(piece) == PieceType::King)
{
m_kings[static_cast<uint8_t>(color_of(piece))] = sq;
assert(sq != Square::NB || KING_SQUARES == 1);
if (sq == Square::NB)
// No king
return;
}
auto oldPiece = m_pieces[static_cast<uint8_t>(sq)];
m_pieces[static_cast<uint8_t>(sq)] = piece;
if (oldPiece != Piece::None)
--m_pieceCount;
if (piece != Piece::None)
++m_pieceCount;
}
constexpr void setHandCount(Piece piece, uint8_t count)
{
m_pocketCount[static_cast<uint8_t>(piece)] = count;
}
constexpr int getHandCount(Piece piece) const
{
return m_pocketCount[static_cast<uint8_t>(piece)];
}
[[nodiscard]] constexpr Piece pieceAt(Square sq) const
{
return m_pieces[static_cast<uint8_t>(sq)];
}
[[nodiscard]] inline Square kingSquare(Color c) const
{
return m_kings[static_cast<uint8_t>(c)];
}
[[nodiscard]] constexpr std::uint8_t pieceCount() const
{
return m_pieceCount;
}
const Piece* piecesRaw() const;
private:
Piece m_pieces[(unsigned int)(Square::NB)];
uint8_t m_pocketCount[(unsigned int)(Piece::NB)];
uint8_t m_pieceCount;
Square m_kings[(unsigned int)(Color::NB)];
};
struct Position : public Board
{
using BaseType = Board;
Position() noexcept :
Board(),
m_sideToMove(Color::White),
m_epSquare(Square::NB),
m_castlingRights(CastlingRights::All),
m_rule50Counter(0),
m_ply(0)
{
}
constexpr Position(const Board& board, const Board& pocket, Color sideToMove, Square epSquare, CastlingRights castlingRights) :
Board(board),
m_sideToMove(sideToMove),
m_epSquare(epSquare),
m_castlingRights(castlingRights),
m_rule50Counter(0),
m_ply(0)
{
}
void setEpSquare(Square sq)
{
m_epSquare = sq;
}
constexpr void setSideToMove(Color color)
{
m_sideToMove = color;
}
constexpr void addCastlingRights(CastlingRights rights)
{
m_castlingRights |= rights;
}
constexpr void setCastlingRights(CastlingRights rights)
{
m_castlingRights = rights;
}
constexpr void setRule50Counter(std::uint8_t v)
{
m_rule50Counter = v;
}
constexpr void setPly(std::uint16_t ply)
{
m_ply = ply;
}
inline void setFullMove(std::uint16_t hm)
{
m_ply = 2 * hm - 1 + (m_sideToMove == Color::Black);
}
[[nodiscard]] constexpr Color sideToMove() const
{
return m_sideToMove;
}
[[nodiscard]] inline std::uint8_t rule50Counter() const
{
return m_rule50Counter;
}
[[nodiscard]] inline std::uint16_t ply() const
{
return m_ply;
}
[[nodiscard]] inline std::uint16_t fullMove() const
{
return (m_ply + 1) / 2;
}
protected:
Color m_sideToMove;
Square m_epSquare;
CastlingRights m_castlingRights;
std::uint8_t m_rule50Counter;
std::uint16_t m_ply;
static_assert(sizeof(Color) + sizeof(Square) + sizeof(CastlingRights) + sizeof(std::uint8_t) == 4);
};
}
namespace bin
{
constexpr std::size_t KiB = 1024;
constexpr std::size_t MiB = (1024*KiB);
constexpr std::size_t GiB = (1024*MiB);
constexpr std::size_t suggestedChunkSize = MiB;
constexpr std::size_t maxMovelistSize = 10*KiB; // a safe upper bound
constexpr std::size_t maxChunkSize = 100*MiB; // to prevent malformed files from causing huge allocations
using namespace std::literals;
namespace nodchip
{
// This namespace contains modified code from https://github.com/nodchip/Stockfish
// which is released under GPL v3 license https://www.gnu.org/licenses/gpl-3.0.html
using namespace std;
struct StockfishMove
{
[[nodiscard]] chess::Move toMove() const
{
const chess::Square to = static_cast<chess::Square>((m_raw & (0b111111 << 0) >> 0));
const chess::Square from = static_cast<chess::Square>((m_raw & (0b111111 << 6)) >> 6);
const unsigned promotionIndex = (m_raw & (0b11 << 12)) >> 12;
const chess::PieceType promotionType = static_cast<chess::PieceType>(static_cast<int>(chess::PieceType::Knight) + promotionIndex);
const unsigned moveFlag = (m_raw & (0b11 << 14)) >> 14;
chess::MoveType type = chess::MoveType::Normal;
if (moveFlag == 1) type = chess::MoveType::Promotion;
else if (moveFlag == 2) type = chess::MoveType::EnPassant;
else if (moveFlag == 3) type = chess::MoveType::Castle;
if (type == chess::MoveType::Promotion)
{
const chess::Color stm = rank_of(to) >= chess::Rank::RANK_5 ? chess::Color::White : chess::Color::Black;
return chess::Move{from, to, type, make_piece(promotionType, stm)};
}
return chess::Move{from, to, type};
}
private:
std::uint16_t m_raw;
};
static_assert(sizeof(StockfishMove) == sizeof(std::uint16_t));
struct PackedSfen
{
uint8_t data[DATA_SIZE / 8];
};
struct PackedSfenValue
{
// phase
PackedSfen sfen;
// Evaluation value returned from Learner::search()
int16_t score;
// PV first move
// Used when finding the match rate with the teacher
StockfishMove move;
// Trouble of the phase from the initial phase.
uint16_t gamePly;
// 1 if the player on this side ultimately wins the game. -1 if you are losing.
// 0 if a draw is reached.
// The draw is in the teacher position generation command gensfen,
// Only write if LEARN_GENSFEN_DRAW_RESULT is enabled.
int8_t game_result;
// When exchanging the file that wrote the teacher aspect with other people
//Because this structure size is not fixed, pad it so that it is 40 bytes in any environment.
uint8_t padding;
// 32 + 2 + 2 + 2 + 1 + 1 = 40bytes
};
static_assert(sizeof(PackedSfenValue) == DATA_SIZE / 8 + 8);
// Class that handles bitstream
// useful when doing aspect encoding
struct BitStream
{
// Set the memory to store the data in advance.
// Assume that memory is cleared to 0.
void set_data(uint8_t* data_) { data = data_; reset(); }
// Get the pointer passed in set_data().
uint8_t* get_data() const { return data; }
// Get the cursor.
int get_cursor() const { return bit_cursor; }
// reset the cursor
void reset() { bit_cursor = 0; }
// Get 1 bit from the stream.
int read_one_bit()
{
int b = (data[bit_cursor / 8] >> (bit_cursor & 7)) & 1;
++bit_cursor;
return b;
}
// read n bits of data
// Reverse conversion of write_n_bit().
int read_n_bit(int n)
{
int result = 0;
for (int i = 0; i < n; ++i)
result |= read_one_bit() ? (1 << i) : 0;
return result;
}
private:
// Next bit position to read/write.
int bit_cursor;
// data entity
uint8_t* data;
};
// Huffman coding
// * is simplified from mini encoding to make conversion easier.
//
// Huffman Encoding
//
// Empty xxxxxxx0
// Pawn xxxxx001 + 1 bit (Color)
// Knight xxxxx011 + 1 bit (Color)
// Bishop xxxxx101 + 1 bit (Color)
// Rook xxxxx111 + 1 bit (Color)
// Queen xxxx1001 + 1 bit (Color)
//
// Worst case:
// - 32 empty squares 32 bits
// - 30 pieces 150 bits
// - 2 kings 12 bits
// - castling rights 4 bits
// - ep square 7 bits
// - rule50 7 bits
// - game ply 16 bits
// - TOTAL 228 bits < 256 bits
struct HuffmanedPiece
{
int code; // how it will be coded
int bits; // How many bits do you have
};
// NOTE: Order adjusted for this library because originally NO_PIECE had index 0
constexpr HuffmanedPiece huffman_table[] =
{
{0b00000,1}, // NO_PIECE
{0b00001,5}, // PAWN
{0b00011,5}, // KNIGHT
{0b00101,5}, // BISHOP
{0b00111,5}, // ROOK
{0b01001,5}, // QUEEN
{0b01011,5}, //
{0b01101,5}, //
{0b01111,5}, //
{0b10001,5}, //
{0b10011,5}, //
{0b10101,5}, //
{0b10111,5}, //
{0b11001,5}, //
{0b11011,5}, //
{0b11101,5}, //
{0b11111,5}, //
};
// Class for compressing/decompressing sfen
// sfen can be packed to 256bit (32bytes) by Huffman coding.
// This is proven by mini. The above is Huffman coding.
//
// Internal format = 1-bit turn + 7-bit king position *2 + piece on board (Huffman coding) + hand piece (Huffman coding)
// Side to move (White = 0, Black = 1) (1bit)
// White King Position (6 bits)
// Black King Position (6 bits)
// Huffman Encoding of the board
// Castling availability (1 bit x 4)
// En passant square (1 or 1 + 6 bits)
// Rule 50 (6 bits)
// Game play (8 bits)
//
// TODO(someone): Rename SFEN to FEN.
//
struct SfenPacker
{
// sfen packed by pack() (256bit = 32bytes)
// Or sfen to decode with unpack()
uint8_t *data; // uint8_t[32];
BitStream stream;
// Read one board piece from stream
[[nodiscard]] chess::Piece read_board_piece_from_stream()
{
int pr = 0;
int code = 0, bits = 0;
while (true)
{
code |= stream.read_one_bit() << bits;
++bits;
assert(bits <= 6);
for (pr = 0; pr <= static_cast<int>(chess::PieceType::None); ++pr)
if (huffman_table[pr].code == code
&& huffman_table[pr].bits == bits)
goto Found;
}
Found:;
if (pr == 0)
return chess::Piece::None;
// first and second flag
chess::Color c = (chess::Color)stream.read_one_bit();
return make_piece(static_cast<chess::PieceType>(pr - 1), c);
}
};
[[nodiscard]] inline chess::Position pos_from_packed_sfen(const PackedSfen& sfen)
{
SfenPacker packer;
auto& stream = packer.stream;
stream.set_data(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&sfen)));
chess::Position pos{};
// Active color
pos.setSideToMove((chess::Color)stream.read_one_bit());
// First the position of the ball
pos.place(make_piece(chess::PieceType::King, chess::Color::White), static_cast<chess::Square>(stream.read_n_bit(7)));
pos.place(make_piece(chess::PieceType::King, chess::Color::Black), static_cast<chess::Square>(stream.read_n_bit(7)));
// Piece placement
for (chess::Rank r = chess::Rank::RANK_MAX; r >= chess::Rank::RANK_1; --r)
{
for (chess::File f = chess::File::FILE_A; f <= chess::File::FILE_MAX; ++f)
{
auto sq = make_square(f, r);
if (type_of(pos.pieceAt(sq)) != chess::PieceType::King)
{
assert(pos.pieceAt(sq) == chess::Piece::None);
chess::Piece pc = packer.read_board_piece_from_stream();
// There may be no pieces, so skip in that case.
if (pc != chess::Piece::None)
pos.place(pc, sq);
}
assert(stream.get_cursor() <= DATA_SIZE);
}
}
for (chess::Color c : { chess::Color::White, chess::Color::Black })
for (chess::PieceType pt = chess::PieceType::Pawn; pt <= chess::PieceType::MaxPiece; ++pt)
pos.setHandCount(make_piece(pt, c), static_cast<int>(stream.read_n_bit(DATA_SIZE > 512 ? 7 : 5)));
// Castling availability.
chess::CastlingRights cr = chess::CastlingRights::None;
if (stream.read_one_bit()) {
cr |= chess::CastlingRights::WhiteKingSide;
}
if (stream.read_one_bit()) {
cr |= chess::CastlingRights::WhiteQueenSide;
}
if (stream.read_one_bit()) {
cr |= chess::CastlingRights::BlackKingSide;
}
if (stream.read_one_bit()) {
cr |= chess::CastlingRights::BlackQueenSide;
}
pos.setCastlingRights(cr);
// En passant square. Ignore if no pawn capture is possible
if (stream.read_one_bit()) {
chess::Square ep_square = static_cast<chess::Square>(stream.read_n_bit(7));
pos.setEpSquare(ep_square);
}
// Halfmove clock
std::uint8_t rule50 = stream.read_n_bit(6);
// Fullmove number
std::uint16_t fullmove = stream.read_n_bit(8);
// Fullmove number, high bits
// This was added as a fix for fullmove clock
// overflowing at 256. This change is backwards compatible.
fullmove |= stream.read_n_bit(8) << 8;
// Read the highest bit of rule50. This was added as a fix for rule50
// counter having only 6 bits stored.
// In older entries this will just be a zero bit.
rule50 |= stream.read_n_bit(1) << 6;
pos.setFullMove(fullmove);
pos.setRule50Counter(rule50);
assert(stream.get_cursor() <= DATA_SIZE);
return pos;
}
}
struct TrainingDataEntry
{
chess::Position pos;
chess::Move move;
std::int16_t score;
std::uint16_t ply;
std::int16_t result;
};
[[nodiscard]] inline TrainingDataEntry packedSfenValueToTrainingDataEntry(const nodchip::PackedSfenValue& psv)
{
TrainingDataEntry ret;
ret.pos = nodchip::pos_from_packed_sfen(psv.sfen);
ret.move = psv.move.toMove();
ret.score = psv.score;
ret.ply = psv.gamePly;
ret.result = psv.game_result;
return ret;
}
}