From: Marco Costalba Date: Wed, 1 May 2013 20:55:23 +0000 (+0200) Subject: Re-add "Cache line aligned TT" X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=481eda4ca0121cfa16f5a29f364ca30ee2852409;hp=e381951a248ba4c6873dd81527926c6bdcac2aa6 Re-add "Cache line aligned TT" But this time do not play with pointers, in particular do not assume that size_t is an unsigned type of the same width as pointers. This code should be fully portable. No functional change. --- diff --git a/src/misc.cpp b/src/misc.cpp index c0c00b00..477fb392 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -237,10 +237,8 @@ void prefetch(char* addr) { # if defined(__INTEL_COMPILER) || defined(_MSC_VER) _mm_prefetch(addr, _MM_HINT_T0); - _mm_prefetch(addr+64, _MM_HINT_T0); // 64 bytes ahead # else __builtin_prefetch(addr); - __builtin_prefetch(addr+64); # endif } diff --git a/src/tt.cpp b/src/tt.cpp index 80ea493b..271b98b7 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -40,16 +40,18 @@ void TranspositionTable::set_size(size_t mbSize) { return; hashMask = size - ClusterSize; - delete [] table; - table = new (std::nothrow) TTEntry[size]; + free(mem); + mem = malloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1); - if (!table) + if (!mem) { std::cerr << "Failed to allocate " << mbSize << "MB for transposition table." << std::endl; exit(EXIT_FAILURE); } + // Align table start address to a cache line + for (char* c = (char*)mem; unsigned(table = (TTEntry*)(c)) % CACHE_LINE_SIZE; c++) {} clear(); // Operator new is not guaranteed to initialize memory to zero } diff --git a/src/tt.h b/src/tt.h index c7c39e90..1caa277e 100644 --- a/src/tt.h +++ b/src/tt.h @@ -85,7 +85,7 @@ class TranspositionTable { static const unsigned ClusterSize = 4; // A cluster is 64 Bytes public: - ~TranspositionTable() { delete [] table; } + ~TranspositionTable() { free(mem); } void new_search() { generation++; } TTEntry* probe(const Key key) const; @@ -98,6 +98,7 @@ public: private: uint32_t hashMask; TTEntry* table; + void* mem; uint8_t generation; // Size must be not bigger then TTEntry::generation8 }; diff --git a/src/types.h b/src/types.h index 1def7d00..dae86db3 100644 --- a/src/types.h +++ b/src/types.h @@ -56,10 +56,11 @@ # include // Intel and Microsoft header for _mm_prefetch() # endif +#define CACHE_LINE_SIZE 64 #if defined(_MSC_VER) || defined(__INTEL_COMPILER) -# define CACHE_LINE_ALIGNMENT __declspec(align(64)) +# define CACHE_LINE_ALIGNMENT __declspec(align(CACHE_LINE_SIZE)) #else -# define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(64))) +# define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(CACHE_LINE_SIZE))) #endif #if defined(_MSC_VER)