Skip to content

Commit 60090f1

Browse files
committed
test
1 parent a89906b commit 60090f1

File tree

2 files changed

+36
-51
lines changed

2 files changed

+36
-51
lines changed

src_files/eval.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -139,51 +139,6 @@ void nn::init() {
139139
memoryIndex += OUTPUT_SIZE * sizeof(int32_t);
140140
}
141141

142-
int nn::index(bb::PieceType pieceType, bb::Color pieceColor, bb::Square square, bb::Color view,
143-
bb::Square kingSquare) {
144-
constexpr int pieceTypeFactor = 64;
145-
constexpr int pieceColorFactor = 64 * 6;
146-
constexpr int kingSquareFactor = 64 * 6 * 2;
147-
148-
const bool kingSide = bb::fileIndex(kingSquare) > 3;
149-
const int ksIndex = kingSquareIndex(kingSquare, view);
150-
bb::Square relativeSquare = view == bb::WHITE ? square : bb::mirrorVertically(square);
151-
152-
if (kingSide) {
153-
relativeSquare = bb::mirrorHorizontally(relativeSquare);
154-
}
155-
156-
// clang-format off
157-
return relativeSquare
158-
+ pieceType * pieceTypeFactor
159-
+ (pieceColor == view) * pieceColorFactor
160-
+ ksIndex * kingSquareFactor;
161-
// clang-format on
162-
}
163-
164-
int nn::kingSquareIndex(bb::Square relativeKingSquare, bb::Color kingColor) {
165-
// return zero if there is no king on the board yet ->
166-
// requires manual reset
167-
if (relativeKingSquare > 63)
168-
return 0;
169-
// clang-format off
170-
constexpr int indices[bb::N_SQUARES] {
171-
0, 1, 2, 3, 3, 2, 1, 0,
172-
4, 5, 6, 7, 7, 6, 5, 4,
173-
8, 9, 10, 11, 11, 10, 9, 8,
174-
8, 9, 10, 11, 11, 10, 9, 8,
175-
12, 12, 13, 13, 13, 13, 12, 12,
176-
12, 12, 13, 13, 13, 13, 12, 12,
177-
14, 14, 15, 15, 15, 15, 14, 14,
178-
14, 14, 15, 15, 15, 15, 14, 14,
179-
};
180-
// clang-format on
181-
if (kingColor == bb::BLACK) {
182-
relativeKingSquare = bb::mirrorVertically(relativeKingSquare);
183-
}
184-
return indices[relativeKingSquare];
185-
}
186-
187142

188143
void nn::AccumulatorTable::use(bb::Color view, Board* board, nn::Evaluator& evaluator) {
189144
const bb::Square king_sq = bb::bitscanForward(board->getPieceBB(view, bb::KING));
@@ -230,7 +185,6 @@ void nn::AccumulatorTable::use(bb::Color view, Board* board, nn::Evaluator& eval
230185
}
231186

232187
std::memcpy(evaluator.history.back().summation[view], entry.accumulator.summation[view],sizeof(int16_t) * HIDDEN_SIZE);
233-
234188
}
235189

236190
void nn::AccumulatorTable::reset() {

src_files/eval.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define KOIVISTO_EVAL_H
2121

2222
#include "bitboard.h"
23+
#include "uciassert.h"
2324

2425
#include <cstdint>
2526
#include <cstring>
@@ -58,17 +59,47 @@ extern int16_t hiddenWeights[OUTPUT_SIZE][HIDDEN_DSIZE];
5859
extern int16_t inputBias [HIDDEN_SIZE];
5960
extern int32_t hiddenBias [OUTPUT_SIZE];
6061

62+
constexpr int kingSquareIndices[bb::N_SQUARES] {
63+
0, 1, 2, 3, 3, 2, 1, 0,
64+
4, 5, 6, 7, 7, 6, 5, 4,
65+
8, 9, 10, 11, 11, 10, 9, 8,
66+
8, 9, 10, 11, 11, 10, 9, 8,
67+
12, 12, 13, 13, 13, 13, 12, 12,
68+
12, 12, 13, 13, 13, 13, 12, 12,
69+
14, 14, 15, 15, 15, 15, 14, 14,
70+
14, 14, 15, 15, 15, 15, 14, 14,
71+
};
72+
6173
// initialise and load the weights
6274
void init();
6375

64-
// computes the index for a piece (piece type) and its color on the specified square
65-
// also takes the view from with we view at the board as well as the king square of the view side
66-
[[nodiscard]] int index(bb::PieceType pieceType, bb::Color pieceColor, bb::Square square,
67-
bb::Color view, bb::Square kingSquare);
76+
6877

6978
// the index is based on a king bucketing system. the relevant king bucket can be retrieved using
7079
// the function below
71-
[[nodiscard]] int kingSquareIndex(bb::Square kingSquare, bb::Color kingColor);
80+
[[nodiscard]] inline int kingSquareIndex(bb::Square kingSquare, bb::Color kingColor){
81+
UCI_ASSERT(kingSquare >= bb::A1);
82+
UCI_ASSERT(kingSquare <= bb::H8);
83+
kingSquare = (56 * kingColor) ^ kingSquare;
84+
return kingSquareIndices[kingSquare];
85+
86+
}
87+
88+
// computes the index for a piece (piece type) and its color on the specified square
89+
// also takes the view from with we view at the board as well as the king square of the view side
90+
[[nodiscard]] inline int index(bb::PieceType pieceType, bb::Color pieceColor, bb::Square square,
91+
bb::Color view, bb::Square kingSquare){
92+
const int ksIndex = kingSquareIndex(kingSquare, view);
93+
square ^= 56 * view;
94+
square ^= 7 * !!(kingSquare & 0x4);
95+
96+
// clang-format off
97+
return square
98+
+ pieceType * 64
99+
+ !(pieceColor ^ view) * 64 * 6
100+
+ ksIndex * 64 * 6 * 2;
101+
// clang-format on
102+
}
72103

73104
// the accumulator which is used as the first hidden layer of the network.
74105
// it is updated efficiently and contains the accumulated weights for whites and black pov.

0 commit comments

Comments
 (0)