2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2022 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 // SIMD width (in bytes)
62 constexpr std::size_t SimdWidth = 32;
64 #elif defined(USE_SSE2)
65 constexpr std::size_t SimdWidth = 16;
67 #elif defined(USE_MMX)
68 constexpr std::size_t SimdWidth = 8;
70 #elif defined(USE_NEON)
71 constexpr std::size_t SimdWidth = 16;
74 constexpr std::size_t MaxSimdWidth = 32;
76 // Type of input feature after conversion
77 using TransformedFeatureType = std::uint8_t;
78 using IndexType = std::uint32_t;
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;
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) {
94 stream.read(reinterpret_cast<char*>(&result), sizeof(IntType));
97 std::uint8_t u[sizeof(IntType)];
98 typename std::make_unsigned<IntType>::type v = 0;
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];
104 std::memcpy(&result, &v, sizeof(IntType));
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, independently of the byte
113 // ordering of the compiling machine.
114 template <typename IntType>
115 inline void write_little_endian(std::ostream& stream, IntType value) {
118 stream.write(reinterpret_cast<const char*>(&value), sizeof(IntType));
121 std::uint8_t u[sizeof(IntType)];
122 typename std::make_unsigned<IntType>::type v = value;
125 // if constexpr to silence the warning about shift by 8
126 if constexpr (sizeof(IntType) > 1)
128 for (; i + 1 < sizeof(IntType); ++i)
130 u[i] = (std::uint8_t)v;
134 u[i] = (std::uint8_t)v;
136 stream.write(reinterpret_cast<char*>(u), sizeof(IntType));
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) {
145 stream.read(reinterpret_cast<char*>(out), sizeof(IntType) * count);
147 for (std::size_t i = 0; i < count; ++i)
148 out[i] = read_little_endian<IntType>(stream);
151 // write_little_endian(s, values, 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) {
156 stream.write(reinterpret_cast<const char*>(values), sizeof(IntType) * count);
158 for (std::size_t i = 0; i < count; ++i)
159 write_little_endian<IntType>(stream, values[i]);
162 } // namespace Stockfish::Eval::NNUE
164 #endif // #ifndef NNUE_COMMON_H_INCLUDED