2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2021 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
28 #include <immintrin.h>
30 #elif defined(USE_SSE41)
31 #include <smmintrin.h>
33 #elif defined(USE_SSSE3)
34 #include <tmmintrin.h>
36 #elif defined(USE_SSE2)
37 #include <emmintrin.h>
39 #elif defined(USE_MMX)
42 #elif defined(USE_NEON)
46 namespace Stockfish::Eval::NNUE {
48 // Version of the evaluation file
49 constexpr std::uint32_t Version = 0x7AF32F16u;
51 // Constant used in evaluation value calculation
52 constexpr int OutputScale = 16;
53 constexpr int WeightScaleBits = 6;
55 // Size of cache line (in bytes)
56 constexpr std::size_t CacheLineSize = 64;
58 // SIMD width (in bytes)
60 constexpr std::size_t SimdWidth = 32;
62 #elif defined(USE_SSE2)
63 constexpr std::size_t SimdWidth = 16;
65 #elif defined(USE_MMX)
66 constexpr std::size_t SimdWidth = 8;
68 #elif defined(USE_NEON)
69 constexpr std::size_t SimdWidth = 16;
72 constexpr std::size_t MaxSimdWidth = 32;
74 // Type of input feature after conversion
75 using TransformedFeatureType = std::uint8_t;
76 using IndexType = std::uint32_t;
78 // Round n up to be a multiple of base
79 template <typename IntType>
80 constexpr IntType ceil_to_multiple(IntType n, IntType base) {
81 return (n + base - 1) / base * base;
84 // read_little_endian() is our utility to read an integer (signed or unsigned, any size)
85 // from a stream in little-endian order. We swap the byte order after the read if
86 // necessary to return a result with the byte ordering of the compiling machine.
87 template <typename IntType>
88 inline IntType read_little_endian(std::istream& stream) {
91 std::uint8_t u[sizeof(IntType)];
92 typename std::make_unsigned<IntType>::type v = 0;
94 stream.read(reinterpret_cast<char*>(u), sizeof(IntType));
95 for (std::size_t i = 0; i < sizeof(IntType); ++i)
96 v = (v << 8) | u[sizeof(IntType) - i - 1];
98 std::memcpy(&result, &v, sizeof(IntType));
102 template <typename IntType>
103 inline void write_little_endian(std::ostream& stream, IntType value) {
105 std::uint8_t u[sizeof(IntType)];
106 typename std::make_unsigned<IntType>::type v = value;
109 // if constexpr to silence the warning about shift by 8
110 if constexpr (sizeof(IntType) > 1) {
111 for (; i + 1 < sizeof(IntType); ++i) {
118 stream.write(reinterpret_cast<char*>(u), sizeof(IntType));
120 } // namespace Stockfish::Eval::NNUE
122 #endif // #ifndef NNUE_COMMON_H_INCLUDED