Types.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #pragma once
  2. #include "Platform.h"
  3. #include "Bitvec.h"
  4. #include <memory.h>
  5. #include <vector>
  6. #include <map>
  7. #include <set>
  8. //-----------------------------------------------------------------------------
  9. // If the optimizer detects that a value in a speed test is constant or unused,
  10. // the optimizer may remove references to it or otherwise create code that
  11. // would not occur in a real-world application. To prevent the optimizer from
  12. // doing this we declare two trivial functions that either sink or source data,
  13. // and bar the compiler from optimizing them.
  14. void blackhole ( uint32_t x );
  15. uint32_t whitehole ( void );
  16. //-----------------------------------------------------------------------------
  17. // We want to verify that every test produces the same result on every platform
  18. // To do this, we hash the results of every test to produce an overall
  19. // verification value for the whole test suite. If two runs produce the same
  20. // verification value, then every test in both run produced the same results
  21. extern uint32_t g_verify;
  22. // Mix the given blob of data into the verification code
  23. void MixVCode ( const void * blob, int len );
  24. //-----------------------------------------------------------------------------
  25. typedef void (*pfHash) ( const void * blob, const int len, const uint32_t seed, void * out );
  26. struct ByteVec : public std::vector<uint8_t>
  27. {
  28. ByteVec ( const void * key, int len )
  29. {
  30. resize(len);
  31. memcpy(&front(),key,len);
  32. }
  33. };
  34. template< typename hashtype, typename keytype >
  35. struct CollisionMap : public std::map< hashtype, std::vector<keytype> >
  36. {
  37. };
  38. template< typename hashtype >
  39. struct HashSet : public std::set<hashtype>
  40. {
  41. };
  42. //-----------------------------------------------------------------------------
  43. template < class T >
  44. class hashfunc
  45. {
  46. public:
  47. hashfunc ( pfHash h ) : m_hash(h)
  48. {
  49. }
  50. inline void operator () ( const void * key, const int len, const uint32_t seed, uint32_t * out )
  51. {
  52. m_hash(key,len,seed,out);
  53. }
  54. inline operator pfHash ( void ) const
  55. {
  56. return m_hash;
  57. }
  58. inline T operator () ( const void * key, const int len, const uint32_t seed )
  59. {
  60. T result;
  61. m_hash(key,len,seed,(uint32_t*)&result);
  62. return result;
  63. }
  64. pfHash m_hash;
  65. };
  66. //-----------------------------------------------------------------------------
  67. // Key-processing callback objects. Simplifies keyset testing a bit.
  68. struct KeyCallback
  69. {
  70. KeyCallback() : m_count(0)
  71. {
  72. }
  73. virtual ~KeyCallback()
  74. {
  75. }
  76. virtual void operator() ( const void * key, int len )
  77. {
  78. m_count++;
  79. }
  80. virtual void reserve ( int keycount )
  81. {
  82. };
  83. int m_count;
  84. };
  85. //----------
  86. template<typename hashtype>
  87. struct HashCallback : public KeyCallback
  88. {
  89. typedef std::vector<hashtype> hashvec;
  90. HashCallback ( pfHash hash, hashvec & hashes ) : m_hashes(hashes), m_pfHash(hash)
  91. {
  92. m_hashes.clear();
  93. }
  94. virtual void operator () ( const void * key, int len )
  95. {
  96. size_t newsize = m_hashes.size() + 1;
  97. m_hashes.resize(newsize);
  98. m_pfHash(key,len,0,&m_hashes.back());
  99. }
  100. virtual void reserve ( int keycount )
  101. {
  102. m_hashes.reserve(keycount);
  103. }
  104. hashvec & m_hashes;
  105. pfHash m_pfHash;
  106. //----------
  107. private:
  108. HashCallback & operator = ( const HashCallback & );
  109. };
  110. //----------
  111. template<typename hashtype>
  112. struct CollisionCallback : public KeyCallback
  113. {
  114. typedef HashSet<hashtype> hashset;
  115. typedef CollisionMap<hashtype,ByteVec> collmap;
  116. CollisionCallback ( pfHash hash, hashset & collisions, collmap & cmap )
  117. : m_pfHash(hash),
  118. m_collisions(collisions),
  119. m_collmap(cmap)
  120. {
  121. }
  122. virtual void operator () ( const void * key, int len )
  123. {
  124. hashtype h;
  125. m_pfHash(key,len,0,&h);
  126. if(m_collisions.count(h))
  127. {
  128. m_collmap[h].push_back( ByteVec(key,len) );
  129. }
  130. }
  131. //----------
  132. pfHash m_pfHash;
  133. hashset & m_collisions;
  134. collmap & m_collmap;
  135. private:
  136. CollisionCallback & operator = ( const CollisionCallback & c );
  137. };
  138. //-----------------------------------------------------------------------------
  139. template < int _bits >
  140. class Blob
  141. {
  142. public:
  143. Blob()
  144. {
  145. for(size_t i = 0; i < sizeof(bytes); i++)
  146. {
  147. bytes[i] = 0;
  148. }
  149. }
  150. Blob ( int x )
  151. {
  152. for(size_t i = 0; i < sizeof(bytes); i++)
  153. {
  154. bytes[i] = 0;
  155. }
  156. *(int*)bytes = x;
  157. }
  158. Blob ( const Blob & k )
  159. {
  160. for(size_t i = 0; i < sizeof(bytes); i++)
  161. {
  162. bytes[i] = k.bytes[i];
  163. }
  164. }
  165. Blob & operator = ( const Blob & k )
  166. {
  167. for(size_t i = 0; i < sizeof(bytes); i++)
  168. {
  169. bytes[i] = k.bytes[i];
  170. }
  171. return *this;
  172. }
  173. Blob ( uint64_t a, uint64_t b )
  174. {
  175. uint64_t t[2] = {a,b};
  176. set(&t,16);
  177. }
  178. void set ( const void * blob, size_t len )
  179. {
  180. const uint8_t * k = (const uint8_t*)blob;
  181. len = len > sizeof(bytes) ? sizeof(bytes) : len;
  182. for(size_t i = 0; i < len; i++)
  183. {
  184. bytes[i] = k[i];
  185. }
  186. for(size_t i = len; i < sizeof(bytes); i++)
  187. {
  188. bytes[i] = 0;
  189. }
  190. }
  191. uint8_t & operator [] ( int i )
  192. {
  193. return bytes[i];
  194. }
  195. const uint8_t & operator [] ( int i ) const
  196. {
  197. return bytes[i];
  198. }
  199. //----------
  200. // boolean operations
  201. bool operator < ( const Blob & k ) const
  202. {
  203. for(size_t i = 0; i < sizeof(bytes); i++)
  204. {
  205. if(bytes[i] < k.bytes[i]) return true;
  206. if(bytes[i] > k.bytes[i]) return false;
  207. }
  208. return false;
  209. }
  210. bool operator == ( const Blob & k ) const
  211. {
  212. for(size_t i = 0; i < sizeof(bytes); i++)
  213. {
  214. if(bytes[i] != k.bytes[i]) return false;
  215. }
  216. return true;
  217. }
  218. bool operator != ( const Blob & k ) const
  219. {
  220. return !(*this == k);
  221. }
  222. //----------
  223. // bitwise operations
  224. Blob operator ^ ( const Blob & k ) const
  225. {
  226. Blob t;
  227. for(size_t i = 0; i < sizeof(bytes); i++)
  228. {
  229. t.bytes[i] = bytes[i] ^ k.bytes[i];
  230. }
  231. return t;
  232. }
  233. Blob & operator ^= ( const Blob & k )
  234. {
  235. for(size_t i = 0; i < sizeof(bytes); i++)
  236. {
  237. bytes[i] ^= k.bytes[i];
  238. }
  239. return *this;
  240. }
  241. int operator & ( int x )
  242. {
  243. return (*(int*)bytes) & x;
  244. }
  245. Blob & operator &= ( const Blob & k )
  246. {
  247. for(size_t i = 0; i < sizeof(bytes); i++)
  248. {
  249. bytes[i] &= k.bytes[i];
  250. }
  251. }
  252. Blob operator << ( int c )
  253. {
  254. Blob t = *this;
  255. lshift(&t.bytes[0],sizeof(bytes),c);
  256. return t;
  257. }
  258. Blob operator >> ( int c )
  259. {
  260. Blob t = *this;
  261. rshift(&t.bytes[0],sizeof(bytes),c);
  262. return t;
  263. }
  264. Blob & operator <<= ( int c )
  265. {
  266. lshift(&bytes[0],sizeof(bytes),c);
  267. return *this;
  268. }
  269. Blob & operator >>= ( int c )
  270. {
  271. rshift(&bytes[0],sizeof(bytes),c);
  272. return *this;
  273. }
  274. //----------
  275. private:
  276. uint8_t bytes[(_bits+7)/8];
  277. };
  278. typedef Blob<128> uint128_t;
  279. typedef Blob<256> uint256_t;
  280. //-----------------------------------------------------------------------------