Skip to content

Commit

Permalink
Assorted trivial cleanups 3/2019 (#2030)
Browse files Browse the repository at this point in the history
No functional change.
  • Loading branch information
mcostalba authored Mar 31, 2019
1 parent 95ba7f7 commit 82ad9ce
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 39 deletions.
3 changes: 1 addition & 2 deletions src/bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
uint8_t PopCnt16[1 << 16];
uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];

Bitboard SquareBB[SQUARE_NB];
Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
Bitboard DistanceRingBB[SQUARE_NB][8];
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
Bitboard SquareBB[SQUARE_NB];

Bitboard KingFlank[FILE_NB] = {
QueenSide ^ FileDBB, QueenSide, QueenSide,
Expand Down
11 changes: 5 additions & 6 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
extern uint8_t PopCnt16[1 << 16];
extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];

extern Bitboard SquareBB[SQUARE_NB];
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
extern Bitboard DistanceRingBB[SQUARE_NB][8];
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
extern Bitboard KingFlank[FILE_NB];
extern Bitboard SquareBB[SQUARE_NB];


/// Magic holds all magic bitboards relevant data for a single square
Expand Down Expand Up @@ -102,15 +102,14 @@ struct Magic {
extern Magic RookMagics[SQUARE_NB];
extern Magic BishopMagics[SQUARE_NB];


/// Overloads of bitwise operators between a Bitboard and a Square for testing
/// whether a given bit is set in a bitboard, and for setting and clearing bits.

inline Bitboard square_bb(Square s) {
assert(s >= SQ_A1 && s <= SQ_H8);
return SquareBB[s];
}


/// Overloads of bitwise operators between a Bitboard and a Square for testing
/// whether a given bit is set in a bitboard, and for setting and clearing bits.

inline Bitboard operator&( Bitboard b, Square s) { return b & square_bb(s); }
inline Bitboard operator|( Bitboard b, Square s) { return b | square_bb(s); }
inline Bitboard operator^( Bitboard b, Square s) { return b ^ square_bb(s); }
Expand Down
19 changes: 8 additions & 11 deletions src/endgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ namespace {
if (file_of(pos.square<PAWN>(strongSide)) >= FILE_E)
sq = Square(sq ^ 7); // Mirror SQ_H1 -> SQ_A1

if (strongSide == BLACK)
sq = ~sq;

return sq;
return strongSide == WHITE ? sq : ~sq;
}

} // namespace
Expand Down Expand Up @@ -285,18 +282,18 @@ Value Endgame<KQKR>::operator()(const Position& pos) const {
}


/// KNN vs KP. Simply push the opposing king to the corner.
/// KNN vs KP. Simply push the opposing king to the corner
template<>
Value Endgame<KNNKP>::operator()(const Position& pos) const {

assert(verify_material(pos, strongSide, 2 * KnightValueMg, 0));
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
assert(verify_material(pos, strongSide, 2 * KnightValueMg, 0));
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));

Value result = 2 * KnightValueEg
- PawnValueEg
+ PushToEdges[pos.square<KING>(weakSide)];
Value result = 2 * KnightValueEg
- PawnValueEg
+ PushToEdges[pos.square<KING>(weakSide)];

return strongSide == pos.side_to_move() ? result : -result;
return strongSide == pos.side_to_move() ? result : -result;
}


Expand Down
4 changes: 2 additions & 2 deletions src/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ Entry* probe(const Position& pos) {

// OK, we didn't find any special evaluation function for the current material
// configuration. Is there a suitable specialized scaling function?
const EndgameBase<ScaleFactor>* sf;
const auto* sf = pos.this_thread()->endgames.probe<ScaleFactor>(key);

if ((sf = pos.this_thread()->endgames.probe<ScaleFactor>(key)) != nullptr)
if (sf)
{
e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned
return e;
Expand Down
2 changes: 1 addition & 1 deletion src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct HashTable {
Entry* operator[](Key key) { return &table[(uint32_t)key & (Size - 1)]; }

private:
std::vector<Entry> table = std::vector<Entry>(Size);
std::vector<Entry> table = std::vector<Entry>(Size); // Allocate on the heap
};


Expand Down
2 changes: 1 addition & 1 deletion src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ bool Position::pseudo_legal(const Move m) const {
{
// We have already handled promotion moves, so destination
// cannot be on the 8th/1st rank.
if (rank_of(to) == relative_rank(us, RANK_8))
if ((Rank8BB | Rank1BB) & to)
return false;

if ( !(attacks_from<PAWN>(from, us) & pieces(~us) & to) // Not a capture
Expand Down
2 changes: 1 addition & 1 deletion src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ inline void Position::move_piece(Piece pc, Square from, Square to) {

// index[from] is not updated and becomes stale. This works as long as index[]
// is accessed just by known occupied squares.
Bitboard fromTo = square_bb(from) ^ square_bb(to);
Bitboard fromTo = square_bb(from) | square_bb(to);
byTypeBB[ALL_PIECES] ^= fromTo;
byTypeBB[type_of(pc)] ^= fromTo;
byColorBB[color_of(pc)] ^= fromTo;
Expand Down
28 changes: 14 additions & 14 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ namespace {
return d > 17 ? 0 : 29 * d * d + 138 * d - 134;
}

// Add a small random component to draw evaluations to keep search dynamic
// and to avoid 3fold-blindness.
// Add a small random component to draw evaluations to avoid 3fold-blindness
Value value_draw(Depth depth, Thread* thisThread) {
return depth < 4 ? VALUE_DRAW
: VALUE_DRAW + Value(2 * (thisThread->nodes.load(std::memory_order_relaxed) % 2) - 1);
: VALUE_DRAW + Value(2 * (thisThread->nodes & 1) - 1);
}

// Skill structure is used to implement strength limit
Expand Down Expand Up @@ -162,12 +161,12 @@ void Search::clear() {
Time.availableNodes = 0;
TT.clear();
Threads.clear();
Tablebases::init(Options["SyzygyPath"]); // Free up mapped files
Tablebases::init(Options["SyzygyPath"]); // Free mapped files
}


/// MainThread::search() is called by the main thread when the program receives
/// the UCI 'go' command. It searches from the root position and outputs the "bestmove".
/// MainThread::search() is started when the program receives the UCI 'go'
/// command. It searches from the root position and outputs the "bestmove".

void MainThread::search() {

Expand Down Expand Up @@ -221,8 +220,9 @@ void MainThread::search() {
if (Limits.npmsec)
Time.availableNodes += Limits.inc[us] - Threads.nodes_searched();

// Check if there are threads with a better score than main thread
Thread* bestThread = this;

// Check if there are threads with a better score than main thread
if ( Options["MultiPV"] == 1
&& !Limits.depth
&& !Skill(Options["Skill Level"]).enabled()
Expand Down Expand Up @@ -273,9 +273,9 @@ void MainThread::search() {

void Thread::search() {

// To allow access to (ss-5) up to (ss+2), the stack must be oversized.
// To allow access to (ss-7) up to (ss+2), the stack must be oversized.
// The former is needed to allow update_continuation_histories(ss-1, ...),
// which accesses its argument at ss-4, also near the root.
// which accesses its argument at ss-6, also near the root.
// The latter is needed for statScores and killer initialization.
Stack stack[MAX_PLY+10], *ss = stack+7;
Move pv[MAX_PLY+1];
Expand Down Expand Up @@ -317,7 +317,7 @@ void Thread::search() {
: Options["Analysis Contempt"] == "Black" && us == WHITE ? -ct
: ct;

// In evaluate.cpp the evaluation is from the white point of view
// Evaluation score is from the white point of view
contempt = (us == WHITE ? make_score(ct, ct / 2)
: -make_score(ct, ct / 2));

Expand Down Expand Up @@ -832,8 +832,7 @@ namespace {
}

// Step 11. Internal iterative deepening (~2 Elo)
if ( depth >= 8 * ONE_PLY
&& !ttMove)
if (depth >= 8 * ONE_PLY && !ttMove)
{
search<NT>(pos, ss, alpha, beta, depth - 7 * ONE_PLY, cutNode);

Expand All @@ -847,15 +846,16 @@ namespace {
const PieceToHistory* contHist[] = { (ss-1)->continuationHistory, (ss-2)->continuationHistory,
nullptr, (ss-4)->continuationHistory,
nullptr, (ss-6)->continuationHistory };

Move countermove = thisThread->counterMoves[pos.piece_on(prevSq)][prevSq];

MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory,
&thisThread->captureHistory,
contHist,
countermove,
ss->killers);
value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc

value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
moveCountPruning = false;
ttCapture = ttMove && pos.capture_or_promotion(ttMove);

Expand Down Expand Up @@ -946,7 +946,7 @@ namespace {
&& bestValue > VALUE_MATED_IN_MAX_PLY)
{
// Skip quiet moves if movecount exceeds our FutilityMoveCount threshold
moveCountPruning = moveCount >= futility_move_count(improving,depth / ONE_PLY);
moveCountPruning = moveCount >= futility_move_count(improving, depth / ONE_PLY);

if ( !captureOrPromotion
&& !givesCheck
Expand Down
2 changes: 1 addition & 1 deletion src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ThreadPool Threads; // Global object


/// Thread constructor launches the thread and waits until it goes to sleep
/// in idle_loop(). Note that 'searching' and 'exit' should be alredy set.
/// in idle_loop(). Note that 'searching' and 'exit' should be already set.

Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) {

Expand Down

0 comments on commit 82ad9ce

Please sign in to comment.