]> git.sesse.net Git - stockfish/blob - src/nnue/nnue_common.h
Update copyright years
[stockfish] / src / nnue / nnue_common.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2021 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 namespace Eval::NNUE {
47
48   // Version of the evaluation file
49   constexpr std::uint32_t kVersion = 0x7AF32F16u;
50
51   // Constant used in evaluation value calculation
52   constexpr int FV_SCALE = 16;
53   constexpr int kWeightScaleBits = 6;
54
55   // Size of cache line (in bytes)
56   constexpr std::size_t kCacheLineSize = 64;
57
58   // SIMD width (in bytes)
59   #if defined(USE_AVX2)
60   constexpr std::size_t kSimdWidth = 32;
61
62   #elif defined(USE_SSE2)
63   constexpr std::size_t kSimdWidth = 16;
64
65   #elif defined(USE_MMX)
66   constexpr std::size_t kSimdWidth = 8;
67
68   #elif defined(USE_NEON)
69   constexpr std::size_t kSimdWidth = 16;
70   #endif
71
72   constexpr std::size_t kMaxSimdWidth = 32;
73
74   // unique number for each piece type on each square
75   enum {
76     PS_NONE     =  0,
77     PS_W_PAWN   =  1,
78     PS_B_PAWN   =  1 * SQUARE_NB + 1,
79     PS_W_KNIGHT =  2 * SQUARE_NB + 1,
80     PS_B_KNIGHT =  3 * SQUARE_NB + 1,
81     PS_W_BISHOP =  4 * SQUARE_NB + 1,
82     PS_B_BISHOP =  5 * SQUARE_NB + 1,
83     PS_W_ROOK   =  6 * SQUARE_NB + 1,
84     PS_B_ROOK   =  7 * SQUARE_NB + 1,
85     PS_W_QUEEN  =  8 * SQUARE_NB + 1,
86     PS_B_QUEEN  =  9 * SQUARE_NB + 1,
87     PS_W_KING   = 10 * SQUARE_NB + 1,
88     PS_END      = PS_W_KING, // pieces without kings (pawns included)
89     PS_B_KING   = 11 * SQUARE_NB + 1,
90     PS_END2     = 12 * SQUARE_NB + 1
91   };
92
93   constexpr uint32_t kpp_board_index[COLOR_NB][PIECE_NB] = {
94     // convention: W - us, B - them
95     // viewed from other side, W and B are reversed
96     { PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_W_KING, PS_NONE,
97       PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_B_KING, PS_NONE },
98     { PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_B_KING, PS_NONE,
99       PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_W_KING, PS_NONE }
100   };
101
102   // Type of input feature after conversion
103   using TransformedFeatureType = std::uint8_t;
104   using IndexType = std::uint32_t;
105
106   // Round n up to be a multiple of base
107   template <typename IntType>
108   constexpr IntType CeilToMultiple(IntType n, IntType base) {
109       return (n + base - 1) / base * base;
110   }
111
112   // read_little_endian() is our utility to read an integer (signed or unsigned, any size)
113   // from a stream in little-endian order. We swap the byte order after the read if
114   // necessary to return a result with the byte ordering of the compiling machine.
115   template <typename IntType>
116   inline IntType read_little_endian(std::istream& stream) {
117
118       IntType result;
119       std::uint8_t u[sizeof(IntType)];
120       typename std::make_unsigned<IntType>::type v = 0;
121
122       stream.read(reinterpret_cast<char*>(u), sizeof(IntType));
123       for (std::size_t i = 0; i < sizeof(IntType); ++i)
124           v = (v << 8) | u[sizeof(IntType) - i - 1];
125
126       std::memcpy(&result, &v, sizeof(IntType));
127       return result;
128   }
129
130 }  // namespace Eval::NNUE
131
132 #endif // #ifndef NNUE_COMMON_H_INCLUDED