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

Add clang-format and workflow for consistency #21 #22

Merged
merged 11 commits into from
Oct 25, 2024
Merged
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
44 changes: 44 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignEscapedNewlines: DontAlign
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AlwaysBreakTemplateDeclarations: Yes
BasedOnStyle: WebKit
BitFieldColonSpacing: After
BinPackParameters: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: false
nikhiljangra264 marked this conversation as resolved.
Show resolved Hide resolved
AfterClass: false
AfterControlStatement: false
BeforeElse: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: AfterColon
BreakStringLiterals: false
ColumnLimit: 100
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
IndentGotoLabels: false
IndentPPDirectives: BeforeHash
IndentWidth: 4
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
PackConstructorInitializers: Never
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: false
SpaceBeforeCaseColon: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeInheritanceColon: false
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 1
30 changes: 30 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Clang Format

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
clang-format:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Clang
run: |
sudo apt-get install -y clang-format

- name: Run clang-format Check on MediaProcessor
run: |
find src -regex '.*\.\(cpp\|h\)' -exec clang-format --dry-run --Werror {} +

- name: Display Message if Formatting Fails
if: failure()
run: echo "Code formatting issues found. Please run clang-format to fix them."

10 changes: 10 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ all: $(EXE)
clean:
$(RM) $(EXE) *.o bench/*.o chess/*.o utils/*.o

.PHONY: format # Mark `format` as a phony target so it runs each time
# Set the format command based on the OS
ifeq ($(OS),Windows_NT)
format:
powershell -Command "Get-ChildItem -Recurse -Include *.cpp, *.h | ForEach-Object { clang-format -i $$_.FullName }"
else
format:
find . -type f \( -name "*.cpp" -o -name "*.h" \) -exec clang-format -i {} +
endif

###
### Compiler:
###
Expand Down
33 changes: 17 additions & 16 deletions src/bench/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ std::vector<std::string> bench_positions = {
"4rrk1/1p1nq3/p7/2p1P1pp/3P2bp/3Q1Bn1/PPPB4/1K2R1NR w - - 40 21",
"r3k2r/3nnpbp/q2pp1p1/p7/Pp1PPPP1/4BNN1/1P5P/R2Q1RK1 w kq - 0 16",
"3Qb1k1/1r2ppb1/pN1n2q1/Pp1Pp1Pr/4P2p/4BP2/4B1R1/1R5K b - - 11 40",
"4k3/3q1r2/1N2r1b1/3ppN2/2nPP3/1B1R2n1/2R1Q3/3K4 w - - 5 1",
"4k3/3q1r2/1N2r1b1/3ppN2/2nPP3/1B1R2n1/2R1Q3/3K4 w - - 5 1",
// 5-man positions
"8/8/8/8/5kp1/P7/8/1K1N4 w - - 0 1", // Kc2 - mate
"8/8/8/5N2/8/p7/8/2NK3k w - - 0 1", // Na2 - mate
"8/3k4/8/8/8/4B3/4KB2/2B5 w - - 0 1", // draw
"8/8/8/8/5kp1/P7/8/1K1N4 w - - 0 1", // Kc2 - mate
"8/8/8/5N2/8/p7/8/2NK3k w - - 0 1", // Na2 - mate
"8/3k4/8/8/8/4B3/4KB2/2B5 w - - 0 1", // draw
// 6-man positions
"8/8/1P6/5pr1/8/4R3/7k/2K5 w - - 0 1", // Re5 - mate
"8/2p4P/8/kr6/6R1/8/8/1K6 w - - 0 1", // Ka2 - mate
"8/8/3P3k/8/1p6/8/1P6/1K3n2 b - - 0 1", // Nd2 - draw
"8/8/1P6/5pr1/8/4R3/7k/2K5 w - - 0 1", // Re5 - mate
"8/2p4P/8/kr6/6R1/8/8/1K6 w - - 0 1", // Ka2 - mate
"8/8/3P3k/8/1p6/8/1P6/1K3n2 b - - 0 1", // Nd2 - draw
// 7-man positions
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
// Mate and stalemate positions
"6k1/3b3r/1p1p4/p1n2p2/1PPNpP1q/P3Q1p1/1R1RB1P1/5K2 b - - 0 1",
"r2r1n2/pp2bk2/2p1p2p/3q4/3PN1QP/2P3R1/P4PP1/5RK1 w - - 0 1",
Expand All @@ -72,18 +72,19 @@ std::vector<std::string> bench_positions = {
namespace sonic {

void run_bench() {
const std::vector<std::string> go_params = {"go", "depth", "6"};
std::uint64_t node_count = 0;
TimePoint start = current_time();
Position pos;
SearchInfo search_info;
for(size_t i = 0; i < bench_positions.size(); i++) {
std::string fen = "position fen " + bench_positions[i];
const std::vector<std::string> go_params = {"go", "depth", "6"};
std::uint64_t node_count = 0;
TimePoint start = current_time();
Position pos;
SearchInfo search_info;
for (size_t i = 0; i < bench_positions.size(); i++) {
std::string fen = "position fen " + bench_positions[i];
std::vector<std::string> params = split_string(fen, ' ');
parse_position(pos, search_info, params);
parse_go(pos, search_info, go_params);
TT.clear();
std::cout << "Position [" << i + 1 << "/" << bench_positions.size() << "]" << " (" << pos.fen() << ")" << std::endl;
std::cout << "Position [" << i + 1 << "/" << bench_positions.size() << "]"
<< " (" << pos.fen() << ")" << std::endl;
search(pos, search_info);
node_count += search_info.nodes;
std::cout << "\n";
Expand Down
18 changes: 10 additions & 8 deletions src/bench/perft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace sonic {

// Count number of leaf nodes.
std::uint64_t perft(Position& pos, int depth) {
if(depth == 0) {
if (depth == 0) {
return 1;
}
std::uint64_t node_count = 0;
MoveList movelist;
MoveList movelist;
generate_moves<GenType::ALL>(pos, movelist);
for(const Move& m : movelist) {
for (const Move& m : movelist) {
UndoInfo info;
if(!pos.make_move(m, info)) {
if (!pos.make_move(m, info)) {
pos.unmake_move(info);
continue;
}
Expand All @@ -33,6 +33,7 @@ std::uint64_t perft(Position& pos, int depth) {
}

void bench_perft() {
// clang-format off
const std::vector<PerftTest> perft_tests = {
{"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", 119060324ULL, 6},
{"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1", 193690690ULL, 5},
Expand All @@ -42,14 +43,15 @@ void bench_perft() {
{"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8", 89941194ULL, 5},
{"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10", 164075551ULL, 5},
};
// clang-format on

for(std::size_t i = 0; i < perft_tests.size(); i++) {
for (std::size_t i = 0; i < perft_tests.size(); i++) {
const auto& test = perft_tests[i];
Position pos(test.fen);
Position pos(test.fen);

TimePoint start = current_time();
TimePoint start = current_time();
std::uint64_t node_count = perft(pos, test.depth);
std::uint64_t ms = time_elapsed(start);
std::uint64_t ms = time_elapsed(start);

std::cout << "Position [" << i + 1 << "/" << perft_tests.size() << "]:";
std::cout << std::left;
Expand Down
6 changes: 3 additions & 3 deletions src/bench/perft.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace sonic {

struct PerftTest {
std::string fen;
std::string fen;
std::uint64_t expected_node_count;
int depth;
int depth;
};

std::uint64_t perft(Position& pos, int depth);
void bench_perft();
void bench_perft();

} // namespace sonic
50 changes: 25 additions & 25 deletions src/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace sonic {

void Book::open(const std::string& file) {
book = fopen(file.c_str(), "rb+");
if(book == NULL) {
if (book == NULL) {
return;
}
fseek(book, 0, SEEK_END);
Expand All @@ -25,42 +25,42 @@ void Book::close() {

// Select a random book move.
Move Book::book_move(const Position& pos) const {
if(!is_open()) {
if (!is_open()) {
return MOVE_NONE;
}
// Convert polyglot move format to ours.
auto convert_move = [&](int move) -> Move {
int to_file = move & 7;
int to_rank = (move >> 3) & 7;
int from_file = (move >> 6) & 7;
int from_rank = (move >> 9) & 7;
int promotion_piece = (move >> 12) & 7;
Square from = Square(File(from_file), Rank(from_rank));
Square to = Square(File(to_file), Rank(to_rank));
Move::Promotion promotion = Move::Promotion::None;
if(promotion_piece != 0) {
int to_file = move & 7;
int to_rank = (move >> 3) & 7;
int from_file = (move >> 6) & 7;
int from_rank = (move >> 9) & 7;
int promotion_piece = (move >> 12) & 7;
Square from = Square(File(from_file), Rank(from_rank));
Square to = Square(File(to_file), Rank(to_rank));
Move::Promotion promotion = Move::Promotion::None;
if (promotion_piece != 0) {
// Our piece order is reversed.
promotion = Move::Promotion(5 - promotion_piece);
}
return Move(from, to, promotion);
};
MoveList movelist;
generate_moves<GenType::ALL>(pos, movelist);
Move best_move = MOVE_NONE;
int best_score = 0;
for(int i = find_key(pos.hashkey()); i < book_size; i++) {
Move best_move = MOVE_NONE;
int best_score = 0;
for (int i = find_key(pos.hashkey()); i < book_size; i++) {
Book::Entry entry = read_entry(i);
if(entry.key != pos.hashkey()) {
if (entry.key != pos.hashkey()) {
break;
}
Move move = convert_move(entry.move);
// Check if the given move is in movelist. This can filter out chess960 moves.
if(!movelist.contains(move)) {
if (!movelist.contains(move)) {
continue;
}
int score = entry.count;
best_score += score;
if(int(rng() % best_score) < score) {
if (int(rng() % best_score) < score) {
best_move = move;
}
}
Expand All @@ -70,10 +70,10 @@ Move Book::book_move(const Position& pos) const {
// Binary search the file to find the first occurance of the key.
int Book::find_key(std::uint64_t key) const {
int l = 0, r = book_size - 1;
while(l < r) {
int mid = (l + r) / 2;
while (l < r) {
int mid = (l + r) / 2;
Book::Entry entry = read_entry(mid);
if(key <= entry.key) {
if (key <= entry.key) {
r = mid;
} else {
l = mid + 1;
Expand All @@ -86,7 +86,7 @@ int Book::find_key(std::uint64_t key) const {
Book::Entry Book::read_entry(int pos) const {
auto read_int = [&](int bytes) -> std::uint64_t {
std::uint64_t result = 0;
for(int i = 0; i < bytes; i++) {
for (int i = 0; i < bytes; i++) {
int byte = fgetc(book);
assert(byte != EOF);
result = (result << 8) | byte;
Expand All @@ -95,11 +95,11 @@ Book::Entry Book::read_entry(int pos) const {
};
assert(fseek(book, pos * 16, SEEK_SET) == 0);
Book::Entry entry;
entry.key = read_int(8);
entry.move = read_int(2);
entry.key = read_int(8);
entry.move = read_int(2);
entry.count = read_int(2);
entry.n = read_int(2);
entry.sum = read_int(2);
entry.n = read_int(2);
entry.sum = read_int(2);
return entry;
}

Expand Down
14 changes: 6 additions & 8 deletions src/book.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
namespace sonic {

class Book {
public:
public:
Book() {}

Book(const std::string& file) {
open(file);
}
Book(const std::string& file) { open(file); }

struct Entry {
std::uint64_t key;
Expand All @@ -31,12 +29,12 @@ class Book {
// Select a random book move.
Move book_move(const Position& pos) const;

private:
int find_key(std::uint64_t key) const;
private:
int find_key(std::uint64_t key) const;
Entry read_entry(int pos) const;

FILE* book = NULL;
int book_size = 0;
FILE* book = NULL;
int book_size = 0;
};

} // namespace sonic
Loading
Loading