From d3f0db101141ff7fdbbbc3a71c894f81250867cd Mon Sep 17 00:00:00 2001 From: MinetaS Date: Sat, 21 Sep 2024 03:07:18 +0900 Subject: [PATCH] Update * Renamed __popcount_use_table to __popcount_table. * [PPC] Added PowerPC support from ppc32 to Power10. --- src/Makefile | 6 +++- src/arch/i386/arch.h | 2 +- src/arch/ppc/Makefile | 48 +++++++++++++++++++++++++++++++ src/arch/ppc/arch.h | 66 +++++++++++++++++++++++++++++++++++++++++++ src/common.h | 11 +++++--- 5 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 src/arch/ppc/Makefile create mode 100644 src/arch/ppc/arch.h diff --git a/src/Makefile b/src/Makefile index 0648ce04df2..1b4b1155e39 100644 --- a/src/Makefile +++ b/src/Makefile @@ -198,7 +198,7 @@ ifeq ($(COMP),gcc) -Wshadow else ifeq ($(COMP),clang) SF_CXXFLAGS += -pedantic -Wextra -Wcast-qual -Wconditional-uninitialized \ - -Wmissing-prototypes -Wshadow + -Wmissing-prototypes -Wshadow else ifeq ($(COMP),icx) SF_CXXFLAGS += -Wabi -Wmissing-declarations -Wmissing-prototypes -Wshadow endif @@ -274,6 +274,8 @@ ifeq ($(ARCH),) endif ifeq ($(ARCH),native) + # -march=native is not available on some architectures. + # Need some automatic detection here. ARCH_NATIVE := y SF_CXXFLAGS += -march=native -DARCH_NATIVE else @@ -281,6 +283,8 @@ else ARCH_FAMILY := i386 else ifneq ($(filter arm%,$(ARCH)),) ARCH_FAMILY := arm + else ifneq ($(filter ppc%,$(ARCH)),) + ARCH_FAMILY := ppc else ARCH_FAMILY := generic endif diff --git a/src/arch/i386/arch.h b/src/arch/i386/arch.h index 1ef79088240..b119989d5f2 100644 --- a/src/arch/i386/arch.h +++ b/src/arch/i386/arch.h @@ -136,7 +136,7 @@ inline unsigned int ArchImpl::popcount(T n) { return _mm_popcnt_u32(std::uint32_t(n)); #else if constexpr (!is_64bit() && sizeof(T) == 8) - return __popcount_use_table(n); + return __popcount_table(n); else return __popcount_value(n); #endif diff --git a/src/arch/ppc/Makefile b/src/arch/ppc/Makefile new file mode 100644 index 00000000000..d27badfecde --- /dev/null +++ b/src/arch/ppc/Makefile @@ -0,0 +1,48 @@ +define HELP_STRING_PPC +To build Stockfish, run the following command: + +make [ARCH=] + +Build presets for PowerPC architecture: + +ppc-power10 > Power10 +ppc-power9 > Power9 +ppc-power8 > Power8 +ppc-power7 > Power7 +ppc-64 > PowerPC 64-bit +ppc-32 > PowerPC 32-bit + +endef +export HELP_STRING_PPC + +ifneq ($(filter $(COMP),gcc clang icx),) + +ifeq ($(ARCH),ppc-power10) + SF_CXXFLAGS += -mcpu=power10 +else ifeq ($(ARCH),ppc-power9) + SF_CXXFLAGS += -mcpu=power9 +else ifeq ($(ARCH),ppc-power8) + SF_CXXFLAGS += -mcpu=power8 +else ifeq ($(ARCH),ppc-power7) + SF_CXXFLAGS += -mcpu=power7 +else ifeq ($(ARCH),ppc-64) + SF_CXXFLAGS += -mcpu=powerpc64 +else ifeq ($(ARCH),ppc-32) + SF_CXXFLAGS += -mcpu=powerpc +endif + +endif # gcc clang icx + +.PHONY: help-arch config-sanity-arch + +help-arch: + @echo "$${HELP_STRING_PPC}" + +config-sanity-arch: + @[ "$(ARCH)" = "ppc-power10" -o \ + "$(ARCH)" = "ppc-power9" -o \ + "$(ARCH)" = "ppc-power8" -o \ + "$(ARCH)" = "ppc-power7" -o \ + "$(ARCH)" = "ppc-64" -o \ + "$(ARCH)" = "ppc-32" \ + ] diff --git a/src/arch/ppc/arch.h b/src/arch/ppc/arch.h new file mode 100644 index 00000000000..e7d69ef824e --- /dev/null +++ b/src/arch/ppc/arch.h @@ -0,0 +1,66 @@ +/* + Stockfish, a UCI chess playing engine derived from Glaurung 2.1 + Copyright (C) 2004-2024 The Stockfish developers (see AUTHORS file) + + Stockfish is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Stockfish is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef PPC_ARCH_H_INCLUDED +#define PPC_ARCH_H_INCLUDED + +#if !defined(__PPC__) +#error "Not supported in the current architecture." +#endif + +#include +#include +#include + +#include "common.h" + +#if STOCKFISH_COMPILER == STOCKFISH_COMPILER_GCC +#include +#endif + +namespace Stockfish { + +#ifdef __PPC64__ +inline constexpr bool ArchImpl::Is64Bit = true; +#else +inline constexpr bool ArchImpl::Is64Bit = false; +#endif + +inline constexpr bool ArchImpl::UsePEXT = false; + +template +inline void ArchImpl::prefetch(const void* m) { + __dcbt(m); +} + +template +inline unsigned int ArchImpl::popcount(T n) { + static_assert(std::is_integral_v && sizeof(T) <= 8); + + // This will generate POPCNTD instruction on PWR7 and later. + return __popcount_value(n); +} + +template +inline T ArchImpl::pext([[maybe_unused]] T n, [[maybe_unused]] T mask) { + return 0; +} + +} // namespace Stockfish + +#endif // I386_ARCH_H_INCLUDED diff --git a/src/common.h b/src/common.h index eaf5c2ed551..bcda68cbf3f 100644 --- a/src/common.h +++ b/src/common.h @@ -100,10 +100,9 @@ enum class PrefetchHint; // This struct is used to provide generalized functionalities that might have // different implementations depending on the target architecture. Each -// function defined in this struct is specialized in arch.h file respectively. +// member and function defined in this struct is specialized in arch.h file +// respectively. struct ArchImpl { - /// Does the struct name fit to its purpose? - static const bool Is64Bit; static const bool UsePEXT; // used in Bitboard @@ -130,7 +129,7 @@ constexpr bool use_pext() { return ArchImpl::UsePEXT; } // 32-bit environment. Therefore we provide two versions of it and leave it // for each platform to decide which one to use. template -inline int __popcount_use_table(T n) { +inline int __popcount_table(T n) { static_assert(std::is_integral_v && sizeof(T) % 2 == 0); static const std::array popcntTable = [] { @@ -196,6 +195,10 @@ inline T pext(T n, T mask) { #include "arch/arm/arch.h" +#elif defined(__PPC__) + + #include "arch/ppc/arch.h" + #else #include "arch/generic/arch.h"