3 Commits

6 changed files with 83 additions and 42 deletions

70
src/bitboard.h Normal file
View File

@@ -0,0 +1,70 @@
/* bitboard.h - bitboard definitions.
*
* Copyright (C) 2021 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef BITBOARD_H
#define BITBOARD_H
#include <bits.h>
#include "chessdefs.h"
#include "piece.h"
enum bb_square {
A1 = 1UL << 0, B1 = 1UL << 1, C1 = 1UL << 2, D1 = 1UL << 3,
E1 = 1UL << 4, F1 = 1UL << 5, G1 = 1UL << 6, H1 = 1UL << 7,
A2 = 1UL << 8, B2 = 1UL << 9, C2 = 1UL << 10, D2 = 1UL << 11,
E2 = 1UL << 12, F2 = 1UL << 13, G2 = 1UL << 14, H2 = 1UL << 15,
A3 = 1UL << 16, B3 = 1UL << 17, C3 = 1UL << 18, D3 = 1UL << 19,
E3 = 1UL << 20, F3 = 1UL << 21, G3 = 1UL << 22, H3 = 1UL << 23,
A4 = 1UL << 24, B4 = 1UL << 25, C4 = 1UL << 26, D4 = 1UL << 27,
E4 = 1UL << 28, F4 = 1UL << 29, G4 = 1UL << 30, H4 = 1UL << 31,
A5 = 1UL << 32, B5 = 1UL << 33, C5 = 1UL << 34, D5 = 1UL << 35,
E5 = 1UL << 36, F5 = 1UL << 37, G5 = 1UL << 38, H5 = 1UL << 39,
A6 = 1UL << 40, B6 = 1UL << 41, C6 = 1UL << 42, D6 = 1UL << 43,
E6 = 1UL << 44, F6 = 1UL << 45, G6 = 1UL << 46, H6 = 1UL << 47,
A7 = 1UL << 48, B7 = 1UL << 49, C7 = 1UL << 50, D7 = 1UL << 51,
E7 = 1UL << 52, F7 = 1UL << 53, G7 = 1UL << 54, H7 = 1UL << 55,
A8 = 1UL << 56, B8 = 1UL << 57, C8 = 1UL << 58, D8 = 1UL << 59,
E8 = 1UL << 60, F8 = 1UL << 61, G8 = 1UL << 62, H8 = 1UL << 63,
};
enum bb_files {
F_1 = A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8,
F_2 = B1 | B2 | B3 | B4 | B5 | B6 | B7 | B8,
F_3 = C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8,
F_4 = D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8,
F_5 = E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8,
F_6 = F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8,
F_7 = G1 | G2 | G3 | G4 | G5 | G6 | G7 | G8,
F_8 = H1 | H2 | H3 | H4 | H5 | H6 | H7 | H8,
};
enum bb_ranges {
R_1 = A1 | B1 | C1 | D1 | E1 | F1 | G1 | H1,
R_2 = A2 | B2 | C2 | D2 | E2 | F2 | G2 | H2,
R_3 = A3 | B3 | C3 | D3 | E3 | F3 | G3 | H3,
R_4 = A4 | B4 | C4 | D4 | E4 | F4 | G4 | H4,
R_5 = A5 | B5 | C5 | D5 | E5 | F5 | G5 | H5,
R_6 = A6 | B6 | C6 | D6 | E6 | F6 | G6 | H6,
R_7 = A7 | B7 | C7 | D7 | E7 | F7 | G7 | H7,
R_8 = A8 | B8 | C8 | D8 | E8 | F8 | G8 | H8,
};
#endif /* BOARD_H */

View File

@@ -77,32 +77,4 @@ enum x88_square {
x88_A8=0x70, x88_B8, x88_C8, x88_D8, x88_E8, x88_F8, x88_G8, x88_H8,
};
/* necessary not to become mad to set bitboards
*/
enum bb_square {
A1=(u64)1, B1=(u64)A1<<1, C1=(u64)B1<<1, D1=(u64)C1<<1,
E1=(u64)D1<<1, F1=(u64)E1<<1, G1=(u64)F1<<1, H1=(u64)G1<<1,
A2=(u64)A1<<8, B2=(u64)B1<<8, C2=(u64)C1<<8, D2=(u64)D1<<8,
E2=(u64)E1<<8, F2=(u64)F1<<8, G2=(u64)G1<<8, H2=(u64)H1<<8,
A3=(u64)A2<<8, B3=(u64)B2<<8, C3=(u64)C2<<8, D3=(u64)D2<<8,
E3=(u64)E2<<8, F3=(u64)F2<<8, G3=(u64)G2<<8, H3=(u64)H2<<8,
A4=(u64)A3<<8, B4=(u64)B3<<8, C4=(u64)C3<<8, D4=(u64)D3<<8,
E4=(u64)E3<<8, F4=(u64)F3<<8, G4=(u64)G3<<8, H4=(u64)H3<<8,
A5=(u64)A4<<8, B5=(u64)B4<<8, C5=(u64)C4<<8, D5=(u64)D4<<8,
E5=(u64)E4<<8, F5=(u64)F4<<8, G5=(u64)G4<<8, H5=(u64)H4<<8,
A6=(u64)A5<<8, B6=(u64)B5<<8, C6=(u64)C5<<8, D6=(u64)D5<<8,
E6=(u64)E5<<8, F6=(u64)F5<<8, G6=(u64)G5<<8, H6=(u64)H5<<8,
A7=(u64)A6<<8, B7=(u64)B6<<8, C7=(u64)C6<<8, D7=(u64)D6<<8,
E7=(u64)E6<<8, F7=(u64)F6<<8, G7=(u64)G6<<8, H7=(u64)H6<<8,
A8=(u64)A7<<8, B8=(u64)B7<<8, C8=(u64)C7<<8, D8=(u64)D7<<8,
E8=(u64)E7<<8, F8=(u64)F7<<8, G8=(u64)G7<<8, H8=(u64)H7<<8,
};
#endif /* BOARD_H */

View File

@@ -437,28 +437,26 @@ int do_depth(__unused pos_t *pos, char *arg)
int do_search(pos_t *pos, __unused char *arg)
{
int debug_level = debug_level_get();
long long timer1, timer2;
float nodes_sec;
float timer1, timer2, nodes_sec;
timer1 = debug_timer_elapsed();
negamax(pos, depth, pos->turn == WHITE ? 1 : -1);
timer2 = debug_timer_elapsed();
nodes_sec = (float) pos->node_count / ((float) (timer2 - timer1) / (float)NANOSEC);
debug_level_set(1);
log(1, "best=");
debug_level_set(1);
move_print(0, pos->bestmove, 0);
log(1, " negamax=%d\n", pos->bestmove->negamax);
debug_level_set(debug_level);
printf("Total nodes: %lu time=%lld %.0f nodes/sec\n",
pos->node_count, timer2 - timer1, nodes_sec);
log(1, " negamax=%d\n", pos->bestmove->negamax);
printf("Depth:%d Nodes:%luK time:%.02fs (%.0f kn/s)\n", depth,
pos->node_count / 1000, (timer2 - timer1)/NANOSEC, nodes_sec/1000);
return 1;
}
int do_pvs(pos_t *pos, __unused char *arg)
{
int debug_level = debug_level_get();
long long timer1, timer2;
float nodes_sec;
float timer1, timer2, nodes_sec;
eval_t _pvs;
timer1 = debug_timer_elapsed();
@@ -466,17 +464,17 @@ int do_pvs(pos_t *pos, __unused char *arg)
_pvs = pvs(pos, depth, EVAL_MIN, EVAL_MAX, pos->turn == WHITE ? 1 : -1);
timer2 = debug_timer_elapsed();
nodes_sec = (float) pos->node_count / ((float) (timer2 - timer1) / (float)NANOSEC);
debug_level_set(1);
log(1, "best=");
if (pos->bestmove) {
debug_level_set(1);
move_print(0, pos->bestmove, 0);
debug_level_set(debug_level);
log(1, " pvs=%d stored=%d\n", _pvs, pos->bestmove->negamax);
} else {
log(1, "<no-best-move>");
}
debug_level_set(debug_level);
printf("Total nodes: %lu time=%lld %.0f nodes/sec\n",
pos->node_count, timer2 - timer1, nodes_sec);
printf("Depth:%d Nodes:%luK time:%.02fs (%.0f kn/s)\n", depth,
pos->node_count / 1000, (timer2 - timer1)/NANOSEC, nodes_sec/1000);
return 1;
}

View File

@@ -21,6 +21,7 @@
#include "chessdefs.h"
#include "board.h"
#include "bitboard.h"
#include "piece.h"
#include "move.h"
#include "eval.h"

View File

@@ -21,6 +21,7 @@
#include "chessdefs.h"
#include "piece.h"
#include "board.h"
#include "bitboard.h"
#include "position.h"
static pool_t *pieces_pool;

View File

@@ -45,7 +45,6 @@ eval_t negamax(pos_t *pos, int depth, int color)
}
moves_gen_all(pos);
list_for_each_entry(move, &pos->moves[pos->turn], list) {
log(1, "%.*s", 5 - depth, " ");
newpos = move_do(pos, move);
score = -negamax(newpos, depth - 1, -color);
pos->node_count += newpos->node_count;
@@ -86,7 +85,7 @@ eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color)
if (depth == 0) {
//return quiesce(p, alpha, beta); /* leaf node */
moves_gen_all(pos);
moves_gen_all_nomoves(pos);
score = eval(pos) * color;
log_f(2, "Terminal: depth=%d ", depth);
log_f(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);