Skip to content

Commit

Permalink
Bench: 24311257
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Dec 31, 2024
1 parent 179c96b commit edcd959
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
8 changes: 1 addition & 7 deletions src/makemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ 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));

Expand Down Expand Up @@ -279,16 +279,10 @@ 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->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);
31 changes: 31 additions & 0 deletions src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,37 @@ bool MoveIsPseudoLegal(const Position *pos, const Move move) {
return BB(to) & AttackBB(pieceTypeOn(from), from, pieceBB(ALL));
}

// Checks whether a move is legal
// (assumes the move is pseudo-legal in the current position)
bool MoveIsLegal(const Position *pos, const Move move) {

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

Square kingSq = PieceTypeOf(pieceOn(from)) == KING ? to : (Square)Lsb(colorPieceBB(color, KING));

Bitboard occ = (pieceBB(ALL) ^ BB(from)) | BB(to);
Bitboard exclude = BB(to);

if (moveIsEnPas(move))
occ ^= BB(to ^ 8),
exclude ^= BB(to ^ 8);

Bitboard bishops = colorBB(!color) & (pieceBB(BISHOP) | pieceBB(QUEEN));
Bitboard rooks = colorBB(!color) & (pieceBB(ROOK) | pieceBB(QUEEN));

Bitboard kingAttackers =
( (PawnAttackBB(color, kingSq) & colorPieceBB(!color, PAWN))
| (AttackBB(KING, kingSq, occ) & colorPieceBB(!color, KING))
| (AttackBB(KNIGHT, kingSq, occ) & colorPieceBB(!color, KNIGHT))
| (AttackBB(BISHOP, kingSq, occ) & bishops)
| (AttackBB(ROOK, kingSq, occ) & rooks));

// If any non-excluded piece is attacking the king, the move is illegal
return !(kingAttackers & ~exclude);
}

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

Expand Down
1 change: 1 addition & 0 deletions src/move.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ INLINE bool CastleLegal(const Position *pos, Square to) {
}

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);
bool NotInSearchMoves(Move searchmoves[], Move move);
3 changes: 1 addition & 2 deletions src/movegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ void GenLegalMoves(Position *pos, MoveList *list) {

for (int i = 0; i < allMoves.count; ++i) {
Move move = allMoves.moves[i].move;
if (!MakeMove(pos, move)) continue;
if (!MoveIsLegal(pos, move)) continue;
list->moves[list->count++].move = move;
TakeMove(pos);
}
pos->nodes = 0;
}
Expand Down
9 changes: 6 additions & 3 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, int beta) {
ss->contCorr = &thread->contCorrHistory[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 @@ -405,7 +406,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->move = move;
ss->continuation = &thread->continuation[inCheck][moveIsCapture(move)][piece(move)][toSq(move)];
Expand Down Expand Up @@ -474,7 +476,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
3 changes: 2 additions & 1 deletion src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ 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;
if (!MoveIsLegal(pos, list.moves[i].move)) continue;
MakeMove(pos, list.moves[i].move);
leafnodes += RecursivePerft(pos, depth - 1);
TakeMove(pos);
}
Expand Down

0 comments on commit edcd959

Please sign in to comment.