Skip to content

Commit

Permalink
Generate lookup indices at compile time
Browse files Browse the repository at this point in the history
No functional change
  • Loading branch information
PikaCat-OuO committed Jan 25, 2025
1 parent 252ef03 commit 8732c59
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 94 deletions.
134 changes: 41 additions & 93 deletions src/nnue/layers/affine_transform_sparse_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,101 +37,49 @@

namespace Stockfish::Eval::NNUE::Layers {


#if (USE_SSSE3 | (USE_NEON >= 8))
constexpr int constexpr_lsb(uint64_t bb) {
// clang-format off
constexpr int lsb_index64[64] = {
0, 47, 1, 56, 48, 27, 2, 60,
57, 49, 41, 37, 28, 16, 3, 61,
54, 58, 35, 52, 50, 42, 21, 44,
38, 32, 29, 23, 17, 11, 4, 62,
46, 55, 26, 59, 40, 36, 15, 53,
34, 51, 20, 43, 31, 22, 10, 45,
25, 39, 14, 33, 19, 30, 9, 24,
13, 18, 8, 12, 7, 6, 5, 63
};
// clang-format on

assert(bb != 0);
constexpr uint64_t debruijn64 = 0x03F79D71B4CB0A89ULL;
return lsb_index64[((bb ^ (bb - 1)) * debruijn64) >> 58];
}

alignas(CacheLineSize) static constexpr struct OffsetIndices {

#if (USE_SSE41)
alignas(CacheLineSize) static constexpr std::uint8_t
std::uint8_t offset_indices[256][8];
#else
alignas(CacheLineSize) static constexpr std::uint16_t
std::uint16_t offset_indices[256][8];
#endif
lookup_indices[256][8] = {
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0}, {2, 0, 0, 0, 0, 0, 0, 0}, {0, 2, 0, 0, 0, 0, 0, 0},
{1, 2, 0, 0, 0, 0, 0, 0}, {0, 1, 2, 0, 0, 0, 0, 0}, {3, 0, 0, 0, 0, 0, 0, 0},
{0, 3, 0, 0, 0, 0, 0, 0}, {1, 3, 0, 0, 0, 0, 0, 0}, {0, 1, 3, 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0; i < 256; ++i)
{
std::uint64_t j = i, k = 0;
while (j)
{
offset_indices[i][k++] = constexpr_lsb(j);
j &= j - 1;
}
while (k < 8)
offset_indices[i][k++] = 0;
}
}

} Lookup;

// Find indices of nonzero numbers in an int32_t array
template<const IndexType InputDimensions>
Expand Down Expand Up @@ -197,9 +145,9 @@ void find_nnz(const std::int32_t* input, std::uint16_t* out, IndexType& count_ou
}
for (IndexType j = 0; j < OutputsPerChunk; ++j)
{
const auto lookup = (nnz >> (j * 8)) & 0xFF;
const auto offsets =
vec128_load(reinterpret_cast<const vec128_t*>(&lookup_indices[lookup]));
const unsigned lookup = (nnz >> (j * 8)) & 0xFF;
const vec128_t offsets =
vec128_load(reinterpret_cast<const vec128_t*>(&Lookup.offset_indices[lookup]));
vec128_storeu(reinterpret_cast<vec128_t*>(out + count), vec128_add(base, offsets));
count += popcount(lookup);
base = vec128_add(base, increment);
Expand Down
2 changes: 1 addition & 1 deletion src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ Value Search::Worker::search(
return qsearch<PV>(pos, ss, alpha, beta);

// Step 10. ProbCut (~10 Elo)
// If we have a good enough capture (or queen promotion) and a reduced search
// If we have a good enough capture and a reduced search
// returns a value much above beta, we can (almost) safely prune the previous move.
probCutBeta = beta + 234 - 66 * improving;
if (depth >= 4
Expand Down

0 comments on commit 8732c59

Please sign in to comment.