Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++20 #456

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

C++20 #456

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ifeq ($(EXE),)
EXE=RubiChess
endif

CXXFLAGS=-std=c++11 -Wall -pedantic -Wextra -Wshadow
CXXFLAGS=-std=c++2a -Wall -pedantic -Wextra -Wshadow
CFLAGS=-Wall -pedantic -Wextra -Wshadow -Wno-implicit-function-declaration


Expand Down
25 changes: 20 additions & 5 deletions src/Makefile.clang
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ CXX="$(CompilerBaseDir)\bin\clang-cl.exe"
LD="$(CompilerBaseDir)\bin\lld-link.exe"
PROFDATATOOLEXE="$(CompilerBaseDir)\bin\llvm-profdata.exe"
!ELSEIF "$(COMP)" == "icx"
CompilerBaseDir=C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows
CompilerBaseDir=C:\Program Files (x86)\Intel\oneAPI\compiler\latest
!IF EXIST("$(CompilerBaseDir)\windows") # Path up to Intel oneAPI 2023.1
CompilerBaseDir=$(CompilerBaseDir)\windows
!ENDIF
CXX="$(CompilerBaseDir)\bin\icx-cl.exe"
LD="$(CompilerBaseDir)\bin-llvm\lld-link.exe"
PROFDATATOOLEXE="$(CompilerBaseDir)\bin-llvm\llvm-profdata.exe"
!IF EXISTS("$(CompilerBaseDir)\bin-llvm") # Path up to Intel oneAPI 2023.1
CompilerClangDir=$(CompilerBaseDir)\bin-llvm
!ELSEIF EXISTS("$(CompilerBaseDir)\bin\compiler") # Path of Intel oneAPI 2024.0
CompilerClangDir=$(CompilerBaseDir)\bin\compiler
!ELSE
!ERROR Folder with Linker tools not found.
!ENDIF
LD="$(CompilerClangDir)\lld-link.exe"
PROFDATATOOLEXE="$(CompilerClangDir)\llvm-profdata.exe"
!ELSE
!ERROR Not supported compiler $(COMP)
!ENDIF
Expand All @@ -38,6 +48,11 @@ OBJ=*.obj
RELDIR=Release-clang
ZLIBDIR=zlib
CXXFLAGS=/EHsc /O2 /Oi /Ot /c -flto -fuse-ld=lld
!IF "$(COMP)" == "clang"
CPPFLAGS=/std:c++20
!ELSE
CPPFLAGS=-Qstd=c++20
!ENDIF
LDFLAGS=/OPT:REF /OPT:ICF advapi32.lib
PROFEXE=RubiChess-WinProf
NETURL=https://github.com/Matthies/NN/raw/main/
Expand Down Expand Up @@ -103,7 +118,7 @@ ARCH=native

# nativeflags
!IF "$(ARCH)"=="native" && !EXIST($(NATIVEFLAGS)) && "$(VSCMD_ARG_HOST_ARCH)"=="$(VSCMD_ARG_TGT_ARCH)"
!IF [@$(CXX) /GX -DCPUTEST $(CPUTEST).cpp -o $(CPUTEST) && @$(CPUTEST) > $(NATIVEFLAGS)] != 0
!IF [$(CXX) $(CPPFLAGS) /GX -DCPUTEST $(CPUTEST).cpp -o $(CPUTEST) && @$(CPUTEST) > $(NATIVEFLAGS)] != 0
!ERROR Cannot compile and/or run $(CPUTEST) to determine cpu flags on this machine!
!ENDIF
!ENDIF
Expand Down Expand Up @@ -221,7 +236,7 @@ compile: $(ZLIBDIR)\zlib.lib
!IFNDEF GITID
@FOR /F "tokens=2" %A IN ('cmd /c "($(GIT) show --name-only --abbrev-commit || echo commit unknown) | findstr /r /c:"^commit"" ') DO @(set GITID=%A&& nmake -f Makefile.clang $* /$(MAKEFLAGS) && exit /B)
!ELSE
@$(CXX) $(CXXFLAGS) $(ARCHFLAGS) $(SOURCE) $(PROFILEFLAGS) -D GITVER=\"$(GITVER)\" -D GITID=\"$(GITID)\"
@$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(ARCHFLAGS) $(SOURCE) $(PROFILEFLAGS) -D GITVER=\"$(GITVER)\" -D GITID=\"$(GITID)\"
!IFDEF USEPROFLIB
@$(LD) $(LDFLAGS) /OUT:$(PROFEXE).exe $(OBJ) "$(USEPROFLIB)"
!ELSE
Expand Down
2 changes: 1 addition & 1 deletion src/RubiChess.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-std=c++11" />
<Add option="-std=c++20" />
<Add option="-fexceptions" />
<Add option="-march=znver1" />
<Add option="-DUSE_AVX2" />
Expand Down
147 changes: 13 additions & 134 deletions src/RubiChess.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <bit>
#include <iostream>
#include <iomanip>
#include <stdarg.h>
Expand Down Expand Up @@ -211,150 +212,28 @@ typedef unsigned int PieceType;
#define MORETHANONE(x) ((x) & ((x) - 1))
#define ONEORZERO(x) (!MORETHANONE(x))

template <typename T> int popcount_legacy(T v)
{
v = v - ((v >> 1) & (T)~(T)0 / 3);
v = (v & (T)~(T)0 / 15 * 3) + ((v >> 2) & (T)~(T)0 / 15 * 3);
v = (v + (v >> 4)) & (T)~(T)0 / 255 * 15;
return (T)(v * ((T)~(T)0 / 255)) >> (sizeof(T) - 1) * CHAR_BIT;
}
//
// Use C++20 intrinsics
//
#define POPCOUNT(x) popcount(x)
#define POPCOUNT32(x) popcount(x)
#define GETMSB(i,x) (i = (sizeof(x) * 8 - countl_zero(x) - 1))
#define GETLSB(i,x) (i = countr_zero(x))

#if defined(_MSC_VER)
#ifdef USE_BMI1
#include <immintrin.h>
#define GETLSB32(i,x) (i =(int) _tzcnt_u32(x))
#ifdef _M_X64
#define GETLSB(i,x) (i =(int) _tzcnt_u64(x))
inline int pullLsb(U64* x) {
int i;
i = (int)_tzcnt_u64(*x);
*x = _blsr_u64(*x);
return i;
}
#define GETMSB(i,x) (i = 63 ^ (int) _lzcnt_u64(x))
inline int pullMsb(U64* x) {
int i;
i = 63 ^ (int)_lzcnt_u64(*x);
*x ^= (1ULL << i);
return i;
}
#else // _M_X64
#define GETLSB(i,x) (i =(int) ((x) & 0xffffffff ? _tzcnt_u32((x) & 0xffffffff) : _tzcnt_u32((x) >> 32) + 32))
inline int pullLsb(U64* x) {
int i;
i = (int)(*(x) & 0xffffffff ? _tzcnt_u32(*(x) & 0xffffffff) : _tzcnt_u32(*(x) >> 32) + 32);
*x ^= (1ULL << i);
return i;
}
#define GETMSB(i,x) (i = 63 ^ (int) ((x) >> 32 ? _lzcnt_u32((x) >> 32) : _lzcnt_u32((x) & 0xffffffff) + 32))
inline int pullMsb(U64* x) {
int i;
i = 63 ^ (int)((*(x) >> 32) ? _lzcnt_u32(*(x) >> 32) : _lzcnt_u32(*(x) & 0xffffffff) + 32);
*x ^= (1ULL << i);
unsigned int i;
GETMSB(i, *x);
*x ^= BITSET(i);
return i;
}
#endif
#else // USE_BMI1

#define GETLSB32(i,x) _BitScanForward((DWORD*)&(i), (x))
#if defined(_M_X64) || defined(_M_ARM64)
#define GETLSB(i,x) _BitScanForward64((DWORD*)&(i), (x))
inline int pullLsb(U64* x) {
DWORD i;
_BitScanForward64(&i, *x);
unsigned int i;
GETLSB(i, *x);
*x &= *x - 1;
return i;
}
#define GETMSB(i,x) _BitScanReverse64((DWORD*)&(i), (x))
inline int pullMsb(U64* x) {
DWORD i;
_BitScanReverse64(&i, *x);
*x ^= (1ULL << i);
return i;
}
#else // _M_X64 || _M_ARM64
#define GETLSB(i,x) ((x) & 0xffffffff ? _BitScanForward((DWORD*)&(i), (x) & 0xffffffff) : (_BitScanForward((DWORD*)&(i), (x) >> 32), i += 32))
inline int pullLsb(U64* x) {
DWORD i;
(*(x) & 0xffffffff ? _BitScanForward(&i, *(x) & 0xffffffff) : (_BitScanForward(&i, *(x) >> 32), i += 32));
*x ^= (1ULL << i);
return i;
}
#define GETMSB(i,x) (((x) >> 32) ? (_BitScanReverse((DWORD*)&i, (x) >> 32), (i) += 32) : _BitScanReverse((DWORD*)&i, (x) & 0xffffffff))
inline int pullMsb(U64* x) {
DWORD i;
(*(x) >> 32 ? _BitScanReverse(&i, *(x) >> 32) : (_BitScanReverse(&i, *(x) & 0xffffffff), i += 32));
*x ^= (1ULL << i);
return i;
}



#endif
#endif
#if !defined(__clang_major__) && (defined(_M_ARM) || defined(_M_ARM64))
#define POPCOUNT(x) popcount_legacy(x)
#define POPCOUNT32(x) popcount_legacy(x)
#else
#ifdef _M_X64
#define POPCOUNT(x) (int)(__popcnt64(x))
#define POPCOUNT32(x) __popcnt(x)
#else
#define POPCOUNT(x) (int)(__popcnt((unsigned int)((x) >> 32)) + __popcnt((unsigned int)((x) & 0xffffffff)))
#define POPCOUNT32(x) __popcnt(x)
#endif
#endif
#else // _MSC_VER
#ifdef USE_BMI1
#define GETLSB32(i,x) (i =(int) _tzcnt_u32(x))
#ifdef IS_64BIT
#define GETLSB(i,x) (i = _tzcnt_u64(x))
inline int pullLsb(U64* x) {
int i = _tzcnt_u64(*x);
*x = _blsr_u64(*x);
return i;
}
#define GETMSB(i,x) (i = (63 ^ _lzcnt_u64(x)))
inline int pullMsb(U64* x) {
int i = 63 ^ _lzcnt_u64(*x);
*x ^= (1ULL << i);
return i;
}
#else // IS_64BIT
#define GETLSB(i,x) (i =(int) ((x) & 0xffffffff ? _tzcnt_u32((x) & 0xffffffff) : _tzcnt_u32((x) >> 32) + 32))
inline int pullLsb(U64* x) {
int i;
i = (int)(*(x) & 0xffffffff ? _tzcnt_u32(*(x) & 0xffffffff) : _tzcnt_u32(*(x) >> 32) + 32);
*x ^= (1ULL << i);
return i;
}
#define GETMSB(i,x) (i = 63 ^ (int) ((x) >> 32 ? _lzcnt_u32((x) >> 32) : _lzcnt_u32((x) & 0xffffffff) + 32))
inline int pullMsb(U64* x) {
int i;
i = 63 ^ (int)((*(x) >> 32) ? _lzcnt_u32(*(x) >> 32) : _lzcnt_u32(*(x) & 0xffffffff) + 32);
*x ^= (1ULL << i);
return i;
}
#endif
#else
#define GETLSB(i,x) (i = __builtin_ctzll(x))
#define GETLSB32(i,x) (i =(int) __builtin_ctz(x))
inline int pullLsb(U64* x) {
int i = __builtin_ctzll(*x);
*x &= *x - 1;
return i;
}
#define GETMSB(i,x) (i = (63 ^ __builtin_clzll(x)))
inline int pullMsb(U64* x) {
int i = 63 - __builtin_clzll(*x);
*x ^= (1ULL << i);
return i;
}
#endif
#define POPCOUNT(x) __builtin_popcountll(x)
#define POPCOUNT32(x) __builtin_popcount(x)
#endif

enum Color { WHITE, BLACK };
#define WHITEBB 0x55aa55aa55aa55aa
#define BLACKBB 0xaa55aa55aa55aa55
Expand Down
8 changes: 8 additions & 0 deletions src/RubiChess.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);USE_SSE2;USE_SSSE3;USE_POPCNT;USE_BMI1;USE_AVX2;USE_ZLIB</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -222,6 +223,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);USE_SSE2;USE_SSSE3;USE_POPCNT;USE_BMI1;USE_AVX2;USE_ZLIB</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -255,6 +257,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);USE_NEON;USE_ZLIB</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -288,6 +291,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);USE_NEON;USE_ARM64;USE_ZLIB</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -325,6 +329,7 @@
<PreprocessorDefinitions>_CONSOLE;%(PreprocessorDefinitions);USE_SSE2;USE_SSSE3;USE_POPCNT;USE_BMI1;USE_AVX2;USE_ZLIB</PreprocessorDefinitions>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -366,6 +371,7 @@
<PreprocessorDefinitions>_CONSOLE;%(PreprocessorDefinitions);USE_SSE2;USE_SSSE3;USE_POPCNT;xUSE_BMI1;USE_AVX2;USE_ZLIB</PreprocessorDefinitions>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -407,6 +413,7 @@
<PreprocessorDefinitions>_CONSOLE;%(PreprocessorDefinitions);USE_NEON;USE_ZLIB</PreprocessorDefinitions>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -448,6 +455,7 @@
<PreprocessorDefinitions>_CONSOLE;%(PreprocessorDefinitions);USE_NEON;USE_ARM64;USE_ZLIB</PreprocessorDefinitions>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down
6 changes: 3 additions & 3 deletions src/nnue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,14 +672,14 @@ typedef uint16x8_t vec128_t;
#if defined(USE_PROPAGATESPARSE)
alignas(64) static const array<array<uint16_t, 8>, 256> lookup_indices = []() {
array<array<uint16_t, 8>, 256> v{};
for (int i = 0; i < 256; ++i)
for (unsigned int i = 0; i < 256; ++i)
{
int j = i;
unsigned int j = i;
int k = 0;
while (j)
{
unsigned int lsbIndex;
GETLSB32(lsbIndex, j);
GETLSB(lsbIndex, j);
j &= j - 1;
v[i][k++] = lsbIndex;
}
Expand Down