2828
2929namespace Stockfish ::Eval::NNUE {
3030
31+ using BiasType = std::int16_t ;
32+ using WeightType = std::int16_t ;
33+ using PSQTWeightType = std::int32_t ;
34+
3135 // If vector instructions are enabled, we update and refresh the
3236 // accumulator tile by tile such that each tile fits in the CPU's
3337 // vector registers.
3438 #define VECTOR
3539
36- static_assert (PSQTBuckets == 8 , " Assumed by the current choice of constants." );
40+ static_assert (PSQTBuckets % 8 == 0 ,
41+ " Per feature PSQT values cannot be processed at granularity lower than 8 at a time." );
3742
3843 #ifdef USE_AVX512
3944 typedef __m512i vec_t ;
@@ -47,8 +52,7 @@ namespace Stockfish::Eval::NNUE {
4752 #define vec_add_psqt_32 (a,b ) _mm256_add_epi32(a,b)
4853 #define vec_sub_psqt_32 (a,b ) _mm256_sub_epi32(a,b)
4954 #define vec_zero_psqt () _mm256_setzero_si256()
50- static constexpr IndexType NumRegs = 8 ; // only 8 are needed
51- static constexpr IndexType NumPsqtRegs = 1 ;
55+ #define NumRegistersSIMD 32
5256
5357 #elif USE_AVX2
5458 typedef __m256i vec_t ;
@@ -62,8 +66,7 @@ namespace Stockfish::Eval::NNUE {
6266 #define vec_add_psqt_32 (a,b ) _mm256_add_epi32(a,b)
6367 #define vec_sub_psqt_32 (a,b ) _mm256_sub_epi32(a,b)
6468 #define vec_zero_psqt () _mm256_setzero_si256()
65- static constexpr IndexType NumRegs = 16 ;
66- static constexpr IndexType NumPsqtRegs = 1 ;
69+ #define NumRegistersSIMD 16
6770
6871 #elif USE_SSE2
6972 typedef __m128i vec_t ;
@@ -77,8 +80,7 @@ namespace Stockfish::Eval::NNUE {
7780 #define vec_add_psqt_32 (a,b ) _mm_add_epi32(a,b)
7881 #define vec_sub_psqt_32 (a,b ) _mm_sub_epi32(a,b)
7982 #define vec_zero_psqt () _mm_setzero_si128()
80- static constexpr IndexType NumRegs = Is64Bit ? 16 : 8 ;
81- static constexpr IndexType NumPsqtRegs = 2 ;
83+ #define NumRegistersSIMD (Is64Bit ? 16 : 8 )
8284
8385 #elif USE_MMX
8486 typedef __m64 vec_t ;
@@ -92,8 +94,7 @@ namespace Stockfish::Eval::NNUE {
9294 #define vec_add_psqt_32 (a,b ) _mm_add_pi32(a,b)
9395 #define vec_sub_psqt_32 (a,b ) _mm_sub_pi32(a,b)
9496 #define vec_zero_psqt () _mm_setzero_si64()
95- static constexpr IndexType NumRegs = 8 ;
96- static constexpr IndexType NumPsqtRegs = 4 ;
97+ #define NumRegistersSIMD 8
9798
9899 #elif USE_NEON
99100 typedef int16x8_t vec_t ;
@@ -107,14 +108,61 @@ namespace Stockfish::Eval::NNUE {
107108 #define vec_add_psqt_32 (a,b ) vaddq_s32(a,b)
108109 #define vec_sub_psqt_32 (a,b ) vsubq_s32(a,b)
109110 #define vec_zero_psqt () psqt_vec_t {0 }
110- static constexpr IndexType NumRegs = 16 ;
111- static constexpr IndexType NumPsqtRegs = 2 ;
111+ #define NumRegistersSIMD 16
112112
113113 #else
114114 #undef VECTOR
115115
116116 #endif
117117
118+
119+ #ifdef VECTOR
120+
121+ // Compute optimal SIMD register count for feature transformer accumulation.
122+
123+ // We use __m* types as template arguments, which causes GCC to emit warnings
124+ // about losing some attribute information. This is irrelevant to us as we
125+ // only take their size, so the following pragma are harmless.
126+ #pragma GCC diagnostic push
127+ #pragma GCC diagnostic ignored "-Wignored-attributes"
128+
129+ template <typename SIMDRegisterType,
130+ typename LaneType,
131+ int NumLanes,
132+ int MaxRegisters>
133+ static constexpr int BestRegisterCount ()
134+ {
135+ #define RegisterSize sizeof (SIMDRegisterType)
136+ #define LaneSize sizeof (LaneType)
137+
138+ static_assert (RegisterSize >= LaneSize);
139+ static_assert (MaxRegisters <= NumRegistersSIMD);
140+ static_assert (MaxRegisters > 0 );
141+ static_assert (NumRegistersSIMD > 0 );
142+ static_assert (RegisterSize % LaneSize == 0 );
143+ static_assert ((NumLanes * LaneSize) % RegisterSize == 0 );
144+
145+ const int ideal = (NumLanes * LaneSize) / RegisterSize;
146+ if (ideal <= MaxRegisters)
147+ return ideal;
148+
149+ // Look for the largest divisor of the ideal register count that is smaller than MaxRegisters
150+ for (int divisor = MaxRegisters; divisor > 1 ; --divisor)
151+ if (ideal % divisor == 0 )
152+ return divisor;
153+
154+ return 1 ;
155+ }
156+
157+ static constexpr int NumRegs = BestRegisterCount<vec_t , WeightType, TransformedFeatureDimensions, NumRegistersSIMD>();
158+ static constexpr int NumPsqtRegs = BestRegisterCount<psqt_vec_t , PSQTWeightType, PSQTBuckets, NumRegistersSIMD>();
159+
160+ #pragma GCC diagnostic pop
161+
162+ #endif
163+
164+
165+
118166 // Input feature converter
119167 class FeatureTransformer {
120168
@@ -557,10 +605,6 @@ namespace Stockfish::Eval::NNUE {
557605 #endif
558606 }
559607
560- using BiasType = std::int16_t ;
561- using WeightType = std::int16_t ;
562- using PSQTWeightType = std::int32_t ;
563-
564608 alignas (CacheLineSize) BiasType biases[HalfDimensions];
565609 alignas (CacheLineSize) WeightType weights[HalfDimensions * InputDimensions];
566610 alignas (CacheLineSize) PSQTWeightType psqtWeights[InputDimensions * PSQTBuckets];
0 commit comments