almost working now
This commit is contained in:
23
Makefile
23
Makefile
@@ -1,16 +1,27 @@
|
|||||||
|
|
||||||
CFLAGS := -std=c23 -g
|
BUILD ?= debug
|
||||||
|
CC := clang
|
||||||
|
|
||||||
|
CFLAGS.gcc.release := -O3
|
||||||
|
CFLAGS.gcc.debug := -ggdb -O0 -fsanitize=address
|
||||||
|
CFLAGS.gcc := -std=c23 -Wall -Wextra -Wconversion -Wno-unused-function
|
||||||
|
|
||||||
|
CFLAGS.clang.release := -O3
|
||||||
|
CFLAGS.clang.debug := -ggdb -O0
|
||||||
|
CFLAGS.clang := -std=c23 -Wall -Wextra -Wconversion -Wno-unused-function -Wimplicit-int-conversion
|
||||||
|
|
||||||
|
CFLAGS := $(CFLAGS.$(CC)) $(CFLAGS.$(CC).$(BUILD))
|
||||||
|
|
||||||
all: tests
|
all: tests
|
||||||
|
|
||||||
codegen: codegen.c
|
codegen: codegen.c
|
||||||
$(CC) -o $@ $(CFLAGS) -DCODEGEN $^
|
$(CC) -o $@ $(CFLAGS) $^
|
||||||
|
|
||||||
mbb_rook.h: codegen
|
mbb_rook.h: codegen
|
||||||
#./codegen
|
./codegen
|
||||||
|
|
||||||
mbb_bishop.h: codegen
|
mbb_bishop.h: codegen
|
||||||
#./codegen
|
./codegen
|
||||||
|
|
||||||
tests: tests.c | mbb_rook.h mbb_bishop.h base.h
|
tests: tests.c mbb_rook.h mbb_bishop.h base.h
|
||||||
$(CC) -o $@ $(CFLAGS) $^
|
$(CC) -o $@ $(CFLAGS) tests.c
|
||||||
|
|||||||
147
codegen.c
147
codegen.c
@@ -1,104 +1,8 @@
|
|||||||
|
#define CODEGEN
|
||||||
#include "base.h"
|
#include "base.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
static bitboard bishop_attacks_from_index_slow(enum square_index sq, bitboard occ)
|
|
||||||
{
|
|
||||||
const enum rank_index rank = index_to_rank(sq);
|
|
||||||
const enum file_index file = index_to_file(sq);
|
|
||||||
|
|
||||||
bitboard atk = 0ULL;
|
|
||||||
|
|
||||||
/* following loops assume rank and file types are unsigned, which relies on C23 enums */
|
|
||||||
_Static_assert(((enum rank_index)0) - ((enum rank_index)1) > ((enum rank_index)0));
|
|
||||||
_Static_assert(((enum file_index)0) - ((enum file_index)1) > ((enum file_index)0));
|
|
||||||
|
|
||||||
enum rank_index walk_rank;
|
|
||||||
enum file_index walk_file;
|
|
||||||
for (walk_rank = rank+1, walk_file = file+1;
|
|
||||||
walk_rank <= RANK_INDEX_8 && walk_file <= FILE_INDEX_H;
|
|
||||||
++walk_rank, ++walk_file) {
|
|
||||||
const bitboard sq = SQ_MASK_FROM_RF(walk_rank, walk_file);
|
|
||||||
atk |= sq;
|
|
||||||
if (occ & sq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (walk_rank = rank+1, walk_file = file-1;
|
|
||||||
walk_rank <= RANK_INDEX_8 && walk_file <= FILE_INDEX_H;
|
|
||||||
++walk_rank, --walk_file) {
|
|
||||||
const bitboard sq = SQ_MASK_FROM_RF(walk_rank, walk_file);
|
|
||||||
atk |= sq;
|
|
||||||
if (occ & sq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (walk_rank = rank-1, walk_file = file+1;
|
|
||||||
walk_rank <= RANK_INDEX_8 && walk_file <= FILE_INDEX_H;
|
|
||||||
--walk_rank, ++walk_file) {
|
|
||||||
const bitboard sq = SQ_MASK_FROM_RF(walk_rank, walk_file);
|
|
||||||
atk |= sq;
|
|
||||||
if (occ & sq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (walk_rank = rank-1, walk_file = file-1;
|
|
||||||
walk_rank <= RANK_INDEX_8 && walk_file <= FILE_INDEX_H;
|
|
||||||
--walk_rank, --walk_file) {
|
|
||||||
const bitboard sq = SQ_MASK_FROM_RF(walk_rank, walk_file);
|
|
||||||
atk |= sq;
|
|
||||||
if (occ & sq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return atk;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bitboard rook_attacks_from_index_slow(enum square_index sq, bitboard occ)
|
|
||||||
{
|
|
||||||
const enum rank_index rank = index_to_rank(sq);
|
|
||||||
const enum file_index file = index_to_file(sq);
|
|
||||||
|
|
||||||
bitboard atk = 0ULL;
|
|
||||||
|
|
||||||
/* following loops assume rank and file types are unsigned, which relies on C23 enums */
|
|
||||||
_Static_assert(((enum rank_index)0) - ((enum rank_index)1) > ((enum rank_index)0));
|
|
||||||
_Static_assert(((enum file_index)0) - ((enum file_index)1) > ((enum file_index)0));
|
|
||||||
|
|
||||||
for (enum rank_index walk_rank = rank+1; walk_rank <= RANK_INDEX_8; ++walk_rank) {
|
|
||||||
const bitboard sq = SQ_MASK_FROM_RF(walk_rank, file);
|
|
||||||
atk |= sq;
|
|
||||||
if (occ & sq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (enum rank_index walk_rank = rank-1; walk_rank <= RANK_INDEX_8; --walk_rank) {
|
|
||||||
const bitboard sq = SQ_MASK_FROM_RF(walk_rank, file);
|
|
||||||
atk |= sq;
|
|
||||||
if (occ & sq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (enum file_index walk_file = file+1; walk_file <= FILE_INDEX_H; ++walk_file) {
|
|
||||||
const bitboard sq = SQ_MASK_FROM_RF(rank, walk_file);
|
|
||||||
atk |= sq;
|
|
||||||
if (occ & sq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (enum file_index walk_file = file-1; walk_file <= FILE_INDEX_H; --walk_file) {
|
|
||||||
const bitboard sq = SQ_MASK_FROM_RF(rank, walk_file);
|
|
||||||
atk |= sq;
|
|
||||||
if (occ & sq) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return atk;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* gets the next number with the same bitcount, unless it overflows, in which
|
/* gets the next number with the same bitcount, unless it overflows, in which
|
||||||
* it returns the first number with popcount+1 */
|
* it returns the first number with popcount+1 */
|
||||||
uint64_t next_magic(uint64_t seed) {
|
uint64_t next_magic(uint64_t seed) {
|
||||||
@@ -116,21 +20,6 @@ uint64_t next_magic(uint64_t seed) {
|
|||||||
return seed;
|
return seed;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t rand64() {
|
|
||||||
union {
|
|
||||||
uint64_t v;
|
|
||||||
uint16_t vs[4];
|
|
||||||
} x = {
|
|
||||||
.vs = {
|
|
||||||
(uint16_t)rand(),
|
|
||||||
(uint16_t)rand(),
|
|
||||||
(uint16_t)rand(),
|
|
||||||
(uint16_t)rand(),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return x.v;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -146,6 +35,8 @@ int main()
|
|||||||
index bishop_relevant_bits[9] = {0};
|
index bishop_relevant_bits[9] = {0};
|
||||||
size_t bishop_relevant_bits_count = 0;
|
size_t bishop_relevant_bits_count = 0;
|
||||||
|
|
||||||
|
bitboard between_lookup[SQ_INDEX_COUNT][SQ_INDEX_COUNT];
|
||||||
|
|
||||||
for (enum square_index sq_index = SQ_INDEX_BEGIN; sq_index < SQ_INDEX_COUNT; ++sq_index) {
|
for (enum square_index sq_index = SQ_INDEX_BEGIN; sq_index < SQ_INDEX_COUNT; ++sq_index) {
|
||||||
enum file_index file = index_to_file(sq_index);
|
enum file_index file = index_to_file(sq_index);
|
||||||
enum rank_index rank = index_to_rank(sq_index);
|
enum rank_index rank = index_to_rank(sq_index);
|
||||||
@@ -183,7 +74,7 @@ int main()
|
|||||||
occ |= ((test >> i) & 1ULL) << rook_relevant_bits[i];
|
occ |= ((test >> i) & 1ULL) << rook_relevant_bits[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
atk = rook_attacks_from_index_slow(sq_index, occ);
|
atk = rook_attacks_from_index(sq_index, occ);
|
||||||
|
|
||||||
const size_t hash = ((occ * magic) >> (64ULL-12));
|
const size_t hash = ((occ * magic) >> (64ULL-12));
|
||||||
|
|
||||||
@@ -239,7 +130,7 @@ int main()
|
|||||||
occ |= ((test >> i) & 1ULL) << bishop_relevant_bits[i];
|
occ |= ((test >> i) & 1ULL) << bishop_relevant_bits[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
atk = bishop_attacks_from_index_slow(sq_index, occ);
|
atk = bishop_attacks_from_index(sq_index, occ);
|
||||||
|
|
||||||
const size_t hash = ((occ * magic) >> (64ULL-9));
|
const size_t hash = ((occ * magic) >> (64ULL-9));
|
||||||
|
|
||||||
@@ -258,6 +149,16 @@ int main()
|
|||||||
next_bishop_magic:
|
next_bishop_magic:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* BETWEEN TABLE
|
||||||
|
* ===================== */
|
||||||
|
{
|
||||||
|
for (enum square_index i = SQ_INDEX_BEGIN; i < SQ_INDEX_COUNT; ++i) {
|
||||||
|
for (enum square_index j = SQ_INDEX_BEGIN; j < SQ_INDEX_COUNT; ++j) {
|
||||||
|
between_lookup[i][j] = between_mask(i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --- write to files --- */
|
/* --- write to files --- */
|
||||||
@@ -336,6 +237,24 @@ int main()
|
|||||||
fprintf(f,"};\n");
|
fprintf(f,"};\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{ /* between table */
|
||||||
|
FILE* f = fopen("between_lookup.h", "w");
|
||||||
|
if (!f) {
|
||||||
|
perror("fopen");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(f, "static const bitboard between_lookup[SQ_INDEX_COUNT][SQ_INDEX_COUNT] = {\n");
|
||||||
|
for (enum square_index i = SQ_INDEX_BEGIN; i < SQ_INDEX_COUNT; ++i) {
|
||||||
|
fprintf(f, "[%s] = {\n", square_index_str[i]);
|
||||||
|
for (enum square_index j = SQ_INDEX_BEGIN; j < SQ_INDEX_COUNT; ++j) {
|
||||||
|
fprintf(f, "0x%016"BITBOARD_FMT_X"ULL, \n", between_lookup[i][j]);
|
||||||
|
}
|
||||||
|
fprintf(f, "\n},\n");
|
||||||
|
}
|
||||||
|
fprintf(f,"};\n");
|
||||||
|
}
|
||||||
|
|
||||||
#undef TAB
|
#undef TAB
|
||||||
#undef NL
|
#undef NL
|
||||||
}
|
}
|
||||||
|
|||||||
74
tests.c
74
tests.c
@@ -357,9 +357,11 @@ static void test_bishops(void)
|
|||||||
printf("\nAll bishop_attacks_from_index tests passed.\n");
|
printf("\nAll bishop_attacks_from_index tests passed.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
printf("sizeof board: %zu\n", sizeof (struct board));
|
||||||
|
|
||||||
test_rooks();
|
test_rooks();
|
||||||
test_bishops();
|
test_bishops();
|
||||||
|
|
||||||
@@ -369,37 +371,61 @@ int main()
|
|||||||
fprintf(stdout, "\033[0m\n"); /* reset background color */
|
fprintf(stdout, "\033[0m\n"); /* reset background color */
|
||||||
struct board board = BOARD_INITIAL;
|
struct board board = BOARD_INITIAL;
|
||||||
|
|
||||||
enum player player = PLAYER_WHITE;
|
//board_load_fen_unsafe(&board, "1n1q1rk1/r1p2P2/1p1pp2p/pB2P3/2P5/PPN5/6b1/3QK1NR b - - 0 1");
|
||||||
enum square_index pieces[SQ_INDEX_COUNT] = {0};
|
//board_print_fen(&board, stdout);
|
||||||
struct move moves[MOVE_MAX] = {0};
|
board_print(&board.pos, NULL, stdout);
|
||||||
for (int i = 0; i < 40; ++i) {
|
|
||||||
board_print_threats(&board, stdout, player);
|
|
||||||
|
|
||||||
const size_t piece_count = all_player_pieces(&board, player, pieces);
|
struct move moves[MOVE_MAX];
|
||||||
|
|
||||||
if (piece_count == 0) {
|
for (int turn = 0; turn < 200; ++turn) {
|
||||||
printf("no pieces for %s, aborting\n", player_str[player]);
|
size_t move_count = 0;
|
||||||
break;
|
all_moves(&board.pos, board.pos.player, &move_count, moves);
|
||||||
}
|
|
||||||
|
|
||||||
size_t move_count;
|
|
||||||
for (size_t i = 0; i < piece_count; ++i) {
|
|
||||||
move_count = all_moves_from(&board, player, pieces[i], moves);
|
|
||||||
|
|
||||||
if (move_count > 0) {
|
|
||||||
board_move_piece(&board, player, moves[0]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (move_count == 0) {
|
if (move_count == 0) {
|
||||||
printf("no moves for %s, aborting\n", player_str[player]);
|
printf("no moves for %s, aborting\n", player_str[board.pos.player]);
|
||||||
|
board_print_threats(&board.pos, stdout, NULL);
|
||||||
|
board.pos.player = opposite_player(board.pos.player);
|
||||||
|
board_print_threats(&board.pos, stdout, NULL);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
player = opposite_player(player);
|
//struct move move = moves[0];
|
||||||
|
struct move move = search(&board, board.pos.player, 6);
|
||||||
|
|
||||||
usleep(300000);
|
printf("move %d: {\n"
|
||||||
|
" .from = %s, (%s)\n"
|
||||||
|
" .to = %s,\n"
|
||||||
|
" .mask = ",
|
||||||
|
turn,
|
||||||
|
square_index_display[move.from],
|
||||||
|
piece_str[board.mailbox[move.from]],
|
||||||
|
square_index_display[move.to]
|
||||||
|
);
|
||||||
|
if (move.attr & MATTR_CAPTURE) printf("MATTR_CAPTURE ");
|
||||||
|
if (move.attr & MATTR_PROMOTE) printf("MATTR_PROMOTE ");
|
||||||
|
printf("\n}\n");
|
||||||
|
|
||||||
|
enum move_result const r = board_move_2(&board, move);
|
||||||
|
|
||||||
|
#if 1
|
||||||
|
board_print(&board.pos, &move, stdout);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (r == MR_STALEMATE) {
|
||||||
|
printf("stalemate\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (board.pos.pieces[PLAYER_WHITE][PIECE_KING] == 0ULL) {
|
||||||
|
printf("white king gone!!\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (board.pos.pieces[PLAYER_BLACK][PIECE_KING] == 0ULL) {
|
||||||
|
printf("black king gone!!\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//usleep(1000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
Reference in New Issue
Block a user