Skip to content
Browse files

Rename Materials and Pawns hash stuff

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
  • Loading branch information...
1 parent d84865e commit 304deb5e833baf47c147e93377f5c7ef582ab822 @mcostalba mcostalba committed Mar 31, 2012
Showing with 97 additions and 111 deletions.
  1. +8 −18 src/evaluate.cpp
  2. +13 −17 src/material.cpp
  3. +16 −16 src/material.h
  4. +23 −30 src/pawns.cpp
  5. +20 −19 src/pawns.h
  6. +3 −3 src/thread.h
  7. +8 −8 src/tt.h
  8. +6 −0 src/types.h
View
26 src/evaluate.cpp
@@ -36,8 +36,8 @@ namespace {
struct EvalInfo {
// Pointers to material and pawn hash table entries
- MaterialInfo* mi;
- PawnInfo* pi;
+ MaterialEntry* mi;
+ PawnEntry* pi;
// attackedBy[color][piece type] is a bitboard representing all squares
// attacked by a given color and piece type, attackedBy[color][0] contains
@@ -250,8 +250,7 @@ namespace {
Score evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei);
- inline Score apply_weight(Score v, Score weight);
- Value scale_by_game_phase(const Score& v, Phase ph, ScaleFactor sf);
+ Value interpolate(const Score& v, Phase ph, ScaleFactor sf);
Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
double to_cp(Value v);
void trace_add(int idx, Score term_w, Score term_b = SCORE_ZERO);
@@ -372,7 +371,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
margins[WHITE] = margins[BLACK] = VALUE_ZERO;
// Probe the material hash table
- ei.mi = Threads[pos.thread()].materialTable.material_info(pos);
+ ei.mi = Threads[pos.thread()].materialTable.probe(pos);
score += ei.mi->material_value();
// If we have a specialized evaluation function for the current material
@@ -384,7 +383,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
}
// Probe the pawn hash table
- ei.pi = Threads[pos.thread()].pawnTable.pawn_info(pos);
+ ei.pi = Threads[pos.thread()].pawnTable.probe(pos);
score += ei.pi->pawns_value();
// Initialize attack and king safety bitboards
@@ -446,9 +445,8 @@ Value do_evaluate(const Position& pos, Value& margin) {
sf = ScaleFactor(50);
}
- // Interpolate between the middle game and the endgame score
margin = margins[pos.side_to_move()];
- Value v = scale_by_game_phase(score, ei.mi->game_phase(), sf);
+ Value v = interpolate(score, ei.mi->game_phase(), sf);
// In case of tracing add all single evaluation contributions for both white and black
if (Trace)
@@ -1140,18 +1138,10 @@ Value do_evaluate(const Position& pos, Value& margin) {
}
- // apply_weight() applies an evaluation weight to a value trying to prevent overflow
-
- inline Score apply_weight(Score v, Score w) {
- return make_score((int(mg_value(v)) * mg_value(w)) / 0x100,
- (int(eg_value(v)) * eg_value(w)) / 0x100);
- }
-
-
- // scale_by_game_phase() interpolates between a middle game and an endgame score,
+ // interpolate() interpolates between a middle game and an endgame score,
// based on game phase. It also scales the return value by a ScaleFactor array.
- Value scale_by_game_phase(const Score& v, Phase ph, ScaleFactor sf) {
+ Value interpolate(const Score& v, Phase ph, ScaleFactor sf) {
assert(mg_value(v) > -VALUE_INFINITE && mg_value(v) < VALUE_INFINITE);
assert(eg_value(v) > -VALUE_INFINITE && eg_value(v) < VALUE_INFINITE);
View
30 src/material.cpp
@@ -84,30 +84,26 @@ namespace {
} // namespace
-/// MaterialInfoTable::material_info() takes a position object as input,
-/// computes or looks up a MaterialInfo object, and returns a pointer to it.
-/// If the material configuration is not already present in the table, it
-/// is stored there, so we don't have to recompute everything when the
-/// same material configuration occurs again.
+/// MaterialTable::probe() takes a position object as input, looks up a MaterialEntry
+/// object, and returns a pointer to it. If the material configuration is not
+/// already present in the table, it is computed and stored there, so we don't
+/// have to recompute everything when the same material configuration occurs again.
-MaterialInfo* MaterialInfoTable::material_info(const Position& pos) const {
+MaterialEntry* MaterialTable::probe(const Position& pos) const {
Key key = pos.material_key();
- MaterialInfo* mi = probe(key);
+ MaterialEntry* mi = Base::probe(key);
// If mi->key matches the position's material hash key, it means that we
// have analysed this material configuration before, and we can simply
// return the information we found the last time instead of recomputing it.
if (mi->key == key)
return mi;
- // Initialize MaterialInfo entry
- memset(mi, 0, sizeof(MaterialInfo));
+ memset(mi, 0, sizeof(MaterialEntry));
mi->key = key;
mi->factor[WHITE] = mi->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
-
- // Store game phase
- mi->gamePhase = MaterialInfoTable::game_phase(pos);
+ mi->gamePhase = MaterialTable::game_phase(pos);
// Let's look if we have a specialized evaluation function for this
// particular material configuration. First we look for a fixed
@@ -230,11 +226,11 @@ MaterialInfo* MaterialInfoTable::material_info(const Position& pos) const {
}
-/// MaterialInfoTable::imbalance() calculates imbalance comparing piece count of each
+/// MaterialTable::imbalance() calculates imbalance comparing piece count of each
/// piece type for both colors.
template<Color Us>
-int MaterialInfoTable::imbalance(const int pieceCount[][8]) {
+int MaterialTable::imbalance(const int pieceCount[][8]) {
const Color Them = (Us == WHITE ? BLACK : WHITE);
@@ -266,11 +262,11 @@ int MaterialInfoTable::imbalance(const int pieceCount[][8]) {
}
-/// MaterialInfoTable::game_phase() calculates the phase given the current
+/// MaterialTable::game_phase() calculates the phase given the current
/// position. Because the phase is strictly a function of the material, it
-/// is stored in MaterialInfo.
+/// is stored in MaterialEntry.
-Phase MaterialInfoTable::game_phase(const Position& pos) {
+Phase MaterialTable::game_phase(const Position& pos) {
Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);
View
32 src/material.h
@@ -34,7 +34,7 @@ enum Phase {
};
-/// MaterialInfo is a class which contains various information about a
+/// MaterialEntry is a class which contains various information about a
/// material configuration. It contains a material balance evaluation,
/// a function pointer to a special endgame evaluation function (which in
/// most cases is NULL, meaning that the standard evaluation function will
@@ -44,9 +44,9 @@ enum Phase {
/// For instance, in KRB vs KR endgames, the score is scaled down by a factor
/// of 4, which will result in scores of absolute value less than one pawn.
-class MaterialInfo {
+class MaterialEntry {
- friend class MaterialInfoTable;
+ friend class MaterialTable;
public:
Score material_value() const;
@@ -67,15 +67,15 @@ class MaterialInfo {
};
-/// The MaterialInfoTable class represents a pawn hash table. The most important
-/// method is material_info(), which returns a pointer to a MaterialInfo object.
+/// The MaterialTable class represents a material hash table. The most important
+/// method is probe(), which returns a pointer to a MaterialEntry object.
-class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> {
+class MaterialTable : public HashTable<MaterialEntry, MaterialTableSize> {
public:
- MaterialInfoTable() : funcs(new Endgames()) {}
- ~MaterialInfoTable() { delete funcs; }
+ MaterialTable() : funcs(new Endgames()) {}
+ ~MaterialTable() { delete funcs; }
- MaterialInfo* material_info(const Position& pos) const;
+ MaterialEntry* probe(const Position& pos) const;
static Phase game_phase(const Position& pos);
private:
@@ -86,14 +86,14 @@ class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> {
};
-/// MaterialInfo::scale_factor takes a position and a color as input, and
+/// MaterialEntry::scale_factor takes a position and a color as input, and
/// returns a scale factor for the given color. We have to provide the
/// position in addition to the color, because the scale factor need not
/// to be a constant: It can also be a function which should be applied to
/// the position. For instance, in KBP vs K endgames, a scaling function
/// which checks for draws with rook pawns and wrong-colored bishops.
-inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
+inline ScaleFactor MaterialEntry::scale_factor(const Position& pos, Color c) const {
if (!scalingFunction[c])
return ScaleFactor(factor[c]);
@@ -102,23 +102,23 @@ inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) cons
return sf == SCALE_FACTOR_NONE ? ScaleFactor(factor[c]) : sf;
}
-inline Value MaterialInfo::evaluate(const Position& pos) const {
+inline Value MaterialEntry::evaluate(const Position& pos) const {
return (*evaluationFunction)(pos);
}
-inline Score MaterialInfo::material_value() const {
+inline Score MaterialEntry::material_value() const {
return make_score(value, value);
}
-inline int MaterialInfo::space_weight() const {
+inline int MaterialEntry::space_weight() const {
return spaceWeight;
}
-inline Phase MaterialInfo::game_phase() const {
+inline Phase MaterialEntry::game_phase() const {
return gamePhase;
}
-inline bool MaterialInfo::specialized_eval_exists() const {
+inline bool MaterialEntry::specialized_eval_exists() const {
return evaluationFunction != NULL;
}
View
53 src/pawns.cpp
@@ -80,43 +80,35 @@ namespace {
#undef S
#undef V
-
- inline Score apply_weight(Score v, Score w) {
- return make_score((int(mg_value(v)) * mg_value(w)) / 0x100,
- (int(eg_value(v)) * eg_value(w)) / 0x100);
- }
}
-/// PawnInfoTable::pawn_info() takes a position object as input, computes
-/// a PawnInfo object, and returns a pointer to it. The result is also stored
-/// in an hash table, so we don't have to recompute everything when the same
-/// pawn structure occurs again.
+/// PawnTable::probe() takes a position object as input, computes a PawnEntry
+/// object, and returns a pointer to it. The result is also stored in a hash
+/// table, so we don't have to recompute everything when the same pawn structure
+/// occurs again.
-PawnInfo* PawnInfoTable::pawn_info(const Position& pos) const {
+PawnEntry* PawnTable::probe(const Position& pos) const {
Key key = pos.pawn_key();
- PawnInfo* pi = probe(key);
+ PawnEntry* pi = Base::probe(key);
// If pi->key matches the position's pawn hash key, it means that we
// have analysed this pawn structure before, and we can simply return
// the information we found the last time instead of recomputing it.
if (pi->key == key)
return pi;
- // Initialize PawnInfo entry
pi->key = key;
pi->passedPawns[WHITE] = pi->passedPawns[BLACK] = 0;
pi->kingSquares[WHITE] = pi->kingSquares[BLACK] = SQ_NONE;
pi->halfOpenFiles[WHITE] = pi->halfOpenFiles[BLACK] = 0xFF;
- // Calculate pawn attacks
Bitboard wPawns = pos.pieces(PAWN, WHITE);
Bitboard bPawns = pos.pieces(PAWN, BLACK);
- pi->pawnAttacks[WHITE] = ((wPawns << 9) & ~FileABB) | ((wPawns << 7) & ~FileHBB);
- pi->pawnAttacks[BLACK] = ((bPawns >> 7) & ~FileABB) | ((bPawns >> 9) & ~FileHBB);
+ pi->pawnAttacks[WHITE] = ((wPawns & ~FileHBB) << 9) | ((wPawns & ~FileABB) << 7);
+ pi->pawnAttacks[BLACK] = ((bPawns & ~FileHBB) >> 7) | ((bPawns & ~FileABB) >> 9);
- // Evaluate pawns for both colors and weight the result
pi->value = evaluate_pawns<WHITE>(pos, wPawns, bPawns, pi)
- evaluate_pawns<BLACK>(pos, bPawns, wPawns, pi);
@@ -126,11 +118,11 @@ PawnInfo* PawnInfoTable::pawn_info(const Position& pos) const {
}
-/// PawnInfoTable::evaluate_pawns() evaluates each pawn of the given color
+/// PawnTable::evaluate_pawns() evaluates each pawn of the given color
template<Color Us>
-Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
- Bitboard theirPawns, PawnInfo* pi) {
+Score PawnTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
+ Bitboard theirPawns, PawnEntry* pi) {
const Color Them = (Us == WHITE ? BLACK : WHITE);
@@ -158,11 +150,11 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
// Flag the pawn as passed, isolated, doubled or member of a pawn
// chain (but not the backward one).
- passed = !(theirPawns & passed_pawn_mask(Us, s));
+ chain = ourPawns & adjacent_files_bb(f) & b;
+ isolated = !(ourPawns & adjacent_files_bb(f));
doubled = ourPawns & squares_in_front_of(Us, s);
opposed = theirPawns & squares_in_front_of(Us, s);
- isolated = !(ourPawns & adjacent_files_bb(f));
- chain = ourPawns & adjacent_files_bb(f) & b;
+ passed = !(theirPawns & passed_pawn_mask(Us, s));
// Test for backward pawn
backward = false;
@@ -222,15 +214,16 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
if (candidate)
value += CandidateBonus[relative_rank(Us, s)];
}
+
return value;
}
-/// PawnInfo::shelter_storm() calculates shelter and storm penalties for the file
+/// PawnEntry::shelter_storm() calculates shelter and storm penalties for the file
/// the king is on, as well as the two adjacent files.
template<Color Us>
-Value PawnInfo::shelter_storm(const Position& pos, Square ksq) {
+Value PawnEntry::shelter_storm(const Position& pos, Square ksq) {
const Color Them = (Us == WHITE ? BLACK : WHITE);
@@ -260,16 +253,16 @@ Value PawnInfo::shelter_storm(const Position& pos, Square ksq) {
}
-/// PawnInfo::update_safety() calculates and caches a bonus for king safety. It is
+/// PawnEntry::update_safety() calculates and caches a bonus for king safety. It is
/// called only when king square changes, about 20% of total king_safety() calls.
template<Color Us>
-Score PawnInfo::update_safety(const Position& pos, Square ksq) {
+Score PawnEntry::update_safety(const Position& pos, Square ksq) {
kingSquares[Us] = ksq;
if (relative_rank(Us, ksq) > RANK_4)
- return kingShelters[Us] = SCORE_ZERO;
+ return kingSafety[Us] = SCORE_ZERO;
Value bonus = shelter_storm<Us>(pos, ksq);
@@ -280,9 +273,9 @@ Score PawnInfo::update_safety(const Position& pos, Square ksq) {
if (pos.can_castle(Us == WHITE ? WHITE_OOO : BLACK_OOO))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
- return kingShelters[Us] = make_score(bonus, 0);
+ return kingSafety[Us] = make_score(bonus, 0);
}
// Explicit template instantiation
-template Score PawnInfo::update_safety<WHITE>(const Position& pos, Square ksq);
-template Score PawnInfo::update_safety<BLACK>(const Position& pos, Square ksq);
+template Score PawnEntry::update_safety<WHITE>(const Position& pos, Square ksq);
+template Score PawnEntry::update_safety<BLACK>(const Position& pos, Square ksq);
View
39 src/pawns.h
@@ -26,16 +26,16 @@
const int PawnTableSize = 16384;
-/// PawnInfo is a class which contains various information about a pawn
+/// PawnEntry is a class which contains various information about a pawn
/// structure. Currently, it only includes a middle game and an end game
/// pawn structure evaluation, and a bitboard of passed pawns. We may want
/// to add further information in the future. A lookup to the pawn hash
-/// table (performed by calling the pawn_info method in a PawnInfoTable
-/// object) returns a pointer to a PawnInfo object.
+/// table (performed by calling the probe method in a PawnTable object)
+/// returns a pointer to a PawnEntry object.
-class PawnInfo {
+class PawnEntry {
- friend class PawnInfoTable;
+ friend class PawnTable;
public:
Score pawns_value() const;
@@ -61,50 +61,51 @@ class PawnInfo {
Square kingSquares[2];
Score value;
int halfOpenFiles[2];
- Score kingShelters[2];
+ Score kingSafety[2];
};
-/// The PawnInfoTable class represents a pawn hash table. The most important
-/// method is pawn_info, which returns a pointer to a PawnInfo object.
+/// The PawnTable class represents a pawn hash table. The most important
+/// method is probe, which returns a pointer to a PawnEntry object.
-class PawnInfoTable : public SimpleHash<PawnInfo, PawnTableSize> {
+class PawnTable : public HashTable<PawnEntry, PawnTableSize> {
public:
- PawnInfo* pawn_info(const Position& pos) const;
+ PawnEntry* probe(const Position& pos) const;
private:
template<Color Us>
- static Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi);
+ static Score evaluate_pawns(const Position& pos, Bitboard ourPawns,
+ Bitboard theirPawns, PawnEntry* pi);
};
-inline Score PawnInfo::pawns_value() const {
+inline Score PawnEntry::pawns_value() const {
return value;
}
-inline Bitboard PawnInfo::pawn_attacks(Color c) const {
+inline Bitboard PawnEntry::pawn_attacks(Color c) const {
return pawnAttacks[c];
}
-inline Bitboard PawnInfo::passed_pawns(Color c) const {
+inline Bitboard PawnEntry::passed_pawns(Color c) const {
return passedPawns[c];
}
-inline int PawnInfo::file_is_half_open(Color c, File f) const {
+inline int PawnEntry::file_is_half_open(Color c, File f) const {
return halfOpenFiles[c] & (1 << int(f));
}
-inline int PawnInfo::has_open_file_to_left(Color c, File f) const {
+inline int PawnEntry::has_open_file_to_left(Color c, File f) const {
return halfOpenFiles[c] & ((1 << int(f)) - 1);
}
-inline int PawnInfo::has_open_file_to_right(Color c, File f) const {
+inline int PawnEntry::has_open_file_to_right(Color c, File f) const {
return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
}
template<Color Us>
-inline Score PawnInfo::king_safety(const Position& pos, Square ksq) {
- return kingSquares[Us] == ksq ? kingShelters[Us] : update_safety<Us>(pos, ksq);
+inline Score PawnEntry::king_safety(const Position& pos, Square ksq) {
+ return kingSquares[Us] == ksq ? kingSafety[Us] : update_safety<Us>(pos, ksq);
}
#endif // !defined(PAWNS_H_INCLUDED)
View
6 src/thread.h
@@ -85,8 +85,8 @@ class Thread {
void wait_for_stop_or_ponderhit();
SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
- MaterialInfoTable materialTable;
- PawnInfoTable pawnTable;
+ MaterialTable materialTable;
+ PawnTable pawnTable;
int threadID;
int maxPly;
Lock sleepLock;
@@ -111,7 +111,7 @@ class ThreadsManager {
static storage duration are automatically set to zero before enter main()
*/
public:
- void init(); // No c'tor becuase Threads is static and we need stuff initialized
+ void init(); // No c'tor becuase Threads is static and we need engine initialized
~ThreadsManager();
Thread& operator[](int id) { return *threads[id]; }
View
16 src/tt.h
@@ -137,16 +137,16 @@ inline void TranspositionTable::refresh(const TTEntry* tte) const {
}
-/// A simple fixed size hash table used to store pawns and material
-/// configurations. It is basically just an array of Entry objects.
-/// Without cluster concept, overwrite policy nor resizing.
+/// A simple hash table used to store pawns and material configurations. It is
+/// basically just an array of Entry objects. Without cluster concept, overwrite
+/// policy nor resizing.
template<class Entry, int HashSize>
-struct SimpleHash {
+struct HashTable {
- typedef SimpleHash<Entry, HashSize> Base;
+ typedef HashTable<Entry, HashSize> Base;
- SimpleHash() {
+ HashTable() {
entries = new (std::nothrow) Entry[HashSize];
if (!entries)
@@ -158,12 +158,12 @@ struct SimpleHash {
memset(entries, 0, HashSize * sizeof(Entry));
}
- virtual ~SimpleHash() { delete [] entries; }
+ virtual ~HashTable() { delete [] entries; }
Entry* probe(Key key) const { return entries + ((uint32_t)key & (HashSize - 1)); }
void prefetch(Key key) const { ::prefetch((char*)probe(key)); }
-protected:
+private:
Entry* entries;
};
View
6 src/types.h
@@ -297,6 +297,12 @@ inline Score operator/(Score s, int i) {
return make_score(mg_value(s) / i, eg_value(s) / i);
}
+/// Weight score v by score w trying to prevent overflow
+inline Score apply_weight(Score v, Score w) {
+ return make_score((int(mg_value(v)) * mg_value(w)) / 0x100,
+ (int(eg_value(v)) * eg_value(w)) / 0x100);
+}
+
#undef ENABLE_OPERATORS_ON
#undef ENABLE_SAFE_OPERATORS_ON

0 comments on commit 304deb5

Please sign in to comment.
Something went wrong with that request. Please try again.