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

Improve Stats definition

Use operator const T&() instead of operator T() to avoid possible
costly hidden copies of non-scalar nested types.

Currently StatsEntry has a single member T, so assuming
sizeof(StatsEntry) == sizeof(T) it happens to work, but it's
better to use the size of the proper entry type in std::fill.
Note that current code works because std::array items are ensured
to be allocated in contiguous memory and there is no padding among
nested arrays. The latter condition does not seem to be strictly
enforced by the standard, so be careful here.

Finally use address-of operator instead of get() to fully hide the
wrapper class StatsEntry at calling sites. For completness add
the arrow operator too and simplify the C++ code a bit more.

Same binary code as previous master under the Clang compiler.

No functional change.
This commit is contained in:
Marco Costalba 2018-07-28 15:33:39 +02:00 committed by Stéphane Nicolet
parent fae57273b2
commit 571f54b176
3 changed files with 22 additions and 21 deletions

View file

@ -36,15 +36,13 @@
template<typename T, int D> template<typename T, int D>
class StatsEntry { class StatsEntry {
static const bool IsInt = std::is_integral<T>::value;
typedef typename std::conditional<IsInt, int, T>::type TT;
T entry; T entry;
public: public:
T* get() { return &entry; }
void operator=(const T& v) { entry = v; } void operator=(const T& v) { entry = v; }
operator TT() const { return entry; } T* operator&() { return &entry; }
T* operator->() { return &entry; }
operator const T&() const { return entry; }
void operator<<(int bonus) { void operator<<(int bonus) {
assert(abs(bonus) <= D); // Ensure range is [-D, D] assert(abs(bonus) <= D); // Ensure range is [-D, D]
@ -64,18 +62,21 @@ public:
template <typename T, int D, int Size, int... Sizes> template <typename T, int D, int Size, int... Sizes>
struct Stats : public std::array<Stats<T, D, Sizes...>, Size> struct Stats : public std::array<Stats<T, D, Sizes...>, Size>
{ {
T* get() { return this->at(0).get(); } typedef Stats<T, D, Size, Sizes...> stats;
void fill(const T& v) { void fill(const T& v) {
T* p = get();
std::fill(p, p + sizeof(*this) / sizeof(*p), v); // For standard-layout 'this' points to first struct member
assert(std::is_standard_layout<stats>::value);
typedef StatsEntry<T, D> entry;
entry* p = reinterpret_cast<entry*>(this);
std::fill(p, p + sizeof(*this) / sizeof(entry), v);
} }
}; };
template <typename T, int D, int Size> template <typename T, int D, int Size>
struct Stats<T, D, Size> : public std::array<StatsEntry<T, D>, Size> { struct Stats<T, D, Size> : public std::array<StatsEntry<T, D>, Size> {};
T* get() { return this->at(0).get(); }
};
/// In stats table, D=0 means that the template parameter is not used /// In stats table, D=0 means that the template parameter is not used
enum StatsParams { NOT_USED = 0 }; enum StatsParams { NOT_USED = 0 };

View file

@ -290,7 +290,7 @@ void Thread::search() {
std::memset(ss-4, 0, 7 * sizeof(Stack)); std::memset(ss-4, 0, 7 * sizeof(Stack));
for (int i = 4; i > 0; i--) for (int i = 4; i > 0; i--)
(ss-i)->continuationHistory = this->continuationHistory[NO_PIECE][0].get(); // Use as sentinel (ss-i)->continuationHistory = &this->continuationHistory[NO_PIECE][0]; // Use as sentinel
bestValue = delta = alpha = -VALUE_INFINITE; bestValue = delta = alpha = -VALUE_INFINITE;
beta = VALUE_INFINITE; beta = VALUE_INFINITE;
@ -587,7 +587,7 @@ namespace {
(ss+1)->ply = ss->ply + 1; (ss+1)->ply = ss->ply + 1;
ss->currentMove = (ss+1)->excludedMove = bestMove = MOVE_NONE; ss->currentMove = (ss+1)->excludedMove = bestMove = MOVE_NONE;
ss->continuationHistory = thisThread->continuationHistory[NO_PIECE][0].get(); ss->continuationHistory = &thisThread->continuationHistory[NO_PIECE][0];
(ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE; (ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE;
Square prevSq = to_sq((ss-1)->currentMove); Square prevSq = to_sq((ss-1)->currentMove);
@ -751,7 +751,7 @@ namespace {
Depth R = ((823 + 67 * depth / ONE_PLY) / 256 + std::min((eval - beta) / PawnValueMg, 3)) * ONE_PLY; Depth R = ((823 + 67 * depth / ONE_PLY) / 256 + std::min((eval - beta) / PawnValueMg, 3)) * ONE_PLY;
ss->currentMove = MOVE_NULL; ss->currentMove = MOVE_NULL;
ss->continuationHistory = thisThread->continuationHistory[NO_PIECE][0].get(); ss->continuationHistory = &thisThread->continuationHistory[NO_PIECE][0];
pos.do_null_move(st); pos.do_null_move(st);
@ -802,7 +802,7 @@ namespace {
probCutCount++; probCutCount++;
ss->currentMove = move; ss->currentMove = move;
ss->continuationHistory = thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)].get(); ss->continuationHistory = &thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)];
assert(depth >= 5 * ONE_PLY); assert(depth >= 5 * ONE_PLY);
@ -970,7 +970,7 @@ moves_loop: // When in check, search starts from here
// Update the current move (this must be done after singular extension search) // Update the current move (this must be done after singular extension search)
ss->currentMove = move; ss->currentMove = move;
ss->continuationHistory = thisThread->continuationHistory[movedPiece][to_sq(move)].get(); ss->continuationHistory = &thisThread->continuationHistory[movedPiece][to_sq(move)];
// Step 15. Make the move // Step 15. Make the move
pos.do_move(move, st, givesCheck); pos.do_move(move, st, givesCheck);
@ -1213,7 +1213,7 @@ moves_loop: // When in check, search starts from here
Thread* thisThread = pos.this_thread(); Thread* thisThread = pos.this_thread();
(ss+1)->ply = ss->ply + 1; (ss+1)->ply = ss->ply + 1;
ss->currentMove = bestMove = MOVE_NONE; ss->currentMove = bestMove = MOVE_NONE;
ss->continuationHistory = thisThread->continuationHistory[NO_PIECE][0].get(); ss->continuationHistory = &thisThread->continuationHistory[NO_PIECE][0];
inCheck = pos.checkers(); inCheck = pos.checkers();
moveCount = 0; moveCount = 0;
@ -1348,7 +1348,7 @@ moves_loop: // When in check, search starts from here
} }
ss->currentMove = move; ss->currentMove = move;
ss->continuationHistory = thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)].get(); ss->continuationHistory = &thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)];
// Make and search the move // Make and search the move
pos.do_move(move, st, givesCheck); pos.do_move(move, st, givesCheck);

View file

@ -63,9 +63,9 @@ void Thread::clear() {
for (auto& to : continuationHistory) for (auto& to : continuationHistory)
for (auto& h : to) for (auto& h : to)
h.get()->fill(0); h->fill(0);
continuationHistory[NO_PIECE][0].get()->fill(Search::CounterMovePruneThreshold - 1); continuationHistory[NO_PIECE][0]->fill(Search::CounterMovePruneThreshold - 1);
} }
/// Thread::start_searching() wakes up the thread that will start the search /// Thread::start_searching() wakes up the thread that will start the search