Cinnamon  1.0
chess engine
Hash.cpp
Go to the documentation of this file.
00001 #include "Hash.h"
00002 
00003 Hash::Hash() {
00004     hash_array_greater[BLACK] = hash_array_greater[WHITE] = NULL;
00005     hash_array_always[BLACK] = hash_array_always[WHITE] = NULL;
00006     HASH_SIZE = 0;
00007 #ifdef DEBUG_MODE
00008     n_cut_hashA = n_cut_hashE = n_cut_hashB = cutFailed = probeHash = 0;
00009     n_cut_hash = nRecordHashA=nRecordHashB=nRecordHashE = collisions = 0;
00010 #endif
00011 #ifndef NO_HASH_MODE
00012     setHashSize(64);
00013 #endif
00014 }
00015 
00016 void Hash::clearAge() {
00017     for (int i = 0; i < HASH_SIZE; i++) {
00018         hash_array_greater[BLACK][i].entryAge = 0;
00019         hash_array_greater[WHITE][i].entryAge = 0;
00020     }
00021 }
00022 
00023 void Hash::clearHash() {
00024     if(!HASH_SIZE)return;
00025     memset(hash_array_greater[BLACK],0,sizeof(_Thash)*HASH_SIZE);
00026     memset(hash_array_greater[WHITE],0,sizeof(_Thash)*HASH_SIZE);
00027 }
00028 
00029 int Hash::getHashSize() {
00030     return HASH_SIZE/(1024 * 1000 / (sizeof(_Thash) * 4));
00031 }
00032 
00033 void Hash::setHashSize(int mb) {
00034     dispose();
00035     if (mb) {
00036         HASH_SIZE = mb * 1024 * 1000 / (sizeof(_Thash) * 4);
00037         hash_array_greater[BLACK] = (_Thash *) calloc(HASH_SIZE, sizeof(_Thash));
00038         hash_array_greater[WHITE] = (_Thash *) calloc(HASH_SIZE, sizeof(_Thash));
00039         assert(hash_array_greater[BLACK]);
00040         assert(hash_array_greater[WHITE]);
00041         hash_array_always[BLACK] = (_Thash *) calloc(HASH_SIZE, sizeof(_Thash));
00042         hash_array_always[WHITE] = (_Thash *) calloc(HASH_SIZE, sizeof(_Thash));
00043         assert(hash_array_always[BLACK]);
00044         assert(hash_array_always[WHITE]);
00045     }
00046 }
00047 
00048 void Hash::recordHash(bool running, _Thash* phashe_greater,_Thash* phashe_always,const char depth, const char flags, const u64 key, const int score, _Tmove* bestMove) {
00049 #ifndef NO_HASH_MODE
00050     ASSERT(key);
00051     if (!running)
00052         return;
00053     ASSERT(abs(score)<=32200);
00054     _Thash* phashe = phashe_greater;
00055     phashe->key = key;
00056     phashe->score = score;
00057     phashe->flags = flags;
00058     phashe->depth = depth;
00059 
00060     if (bestMove && bestMove->from != bestMove->to) {
00061         phashe->from = bestMove->from;
00062         phashe->to = bestMove->to;
00063     } else {
00064         phashe->from = phashe->to=0;
00065     }
00067     phashe = phashe_always;
00068     if (phashe->key && phashe->depth >= depth  && phashe->entryAge) {
00069         INC(collisions);
00070         return;
00071     }
00072 #ifdef DEBUG_MODE
00073     if(flags==hashfALPHA)
00074         nRecordHashA++;
00075     else if(flags==hashfBETA)
00076         nRecordHashB++;
00077     else
00078         nRecordHashE++;
00079 #endif
00080     phashe->key = key;
00081     phashe->score = score;
00082     phashe->flags = flags;
00083     phashe->depth = depth;
00084 
00085     phashe->entryAge = 1;
00086     if (bestMove && bestMove->from != bestMove->to) {
00087         phashe->from = bestMove->from;
00088         phashe->to = bestMove->to;
00089     } else {
00090         phashe->from = phashe->to=0;
00091     }
00092 #endif
00093 }
00094 
00095 void Hash::dispose() {
00096     if (hash_array_greater[BLACK])
00097         free(hash_array_greater[BLACK]);
00098     if (hash_array_greater[WHITE])
00099         free(hash_array_greater[WHITE]);
00100     if (hash_array_always[BLACK])
00101         free(hash_array_always[BLACK]);
00102     if (hash_array_always[WHITE])
00103         free(hash_array_always[WHITE]);
00104     hash_array_greater[BLACK] = hash_array_greater[WHITE] = NULL;
00105     hash_array_always[BLACK] = hash_array_always[WHITE] = NULL;
00106     HASH_SIZE = 0;
00107 }
00108 
00109 Hash::~Hash() {
00110     dispose();
00111 }
00112 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines