mirror of
https://github.com/sockspls/badfish
synced 2025-07-11 11:39:15 +00:00
Enable prefetch also for gcc
This fix a compile error under Linux with gcc when there aren't the intel dev libraries. Also simplify the previous patch moving TT definition from search.cpp to tt.cpp so to avoid using passing a pointer to TT to the current position. Finally simplify do_move(), now we miss a prefetch in the rare case of setting an en-passant square but code is much cleaner and performance penalty is almost zero. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
4251eac860
commit
76ae0e36be
5 changed files with 19 additions and 36 deletions
|
@ -72,14 +72,6 @@ Position::Position(const string& fen) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::setTranspositionTable() is used by search functions to pass
|
|
||||||
/// the pointer to the used TT so that do_move() will prefetch TT access.
|
|
||||||
|
|
||||||
void Position::setTranspositionTable(TranspositionTable* tt) {
|
|
||||||
TT = tt;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Position::from_fen() initializes the position object with the given FEN
|
/// Position::from_fen() initializes the position object with the given FEN
|
||||||
/// string. This function is not very robust - make sure that input FENs are
|
/// string. This function is not very robust - make sure that input FENs are
|
||||||
/// correct (this is assumed to be the responsibility of the GUI).
|
/// correct (this is assumed to be the responsibility of the GUI).
|
||||||
|
@ -728,6 +720,9 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
// case of non-reversible moves is taken care of later.
|
// case of non-reversible moves is taken care of later.
|
||||||
st->rule50++;
|
st->rule50++;
|
||||||
|
|
||||||
|
// Update side to move
|
||||||
|
st->key ^= zobSideToMove;
|
||||||
|
|
||||||
if (move_is_castle(m))
|
if (move_is_castle(m))
|
||||||
do_castle_move(m);
|
do_castle_move(m);
|
||||||
else if (move_is_promotion(m))
|
else if (move_is_promotion(m))
|
||||||
|
@ -750,11 +745,10 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
st->capture = type_of_piece_on(to);
|
st->capture = type_of_piece_on(to);
|
||||||
|
|
||||||
if (st->capture)
|
if (st->capture)
|
||||||
do_capture_move(st->capture, them, to);
|
do_capture_move(st->capture, them, to);
|
||||||
|
|
||||||
// Update hash key
|
// Update hash key
|
||||||
st->key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
|
st->key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
|
||||||
st->key ^= zobSideToMove;
|
|
||||||
|
|
||||||
// Reset en passant square
|
// Reset en passant square
|
||||||
if (st->epSquare != SQ_NONE)
|
if (st->epSquare != SQ_NONE)
|
||||||
|
@ -772,11 +766,8 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
st->key ^= zobCastle[st->castleRights];
|
st->key ^= zobCastle[st->castleRights];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checkEpSquare = (pt == PAWN && abs(int(to) - int(from)) == 16);
|
|
||||||
|
|
||||||
// Prefetch TT access as soon as we know key is updated
|
// Prefetch TT access as soon as we know key is updated
|
||||||
if (!checkEpSquare && TT)
|
TT.prefetch(st->key);
|
||||||
TT->prefetch(st->key);
|
|
||||||
|
|
||||||
// Move the piece
|
// Move the piece
|
||||||
Bitboard move_bb = make_move_bb(from, to);
|
Bitboard move_bb = make_move_bb(from, to);
|
||||||
|
@ -797,7 +788,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
st->pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
|
st->pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
|
||||||
|
|
||||||
// Set en passant square, only if moved pawn can be captured
|
// Set en passant square, only if moved pawn can be captured
|
||||||
if (checkEpSquare)
|
if (abs(int(to) - int(from)) == 16)
|
||||||
{
|
{
|
||||||
if ( (us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
|
if ( (us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
|
||||||
|| (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
|
|| (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
|
||||||
|
@ -808,10 +799,6 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prefetch only here in the few cases we needed zobEp[] to update the key
|
|
||||||
if (checkEpSquare && TT)
|
|
||||||
TT->prefetch(st->key);
|
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
st->mgValue += pst_delta<MidGame>(piece, from, to);
|
st->mgValue += pst_delta<MidGame>(piece, from, to);
|
||||||
st->egValue += pst_delta<EndGame>(piece, from, to);
|
st->egValue += pst_delta<EndGame>(piece, from, to);
|
||||||
|
@ -979,8 +966,6 @@ void Position::do_castle_move(Move m) {
|
||||||
|
|
||||||
// Update checkers BB
|
// Update checkers BB
|
||||||
st->checkersBB = attacks_to(king_square(them), us);
|
st->checkersBB = attacks_to(king_square(them), us);
|
||||||
|
|
||||||
st->key ^= zobSideToMove;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1071,8 +1056,6 @@ void Position::do_promotion_move(Move m) {
|
||||||
|
|
||||||
// Update checkers BB
|
// Update checkers BB
|
||||||
st->checkersBB = attacks_to(king_square(them), us);
|
st->checkersBB = attacks_to(king_square(them), us);
|
||||||
|
|
||||||
st->key ^= zobSideToMove;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1150,8 +1133,6 @@ void Position::do_ep_move(Move m) {
|
||||||
|
|
||||||
// Update checkers BB
|
// Update checkers BB
|
||||||
st->checkersBB = attacks_to(king_square(them), us);
|
st->checkersBB = attacks_to(king_square(them), us);
|
||||||
|
|
||||||
st->key ^= zobSideToMove;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1436,7 +1417,7 @@ void Position::do_null_move(StateInfo& backupSt) {
|
||||||
st->key ^= zobEp[st->epSquare];
|
st->key ^= zobEp[st->epSquare];
|
||||||
|
|
||||||
st->key ^= zobSideToMove;
|
st->key ^= zobSideToMove;
|
||||||
TT->prefetch(st->key);
|
TT.prefetch(st->key);
|
||||||
sideToMove = opposite_color(sideToMove);
|
sideToMove = opposite_color(sideToMove);
|
||||||
st->epSquare = SQ_NONE;
|
st->epSquare = SQ_NONE;
|
||||||
st->rule50++;
|
st->rule50++;
|
||||||
|
@ -1680,7 +1661,6 @@ void Position::clear() {
|
||||||
initialKFile = FILE_E;
|
initialKFile = FILE_E;
|
||||||
initialKRFile = FILE_H;
|
initialKRFile = FILE_H;
|
||||||
initialQRFile = FILE_A;
|
initialQRFile = FILE_A;
|
||||||
TT = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,6 @@ struct StateInfo {
|
||||||
StateInfo* previous;
|
StateInfo* previous;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TranspositionTable;
|
|
||||||
|
|
||||||
/// The position data structure. A position consists of the following data:
|
/// The position data structure. A position consists of the following data:
|
||||||
///
|
///
|
||||||
|
@ -259,7 +258,6 @@ public:
|
||||||
void undo_move(Move m);
|
void undo_move(Move m);
|
||||||
void do_null_move(StateInfo& st);
|
void do_null_move(StateInfo& st);
|
||||||
void undo_null_move();
|
void undo_null_move();
|
||||||
void setTranspositionTable(TranspositionTable* tt);
|
|
||||||
|
|
||||||
// Static exchange evaluation
|
// Static exchange evaluation
|
||||||
int see(Square from, Square to) const;
|
int see(Square from, Square to) const;
|
||||||
|
@ -358,7 +356,6 @@ private:
|
||||||
File initialKFile, initialKRFile, initialQRFile;
|
File initialKFile, initialKRFile, initialQRFile;
|
||||||
StateInfo startState;
|
StateInfo startState;
|
||||||
StateInfo* st;
|
StateInfo* st;
|
||||||
TranspositionTable* TT;
|
|
||||||
|
|
||||||
// Static variables
|
// Static variables
|
||||||
static int castleRightsMask[64];
|
static int castleRightsMask[64];
|
||||||
|
|
|
@ -190,9 +190,6 @@ namespace {
|
||||||
// Remaining depth: 1 ply 1.5 ply 2 ply 2.5 ply 3 ply 3.5 ply
|
// Remaining depth: 1 ply 1.5 ply 2 ply 2.5 ply 3 ply 3.5 ply
|
||||||
const Value RazorApprMargins[6] = { Value(0x520), Value(0x300), Value(0x300), Value(0x300), Value(0x300), Value(0x300) };
|
const Value RazorApprMargins[6] = { Value(0x520), Value(0x300), Value(0x300), Value(0x300), Value(0x300), Value(0x300) };
|
||||||
|
|
||||||
// The main transposition table
|
|
||||||
TranspositionTable TT;
|
|
||||||
|
|
||||||
|
|
||||||
/// Variables initialized by UCI options
|
/// Variables initialized by UCI options
|
||||||
|
|
||||||
|
@ -663,7 +660,6 @@ namespace {
|
||||||
|
|
||||||
// Initialize
|
// Initialize
|
||||||
TT.new_search();
|
TT.new_search();
|
||||||
p.setTranspositionTable(&TT);
|
|
||||||
H.clear();
|
H.clear();
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
|
|
12
src/tt.cpp
12
src/tt.cpp
|
@ -25,15 +25,19 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <xmmintrin.h>
|
|
||||||
|
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#include <xmmintrin.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
/// This is the number of TTEntry slots for each position
|
// This is the number of TTEntry slots for each position
|
||||||
static const int ClusterSize = 5;
|
static const int ClusterSize = 5;
|
||||||
|
|
||||||
|
// The main transposition table
|
||||||
|
TranspositionTable TT;
|
||||||
|
|
||||||
////
|
////
|
||||||
//// Functions
|
//// Functions
|
||||||
|
@ -174,7 +178,11 @@ TTEntry* TranspositionTable::retrieve(const Key posKey) const {
|
||||||
|
|
||||||
void TranspositionTable::prefetch(const Key posKey) const {
|
void TranspositionTable::prefetch(const Key posKey) const {
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
_mm_prefetch((char*)first_entry(posKey), _MM_HINT_T0);
|
_mm_prefetch((char*)first_entry(posKey), _MM_HINT_T0);
|
||||||
|
#else
|
||||||
|
__builtin_prefetch((const void*)first_entry(posKey), 0, 3);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
2
src/tt.h
2
src/tt.h
|
@ -105,4 +105,6 @@ private:
|
||||||
uint8_t generation;
|
uint8_t generation;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern TranspositionTable TT;
|
||||||
|
|
||||||
#endif // !defined(TT_H_INCLUDED)
|
#endif // !defined(TT_H_INCLUDED)
|
||||||
|
|
Loading…
Add table
Reference in a new issue