]> git.sesse.net Git - stockfish/blob - src/nnue/nnue_common.h
Read NNUE net faster
[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 #include "../misc.h"  // for IsLittleEndian
28
29 #if defined(USE_AVX2)
30 #include <immintrin.h>
31
32 #elif defined(USE_SSE41)
33 #include <smmintrin.h>
34
35 #elif defined(USE_SSSE3)
36 #include <tmmintrin.h>
37
38 #elif defined(USE_SSE2)
39 #include <emmintrin.h>
40
41 #elif defined(USE_MMX)
42 #include <mmintrin.h>
43
44 #elif defined(USE_NEON)
45 #include <arm_neon.h>
46 #endif
47
48 namespace Stockfish::Eval::NNUE {
49
50   // Version of the evaluation file
51   constexpr std::uint32_t Version = 0x7AF32F20u;
52
53   // Constant used in evaluation value calculation
54   constexpr int OutputScale = 16;
55   constexpr int WeightScaleBits = 6;
56
57   // Size of cache line (in bytes)
58   constexpr std::size_t CacheLineSize = 64;
59
60   // SIMD width (in bytes)
61   #if defined(USE_AVX2)
62   constexpr std::size_t SimdWidth = 32;
63
64   #elif defined(USE_SSE2)
65   constexpr std::size_t SimdWidth = 16;
66
67   #elif defined(USE_MMX)
68   constexpr std::size_t SimdWidth = 8;
69
70   #elif defined(USE_NEON)
71   constexpr std::size_t SimdWidth = 16;
72   #endif
73
74   constexpr std::size_t MaxSimdWidth = 32;
75
76   // Type of input feature after conversion
77   using TransformedFeatureType = std::uint8_t;
78   using IndexType = std::uint32_t;
79
80   // Round n up to be a multiple of base
81   template <typename IntType>
82   constexpr IntType ceil_to_multiple(IntType n, IntType base) {
83       return (n + base - 1) / base * base;
84   }
85
86   // read_little_endian() is our utility to read an integer (signed or unsigned, any size)
87   // from a stream in little-endian order. We swap the byte order after the read if
88   // necessary to return a result with the byte ordering of the compiling machine.
89   template <typename IntType>
90   inline IntType read_little_endian(std::istream& stream) {
91       IntType result;
92
93       if (IsLittleEndian)
94           stream.read(reinterpret_cast<char*>(&result), sizeof(IntType));
95       else
96       {
97           std::uint8_t u[sizeof(IntType)];
98           typename std::make_unsigned<IntType>::type v = 0;
99
100           stream.read(reinterpret_cast<char*>(u), sizeof(IntType));
101           for (std::size_t i = 0; i < sizeof(IntType); ++i)
102               v = (v << 8) | u[sizeof(IntType) - i - 1];
103
104           std::memcpy(&result, &v, sizeof(IntType));
105       }
106
107       return result;
108   }
109
110   // write_little_endian() is our utility to write an integer (signed or unsigned, any size)
111   // to a stream in little-endian order. We swap the byte order before the write if
112   // necessary to always write in little endian order, independantly of the byte
113   // ordering of the compiling machine.
114   template <typename IntType>
115   inline void write_little_endian(std::ostream& stream, IntType value) {
116
117       if (IsLittleEndian)
118           stream.write(reinterpret_cast<const char*>(&value), sizeof(IntType));
119       else
120       {
121           std::uint8_t u[sizeof(IntType)];
122           typename std::make_unsigned<IntType>::type v = value;
123
124           std::size_t i = 0;
125           // if constexpr to silence the warning about shift by 8
126           if constexpr (sizeof(IntType) > 1)
127           {
128             for (; i + 1 < sizeof(IntType); ++i)
129             {
130                 u[i] = v;
131                 v >>= 8;
132             }
133           }
134           u[i] = v;
135
136           stream.write(reinterpret_cast<char*>(u), sizeof(IntType));
137       }
138   }
139
140   // read_little_endian(s, out, N) : read integers in bulk from a little indian stream.
141   // This reads N integers from stream s and put them in array out.
142   template <typename IntType>
143   inline void read_little_endian(std::istream& stream, IntType* out, std::size_t count) {
144       if (IsLittleEndian)
145           stream.read(reinterpret_cast<char*>(out), sizeof(IntType) * count);
146       else
147           for (std::size_t i = 0; i < count; ++i)
148               out[i] = read_little_endian<IntType>(stream);
149   }
150
151   // write_little_endian(s, out, N) : write integers in bulk to a little indian stream.
152   // This takes N integers from array values and writes them on stream s.
153   template <typename IntType>
154   inline void write_little_endian(std::ostream& stream, const IntType* values, std::size_t count) {
155       if (IsLittleEndian)
156           stream.write(reinterpret_cast<const char*>(values), sizeof(IntType) * count);
157       else
158           for (std::size_t i = 0; i < count; ++i)
159               write_little_endian<IntType>(stream, values[i]);
160   }
161
162 }  // namespace Stockfish::Eval::NNUE
163
164 #endif // #ifndef NNUE_COMMON_H_INCLUDED