diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 6c1a7d28fcf..985f256594f 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -18,6 +18,7 @@ #include "benchmark.h" #include "numa.h" +#include "misc.h" #include #include @@ -483,7 +484,7 @@ BenchmarkSetup setup_benchmark(std::istream& is) { float totalTime = 0; for (const auto& game : BenchmarkPositions) - for (size_t i = 0; i < game.size(); ++i) + for (usize i = 0; i < game.size(); ++i) totalTime += float(getCorrectedTime(i + 1)); float timeScaleFactor = static_cast(desiredTimeS * 1000) / totalTime; diff --git a/src/memory.cpp b/src/memory.cpp index a271b37c735..537ef20c1ea 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -68,7 +68,7 @@ namespace Stockfish { // availability of aligned_alloc(). Memory allocated with std_aligned_alloc() // must be freed with std_aligned_free(). -void* std_aligned_alloc(size_t alignment, size_t size) { +void* std_aligned_alloc(usize alignment, usize size) { #if defined(_ISOC11_SOURCE) return aligned_alloc(alignment, size); #elif defined(POSIXALIGNEDALLOC) @@ -102,19 +102,19 @@ void std_aligned_free(void* ptr) { #if defined(_WIN32) -static void* aligned_large_pages_alloc_windows([[maybe_unused]] size_t allocSize) { +static void* aligned_large_pages_alloc_windows([[maybe_unused]] usize allocSize) { return windows_try_with_large_page_priviliges( - [&](size_t largePageSize) { + [&](usize largePageSize) { // Round up size to full pages and allocate - allocSize = (allocSize + largePageSize - 1) & ~size_t(largePageSize - 1); + allocSize = (allocSize + largePageSize - 1) & ~usize(largePageSize - 1); return VirtualAlloc(nullptr, allocSize, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); }, []() { return (void*) nullptr; }); } -void* aligned_large_pages_alloc_with_hint(size_t allocSize, bool) { +void* aligned_large_pages_alloc_with_hint(usize allocSize, bool) { // Try to allocate large pages void* mem = aligned_large_pages_alloc_windows(allocSize); @@ -131,13 +131,13 @@ void* aligned_large_pages_alloc_with_hint(size_t allocSize, bool) { #if defined(__linux__) && defined(MAP_HUGE_SHIFT) && defined(__x86_64__) #define HAS_HUGE_PAGES -static std::map huge_pages; -static std::mutex huge_pages_mtx; +static std::map huge_pages; +static std::mutex huge_pages_mtx; -static void* try_huge_pages_alloc(size_t allocSize) { - size_t size = ((allocSize + HugePageSize - 1) / HugePageSize) * HugePageSize; - void* mem = mmap(NULL, size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | (30 << MAP_HUGE_SHIFT), -1, 0); +static void* try_huge_pages_alloc(usize allocSize) { + usize size = ((allocSize + HugePageSize - 1) / HugePageSize) * HugePageSize; + void* mem = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | (30 << MAP_HUGE_SHIFT), -1, 0); if (mem == MAP_FAILED) return nullptr; @@ -148,7 +148,7 @@ static void* try_huge_pages_alloc(size_t allocSize) { } #endif // defined(__linux__) && defined(MAP_HUGE_SHIFT) && defined(__x86_64__) -void* aligned_large_pages_alloc_with_hint(size_t allocSize, [[maybe_unused]] bool hugePageHint) { +void* aligned_large_pages_alloc_with_hint(usize allocSize, [[maybe_unused]] bool hugePageHint) { #ifdef HAS_HUGE_PAGES if (hugePageHint && allocSize >= HugePageSize) { @@ -159,14 +159,14 @@ void* aligned_large_pages_alloc_with_hint(size_t allocSize, [[maybe_unused]] boo #endif #if defined(__linux__) - constexpr size_t alignment = 2 * 1024 * 1024; // 2MB page size assumed + constexpr usize alignment = 2 * 1024 * 1024; // 2MB page size assumed #else - constexpr size_t alignment = 4096; // small page size assumed + constexpr usize alignment = 4096; // small page size assumed #endif // Round up to multiples of alignment - size_t size = ((allocSize + alignment - 1) / alignment) * alignment; - void* mem = std_aligned_alloc(alignment, size); + usize size = ((allocSize + alignment - 1) / alignment) * alignment; + void* mem = std_aligned_alloc(alignment, size); #if defined(MADV_HUGEPAGE) madvise(mem, size, MADV_HUGEPAGE); #endif @@ -175,7 +175,7 @@ void* aligned_large_pages_alloc_with_hint(size_t allocSize, [[maybe_unused]] boo #endif -void* aligned_large_pages_alloc(size_t size) { +void* aligned_large_pages_alloc(usize size) { return aligned_large_pages_alloc_with_hint(size, false); } @@ -183,8 +183,8 @@ bool has_large_pages() { #if defined(_WIN32) - constexpr size_t page_size = 2 * 1024 * 1024; // 2MB page size assumed - void* mem = aligned_large_pages_alloc_windows(page_size); + constexpr usize page_size = 2 * 1024 * 1024; // 2MB page size assumed + void* mem = aligned_large_pages_alloc_windows(page_size); if (mem == nullptr) { return false; diff --git a/src/memory.h b/src/memory.h index f2dea0d0a02..d5bccf34e4a 100644 --- a/src/memory.h +++ b/src/memory.h @@ -28,6 +28,7 @@ #include #include "types.h" +#include "misc.h" #if defined(_WIN64) @@ -61,14 +62,14 @@ using AdjustTokenPrivileges_t = namespace Stockfish { -constexpr size_t HugePageSize = size_t(1) << 30; +constexpr usize HugePageSize = usize(1) << 30; -void* std_aligned_alloc(size_t alignment, size_t size); +void* std_aligned_alloc(usize alignment, usize size); void std_aligned_free(void* ptr); // Memory aligned by page size, min alignment: 4096 bytes -void* aligned_large_pages_alloc_with_hint(size_t size, bool hugePageHint); -void* aligned_large_pages_alloc(size_t size); +void* aligned_large_pages_alloc_with_hint(usize size, bool hugePageHint); +void* aligned_large_pages_alloc(usize size); void aligned_large_pages_free(void* mem); bool has_large_pages(); @@ -96,15 +97,15 @@ void memory_deleter_array(T* ptr, FREE_FUNC free_func) { // Move back on the pointer to where the size is allocated - const size_t array_offset = std::max(sizeof(size_t), alignof(T)); - char* raw_memory = reinterpret_cast(ptr) - array_offset; + const usize array_offset = std::max(sizeof(usize), alignof(T)); + char* raw_memory = reinterpret_cast(ptr) - array_offset; if constexpr (!std::is_trivially_destructible_v) { - const size_t size = *reinterpret_cast(raw_memory); + const usize size = *reinterpret_cast(raw_memory); // Explicitly call the destructor for each element in reverse order - for (size_t i = size; i-- > 0;) + for (usize i = size; i-- > 0;) ptr[i].~T(); } @@ -123,19 +124,19 @@ inline std::enable_if_t, T*> memory_allocator(ALLOC_FUNC all // Allocates memory for an array of unknown bound and places it there with placement new template inline std::enable_if_t, std::remove_extent_t*> -memory_allocator(ALLOC_FUNC alloc_func, size_t num) { +memory_allocator(ALLOC_FUNC alloc_func, usize num) { using ElementType = std::remove_extent_t; - const size_t array_offset = std::max(sizeof(size_t), alignof(ElementType)); + const usize array_offset = std::max(sizeof(usize), alignof(ElementType)); // Save the array size in the memory location char* raw_memory = reinterpret_cast(alloc_func(array_offset + num * sizeof(ElementType))); ASSERT_ALIGNED(raw_memory, alignof(T)); - new (raw_memory) size_t(num); + new (raw_memory) usize(num); - for (size_t i = 0; i < num; ++i) + for (usize i = 0; i < num; ++i) new (raw_memory + array_offset + i * sizeof(ElementType)) ElementType(); // Need to return the pointer at the start of the array so that @@ -178,7 +179,7 @@ std::enable_if_t, LargePagePtr> make_unique_large_page(Ar // make_unique_large_page for arrays of unknown bound template -std::enable_if_t, LargePagePtr> make_unique_large_page(size_t num) { +std::enable_if_t, LargePagePtr> make_unique_large_page(usize num) { using ElementType = std::remove_extent_t; static_assert(alignof(ElementType) <= 4096, @@ -214,7 +215,7 @@ using AlignedPtr = // make_unique_aligned for single objects template std::enable_if_t, AlignedPtr> make_unique_aligned(Args&&... args) { - const auto func = [](size_t size) { return std_aligned_alloc(alignof(T), size); }; + const auto func = [](usize size) { return std_aligned_alloc(alignof(T), size); }; T* obj = memory_allocator(func, std::forward(args)...); return AlignedPtr(obj); @@ -222,10 +223,10 @@ std::enable_if_t, AlignedPtr> make_unique_aligned(Args&&. // make_unique_aligned for arrays of unknown bound template -std::enable_if_t, AlignedPtr> make_unique_aligned(size_t num) { +std::enable_if_t, AlignedPtr> make_unique_aligned(usize num) { using ElementType = std::remove_extent_t; - const auto func = [](size_t size) { return std_aligned_alloc(alignof(ElementType), size); }; + const auto func = [](usize size) { return std_aligned_alloc(alignof(ElementType), size); }; ElementType* memory = memory_allocator(func, num); return AlignedPtr(memory); @@ -256,7 +257,7 @@ auto windows_try_with_large_page_priviliges([[maybe_unused]] FuncYesT&& fyes, Fu HANDLE hProcessToken{}; LUID luid{}; - const size_t largePageSize = GetLargePageMinimum(); + const usize largePageSize = GetLargePageMinimum(); if (!largePageSize) return fno(); diff --git a/src/misc.h b/src/misc.h index 0a6a846c284..ccf639374b4 100644 --- a/src/misc.h +++ b/src/misc.h @@ -349,7 +349,7 @@ template class RelaxedAtomic { static constexpr bool UseAtomic = #ifdef USE_SLOPPY_ATOMICS - !std::atomic::is_always_lock_free || sizeof(T) > sizeof(size_t); + !std::atomic::is_always_lock_free || sizeof(T) > sizeof(usize); #else true; #endif diff --git a/src/movegen.h b/src/movegen.h index 7f209f92a7e..ee79ea27ee4 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -20,8 +20,8 @@ #define MOVEGEN_H_INCLUDED #include // IWYU pragma: keep -#include +#include "misc.h" #include "types.h" namespace Stockfish { @@ -61,7 +61,7 @@ struct MoveList { last(generate(pos, moveList)) {} const Move* begin() const { return moveList; } const Move* end() const { return last; } - size_t size() const { return last - moveList; } + usize size() const { return last - moveList; } bool contains(Move move) const { return std::find(begin(), end(), move) != end(); } private: diff --git a/src/nnue/nnz_helper.h b/src/nnue/nnz_helper.h index b7542c7bcc4..00202f524a7 100644 --- a/src/nnue/nnz_helper.h +++ b/src/nnue/nnz_helper.h @@ -26,7 +26,7 @@ namespace Stockfish::Eval::NNUE { -template +template struct NNZInfo { #if defined(USE_AVX512) @@ -146,7 +146,7 @@ struct NNZInfo { } else { - size_t bytes = sizeof(neurons1) / 32; + usize bytes = sizeof(neurons1) / 32; memcpy(out, &m1, bytes); out += bytes; memcpy(out, &m2, bytes); diff --git a/src/nnue/simd.h b/src/nnue/simd.h index bf6c7873243..d14a7454c1f 100644 --- a/src/nnue/simd.h +++ b/src/nnue/simd.h @@ -171,8 +171,7 @@ inline __m128i _mm_cvtsi64_si128(i64 val) { #ifdef __wasm__ #define vec_convert_8_16(a) wasm_i16x8_load8x8(reinterpret_cast(&a)) #else - #define vec_convert_8_16(a) \ - _mm_cvtepi8_epi16(_mm_cvtsi64_si128(static_cast(a))) + #define vec_convert_8_16(a) _mm_cvtepi8_epi16(_mm_cvtsi64_si128(static_cast(a))) #endif #else // Credit: Yoshie2000 diff --git a/src/search.h b/src/search.h index 04c69606f08..ca605c68f54 100644 --- a/src/search.h +++ b/src/search.h @@ -375,7 +375,7 @@ class Worker { LimitsType limits; - size_t pvIdx, pvLast; + usize pvIdx, pvLast; RelaxedAtomic nodes, tbHits, bestMoveChanges; int selDepth, nmpMinPly; diff --git a/src/thread_win32_osx.h b/src/thread_win32_osx.h index 5a8d43a2e77..4b824e29679 100644 --- a/src/thread_win32_osx.h +++ b/src/thread_win32_osx.h @@ -37,7 +37,7 @@ namespace Stockfish { class NativeThread { pthread_t thread; - static constexpr size_t TH_STACK_SIZE = 8 * 1024 * 1024; + static constexpr usize TH_STACK_SIZE = 8 * 1024 * 1024; public: template diff --git a/src/tune.h b/src/tune.h index 4ce6e759fde..bc3fb355360 100644 --- a/src/tune.h +++ b/src/tune.h @@ -19,13 +19,14 @@ #ifndef TUNE_H_INCLUDED #define TUNE_H_INCLUDED -#include #include #include #include // IWYU pragma: keep #include #include +#include "misc.h" + namespace Stockfish { class OptionsMap; @@ -132,9 +133,9 @@ class Tune { } // Template specialization for arrays: recursively handle multi-dimensional arrays - template + template int add(const SetRange& range, std::string&& names, T (&value)[N], Args&&... args) { - for (size_t i = 0; i < N; i++) + for (usize i = 0; i < N; i++) add(range, next(names, i == N - 1) + "[" + std::to_string(i) + "]", value[i]); return add(range, std::move(names), args...); }