]> git.sesse.net Git - stockfish/blob - src/nnue/nnue_common.h
eab7d258816cfe4dc2489a43d5c27b098b676d76
[stockfish] / src / nnue / nnue_common.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
4
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.
9
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.
14
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/>.
17 */
18
19 // Constants used in NNUE evaluation function
20
21 #ifndef NNUE_COMMON_H_INCLUDED
22 #define NNUE_COMMON_H_INCLUDED
23
24 #if defined(USE_AVX2)
25 #include <immintrin.h>
26
27 #elif defined(USE_SSE41)
28 #include <smmintrin.h>
29
30 #elif defined(USE_SSSE3)
31 #include <tmmintrin.h>
32
33 #elif defined(USE_SSE2)
34 #include <emmintrin.h>
35
36 #elif defined(USE_MMX)
37 #include <mmintrin.h>
38
39 #elif defined(USE_NEON)
40 #include <arm_neon.h>
41 #endif
42
43 // HACK: Use _mm256_loadu_si256() instead of _mm256_load_si256. Otherwise a binary
44 //       compiled with older g++ crashes because the output memory is not aligned
45 //       even though alignas is specified.
46 #if defined(USE_AVX2)
47 #if defined(__GNUC__ ) && (__GNUC__ < 9) && defined(_WIN32)
48 #define _mm256_loadA_si256  _mm256_loadu_si256
49 #define _mm256_storeA_si256 _mm256_storeu_si256
50 #else
51 #define _mm256_loadA_si256  _mm256_load_si256
52 #define _mm256_storeA_si256 _mm256_store_si256
53 #endif
54 #endif
55
56 #if defined(USE_AVX512)
57 #if defined(__GNUC__ ) && (__GNUC__ < 9) && defined(_WIN32)
58 #define _mm512_loadA_si512   _mm512_loadu_si512
59 #define _mm512_storeA_si512  _mm512_storeu_si512
60 #else
61 #define _mm512_loadA_si512   _mm512_load_si512
62 #define _mm512_storeA_si512  _mm512_store_si512
63 #endif
64 #endif
65
66 namespace Eval::NNUE {
67
68   // Version of the evaluation file
69   constexpr std::uint32_t kVersion = 0x7AF32F16u;
70
71   // Constant used in evaluation value calculation
72   constexpr int FV_SCALE = 16;
73   constexpr int kWeightScaleBits = 6;
74
75   // Size of cache line (in bytes)
76   constexpr std::size_t kCacheLineSize = 64;
77
78   // SIMD width (in bytes)
79   #if defined(USE_AVX2)
80   constexpr std::size_t kSimdWidth = 32;
81
82   #elif defined(USE_SSE2)
83   constexpr std::size_t kSimdWidth = 16;
84
85   #elif defined(USE_MMX)
86   constexpr std::size_t kSimdWidth = 8;
87
88   #elif defined(USE_NEON)
89   constexpr std::size_t kSimdWidth = 16;
90   #endif
91
92   constexpr std::size_t kMaxSimdWidth = 32;
93
94   // Type of input feature after conversion
95   using TransformedFeatureType = std::uint8_t;
96   using IndexType = std::uint32_t;
97
98   // Round n up to be a multiple of base
99   template <typename IntType>
100   constexpr IntType CeilToMultiple(IntType n, IntType base) {
101     return (n + base - 1) / base * base;
102   }
103
104 }  // namespace Eval::NNUE
105
106 #endif // #ifndef NNUE_COMMON_H_INCLUDED