Skip to content

Commit 4b759bb

Browse files
authored
Fix UB (#155)
Passes Nonregr ``` Elo | 0.02 +- 1.23 (95%) SPRT | 8.0+0.08s Threads=1 Hash=32MB LLR | 2.95 (-2.94, 2.94) [-3.00, 1.00] Games | N: 85856 W: 19311 L: 19306 D: 47239 Penta | [471, 10150, 21668, 10181, 458] ``` https://furybench.com/test/1180/ bench: 8916225
1 parent d559566 commit 4b759bb

File tree

5 files changed

+24
-25
lines changed

5 files changed

+24
-25
lines changed

src/eval.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,22 @@ constexpr std::array<int, 64> inputBuckets = []{
6161
}();
6262

6363
// organizing this somewhat similarly to code I've seen, mostly from clarity_sp_nnue, made by Ciekce.
64-
65-
struct Network {
66-
alignas(64) std::array<std::int16_t, inputSize * inputBucketCount * layer1Size> featureWeights;
67-
alignas(64) std::array<std::int16_t, layer1Size> featureBiases;
68-
alignas(64) std::array<std::int16_t, layer1Size * 2 * outputBucketCount> outputWeights;
69-
alignas(64) std::array<std::int16_t, outputBucketCount> outputBiases;
64+
#if defined(__AVX512F__) && defined(__AVX512BW__)
65+
constexpr int alignmentAmount = 64;
66+
#else
67+
constexpr int alignmentAmount = 32;
68+
#endif
69+
70+
struct alignas(alignmentAmount) Network {
71+
std::array<std::int16_t, inputSize * inputBucketCount * layer1Size> featureWeights;
72+
std::array<std::int16_t, layer1Size> featureBiases;
73+
std::array<std::int16_t, layer1Size * 2 * outputBucketCount> outputWeights;
74+
std::array<std::int16_t, outputBucketCount> outputBiases;
7075
};
7176

7277
struct Accumulator {
73-
alignas(64) std::array<std::int16_t, layer1Size> black;
74-
alignas(64) std::array<std::int16_t, layer1Size> white;
78+
alignas(alignmentAmount) std::array<std::int16_t, layer1Size> black;
79+
alignas(alignmentAmount) std::array<std::int16_t, layer1Size> white;
7580
void initialize(std::span<const std::int16_t, layer1Size> bias);
7681
void initHalf(std::span<const std::int16_t, layer1Size> bias, int color);
7782
};
@@ -132,4 +137,4 @@ constexpr bool refreshRequired(int color, int oldKingSquare, int newKingSquare)
132137
}
133138

134139
return inputBuckets[oldKingSquare] != inputBuckets[newKingSquare];
135-
}
140+
}

src/globals.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ std::array<std::array<uint8_t, 218>, 150> reductions;
7777
void calculateReductions() {
7878
for(int depth = 0; depth < 150; depth++) {
7979
for(int move = 0; move < 218; move++) {
80-
reductions[depth][move] = uint8_t(std::clamp(lmrBase.value + log(depth) * log(move) * lmrMultiplier.value, -32678.0, 32678.0));
80+
if (depth == 0 || move == 0) {
81+
reductions[depth][move] = 0;
82+
continue;
83+
}
84+
reductions[depth][move] = uint8_t(lmrBase.value + log(depth) * log(move) * lmrMultiplier.value);
8185
}
8286
}
8387
}
@@ -144,14 +148,3 @@ void sortMoves(std::array<int, 256> &values, std::array<Move, 256> &moves, int n
144148
int flipIndex(int index) {
145149
return index ^ 56;
146150
}
147-
148-
// thanks z5
149-
// also I completely misunderstood how this was supposed to work, this isn't it lol
150-
void incrementalSort(std::array<int, 256> &values, std::array<Move, 256> &moves, int numMoves, int i) {
151-
for(int j = i + 1; j < numMoves; j++) {
152-
if(values[j] > values[i]) {
153-
std::swap(values[j], values[i]);
154-
std::swap(moves[j], moves[i]);
155-
}
156-
}
157-
}

src/movegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ uint64_t getBishopAttacksOld(int square, uint64_t occupiedBitboard) {
3737
for(int direction = 4; direction < 8; direction++) {
3838
uint64_t currentAttack = slideyPieceRays[direction][square];
3939
if((direction & 1) == 0) {
40-
currentAttack ^= slideyPieceRays[direction][std::countr_zero(currentAttack & occupiedBitboard)];
40+
currentAttack ^= slideyPieceRays[direction][std::clamp(std::countr_zero(currentAttack & occupiedBitboard), 0, 63)];
4141
} else {
42-
currentAttack ^= slideyPieceRays[direction][63 - std::countl_zero(currentAttack & occupiedBitboard)];
42+
currentAttack ^= slideyPieceRays[direction][std::clamp(63 - std::countl_zero(currentAttack & occupiedBitboard), 0, 63)];
4343
}
4444
attacks |= currentAttack;
4545
}

src/search.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ int16_t Engine::negamax(Board &board, int depth, int alpha, int beta, int16_t pl
494494
improving = true;
495495
}
496496
bool opponent_worsening = false;
497-
if(!inCheck) {
497+
if(!inCheck && ply > 0) {
498498
opponent_worsening = stack[ply].staticEval + stack[ply - 1].staticEval > 1;
499499
}
500500

@@ -515,7 +515,7 @@ int16_t Engine::negamax(Board &board, int depth, int alpha, int beta, int16_t pl
515515
}
516516
}
517517

518-
if(!isPV && !inCheck && stack[ply - 1].reduction >= 3 && !opponent_worsening) {
518+
if(!isPV && !inCheck && ply > 0 && stack[ply - 1].reduction >= 3 && !opponent_worsening) {
519519
++depth;
520520
}
521521

src/uci.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "external/fathom/tbprobe.h"
2424
#include "uci.h"
2525
#include "tunables.h"
26+
#include "eval.h"
2627

2728
bool useSyzygy = false;
2829

0 commit comments

Comments
 (0)