Skip to content

Commit

Permalink
small cleanups
Browse files Browse the repository at this point in the history
closes #2653

No functional change
  • Loading branch information
vondele committed May 23, 2020
1 parent d940e59 commit 383b12e
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/bitboard.cpp
Expand Up @@ -69,7 +69,7 @@ const std::string Bitboards::pretty(Bitboard b) {
void Bitboards::init() {

for (unsigned i = 0; i < (1 << 16); ++i)
PopCnt16[i] = std::bitset<16>(i).count();
PopCnt16[i] = uint8_t(std::bitset<16>(i).count());

for (Square s = SQ_A1; s <= SQ_H8; ++s)
SquareBB[s] = (1ULL << s);
Expand Down
2 changes: 1 addition & 1 deletion src/evaluate.cpp
Expand Up @@ -311,7 +311,7 @@ namespace {

if (Pt == BISHOP)
{
// Penalty according to number of pawns on the same color square as the
// Penalty according to the number of our pawns on the same color square as the
// bishop, bigger when the center files are blocked with pawns and smaller
// when the bishop is outside the pawn chain.
Bitboard blocked = pos.pieces(Us, PAWN) & shift<Down>(pos.pieces());
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char* argv[]) {
Position::init();
Bitbases::init();
Endgames::init();
Threads.set(Options["Threads"]);
Threads.set(size_t(Options["Threads"]));
Search::clear(); // After threads are up

UCI::loop(argc, argv);
Expand Down
44 changes: 30 additions & 14 deletions src/movegen.cpp
Expand Up @@ -213,8 +213,31 @@ namespace {


template<Color Us, GenType Type>
ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) {
ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantations
Bitboard target;

switch (Type)
{
case CAPTURES:
target = pos.pieces(~Us);
break;
case QUIETS:
case QUIET_CHECKS:
target = ~pos.pieces();
break;
case EVASIONS:
{
Square checksq = lsb(pos.checkers());
target = between_bb(pos.square<KING>(Us), checksq) | checksq;
break;
}
case NON_EVASIONS:
target = ~pos.pieces(Us);
break;
default:
static_assert(true, "Unsupported type in generate_all()");
}

moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target);
Expand Down Expand Up @@ -255,12 +278,8 @@ ExtMove* generate(const Position& pos, ExtMove* moveList) {

Color us = pos.side_to_move();

Bitboard target = Type == CAPTURES ? pos.pieces(~us)
: Type == QUIETS ? ~pos.pieces()
: Type == NON_EVASIONS ? ~pos.pieces(us) : 0;

return us == WHITE ? generate_all<WHITE, Type>(pos, moveList, target)
: generate_all<BLACK, Type>(pos, moveList, target);
return us == WHITE ? generate_all<WHITE, Type>(pos, moveList)
: generate_all<BLACK, Type>(pos, moveList);
}

// Explicit template instantiations
Expand Down Expand Up @@ -293,8 +312,8 @@ ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
*moveList++ = make_move(from, pop_lsb(&b));
}

return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces())
: generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces());
return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList)
: generate_all<BLACK, QUIET_CHECKS>(pos, moveList);
}


Expand Down Expand Up @@ -325,11 +344,8 @@ ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
return moveList; // Double check, only a king move can save the day

// Generate blocking evasions or captures of the checking piece
Square checksq = lsb(pos.checkers());
Bitboard target = between_bb(checksq, ksq) | checksq;

return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList, target)
: generate_all<BLACK, EVASIONS>(pos, moveList, target);
return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList)
: generate_all<BLACK, EVASIONS>(pos, moveList);
}


Expand Down
5 changes: 2 additions & 3 deletions src/pawns.cpp
Expand Up @@ -87,6 +87,7 @@ namespace {
e->passedPawns[Us] = 0;
e->kingSquares[Us] = SQ_NONE;
e->pawnAttacks[Us] = e->pawnAttacksSpan[Us] = pawn_attacks_bb<Us>(ourPawns);
e->blockedCount += popcount(shift<Up>(ourPawns) & (theirPawns | doubleAttackThem));

// Loop through all pawns of the current color and score each pawn
while ((s = *pl++) != SQ_NONE)
Expand All @@ -106,8 +107,6 @@ namespace {
phalanx = neighbours & rank_bb(s);
support = neighbours & rank_bb(s - Up);

e->blockedCount += blocked || more_than_one(leverPush);

// A pawn is backward when it is behind all pawns of the same color on
// the adjacent files and cannot safely advance.
backward = !(neighbours & forward_ranks_bb(Them, s + Up))
Expand Down Expand Up @@ -216,7 +215,7 @@ Score Entry::evaluate_shelter(const Position& pos, Square ksq) {
b = theirPawns & file_bb(f);
int theirRank = b ? relative_rank(Us, frontmost_sq(Them, b)) : 0;

File d = File(edge_distance(f));
int d = edge_distance(f);
bonus += make_score(ShelterStrength[d][ourRank], 0);

if (ourRank && (ourRank == theirRank - 1))
Expand Down
2 changes: 1 addition & 1 deletion src/position.cpp
Expand Up @@ -591,7 +591,7 @@ bool Position::pseudo_legal(const Move m) const {
if ( !(attacks_from<PAWN>(from, us) & pieces(~us) & to) // Not a capture
&& !((from + pawn_push(us) == to) && empty(to)) // Not a single push
&& !( (from + 2 * pawn_push(us) == to) // Not a double push
&& (rank_of(from) == relative_rank(us, RANK_2))
&& (relative_rank(us, from) == RANK_2)
&& empty(to)
&& empty(to - pawn_push(us))))
return false;
Expand Down
7 changes: 3 additions & 4 deletions src/position.h
Expand Up @@ -98,7 +98,7 @@ class Position {
bool is_on_semiopen_file(Color c, Square s) const;

// Castling
int castling_rights(Color c) const;
CastlingRights castling_rights(Color c) const;
bool can_castle(CastlingRights cr) const;
bool castling_impeded(CastlingRights cr) const;
Square castling_rook_square(CastlingRights cr) const;
Expand Down Expand Up @@ -268,7 +268,7 @@ inline bool Position::can_castle(CastlingRights cr) const {
return st->castlingRights & cr;
}

inline int Position::castling_rights(Color c) const {
inline CastlingRights Position::castling_rights(Color c) const {
return c & CastlingRights(st->castlingRights);
}

Expand Down Expand Up @@ -399,8 +399,7 @@ inline Thread* Position::this_thread() const {
inline void Position::put_piece(Piece pc, Square s) {

board[s] = pc;
byTypeBB[ALL_PIECES] |= s;
byTypeBB[type_of(pc)] |= s;
byTypeBB[ALL_PIECES] |= byTypeBB[type_of(pc)] |= s;
byColorBB[color_of(pc)] |= s;
index[s] = pieceCount[pc]++;
pieceList[pc][index[s]] = s;
Expand Down
14 changes: 7 additions & 7 deletions src/search.cpp
Expand Up @@ -272,9 +272,9 @@ void MainThread::search() {
Thread* bestThread = this;

// Check if there are threads with a better score than main thread
if ( Options["MultiPV"] == 1
if ( int(Options["MultiPV"]) == 1
&& !Limits.depth
&& !(Skill(Options["Skill Level"]).enabled() || Options["UCI_LimitStrength"])
&& !(Skill(Options["Skill Level"]).enabled() || int(Options["UCI_LimitStrength"]))
&& rootMoves[0].pv[0] != MOVE_NONE)
{
std::map<Move, int64_t> votes;
Expand Down Expand Up @@ -350,17 +350,17 @@ void Thread::search() {
if (mainThread)
{
if (mainThread->bestPreviousScore == VALUE_INFINITE)
for (int i=0; i<4; ++i)
for (int i = 0; i < 4; ++i)
mainThread->iterValue[i] = VALUE_ZERO;
else
for (int i=0; i<4; ++i)
for (int i = 0; i < 4; ++i)
mainThread->iterValue[i] = mainThread->bestPreviousScore;
}

std::copy(&lowPlyHistory[2][0], &lowPlyHistory.back().back() + 1, &lowPlyHistory[0][0]);
std::fill(&lowPlyHistory[MAX_LPH - 2][0], &lowPlyHistory.back().back() + 1, 0);

size_t multiPV = Options["MultiPV"];
size_t multiPV = size_t(Options["MultiPV"]);

// Pick integer skill levels, but non-deterministically round up or down
// such that the average integer skill corresponds to the input floating point one.
Expand Down Expand Up @@ -540,8 +540,8 @@ void Thread::search() {
&& !Threads.stop
&& !mainThread->stopOnPonderhit)
{
double fallingEval = (332 + 6 * (mainThread->bestPreviousScore - bestValue)
+ 6 * (mainThread->iterValue[iterIdx] - bestValue)) / 704.0;
double fallingEval = (332 + 6 * (mainThread->bestPreviousScore - bestValue)
+ 6 * (mainThread->iterValue[iterIdx] - bestValue)) / 704.0;
fallingEval = Utility::clamp(fallingEval, 0.5, 1.5);

// If the bestMove is stable over several iterations, reduce time accordingly
Expand Down
6 changes: 3 additions & 3 deletions src/syzygy/tbprobe.cpp
Expand Up @@ -530,7 +530,7 @@ int decompress_pairs(PairsData* d, uint64_t idx) {
// I(k) = k * d->span + d->span / 2 (1)

// First step is to get the 'k' of the I(k) nearest to our idx, using definition (1)
uint32_t k = idx / d->span;
uint32_t k = uint32_t(idx / d->span);

// Then we read the corresponding SparseIndex[] entry
uint32_t block = number<uint32_t, LittleEndian>(&d->sparseIndex[k].block);
Expand Down Expand Up @@ -576,7 +576,7 @@ int decompress_pairs(PairsData* d, uint64_t idx) {
// All the symbols of a given length are consecutive integers (numerical
// sequence property), so we can compute the offset of our symbol of
// length len, stored at the beginning of buf64.
sym = (buf64 - d->base64[len]) >> (64 - len - d->minSymLen);
sym = Sym((buf64 - d->base64[len]) >> (64 - len - d->minSymLen));

// Now add the value of the lowest symbol of length len to get our symbol
sym += number<Sym, LittleEndian>(&d->lowestSym[len]);
Expand Down Expand Up @@ -984,7 +984,7 @@ uint8_t* set_sizes(PairsData* d, uint8_t* data) {

d->sizeofBlock = 1ULL << *data++;
d->span = 1ULL << *data++;
d->sparseIndexSize = (tbSize + d->span - 1) / d->span; // Round up
d->sparseIndexSize = size_t((tbSize + d->span - 1) / d->span); // Round up
auto padding = number<uint8_t, LittleEndian>(data++);
d->blocksNum = number<uint32_t, LittleEndian>(data); data += sizeof(uint32_t);
d->blockLengthSize = d->blocksNum + padding; // Padded to ensure SparseIndex[]
Expand Down
2 changes: 1 addition & 1 deletion src/thread.cpp
Expand Up @@ -151,7 +151,7 @@ void ThreadPool::set(size_t requested) {
clear();

// Reallocate the hash with the new threadpool size
TT.resize(Options["Hash"]);
TT.resize(size_t(Options["Hash"]));

// Init thread number dependent search params.
Search::init();
Expand Down
12 changes: 6 additions & 6 deletions src/timeman.cpp
Expand Up @@ -35,10 +35,10 @@ TimeManagement Time; // Our global time management object

void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {

TimePoint minThinkingTime = Options["Minimum Thinking Time"];
TimePoint moveOverhead = Options["Move Overhead"];
TimePoint slowMover = Options["Slow Mover"];
TimePoint npmsec = Options["nodestime"];
TimePoint minThinkingTime = TimePoint(Options["Minimum Thinking Time"]);
TimePoint moveOverhead = TimePoint(Options["Move Overhead"]);
TimePoint slowMover = TimePoint(Options["Slow Mover"]);
TimePoint npmsec = TimePoint(Options["nodestime"]);

// opt_scale is a percentage of available time to use for the current move.
// max_scale is a multiplier applied to optimumTime.
Expand Down Expand Up @@ -91,8 +91,8 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
}

// Never use more than 80% of the available time for this move
optimumTime = std::max<int>(minThinkingTime, opt_scale * timeLeft);
maximumTime = std::min(0.8 * limits.time[us] - moveOverhead, max_scale * optimumTime);
optimumTime = std::max(minThinkingTime, TimePoint(opt_scale * timeLeft));
maximumTime = TimePoint(std::min(0.8 * limits.time[us] - moveOverhead, max_scale * optimumTime));

if (Options["Ponder"])
optimumTime += optimumTime / 4;
Expand Down
6 changes: 3 additions & 3 deletions src/tt.cpp
Expand Up @@ -94,16 +94,16 @@ void TranspositionTable::clear() {
WinProcGroup::bindThisThread(idx);

// Each thread will zero its part of the hash table
const size_t stride = clusterCount / Options["Threads"],
start = stride * idx,
const size_t stride = size_t(clusterCount / Options["Threads"]),
start = size_t(stride * idx),
len = idx != Options["Threads"] - 1 ?
stride : clusterCount - start;

std::memset(&table[start], 0, len * sizeof(Cluster));
});
}

for (std::thread& th: threads)
for (std::thread& th : threads)
th.join();
}

Expand Down
6 changes: 3 additions & 3 deletions src/tune.cpp
Expand Up @@ -83,7 +83,7 @@ template<> void Tune::Entry<int>::init_option() { make_option(name, value, range

template<> void Tune::Entry<int>::read_option() {
if (Options.count(name))
value = Options[name];
value = int(Options[name]);
}

template<> void Tune::Entry<Value>::init_option() { make_option(name, value, range); }
Expand All @@ -100,10 +100,10 @@ template<> void Tune::Entry<Score>::init_option() {

template<> void Tune::Entry<Score>::read_option() {
if (Options.count("m" + name))
value = make_score(Options["m" + name], eg_value(value));
value = make_score(int(Options["m" + name]), eg_value(value));

if (Options.count("e" + name))
value = make_score(mg_value(value), Options["e" + name]);
value = make_score(mg_value(value), int(Options["e" + name]));
}

// Instead of a variable here we have a PostUpdate function: just call it
Expand Down
4 changes: 2 additions & 2 deletions src/ucioption.cpp
Expand Up @@ -38,9 +38,9 @@ namespace UCI {

/// 'On change' actions, triggered by an option's value change
void on_clear_hash(const Option&) { Search::clear(); }
void on_hash_size(const Option& o) { TT.resize(o); }
void on_hash_size(const Option& o) { TT.resize(size_t(o)); }
void on_logger(const Option& o) { start_logger(o); }
void on_threads(const Option& o) { Threads.set(o); }
void on_threads(const Option& o) { Threads.set(size_t(o)); }
void on_tb_path(const Option& o) { Tablebases::init(o); }


Expand Down

0 comments on commit 383b12e

Please sign in to comment.