Guest User

void.h 1.48 by FrozenVoid

a guest
Aug 8th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.12 KB | None | 0 0
  1. /* VOID.H - Universal Header File.
  2. void.h ver1.48 for Digital Mars C by FrozenVoid original content licensed under UFIL1.02 http://pastebin.com/L3p9XK3T
  3. portions of file are licensed under other licenses(check source of content)
  4. @..\dmc  -gg -Nc -Jm  -o+all -o-dv %1.c -o%1.exe
  5. */
  6. /* Features:
  7. #include default C libraries.
  8. short syntax for type declarations [type][bytes]([p=restrict pointer]*Level)
  9. short syntax for main/exitmain
  10. short syntax for common keywords
  11. u8 rdtsc() return 64bit CPU cycle counter.
  12. u[num] ru[num]() returns num random bytes
  13. u8 rrange(u8 max) return random u8 number in range 0<>max
  14. u1* chrgen(resstr,size) return a random string of [size] bytes composed from random characters in resstr.
  15.  sas(expr,errmsg) static(compile time) assert wil fail if expr isn't true with errmsg
  16. u1* getfile(u1* filename,u8* size) copy file co
  17. ntent to pointer and set size to filesize.
  18. u8 putfile(u1* ptr,u8 size,u1* filename)  copy content to filename, return number of bytes written
  19. u4* hash(u1* text,u8 len) return 16byte MurmurHash3 of string, len is length of string/text;
  20. f10 ent(u1* str,u8 size)  return entropy of string(size of string)
  21. v0 prbin(s4 num) print binary value of int32
  22. u4 coutnwords(str,size) counts number of words in string
  23. u4 countlines(str,size) counts number of  lines in string
  24. various bithacks,murmurhash,etc
  25. u4 countnonspaceseq(str,size) counts number of character sequences which don't contains spaces/tabs and newlines(with or withotu feeds)
  26. u4 countcharseq(str,size,table) count number of character sequences which are 1's in the table.
  27. */
  28. #ifndef __DMC__
  29. #error "This header contains Digital Mars C-only code."
  30. #endif
  31. #pragma once
  32. //void.h default includes
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <assert.h>
  36. #include <errno.h>
  37. #include <ctype.h>
  38. #include <fenv.h>
  39. #include <float.h>
  40. #include <inttypes.h>
  41. #include <limits.h>
  42. #include <math.h>
  43. #include <setjmp.h>
  44. #include <signal.h>
  45. #include <stdarg.h>
  46. #include <stddef.h>
  47. #include <stdbool.h>
  48. #include <stdint.h>
  49. #include <time.h>
  50. #include <string.h>
  51. #define d #define
  52. // Typedefs
  53. d td typedef
  54. td uint8_t u1;
  55. td uint16_t u2;
  56. td uint32_t u4;
  57. td uint64_t u8;
  58. td int8_t s1;
  59. td int16_t s2;
  60. td int32_t s4;
  61. td int64_t s8;
  62. td float f4;
  63. td double f8;
  64. td long double f10;
  65. td void v0;
  66. //Pointers restrict+unsigned 3levels
  67. td restrict u1* u1p;
  68. td restrict u1** u1pp;
  69. td restrict u1*** u1ppp;
  70. td restrict u2* u2p;
  71. td restrict u2** u2pp;
  72. td restrict u2*** u2ppp;
  73. td restrict u4* u4p;
  74. td restrict u4** u4pp;
  75. td restrict u4*** u4ppp;
  76. td restrict u8* u8p;
  77. td restrict u8** u8pp;
  78. td restrict u8*** u8ppp;
  79. td restrict v0* v0p;
  80. td restrict v0** v0pp;
  81. td restrict v0*** v0ppp;
  82. //aliases
  83. d STDSTART s4 main(s4 argc,u1**argv){;
  84. d STDEND ;return 0;};
  85. d FASTSTART v0 main(v0){;
  86. d FASTEND ;};
  87. d pr printf
  88. d ma malloc
  89. d go goto
  90. d wh while
  91. d br break;
  92. d sw switch
  93. d el else
  94. d st struct
  95. d ts typedef struct
  96. d re return
  97. d sta static
  98. d reg register
  99. d con continue
  100. d iv inline void
  101. d ln(x) (_msize(x))//object size in malloced bytes
  102. // constants
  103. //Common functions
  104. //BITHACKS
  105. //set,clear,toggle,check bit of integer
  106. //http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/
  107. d setb(o,p) o|=(1<<p)      //byte| 1<< pos  
  108. d clrb(o,p) o&=(~(1<<p))  // byte | 11101111
  109. d togb(o,p) o^=(1<<p)
  110. d chkb(o,p) ((o>>p)&1)
  111. //Turnoff rightmost bit
  112. d offlast1(x) (x&(x-1))
  113. d onlast0(x) (x|(x+1))
  114. d last1(x)  (x&(-x))
  115. d last0(x) ((~x)&(x+1))
  116. //sign of integer
  117. d signof(x) (x>>((sizeof(x)*8)-1)) //-1/0
  118. d even(x) (!(x&1))
  119. d odd(x)  (x&1)
  120. //http://graphics.stanford.edu/~seander/bithacks.html
  121. //absolute values
  122. d abs(x) ((x^(x>>(sizeof(x)*8-1)))-(x>>(sizeof(x)*8-1)))
  123. //minimum and maximum
  124. d min(x,y)  (y ^ ((x ^ y) & -(x < y)))
  125. d max(x,y)  (x ^ ((x ^ y) & -(x < y)))
  126. //is power of 2
  127. d is2pow(x) (x&&(!(x & (x - 1))))
  128. //Conditionally set or clear bits without branching
  129. d scbits(x,bitmask,cond) (x&(~bitmask))|((-cond)&bitmask)
  130. //Conditional negation(if flag is true/1)
  131. d condneg(x,flag) ((x^(-flag))+flag)
  132. //merge bits from x,y from bitmask:1=ybit,0=xbit
  133. d mergebits(x,y,bitmask) (x^((x^y)&mask))
  134. // coutn bit in int32
  135. inline s4 counts4bits(s4 num){
  136. s4 v=num-((num>>1)&0x55555555);
  137. v=(v & 0x33333333) + ((v >> 2) & 0x33333333);
  138. re (((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);}
  139. inline s8 counts8bits(s8 num){//untested
  140. s4 v=num-((num>>1)&0x5555555555555555ULL);
  141. v=(v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
  142. re (((v + (v >> 4) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56);}
  143.  s4 inline s4parity(s4 num){
  144. s4 v=num;
  145. v^=(v>>16);
  146. v^=(v>>8);
  147. v^=(v>>4);
  148. v&=0xf;
  149. re ((0x6996 >> v) & 1);
  150. }
  151. s1 inline  s1parity(s1 num){
  152. s4 v=num;
  153. v^=(v>>4);
  154. v&=0xf;
  155. re ((0x6996 >> v) & 1);
  156. }
  157. //Random integers 1*,2*,4,8 bytes
  158. //http://en.wikipedia.org/wiki/Xorshift
  159. d DSEC 1 //0 low entropy/faster 1=highentropy
  160. inline u8 rdtsc(){__asm{RDTSC}} //timestamp
  161. uint32_t ru4(v0) {//xor128 RNG
  162.   static uint32_t x = 123456789;
  163.   static uint32_t y = 362436069;
  164.   static uint32_t z = 521288629;
  165.   static uint32_t w = 88675123;
  166.   uint32_t t;
  167.    t = x ^ (x << 11);
  168.   x = y; y = z; z = w;
  169. w = w ^ (w >> 19) ^ (t ^ (t >> 8));
  170.   return w;
  171. }
  172. u8 ru8(v0){
  173. u8 res;u4* h=(u4*)&res;
  174. h[0]=ru4();
  175. h[1]=ru4();
  176. re res;
  177. }
  178.  u1 ru1(v0){
  179. //select 1/4 of int32
  180. u4 r=ru4();
  181. u4 sh=(r&3)<<3;
  182. #ifdef DSEC
  183. re (u1)((ru4()&(0xFF<<(sh)))>>(sh));
  184. #else
  185. re (u1)((r&(0xFF<<(sh)))>>(sh));
  186. #endif
  187. }
  188.  u2 ru2(v0){
  189. u4 r=ru4();u4 sh=(r&1)<<4;
  190. #ifdef DSEC
  191. re (u2)((ru4()&(0xFFFF<<(sh)))>>(sh));
  192. #else
  193. re (u2)((r&(0xFFFF<<(sh)))>>(sh));
  194. #endif
  195. }
  196. /*
  197. random text: 0-x positon in string.
  198. */
  199. u8 rrange(u8 max){
  200. f10 div=((f10)ULLONG_MAX);
  201. u8 r=ru8();if(!r)r++;
  202. f10 d2=(((f10)r)/div)*((f10)max);//0..1 rnd number
  203. u8 res=(u8)d2;
  204. re res;
  205. }
  206. u1* chrgen(u1* str,u8 ressize){
  207. u8 strl=strlen(str);//"12" string index max
  208. u1* res=ma(ressize);u8 i=0;
  209. for(i=0;i<ressize;i++){res[i]=str[rrange(strl)];}
  210. re res;
  211. }
  212. v0 prbin(s4 num){//prints binary value of int32
  213. s4 i;
  214. for(i=31;i>-1;i--){putchar(chkb(num,i)+48);}
  215. }
  216. //return number of words and lines.
  217. const u1 alphanumeric[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  218. const u1 nonspaceseq[]={1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  219. u4 inline countwords(u1* str,u4 strsize){
  220. u4 reg count=0;u4 reg old=0;u4 reg state=0;
  221. u4 i;
  222. for(i=0;i<strsize;i++){
  223. state=alphanumeric[str[i]];
  224. count+=(old^state);old=state;}
  225. count>>=1;
  226. re count;}
  227. u4 inline countlines(u1* str,u4 strsize){
  228. u4 reg count=1;u4 i=strsize;
  229. wh(i--){count+=(!(str[i]^'\n'));};
  230. re count;
  231. }
  232. u4 inline countnonspaceseq(u1* str,u4 strsize){
  233. u4 reg count=0;u4 reg old=0;u4 reg state=0;
  234. ;u4 i;
  235. for(i=0;i<strsize;i++){
  236. state=nonspaceseq[str[i]];
  237. count+=(old^state);old=state;}
  238. count>>=1;
  239. re count;}
  240. u4 countcharseq(u1* str,u4 strsize,u1* table){
  241. u4 reg count=0;u4 reg old=0;u4 reg state=0;
  242. ;u4 i;
  243. for(i=0;i<strsize;i++){
  244. state=table[str[i]];
  245. count+=(old^state);old=state;}
  246. count>>=1;
  247. re count;
  248. }
  249.  
  250. //----------------------------------------------
  251. // ASCII   !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  252. #define epr(...) fprintf(stderr, __VA_ARGS__)
  253. //static assert
  254. #define sas(expr,errmsg) \
  255. int __static_assert(int errmsg##static_assert_failed[(expr)?1:-1])
  256. d getext(filename) strrchr(filename,'.')
  257. d hasdigits(str) strpbrk(str,"0123456789")
  258. d startswithdigits(str) (strcspn(str,"0123456789")==0)
  259. //Transfer file content into a pointer.
  260. u1* getfile(u1* filename,u8* size){
  261. FILE* in=fopen(filename,"rb");
  262. if(in==NULL){size[0]=0;perror("File access failed");return NULL;};
  263. fseek(in,0,SEEK_END);
  264. *size=(u8)ftell(in);rewind(in);
  265. u1* resfile=ma(size[0]);
  266. fread(resfile,size[0],1,in);
  267. fclose(in);
  268. re resfile;
  269. }
  270. //transfer pointer content into a file
  271. u8 putfile(u1* ptr,u8 size,u1* filename){
  272. FILE* out=fopen(filename,"wb");
  273. if(!out){perror("Cannot write to file");return 0;}
  274. u8 res=fwrite(ptr,size,1,out);fclose(out);
  275. re res;}
  276.  
  277. // Entropy calculation
  278. f10 ent(u1* str,u8 size){
  279. f10 entropy=0;f10 prob;f10 ml2=logl(2);
  280. u8 counts[256]={0};
  281. u8 iter=0;
  282. wh(iter<size){counts[str[iter++]]++;};iter=0;
  283. for(iter=0;iter<256;iter++){
  284. if(!counts[iter]){;con;}
  285. prob=1.0*((f10)counts[iter]/(f10)size);
  286. entropy-=prob*(logl(prob)/ml2);;}
  287. re entropy;
  288. }
  289. #undef d
  290. //-------------MurmurHash3
  291. //http://en.wikipedia.org/wiki/MurmurHash
  292. // Platform-specific functions and macros
  293. #ifdef __GNUC__
  294. #define FORCE_INLINE __attribute__((always_inline)) inline
  295. #else
  296. #define FORCE_INLINE
  297. #endif
  298. static inline FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r )
  299. {
  300.   return (x << r) | (x >> (32 - r));
  301. }
  302. static inline FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r )
  303. {
  304.   return (x << r) | (x >> (64 - r));
  305. }
  306. #define ROTL32(x,y) rotl32(x,y)
  307. #define ROTL64(x,y) rotl64(x,y)
  308. #define BIG_CONSTANT(x) (x##LLU)
  309. //-----------------------------------------------------------------------------
  310. // Block read - if your platform needs to do endian-swapping or can only
  311. // handle aligned reads, do the conversion here
  312. #define getblock(p, i) (p[i])
  313. //-----------------------------------------------------------------------------
  314. // Finalization mix - force all bits of a hash block to avalanche
  315. static inline FORCE_INLINE uint32_t fmix32 ( uint32_t h )
  316. {
  317.   h ^= h >> 16;
  318.   h *= 0x85ebca6b;
  319.   h ^= h >> 13;
  320.   h *= 0xc2b2ae35;
  321.   h ^= h >> 16;
  322.   return h;
  323. }
  324. //----------
  325. static inline FORCE_INLINE uint64_t fmix64 ( uint64_t k )
  326. {
  327.   k ^= k >> 33;
  328.   k *= BIG_CONSTANT(0xff51afd7ed558ccd);
  329.   k ^= k >> 33;
  330.   k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
  331.   k ^= k >> 33;
  332.   return k;
  333. }
  334. //-----------------------------------------------------------------------------
  335. void MurmurHash3_x86_32 ( const void * key, int len,
  336.                           uint32_t seed, void * out )
  337. {
  338.   const uint8_t * data = (const uint8_t*)key;
  339.   const int nblocks = len / 4;
  340.   int i;
  341.   uint32_t h1 = seed;
  342.   uint32_t c1 = 0xcc9e2d51;
  343.   uint32_t c2 = 0x1b873593;
  344.   //----------
  345.   // body
  346.   const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
  347.   for(i = -nblocks; i; i++)
  348.   {
  349.     uint32_t k1 = getblock(blocks,i);
  350.     k1 *= c1;
  351.     k1 = ROTL32(k1,15);
  352.     k1 *= c2;
  353.    
  354.     h1 ^= k1;
  355.     h1 = ROTL32(h1,13);
  356.     h1 = h1*5+0xe6546b64;
  357.   }
  358.   //----------
  359.   // tail
  360.   const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
  361.   uint32_t k1 = 0;
  362.   switch(len & 3)
  363.   {
  364.   case 3: k1 ^= tail[2] << 16;
  365.   case 2: k1 ^= tail[1] << 8;
  366.   case 1: k1 ^= tail[0];
  367.           k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  368.   };
  369.   //----------
  370.   // finalization
  371.   h1 ^= len;
  372.   h1 = fmix32(h1);
  373.   *(uint32_t*)out = h1;
  374. }
  375. //-----------------------------------------------------------------------------
  376. void MurmurHash3_x86_128 ( const void * key, const int len,
  377.                            uint32_t seed, void * out )
  378. {
  379.   const uint8_t * data = (const uint8_t*)key;
  380.   const int nblocks = len / 16;
  381.   int i;
  382.   uint32_t h1 = seed;
  383.   uint32_t h2 = seed;
  384.   uint32_t h3 = seed;
  385.   uint32_t h4 = seed;
  386.   uint32_t c1 = 0x239b961b;
  387.   uint32_t c2 = 0xab0e9789;
  388.   uint32_t c3 = 0x38b34ae5;
  389.   uint32_t c4 = 0xa1e38b93;
  390.   //----------
  391.   // body
  392.   const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
  393.   for(i = -nblocks; i; i++)
  394.   {
  395.     uint32_t k1 = getblock(blocks,i*4+0);
  396.     uint32_t k2 = getblock(blocks,i*4+1);
  397.     uint32_t k3 = getblock(blocks,i*4+2);
  398.     uint32_t k4 = getblock(blocks,i*4+3);
  399.     k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  400.     h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
  401.     k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
  402.     h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
  403.     k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
  404.     h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
  405.     k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
  406.     h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
  407.   }
  408.   //----------
  409.   // tail
  410.   const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
  411.   uint32_t k1 = 0;
  412.   uint32_t k2 = 0;
  413.   uint32_t k3 = 0;
  414.   uint32_t k4 = 0;
  415.   switch(len & 15)
  416.   {
  417.   case 15: k4 ^= tail[14] << 16;
  418.   case 14: k4 ^= tail[13] << 8;
  419.   case 13: k4 ^= tail[12] << 0;
  420.            k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
  421.   case 12: k3 ^= tail[11] << 24;
  422.   case 11: k3 ^= tail[10] << 16;
  423.   case 10: k3 ^= tail[ 9] << 8;
  424.   case 9: k3 ^= tail[ 8] << 0;
  425.            k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
  426.   case 8: k2 ^= tail[ 7] << 24;
  427.   case 7: k2 ^= tail[ 6] << 16;
  428.   case 6: k2 ^= tail[ 5] << 8;
  429.   case 5: k2 ^= tail[ 4] << 0;
  430.            k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
  431.   case 4: k1 ^= tail[ 3] << 24;
  432.   case 3: k1 ^= tail[ 2] << 16;
  433.   case 2: k1 ^= tail[ 1] << 8;
  434.   case 1: k1 ^= tail[ 0] << 0;
  435.            k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  436.   };
  437.   //----------
  438.   // finalization
  439.   h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
  440.   h1 += h2; h1 += h3; h1 += h4;
  441.   h2 += h1; h3 += h1; h4 += h1;
  442.   h1 = fmix32(h1);
  443.   h2 = fmix32(h2);
  444.   h3 = fmix32(h3);
  445.   h4 = fmix32(h4);
  446.   h1 += h2; h1 += h3; h1 += h4;
  447.   h2 += h1; h3 += h1; h4 += h1;
  448.   ((uint32_t*)out)[0] = h1;
  449.   ((uint32_t*)out)[1] = h2;
  450.   ((uint32_t*)out)[2] = h3;
  451.   ((uint32_t*)out)[3] = h4;
  452. }
  453. //-----------------------------------------------------------------------------
  454. void MurmurHash3_x64_128 ( const void * key, const int len,
  455.                            const uint32_t seed, void * out )
  456. {
  457.   const uint8_t * data = (const uint8_t*)key;
  458.   const int nblocks = len / 16;
  459.   int i;
  460.   uint64_t h1 = seed;
  461.   uint64_t h2 = seed;
  462.   uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
  463.   uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
  464.   //----------
  465.   // body
  466.   const uint64_t * blocks = (const uint64_t *)(data);
  467.   for(i = 0; i < nblocks; i++)
  468.   {
  469.     uint64_t k1 = getblock(blocks,i*2+0);
  470.     uint64_t k2 = getblock(blocks,i*2+1);
  471.     k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
  472.     h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
  473.     k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
  474.     h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
  475.   }
  476.   //----------
  477.   // tail
  478.   const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
  479.   uint64_t k1 = 0;
  480.   uint64_t k2 = 0;
  481.   switch(len & 15)
  482.   {
  483.   case 15: k2 ^= (uint64_t)(tail[14]) << 48;
  484.   case 14: k2 ^= (uint64_t)(tail[13]) << 40;
  485.   case 13: k2 ^= (uint64_t)(tail[12]) << 32;
  486.   case 12: k2 ^= (uint64_t)(tail[11]) << 24;
  487.   case 11: k2 ^= (uint64_t)(tail[10]) << 16;
  488.   case 10: k2 ^= (uint64_t)(tail[ 9]) << 8;
  489.   case 9: k2 ^= (uint64_t)(tail[ 8]) << 0;
  490.            k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
  491.   case 8: k1 ^= (uint64_t)(tail[ 7]) << 56;
  492.   case 7: k1 ^= (uint64_t)(tail[ 6]) << 48;
  493.   case 6: k1 ^= (uint64_t)(tail[ 5]) << 40;
  494.   case 5: k1 ^= (uint64_t)(tail[ 4]) << 32;
  495.   case 4: k1 ^= (uint64_t)(tail[ 3]) << 24;
  496.   case 3: k1 ^= (uint64_t)(tail[ 2]) << 16;
  497.   case 2: k1 ^= (uint64_t)(tail[ 1]) << 8;
  498.   case 1: k1 ^= (uint64_t)(tail[ 0]) << 0;
  499.            k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
  500.   };
  501.   //----------
  502.   // finalization
  503.   h1 ^= len; h2 ^= len;
  504.   h1 += h2;
  505.   h2 += h1;
  506.   h1 = fmix64(h1);
  507.   h2 = fmix64(h2);
  508.   h1 += h2;
  509.   h2 += h1;
  510.   ((uint64_t*)out)[0] = h1;
  511.   ((uint64_t*)out)[1] = h2;
  512. }
  513. //===============
  514. u4* hash(u1* text,u8 len){
  515. u4* res=ma(16);
  516. MurmurHash3_x86_128(text,len,0x12345678,res);
  517. re res;
  518. }
Advertisement
Add Comment
Please, Sign In to add comment