2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
5 Stockfish is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Stockfish is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // Constants used in NNUE evaluation function
21 #ifndef NNUE_COMMON_H_INCLUDED
22 #define NNUE_COMMON_H_INCLUDED
25 #include <immintrin.h>
27 #elif defined(USE_SSE41)
28 #include <smmintrin.h>
30 #elif defined(USE_SSSE3)
31 #include <tmmintrin.h>
33 #elif defined(USE_SSE2)
34 #include <emmintrin.h>
36 #elif defined(USE_NEON)
40 // HACK: Use _mm256_loadu_si256() instead of _mm256_load_si256. Otherwise a binary
41 // compiled with older g++ crashes because the output memory is not aligned
42 // even though alignas is specified.
44 #if defined(__GNUC__ ) && (__GNUC__ < 9)
45 #define _mm256_loadA_si256 _mm256_loadu_si256
46 #define _mm256_storeA_si256 _mm256_storeu_si256
48 #define _mm256_loadA_si256 _mm256_load_si256
49 #define _mm256_storeA_si256 _mm256_store_si256
53 #if defined(USE_AVX512)
54 #if defined(__GNUC__ ) && (__GNUC__ < 9)
55 #define _mm512_loadA_si512 _mm512_loadu_si512
57 #define _mm512_loadA_si512 _mm512_load_si512
61 namespace Eval::NNUE {
63 // Version of the evaluation file
64 constexpr std::uint32_t kVersion = 0x7AF32F16u;
66 // Constant used in evaluation value calculation
67 constexpr int FV_SCALE = 16;
68 constexpr int kWeightScaleBits = 6;
70 // Size of cache line (in bytes)
71 constexpr std::size_t kCacheLineSize = 64;
73 // SIMD width (in bytes)
75 constexpr std::size_t kSimdWidth = 32;
77 #elif defined(USE_SSE2)
78 constexpr std::size_t kSimdWidth = 16;
80 #elif defined(USE_NEON)
81 constexpr std::size_t kSimdWidth = 16;
84 constexpr std::size_t kMaxSimdWidth = 32;
86 // Type of input feature after conversion
87 using TransformedFeatureType = std::uint8_t;
88 using IndexType = std::uint32_t;
90 // Round n up to be a multiple of base
91 template <typename IntType>
92 constexpr IntType CeilToMultiple(IntType n, IntType base) {
93 return (n + base - 1) / base * base;
96 } // namespace Eval::NNUE
98 #endif // #ifndef NNUE_COMMON_H_INCLUDED