aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-14 17:25:09 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-14 17:25:09 +0300
commit1373e30387bf5e8e7858e10c9dee4413bf20d71c (patch)
tree21ec9888a613cfe454ccdefa3de2d37854a112f1
parent2dfa7cecdacacf7ef84e1ca768db6d636e225a0c (diff)
downloadj1chess-1373e30387bf5e8e7858e10c9dee4413bf20d71c.tar.gz
refactor: separate bitboard functionality
-rw-r--r--src/bitboard.c30
-rw-r--r--src/bitboard.h12
-rw-r--r--src/board.c87
-rw-r--r--src/board.h20
-rw-r--r--src/main.c1
5 files changed, 88 insertions, 62 deletions
diff --git a/src/bitboard.c b/src/bitboard.c
new file mode 100644
index 0000000..e9a7d53
--- /dev/null
+++ b/src/bitboard.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include "bitboard.h"
+
+/* Print bitboard on screen in the same way squares appear in memory
+ * (i.e the board is actually flipped along X) */
+void print_bitboard(Bitboard bb) {
+ for (U64 index = 0; index < 64; index++) {
+ printf("%lu", (bb >> index) & 1);
+ if ((index + 1) % 8 == 0) printf("\n");
+ }
+ printf("\n\n");
+}
+
+/* Return bitboard cardinality, aka number of elements in the set */
+inline int pop_count(Bitboard bb){
+ if (bb == 0) return 0;
+ return pop_count(bb >> 1) + (bb & 1);
+}
+
+/* Return Bitboard with only Least Single Bit */
+inline Bitboard ls1b(Bitboard bb) {
+ return bb & -bb;
+}
+
+/* Log base 2 (aka Trailing Zero Count)
+ * Only works for SINGLE Bitboards
+ * Useful for calculating bit-index of LS1B */
+inline Bitboard bitscan(Bitboard bb) {
+ return pop_count(ls1b(bb) - 1);
+}
diff --git a/src/bitboard.h b/src/bitboard.h
new file mode 100644
index 0000000..b90bd8f
--- /dev/null
+++ b/src/bitboard.h
@@ -0,0 +1,12 @@
+#include "types.h"
+
+typedef U64 Bitboard;
+#define BIT (U64)1
+
+int pop_count(Bitboard bb);
+
+void print_bitboard(Bitboard bb);
+
+Bitboard ls1b(Bitboard bb);
+
+Bitboard bitscan(Bitboard bb);
diff --git a/src/board.c b/src/board.c
index 0c133a8..d88c1ee 100644
--- a/src/board.c
+++ b/src/board.c
@@ -2,56 +2,6 @@
#include <string.h>
#include "board.h"
-int pop_count(Bitboard bb){
- if (bb == 0) return 0;
- return pop_count(bb >> 1) + (bb & 1);
-}
-
-void print_bitboard(Bitboard bb) {
- for (U64 index = 0; index < 64; index++) {
- printf("%lu", (bb >> index) & 1);
- if ((index + 1) % 8 == 0) printf("\n");
- }
- printf("\n\n");
-}
-
-
-void precompute_knight_attack_table(Bitboard attacks[64]) {
- for (int index = 0; index < 64; index++) {
- U64 position = BIT << index;
-
- attacks[index] =
- ((position & notAFile & notBFile) << 6) |
- ((position & notGFile & notHFile) << 10) |
- ((position & notAFile) << 15) |
- ((position & notHFile) << 17) |
- ((position & notGFile & notHFile) >> 6) |
- ((position & notAFile & notBFile) >> 10) |
- ((position & notHFile) >> 15) |
- ((position & notAFile) >> 17);
- }
-}
-
-void precompute_king_attack_table(Bitboard attacks[64]) {
- for (int index = 0; index < 64; index++) {
- U64 position = BIT << index;
-
- attacks[index] =
- (position & notAFile) << 7 | (position << 8) | (position & notHFile) << 9 |
- (position & notAFile) >> 1 | (position & notHFile) << 1 |
- (position & notAFile) >> 9 | (position >> 8) | (position & notHFile) >> 7;
- }
-}
-
-void precompute_pawn_attack_table(Bitboard attacks[64], BYTE color) {
- for (int index = 0; index < 64; index++) {
- U64 position = BIT << index;
-
- if (color == WHITE) attacks[index] = (position & notAFile) << 7 | (position & notHFile) << 9;
- else attacks[index] = (position & notAFile) >> 9 | (position & notHFile) >> 7;
- }
-}
-
Board parse_FEN(char* FEN) {
Board board;
for (int i = 0; i < 12; i++) board.pieces[i] = 0;
@@ -138,3 +88,40 @@ void print_board(Board board) {
}
printf(" a b c d e f g h\n");
}
+
+void precompute_knight_attack_table(Bitboard attacks[64]) {
+ for (int index = 0; index < 64; index++) {
+ U64 position = BIT << index;
+
+ attacks[index] =
+ ((position & notAFile & notBFile) << 6) |
+ ((position & notGFile & notHFile) << 10) |
+ ((position & notAFile) << 15) |
+ ((position & notHFile) << 17) |
+ ((position & notGFile & notHFile) >> 6) |
+ ((position & notAFile & notBFile) >> 10) |
+ ((position & notHFile) >> 15) |
+ ((position & notAFile) >> 17);
+ }
+}
+
+void precompute_king_attack_table(Bitboard attacks[64]) {
+ for (int index = 0; index < 64; index++) {
+ U64 position = BIT << index;
+
+ attacks[index] =
+ (position & notAFile) << 7 | (position << 8) | (position & notHFile) << 9 |
+ (position & notAFile) >> 1 | (position & notHFile) << 1 |
+ (position & notAFile) >> 9 | (position >> 8) | (position & notHFile) >> 7;
+ }
+}
+
+void precompute_pawn_attack_table(Bitboard attacks[64], BYTE color) {
+ for (int index = 0; index < 64; index++) {
+ U64 position = BIT << index;
+
+ if (color == WHITE) attacks[index] = (position & notAFile) << 7 | (position & notHFile) << 9;
+ else attacks[index] = (position & notAFile) >> 9 | (position & notHFile) >> 7;
+ }
+}
+
diff --git a/src/board.h b/src/board.h
index cf89794..e3309e2 100644
--- a/src/board.h
+++ b/src/board.h
@@ -1,4 +1,4 @@
-#include "types.h"
+#include "bitboard.h"
#define WHITE 0b0000
#define BLACK 0b0001
@@ -10,9 +10,6 @@
#define QUEEN 0b1000
#define KING 0b1010
-typedef U64 Bitboard;
-#define BIT (U64)1
-
typedef struct {
Bitboard pieces[12];
BYTE side;
@@ -31,11 +28,6 @@ enum enumSquare {
a8, b8, c8, d8, e8, f8, g8, h8
};
-#define notAFile 0xFEFEFEFEFEFEFEFE
-#define notBFile 0xFDFDFDFDFDFDFDFD
-#define notGFile 0xBFBFBFBFBFBFBFBF
-#define notHFile 0x7F7F7F7F7F7F7F7F
-
static const char* notation[64] = {
"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
@@ -56,11 +48,15 @@ static const char* pieces[] = {
"♚", "♔",
};
-void print_bitboard(Bitboard bb);
-int pop_count(Bitboard bb);
-void print_board(Board board);
+#define notAFile 0xFEFEFEFEFEFEFEFE
+#define notBFile 0xFDFDFDFDFDFDFDFD
+#define notGFile 0xBFBFBFBFBFBFBFBF
+#define notHFile 0x7F7F7F7F7F7F7F7F
+
Board parse_FEN(char* FEN);
+void print_board(Board board);
+
void precompute_knight_attack_table(Bitboard table[64]);
void precompute_king_attack_table(Bitboard table[64]);
void precompute_pawn_attack_table(Bitboard table[64], BYTE color);
diff --git a/src/main.c b/src/main.c
index d6aa2b8..2bd9583 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,6 @@
#include "unittest.h"
#include "board.h"
+#include "bitboard.h"
int main() {
start_test_section("Bitboards"); {