summaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/moves.c2
-rw-r--r--src/engine/moves/bishop.c3
-rw-r--r--src/engine/moves/king.c6
-rw-r--r--src/engine/moves/knight.c3
-rw-r--r--src/engine/moves/pawn.c3
-rw-r--r--src/engine/moves/queen.c3
-rw-r--r--src/engine/moves/rook.c3
-rw-r--r--src/engine/moves/vec.c3
-rw-r--r--src/engine/thread.c34
-rw-r--r--src/engine/threads.c91
10 files changed, 141 insertions, 10 deletions
diff --git a/src/engine/moves.c b/src/engine/moves.c
index 56bb47d..05278a5 100644
--- a/src/engine/moves.c
+++ b/src/engine/moves.c
@@ -1,4 +1,4 @@
-#include "moves.h"
+#include "engine/moves.h"
#include "bitboard.h"
#include "moves/vec.c"
diff --git a/src/engine/moves/bishop.c b/src/engine/moves/bishop.c
index b69be57..0bc47f8 100644
--- a/src/engine/moves/bishop.c
+++ b/src/engine/moves/bishop.c
@@ -1,4 +1,5 @@
-#include "moves.h"
+#define MOVES_INTERNAL
+#include "engine/moves.h"
void __forloop_bishop_moves_gen(
moves_t* moves,
diff --git a/src/engine/moves/king.c b/src/engine/moves/king.c
index 1775e7f..706cf9c 100644
--- a/src/engine/moves/king.c
+++ b/src/engine/moves/king.c
@@ -1,8 +1,8 @@
-#include "moves.h"
-#include <stdio.h>
+#define MOVES_INTERNAL
+#include "engine/moves.h"
void get_king_moves(moves_t* moves, position_t position) {
- // assert_valid_position(position);
+ assert_valid_position(position);
bitboard_t friendly_king;
int king_square;
diff --git a/src/engine/moves/knight.c b/src/engine/moves/knight.c
index e7615f0..64e44ed 100644
--- a/src/engine/moves/knight.c
+++ b/src/engine/moves/knight.c
@@ -1,4 +1,5 @@
-#include "moves.h"
+#define MOVES_INTERNAL
+#include "engine/moves.h"
bitboard_t knight_moves[64] = {
132096ULL,
diff --git a/src/engine/moves/pawn.c b/src/engine/moves/pawn.c
index d9757f4..66b1d06 100644
--- a/src/engine/moves/pawn.c
+++ b/src/engine/moves/pawn.c
@@ -1,4 +1,5 @@
-#include "moves.h"
+#define MOVES_INTERNAL
+#include "engine/moves.h"
#define add_promote_moves(other_flags) \
add_move(moves, from, to, .flags = other_flags | MOVE_PROMOTE_Q); \
diff --git a/src/engine/moves/queen.c b/src/engine/moves/queen.c
index bdf96fc..3e0b8ef 100644
--- a/src/engine/moves/queen.c
+++ b/src/engine/moves/queen.c
@@ -1,4 +1,5 @@
-#include "moves.h"
+#define MOVES_INTERNAL
+#include "engine/moves.h"
void get_queen_moves(moves_t* moves, position_t position) {
assert_valid_position(position);
diff --git a/src/engine/moves/rook.c b/src/engine/moves/rook.c
index 3e0ac95..abbc784 100644
--- a/src/engine/moves/rook.c
+++ b/src/engine/moves/rook.c
@@ -1,4 +1,5 @@
-#include "moves.h"
+#define MOVES_INTERNAL
+#include "engine/moves.h"
void __forloop_rook_moves_gen(
moves_t* moves,
diff --git a/src/engine/moves/vec.c b/src/engine/moves/vec.c
index 9ded70d..b5cbb6f 100644
--- a/src/engine/moves/vec.c
+++ b/src/engine/moves/vec.c
@@ -1,4 +1,5 @@
-#include "moves.h"
+#include "engine/moves.h"
+
#include <assert.h>
#include <stdlib.h>
diff --git a/src/engine/thread.c b/src/engine/thread.c
new file mode 100644
index 0000000..017e9b6
--- /dev/null
+++ b/src/engine/thread.c
@@ -0,0 +1,34 @@
+#include "engine/threads.h"
+#include <stdio.h>
+
+void *engine_thread(void *_args) {
+ struct thread_args *args = (struct thread_args*)_args;
+ args->stop = 0;
+
+ int depth = 0;
+ engine_message_t *message = args->message->newest_valid;
+ while (true) {
+ message->depth = depth++;
+ message->multipv = args->id;
+ ipc_moves_init(&message->pv);
+
+ // pseudo engine work
+ for (volatile int k = 0; k < 1000000000; k++) {}
+
+ add_ipc_move(&message->pv, (ipc_move_t) { 14, 24 });
+
+ if (args->stop || !should_continue(message, args->go_args)) {
+ message->best_move = (ipc_move_t) { 12, 24 };
+ message->ponder = (ipc_move_t) { 42, 54 };
+ message->next = NULL;
+ engine_message_mark_ready(message);
+ break;
+ }
+
+ engine_message_mark_ready(message);
+ message = engine_message_handler_push(args->message);
+ }
+
+ args->stop = 2;
+}
+
diff --git a/src/engine/threads.c b/src/engine/threads.c
new file mode 100644
index 0000000..5afe155
--- /dev/null
+++ b/src/engine/threads.c
@@ -0,0 +1,91 @@
+#define ENGINE_THREADS_INTERNAL
+#include "engine/threads.h"
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+
+void threads_init(struct threads *handler, size_t capacity) {
+ // maybe this will be a speed up?
+ // why am i micro optimizing here??????
+ // idk i just watched the eskil steenberg video about UB
+ memset(handler, 0, sizeof(*handler));
+
+ handler->threads = malloc(sizeof(*handler->threads) * capacity);
+ handler->args = malloc(sizeof(*handler->args) * capacity);
+ handler->capacity = capacity;
+ handler->count = 0;
+ handler->running_count = 0;
+ handler->engine_messages = NULL;
+ handler->go_args = NULL;
+ handler->position = NULL;
+}
+
+void threads_deinit(struct threads *handler) {
+ assert(handler->running_count == 0);
+ free(handler->threads);
+ free(handler->args);
+}
+
+void threads_reserve(struct threads *handler, size_t capacity) {
+ if (handler->capacity >= capacity) return;
+
+ handler->threads = realloc(
+ handler->threads,
+ sizeof(*handler->threads) * capacity
+ );
+ handler->args = realloc(
+ handler->args,
+ sizeof(*handler->args) * capacity
+ );
+ handler->capacity = capacity;
+}
+
+void threads_go(struct threads *handler) {
+ int running_count = handler->running_count;
+ for (int i = 0; i < running_count; i++) {
+ __threads_args_init(handler->args + i, handler, i);
+ pthread_create(
+ handler->threads + i,
+ NULL,
+ engine_thread,
+ handler->args + i
+ );
+ }
+}
+
+bool threads_go_loop(struct threads *handler) {
+ // idk maybe do something while waiting for calculations to flow in
+}
+
+bool threads_all_done(struct threads *handler) {
+ int running_count = handler->running_count;
+ assert(running_count > 0);
+ for (int i = 0; i < running_count; i++) {
+ if (atomic_load(&handler->args[i].finished) == 1) continue;
+ return false;
+ }
+ return true;
+}
+
+void threads_cleanup(struct threads *handler) {
+ int running_count = handler->running_count;
+ for (int i = 0; i < running_count; i++) {
+ __threads_args_init(handler->args + i, handler, i);
+ pthread_cancel(handler->threads[i]);
+ }
+ handler->running_count = 0;
+}
+
+void __threads_args_init(
+ struct thread_args* args,
+ struct threads *handler,
+ int i
+) {
+ atomic_init(&args->stop, 0);
+ atomic_init(&args->finished, 0);
+ args->id = i;
+ args->position = handler->position;
+ args->go_args = handler->go_args;
+ args->message = handler->engine_messages->data[i];
+}