]> git.sesse.net Git - stockfish/blob - src/nnue/nnue_common.h
Remove EvalList
[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 #include <cstring>
25 #include <iostream>
26
27 #if defined(USE_AVX2)
28 #include <immintrin.h>
29
30 #elif defined(USE_SSE41)
31 #include <smmintrin.h>
32
33 #elif defined(USE_SSSE3)
34 #include <tmmintrin.h>
35
36 #elif defined(USE_SSE2)
37 #include <emmintrin.h>
38
39 #elif defined(USE_MMX)
40 #include <mmintrin.h>
41
42 #elif defined(USE_NEON)
43 #include <arm_neon.h>
44 #endif
45
46 // HACK: Use _mm256_loadu_si256() instead of _mm256_load_si256. Otherwise a binary
47 //       compiled with older g++ crashes because the output memory is not aligned
48 //       even though alignas is specified.
49 #if defined(USE_AVX2)
50 #if defined(__GNUC__ ) && (__GNUC__ < 9) && defined(_WIN32) && !defined(__clang__)
51 #define _mm256_loadA_si256  _mm256_loadu_si256
52 #define _mm256_storeA_si256 _mm256_storeu_si256
53 #else
54 #define _mm256_loadA_si256  _mm256_load_si256
55 #define _mm256_storeA_si256 _mm256_store_si256
56 #endif
57 #endif
58
59 #if defined(USE_AVX512)
60 #if defined(__GNUC__ ) && (__GNUC__ < 9) && defined(_WIN32) && !defined(__clang__)
61 #define _mm512_loadA_si512   _mm512_loadu_si512
62 #define _mm512_storeA_si512  _mm512_storeu_si512
63 #else
64 #define _mm512_loadA_si512   _mm512_load_si512
65 #define _mm512_storeA_si512  _mm512_store_si512
66 #endif
67 #endif
68
69 namespace Eval::NNUE {
70
71   // Version of the evaluation file
72   constexpr std::uint32_t kVersion = 0x7AF32F16u;
73
74   // Constant used in evaluation value calculation
75   constexpr int FV_SCALE = 16;
76   constexpr int kWeightScaleBits = 6;
77
78   // Size of cache line (in bytes)
79   constexpr std::size_t kCacheLineSize = 64;
80
81   // SIMD width (in bytes)
82   #if defined(USE_AVX2)
83   constexpr std::size_t kSimdWidth = 32;
84
85   #elif defined(USE_SSE2)
86   constexpr std::size_t kSimdWidth = 16;
87
88   #elif defined(USE_MMX)
89   constexpr std::size_t kSimdWidth = 8;
90
91   #elif defined(USE_NEON)
92   constexpr std::size_t kSimdWidth = 16;
93   #endif
94
95   constexpr std::size_t kMaxSimdWidth = 32;
96
97   // unique number for each piece type on each square
98   enum {
99     PS_NONE     =  0,
100     PS_W_PAWN   =  1,
101     PS_B_PAWN   =  1 * SQUARE_NB + 1,
102     PS_W_KNIGHT =  2 * SQUARE_NB + 1,
103     PS_B_KNIGHT =  3 * SQUARE_NB + 1,
104     PS_W_BISHOP =  4 * SQUARE_NB + 1,
105     PS_B_BISHOP =  5 * SQUARE_NB + 1,
106     PS_W_ROOK   =  6 * SQUARE_NB + 1,
107     PS_B_ROOK   =  7 * SQUARE_NB + 1,
108     PS_W_QUEEN  =  8 * SQUARE_NB + 1,
109     PS_B_QUEEN  =  9 * SQUARE_NB + 1,
110     PS_W_KING   = 10 * SQUARE_NB + 1,
111     PS_END      = PS_W_KING, // pieces without kings (pawns included)
112     PS_B_KING   = 11 * SQUARE_NB + 1,
113     PS_END2     = 12 * SQUARE_NB + 1
114   };
115
116   extern uint32_t kpp_board_index[PIECE_NB][COLOR_NB];
117
118   // Type of input feature after conversion
119   using TransformedFeatureType = std::uint8_t;
120   using IndexType = std::uint32_t;
121
122   // Round n up to be a multiple of base
123   template <typename IntType>
124   constexpr IntType CeilToMultiple(IntType n, IntType base) {
125       return (n + base - 1) / base * base;
126   }
127
128   // read_little_endian() is our utility to read an integer (signed or unsigned, any size)
129   // from a stream in little-endian order. We swap the byte order after the read if
130   // necessary to return a result with the byte ordering of the compiling machine.
131   template <typename IntType>
132   inline IntType read_little_endian(std::istream& stream) {
133
134       IntType result;
135       std::uint8_t u[sizeof(IntType)];
136       typename std::make_unsigned<IntType>::type v = 0;
137
138       stream.read(reinterpret_cast<char*>(u), sizeof(IntType));
139       for (std::size_t i = 0; i < sizeof(IntType); ++i)
140           v = (v << 8) | u[sizeof(IntType) - i - 1];
141
142       std::memcpy(&result, &v, sizeof(IntType));
143       return result;
144   }
145
146 }  // namespace Eval::NNUE
147
148 #endif // #ifndef NNUE_COMMON_H_INCLUDED