Skip to content

Commit

Permalink
Bench: 18586766
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Oct 23, 2023
1 parent cfdf4d6 commit 1af0f0d
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/bitboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const Bitboard RankBB[RANK_NB] = {
rank1BB, rank2BB, rank3BB, rank4BB, rank5BB, rank6BB, rank7BB, rank8BB
};

Bitboard LineBB[64][64];
Bitboard BetweenBB[64][64];

static Bitboard BishopAttacks[5248];
Expand Down Expand Up @@ -132,8 +133,10 @@ CONSTR(2) InitBitboards() {
for (Square sq1 = A1; sq1 <= H8; sq1++)
for (Square sq2 = A1; sq2 <= H8; sq2++)
for (PieceType pt = BISHOP; pt <= ROOK; pt++)
if (AttackBB(pt, sq1, BB(sq2)) & BB(sq2))
if (AttackBB(pt, sq1, BB(sq2)) & BB(sq2)) {
LineBB[sq1][sq2] = (AttackBB(pt, sq1, 0) & AttackBB(pt, sq2, 0)) | BB(sq1) | BB(sq2);
BetweenBB[sq1][sq2] = AttackBB(pt, sq1, BB(sq2)) & AttackBB(pt, sq2, BB(sq1));
}

for (Square sq = A1; sq <= H8; ++sq) {

Expand Down
5 changes: 5 additions & 0 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ enum {
extern const Bitboard FileBB[FILE_NB];
extern const Bitboard RankBB[RANK_NB];

extern Bitboard LineBB[64][64];
extern Bitboard BetweenBB[64][64];

extern Magic BishopTable[64];
Expand Down Expand Up @@ -177,6 +178,10 @@ INLINE bool Single(Bitboard bb) {
return bb && !Multiple(bb);
}

INLINE bool Aligned(Square sq1, Square sq2, Square sq3) {
return LineBB[sq1][sq2] & BB(sq3);
}

INLINE int PieceCount(const Position *pos, Piece piece) {
return PopCount(colorPieceBB(ColorOf(piece), PieceTypeOf(piece)));
}
Expand Down
24 changes: 24 additions & 0 deletions src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ static void InitCastlingRight(Position *pos, Color color, int file) {
RookSquare[cr] = rFrom;
}

Bitboard Blockers(const Position *pos, Bitboard sliders, Square sq) {

Bitboard blockers = 0;

Bitboard snipers = ( (AttackBB( ROOK, sq, 0) & (pieceBB(QUEEN) | pieceBB( ROOK)))
| (AttackBB(BISHOP, sq, 0) & (pieceBB(QUEEN) | pieceBB(BISHOP)))) & sliders;

while (snipers) {
Square sniperSq = PopLsb(&snipers);
Bitboard piecesBetween = BetweenBB[sq][sniperSq] & pieceBB(ALL);

if (Single(piecesBetween))
blockers |= piecesBetween;
}

return blockers;
}

// Parse FEN and set up the position as described
void ParseFen(const char *fen, Position *pos) {

Expand Down Expand Up @@ -236,6 +254,8 @@ void ParseFen(const char *fen, Position *pos) {
pos->gameMoves = atoi(strtok(NULL, " "));

// Final initializations
pos->blockers[WHITE] = Blockers(pos, colorBB(BLACK), kingSq(WHITE));
pos->blockers[BLACK] = Blockers(pos, colorBB(WHITE), kingSq(BLACK));
pos->checkers = Checkers(pos);
pos->key = GenPosKey(pos);
pos->materialKey = GenMaterialKey(pos);
Expand Down Expand Up @@ -523,6 +543,10 @@ bool PositionOk(const Position *pos) {

assert(pos->castlingRights >= 0 && pos->castlingRights <= 15);

assert(pos->blockers[WHITE] == Blockers(pos, colorBB(BLACK), kingSq(WHITE)));
assert(pos->blockers[BLACK] == Blockers(pos, colorBB(WHITE), kingSq(BLACK)));
assert(pos->checkers == Checkers(pos));

assert(GenPosKey(pos) == pos->key);
assert(GenMaterialKey(pos) == pos->materialKey);
assert(GenPawnKey(pos) == pos->pawnKey);
Expand Down
3 changes: 3 additions & 0 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
typedef struct {
Key key;
Key materialKey;
Bitboard blockers[COLOR_NB];
Bitboard checkers;
Move move;
Square epSquare;
Expand All @@ -35,6 +36,7 @@ typedef struct Position {
uint8_t board[64];
Bitboard pieceBB[7];
Bitboard colorBB[COLOR_NB];
Bitboard blockers[COLOR_NB];
Bitboard checkers;

int nonPawnCount[COLOR_NB];
Expand Down Expand Up @@ -77,6 +79,7 @@ extern Bitboard CastlePath[16];
extern Square RookSquare[16];


Bitboard Blockers(const Position *pos, Bitboard sliders, Square sq);
void ParseFen(const char *fen, Position *pos);
Key KeyAfter(const Position *pos, Move move);
bool SEE(const Position *pos, const Move move, const int threshold);
Expand Down
13 changes: 7 additions & 6 deletions src/makemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ void TakeMove(Position *pos) {
// Get various info from history
pos->key = history(0).key;
pos->materialKey = history(0).materialKey;
pos->blockers[WHITE] = history(0).blockers[WHITE];
pos->blockers[BLACK] = history(0).blockers[BLACK];
pos->checkers = history(0).checkers;
pos->epSquare = history(0).epSquare;
pos->rule50 = history(0).rule50;
Expand All @@ -194,13 +196,15 @@ void TakeMove(Position *pos) {
}

// Make a move - take it back and return false if move was illegal
bool MakeMove(Position *pos, const Move move) {
void MakeMove(Position *pos, const Move move) {

TTPrefetch(KeyAfter(pos, move));

// Save position
history(0).key = pos->key;
history(0).materialKey = pos->materialKey;
history(0).blockers[WHITE] = pos->blockers[WHITE];
history(0).blockers[BLACK] = pos->blockers[BLACK];
history(0).checkers = pos->checkers;
history(0).move = move;
history(0).epSquare = pos->epSquare;
Expand Down Expand Up @@ -279,16 +283,13 @@ bool MakeMove(Position *pos, const Move move) {
sideToMove ^= 1;
HASH_SIDE;

// If own king is attacked after the move, take it back immediately
if (KingAttacked(pos, sideToMove^1))
return TakeMove(pos), false;
pos->blockers[WHITE] = Blockers(pos, colorBB(BLACK), kingSq(WHITE));
pos->blockers[BLACK] = Blockers(pos, colorBB(WHITE), kingSq(BLACK));

pos->checkers = Checkers(pos);
pos->nodes++;

assert(PositionOk(pos));

return true;
}

// Pass the turn without moving
Expand Down
2 changes: 1 addition & 1 deletion src/makemove.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "types.h"


bool MakeMove(Position *pos, Move move);
void MakeMove(Position *pos, Move move);
void TakeMove(Position *pos);
void MakeNullMove(Position *pos);
void TakeNullMove(Position *pos);
24 changes: 24 additions & 0 deletions src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ bool MoveIsPseudoLegal(const Position *pos, const Move move) {
return BB(to) & AttackBB(pieceTypeOn(from), from, pieceBB(ALL));
}

// Checks whether a move is legal (assuming it is pseudo-legal in this position)
bool MoveIsLegal(const Position *pos, const Move move) {

const Color color = sideToMove;
const Square from = fromSq(move);
const Square to = toSq(move);

if (moveIsEnPas(move)) {
Bitboard occupied = pieceBB(ALL) ^ BB(from) ^ BB(to) ^ BB(to ^ 8);
Bitboard rooks = colorPieceBB(!color, ROOK) | colorPieceBB(!color, QUEEN);
Bitboard bishops = colorPieceBB(!color, BISHOP) | colorPieceBB(!color, QUEEN);
return !(AttackBB( ROOK, kingSq(color), occupied) & rooks)
&& !(AttackBB(BISHOP, kingSq(color), occupied) & bishops);
}

if (moveIsCastle(move))
return true;

if (PieceTypeOf(piece(move)) == KING)
return !(Attackers(pos, to, pieceBB(ALL) ^ BB(from)) & colorBB(!color));

return !(pos->blockers[color] & BB(from)) || Aligned(from, to, kingSq(color));
}

// Translates a move to a string
char *MoveToStr(const Move move) {

Expand Down
3 changes: 2 additions & 1 deletion src/move.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ INLINE bool CastleLegal(const Position *pos, Square to) {
if (SqAttacked(pos, PopLsb(&kingPath), !color))
return false;

return !chess960 || !(Attackers(pos, to, pieceBB(ALL) ^ BB(RookSquare[castle])) & colorBB(!color));
return !chess960 || !(pos->blockers[color] & BB(RookSquare[castle]));
}

bool MoveIsPseudoLegal(const Position *pos, Move move);
bool MoveIsLegal(const Position *pos, const Move move);
char *MoveToStr(Move move);
Move ParseMove(const char *ptrChar, const Position *pos);
9 changes: 6 additions & 3 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) {
ss->continuation = &thread->continuation[inCheck][moveIsCapture(move)][piece(move)][toSq(move)];

// Recursively search the positions after making the moves, skipping illegal ones
if (!MakeMove(pos, move)) continue;
if (!MoveIsLegal(pos, move)) continue;
MakeMove(pos, move);
int score = -Quiescence(thread, ss+1, -beta, -alpha);
TakeMove(pos);

Expand Down Expand Up @@ -361,7 +362,8 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth

if (mp.stage > NOISY_GOOD) break;

if (!MakeMove(pos, move)) continue;
if (!MoveIsLegal(pos, move)) continue;
MakeMove(pos, move);

ss->continuation = &thread->continuation[inCheck][moveIsCapture(move)][piece(move)][toSq(move)];

Expand Down Expand Up @@ -426,7 +428,8 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
}

// Make the move, skipping to the next if illegal
if (!MakeMove(pos, move)) continue;
if (!MoveIsLegal(pos, move)) continue;
MakeMove(pos, move);

moveCount++;

Expand Down
4 changes: 3 additions & 1 deletion src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ static uint64_t RecursivePerft(Position *pos, const Depth depth) {
GenAllMoves(pos, &list);

for (int i = 0; i < list.count; i++) {
if (!MakeMove(pos, list.moves[i].move)) continue;
Move move = list.moves[i].move;
if (!MoveIsLegal(pos, move)) continue;
MakeMove(pos, move);
leafnodes += RecursivePerft(pos, depth - 1);
TakeMove(pos);
}
Expand Down
5 changes: 2 additions & 3 deletions src/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <stdlib.h>
#include <string.h>

#include "makemove.h"
#include "move.h"
#include "movegen.h"
#include "threads.h"

Expand Down Expand Up @@ -108,9 +108,8 @@ void PrepareSearch(Position *pos, Move searchmoves[]) {
for (int i = 0; i < list.count; ++i) {
Move move = list.moves[list.next++].move;
if (NotInSearchMoves(searchmoves, move)) continue;
if (!MakeMove(pos, move)) continue;
if (!MoveIsLegal(pos, move)) continue;
++rootMoveCount;
TakeMove(pos);
}
pos->nodes = 0;

Expand Down

0 comments on commit 1af0f0d

Please sign in to comment.