]> git.sesse.net Git - stockfish/blob - src/nnue/nnue_common.h
Use C++17 variable templates for type traits
[stockfish] / src / nnue / nnue_common.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 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 <algorithm>
25 #include <cassert>
26 #include <cstdint>
27 #include <cstring>
28 #include <iostream>
29 #include <type_traits>
30
31 #include "../misc.h"
32
33 #if defined(USE_AVX2)
34 #include <immintrin.h>
35
36 #elif defined(USE_SSE41)
37 #include <smmintrin.h>
38
39 #elif defined(USE_SSSE3)
40 #include <tmmintrin.h>
41
42 #elif defined(USE_SSE2)
43 #include <emmintrin.h>
44
45 #elif defined(USE_MMX)
46 #include <mmintrin.h>
47
48 #elif defined(USE_NEON)
49 #include <arm_neon.h>
50 #endif
51
52 namespace Stockfish::Eval::NNUE {
53
54   // Version of the evaluation file
55   constexpr std::uint32_t Version = 0x7AF32F20u;
56
57   // Constant used in evaluation value calculation
58   constexpr int OutputScale = 16;
59   constexpr int WeightScaleBits = 6;
60
61   // Size of cache line (in bytes)
62   constexpr std::size_t CacheLineSize = 64;
63
64   constexpr const char Leb128MagicString[] = "COMPRESSED_LEB128";
65   constexpr const std::size_t Leb128MagicStringSize = sizeof(Leb128MagicString) - 1;
66
67   // SIMD width (in bytes)
68   #if defined(USE_AVX2)
69   constexpr std::size_t SimdWidth = 32;
70
71   #elif defined(USE_SSE2)
72   constexpr std::size_t SimdWidth = 16;
73
74   #elif defined(USE_MMX)
75   constexpr std::size_t SimdWidth = 8;
76
77   #elif defined(USE_NEON)
78   constexpr std::size_t SimdWidth = 16;
79   #endif
80
81   constexpr std::size_t MaxSimdWidth = 32;
82
83   // Type of input feature after conversion
84   using TransformedFeatureType = std::uint8_t;
85   using IndexType = std::uint32_t;
86
87   // Round n up to be a multiple of base
88   template <typename IntType>
89   constexpr IntType ceil_to_multiple(IntType n, IntType base) {
90       return (n + base - 1) / base * base;
91   }
92
93
94   // read_little_endian() is our utility to read an integer (signed or unsigned, any size)
95   // from a stream in little-endian order. We swap the byte order after the read if
96   // necessary to return a result with the byte ordering of the compiling machine.
97   template <typename IntType>
98   inline IntType read_little_endian(std::istream& stream) {
99       IntType result;
100
101       if (IsLittleEndian)
102           stream.read(reinterpret_cast<char*>(&result), sizeof(IntType));
103       else
104       {
105           std::uint8_t u[sizeof(IntType)];
106           std::make_unsigned_t<IntType> v = 0;
107
108           stream.read(reinterpret_cast<char*>(u), sizeof(IntType));
109           for (std::size_t i = 0; i < sizeof(IntType); ++i)
110               v = (v << 8) | u[sizeof(IntType) - i - 1];
111
112           std::memcpy(&result, &v, sizeof(IntType));
113       }
114
115       return result;
116   }
117
118
119   // write_little_endian() is our utility to write an integer (signed or unsigned, any size)
120   // to a stream in little-endian order. We swap the byte order before the write if
121   // necessary to always write in little endian order, independently of the byte
122   // ordering of the compiling machine.
123   template <typename IntType>
124   inline void write_little_endian(std::ostream& stream, IntType value) {
125
126       if (IsLittleEndian)
127           stream.write(reinterpret_cast<const char*>(&value), sizeof(IntType));
128       else
129       {
130           std::uint8_t u[sizeof(IntType)];
131           std::make_unsigned_t<IntType> v = value;
132
133           std::size_t i = 0;
134           // if constexpr to silence the warning about shift by 8
135           if constexpr (sizeof(IntType) > 1)
136           {
137             for (; i + 1 < sizeof(IntType); ++i)
138             {
139                 u[i] = (std::uint8_t)v;
140                 v >>= 8;
141             }
142           }
143           u[i] = (std::uint8_t)v;
144
145           stream.write(reinterpret_cast<char*>(u), sizeof(IntType));
146       }
147   }
148
149
150   // read_little_endian(s, out, N) : read integers in bulk from a little indian stream.
151   // This reads N integers from stream s and put them in array out.
152   template <typename IntType>
153   inline void read_little_endian(std::istream& stream, IntType* out, std::size_t count) {
154       if (IsLittleEndian)
155           stream.read(reinterpret_cast<char*>(out), sizeof(IntType) * count);
156       else
157           for (std::size_t i = 0; i < count; ++i)
158               out[i] = read_little_endian<IntType>(stream);
159   }
160
161
162   // write_little_endian(s, values, N) : write integers in bulk to a little indian stream.
163   // This takes N integers from array values and writes them on stream s.
164   template <typename IntType>
165   inline void write_little_endian(std::ostream& stream, const IntType* values, std::size_t count) {
166       if (IsLittleEndian)
167           stream.write(reinterpret_cast<const char*>(values), sizeof(IntType) * count);
168       else
169           for (std::size_t i = 0; i < count; ++i)
170               write_little_endian<IntType>(stream, values[i]);
171   }
172
173
174   // read_leb_128(s, out, N) : read N signed integers from the stream s, putting them in
175   // the array out. The stream is assumed to be compressed using the signed LEB128 format.
176   // See https://en.wikipedia.org/wiki/LEB128 for a description of the compression scheme.
177   template <typename IntType>
178   inline void read_leb_128(std::istream& stream, IntType* out, std::size_t count) {
179
180       // Check the presence of our LEB128 magic string
181       char leb128MagicString[Leb128MagicStringSize];
182       stream.read(leb128MagicString, Leb128MagicStringSize);
183       assert(strncmp(Leb128MagicString, leb128MagicString, Leb128MagicStringSize) == 0);
184
185       static_assert(std::is_signed_v<IntType>, "Not implemented for unsigned types");
186
187       const std::uint32_t BUF_SIZE = 4096;
188       std::uint8_t buf[BUF_SIZE];
189
190       auto bytes_left = read_little_endian<std::uint32_t>(stream);
191
192       std::uint32_t buf_pos = BUF_SIZE;
193       for (std::size_t i = 0; i < count; ++i)
194       {
195           IntType result = 0;
196           size_t shift = 0;
197           do
198           {
199               if (buf_pos == BUF_SIZE)
200               {
201                   stream.read(reinterpret_cast<char*>(buf), std::min(bytes_left, BUF_SIZE));
202                   buf_pos = 0;
203               }
204
205               std::uint8_t byte = buf[buf_pos++];
206               --bytes_left;
207               result |= (byte & 0x7f) << shift;
208               shift += 7;
209
210               if ((byte & 0x80) == 0)
211               {
212                   out[i] = (sizeof(IntType) * 8 <= shift || (byte & 0x40) == 0) ? result
213                                                                                 : result | ~((1 << shift) - 1);
214                   break;
215               }
216           }
217           while (shift < sizeof(IntType) * 8);
218       }
219
220       assert(bytes_left == 0);
221   }
222
223
224   // write_leb_128(s, values, N) : write signed integers to a stream with LEB128 compression.
225   // This takes N integers from array values, compress them with the LEB128 algorithm and
226   // writes the result on the stream s.
227   // See https://en.wikipedia.org/wiki/LEB128 for a description of the compression scheme.
228   template <typename IntType>
229   inline void write_leb_128(std::ostream& stream, const IntType* values, std::size_t count) {
230
231       // Write our LEB128 magic string
232       stream.write(Leb128MagicString, Leb128MagicStringSize);
233
234       static_assert(std::is_signed_v<IntType>, "Not implemented for unsigned types");
235
236       std::uint32_t byte_count = 0;
237       for (std::size_t i = 0; i < count; ++i)
238       {
239           IntType value = values[i];
240           std::uint8_t byte;
241           do
242           {
243               byte = value & 0x7f;
244               value >>= 7;
245               ++byte_count;
246           }
247           while ((byte & 0x40) == 0 ? value != 0 : value != -1);
248       }
249
250       write_little_endian(stream, byte_count);
251
252       const std::uint32_t BUF_SIZE = 4096;
253       std::uint8_t buf[BUF_SIZE];
254       std::uint32_t buf_pos = 0;
255
256       auto flush = [&]() {
257           if (buf_pos > 0)
258           {
259               stream.write(reinterpret_cast<char*>(buf), buf_pos);
260               buf_pos = 0;
261           }
262       };
263
264       auto write = [&](std::uint8_t byte) {
265           buf[buf_pos++] = byte;
266           if (buf_pos == BUF_SIZE)
267               flush();
268       };
269
270       for (std::size_t i = 0; i < count; ++i)
271       {
272           IntType value = values[i];
273           while (true)
274           {
275               std::uint8_t byte = value & 0x7f;
276               value >>= 7;
277               if ((byte & 0x40) == 0 ? value == 0 : value == -1)
278               {
279                   write(byte);
280                   break;
281               }
282               write(byte | 0x80);
283           }
284       }
285
286       flush();
287   }
288
289 }  // namespace Stockfish::Eval::NNUE
290
291 #endif // #ifndef NNUE_COMMON_H_INCLUDED