Skip to content

Commit dff750f

Browse files
add chess960 support
* Add Chess960 support, castling refactor, and <del>new tests</del> (there's some headaches on Chess960 castling things, don't use it yet) * convert crlf to lf and more * WORK IN PROGRESS enabled test cases * added doctest as submodule * added full support for chess960 BUT PLEASE don't use i/o for chess960 moves * Normalize line endings and apply clang-format * fix format --------- Co-authored-by: GitHub Actions <actions@github.com>
1 parent 29cefb4 commit dff750f

21 files changed

Lines changed: 8347 additions & 2690 deletions

.github/workflows/clang-format.yml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
name: clang-format
22

33
on:
4-
workflow_run:
5-
workflows: ["CMake on multiple platforms"]
6-
types:
7-
- completed
8-
4+
pull_request:
5+
branches: [ "main" ]
6+
97
jobs:
108
format:
11-
if: >
12-
github.event.workflow_run.conclusion == 'success' &&
13-
github.event.workflow_run.event == 'pull_request'
9+
if: github.actor != 'github-actions[bot]'
1410
runs-on: ubuntu-latest
1511
permissions:
1612
contents: write
@@ -20,6 +16,9 @@ jobs:
2016
with:
2117
token: ${{ secrets.GITHUB_TOKEN }}
2218

19+
- name: Install clang-format
20+
run: sudo apt-get update && sudo apt-get install -y clang-format
21+
2322
- name: Run clang-format
2423
run: |
2524
clang-format -i $(git ls-files '*.cpp' '*.hpp' '*.h' '*.c')
@@ -28,8 +27,7 @@ jobs:
2827
run: |
2928
if ! git diff --quiet; then
3029
git config user.email "actions@github.com"
31-
git.config user.name "GitHub Actions"
32-
git add .
33-
git commit -m "Apply clang-format"
30+
git config user.name "GitHub Actions"
31+
git commit -am "Apply clang-format"
3432
git push
35-
fi
33+
fi

.github/workflows/cmake-multi-platform.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: CMake on multiple platforms
22

33
on:
4-
push:
5-
branches: [ "main" ]
64
pull_request:
75
branches: [ "main" ]
86

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ build/
77
!LICENSE
88
!CMakeLists.txt
99
!.gitignore
10+
!.gitattributes
1011
CMakePresets.json
1112
CMakeSettings.json
1213
*.s
13-
.vscode
14+
.vscode
15+
.cache
16+
deps/*
17+
!deps/
18+
*

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "deps/doctest"]
2+
path = deps/doctest
3+
url = https://github.com/doctest/doctest.git

.vscode/settings.json

Lines changed: 0 additions & 71 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ project(chesslib LANGUAGES CXX)
33

44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6-
6+
option(ENABLE_COVERAGE "Enable coverage reporting" OFF)
77
# --- Compiler tuning ---
8-
set(SAN_ENABLED OFF CACHE BOOL "Whether to enable ASan or not")
98
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
109
if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
1110
# clang-cl: forward GCC/Clang style constexpr flags via /clang:
@@ -22,16 +21,21 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
2221
-fconstexpr-steps=2000000000
2322
-fconstexpr-depth=1024
2423
-march=native -mtune=native
25-
-ftemplate-backtrace-limit=0
24+
-ftemplate-backtrace-limit=0 -static
2625
)
2726
endif()
2827
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
29-
add_compile_options(-fconstexpr-ops-limit=2000000000 -fconstexpr-depth=1024 -march=native -mtune=native -ftemplate-backtrace-limit=0)
28+
add_compile_options(-fconstexpr-ops-limit=2000000000 -fconstexpr-depth=1024 -march=native -mtune=native -ftemplate-backtrace-limit=0 -static)
29+
3030
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
3131
set(ARCH_FLAG "/arch:AVX2" CACHE STRING "MSVC architecture flag (/arch:SSE2, /arch:AVX, /arch:AVX2, /arch:AVX512)")
3232
add_compile_options(/constexpr:steps2000000000 /constexpr:depth1024 ${ARCH_FLAG})
3333
endif()
3434

35+
if(ENABLE_COVERAGE)
36+
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
37+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)
38+
endif()
3539
add_compile_definitions(GENERATE_AT_RUNTIME)
3640
if(CMAKE_BUILD_TYPE MATCHES "Debug")
3741
add_compile_definitions(_DEBUG)
@@ -64,4 +68,106 @@ target_include_directories(test_chess PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${doct
6468
target_link_libraries(NonImportantTests PRIVATE chesslib)
6569
target_include_directories(NonImportantTests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${doctest_SOURCE_DIR})
6670
add_test(NAME test_core COMMAND test_chess)
67-
add_test(NAME api_tests COMMAND NonImportantTests)
71+
add_test(NAME api_tests COMMAND NonImportantTests)
72+
if(ENABLE_COVERAGE)
73+
74+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
75+
foreach(t chesslib test_chess NonImportantTests)
76+
target_compile_options(${t} PRIVATE
77+
--coverage
78+
-O0
79+
)
80+
target_link_options(${t} PRIVATE
81+
--coverage
82+
)
83+
endforeach()
84+
85+
find_program(GCOVR gcovr REQUIRED)
86+
87+
add_custom_target(coverage
88+
COMMAND ${CMAKE_COMMAND} -E make_directory coverage
89+
COMMAND ${CMAKE_CTEST_COMMAND}
90+
COMMAND ${GCOVR}
91+
-r ${CMAKE_SOURCE_DIR}
92+
--html
93+
--html-details
94+
-o coverage/index.html
95+
--exclude-directories tests
96+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
97+
COMMENT "Generating GCC coverage report"
98+
)
99+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
100+
foreach(t chesslib test_chess NonImportantTests)
101+
target_compile_options(${t} PRIVATE
102+
-fprofile-instr-generate
103+
-fcoverage-mapping
104+
-O0
105+
)
106+
target_link_options(${t} PRIVATE
107+
-fprofile-instr-generate
108+
)
109+
endforeach()
110+
111+
if(WIN32)
112+
113+
find_program(LLVM_PROFDATA llvm-profdata REQUIRED)
114+
find_program(LLVM_COV llvm-cov REQUIRED)
115+
116+
add_custom_target(coverage
117+
COMMAND ${CMAKE_COMMAND} -E make_directory coverage
118+
119+
COMMAND ${CMAKE_COMMAND} -E env
120+
LLVM_PROFILE_FILE=coverage/coverage.profraw
121+
CTEST_PARALLEL_LEVEL=1
122+
${CMAKE_CTEST_COMMAND}
123+
124+
COMMAND ${LLVM_PROFDATA} merge
125+
-sparse
126+
coverage/coverage.profraw
127+
-o coverage/coverage.profdata
128+
129+
COMMAND ${LLVM_COV} show
130+
$<TARGET_FILE:test_chess>
131+
$<TARGET_FILE:NonImportantTests>
132+
-instr-profile=coverage/coverage.profdata
133+
-format=html
134+
-output-dir=coverage
135+
-ignore-filename-regex="tests|doctest"
136+
-Xdemangler=llvm-cxxfilt
137+
138+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
139+
COMMENT "Generating Clang coverage report (Windows)"
140+
)
141+
else()
142+
143+
find_program(LLVM_PROFDATA llvm-profdata REQUIRED)
144+
find_program(LLVM_COV llvm-cov REQUIRED)
145+
146+
add_custom_target(coverage
147+
COMMAND ${CMAKE_COMMAND} -E make_directory coverage
148+
149+
COMMAND ${CMAKE_COMMAND} -E env
150+
LLVM_PROFILE_FILE=coverage/%m.profraw
151+
${CMAKE_CTEST_COMMAND}
152+
153+
COMMAND ${LLVM_PROFDATA} merge
154+
-sparse
155+
coverage
156+
-o coverage/coverage.profdata
157+
158+
COMMAND ${LLVM_COV} show
159+
$<TARGET_FILE:test_chess>
160+
$<TARGET_FILE:NonImportantTests>
161+
-instr-profile=coverage/coverage.profdata
162+
-format=html
163+
-output-dir=coverage
164+
-ignore-filename-regex="tests|doctest"
165+
-Xdemangler=c++filt
166+
167+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
168+
COMMENT "Generating Clang coverage report"
169+
)
170+
endif()
171+
endif()
172+
173+
endif()

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
# chesslib
2-
(formerly MultiTypeChess, but it was an absolutely bad name)
3-
4-
This is now.... chesslib (it's super duplicated as I searched) a yet good chess library, as the name.
2+
A chess library that supports Chess960 and decently fast enough.

attacks.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,8 @@ inline static Bitboard att(PieceType pt, Square sq, Bitboard occ) {
173173
return (pt == BISHOP) ? chess::_chess::_HyperbolaBishopAttacks(sq, occ) : chess::_chess::_HyperbolaRookAttacks(sq, occ);
174174
}
175175

176-
inline static std::array<std::array<Bitboard, 64>, 64> generate_between() {
177-
assert(att(BISHOP, SQ_A1, 0) == 9241421688590303744ULL &&
178-
"Well... this is a debug message, but this is a landmine that the magic bitboards are NOT initialized before this, "
179-
"wontwork\n");
180-
std::array<std::array<Bitboard, 64>, 64> squares_between_bb{};
176+
inline static std::array<std::array<Bitboard, SQ_NONE + 1>, SQ_NONE + 1> generate_between() {
177+
std::array<std::array<Bitboard, SQ_NONE + 1>, SQ_NONE + 1> squares_between_bb{};
181178

182179
for (int sq1 = 0; sq1 < 64; ++sq1) {
183180
for (PieceType pt : { BISHOP, ROOK }) {
@@ -192,5 +189,5 @@ inline static std::array<std::array<Bitboard, 64>, 64> generate_between() {
192189

193190
return squares_between_bb;
194191
}
195-
std::array<std::array<Bitboard, 64>, 64> SQUARES_BETWEEN_BB = generate_between();
192+
std::array<std::array<Bitboard, SQ_NONE + 1>, SQ_NONE + 1> SQUARES_BETWEEN_BB = generate_between();
196193
} // namespace chess::movegen

0 commit comments

Comments
 (0)