2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 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
27 #include "../misc.h" // for IsLittleEndian
30 #include <immintrin.h>
32 #elif defined(USE_SSE41)
33 #include <smmintrin.h>
35 #elif defined(USE_SSSE3)
36 #include <tmmintrin.h>
38 #elif defined(USE_SSE2)
39 #include <emmintrin.h>
41 #elif defined(USE_MMX)
44 #elif defined(USE_NEON)
48 namespace Stockfish::Eval::NNUE {
50 // Version of the evaluation file
51 constexpr std::uint32_t Version = 0x7AF32F20u;
53 // Constant used in evaluation value calculation
54 constexpr int OutputScale = 16;
55 constexpr int WeightScaleBits = 6;
57 // Size of cache line (in bytes)
58 constexpr std::size_t CacheLineSize = 64;
60 constexpr const char Leb128MagicString[] = "COMPRESSED_LEB128";
61 constexpr const std::size_t Leb128MagicStringSize = sizeof(Leb128MagicString) - 1;
63 // SIMD width (in bytes)
65 constexpr std::size_t SimdWidth = 32;
67 #elif defined(USE_SSE2)
68 constexpr std::size_t SimdWidth = 16;
70 #elif defined(USE_MMX)
71 constexpr std::size_t SimdWidth = 8;
73 #elif defined(USE_NEON)
74 constexpr std::size_t SimdWidth = 16;
77 constexpr std::size_t MaxSimdWidth = 32;
79 // Type of input feature after conversion
80 using TransformedFeatureType = std::uint8_t;
81 using IndexType = std::uint32_t;
83 // Round n up to be a multiple of base
84 template <typename IntType>
85 constexpr IntType ceil_to_multiple(IntType n, IntType base) {
86 return (n + base - 1) / base * base;
89 // read_little_endian() is our utility to read an integer (signed or unsigned, any size)
90 // from a stream in little-endian order. We swap the byte order after the read if
91 // necessary to return a result with the byte ordering of the compiling machine.
92 template <typename IntType>
93 inline IntType read_little_endian(std::istream& stream) {
97 stream.read(reinterpret_cast<char*>(&result), sizeof(IntType));
100 std::uint8_t u[sizeof(IntType)];
101 typename std::make_unsigned<IntType>::type v = 0;
103 stream.read(reinterpret_cast<char*>(u), sizeof(IntType));
104 for (std::size_t i = 0; i < sizeof(IntType); ++i)
105 v = (v << 8) | u[sizeof(IntType) - i - 1];
107 std::memcpy(&result, &v, sizeof(IntType));
113 // write_little_endian() is our utility to write an integer (signed or unsigned, any size)
114 // to a stream in little-endian order. We swap the byte order before the write if
115 // necessary to always write in little endian order, independently of the byte
116 // ordering of the compiling machine.
117 template <typename IntType>
118 inline void write_little_endian(std::ostream& stream, IntType value) {
121 stream.write(reinterpret_cast<const char*>(&value), sizeof(IntType));
124 std::uint8_t u[sizeof(IntType)];
125 typename std::make_unsigned<IntType>::type v = value;
128 // if constexpr to silence the warning about shift by 8
129 if constexpr (sizeof(IntType) > 1)
131 for (; i + 1 < sizeof(IntType); ++i)
133 u[i] = (std::uint8_t)v;
137 u[i] = (std::uint8_t)v;
139 stream.write(reinterpret_cast<char*>(u), sizeof(IntType));
143 // read_little_endian(s, out, N) : read integers in bulk from a little indian stream.
144 // This reads N integers from stream s and put them in array out.
145 template <typename IntType>
146 inline void read_little_endian(std::istream& stream, IntType* out, std::size_t count) {
148 stream.read(reinterpret_cast<char*>(out), sizeof(IntType) * count);
150 for (std::size_t i = 0; i < count; ++i)
151 out[i] = read_little_endian<IntType>(stream);
154 // write_little_endian(s, values, N) : write integers in bulk to a little indian stream.
155 // This takes N integers from array values and writes them on stream s.
156 template <typename IntType>
157 inline void write_little_endian(std::ostream& stream, const IntType* values, std::size_t count) {
159 stream.write(reinterpret_cast<const char*>(values), sizeof(IntType) * count);
161 for (std::size_t i = 0; i < count; ++i)
162 write_little_endian<IntType>(stream, values[i]);
165 template <typename IntType>
166 inline void read_leb_128(std::istream& stream, IntType* out, std::size_t count) {
167 static_assert(std::is_signed_v<IntType>, "Not implemented for unsigned types");
168 char leb128MagicString[Leb128MagicStringSize];
169 stream.read(leb128MagicString, Leb128MagicStringSize);
170 assert(strncmp(Leb128MagicString, leb128MagicString, Leb128MagicStringSize) == 0);
171 const std::uint32_t BUF_SIZE = 4096;
172 std::uint8_t buf[BUF_SIZE];
173 auto bytes_left = read_little_endian<std::uint32_t>(stream);
174 std::uint32_t buf_pos = BUF_SIZE;
175 for (std::size_t i = 0; i < count; ++i) {
179 if (buf_pos == BUF_SIZE) {
180 stream.read(reinterpret_cast<char*>(buf), std::min(bytes_left, BUF_SIZE));
183 std::uint8_t byte = buf[buf_pos++];
185 result |= (byte & 0x7f) << shift;
187 if ((byte & 0x80) == 0) {
188 out[i] = sizeof(IntType) * 8 <= shift || (byte & 0x40) == 0 ? result : result | ~((1 << shift) - 1);
191 } while (shift < sizeof(IntType) * 8);
193 assert(bytes_left == 0);
196 template <typename IntType>
197 inline void write_leb_128(std::ostream& stream, const IntType* values, std::size_t count) {
198 static_assert(std::is_signed_v<IntType>, "Not implemented for unsigned types");
199 stream.write(Leb128MagicString, Leb128MagicStringSize);
200 std::uint32_t byte_count = 0;
201 for (std::size_t i = 0; i < count; ++i) {
202 IntType value = values[i];
208 } while ((byte & 0x40) == 0 ? value != 0 : value != -1);
210 write_little_endian(stream, byte_count);
211 const std::uint32_t BUF_SIZE = 4096;
212 std::uint8_t buf[BUF_SIZE];
213 std::uint32_t buf_pos = 0;
216 stream.write(reinterpret_cast<char*>(buf), buf_pos);
220 auto write = [&](std::uint8_t byte) {
221 buf[buf_pos++] = byte;
222 if (buf_pos == BUF_SIZE) flush();
224 for (std::size_t i = 0; i < count; ++i) {
225 IntType value = values[i];
227 std::uint8_t byte = value & 0x7f;
229 if ((byte & 0x40) == 0 ? value == 0 : value == -1) {
239 } // namespace Stockfish::Eval::NNUE
241 #endif // #ifndef NNUE_COMMON_H_INCLUDED