1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-29 16:23:09 +00:00

Retire move.h

Also some assorted comments fixes and other trivia.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2011-12-04 10:53:40 +01:00
parent a44c5cf4f7
commit 81cd417b45
19 changed files with 164 additions and 205 deletions

View file

@ -28,7 +28,7 @@
using namespace std;
static const string Defaults[] = {
static const char* Defaults[] = {
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
@ -59,7 +59,7 @@ static const string Defaults[] = {
void benchmark(int argc, char* argv[]) {
std::vector<Move> searchMoves(1, MOVE_NONE);
vector<Move> searchMoves(1, MOVE_NONE);
vector<string> fenList;
Search::LimitsType limits;
int64_t totalNodes;
@ -85,7 +85,10 @@ void benchmark(int argc, char* argv[]) {
limits.maxDepth = atoi(valStr.c_str());
// Do we need to load positions from a given FEN file?
if (fenFile != "default")
if (fenFile == "default")
for (int i = 0; *Defaults[i]; i++)
fenList.push_back(Defaults[i]);
else
{
string fen;
ifstream f(fenFile.c_str());
@ -102,9 +105,6 @@ void benchmark(int argc, char* argv[]) {
f.close();
}
else // Load default positions
for (int i = 0; !Defaults[i].empty(); i++)
fenList.push_back(Defaults[i]);
// Ok, let's start the benchmark !
totalNodes = 0;

View file

@ -28,6 +28,7 @@
#include <iostream>
#include "book.h"
#include "misc.h"
#include "movegen.h"
using namespace std;

View file

@ -23,14 +23,13 @@
#include <fstream>
#include <string>
#include "move.h"
#include "position.h"
#include "rkiss.h"
// A Polyglot book is a series of "entries" of 16 bytes. All integers are
// stored highest byte first (regardless of size). The entries are ordered
// according to key. Lowest key first.
/// A Polyglot book is a series of "entries" of 16 bytes. All integers are
/// stored highest byte first (regardless of size). The entries are ordered
/// according to key. Lowest key first.
struct BookEntry {
uint64_t key;
uint16_t move;

View file

@ -17,7 +17,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(_MSC_VER)
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_DEPRECATE
#define NOMINMAX // disable macros min() and max()
#include <windows.h>
#include <sys/timeb.h>
#else
# include <sys/time.h>
# include <sys/types.h>
@ -26,13 +33,6 @@
# include <sys/pstat.h>
# endif
#else
#define _CRT_SECURE_NO_DEPRECATE
#define NOMINMAX // disable macros min() and max()
#include <windows.h>
#include <sys/timeb.h>
#endif
#if !defined(NO_PREFETCH)
@ -178,18 +178,13 @@ int cpu_count() {
/// timed_wait() waits for msec milliseconds. It is mainly an helper to wrap
/// conversion from milliseconds to struct timespec, as used by pthreads.
void timed_wait(WaitCondition* sleepCond, Lock* sleepLock, int msec) {
#if defined(_MSC_VER)
void timed_wait(WaitCondition* sleepCond, Lock* sleepLock, int msec) {
cond_timedwait(sleepCond, sleepLock, msec);
}
int tm = msec;
#else
void timed_wait(WaitCondition* sleepCond, Lock* sleepLock, int msec) {
struct timeval t;
struct timespec abstime;
struct timespec abstime, *tm = &abstime;
gettimeofday(&t, NULL);
@ -201,12 +196,11 @@ void timed_wait(WaitCondition* sleepCond, Lock* sleepLock, int msec) {
abstime.tv_sec += 1;
abstime.tv_nsec -= 1000000000LL;
}
cond_timedwait(sleepCond, sleepLock, &abstime);
}
#endif
cond_timedwait(sleepCond, sleepLock, tm);
}
/// prefetch() preloads the given address in L1/L2 cache. This is a non
/// blocking function and do not stalls the CPU waiting for data to be

View file

@ -41,6 +41,11 @@ extern void dbg_mean_of(int v);
extern void dbg_print_hit_rate();
extern void dbg_print_mean();
class Position;
extern Move move_from_uci(const Position& pos, const std::string& str);
extern const std::string move_to_uci(Move m, bool chess960);
extern const std::string move_to_san(Position& pos, Move m);
struct Log : public std::ofstream {
Log(const std::string& f = "log.txt") : std::ofstream(f.c_str(), std::ios::out | std::ios::app) {}
~Log() { if (is_open()) close(); }

View file

@ -21,7 +21,6 @@
#include <cstring>
#include <string>
#include "move.h"
#include "movegen.h"
#include "position.h"

View file

@ -1,131 +0,0 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(MOVE_H_INCLUDED)
#define MOVE_H_INCLUDED
#include <string>
#include "misc.h"
#include "types.h"
// Maximum number of allowed moves per position
const int MAX_MOVES = 256;
/// A move needs 16 bits to be stored
///
/// bit 0- 5: destination square (from 0 to 63)
/// bit 6-11: origin square (from 0 to 63)
/// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2)
/// bit 14-15: special move flag: promotion (1), en passant (2), castle (3)
///
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in
/// because in any normal move destination square is always different
/// from origin square while MOVE_NONE and MOVE_NULL have the same
/// origin and destination square, 0 and 1 respectively.
enum Move {
MOVE_NONE = 0,
MOVE_NULL = 65
};
struct MoveStack {
Move move;
int score;
};
inline bool operator<(const MoveStack& f, const MoveStack& s) { return f.score < s.score; }
// An helper insertion sort implementation, works with pointers and iterators
template<typename T, typename K>
inline void sort(K firstMove, K lastMove)
{
T value;
K cur, p, d;
if (firstMove != lastMove)
for (cur = firstMove + 1; cur != lastMove; cur++)
{
p = d = cur;
value = *p--;
if (*p < value)
{
do *d = *p;
while (--d != firstMove && *--p < value);
*d = value;
}
}
}
inline Square move_from(Move m) {
return Square((m >> 6) & 0x3F);
}
inline Square move_to(Move m) {
return Square(m & 0x3F);
}
inline bool is_special(Move m) {
return m & (3 << 14);
}
inline bool is_promotion(Move m) {
return (m & (3 << 14)) == (1 << 14);
}
inline int is_enpassant(Move m) {
return (m & (3 << 14)) == (2 << 14);
}
inline int is_castle(Move m) {
return (m & (3 << 14)) == (3 << 14);
}
inline PieceType promotion_piece_type(Move m) {
return PieceType(((m >> 12) & 3) + 2);
}
inline Move make_move(Square from, Square to) {
return Move(to | (from << 6));
}
inline Move make_promotion_move(Square from, Square to, PieceType promotion) {
return Move(to | (from << 6) | (1 << 14) | ((promotion - 2) << 12)) ;
}
inline Move make_enpassant_move(Square from, Square to) {
return Move(to | (from << 6) | (2 << 14));
}
inline Move make_castle_move(Square from, Square to) {
return Move(to | (from << 6) | (3 << 14));
}
inline bool is_ok(Move m) {
return move_from(m) != move_to(m); // Catches also MOVE_NONE
}
class Position;
extern const std::string move_to_uci(Move m, bool chess960);
extern Move move_from_uci(const Position& pos, const std::string& str);
extern const std::string move_to_san(Position& pos, Move m);
#endif // !defined(MOVE_H_INCLUDED)

View file

@ -47,8 +47,7 @@ namespace {
template<PieceType Pt>
inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
assert(Pt != QUEEN);
assert(Pt != PAWN);
assert(Pt != QUEEN && Pt != PAWN);
Bitboard b = pos.attacks_from<Pt>(from) & pos.empty_squares();
@ -62,8 +61,7 @@ namespace {
template<PieceType Pt>
inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
Bitboard dc, Square ksq) {
assert(Pt != KING);
assert(Pt != PAWN);
assert(Pt != KING && Pt != PAWN);
Bitboard checkSqs, b;
Square from;
@ -198,7 +196,7 @@ template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mli
template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
/// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
/// generate<MV_NON_CAPTURE_CHECK> generates all pseudo-legal non-captures and knight
/// underpromotions that give check. Returns a pointer to the end of the move list.
template<>
MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
@ -238,8 +236,8 @@ MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist)
}
/// generate_evasions() generates all pseudo-legal check evasions when
/// the side to move is in check. Returns a pointer to the end of the move list.
/// generate<MV_EVASION> generates all pseudo-legal check evasions when the side
/// to move is in check. Returns a pointer to the end of the move list.
template<>
MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {

View file

@ -20,7 +20,7 @@
#if !defined(MOVEGEN_H_INCLUDED)
#define MOVEGEN_H_INCLUDED
#include "move.h"
#include "types.h"
enum MoveType {
MV_CAPTURE,
@ -32,6 +32,8 @@ enum MoveType {
MV_LEGAL
};
class Position;
template<MoveType>
MoveStack* generate(const Position& pos, MoveStack* mlist);

View file

@ -21,7 +21,6 @@
#define MOVEPICK_H_INCLUDED
#include "history.h"
#include "move.h"
#include "position.h"
#include "types.h"

View file

@ -23,12 +23,12 @@
#include <cassert>
#include "bitboard.h"
#include "move.h"
#include "types.h"
/// The checkInfo struct is initialized at c'tor time and keeps info used
/// to detect if a move gives check.
class Position;
struct CheckInfo {
@ -44,7 +44,6 @@ struct CheckInfo {
/// object to its previous state when we retract a move. Whenever a move
/// is made on the board (by calling Position::do_move), an StateInfo object
/// must be passed as a parameter.
class Position;
struct StateInfo {
Key pawnKey, materialKey;

View file

@ -24,9 +24,11 @@
#define S(mg, eg) make_score(mg, eg)
// PSQT[PieceType][Square] contains Piece-Square scores. For each piece type on a
// given square a (midgame, endgame) score pair is assigned. PSQT is defined for
// white side, for black side the tables are symmetric.
/// PSQT[PieceType][Square] contains Piece-Square scores. For each piece type on
/// a given square a (midgame, endgame) score pair is assigned. PSQT is defined
/// for white side, for black side the tables are symmetric.
static const Score PSQT[][64] = {
{ },
{ // Pawn

View file

@ -20,19 +20,6 @@
available under the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
** George Marsaglia invented the RNG-Kiss-family in the early 90's.
** This is a specific version that Heinz van Saanen derived and
** tested from some public domain code by Bob Jenkins:
**
** Quite platform independent
** Passes ALL dieharder tests! Here *nix sys-rand() e.g. fails miserably:-)
** ~12 times faster than my *nix sys-rand()
** ~4 times faster than SSE2-version of Mersenne twister
** Average cycle length: ~2^126
** 64 bit seed
** Return doubles with a full 53 bit mantissa
** Thread safe
*/
#if !defined(RKISS_H_INCLUDED)
@ -40,6 +27,20 @@
#include "types.h"
/// RKISS is our pseudo random number generator (PRNG) used to compute hash keys.
/// George Marsaglia invented the RNG-Kiss-family in the early 90's. This is a
/// specific version that Heinz van Saanen derived from some public domain code
/// by Bob Jenkins. Following the feature list, as tested by Heinz.
///
/// - Quite platform independent
/// - Passes ALL dieharder tests! Here *nix sys-rand() e.g. fails miserably:-)
/// - ~12 times faster than my *nix sys-rand()
/// - ~4 times faster than SSE2-version of Mersenne twister
/// - Average cycle length: ~2^126
/// - 64 bit seed
/// - Return doubles with a full 53 bit mantissa
/// - Thread safe
class RKISS {
// Keep variables always together

View file

@ -30,7 +30,6 @@
#include "evaluate.h"
#include "history.h"
#include "misc.h"
#include "move.h"
#include "movegen.h"
#include "movepick.h"
#include "search.h"
@ -353,10 +352,9 @@ int64_t Search::perft(Position& pos, Depth depth) {
}
/// think() is the external interface to Stockfish's search, and is called when
/// the program receives the UCI 'go' command. It initializes various global
/// variables, and calls id_loop(). It returns false when a "quit" command is
/// received during the search.
/// think() is the external interface to Stockfish's search, and is called by the
/// main thread when the program receives the UCI 'go' command. It searches from
/// RootPosition and at the end prints the "bestmove" to output.
void Search::think() {

View file

@ -23,7 +23,6 @@
#include <cstring>
#include <vector>
#include "move.h"
#include "types.h"
class Position;
@ -74,7 +73,7 @@ extern void init();
extern int64_t perft(Position& pos, Depth depth);
extern void think();
}
} // namespace
extern void do_timer_event();

View file

@ -20,11 +20,11 @@
#if !defined(TIMEMAN_H_INCLUDED)
#define TIMEMAN_H_INCLUDED
struct SearchLimits;
/// The TimeManager class computes the optimal time to think depending on the
/// maximum available time, the move game number and other parameters.
class TimeManager {
public:
void init(const Search::LimitsType& limits, int currentPly);
void pv_instability(int curChanges, int prevChanges);
int available_time() const { return optimumSearchTime + unstablePVExtraTime; }

View file

@ -22,7 +22,7 @@
#include <iostream>
#include "move.h"
#include "misc.h"
#include "types.h"

View file

@ -155,6 +155,7 @@ const bool CpuIs64Bit = false;
typedef uint64_t Key;
typedef uint64_t Bitboard;
const int MAX_MOVES = 256;
const int PLY_MAX = 100;
const int PLY_MAX_PLUS_2 = PLY_MAX + 2;
@ -176,6 +177,31 @@ const Bitboard Rank6BB = Rank1BB << (8 * 5);
const Bitboard Rank7BB = Rank1BB << (8 * 6);
const Bitboard Rank8BB = Rank1BB << (8 * 7);
/// A move needs 16 bits to be stored
///
/// bit 0- 5: destination square (from 0 to 63)
/// bit 6-11: origin square (from 0 to 63)
/// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2)
/// bit 14-15: special move flag: promotion (1), en passant (2), castle (3)
///
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in because in
/// any normal move destination square is always different from origin square
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
enum Move {
MOVE_NONE = 0,
MOVE_NULL = 65
};
struct MoveStack {
Move move;
int score;
};
inline bool operator<(const MoveStack& f, const MoveStack& s) {
return f.score < s.score;
}
enum ValueType {
VALUE_TYPE_NONE = 0,
VALUE_TYPE_UPPER = 1,
@ -467,4 +493,73 @@ inline Square pawn_push(Color c) {
return c == WHITE ? DELTA_N : DELTA_S;
}
// An helper insertion sort implementation, works with pointers and iterators
template<typename T, typename K>
inline void sort(K firstMove, K lastMove)
{
T value;
K cur, p, d;
if (firstMove != lastMove)
for (cur = firstMove + 1; cur != lastMove; cur++)
{
p = d = cur;
value = *p--;
if (*p < value)
{
do *d = *p;
while (--d != firstMove && *--p < value);
*d = value;
}
}
}
inline Square move_from(Move m) {
return Square((m >> 6) & 0x3F);
}
inline Square move_to(Move m) {
return Square(m & 0x3F);
}
inline bool is_special(Move m) {
return m & (3 << 14);
}
inline bool is_promotion(Move m) {
return (m & (3 << 14)) == (1 << 14);
}
inline int is_enpassant(Move m) {
return (m & (3 << 14)) == (2 << 14);
}
inline int is_castle(Move m) {
return (m & (3 << 14)) == (3 << 14);
}
inline PieceType promotion_piece_type(Move m) {
return PieceType(((m >> 12) & 3) + 2);
}
inline Move make_move(Square from, Square to) {
return Move(to | (from << 6));
}
inline Move make_promotion_move(Square from, Square to, PieceType promotion) {
return Move(to | (from << 6) | (1 << 14) | ((promotion - 2) << 12)) ;
}
inline Move make_enpassant_move(Square from, Square to) {
return Move(to | (from << 6) | (2 << 14));
}
inline Move make_castle_move(Square from, Square to) {
return Move(to | (from << 6) | (3 << 14));
}
inline bool is_ok(Move m) {
return move_from(m) != move_to(m); // Catches also MOVE_NULL and MOVE_NONE
}
#endif // !defined(TYPES_H_INCLUDED)

View file

@ -24,7 +24,6 @@
#include "evaluate.h"
#include "misc.h"
#include "move.h"
#include "position.h"
#include "search.h"
#include "thread.h"