generics.hashes.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. {
  2. This file is part of the Free Pascal/NewPascal run time library.
  3. Copyright (c) 2014 by Maciej Izak (hnb)
  4. member of the NewPascal development team (http://newpascal.org)
  5. Copyright(c) 2004-2018 DaThoX
  6. It contains the generics collections library
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. Acknowledgment
  13. Thanks to Sphere 10 Software (http://sphere10.com) for sponsoring
  14. many new types and major refactoring of entire library.
  15. Thanks to mORMot (http://synopse.info) project for the best implementations
  16. of hashing functions like crc32c and xxHash32 :)
  17. **********************************************************************}
  18. unit Generics.Hashes;
  19. {$MODE DELPHI}{$H+}
  20. {$POINTERMATH ON}
  21. {$MACRO ON}
  22. {$COPERATORS ON}
  23. {$OVERFLOWCHECKS OFF}
  24. {$RANGECHECKS OFF}
  25. interface
  26. uses
  27. Classes, SysUtils;
  28. { Warning: the following set of macro code
  29. that decides to use assembler or normal code
  30. needs to stay after the _INTERFACE keyword
  31. because FPC_PIC macro is only set after this keyword,
  32. as it can be modified before by the global $PIC preprocessor directive.
  33. Pierre Muller 2018/07/04 }
  34. {$ifdef FPC_PIC}
  35. {$define DISABLE_X86_CPUINTEL}
  36. {$endif FPC_PIC}
  37. {$if defined(OPENBSD) or defined(EMX) or defined(OS2)}
  38. { These targets have old GNU assemblers that }
  39. { do not support all instructions used in assembler code below }
  40. {$define DISABLE_X86_CPUINTEL}
  41. {$endif}
  42. {$ifdef CPU64}
  43. {$define PUREPASCAL}
  44. {$ifdef CPUX64}
  45. {$define CPUINTEL}
  46. {$ASMMODE INTEL}
  47. {$endif CPUX64}
  48. {$else}
  49. {$ifdef CPUX86}
  50. {$ifndef DISABLE_X86_CPUINTEL}
  51. {$define CPUINTEL}
  52. {$ASMMODE INTEL}
  53. {$else}
  54. { Assembler code uses references to static
  55. variables with are not PIC ready }
  56. {$define PUREPASCAL}
  57. {$endif}
  58. {$else CPUX86}
  59. {$define PUREPASCAL}
  60. {$endif}
  61. {$endif CPU64}
  62. // Original version of Bob Jenkins Hash
  63. // http://burtleburtle.net/bob/c/lookup3.c
  64. function HashWord(
  65. AKey: PLongWord; //* the key, an array of uint32_t values */
  66. ALength: SizeInt; //* the length of the key, in uint32_ts */
  67. AInitVal: UInt32): UInt32; //* the previous hash, or an arbitrary value */
  68. procedure HashWord2 (
  69. AKey: PLongWord; //* the key, an array of uint32_t values */
  70. ALength: SizeInt; //* the length of the key, in uint32_ts */
  71. var APrimaryHashAndInitVal: UInt32; //* IN: seed OUT: primary hash value */
  72. var ASecondaryHashAndInitVal: UInt32); //* IN: more seed OUT: secondary hash value */
  73. function HashLittle(AKey: Pointer; ALength: SizeInt; AInitVal: UInt32): UInt32;
  74. procedure HashLittle2(
  75. AKey: Pointer; //* the key to hash */
  76. ALength: SizeInt; //* length of the key */
  77. var APrimaryHashAndInitVal: UInt32; //* IN: primary initval, OUT: primary hash */
  78. var ASecondaryHashAndInitVal: UInt32); //* IN: secondary initval, OUT: secondary hash */
  79. function DelphiHashLittle(AKey: Pointer; ALength: SizeInt; AInitVal: UInt32): Int32;
  80. procedure DelphiHashLittle2(AKey: Pointer; ALength: SizeInt; var APrimaryHashAndInitVal, ASecondaryHashAndInitVal: UInt32);
  81. // hash function from fstl
  82. function SimpleChecksumHash(AKey: Pointer; ALength: SizeInt): UInt32;
  83. // some other hashes
  84. // http://stackoverflow.com/questions/14409466/simple-hash-functions
  85. // http://www.partow.net/programming/hashfunctions/
  86. // http://en.wikipedia.org/wiki/List_of_hash_functions
  87. // http://www.cse.yorku.ca/~oz/hash.html
  88. // https://code.google.com/p/hedgewars/source/browse/hedgewars/adler32.pas
  89. function Adler32(AKey: Pointer; ALength: SizeInt): UInt32;
  90. function sdbm(AKey: Pointer; ALength: SizeInt): UInt32;
  91. function xxHash32(crc: cardinal; P: Pointer; len: integer): cardinal;{$IFNDEF CPUINTEL}inline;{$ENDIF}
  92. // pure pascal implementation of xxHash32
  93. function xxHash32Pascal(crc: cardinal; P: Pointer; len: integer): cardinal;
  94. type
  95. THasher = function(crc: cardinal; buf: Pointer; len: cardinal): cardinal;
  96. var
  97. crc32c: THasher;
  98. mORMotHasher: THasher;
  99. implementation
  100. function SimpleChecksumHash(AKey: Pointer; ALength: SizeInt): UInt32;
  101. var
  102. i: Integer;
  103. ABuffer: PUInt8 absolute AKey;
  104. begin
  105. Result := 0;
  106. for i := 0 to ALength - 1 do
  107. Inc(Result,ABuffer[i]);
  108. end;
  109. function Adler32(AKey: Pointer; ALength: SizeInt): UInt32;
  110. const
  111. MOD_ADLER = 65521;
  112. var
  113. ABuffer: PUInt8 absolute AKey;
  114. a: UInt32 = 1;
  115. b: UInt32 = 0;
  116. n: Integer;
  117. begin
  118. for n := 0 to ALength -1 do
  119. begin
  120. a := (a + ABuffer[n]) mod MOD_ADLER;
  121. b := (b + a) mod MOD_ADLER;
  122. end;
  123. Result := (b shl 16) or a;
  124. end;
  125. function sdbm(AKey: Pointer; ALength: SizeInt): UInt32;
  126. var
  127. c: PUInt8 absolute AKey;
  128. i: Integer;
  129. begin
  130. Result := 0;
  131. c := AKey;
  132. for i := 0 to ALength - 1 do
  133. begin
  134. Result := c^ + (Result shl 6) + (Result shl 16) {%H-}- Result;
  135. Inc(c);
  136. end;
  137. end;
  138. { BobJenkinsHash }
  139. {$define mix_abc :=
  140. a -= c; a := a xor (((c)shl(4)) or ((c)shr(32-(4)))); c += b;
  141. b -= a; b := b xor (((a)shl(6)) or ((a)shr(32-(6)))); a += c;
  142. c -= b; c := c xor (((b)shl(8)) or ((b)shr(32-(8)))); b += a;
  143. a -= c; a := a xor (((c)shl(16)) or ((c)shr(32-(16)))); c += b;
  144. b -= a; b := b xor (((a)shl(19)) or ((a)shr(32-(19)))); a += c;
  145. c -= b; c := c xor (((b)shl(4)) or ((b)shr(32-(4)))); b += a
  146. }
  147. {$define final_abc :=
  148. c := c xor b; c -= (((b)shl(14)) or ((b)shr(32-(14))));
  149. a := a xor c; a -= (((c)shl(11)) or ((c)shr(32-(11))));
  150. b := b xor a; b -= (((a)shl(25)) or ((a)shr(32-(25))));
  151. c := c xor b; c -= (((b)shl(16)) or ((b)shr(32-(16))));
  152. a := a xor c; a -= (((c)shl(4)) or ((c)shr(32-(4))));
  153. b := b xor a; b -= (((a)shl(14)) or ((a)shr(32-(14))));
  154. c := c xor b; c -= (((b)shl(24)) or ((b)shr(32-(24))))
  155. }
  156. function HashWord(
  157. AKey: PLongWord; //* the key, an array of uint32_t values */
  158. ALength: SizeInt; //* the length of the key, in uint32_ts */
  159. AInitVal: UInt32): UInt32; //* the previous hash, or an arbitrary value */
  160. var
  161. a,b,c: UInt32;
  162. label
  163. Case0, Case1, Case2, Case3;
  164. begin
  165. //* Set up the internal state */
  166. a := $DEADBEEF + (UInt32(ALength) shl 2) + AInitVal;
  167. b := a;
  168. c := b;
  169. //*------------------------------------------------- handle most of the key */
  170. while ALength > 3 do
  171. begin
  172. a += AKey[0];
  173. b += AKey[1];
  174. c += AKey[2];
  175. mix_abc;
  176. ALength -= 3;
  177. AKey += 3;
  178. end;
  179. //*------------------------------------------- handle the last 3 uint32_t's */
  180. case ALength of //* all the case statements fall through */
  181. 3: goto Case3;
  182. 2: goto Case2;
  183. 1: goto Case1;
  184. 0: goto Case0;
  185. end;
  186. Case3: c+=AKey[2];
  187. Case2: b+=AKey[1];
  188. Case1: a+=AKey[0];
  189. final_abc;
  190. Case0: //* case 0: nothing left to add */
  191. //*------------------------------------------------------ report the result */
  192. Result := c;
  193. end;
  194. procedure HashWord2 (
  195. AKey: PLongWord; //* the key, an array of uint32_t values */
  196. ALength: SizeInt; //* the length of the key, in uint32_ts */
  197. var APrimaryHashAndInitVal: UInt32; //* IN: seed OUT: primary hash value */
  198. var ASecondaryHashAndInitVal: UInt32); //* IN: more seed OUT: secondary hash value */
  199. var
  200. a,b,c: UInt32;
  201. label
  202. Case0, Case1, Case2, Case3;
  203. begin
  204. //* Set up the internal state */
  205. a := $deadbeef + (UInt32(ALength shl 2)) + APrimaryHashAndInitVal;
  206. b := a;
  207. c := b;
  208. c += ASecondaryHashAndInitVal;
  209. //*------------------------------------------------- handle most of the key */
  210. while ALength > 3 do
  211. begin
  212. a += AKey[0];
  213. b += AKey[1];
  214. c += AKey[2];
  215. mix_abc;
  216. ALength -= 3;
  217. AKey += 3;
  218. end;
  219. //*------------------------------------------- handle the last 3 uint32_t's */
  220. case ALength of //* all the case statements fall through */
  221. 3: goto Case3;
  222. 2: goto Case2;
  223. 1: goto Case1;
  224. 0: goto Case0;
  225. end;
  226. Case3: c+=AKey[2];
  227. Case2: b+=AKey[1];
  228. Case1: a+=AKey[0];
  229. final_abc;
  230. Case0: //* case 0: nothing left to add */
  231. //*------------------------------------------------------ report the result */
  232. APrimaryHashAndInitVal := c;
  233. ASecondaryHashAndInitVal := b;
  234. end;
  235. function HashLittle(AKey: Pointer; ALength: SizeInt; AInitVal: UInt32): UInt32;
  236. var
  237. a, b, c: UInt32;
  238. u: record case byte of
  239. 0: (ptr: Pointer);
  240. 1: (i: PtrUint);
  241. end absolute AKey;
  242. k32: ^UInt32 absolute AKey;
  243. k16: ^UInt16 absolute AKey;
  244. k8: ^UInt8 absolute AKey;
  245. label _10, _8, _6, _4, _2;
  246. label Case12, Case11, Case10, Case9, Case8, Case7, Case6, Case5, Case4, Case3, Case2, Case1;
  247. begin
  248. a := $DEADBEEF + UInt32(ALength) + AInitVal;
  249. b := a;
  250. c := b;
  251. {$IFDEF ENDIAN_LITTLE}
  252. if (u.i and $3) = 0 then
  253. begin
  254. while (ALength > 12) do
  255. begin
  256. a += k32[0];
  257. b += k32[1];
  258. c += k32[2];
  259. mix_abc;
  260. ALength -= 12;
  261. k32 += 3;
  262. end;
  263. case ALength of
  264. 12: begin c += k32[2]; b += k32[1]; a += k32[0]; end;
  265. 11: begin c += k32[2] and $ffffff; b += k32[1]; a += k32[0]; end;
  266. 10: begin c += k32[2] and $ffff; b += k32[1]; a += k32[0]; end;
  267. 9 : begin c += k32[2] and $ff; b += k32[1]; a += k32[0]; end;
  268. 8 : begin b += k32[1]; a += k32[0]; end;
  269. 7 : begin b += k32[1] and $ffffff; a += k32[0]; end;
  270. 6 : begin b += k32[1] and $ffff; a += k32[0]; end;
  271. 5 : begin b += k32[1] and $ff; a += k32[0]; end;
  272. 4 : begin a += k32[0]; end;
  273. 3 : begin a += k32[0] and $ffffff; end;
  274. 2 : begin a += k32[0] and $ffff; end;
  275. 1 : begin a += k32[0] and $ff; end;
  276. 0 : Exit(c); // zero length strings require no mixing
  277. end
  278. end
  279. else
  280. if (u.i and $1) = 0 then
  281. begin
  282. while (ALength > 12) do
  283. begin
  284. a += k16[0] + (UInt32(k16[1]) shl 16);
  285. b += k16[2] + (UInt32(k16[3]) shl 16);
  286. c += k16[4] + (UInt32(k16[5]) shl 16);
  287. mix_abc;
  288. ALength -= 12;
  289. k16 += 6;
  290. end;
  291. case ALength of
  292. 12:
  293. begin
  294. c+=k16[4]+((UInt32(k16[5])) shl 16);
  295. b+=k16[2]+((UInt32(k16[3])) shl 16);
  296. a+=k16[0]+((UInt32(k16[1])) shl 16);
  297. end;
  298. 11:
  299. begin
  300. c+=(UInt32(k8[10])) shl 16; //* fall through */
  301. goto _10;
  302. end;
  303. 10:
  304. begin _10:
  305. c+=k16[4];
  306. b+=k16[2]+((UInt32(k16[3])) shl 16);
  307. a+=k16[0]+((UInt32(k16[1])) shl 16);
  308. end;
  309. 9 :
  310. begin
  311. c+=k8[8]; //* fall through */
  312. goto _8;
  313. end;
  314. 8 :
  315. begin _8:
  316. b+=k16[2]+((UInt32(k16[3])) shl 16);
  317. a+=k16[0]+((UInt32(k16[1])) shl 16);
  318. end;
  319. 7 :
  320. begin
  321. b+=(UInt32(k8[6])) shl 16; //* fall through */
  322. goto _6;
  323. end;
  324. 6 :
  325. begin _6:
  326. b+=k16[2];
  327. a+=k16[0]+((UInt32(k16[1])) shl 16);
  328. end;
  329. 5 :
  330. begin
  331. b+=k8[4]; //* fall through */
  332. goto _4;
  333. end;
  334. 4 :
  335. begin _4:
  336. a+=k16[0]+((UInt32(k16[1])) shl 16);
  337. end;
  338. 3 :
  339. begin
  340. a+=(UInt32(k8[2])) shl 16; //* fall through */
  341. goto _2;
  342. end;
  343. 2 :
  344. begin _2:
  345. a+=k16[0];
  346. end;
  347. 1 :
  348. begin
  349. a+=k8[0];
  350. end;
  351. 0 : Exit(c); //* zero length requires no mixing */
  352. end;
  353. end
  354. else
  355. {$ENDIF}
  356. begin
  357. while ALength > 12 do
  358. begin
  359. a += k8[0];
  360. a += (UInt32(k8[1])) shl 8;
  361. a += (UInt32(k8[2])) shl 16;
  362. a += (UInt32(k8[3])) shl 24;
  363. b += k8[4];
  364. b += (UInt32(k8[5])) shl 8;
  365. b += (UInt32(k8[6])) shl 16;
  366. b += (UInt32(k8[7])) shl 24;
  367. c += k8[8];
  368. c += (UInt32(k8[9])) shl 8;
  369. c += (UInt32(k8[10])) shl 16;
  370. c += (UInt32(k8[11])) shl 24;
  371. mix_abc;
  372. ALength -= 12;
  373. k8 += 12;
  374. end;
  375. case ALength of
  376. 12: goto Case12;
  377. 11: goto Case11;
  378. 10: goto Case10;
  379. 9 : goto Case9;
  380. 8 : goto Case8;
  381. 7 : goto Case7;
  382. 6 : goto Case6;
  383. 5 : goto Case5;
  384. 4 : goto Case4;
  385. 3 : goto Case3;
  386. 2 : goto Case2;
  387. 1 : goto Case1;
  388. 0 : Exit(c);
  389. end;
  390. Case12: c+=(UInt32(k8[11])) shl 24;
  391. Case11: c+=(UInt32(k8[10])) shl 16;
  392. Case10: c+=(UInt32(k8[9])) shl 8;
  393. Case9: c+=k8[8];
  394. Case8: b+=(UInt32(k8[7])) shl 24;
  395. Case7: b+=(UInt32(k8[6])) shl 16;
  396. Case6: b+=(UInt32(k8[5])) shl 8;
  397. Case5: b+=k8[4];
  398. Case4: a+=(UInt32(k8[3])) shl 24;
  399. Case3: a+=(UInt32(k8[2])) shl 16;
  400. Case2: a+=(UInt32(k8[1])) shl 8;
  401. Case1: a+=k8[0];
  402. end;
  403. final_abc;
  404. Result := c;
  405. end;
  406. (*
  407. * hashlittle2: return 2 32-bit hash values
  408. *
  409. * This is identical to hashlittle(), except it returns two 32-bit hash
  410. * values instead of just one. This is good enough for hash table
  411. * lookup with 2^^64 buckets, or if you want a second hash if you're not
  412. * happy with the first, or if you want a probably-unique 64-bit ID for
  413. * the key. *pc is better mixed than *pb, so use *pc first. If you want
  414. * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
  415. *)
  416. procedure HashLittle2(
  417. AKey: Pointer; //* the key to hash */
  418. ALength: SizeInt; //* length of the key */
  419. var APrimaryHashAndInitVal: UInt32; //* IN: primary initval, OUT: primary hash */
  420. var ASecondaryHashAndInitVal: UInt32); //* IN: secondary initval, OUT: secondary hash */
  421. var
  422. a,b,c: UInt32;
  423. u: record case byte of
  424. 0: (ptr: Pointer);
  425. 1: (i: PtrUint);
  426. end absolute AKey;
  427. k32: ^UInt32 absolute AKey;
  428. k16: ^UInt16 absolute AKey;
  429. k8: ^UInt8 absolute AKey;
  430. label _10, _8, _6, _4, _2;
  431. label Case12, Case11, Case10, Case9, Case8, Case7, Case6, Case5, Case4, Case3, Case2, Case1;
  432. begin
  433. //* Set up the internal state */
  434. a := $DEADBEEF + UInt32(ALength) + APrimaryHashAndInitVal;
  435. b := a;
  436. c := b;
  437. c += ASecondaryHashAndInitVal;
  438. {$IFDEF ENDIAN_LITTLE}
  439. if (u.i and $3) = 0 then
  440. begin
  441. while (ALength > 12) do
  442. begin
  443. a += k32[0];
  444. b += k32[1];
  445. c += k32[2];
  446. mix_abc;
  447. ALength -= 12;
  448. k32 += 3;
  449. end;
  450. case ALength of
  451. 12: begin c += k32[2]; b += k32[1]; a += k32[0]; end;
  452. 11: begin c += k32[2] and $ffffff; b += k32[1]; a += k32[0]; end;
  453. 10: begin c += k32[2] and $ffff; b += k32[1]; a += k32[0]; end;
  454. 9 : begin c += k32[2] and $ff; b += k32[1]; a += k32[0]; end;
  455. 8 : begin b += k32[1]; a += k32[0]; end;
  456. 7 : begin b += k32[1] and $ffffff; a += k32[0]; end;
  457. 6 : begin b += k32[1] and $ffff; a += k32[0]; end;
  458. 5 : begin b += k32[1] and $ff; a += k32[0]; end;
  459. 4 : begin a += k32[0]; end;
  460. 3 : begin a += k32[0] and $ffffff; end;
  461. 2 : begin a += k32[0] and $ffff; end;
  462. 1 : begin a += k32[0] and $ff; end;
  463. 0 :
  464. begin
  465. APrimaryHashAndInitVal := c;
  466. ASecondaryHashAndInitVal := b;
  467. Exit; // zero length strings require no mixing
  468. end;
  469. end
  470. end
  471. else
  472. if (u.i and $1) = 0 then
  473. begin
  474. while (ALength > 12) do
  475. begin
  476. a += k16[0] + (UInt32(k16[1]) shl 16);
  477. b += k16[2] + (UInt32(k16[3]) shl 16);
  478. c += k16[4] + (UInt32(k16[5]) shl 16);
  479. mix_abc;
  480. ALength -= 12;
  481. k16 += 6;
  482. end;
  483. case ALength of
  484. 12:
  485. begin
  486. c+=k16[4]+((UInt32(k16[5])) shl 16);
  487. b+=k16[2]+((UInt32(k16[3])) shl 16);
  488. a+=k16[0]+((UInt32(k16[1])) shl 16);
  489. end;
  490. 11:
  491. begin
  492. c+=(UInt32(k8[10])) shl 16; //* fall through */
  493. goto _10;
  494. end;
  495. 10:
  496. begin _10:
  497. c+=k16[4];
  498. b+=k16[2]+((UInt32(k16[3])) shl 16);
  499. a+=k16[0]+((UInt32(k16[1])) shl 16);
  500. end;
  501. 9 :
  502. begin
  503. c+=k8[8]; //* fall through */
  504. goto _8;
  505. end;
  506. 8 :
  507. begin _8:
  508. b+=k16[2]+((UInt32(k16[3])) shl 16);
  509. a+=k16[0]+((UInt32(k16[1])) shl 16);
  510. end;
  511. 7 :
  512. begin
  513. b+=(UInt32(k8[6])) shl 16; //* fall through */
  514. goto _6;
  515. end;
  516. 6 :
  517. begin _6:
  518. b+=k16[2];
  519. a+=k16[0]+((UInt32(k16[1])) shl 16);
  520. end;
  521. 5 :
  522. begin
  523. b+=k8[4]; //* fall through */
  524. goto _4;
  525. end;
  526. 4 :
  527. begin _4:
  528. a+=k16[0]+((UInt32(k16[1])) shl 16);
  529. end;
  530. 3 :
  531. begin
  532. a+=(UInt32(k8[2])) shl 16; //* fall through */
  533. goto _2;
  534. end;
  535. 2 :
  536. begin _2:
  537. a+=k16[0];
  538. end;
  539. 1 :
  540. begin
  541. a+=k8[0];
  542. end;
  543. 0 :
  544. begin
  545. APrimaryHashAndInitVal := c;
  546. ASecondaryHashAndInitVal := b;
  547. Exit; // zero length strings require no mixing
  548. end;
  549. end;
  550. end
  551. else
  552. {$ENDIF}
  553. begin
  554. while ALength > 12 do
  555. begin
  556. a += k8[0];
  557. a += (UInt32(k8[1])) shl 8;
  558. a += (UInt32(k8[2])) shl 16;
  559. a += (UInt32(k8[3])) shl 24;
  560. b += k8[4];
  561. b += (UInt32(k8[5])) shl 8;
  562. b += (UInt32(k8[6])) shl 16;
  563. b += (UInt32(k8[7])) shl 24;
  564. c += k8[8];
  565. c += (UInt32(k8[9])) shl 8;
  566. c += (UInt32(k8[10])) shl 16;
  567. c += (UInt32(k8[11])) shl 24;
  568. mix_abc;
  569. ALength -= 12;
  570. k8 += 12;
  571. end;
  572. case ALength of
  573. 12: goto Case12;
  574. 11: goto Case11;
  575. 10: goto Case10;
  576. 9 : goto Case9;
  577. 8 : goto Case8;
  578. 7 : goto Case7;
  579. 6 : goto Case6;
  580. 5 : goto Case5;
  581. 4 : goto Case4;
  582. 3 : goto Case3;
  583. 2 : goto Case2;
  584. 1 : goto Case1;
  585. 0 :
  586. begin
  587. APrimaryHashAndInitVal := c;
  588. ASecondaryHashAndInitVal := b;
  589. Exit; // zero length strings require no mixing
  590. end;
  591. end;
  592. Case12: c+=(UInt32(k8[11])) shl 24;
  593. Case11: c+=(UInt32(k8[10])) shl 16;
  594. Case10: c+=(UInt32(k8[9])) shl 8;
  595. Case9: c+=k8[8];
  596. Case8: b+=(UInt32(k8[7])) shl 24;
  597. Case7: b+=(UInt32(k8[6])) shl 16;
  598. Case6: b+=(UInt32(k8[5])) shl 8;
  599. Case5: b+=k8[4];
  600. Case4: a+=(UInt32(k8[3])) shl 24;
  601. Case3: a+=(UInt32(k8[2])) shl 16;
  602. Case2: a+=(UInt32(k8[1])) shl 8;
  603. Case1: a+=k8[0];
  604. end;
  605. final_abc;
  606. APrimaryHashAndInitVal := c;
  607. ASecondaryHashAndInitVal := b;
  608. end;
  609. procedure DelphiHashLittle2(AKey: Pointer; ALength: SizeInt; var APrimaryHashAndInitVal, ASecondaryHashAndInitVal: UInt32);
  610. var
  611. a,b,c: UInt32;
  612. u: record case byte of
  613. 0: (ptr: Pointer);
  614. 1: (i: PtrUint);
  615. end absolute AKey;
  616. k32: ^UInt32 absolute AKey;
  617. k16: ^UInt16 absolute AKey;
  618. k8: ^UInt8 absolute AKey;
  619. label _10, _8, _6, _4, _2;
  620. label Case12, Case11, Case10, Case9, Case8, Case7, Case6, Case5, Case4, Case3, Case2, Case1;
  621. begin
  622. //* Set up the internal state */
  623. a := $DEADBEEF + UInt32(ALength shl 2) + APrimaryHashAndInitVal; // delphi version bug? original version don't have "shl 2"
  624. b := a;
  625. c := b;
  626. c += ASecondaryHashAndInitVal;
  627. {$IFDEF ENDIAN_LITTLE}
  628. if (u.i and $3) = 0 then
  629. begin
  630. while (ALength > 12) do
  631. begin
  632. a += k32[0];
  633. b += k32[1];
  634. c += k32[2];
  635. mix_abc;
  636. ALength -= 12;
  637. k32 += 3;
  638. end;
  639. case ALength of
  640. 12: begin c += k32[2]; b += k32[1]; a += k32[0]; end;
  641. 11: begin c += k32[2] and $ffffff; b += k32[1]; a += k32[0]; end;
  642. 10: begin c += k32[2] and $ffff; b += k32[1]; a += k32[0]; end;
  643. 9 : begin c += k32[2] and $ff; b += k32[1]; a += k32[0]; end;
  644. 8 : begin b += k32[1]; a += k32[0]; end;
  645. 7 : begin b += k32[1] and $ffffff; a += k32[0]; end;
  646. 6 : begin b += k32[1] and $ffff; a += k32[0]; end;
  647. 5 : begin b += k32[1] and $ff; a += k32[0]; end;
  648. 4 : begin a += k32[0]; end;
  649. 3 : begin a += k32[0] and $ffffff; end;
  650. 2 : begin a += k32[0] and $ffff; end;
  651. 1 : begin a += k32[0] and $ff; end;
  652. 0 :
  653. begin
  654. APrimaryHashAndInitVal := c;
  655. ASecondaryHashAndInitVal := b;
  656. Exit; // zero length strings require no mixing
  657. end;
  658. end
  659. end
  660. else
  661. if (u.i and $1) = 0 then
  662. begin
  663. while (ALength > 12) do
  664. begin
  665. a += k16[0] + (UInt32(k16[1]) shl 16);
  666. b += k16[2] + (UInt32(k16[3]) shl 16);
  667. c += k16[4] + (UInt32(k16[5]) shl 16);
  668. mix_abc;
  669. ALength -= 12;
  670. k16 += 6;
  671. end;
  672. case ALength of
  673. 12:
  674. begin
  675. c+=k16[4]+((UInt32(k16[5])) shl 16);
  676. b+=k16[2]+((UInt32(k16[3])) shl 16);
  677. a+=k16[0]+((UInt32(k16[1])) shl 16);
  678. end;
  679. 11:
  680. begin
  681. c+=(UInt32(k8[10])) shl 16; //* fall through */
  682. goto _10;
  683. end;
  684. 10:
  685. begin _10:
  686. c+=k16[4];
  687. b+=k16[2]+((UInt32(k16[3])) shl 16);
  688. a+=k16[0]+((UInt32(k16[1])) shl 16);
  689. end;
  690. 9 :
  691. begin
  692. c+=k8[8]; //* fall through */
  693. goto _8;
  694. end;
  695. 8 :
  696. begin _8:
  697. b+=k16[2]+((UInt32(k16[3])) shl 16);
  698. a+=k16[0]+((UInt32(k16[1])) shl 16);
  699. end;
  700. 7 :
  701. begin
  702. b+=(UInt32(k8[6])) shl 16; //* fall through */
  703. goto _6;
  704. end;
  705. 6 :
  706. begin _6:
  707. b+=k16[2];
  708. a+=k16[0]+((UInt32(k16[1])) shl 16);
  709. end;
  710. 5 :
  711. begin
  712. b+=k8[4]; //* fall through */
  713. goto _4;
  714. end;
  715. 4 :
  716. begin _4:
  717. a+=k16[0]+((UInt32(k16[1])) shl 16);
  718. end;
  719. 3 :
  720. begin
  721. a+=(UInt32(k8[2])) shl 16; //* fall through */
  722. goto _2;
  723. end;
  724. 2 :
  725. begin _2:
  726. a+=k16[0];
  727. end;
  728. 1 :
  729. begin
  730. a+=k8[0];
  731. end;
  732. 0 :
  733. begin
  734. APrimaryHashAndInitVal := c;
  735. ASecondaryHashAndInitVal := b;
  736. Exit; // zero length strings require no mixing
  737. end;
  738. end;
  739. end
  740. else
  741. {$ENDIF}
  742. begin
  743. while ALength > 12 do
  744. begin
  745. a += k8[0];
  746. a += (UInt32(k8[1])) shl 8;
  747. a += (UInt32(k8[2])) shl 16;
  748. a += (UInt32(k8[3])) shl 24;
  749. b += k8[4];
  750. b += (UInt32(k8[5])) shl 8;
  751. b += (UInt32(k8[6])) shl 16;
  752. b += (UInt32(k8[7])) shl 24;
  753. c += k8[8];
  754. c += (UInt32(k8[9])) shl 8;
  755. c += (UInt32(k8[10])) shl 16;
  756. c += (UInt32(k8[11])) shl 24;
  757. mix_abc;
  758. ALength -= 12;
  759. k8 += 12;
  760. end;
  761. case ALength of
  762. 12: goto Case12;
  763. 11: goto Case11;
  764. 10: goto Case10;
  765. 9 : goto Case9;
  766. 8 : goto Case8;
  767. 7 : goto Case7;
  768. 6 : goto Case6;
  769. 5 : goto Case5;
  770. 4 : goto Case4;
  771. 3 : goto Case3;
  772. 2 : goto Case2;
  773. 1 : goto Case1;
  774. 0 :
  775. begin
  776. APrimaryHashAndInitVal := c;
  777. ASecondaryHashAndInitVal := b;
  778. Exit; // zero length strings require no mixing
  779. end;
  780. end;
  781. Case12: c+=(UInt32(k8[11])) shl 24;
  782. Case11: c+=(UInt32(k8[10])) shl 16;
  783. Case10: c+=(UInt32(k8[9])) shl 8;
  784. Case9: c+=k8[8];
  785. Case8: b+=(UInt32(k8[7])) shl 24;
  786. Case7: b+=(UInt32(k8[6])) shl 16;
  787. Case6: b+=(UInt32(k8[5])) shl 8;
  788. Case5: b+=k8[4];
  789. Case4: a+=(UInt32(k8[3])) shl 24;
  790. Case3: a+=(UInt32(k8[2])) shl 16;
  791. Case2: a+=(UInt32(k8[1])) shl 8;
  792. Case1: a+=k8[0];
  793. end;
  794. final_abc;
  795. APrimaryHashAndInitVal := c;
  796. ASecondaryHashAndInitVal := b;
  797. end;
  798. function DelphiHashLittle(AKey: Pointer; ALength: SizeInt; AInitVal: UInt32): Int32;
  799. var
  800. a, b, c: UInt32;
  801. u: record case byte of
  802. 0: (ptr: Pointer);
  803. 1: (i: PtrUint);
  804. end absolute AKey;
  805. k32: ^UInt32 absolute AKey;
  806. //k16: ^UInt16 absolute AKey;
  807. k8: ^UInt8 absolute AKey;
  808. label Case12, Case11, Case10, Case9, Case8, Case7, Case6, Case5, Case4, Case3, Case2, Case1;
  809. begin
  810. a := $DEADBEEF + UInt32(ALength shl 2) + AInitVal; // delphi version bug? original version don't have "shl 2"
  811. b := a;
  812. c := b;
  813. {.$IFDEF ENDIAN_LITTLE} // Delphi version don't care
  814. if (u.i and $3) = 0 then
  815. begin
  816. while (ALength > 12) do
  817. begin
  818. a += k32[0];
  819. b += k32[1];
  820. c += k32[2];
  821. mix_abc;
  822. ALength -= 12;
  823. k32 += 3;
  824. end;
  825. case ALength of
  826. 12: begin c += k32[2]; b += k32[1]; a += k32[0]; end;
  827. 11: begin c += k32[2] and $ffffff; b += k32[1]; a += k32[0]; end;
  828. 10: begin c += k32[2] and $ffff; b += k32[1]; a += k32[0]; end;
  829. 9 : begin c += k32[2] and $ff; b += k32[1]; a += k32[0]; end;
  830. 8 : begin b += k32[1]; a += k32[0]; end;
  831. 7 : begin b += k32[1] and $ffffff; a += k32[0]; end;
  832. 6 : begin b += k32[1] and $ffff; a += k32[0]; end;
  833. 5 : begin b += k32[1] and $ff; a += k32[0]; end;
  834. 4 : begin a += k32[0]; end;
  835. 3 : begin a += k32[0] and $ffffff; end;
  836. 2 : begin a += k32[0] and $ffff; end;
  837. 1 : begin a += k32[0] and $ff; end;
  838. 0 : Exit(c); // zero length strings require no mixing
  839. end
  840. end
  841. else
  842. {.$ENDIF}
  843. begin
  844. while ALength > 12 do
  845. begin
  846. a += k8[0];
  847. a += (UInt32(k8[1])) shl 8;
  848. a += (UInt32(k8[2])) shl 16;
  849. a += (UInt32(k8[3])) shl 24;
  850. b += k8[4];
  851. b += (UInt32(k8[5])) shl 8;
  852. b += (UInt32(k8[6])) shl 16;
  853. b += (UInt32(k8[7])) shl 24;
  854. c += k8[8];
  855. c += (UInt32(k8[9])) shl 8;
  856. c += (UInt32(k8[10])) shl 16;
  857. c += (UInt32(k8[11])) shl 24;
  858. mix_abc;
  859. ALength -= 12;
  860. k8 += 12;
  861. end;
  862. case ALength of
  863. 12: goto Case12;
  864. 11: goto Case11;
  865. 10: goto Case10;
  866. 9 : goto Case9;
  867. 8 : goto Case8;
  868. 7 : goto Case7;
  869. 6 : goto Case6;
  870. 5 : goto Case5;
  871. 4 : goto Case4;
  872. 3 : goto Case3;
  873. 2 : goto Case2;
  874. 1 : goto Case1;
  875. 0 : Exit(c);
  876. end;
  877. Case12: c+=(UInt32(k8[11])) shl 24;
  878. Case11: c+=(UInt32(k8[10])) shl 16;
  879. Case10: c+=(UInt32(k8[9])) shl 8;
  880. Case9: c+=k8[8];
  881. Case8: b+=(UInt32(k8[7])) shl 24;
  882. Case7: b+=(UInt32(k8[6])) shl 16;
  883. Case6: b+=(UInt32(k8[5])) shl 8;
  884. Case5: b+=k8[4];
  885. Case4: a+=(UInt32(k8[3])) shl 24;
  886. Case3: a+=(UInt32(k8[2])) shl 16;
  887. Case2: a+=(UInt32(k8[1])) shl 8;
  888. Case1: a+=k8[0];
  889. end;
  890. final_abc;
  891. Result := Int32(c);
  892. end;
  893. {$ifdef CPUARM} // circumvent FPC issue on ARM
  894. function ToByte(value: cardinal): cardinal; inline;
  895. begin
  896. result := value and $ff;
  897. end;
  898. {$else}
  899. type ToByte = byte;
  900. {$endif}
  901. {$ifdef CPUINTEL} // use optimized x86/x64 asm versions for xxHash32
  902. {$ifdef CPUX86}
  903. function xxHash32(crc: cardinal; P: Pointer; len: integer): cardinal;
  904. asm
  905. xchg edx, ecx
  906. push ebp
  907. push edi
  908. lea ebp, [ecx+edx]
  909. push esi
  910. push ebx
  911. sub esp, 8
  912. cmp edx, 15
  913. mov ebx, eax
  914. mov dword ptr [esp], edx
  915. lea eax, [ebx+165667B1H]
  916. jbe @2
  917. lea eax, [ebp-10H]
  918. lea edi, [ebx+24234428H]
  919. lea esi, [ebx-7A143589H]
  920. mov dword ptr [esp+4H], ebp
  921. mov edx, eax
  922. lea eax, [ebx+61C8864FH]
  923. mov ebp, edx
  924. @1: mov edx, dword ptr [ecx]
  925. imul edx, edx, -2048144777
  926. add edi, edx
  927. rol edi, 13
  928. imul edi, edi, -1640531535
  929. mov edx, dword ptr [ecx+4]
  930. imul edx, edx, -2048144777
  931. add esi, edx
  932. rol esi, 13
  933. imul esi, esi, -1640531535
  934. mov edx, dword ptr [ecx+8]
  935. imul edx, edx, -2048144777
  936. add ebx, edx
  937. rol ebx, 13
  938. imul ebx, ebx, -1640531535
  939. mov edx, dword ptr [ecx+12]
  940. lea ecx, [ecx+16]
  941. imul edx, edx, -2048144777
  942. add eax, edx
  943. rol eax, 13
  944. imul eax, eax, -1640531535
  945. cmp ebp, ecx
  946. jnc @1
  947. rol edi, 1
  948. rol esi, 7
  949. rol ebx, 12
  950. add esi, edi
  951. mov ebp, dword ptr [esp+4H]
  952. ror eax, 14
  953. add ebx, esi
  954. add eax, ebx
  955. @2: lea esi, [ecx+4H]
  956. add eax, dword ptr [esp]
  957. cmp ebp, esi
  958. jc @4
  959. mov ebx, esi
  960. nop
  961. @3: imul edx, dword ptr [ebx-4H], -1028477379
  962. add ebx, 4
  963. add eax, edx
  964. ror eax, 15
  965. imul eax, eax, 668265263
  966. cmp ebp, ebx
  967. jnc @3
  968. lea edx, [ebp-4H]
  969. sub edx, ecx
  970. mov ecx, edx
  971. and ecx, 0FFFFFFFCH
  972. add ecx, esi
  973. @4: cmp ebp, ecx
  974. jbe @6
  975. @5: movzx edx, byte ptr [ecx]
  976. add ecx, 1
  977. imul edx, edx, 374761393
  978. add eax, edx
  979. rol eax, 11
  980. imul eax, eax, -1640531535
  981. cmp ebp, ecx
  982. jnz @5
  983. nop
  984. @6: mov edx, eax
  985. add esp, 8
  986. shr edx, 15
  987. xor eax, edx
  988. imul eax, eax, -2048144777
  989. pop ebx
  990. pop esi
  991. mov edx, eax
  992. shr edx, 13
  993. xor eax, edx
  994. imul eax, eax, -1028477379
  995. pop edi
  996. pop ebp
  997. mov edx, eax
  998. shr edx, 16
  999. xor eax, edx
  1000. end;
  1001. {$endif CPUX86}
  1002. {$ifdef CPUX64}
  1003. function xxHash32(crc: cardinal; P: Pointer; len: integer): cardinal;
  1004. asm
  1005. {$ifndef WIN64} // crc=rdi P=rsi len=rdx
  1006. mov r8, rdi
  1007. mov rcx, rsi
  1008. {$else} // crc=r8 P=rcx len=rdx
  1009. mov r10, r8
  1010. mov r8, rcx
  1011. mov rcx, rdx
  1012. mov rdx, r10
  1013. push rsi // Win64 expects those registers to be preserved
  1014. push rdi
  1015. {$endif}
  1016. // P=r8 len=rcx crc=rdx
  1017. push rbx
  1018. lea r10, [rcx+rdx]
  1019. cmp rdx, 15
  1020. lea eax, [r8+165667B1H]
  1021. jbe @2
  1022. lea rsi, [r10-10H]
  1023. lea ebx, [r8+24234428H]
  1024. lea edi, [r8-7A143589H]
  1025. lea eax, [r8+61C8864FH]
  1026. @1: imul r9d, dword ptr [rcx], -2048144777
  1027. add rcx, 16
  1028. imul r11d, dword ptr [rcx-0CH], -2048144777
  1029. add ebx, r9d
  1030. lea r9d, [r11+rdi]
  1031. rol ebx, 13
  1032. rol r9d, 13
  1033. imul ebx, ebx, -1640531535
  1034. imul edi, r9d, -1640531535
  1035. imul r9d, dword ptr [rcx-8H], -2048144777
  1036. add r8d, r9d
  1037. imul r9d, dword ptr [rcx-4H], -2048144777
  1038. rol r8d, 13
  1039. imul r8d, r8d, -1640531535
  1040. add eax, r9d
  1041. rol eax, 13
  1042. imul eax, eax, -1640531535
  1043. cmp rsi, rcx
  1044. jnc @1
  1045. rol edi, 7
  1046. rol ebx, 1
  1047. rol r8d, 12
  1048. mov r9d, edi
  1049. ror eax, 14
  1050. add r9d, ebx
  1051. add r8d, r9d
  1052. add eax, r8d
  1053. @2: lea r9, [rcx+4H]
  1054. add eax, edx
  1055. cmp r10, r9
  1056. jc @4
  1057. mov r8, r9
  1058. @3: imul edx, dword ptr [r8-4H], -1028477379
  1059. add r8, 4
  1060. add eax, edx
  1061. ror eax, 15
  1062. imul eax, eax, 668265263
  1063. cmp r10, r8
  1064. jnc @3
  1065. lea rdx, [r10-4H]
  1066. sub rdx, rcx
  1067. mov rcx, rdx
  1068. and rcx, 0FFFFFFFFFFFFFFFCH
  1069. add rcx, r9
  1070. @4: cmp r10, rcx
  1071. jbe @6
  1072. @5: movzx edx, byte ptr [rcx]
  1073. add rcx, 1
  1074. imul edx, edx, 374761393
  1075. add eax, edx
  1076. rol eax, 11
  1077. imul eax, eax, -1640531535
  1078. cmp r10, rcx
  1079. jnz @5
  1080. @6: mov edx, eax
  1081. shr edx, 15
  1082. xor eax, edx
  1083. imul eax, eax, -2048144777
  1084. mov edx, eax
  1085. shr edx, 13
  1086. xor eax, edx
  1087. imul eax, eax, -1028477379
  1088. mov edx, eax
  1089. shr edx, 16
  1090. xor eax, edx
  1091. pop rbx
  1092. {$ifdef WIN64}
  1093. pop rdi
  1094. pop rsi
  1095. {$endif}
  1096. end;
  1097. {$endif CPUX64}
  1098. {$else not CPUINTEL}
  1099. function xxHash32(crc: cardinal; P: Pointer; len: integer): cardinal;
  1100. begin
  1101. result := xxHash32Pascal(crc, P, len);
  1102. end;
  1103. {$endif CPUINTEL}
  1104. const
  1105. PRIME32_1 = 2654435761;
  1106. PRIME32_2 = 2246822519;
  1107. PRIME32_3 = 3266489917;
  1108. PRIME32_4 = 668265263;
  1109. PRIME32_5 = 374761393;
  1110. // RolDWord is an intrinsic function under FPC :)
  1111. function Rol13(value: cardinal): cardinal; inline;
  1112. begin
  1113. result := RolDWord(value, 13);
  1114. end;
  1115. function xxHash32Pascal(crc: cardinal; P: Pointer; len: integer): cardinal;
  1116. var c1, c2, c3, c4: cardinal;
  1117. PLimit, PEnd: PAnsiChar;
  1118. begin
  1119. PEnd := P + len;
  1120. if len >= 16 then begin
  1121. PLimit := PEnd - 16;
  1122. c3 := crc;
  1123. c2 := c3 + PRIME32_2;
  1124. c1 := c2 + PRIME32_1;
  1125. c4 := c3 - PRIME32_1;
  1126. repeat
  1127. c1 := PRIME32_1 * Rol13(c1 + PRIME32_2 * PCardinal(P)^);
  1128. c2 := PRIME32_1 * Rol13(c2 + PRIME32_2 * PCardinal(P+4)^);
  1129. c3 := PRIME32_1 * Rol13(c3 + PRIME32_2 * PCardinal(P+8)^);
  1130. c4 := PRIME32_1 * Rol13(c4 + PRIME32_2 * PCardinal(P+12)^);
  1131. inc(P, 16);
  1132. until not (P <= PLimit);
  1133. result := RolDWord(c1, 1) + RolDWord(c2, 7) + RolDWord(c3, 12) + RolDWord(c4, 18);
  1134. end else
  1135. result := crc + PRIME32_5;
  1136. inc(result, len);
  1137. { Use "P + 4 <= PEnd" instead of "P <= PEnd - 4" to avoid crashes in case P = nil.
  1138. When P = nil,
  1139. then "PtrUInt(PEnd - 4)" is 4294967292,
  1140. so the condition "P <= PEnd - 4" would be satisfied,
  1141. and the code would try to access PCardinal(nil)^ causing a SEGFAULT. }
  1142. while P + 4 <= PEnd do begin
  1143. inc(result, PCardinal(P)^ * PRIME32_3);
  1144. result := RolDWord(result, 17) * PRIME32_4;
  1145. inc(P, 4);
  1146. end;
  1147. while P < PEnd do begin
  1148. inc(result, PByte(P)^ * PRIME32_5);
  1149. result := RolDWord(result, 11) * PRIME32_1;
  1150. inc(P);
  1151. end;
  1152. result := result xor (result shr 15);
  1153. result := result * PRIME32_2;
  1154. result := result xor (result shr 13);
  1155. result := result * PRIME32_3;
  1156. result := result xor (result shr 16);
  1157. end;
  1158. {$ifdef CPUINTEL}
  1159. type
  1160. TRegisters = record
  1161. eax,ebx,ecx,edx: cardinal;
  1162. end;
  1163. {$ifdef CPU64}
  1164. procedure GetCPUID(Param: Cardinal; var Registers: TRegisters); nostackframe; assembler;
  1165. asm
  1166. {$ifdef win64}
  1167. mov eax, ecx
  1168. mov r9, rdx
  1169. {$else}
  1170. mov eax, edi
  1171. mov r9, rsi
  1172. {$endif win64}
  1173. mov r10, rbx // preserve rbx
  1174. xor ebx, ebx
  1175. xor ecx, ecx
  1176. xor edx, edx
  1177. cpuid
  1178. mov TRegisters(r9).&eax, eax
  1179. mov TRegisters(r9).&ebx, ebx
  1180. mov TRegisters(r9).&ecx, ecx
  1181. mov TRegisters(r9).&edx, edx
  1182. mov rbx, r10
  1183. end;
  1184. function crc32csse42(crc: cardinal; buf: Pointer; len: cardinal): cardinal; nostackframe; assembler;
  1185. asm // ecx=crc, rdx=buf, r8=len (Linux: edi,rsi,rdx)
  1186. {$ifdef win64}
  1187. mov eax, ecx
  1188. {$else}
  1189. mov eax, edi
  1190. mov r8, rdx
  1191. mov rdx, rsi
  1192. {$endif win64}
  1193. not eax
  1194. test rdx, rdx
  1195. jz @0
  1196. test r8, r8
  1197. jz @0
  1198. @7: test dl, 7
  1199. jz @8 // align to 8 bytes boundary
  1200. crc32 eax, byte ptr[rdx]
  1201. inc rdx
  1202. dec r8
  1203. jz @0
  1204. test dl, 7
  1205. jnz @7
  1206. @8: mov rcx, r8
  1207. shr r8, 3
  1208. jz @2
  1209. @1:
  1210. crc32 rax, qword [rdx] // hash 8 bytes per loop
  1211. dec r8
  1212. lea rdx, [rdx + 8]
  1213. jnz @1
  1214. @2: and ecx, 7
  1215. jz @0
  1216. cmp ecx, 4
  1217. jb @4
  1218. crc32 eax, dword ptr[rdx]
  1219. sub ecx, 4
  1220. lea rdx, [rdx + 4]
  1221. jz @0
  1222. @4: crc32 eax, byte ptr[rdx]
  1223. dec ecx
  1224. jz @0
  1225. crc32 eax, byte ptr[rdx + 1]
  1226. dec ecx
  1227. jz @0
  1228. crc32 eax, byte ptr[rdx + 2]
  1229. @0: not eax
  1230. end;
  1231. {$endif CPU64}
  1232. {$ifdef CPUX86}
  1233. procedure GetCPUID(Param: Cardinal; var Registers: TRegisters);
  1234. asm
  1235. push esi
  1236. push edi
  1237. mov esi, edx
  1238. mov edi, eax
  1239. pushfd
  1240. pop eax
  1241. mov edx, eax
  1242. xor eax, $200000
  1243. push eax
  1244. popfd
  1245. pushfd
  1246. pop eax
  1247. xor eax, edx
  1248. jz @nocpuid
  1249. push ebx
  1250. mov eax, edi
  1251. xor ecx, ecx
  1252. cpuid
  1253. mov TRegisters(esi).&eax, eax
  1254. mov TRegisters(esi).&ebx, ebx
  1255. mov TRegisters(esi).&ecx, ecx
  1256. mov TRegisters(esi).&edx, edx
  1257. pop ebx
  1258. @nocpuid:
  1259. pop edi
  1260. pop esi
  1261. end;
  1262. function crc32csse42(crc: cardinal; buf: Pointer; len: cardinal): cardinal;
  1263. asm // eax=crc, edx=buf, ecx=len
  1264. not eax
  1265. test ecx, ecx
  1266. jz @0
  1267. test edx, edx
  1268. jz @0
  1269. @3: test edx, 3
  1270. jz @8 // align to 4 bytes boundary
  1271. crc32 eax, byte ptr[edx]
  1272. inc edx
  1273. dec ecx
  1274. jz @0
  1275. test edx, 3
  1276. jnz @3
  1277. @8: push ecx
  1278. shr ecx, 3
  1279. jz @2
  1280. @1:
  1281. crc32 eax, dword ptr[edx]
  1282. crc32 eax, dword ptr[edx + 4]
  1283. dec ecx
  1284. lea edx, [edx + 8]
  1285. jnz @1
  1286. @2: pop ecx
  1287. and ecx, 7
  1288. jz @0
  1289. cmp ecx, 4
  1290. jb @4
  1291. crc32 eax, dword ptr[edx]
  1292. sub ecx, 4
  1293. lea edx, [edx + 4]
  1294. jz @0
  1295. @4:
  1296. crc32 eax, byte ptr[edx]
  1297. dec ecx
  1298. jz @0
  1299. crc32 eax, byte ptr[edx + 1]
  1300. dec ecx
  1301. jz @0
  1302. crc32 eax, byte ptr[edx + 2]
  1303. @0: not eax
  1304. end;
  1305. {$endif CPUX86}
  1306. type
  1307. /// the potential features, retrieved from an Intel CPU
  1308. // - see https://en.wikipedia.org/wiki/CPUID#EAX.3D1:_Processor_Info_and_Feature_Bits
  1309. TIntelCpuFeature =
  1310. ( { in EDX }
  1311. cfFPU, cfVME, cfDE, cfPSE, cfTSC, cfMSR, cfPAE, cfMCE,
  1312. cfCX8, cfAPIC, cf_d10, cfSEP, cfMTRR, cfPGE, cfMCA, cfCMOV,
  1313. cfPAT, cfPSE36, cfPSN, cfCLFSH, cf_d20, cfDS, cfACPI, cfMMX,
  1314. cfFXSR, cfSSE, cfSSE2, cfSS, cfHTT, cfTM, cfIA64, cfPBE,
  1315. { in ECX }
  1316. cfSSE3, cfCLMUL, cfDS64, cfMON, cfDSCPL, cfVMX, cfSMX, cfEST,
  1317. cfTM2, cfSSSE3, cfCID, cfSDBG, cfFMA, cfCX16, cfXTPR, cfPDCM,
  1318. cf_c16, cfPCID, cfDCA, cfSSE41, cfSSE42, cfX2A, cfMOVBE, cfPOPCNT,
  1319. cfTSC2, cfAESNI, cfXS, cfOSXS, cfAVX, cfF16C, cfRAND, cfHYP,
  1320. { extended features in EBX, ECX }
  1321. cfFSGS, cf_b01, cfSGX, cfBMI1, cfHLE, cfAVX2, cf_b06, cfSMEP,
  1322. cfBMI2, cfERMS, cfINVPCID, cfRTM, cfPQM, cf_b13, cfMPX, cfPQE,
  1323. cfAVX512F, cfAVX512DQ, cfRDSEED, cfADX, cfSMAP, cfAVX512IFMA, cfPCOMMIT, cfCLFLUSH,
  1324. cfCLWB, cfIPT, cfAVX512PF, cfAVX512ER, cfAVX512CD, cfSHA, cfAVX512BW, cfAVX512VL,
  1325. cfPREFW1, cfAVX512VBMI, cfUMIP, cfPKU, cfOSPKE, cf_c05, cf_c06, cf_c07,
  1326. cf_c08, cf_c09, cf_c10, cf_c11, cf_c12, cf_c13, cfAVX512VPC, cf_c15,
  1327. cf_cc16, cf_c17, cf_c18, cf_c19, cf_c20, cf_c21, cfRDPID, cf_c23,
  1328. cf_c24, cf_c25, cf_c26, cf_c27, cf_c28, cf_c29, cfSGXLC, cf_c31,
  1329. cf_d0, cf_d1, cfAVX512NNI, cfAVX512MAS, cf_d4, cf_d5, cf_d6, cf_d7);
  1330. /// all features, as retrieved from an Intel CPU
  1331. TIntelCpuFeatures = set of TIntelCpuFeature;
  1332. var
  1333. /// the available CPU features, as recognized at program startup
  1334. CpuFeatures: TIntelCpuFeatures;
  1335. procedure TestIntelCpuFeatures;
  1336. var regs: TRegisters;
  1337. begin
  1338. regs.edx := 0;
  1339. regs.ecx := 0;
  1340. GetCPUID(1,regs);
  1341. PIntegerArray(@CpuFeatures)^[0] := regs.edx;
  1342. PIntegerArray(@CpuFeatures)^[1] := regs.ecx;
  1343. GetCPUID(7,regs);
  1344. PIntegerArray(@CpuFeatures)^[2] := regs.ebx;
  1345. PIntegerArray(@CpuFeatures)^[3] := regs.ecx;
  1346. PByte(@PIntegerArray(@CpuFeatures)^[4])^ := regs.edx;
  1347. // assert(sizeof(CpuFeatures)=4*4+1);
  1348. {$ifdef Darwin}
  1349. {$ifdef CPU64}
  1350. // SSE42 asm does not (yet) work on Darwin x64 ...
  1351. Exclude(CpuFeatures, cfSSE42);
  1352. {$endif}
  1353. {$endif}
  1354. end;
  1355. {$endif CPUINTEL}
  1356. var
  1357. crc32ctab: array[0..{$ifdef PUREPASCAL}3{$else}7{$endif},byte] of cardinal;
  1358. function crc32cfast(crc: cardinal; buf: Pointer; len: cardinal): cardinal;
  1359. {$ifdef PUREPASCAL}
  1360. begin
  1361. result := not crc;
  1362. if (buf<>nil) and (len>0) then begin
  1363. repeat
  1364. if PtrUInt(buf) and 3=0 then // align to 4 bytes boundary
  1365. break;
  1366. result := crc32ctab[0,ToByte(result xor cardinal(buf^))] xor (result shr 8);
  1367. dec(len);
  1368. inc(buf);
  1369. until len=0;
  1370. while len>=4 do begin
  1371. result := result xor PCardinal(buf)^;
  1372. inc(buf,4);
  1373. result := crc32ctab[3,ToByte(result)] xor
  1374. crc32ctab[2,ToByte(result shr 8)] xor
  1375. crc32ctab[1,ToByte(result shr 16)] xor
  1376. crc32ctab[0,result shr 24];
  1377. dec(len,4);
  1378. end;
  1379. while len>0 do begin
  1380. result := crc32ctab[0,ToByte(result xor cardinal(buf^))] xor (result shr 8);
  1381. dec(len);
  1382. inc(buf);
  1383. end;
  1384. end;
  1385. result := not result;
  1386. end;
  1387. {$else}
  1388. // adapted from fast Aleksandr Sharahov version
  1389. asm
  1390. test edx, edx
  1391. jz @ret
  1392. neg ecx
  1393. jz @ret
  1394. not eax
  1395. push ebx
  1396. @head: test dl, 3
  1397. jz @aligned
  1398. movzx ebx, byte[edx]
  1399. inc edx
  1400. xor bl, al
  1401. shr eax, 8
  1402. xor eax, dword ptr[ebx * 4 + crc32ctab]
  1403. inc ecx
  1404. jnz @head
  1405. pop ebx
  1406. not eax
  1407. ret
  1408. @ret: rep ret
  1409. @aligned:
  1410. sub edx, ecx
  1411. add ecx, 8
  1412. jg @bodydone
  1413. push esi
  1414. push edi
  1415. mov edi, edx
  1416. mov edx, eax
  1417. @bodyloop:
  1418. mov ebx, [edi + ecx - 4]
  1419. xor edx, [edi + ecx - 8]
  1420. movzx esi, bl
  1421. mov eax, dword ptr[esi * 4 + crc32ctab + 1024 * 3]
  1422. movzx esi, bh
  1423. xor eax, dword ptr[esi * 4 + crc32ctab + 1024 * 2]
  1424. shr ebx, 16
  1425. movzx esi, bl
  1426. xor eax, dword ptr[esi * 4 + crc32ctab + 1024 * 1]
  1427. movzx esi, bh
  1428. xor eax, dword ptr[esi * 4 + crc32ctab + 1024 * 0]
  1429. movzx esi, dl
  1430. xor eax, dword ptr[esi * 4 + crc32ctab + 1024 * 7]
  1431. movzx esi, dh
  1432. xor eax, dword ptr[esi * 4 + crc32ctab + 1024 * 6]
  1433. shr edx, 16
  1434. movzx esi, dl
  1435. xor eax, dword ptr[esi * 4 + crc32ctab + 1024 * 5]
  1436. movzx esi, dh
  1437. xor eax, dword ptr[esi * 4 + crc32ctab + 1024 * 4]
  1438. add ecx, 8
  1439. jg @done
  1440. mov ebx, [edi + ecx - 4]
  1441. xor eax, [edi + ecx - 8]
  1442. movzx esi, bl
  1443. mov edx, dword ptr[esi * 4 + crc32ctab + 1024 * 3]
  1444. movzx esi, bh
  1445. xor edx, dword ptr[esi * 4 + crc32ctab + 1024 * 2]
  1446. shr ebx, 16
  1447. movzx esi, bl
  1448. xor edx, dword ptr[esi * 4 + crc32ctab + 1024 * 1]
  1449. movzx esi, bh
  1450. xor edx, dword ptr[esi * 4 + crc32ctab + 1024 * 0]
  1451. movzx esi, al
  1452. xor edx, dword ptr[esi * 4 + crc32ctab + 1024 * 7]
  1453. movzx esi, ah
  1454. xor edx, dword ptr[esi * 4 + crc32ctab + 1024 * 6]
  1455. shr eax, 16
  1456. movzx esi, al
  1457. xor edx, dword ptr[esi * 4 + crc32ctab + 1024 * 5]
  1458. movzx esi, ah
  1459. xor edx, dword ptr[esi * 4 + crc32ctab + 1024 * 4]
  1460. add ecx, 8
  1461. jle @bodyloop
  1462. mov eax, edx
  1463. @done: mov edx, edi
  1464. pop edi
  1465. pop esi
  1466. @bodydone:
  1467. sub ecx, 8
  1468. jl @tail
  1469. pop ebx
  1470. not eax
  1471. ret
  1472. @tail: movzx ebx, byte[edx + ecx]
  1473. xor bl, al
  1474. shr eax, 8
  1475. xor eax, dword ptr[ebx * 4 + crc32ctab]
  1476. inc ecx
  1477. jnz @tail
  1478. pop ebx
  1479. not eax
  1480. end;
  1481. {$endif PUREPASCAL}
  1482. procedure InitializeCrc32ctab;
  1483. var
  1484. i, n: integer;
  1485. crc: cardinal;
  1486. begin
  1487. // initialize tables for crc32cfast() and SymmetricEncrypt/FillRandom
  1488. for i := 0 to 255 do begin
  1489. crc := i;
  1490. for n := 1 to 8 do
  1491. if (crc and 1)<>0 then // polynom is not the same as with zlib's crc32()
  1492. crc := (crc shr 1) xor $82f63b78 else
  1493. crc := crc shr 1;
  1494. crc32ctab[0,i] := crc;
  1495. end;
  1496. for i := 0 to 255 do begin
  1497. crc := crc32ctab[0,i];
  1498. for n := 1 to high(crc32ctab) do begin
  1499. crc := (crc shr 8) xor crc32ctab[0,ToByte(crc)];
  1500. crc32ctab[n,i] := crc;
  1501. end;
  1502. end;
  1503. end;
  1504. begin
  1505. {$ifdef CPUINTEL}
  1506. TestIntelCpuFeatures;
  1507. if cfSSE42 in CpuFeatures then
  1508. begin
  1509. crc32c := @crc32csse42;
  1510. mORMotHasher := @crc32csse42;
  1511. end
  1512. else
  1513. {$endif CPUINTEL}
  1514. begin
  1515. InitializeCrc32ctab;
  1516. crc32c := @crc32cfast;
  1517. mORMotHasher := @{$IFDEF CPUINTEL}xxHash32{$ELSE}xxHash32Pascal{$ENDIF};
  1518. end;
  1519. end.