xxhash.h 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766
  1. /*
  2. * xxHash - Extremely Fast Hash algorithm
  3. * Header File
  4. * Copyright (C) 2012-2020 Yann Collet
  5. *
  6. * BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * You can contact the author at:
  32. * - xxHash homepage: https://www.xxhash.com
  33. * - xxHash source repository: https://github.com/Cyan4973/xxHash
  34. */
  35. /* TODO: update */
  36. /* Notice extracted from xxHash homepage:
  37. xxHash is an extremely fast hash algorithm, running at RAM speed limits.
  38. It also successfully passes all tests from the SMHasher suite.
  39. Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
  40. Name Speed Q.Score Author
  41. xxHash 5.4 GB/s 10
  42. CrapWow 3.2 GB/s 2 Andrew
  43. MumurHash 3a 2.7 GB/s 10 Austin Appleby
  44. SpookyHash 2.0 GB/s 10 Bob Jenkins
  45. SBox 1.4 GB/s 9 Bret Mulvey
  46. Lookup3 1.2 GB/s 9 Bob Jenkins
  47. SuperFastHash 1.2 GB/s 1 Paul Hsieh
  48. CityHash64 1.05 GB/s 10 Pike & Alakuijala
  49. FNV 0.55 GB/s 5 Fowler, Noll, Vo
  50. CRC32 0.43 GB/s 9
  51. MD5-32 0.33 GB/s 10 Ronald L. Rivest
  52. SHA1-32 0.28 GB/s 10
  53. Q.Score is a measure of quality of the hash function.
  54. It depends on successfully passing SMHasher test set.
  55. 10 is a perfect score.
  56. Note: SMHasher's CRC32 implementation is not the fastest one.
  57. Other speed-oriented implementations can be faster,
  58. especially in combination with PCLMUL instruction:
  59. https://fastcompression.blogspot.com/2019/03/presenting-xxh3.html?showComment=1552696407071#c3490092340461170735
  60. A 64-bit version, named XXH64, is available since r35.
  61. It offers much better speed, but for 64-bit applications only.
  62. Name Speed on 64 bits Speed on 32 bits
  63. XXH64 13.8 GB/s 1.9 GB/s
  64. XXH32 6.8 GB/s 6.0 GB/s
  65. */
  66. #if defined (__cplusplus)
  67. extern "C" {
  68. #endif
  69. /* ****************************
  70. * INLINE mode
  71. ******************************/
  72. /*!
  73. * XXH_INLINE_ALL (and XXH_PRIVATE_API)
  74. * Use these build macros to inline xxhash into the target unit.
  75. * Inlining improves performance on small inputs, especially when the length is
  76. * expressed as a compile-time constant:
  77. *
  78. * https://fastcompression.blogspot.com/2018/03/xxhash-for-small-keys-impressive-power.html
  79. *
  80. * It also keeps xxHash symbols private to the unit, so they are not exported.
  81. *
  82. * Usage:
  83. * #define XXH_INLINE_ALL
  84. * #include "xxhash.h"
  85. *
  86. * Do not compile and link xxhash.o as a separate object, as it is not useful.
  87. */
  88. #if (defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)) \
  89. && !defined(XXH_INLINE_ALL_31684351384)
  90. /* this section should be traversed only once */
  91. # define XXH_INLINE_ALL_31684351384
  92. /* give access to the advanced API, required to compile implementations */
  93. # undef XXH_STATIC_LINKING_ONLY /* avoid macro redef */
  94. # define XXH_STATIC_LINKING_ONLY
  95. /* make all functions private */
  96. # undef XXH_PUBLIC_API
  97. # if defined(__GNUC__)
  98. # define XXH_PUBLIC_API static __inline __attribute__((unused))
  99. # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  100. # define XXH_PUBLIC_API static inline
  101. # elif defined(_MSC_VER)
  102. # define XXH_PUBLIC_API static __inline
  103. # else
  104. /* note: this version may generate warnings for unused static functions */
  105. # define XXH_PUBLIC_API static
  106. # endif
  107. /*
  108. * This part deals with the special case where a unit wants to inline xxHash,
  109. * but "xxhash.h" has previously been included without XXH_INLINE_ALL, such
  110. * as part of some previously included *.h header file.
  111. * Without further action, the new include would just be ignored,
  112. * and functions would effectively _not_ be inlined (silent failure).
  113. * The following macros solve this situation by prefixing all inlined names,
  114. * avoiding naming collision with previous inclusions.
  115. */
  116. # ifdef XXH_NAMESPACE
  117. # error "XXH_INLINE_ALL with XXH_NAMESPACE is not supported"
  118. /*
  119. * Note: Alternative: #undef all symbols (it's a pretty large list).
  120. * Without #error: it compiles, but functions are actually not inlined.
  121. */
  122. # endif
  123. # define XXH_NAMESPACE XXH_INLINE_
  124. /*
  125. * Some identifiers (enums, type names) are not symbols, but they must
  126. * still be renamed to avoid redeclaration.
  127. * Alternative solution: do not redeclare them.
  128. * However, this requires some #ifdefs, and is a more dispersed action.
  129. * Meanwhile, renaming can be achieved in a single block
  130. */
  131. # define XXH_IPREF(Id) XXH_INLINE_ ## Id
  132. # define XXH_OK XXH_IPREF(XXH_OK)
  133. # define XXH_ERROR XXH_IPREF(XXH_ERROR)
  134. # define XXH_errorcode XXH_IPREF(XXH_errorcode)
  135. # define XXH32_canonical_t XXH_IPREF(XXH32_canonical_t)
  136. # define XXH64_canonical_t XXH_IPREF(XXH64_canonical_t)
  137. # define XXH128_canonical_t XXH_IPREF(XXH128_canonical_t)
  138. # define XXH32_state_s XXH_IPREF(XXH32_state_s)
  139. # define XXH32_state_t XXH_IPREF(XXH32_state_t)
  140. # define XXH64_state_s XXH_IPREF(XXH64_state_s)
  141. # define XXH64_state_t XXH_IPREF(XXH64_state_t)
  142. # define XXH3_state_s XXH_IPREF(XXH3_state_s)
  143. # define XXH3_state_t XXH_IPREF(XXH3_state_t)
  144. # define XXH128_hash_t XXH_IPREF(XXH128_hash_t)
  145. /* Ensure the header is parsed again, even if it was previously included */
  146. # undef XXHASH_H_5627135585666179
  147. # undef XXHASH_H_STATIC_13879238742
  148. #endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */
  149. /* ****************************************************************
  150. * Stable API
  151. *****************************************************************/
  152. #ifndef XXHASH_H_5627135585666179
  153. #define XXHASH_H_5627135585666179 1
  154. /* specific declaration modes for Windows */
  155. #if !defined(XXH_INLINE_ALL) && !defined(XXH_PRIVATE_API)
  156. # if defined(WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT))
  157. # ifdef XXH_EXPORT
  158. # define XXH_PUBLIC_API __declspec(dllexport)
  159. # elif XXH_IMPORT
  160. # define XXH_PUBLIC_API __declspec(dllimport)
  161. # endif
  162. # else
  163. # define XXH_PUBLIC_API /* do nothing */
  164. # endif
  165. #endif
  166. /*!
  167. * XXH_NAMESPACE, aka Namespace Emulation:
  168. *
  169. * If you want to include _and expose_ xxHash functions from within your own
  170. * library, but also want to avoid symbol collisions with other libraries which
  171. * may also include xxHash, you can use XXH_NAMESPACE to automatically prefix
  172. * any public symbol from xxhash library with the value of XXH_NAMESPACE
  173. * (therefore, avoid empty or numeric values).
  174. *
  175. * Note that no change is required within the calling program as long as it
  176. * includes `xxhash.h`: Regular symbol names will be automatically translated
  177. * by this header.
  178. */
  179. #ifdef XXH_NAMESPACE
  180. # define XXH_CAT(A,B) A##B
  181. # define XXH_NAME2(A,B) XXH_CAT(A,B)
  182. # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
  183. /* XXH32 */
  184. # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
  185. # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
  186. # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
  187. # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
  188. # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
  189. # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
  190. # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
  191. # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
  192. # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
  193. /* XXH64 */
  194. # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
  195. # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
  196. # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
  197. # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
  198. # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
  199. # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
  200. # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
  201. # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
  202. # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
  203. /* XXH3_64bits */
  204. # define XXH3_64bits XXH_NAME2(XXH_NAMESPACE, XXH3_64bits)
  205. # define XXH3_64bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSecret)
  206. # define XXH3_64bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSeed)
  207. # define XXH3_createState XXH_NAME2(XXH_NAMESPACE, XXH3_createState)
  208. # define XXH3_freeState XXH_NAME2(XXH_NAMESPACE, XXH3_freeState)
  209. # define XXH3_copyState XXH_NAME2(XXH_NAMESPACE, XXH3_copyState)
  210. # define XXH3_64bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset)
  211. # define XXH3_64bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSeed)
  212. # define XXH3_64bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSecret)
  213. # define XXH3_64bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_update)
  214. # define XXH3_64bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_digest)
  215. # define XXH3_generateSecret XXH_NAME2(XXH_NAMESPACE, XXH3_generateSecret)
  216. /* XXH3_128bits */
  217. # define XXH128 XXH_NAME2(XXH_NAMESPACE, XXH128)
  218. # define XXH3_128bits XXH_NAME2(XXH_NAMESPACE, XXH3_128bits)
  219. # define XXH3_128bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSeed)
  220. # define XXH3_128bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSecret)
  221. # define XXH3_128bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset)
  222. # define XXH3_128bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSeed)
  223. # define XXH3_128bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSecret)
  224. # define XXH3_128bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_update)
  225. # define XXH3_128bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_digest)
  226. # define XXH128_isEqual XXH_NAME2(XXH_NAMESPACE, XXH128_isEqual)
  227. # define XXH128_cmp XXH_NAME2(XXH_NAMESPACE, XXH128_cmp)
  228. # define XXH128_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH128_canonicalFromHash)
  229. # define XXH128_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH128_hashFromCanonical)
  230. #endif
  231. /* *************************************
  232. * Version
  233. ***************************************/
  234. #define XXH_VERSION_MAJOR 0
  235. #define XXH_VERSION_MINOR 8
  236. #define XXH_VERSION_RELEASE 0
  237. #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
  238. XXH_PUBLIC_API unsigned XXH_versionNumber (void);
  239. /* ****************************
  240. * Definitions
  241. ******************************/
  242. #include <stddef.h> /* size_t */
  243. typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
  244. /*-**********************************************************************
  245. * 32-bit hash
  246. ************************************************************************/
  247. #if !defined (__VMS) \
  248. && (defined (__cplusplus) \
  249. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  250. # include <stdint.h>
  251. typedef uint32_t XXH32_hash_t;
  252. #else
  253. # include <limits.h>
  254. # if UINT_MAX == 0xFFFFFFFFUL
  255. typedef unsigned int XXH32_hash_t;
  256. # else
  257. # if ULONG_MAX == 0xFFFFFFFFUL
  258. typedef unsigned long XXH32_hash_t;
  259. # else
  260. # error "unsupported platform: need a 32-bit type"
  261. # endif
  262. # endif
  263. #endif
  264. /*!
  265. * XXH32():
  266. * Calculate the 32-bit hash of sequence "length" bytes stored at memory address "input".
  267. * The memory between input & input+length must be valid (allocated and read-accessible).
  268. * "seed" can be used to alter the result predictably.
  269. * Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark): 5.4 GB/s
  270. *
  271. * Note: XXH3 provides competitive speed for both 32-bit and 64-bit systems,
  272. * and offers true 64/128 bit hash results. It provides a superior level of
  273. * dispersion, and greatly reduces the risks of collisions.
  274. */
  275. XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, XXH32_hash_t seed);
  276. /******* Streaming *******/
  277. /*
  278. * Streaming functions generate the xxHash value from an incrememtal input.
  279. * This method is slower than single-call functions, due to state management.
  280. * For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized.
  281. *
  282. * An XXH state must first be allocated using `XXH*_createState()`.
  283. *
  284. * Start a new hash by initializing the state with a seed using `XXH*_reset()`.
  285. *
  286. * Then, feed the hash state by calling `XXH*_update()` as many times as necessary.
  287. *
  288. * The function returns an error code, with 0 meaning OK, and any other value
  289. * meaning there is an error.
  290. *
  291. * Finally, a hash value can be produced anytime, by using `XXH*_digest()`.
  292. * This function returns the nn-bits hash as an int or long long.
  293. *
  294. * It's still possible to continue inserting input into the hash state after a
  295. * digest, and generate new hash values later on by invoking `XXH*_digest()`.
  296. *
  297. * When done, release the state using `XXH*_freeState()`.
  298. */
  299. typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */
  300. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);
  301. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
  302. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state);
  303. XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, XXH32_hash_t seed);
  304. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
  305. XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
  306. /******* Canonical representation *******/
  307. /*
  308. * The default return values from XXH functions are unsigned 32 and 64 bit
  309. * integers.
  310. * This the simplest and fastest format for further post-processing.
  311. *
  312. * However, this leaves open the question of what is the order on the byte level,
  313. * since little and big endian conventions will store the same number differently.
  314. *
  315. * The canonical representation settles this issue by mandating big-endian
  316. * convention, the same convention as human-readable numbers (large digits first).
  317. *
  318. * When writing hash values to storage, sending them over a network, or printing
  319. * them, it's highly recommended to use the canonical representation to ensure
  320. * portability across a wider range of systems, present and future.
  321. *
  322. * The following functions allow transformation of hash values to and from
  323. * canonical format.
  324. */
  325. typedef struct { unsigned char digest[4]; } XXH32_canonical_t;
  326. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);
  327. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);
  328. #ifndef XXH_NO_LONG_LONG
  329. /*-**********************************************************************
  330. * 64-bit hash
  331. ************************************************************************/
  332. #if !defined (__VMS) \
  333. && (defined (__cplusplus) \
  334. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  335. # include <stdint.h>
  336. typedef uint64_t XXH64_hash_t;
  337. #else
  338. /* the following type must have a width of 64-bit */
  339. typedef unsigned long long XXH64_hash_t;
  340. #endif
  341. /*!
  342. * XXH64():
  343. * Returns the 64-bit hash of sequence of length @length stored at memory
  344. * address @input.
  345. * @seed can be used to alter the result predictably.
  346. *
  347. * This function usually runs faster on 64-bit systems, but slower on 32-bit
  348. * systems (see benchmark).
  349. *
  350. * Note: XXH3 provides competitive speed for both 32-bit and 64-bit systems,
  351. * and offers true 64/128 bit hash results. It provides a superior level of
  352. * dispersion, and greatly reduces the risks of collisions.
  353. */
  354. XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, XXH64_hash_t seed);
  355. /******* Streaming *******/
  356. typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
  357. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);
  358. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);
  359. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dst_state, const XXH64_state_t* src_state);
  360. XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, XXH64_hash_t seed);
  361. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
  362. XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr);
  363. /******* Canonical representation *******/
  364. typedef struct { unsigned char digest[sizeof(XXH64_hash_t)]; } XXH64_canonical_t;
  365. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);
  366. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);
  367. /*-**********************************************************************
  368. * XXH3 64-bit variant
  369. ************************************************************************/
  370. /* ************************************************************************
  371. * XXH3 is a new hash algorithm featuring:
  372. * - Improved speed for both small and large inputs
  373. * - True 64-bit and 128-bit outputs
  374. * - SIMD acceleration
  375. * - Improved 32-bit viability
  376. *
  377. * Speed analysis methodology is explained here:
  378. *
  379. * https://fastcompression.blogspot.com/2019/03/presenting-xxh3.html
  380. *
  381. * In general, expect XXH3 to run about ~2x faster on large inputs and >3x
  382. * faster on small ones compared to XXH64, though exact differences depend on
  383. * the platform.
  384. *
  385. * The algorithm is portable: Like XXH32 and XXH64, it generates the same hash
  386. * on all platforms.
  387. *
  388. * It benefits greatly from SIMD and 64-bit arithmetic, but does not require it.
  389. *
  390. * Almost all 32-bit and 64-bit targets that can run XXH32 smoothly can run
  391. * XXH3 at competitive speeds, even if XXH64 runs slowly. Further details are
  392. * explained in the implementation.
  393. *
  394. * Optimized implementations are provided for AVX512, AVX2, SSE2, NEON, POWER8,
  395. * ZVector and scalar targets. This can be controlled with the XXH_VECTOR macro.
  396. *
  397. * XXH3 offers 2 variants, _64bits and _128bits.
  398. * When only 64 bits are needed, prefer calling the _64bits variant, as it
  399. * reduces the amount of mixing, resulting in faster speed on small inputs.
  400. *
  401. * It's also generally simpler to manipulate a scalar return type than a struct.
  402. *
  403. * The 128-bit version adds additional strength, but it is slightly slower.
  404. *
  405. * The XXH3 algorithm is still in development.
  406. * The results it produces may still change in future versions.
  407. *
  408. * Results produced by v0.7.x are not comparable with results from v0.7.y.
  409. * However, the API is completely stable, and it can safely be used for
  410. * ephemeral data (local sessions).
  411. *
  412. * Avoid storing values in long-term storage until the algorithm is finalized.
  413. * XXH3's return values will be officially finalized upon reaching v0.8.0.
  414. *
  415. * After which, return values of XXH3 and XXH128 will no longer change in
  416. * future versions.
  417. *
  418. * The API supports one-shot hashing, streaming mode, and custom secrets.
  419. */
  420. /* XXH3_64bits():
  421. * default 64-bit variant, using default secret and default seed of 0.
  422. * It's the fastest variant. */
  423. XXH_PUBLIC_API XXH64_hash_t XXH3_64bits(const void* data, size_t len);
  424. /*
  425. * XXH3_64bits_withSeed():
  426. * This variant generates a custom secret on the fly
  427. * based on default secret altered using the `seed` value.
  428. * While this operation is decently fast, note that it's not completely free.
  429. * Note: seed==0 produces the same results as XXH3_64bits().
  430. */
  431. XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_withSeed(const void* data, size_t len, XXH64_hash_t seed);
  432. /*
  433. * XXH3_64bits_withSecret():
  434. * It's possible to provide any blob of bytes as a "secret" to generate the hash.
  435. * This makes it more difficult for an external actor to prepare an intentional collision.
  436. * The main condition is that secretSize *must* be large enough (>= XXH3_SECRET_SIZE_MIN).
  437. * However, the quality of produced hash values depends on secret's entropy.
  438. * Technically, the secret must look like a bunch of random bytes.
  439. * Avoid "trivial" or structured data such as repeated sequences or a text document.
  440. * Whenever unsure about the "randomness" of the blob of bytes,
  441. * consider relabelling it as a "custom seed" instead,
  442. * and employ "XXH3_generateSecret()" (see below)
  443. * to generate a high entropy secret derived from the custom seed.
  444. */
  445. #define XXH3_SECRET_SIZE_MIN 136
  446. XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize);
  447. /******* Streaming *******/
  448. /*
  449. * Streaming requires state maintenance.
  450. * This operation costs memory and CPU.
  451. * As a consequence, streaming is slower than one-shot hashing.
  452. * For better performance, prefer one-shot functions whenever applicable.
  453. */
  454. typedef struct XXH3_state_s XXH3_state_t;
  455. XXH_PUBLIC_API XXH3_state_t* XXH3_createState(void);
  456. XXH_PUBLIC_API XXH_errorcode XXH3_freeState(XXH3_state_t* statePtr);
  457. XXH_PUBLIC_API void XXH3_copyState(XXH3_state_t* dst_state, const XXH3_state_t* src_state);
  458. /*
  459. * XXH3_64bits_reset():
  460. * Initialize with default parameters.
  461. * digest will be equivalent to `XXH3_64bits()`.
  462. */
  463. XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset(XXH3_state_t* statePtr);
  464. /*
  465. * XXH3_64bits_reset_withSeed():
  466. * Generate a custom secret from `seed`, and store it into `statePtr`.
  467. * digest will be equivalent to `XXH3_64bits_withSeed()`.
  468. */
  469. XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed);
  470. /*
  471. * XXH3_64bits_reset_withSecret():
  472. * `secret` is referenced, it _must outlive_ the hash streaming session.
  473. * Similar to one-shot API, `secretSize` must be >= `XXH3_SECRET_SIZE_MIN`,
  474. * and the quality of produced hash values depends on secret's entropy
  475. * (secret's content should look like a bunch of random bytes).
  476. * When in doubt about the randomness of a candidate `secret`,
  477. * consider employing `XXH3_generateSecret()` instead (see below).
  478. */
  479. XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize);
  480. XXH_PUBLIC_API XXH_errorcode XXH3_64bits_update (XXH3_state_t* statePtr, const void* input, size_t length);
  481. XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_digest (const XXH3_state_t* statePtr);
  482. /* note : canonical representation of XXH3 is the same as XXH64
  483. * since they both produce XXH64_hash_t values */
  484. /*-**********************************************************************
  485. * XXH3 128-bit variant
  486. ************************************************************************/
  487. typedef struct {
  488. XXH64_hash_t low64;
  489. XXH64_hash_t high64;
  490. } XXH128_hash_t;
  491. XXH_PUBLIC_API XXH128_hash_t XXH3_128bits(const void* data, size_t len);
  492. XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_withSeed(const void* data, size_t len, XXH64_hash_t seed);
  493. XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize);
  494. /******* Streaming *******/
  495. /*
  496. * Streaming requires state maintenance.
  497. * This operation costs memory and CPU.
  498. * As a consequence, streaming is slower than one-shot hashing.
  499. * For better performance, prefer one-shot functions whenever applicable.
  500. *
  501. * XXH3_128bits uses the same XXH3_state_t as XXH3_64bits().
  502. * Use already declared XXH3_createState() and XXH3_freeState().
  503. *
  504. * All reset and streaming functions have same meaning as their 64-bit counterpart.
  505. */
  506. XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset(XXH3_state_t* statePtr);
  507. XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed);
  508. XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize);
  509. XXH_PUBLIC_API XXH_errorcode XXH3_128bits_update (XXH3_state_t* statePtr, const void* input, size_t length);
  510. XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_digest (const XXH3_state_t* statePtr);
  511. /* Following helper functions make it possible to compare XXH128_hast_t values.
  512. * Since XXH128_hash_t is a structure, this capability is not offered by the language.
  513. * Note: For better performance, these functions can be inlined using XXH_INLINE_ALL */
  514. /*!
  515. * XXH128_isEqual():
  516. * Return: 1 if `h1` and `h2` are equal, 0 if they are not.
  517. */
  518. XXH_PUBLIC_API int XXH128_isEqual(XXH128_hash_t h1, XXH128_hash_t h2);
  519. /*!
  520. * XXH128_cmp():
  521. *
  522. * This comparator is compatible with stdlib's `qsort()`/`bsearch()`.
  523. *
  524. * return: >0 if *h128_1 > *h128_2
  525. * =0 if *h128_1 == *h128_2
  526. * <0 if *h128_1 < *h128_2
  527. */
  528. XXH_PUBLIC_API int XXH128_cmp(const void* h128_1, const void* h128_2);
  529. /******* Canonical representation *******/
  530. typedef struct { unsigned char digest[sizeof(XXH128_hash_t)]; } XXH128_canonical_t;
  531. XXH_PUBLIC_API void XXH128_canonicalFromHash(XXH128_canonical_t* dst, XXH128_hash_t hash);
  532. XXH_PUBLIC_API XXH128_hash_t XXH128_hashFromCanonical(const XXH128_canonical_t* src);
  533. #endif /* XXH_NO_LONG_LONG */
  534. #endif /* XXHASH_H_5627135585666179 */
  535. #if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXHASH_H_STATIC_13879238742)
  536. #define XXHASH_H_STATIC_13879238742
  537. /* ****************************************************************************
  538. * This section contains declarations which are not guaranteed to remain stable.
  539. * They may change in future versions, becoming incompatible with a different
  540. * version of the library.
  541. * These declarations should only be used with static linking.
  542. * Never use them in association with dynamic linking!
  543. ***************************************************************************** */
  544. /*
  545. * These definitions are only present to allow static allocation
  546. * of XXH states, on stack or in a struct, for example.
  547. * Never **ever** access their members directly.
  548. */
  549. struct XXH32_state_s {
  550. XXH32_hash_t total_len_32;
  551. XXH32_hash_t large_len;
  552. XXH32_hash_t v1;
  553. XXH32_hash_t v2;
  554. XXH32_hash_t v3;
  555. XXH32_hash_t v4;
  556. XXH32_hash_t mem32[4];
  557. XXH32_hash_t memsize;
  558. XXH32_hash_t reserved; /* never read nor write, might be removed in a future version */
  559. }; /* typedef'd to XXH32_state_t */
  560. #ifndef XXH_NO_LONG_LONG /* defined when there is no 64-bit support */
  561. struct XXH64_state_s {
  562. XXH64_hash_t total_len;
  563. XXH64_hash_t v1;
  564. XXH64_hash_t v2;
  565. XXH64_hash_t v3;
  566. XXH64_hash_t v4;
  567. XXH64_hash_t mem64[4];
  568. XXH32_hash_t memsize;
  569. XXH32_hash_t reserved32; /* required for padding anyway */
  570. XXH64_hash_t reserved64; /* never read nor write, might be removed in a future version */
  571. }; /* typedef'd to XXH64_state_t */
  572. #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11+ */
  573. # include <stdalign.h>
  574. # define XXH_ALIGN(n) alignas(n)
  575. #elif defined(__GNUC__)
  576. # define XXH_ALIGN(n) __attribute__ ((aligned(n)))
  577. #elif defined(_MSC_VER)
  578. # define XXH_ALIGN(n) __declspec(align(n))
  579. #else
  580. # define XXH_ALIGN(n) /* disabled */
  581. #endif
  582. /* Old GCC versions only accept the attribute after the type in structures. */
  583. #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) /* C11+ */ \
  584. && defined(__GNUC__)
  585. # define XXH_ALIGN_MEMBER(align, type) type XXH_ALIGN(align)
  586. #else
  587. # define XXH_ALIGN_MEMBER(align, type) XXH_ALIGN(align) type
  588. #endif
  589. #define XXH3_INTERNALBUFFER_SIZE 256
  590. #define XXH3_SECRET_DEFAULT_SIZE 192
  591. struct XXH3_state_s {
  592. XXH_ALIGN_MEMBER(64, XXH64_hash_t acc[8]);
  593. /* used to store a custom secret generated from a seed */
  594. XXH_ALIGN_MEMBER(64, unsigned char customSecret[XXH3_SECRET_DEFAULT_SIZE]);
  595. XXH_ALIGN_MEMBER(64, unsigned char buffer[XXH3_INTERNALBUFFER_SIZE]);
  596. XXH32_hash_t bufferedSize;
  597. XXH32_hash_t reserved32;
  598. size_t nbStripesSoFar;
  599. XXH64_hash_t totalLen;
  600. size_t nbStripesPerBlock;
  601. size_t secretLimit;
  602. XXH64_hash_t seed;
  603. XXH64_hash_t reserved64;
  604. const unsigned char* extSecret; /* reference to external secret;
  605. * if == NULL, use .customSecret instead */
  606. /* note: there may be some padding at the end due to alignment on 64 bytes */
  607. }; /* typedef'd to XXH3_state_t */
  608. #undef XXH_ALIGN_MEMBER
  609. /* When the XXH3_state_t structure is merely emplaced on stack,
  610. * it should be initialized with XXH3_INITSTATE() or a memset()
  611. * in case its first reset uses XXH3_NNbits_reset_withSeed().
  612. * This init can be omitted if the first reset uses default or _withSecret mode.
  613. * This operation isn't necessary when the state is created with XXH3_createState().
  614. * Note that this doesn't prepare the state for a streaming operation,
  615. * it's still necessary to use XXH3_NNbits_reset*() afterwards.
  616. */
  617. #define XXH3_INITSTATE(XXH3_state_ptr) { (XXH3_state_ptr)->seed = 0; }
  618. /* === Experimental API === */
  619. /* Symbols defined below must be considered tied to a specific library version. */
  620. /*
  621. * XXH3_generateSecret():
  622. *
  623. * Derive a high-entropy secret from any user-defined content, named customSeed.
  624. * The generated secret can be used in combination with `*_withSecret()` functions.
  625. * The `_withSecret()` variants are useful to provide a higher level of protection than 64-bit seed,
  626. * as it becomes much more difficult for an external actor to guess how to impact the calculation logic.
  627. *
  628. * The function accepts as input a custom seed of any length and any content,
  629. * and derives from it a high-entropy secret of length XXH3_SECRET_DEFAULT_SIZE
  630. * into an already allocated buffer secretBuffer.
  631. * The generated secret is _always_ XXH_SECRET_DEFAULT_SIZE bytes long.
  632. *
  633. * The generated secret can then be used with any `*_withSecret()` variant.
  634. * Functions `XXH3_128bits_withSecret()`, `XXH3_64bits_withSecret()`,
  635. * `XXH3_128bits_reset_withSecret()` and `XXH3_64bits_reset_withSecret()`
  636. * are part of this list. They all accept a `secret` parameter
  637. * which must be very long for implementation reasons (>= XXH3_SECRET_SIZE_MIN)
  638. * _and_ feature very high entropy (consist of random-looking bytes).
  639. * These conditions can be a high bar to meet, so
  640. * this function can be used to generate a secret of proper quality.
  641. *
  642. * customSeed can be anything. It can have any size, even small ones,
  643. * and its content can be anything, even stupidly "low entropy" source such as a bunch of zeroes.
  644. * The resulting `secret` will nonetheless provide all expected qualities.
  645. *
  646. * Supplying NULL as the customSeed copies the default secret into `secretBuffer`.
  647. * When customSeedSize > 0, supplying NULL as customSeed is undefined behavior.
  648. */
  649. XXH_PUBLIC_API void XXH3_generateSecret(void* secretBuffer, const void* customSeed, size_t customSeedSize);
  650. /* simple short-cut to pre-selected XXH3_128bits variant */
  651. XXH_PUBLIC_API XXH128_hash_t XXH128(const void* data, size_t len, XXH64_hash_t seed);
  652. #endif /* XXH_NO_LONG_LONG */
  653. #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
  654. # define XXH_IMPLEMENTATION
  655. #endif
  656. #endif /* defined(XXH_STATIC_LINKING_ONLY) && !defined(XXHASH_H_STATIC_13879238742) */
  657. /* ======================================================================== */
  658. /* ======================================================================== */
  659. /* ======================================================================== */
  660. /*-**********************************************************************
  661. * xxHash implementation
  662. *-**********************************************************************
  663. * xxHash's implementation used to be hosted inside xxhash.c.
  664. *
  665. * However, inlining requires implementation to be visible to the compiler,
  666. * hence be included alongside the header.
  667. * Previously, implementation was hosted inside xxhash.c,
  668. * which was then #included when inlining was activated.
  669. * This construction created issues with a few build and install systems,
  670. * as it required xxhash.c to be stored in /include directory.
  671. *
  672. * xxHash implementation is now directly integrated within xxhash.h.
  673. * As a consequence, xxhash.c is no longer needed in /include.
  674. *
  675. * xxhash.c is still available and is still useful.
  676. * In a "normal" setup, when xxhash is not inlined,
  677. * xxhash.h only exposes the prototypes and public symbols,
  678. * while xxhash.c can be built into an object file xxhash.o
  679. * which can then be linked into the final binary.
  680. ************************************************************************/
  681. #if ( defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) \
  682. || defined(XXH_IMPLEMENTATION) ) && !defined(XXH_IMPLEM_13a8737387)
  683. # define XXH_IMPLEM_13a8737387
  684. /* *************************************
  685. * Tuning parameters
  686. ***************************************/
  687. /*!
  688. * XXH_FORCE_MEMORY_ACCESS:
  689. * By default, access to unaligned memory is controlled by `memcpy()`, which is
  690. * safe and portable.
  691. *
  692. * Unfortunately, on some target/compiler combinations, the generated assembly
  693. * is sub-optimal.
  694. *
  695. * The below switch allow selection of a different access method
  696. * in the search for improved performance.
  697. * Method 0 (default):
  698. * Use `memcpy()`. Safe and portable. Default.
  699. * Method 1:
  700. * `__attribute__((packed))` statement. It depends on compiler extensions
  701. * and is therefore not portable.
  702. * This method is safe if your compiler supports it, and *generally* as
  703. * fast or faster than `memcpy`.
  704. * Method 2:
  705. * Direct access via cast. This method doesn't depend on the compiler but
  706. * violates the C standard.
  707. * It can generate buggy code on targets which do not support unaligned
  708. * memory accesses.
  709. * But in some circumstances, it's the only known way to get the most
  710. * performance (example: GCC + ARMv6)
  711. * Method 3:
  712. * Byteshift. This can generate the best code on old compilers which don't
  713. * inline small `memcpy()` calls, and it might also be faster on big-endian
  714. * systems which lack a native byteswap instruction.
  715. * See https://stackoverflow.com/a/32095106/646947 for details.
  716. * Prefer these methods in priority order (0 > 1 > 2 > 3)
  717. */
  718. #ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  719. # if !defined(__clang__) && defined(__GNUC__) && defined(__ARM_FEATURE_UNALIGNED) && defined(__ARM_ARCH) && (__ARM_ARCH == 6)
  720. # define XXH_FORCE_MEMORY_ACCESS 2
  721. # elif !defined(__clang__) && ((defined(__INTEL_COMPILER) && !defined(_WIN32)) || \
  722. (defined(__GNUC__) && (defined(__ARM_ARCH) && __ARM_ARCH >= 7)))
  723. # define XXH_FORCE_MEMORY_ACCESS 1
  724. # endif
  725. #endif
  726. /*!
  727. * XXH_ACCEPT_NULL_INPUT_POINTER:
  728. * If the input pointer is NULL, xxHash's default behavior is to dereference it,
  729. * triggering a segfault.
  730. * When this macro is enabled, xxHash actively checks the input for a null pointer.
  731. * If it is, the result for null input pointers is the same as a zero-length input.
  732. */
  733. #ifndef XXH_ACCEPT_NULL_INPUT_POINTER /* can be defined externally */
  734. # define XXH_ACCEPT_NULL_INPUT_POINTER 0
  735. #endif
  736. /*!
  737. * XXH_FORCE_ALIGN_CHECK:
  738. * This is an important performance trick
  739. * for architectures without decent unaligned memory access performance.
  740. * It checks for input alignment, and when conditions are met,
  741. * uses a "fast path" employing direct 32-bit/64-bit read,
  742. * resulting in _dramatically faster_ read speed.
  743. *
  744. * The check costs one initial branch per hash, which is generally negligible, but not zero.
  745. * Moreover, it's not useful to generate binary for an additional code path
  746. * if memory access uses same instruction for both aligned and unaligned adresses.
  747. *
  748. * In these cases, the alignment check can be removed by setting this macro to 0.
  749. * Then the code will always use unaligned memory access.
  750. * Align check is automatically disabled on x86, x64 & arm64,
  751. * which are platforms known to offer good unaligned memory accesses performance.
  752. *
  753. * This option does not affect XXH3 (only XXH32 and XXH64).
  754. */
  755. #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
  756. # if defined(__i386) || defined(__x86_64__) || defined(__aarch64__) \
  757. || defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64) /* visual */
  758. # define XXH_FORCE_ALIGN_CHECK 0
  759. # else
  760. # define XXH_FORCE_ALIGN_CHECK 1
  761. # endif
  762. #endif
  763. /*!
  764. * XXH_NO_INLINE_HINTS:
  765. *
  766. * By default, xxHash tries to force the compiler to inline almost all internal
  767. * functions.
  768. *
  769. * This can usually improve performance due to reduced jumping and improved
  770. * constant folding, but significantly increases the size of the binary which
  771. * might not be favorable.
  772. *
  773. * Additionally, sometimes the forced inlining can be detrimental to performance,
  774. * depending on the architecture.
  775. *
  776. * XXH_NO_INLINE_HINTS marks all internal functions as static, giving the
  777. * compiler full control on whether to inline or not.
  778. *
  779. * When not optimizing (-O0), optimizing for size (-Os, -Oz), or using
  780. * -fno-inline with GCC or Clang, this will automatically be defined.
  781. */
  782. #ifndef XXH_NO_INLINE_HINTS
  783. # if defined(__OPTIMIZE_SIZE__) /* -Os, -Oz */ \
  784. || defined(__NO_INLINE__) /* -O0, -fno-inline */
  785. # define XXH_NO_INLINE_HINTS 1
  786. # else
  787. # define XXH_NO_INLINE_HINTS 0
  788. # endif
  789. #endif
  790. /*!
  791. * XXH_REROLL:
  792. * Whether to reroll XXH32_finalize, and XXH64_finalize,
  793. * instead of using an unrolled jump table/if statement loop.
  794. *
  795. * This is automatically defined on -Os/-Oz on GCC and Clang.
  796. */
  797. #ifndef XXH_REROLL
  798. # if defined(__OPTIMIZE_SIZE__)
  799. # define XXH_REROLL 1
  800. # else
  801. # define XXH_REROLL 0
  802. # endif
  803. #endif
  804. /* *************************************
  805. * Includes & Memory related functions
  806. ***************************************/
  807. /*!
  808. * Modify the local functions below should you wish to use
  809. * different memory routines for malloc() and free()
  810. */
  811. #include <stdlib.h>
  812. static void* XXH_malloc(size_t s) { return malloc(s); }
  813. static void XXH_free(void* p) { free(p); }
  814. /*! and for memcpy() */
  815. #include <string.h>
  816. static void* XXH_memcpy(void* dest, const void* src, size_t size)
  817. {
  818. return memcpy(dest,src,size);
  819. }
  820. #include <limits.h> /* ULLONG_MAX */
  821. /* *************************************
  822. * Compiler Specific Options
  823. ***************************************/
  824. #ifdef _MSC_VER /* Visual Studio warning fix */
  825. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  826. #endif
  827. #if XXH_NO_INLINE_HINTS /* disable inlining hints */
  828. # if defined(__GNUC__)
  829. # define XXH_FORCE_INLINE static __attribute__((unused))
  830. # else
  831. # define XXH_FORCE_INLINE static
  832. # endif
  833. # define XXH_NO_INLINE static
  834. /* enable inlining hints */
  835. #elif defined(_MSC_VER) /* Visual Studio */
  836. # define XXH_FORCE_INLINE static __forceinline
  837. # define XXH_NO_INLINE static __declspec(noinline)
  838. #elif defined(__GNUC__)
  839. # define XXH_FORCE_INLINE static __inline__ __attribute__((always_inline, unused))
  840. # define XXH_NO_INLINE static __attribute__((noinline))
  841. #elif defined (__cplusplus) \
  842. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* C99 */
  843. # define XXH_FORCE_INLINE static inline
  844. # define XXH_NO_INLINE static
  845. #else
  846. # define XXH_FORCE_INLINE static
  847. # define XXH_NO_INLINE static
  848. #endif
  849. /* *************************************
  850. * Debug
  851. ***************************************/
  852. /*
  853. * XXH_DEBUGLEVEL is expected to be defined externally, typically via the
  854. * compiler's command line options. The value must be a number.
  855. */
  856. #ifndef XXH_DEBUGLEVEL
  857. # ifdef DEBUGLEVEL /* backwards compat */
  858. # define XXH_DEBUGLEVEL DEBUGLEVEL
  859. # else
  860. # define XXH_DEBUGLEVEL 0
  861. # endif
  862. #endif
  863. #if (XXH_DEBUGLEVEL>=1)
  864. # include <assert.h> /* note: can still be disabled with NDEBUG */
  865. # define XXH_ASSERT(c) assert(c)
  866. #else
  867. # define XXH_ASSERT(c) ((void)0)
  868. #endif
  869. /* note: use after variable declarations */
  870. #define XXH_STATIC_ASSERT(c) do { enum { XXH_sa = 1/(int)(!!(c)) }; } while (0)
  871. /* *************************************
  872. * Basic Types
  873. ***************************************/
  874. #if !defined (__VMS) \
  875. && (defined (__cplusplus) \
  876. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  877. # include <stdint.h>
  878. typedef uint8_t xxh_u8;
  879. #else
  880. typedef unsigned char xxh_u8;
  881. #endif
  882. typedef XXH32_hash_t xxh_u32;
  883. #ifdef XXH_OLD_NAMES
  884. # define BYTE xxh_u8
  885. # define U8 xxh_u8
  886. # define U32 xxh_u32
  887. #endif
  888. /* *** Memory access *** */
  889. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
  890. /*
  891. * Manual byteshift. Best for old compilers which don't inline memcpy.
  892. * We actually directly use XXH_readLE32 and XXH_readBE32.
  893. */
  894. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  895. /*
  896. * Force direct memory access. Only works on CPU which support unaligned memory
  897. * access in hardware.
  898. */
  899. static xxh_u32 XXH_read32(const void* memPtr) { return *(const xxh_u32*) memPtr; }
  900. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  901. /*
  902. * __pack instructions are safer but compiler specific, hence potentially
  903. * problematic for some compilers.
  904. *
  905. * Currently only defined for GCC and ICC.
  906. */
  907. #ifdef XXH_OLD_NAMES
  908. typedef union { xxh_u32 u32; } __attribute__((packed)) unalign;
  909. #endif
  910. static xxh_u32 XXH_read32(const void* ptr)
  911. {
  912. typedef union { xxh_u32 u32; } __attribute__((packed)) xxh_unalign;
  913. return ((const xxh_unalign*)ptr)->u32;
  914. }
  915. #else
  916. /*
  917. * Portable and safe solution. Generally efficient.
  918. * see: https://stackoverflow.com/a/32095106/646947
  919. */
  920. static xxh_u32 XXH_read32(const void* memPtr)
  921. {
  922. xxh_u32 val;
  923. memcpy(&val, memPtr, sizeof(val));
  924. return val;
  925. }
  926. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  927. /* *** Endianess *** */
  928. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  929. /*!
  930. * XXH_CPU_LITTLE_ENDIAN:
  931. * Defined to 1 if the target is little endian, or 0 if it is big endian.
  932. * It can be defined externally, for example on the compiler command line.
  933. *
  934. * If it is not defined, a runtime check (which is usually constant folded)
  935. * is used instead.
  936. */
  937. #ifndef XXH_CPU_LITTLE_ENDIAN
  938. /*
  939. * Try to detect endianness automatically, to avoid the nonstandard behavior
  940. * in `XXH_isLittleEndian()`
  941. */
  942. # if defined(_WIN32) /* Windows is always little endian */ \
  943. || defined(__LITTLE_ENDIAN__) \
  944. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  945. # define XXH_CPU_LITTLE_ENDIAN 1
  946. # elif defined(__BIG_ENDIAN__) \
  947. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  948. # define XXH_CPU_LITTLE_ENDIAN 0
  949. # else
  950. /*
  951. * runtime test, presumed to simplify to a constant by compiler
  952. */
  953. static int XXH_isLittleEndian(void)
  954. {
  955. /*
  956. * Portable and well-defined behavior.
  957. * Don't use static: it is detrimental to performance.
  958. */
  959. const union { xxh_u32 u; xxh_u8 c[4]; } one = { 1 };
  960. return one.c[0];
  961. }
  962. # define XXH_CPU_LITTLE_ENDIAN XXH_isLittleEndian()
  963. # endif
  964. #endif
  965. /* ****************************************
  966. * Compiler-specific Functions and Macros
  967. ******************************************/
  968. #define XXH_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  969. #ifdef __has_builtin
  970. # define XXH_HAS_BUILTIN(x) __has_builtin(x)
  971. #else
  972. # define XXH_HAS_BUILTIN(x) 0
  973. #endif
  974. #if !defined(NO_CLANG_BUILTIN) && XXH_HAS_BUILTIN(__builtin_rotateleft32) \
  975. && XXH_HAS_BUILTIN(__builtin_rotateleft64)
  976. # define XXH_rotl32 __builtin_rotateleft32
  977. # define XXH_rotl64 __builtin_rotateleft64
  978. /* Note: although _rotl exists for minGW (GCC under windows), performance seems poor */
  979. #elif defined(_MSC_VER)
  980. # define XXH_rotl32(x,r) _rotl(x,r)
  981. # define XXH_rotl64(x,r) _rotl64(x,r)
  982. #else
  983. # define XXH_rotl32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
  984. # define XXH_rotl64(x,r) (((x) << (r)) | ((x) >> (64 - (r))))
  985. #endif
  986. #if defined(_MSC_VER) /* Visual Studio */
  987. # define XXH_swap32 _byteswap_ulong
  988. #elif XXH_GCC_VERSION >= 403
  989. # define XXH_swap32 __builtin_bswap32
  990. #else
  991. static xxh_u32 XXH_swap32 (xxh_u32 x)
  992. {
  993. return ((x << 24) & 0xff000000 ) |
  994. ((x << 8) & 0x00ff0000 ) |
  995. ((x >> 8) & 0x0000ff00 ) |
  996. ((x >> 24) & 0x000000ff );
  997. }
  998. #endif
  999. /* ***************************
  1000. * Memory reads
  1001. *****************************/
  1002. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  1003. /*
  1004. * XXH_FORCE_MEMORY_ACCESS==3 is an endian-independent byteshift load.
  1005. *
  1006. * This is ideal for older compilers which don't inline memcpy.
  1007. */
  1008. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
  1009. XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* memPtr)
  1010. {
  1011. const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
  1012. return bytePtr[0]
  1013. | ((xxh_u32)bytePtr[1] << 8)
  1014. | ((xxh_u32)bytePtr[2] << 16)
  1015. | ((xxh_u32)bytePtr[3] << 24);
  1016. }
  1017. XXH_FORCE_INLINE xxh_u32 XXH_readBE32(const void* memPtr)
  1018. {
  1019. const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
  1020. return bytePtr[3]
  1021. | ((xxh_u32)bytePtr[2] << 8)
  1022. | ((xxh_u32)bytePtr[1] << 16)
  1023. | ((xxh_u32)bytePtr[0] << 24);
  1024. }
  1025. #else
  1026. XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* ptr)
  1027. {
  1028. return XXH_CPU_LITTLE_ENDIAN ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
  1029. }
  1030. static xxh_u32 XXH_readBE32(const void* ptr)
  1031. {
  1032. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
  1033. }
  1034. #endif
  1035. XXH_FORCE_INLINE xxh_u32
  1036. XXH_readLE32_align(const void* ptr, XXH_alignment align)
  1037. {
  1038. if (align==XXH_unaligned) {
  1039. return XXH_readLE32(ptr);
  1040. } else {
  1041. return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u32*)ptr : XXH_swap32(*(const xxh_u32*)ptr);
  1042. }
  1043. }
  1044. /* *************************************
  1045. * Misc
  1046. ***************************************/
  1047. XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
  1048. /* *******************************************************************
  1049. * 32-bit hash functions
  1050. *********************************************************************/
  1051. static const xxh_u32 XXH_PRIME32_1 = 0x9E3779B1U; /* 0b10011110001101110111100110110001 */
  1052. static const xxh_u32 XXH_PRIME32_2 = 0x85EBCA77U; /* 0b10000101111010111100101001110111 */
  1053. static const xxh_u32 XXH_PRIME32_3 = 0xC2B2AE3DU; /* 0b11000010101100101010111000111101 */
  1054. static const xxh_u32 XXH_PRIME32_4 = 0x27D4EB2FU; /* 0b00100111110101001110101100101111 */
  1055. static const xxh_u32 XXH_PRIME32_5 = 0x165667B1U; /* 0b00010110010101100110011110110001 */
  1056. #ifdef XXH_OLD_NAMES
  1057. # define PRIME32_1 XXH_PRIME32_1
  1058. # define PRIME32_2 XXH_PRIME32_2
  1059. # define PRIME32_3 XXH_PRIME32_3
  1060. # define PRIME32_4 XXH_PRIME32_4
  1061. # define PRIME32_5 XXH_PRIME32_5
  1062. #endif
  1063. static xxh_u32 XXH32_round(xxh_u32 acc, xxh_u32 input)
  1064. {
  1065. acc += input * XXH_PRIME32_2;
  1066. acc = XXH_rotl32(acc, 13);
  1067. acc *= XXH_PRIME32_1;
  1068. #if defined(__GNUC__) && defined(__SSE4_1__) && !defined(XXH_ENABLE_AUTOVECTORIZE)
  1069. /*
  1070. * UGLY HACK:
  1071. * This inline assembly hack forces acc into a normal register. This is the
  1072. * only thing that prevents GCC and Clang from autovectorizing the XXH32
  1073. * loop (pragmas and attributes don't work for some resason) without globally
  1074. * disabling SSE4.1.
  1075. *
  1076. * The reason we want to avoid vectorization is because despite working on
  1077. * 4 integers at a time, there are multiple factors slowing XXH32 down on
  1078. * SSE4:
  1079. * - There's a ridiculous amount of lag from pmulld (10 cycles of latency on
  1080. * newer chips!) making it slightly slower to multiply four integers at
  1081. * once compared to four integers independently. Even when pmulld was
  1082. * fastest, Sandy/Ivy Bridge, it is still not worth it to go into SSE
  1083. * just to multiply unless doing a long operation.
  1084. *
  1085. * - Four instructions are required to rotate,
  1086. * movqda tmp, v // not required with VEX encoding
  1087. * pslld tmp, 13 // tmp <<= 13
  1088. * psrld v, 19 // x >>= 19
  1089. * por v, tmp // x |= tmp
  1090. * compared to one for scalar:
  1091. * roll v, 13 // reliably fast across the board
  1092. * shldl v, v, 13 // Sandy Bridge and later prefer this for some reason
  1093. *
  1094. * - Instruction level parallelism is actually more beneficial here because
  1095. * the SIMD actually serializes this operation: While v1 is rotating, v2
  1096. * can load data, while v3 can multiply. SSE forces them to operate
  1097. * together.
  1098. *
  1099. * How this hack works:
  1100. * __asm__("" // Declare an assembly block but don't declare any instructions
  1101. * : // However, as an Input/Output Operand,
  1102. * "+r" // constrain a read/write operand (+) as a general purpose register (r).
  1103. * (acc) // and set acc as the operand
  1104. * );
  1105. *
  1106. * Because of the 'r', the compiler has promised that seed will be in a
  1107. * general purpose register and the '+' says that it will be 'read/write',
  1108. * so it has to assume it has changed. It is like volatile without all the
  1109. * loads and stores.
  1110. *
  1111. * Since the argument has to be in a normal register (not an SSE register),
  1112. * each time XXH32_round is called, it is impossible to vectorize.
  1113. */
  1114. __asm__("" : "+r" (acc));
  1115. #endif
  1116. return acc;
  1117. }
  1118. /* mix all bits */
  1119. static xxh_u32 XXH32_avalanche(xxh_u32 h32)
  1120. {
  1121. h32 ^= h32 >> 15;
  1122. h32 *= XXH_PRIME32_2;
  1123. h32 ^= h32 >> 13;
  1124. h32 *= XXH_PRIME32_3;
  1125. h32 ^= h32 >> 16;
  1126. return(h32);
  1127. }
  1128. #define XXH_get32bits(p) XXH_readLE32_align(p, align)
  1129. static xxh_u32
  1130. XXH32_finalize(xxh_u32 h32, const xxh_u8* ptr, size_t len, XXH_alignment align)
  1131. {
  1132. #define XXH_PROCESS1 do { \
  1133. h32 += (*ptr++) * XXH_PRIME32_5; \
  1134. h32 = XXH_rotl32(h32, 11) * XXH_PRIME32_1; \
  1135. } while (0)
  1136. #define XXH_PROCESS4 do { \
  1137. h32 += XXH_get32bits(ptr) * XXH_PRIME32_3; \
  1138. ptr += 4; \
  1139. h32 = XXH_rotl32(h32, 17) * XXH_PRIME32_4; \
  1140. } while (0)
  1141. /* Compact rerolled version */
  1142. if (XXH_REROLL) {
  1143. len &= 15;
  1144. while (len >= 4) {
  1145. XXH_PROCESS4;
  1146. len -= 4;
  1147. }
  1148. while (len > 0) {
  1149. XXH_PROCESS1;
  1150. --len;
  1151. }
  1152. return XXH32_avalanche(h32);
  1153. } else {
  1154. switch(len&15) /* or switch(bEnd - p) */ {
  1155. case 12: XXH_PROCESS4;
  1156. /* fallthrough */
  1157. case 8: XXH_PROCESS4;
  1158. /* fallthrough */
  1159. case 4: XXH_PROCESS4;
  1160. return XXH32_avalanche(h32);
  1161. case 13: XXH_PROCESS4;
  1162. /* fallthrough */
  1163. case 9: XXH_PROCESS4;
  1164. /* fallthrough */
  1165. case 5: XXH_PROCESS4;
  1166. XXH_PROCESS1;
  1167. return XXH32_avalanche(h32);
  1168. case 14: XXH_PROCESS4;
  1169. /* fallthrough */
  1170. case 10: XXH_PROCESS4;
  1171. /* fallthrough */
  1172. case 6: XXH_PROCESS4;
  1173. XXH_PROCESS1;
  1174. XXH_PROCESS1;
  1175. return XXH32_avalanche(h32);
  1176. case 15: XXH_PROCESS4;
  1177. /* fallthrough */
  1178. case 11: XXH_PROCESS4;
  1179. /* fallthrough */
  1180. case 7: XXH_PROCESS4;
  1181. /* fallthrough */
  1182. case 3: XXH_PROCESS1;
  1183. /* fallthrough */
  1184. case 2: XXH_PROCESS1;
  1185. /* fallthrough */
  1186. case 1: XXH_PROCESS1;
  1187. /* fallthrough */
  1188. case 0: return XXH32_avalanche(h32);
  1189. }
  1190. XXH_ASSERT(0);
  1191. return h32; /* reaching this point is deemed impossible */
  1192. }
  1193. }
  1194. #ifdef XXH_OLD_NAMES
  1195. # define PROCESS1 XXH_PROCESS1
  1196. # define PROCESS4 XXH_PROCESS4
  1197. #else
  1198. # undef XXH_PROCESS1
  1199. # undef XXH_PROCESS4
  1200. #endif
  1201. XXH_FORCE_INLINE xxh_u32
  1202. XXH32_endian_align(const xxh_u8* input, size_t len, xxh_u32 seed, XXH_alignment align)
  1203. {
  1204. const xxh_u8* bEnd = input + len;
  1205. xxh_u32 h32;
  1206. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  1207. if (input==NULL) {
  1208. len=0;
  1209. bEnd=input=(const xxh_u8*)(size_t)16;
  1210. }
  1211. #endif
  1212. if (len>=16) {
  1213. const xxh_u8* const limit = bEnd - 15;
  1214. xxh_u32 v1 = seed + XXH_PRIME32_1 + XXH_PRIME32_2;
  1215. xxh_u32 v2 = seed + XXH_PRIME32_2;
  1216. xxh_u32 v3 = seed + 0;
  1217. xxh_u32 v4 = seed - XXH_PRIME32_1;
  1218. do {
  1219. v1 = XXH32_round(v1, XXH_get32bits(input)); input += 4;
  1220. v2 = XXH32_round(v2, XXH_get32bits(input)); input += 4;
  1221. v3 = XXH32_round(v3, XXH_get32bits(input)); input += 4;
  1222. v4 = XXH32_round(v4, XXH_get32bits(input)); input += 4;
  1223. } while (input < limit);
  1224. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7)
  1225. + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  1226. } else {
  1227. h32 = seed + XXH_PRIME32_5;
  1228. }
  1229. h32 += (xxh_u32)len;
  1230. return XXH32_finalize(h32, input, len&15, align);
  1231. }
  1232. XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t len, XXH32_hash_t seed)
  1233. {
  1234. #if 0
  1235. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  1236. XXH32_state_t state;
  1237. XXH32_reset(&state, seed);
  1238. XXH32_update(&state, (const xxh_u8*)input, len);
  1239. return XXH32_digest(&state);
  1240. #else
  1241. if (XXH_FORCE_ALIGN_CHECK) {
  1242. if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
  1243. return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
  1244. } }
  1245. return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
  1246. #endif
  1247. }
  1248. /******* Hash streaming *******/
  1249. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)
  1250. {
  1251. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  1252. }
  1253. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  1254. {
  1255. XXH_free(statePtr);
  1256. return XXH_OK;
  1257. }
  1258. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dstState, const XXH32_state_t* srcState)
  1259. {
  1260. memcpy(dstState, srcState, sizeof(*dstState));
  1261. }
  1262. XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, XXH32_hash_t seed)
  1263. {
  1264. XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  1265. memset(&state, 0, sizeof(state));
  1266. state.v1 = seed + XXH_PRIME32_1 + XXH_PRIME32_2;
  1267. state.v2 = seed + XXH_PRIME32_2;
  1268. state.v3 = seed + 0;
  1269. state.v4 = seed - XXH_PRIME32_1;
  1270. /* do not write into reserved, planned to be removed in a future version */
  1271. memcpy(statePtr, &state, sizeof(state) - sizeof(state.reserved));
  1272. return XXH_OK;
  1273. }
  1274. XXH_PUBLIC_API XXH_errorcode
  1275. XXH32_update(XXH32_state_t* state, const void* input, size_t len)
  1276. {
  1277. if (input==NULL)
  1278. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  1279. return XXH_OK;
  1280. #else
  1281. return XXH_ERROR;
  1282. #endif
  1283. { const xxh_u8* p = (const xxh_u8*)input;
  1284. const xxh_u8* const bEnd = p + len;
  1285. state->total_len_32 += (XXH32_hash_t)len;
  1286. state->large_len |= (XXH32_hash_t)((len>=16) | (state->total_len_32>=16));
  1287. if (state->memsize + len < 16) { /* fill in tmp buffer */
  1288. XXH_memcpy((xxh_u8*)(state->mem32) + state->memsize, input, len);
  1289. state->memsize += (XXH32_hash_t)len;
  1290. return XXH_OK;
  1291. }
  1292. if (state->memsize) { /* some data left from previous update */
  1293. XXH_memcpy((xxh_u8*)(state->mem32) + state->memsize, input, 16-state->memsize);
  1294. { const xxh_u32* p32 = state->mem32;
  1295. state->v1 = XXH32_round(state->v1, XXH_readLE32(p32)); p32++;
  1296. state->v2 = XXH32_round(state->v2, XXH_readLE32(p32)); p32++;
  1297. state->v3 = XXH32_round(state->v3, XXH_readLE32(p32)); p32++;
  1298. state->v4 = XXH32_round(state->v4, XXH_readLE32(p32));
  1299. }
  1300. p += 16-state->memsize;
  1301. state->memsize = 0;
  1302. }
  1303. if (p <= bEnd-16) {
  1304. const xxh_u8* const limit = bEnd - 16;
  1305. xxh_u32 v1 = state->v1;
  1306. xxh_u32 v2 = state->v2;
  1307. xxh_u32 v3 = state->v3;
  1308. xxh_u32 v4 = state->v4;
  1309. do {
  1310. v1 = XXH32_round(v1, XXH_readLE32(p)); p+=4;
  1311. v2 = XXH32_round(v2, XXH_readLE32(p)); p+=4;
  1312. v3 = XXH32_round(v3, XXH_readLE32(p)); p+=4;
  1313. v4 = XXH32_round(v4, XXH_readLE32(p)); p+=4;
  1314. } while (p<=limit);
  1315. state->v1 = v1;
  1316. state->v2 = v2;
  1317. state->v3 = v3;
  1318. state->v4 = v4;
  1319. }
  1320. if (p < bEnd) {
  1321. XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
  1322. state->memsize = (unsigned)(bEnd-p);
  1323. }
  1324. }
  1325. return XXH_OK;
  1326. }
  1327. XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* state)
  1328. {
  1329. xxh_u32 h32;
  1330. if (state->large_len) {
  1331. h32 = XXH_rotl32(state->v1, 1)
  1332. + XXH_rotl32(state->v2, 7)
  1333. + XXH_rotl32(state->v3, 12)
  1334. + XXH_rotl32(state->v4, 18);
  1335. } else {
  1336. h32 = state->v3 /* == seed */ + XXH_PRIME32_5;
  1337. }
  1338. h32 += state->total_len_32;
  1339. return XXH32_finalize(h32, (const xxh_u8*)state->mem32, state->memsize, XXH_aligned);
  1340. }
  1341. /******* Canonical representation *******/
  1342. /*
  1343. * The default return values from XXH functions are unsigned 32 and 64 bit
  1344. * integers.
  1345. *
  1346. * The canonical representation uses big endian convention, the same convention
  1347. * as human-readable numbers (large digits first).
  1348. *
  1349. * This way, hash values can be written into a file or buffer, remaining
  1350. * comparable across different systems.
  1351. *
  1352. * The following functions allow transformation of hash values to and from their
  1353. * canonical format.
  1354. */
  1355. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)
  1356. {
  1357. XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
  1358. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
  1359. memcpy(dst, &hash, sizeof(*dst));
  1360. }
  1361. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
  1362. {
  1363. return XXH_readBE32(src);
  1364. }
  1365. #ifndef XXH_NO_LONG_LONG
  1366. /* *******************************************************************
  1367. * 64-bit hash functions
  1368. *********************************************************************/
  1369. /******* Memory access *******/
  1370. typedef XXH64_hash_t xxh_u64;
  1371. #ifdef XXH_OLD_NAMES
  1372. # define U64 xxh_u64
  1373. #endif
  1374. /*!
  1375. * XXH_REROLL_XXH64:
  1376. * Whether to reroll the XXH64_finalize() loop.
  1377. *
  1378. * Just like XXH32, we can unroll the XXH64_finalize() loop. This can be a
  1379. * performance gain on 64-bit hosts, as only one jump is required.
  1380. *
  1381. * However, on 32-bit hosts, because arithmetic needs to be done with two 32-bit
  1382. * registers, and 64-bit arithmetic needs to be simulated, it isn't beneficial
  1383. * to unroll. The code becomes ridiculously large (the largest function in the
  1384. * binary on i386!), and rerolling it saves anywhere from 3kB to 20kB. It is
  1385. * also slightly faster because it fits into cache better and is more likely
  1386. * to be inlined by the compiler.
  1387. *
  1388. * If XXH_REROLL is defined, this is ignored and the loop is always rerolled.
  1389. */
  1390. #ifndef XXH_REROLL_XXH64
  1391. # if (defined(__ILP32__) || defined(_ILP32)) /* ILP32 is often defined on 32-bit GCC family */ \
  1392. || !(defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) /* x86-64 */ \
  1393. || defined(_M_ARM64) || defined(__aarch64__) || defined(__arm64__) /* aarch64 */ \
  1394. || defined(__PPC64__) || defined(__PPC64LE__) || defined(__ppc64__) || defined(__powerpc64__) /* ppc64 */ \
  1395. || defined(__mips64__) || defined(__mips64)) /* mips64 */ \
  1396. || (!defined(SIZE_MAX) || SIZE_MAX < ULLONG_MAX) /* check limits */
  1397. # define XXH_REROLL_XXH64 1
  1398. # else
  1399. # define XXH_REROLL_XXH64 0
  1400. # endif
  1401. #endif /* !defined(XXH_REROLL_XXH64) */
  1402. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
  1403. /*
  1404. * Manual byteshift. Best for old compilers which don't inline memcpy.
  1405. * We actually directly use XXH_readLE64 and XXH_readBE64.
  1406. */
  1407. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  1408. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  1409. static xxh_u64 XXH_read64(const void* memPtr) { return *(const xxh_u64*) memPtr; }
  1410. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  1411. /*
  1412. * __pack instructions are safer, but compiler specific, hence potentially
  1413. * problematic for some compilers.
  1414. *
  1415. * Currently only defined for GCC and ICC.
  1416. */
  1417. #ifdef XXH_OLD_NAMES
  1418. typedef union { xxh_u32 u32; xxh_u64 u64; } __attribute__((packed)) unalign64;
  1419. #endif
  1420. static xxh_u64 XXH_read64(const void* ptr)
  1421. {
  1422. typedef union { xxh_u32 u32; xxh_u64 u64; } __attribute__((packed)) xxh_unalign64;
  1423. return ((const xxh_unalign64*)ptr)->u64;
  1424. }
  1425. #else
  1426. /*
  1427. * Portable and safe solution. Generally efficient.
  1428. * see: https://stackoverflow.com/a/32095106/646947
  1429. */
  1430. static xxh_u64 XXH_read64(const void* memPtr)
  1431. {
  1432. xxh_u64 val;
  1433. memcpy(&val, memPtr, sizeof(val));
  1434. return val;
  1435. }
  1436. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  1437. #if defined(_MSC_VER) /* Visual Studio */
  1438. # define XXH_swap64 _byteswap_uint64
  1439. #elif XXH_GCC_VERSION >= 403
  1440. # define XXH_swap64 __builtin_bswap64
  1441. #else
  1442. static xxh_u64 XXH_swap64 (xxh_u64 x)
  1443. {
  1444. return ((x << 56) & 0xff00000000000000ULL) |
  1445. ((x << 40) & 0x00ff000000000000ULL) |
  1446. ((x << 24) & 0x0000ff0000000000ULL) |
  1447. ((x << 8) & 0x000000ff00000000ULL) |
  1448. ((x >> 8) & 0x00000000ff000000ULL) |
  1449. ((x >> 24) & 0x0000000000ff0000ULL) |
  1450. ((x >> 40) & 0x000000000000ff00ULL) |
  1451. ((x >> 56) & 0x00000000000000ffULL);
  1452. }
  1453. #endif
  1454. /* XXH_FORCE_MEMORY_ACCESS==3 is an endian-independent byteshift load. */
  1455. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
  1456. XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* memPtr)
  1457. {
  1458. const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
  1459. return bytePtr[0]
  1460. | ((xxh_u64)bytePtr[1] << 8)
  1461. | ((xxh_u64)bytePtr[2] << 16)
  1462. | ((xxh_u64)bytePtr[3] << 24)
  1463. | ((xxh_u64)bytePtr[4] << 32)
  1464. | ((xxh_u64)bytePtr[5] << 40)
  1465. | ((xxh_u64)bytePtr[6] << 48)
  1466. | ((xxh_u64)bytePtr[7] << 56);
  1467. }
  1468. XXH_FORCE_INLINE xxh_u64 XXH_readBE64(const void* memPtr)
  1469. {
  1470. const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
  1471. return bytePtr[7]
  1472. | ((xxh_u64)bytePtr[6] << 8)
  1473. | ((xxh_u64)bytePtr[5] << 16)
  1474. | ((xxh_u64)bytePtr[4] << 24)
  1475. | ((xxh_u64)bytePtr[3] << 32)
  1476. | ((xxh_u64)bytePtr[2] << 40)
  1477. | ((xxh_u64)bytePtr[1] << 48)
  1478. | ((xxh_u64)bytePtr[0] << 56);
  1479. }
  1480. #else
  1481. XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* ptr)
  1482. {
  1483. return XXH_CPU_LITTLE_ENDIAN ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
  1484. }
  1485. static xxh_u64 XXH_readBE64(const void* ptr)
  1486. {
  1487. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
  1488. }
  1489. #endif
  1490. XXH_FORCE_INLINE xxh_u64
  1491. XXH_readLE64_align(const void* ptr, XXH_alignment align)
  1492. {
  1493. if (align==XXH_unaligned)
  1494. return XXH_readLE64(ptr);
  1495. else
  1496. return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u64*)ptr : XXH_swap64(*(const xxh_u64*)ptr);
  1497. }
  1498. /******* xxh64 *******/
  1499. static const xxh_u64 XXH_PRIME64_1 = 0x9E3779B185EBCA87ULL; /* 0b1001111000110111011110011011000110000101111010111100101010000111 */
  1500. static const xxh_u64 XXH_PRIME64_2 = 0xC2B2AE3D27D4EB4FULL; /* 0b1100001010110010101011100011110100100111110101001110101101001111 */
  1501. static const xxh_u64 XXH_PRIME64_3 = 0x165667B19E3779F9ULL; /* 0b0001011001010110011001111011000110011110001101110111100111111001 */
  1502. static const xxh_u64 XXH_PRIME64_4 = 0x85EBCA77C2B2AE63ULL; /* 0b1000010111101011110010100111011111000010101100101010111001100011 */
  1503. static const xxh_u64 XXH_PRIME64_5 = 0x27D4EB2F165667C5ULL; /* 0b0010011111010100111010110010111100010110010101100110011111000101 */
  1504. #ifdef XXH_OLD_NAMES
  1505. # define PRIME64_1 XXH_PRIME64_1
  1506. # define PRIME64_2 XXH_PRIME64_2
  1507. # define PRIME64_3 XXH_PRIME64_3
  1508. # define PRIME64_4 XXH_PRIME64_4
  1509. # define PRIME64_5 XXH_PRIME64_5
  1510. #endif
  1511. static xxh_u64 XXH64_round(xxh_u64 acc, xxh_u64 input)
  1512. {
  1513. acc += input * XXH_PRIME64_2;
  1514. acc = XXH_rotl64(acc, 31);
  1515. acc *= XXH_PRIME64_1;
  1516. return acc;
  1517. }
  1518. static xxh_u64 XXH64_mergeRound(xxh_u64 acc, xxh_u64 val)
  1519. {
  1520. val = XXH64_round(0, val);
  1521. acc ^= val;
  1522. acc = acc * XXH_PRIME64_1 + XXH_PRIME64_4;
  1523. return acc;
  1524. }
  1525. static xxh_u64 XXH64_avalanche(xxh_u64 h64)
  1526. {
  1527. h64 ^= h64 >> 33;
  1528. h64 *= XXH_PRIME64_2;
  1529. h64 ^= h64 >> 29;
  1530. h64 *= XXH_PRIME64_3;
  1531. h64 ^= h64 >> 32;
  1532. return h64;
  1533. }
  1534. #define XXH_get64bits(p) XXH_readLE64_align(p, align)
  1535. static xxh_u64
  1536. XXH64_finalize(xxh_u64 h64, const xxh_u8* ptr, size_t len, XXH_alignment align)
  1537. {
  1538. #define XXH_PROCESS1_64 do { \
  1539. h64 ^= (*ptr++) * XXH_PRIME64_5; \
  1540. h64 = XXH_rotl64(h64, 11) * XXH_PRIME64_1; \
  1541. } while (0)
  1542. #define XXH_PROCESS4_64 do { \
  1543. h64 ^= (xxh_u64)(XXH_get32bits(ptr)) * XXH_PRIME64_1; \
  1544. ptr += 4; \
  1545. h64 = XXH_rotl64(h64, 23) * XXH_PRIME64_2 + XXH_PRIME64_3; \
  1546. } while (0)
  1547. #define XXH_PROCESS8_64 do { \
  1548. xxh_u64 const k1 = XXH64_round(0, XXH_get64bits(ptr)); \
  1549. ptr += 8; \
  1550. h64 ^= k1; \
  1551. h64 = XXH_rotl64(h64,27) * XXH_PRIME64_1 + XXH_PRIME64_4; \
  1552. } while (0)
  1553. /* Rerolled version for 32-bit targets is faster and much smaller. */
  1554. if (XXH_REROLL || XXH_REROLL_XXH64) {
  1555. len &= 31;
  1556. while (len >= 8) {
  1557. XXH_PROCESS8_64;
  1558. len -= 8;
  1559. }
  1560. if (len >= 4) {
  1561. XXH_PROCESS4_64;
  1562. len -= 4;
  1563. }
  1564. while (len > 0) {
  1565. XXH_PROCESS1_64;
  1566. --len;
  1567. }
  1568. return XXH64_avalanche(h64);
  1569. } else {
  1570. switch(len & 31) {
  1571. case 24: XXH_PROCESS8_64;
  1572. /* fallthrough */
  1573. case 16: XXH_PROCESS8_64;
  1574. /* fallthrough */
  1575. case 8: XXH_PROCESS8_64;
  1576. return XXH64_avalanche(h64);
  1577. case 28: XXH_PROCESS8_64;
  1578. /* fallthrough */
  1579. case 20: XXH_PROCESS8_64;
  1580. /* fallthrough */
  1581. case 12: XXH_PROCESS8_64;
  1582. /* fallthrough */
  1583. case 4: XXH_PROCESS4_64;
  1584. return XXH64_avalanche(h64);
  1585. case 25: XXH_PROCESS8_64;
  1586. /* fallthrough */
  1587. case 17: XXH_PROCESS8_64;
  1588. /* fallthrough */
  1589. case 9: XXH_PROCESS8_64;
  1590. XXH_PROCESS1_64;
  1591. return XXH64_avalanche(h64);
  1592. case 29: XXH_PROCESS8_64;
  1593. /* fallthrough */
  1594. case 21: XXH_PROCESS8_64;
  1595. /* fallthrough */
  1596. case 13: XXH_PROCESS8_64;
  1597. /* fallthrough */
  1598. case 5: XXH_PROCESS4_64;
  1599. XXH_PROCESS1_64;
  1600. return XXH64_avalanche(h64);
  1601. case 26: XXH_PROCESS8_64;
  1602. /* fallthrough */
  1603. case 18: XXH_PROCESS8_64;
  1604. /* fallthrough */
  1605. case 10: XXH_PROCESS8_64;
  1606. XXH_PROCESS1_64;
  1607. XXH_PROCESS1_64;
  1608. return XXH64_avalanche(h64);
  1609. case 30: XXH_PROCESS8_64;
  1610. /* fallthrough */
  1611. case 22: XXH_PROCESS8_64;
  1612. /* fallthrough */
  1613. case 14: XXH_PROCESS8_64;
  1614. /* fallthrough */
  1615. case 6: XXH_PROCESS4_64;
  1616. XXH_PROCESS1_64;
  1617. XXH_PROCESS1_64;
  1618. return XXH64_avalanche(h64);
  1619. case 27: XXH_PROCESS8_64;
  1620. /* fallthrough */
  1621. case 19: XXH_PROCESS8_64;
  1622. /* fallthrough */
  1623. case 11: XXH_PROCESS8_64;
  1624. XXH_PROCESS1_64;
  1625. XXH_PROCESS1_64;
  1626. XXH_PROCESS1_64;
  1627. return XXH64_avalanche(h64);
  1628. case 31: XXH_PROCESS8_64;
  1629. /* fallthrough */
  1630. case 23: XXH_PROCESS8_64;
  1631. /* fallthrough */
  1632. case 15: XXH_PROCESS8_64;
  1633. /* fallthrough */
  1634. case 7: XXH_PROCESS4_64;
  1635. /* fallthrough */
  1636. case 3: XXH_PROCESS1_64;
  1637. /* fallthrough */
  1638. case 2: XXH_PROCESS1_64;
  1639. /* fallthrough */
  1640. case 1: XXH_PROCESS1_64;
  1641. /* fallthrough */
  1642. case 0: return XXH64_avalanche(h64);
  1643. }
  1644. }
  1645. /* impossible to reach */
  1646. XXH_ASSERT(0);
  1647. return 0; /* unreachable, but some compilers complain without it */
  1648. }
  1649. #ifdef XXH_OLD_NAMES
  1650. # define PROCESS1_64 XXH_PROCESS1_64
  1651. # define PROCESS4_64 XXH_PROCESS4_64
  1652. # define PROCESS8_64 XXH_PROCESS8_64
  1653. #else
  1654. # undef XXH_PROCESS1_64
  1655. # undef XXH_PROCESS4_64
  1656. # undef XXH_PROCESS8_64
  1657. #endif
  1658. XXH_FORCE_INLINE xxh_u64
  1659. XXH64_endian_align(const xxh_u8* input, size_t len, xxh_u64 seed, XXH_alignment align)
  1660. {
  1661. const xxh_u8* bEnd = input + len;
  1662. xxh_u64 h64;
  1663. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  1664. if (input==NULL) {
  1665. len=0;
  1666. bEnd=input=(const xxh_u8*)(size_t)32;
  1667. }
  1668. #endif
  1669. if (len>=32) {
  1670. const xxh_u8* const limit = bEnd - 32;
  1671. xxh_u64 v1 = seed + XXH_PRIME64_1 + XXH_PRIME64_2;
  1672. xxh_u64 v2 = seed + XXH_PRIME64_2;
  1673. xxh_u64 v3 = seed + 0;
  1674. xxh_u64 v4 = seed - XXH_PRIME64_1;
  1675. do {
  1676. v1 = XXH64_round(v1, XXH_get64bits(input)); input+=8;
  1677. v2 = XXH64_round(v2, XXH_get64bits(input)); input+=8;
  1678. v3 = XXH64_round(v3, XXH_get64bits(input)); input+=8;
  1679. v4 = XXH64_round(v4, XXH_get64bits(input)); input+=8;
  1680. } while (input<=limit);
  1681. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  1682. h64 = XXH64_mergeRound(h64, v1);
  1683. h64 = XXH64_mergeRound(h64, v2);
  1684. h64 = XXH64_mergeRound(h64, v3);
  1685. h64 = XXH64_mergeRound(h64, v4);
  1686. } else {
  1687. h64 = seed + XXH_PRIME64_5;
  1688. }
  1689. h64 += (xxh_u64) len;
  1690. return XXH64_finalize(h64, input, len, align);
  1691. }
  1692. XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t len, XXH64_hash_t seed)
  1693. {
  1694. #if 0
  1695. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  1696. XXH64_state_t state;
  1697. XXH64_reset(&state, seed);
  1698. XXH64_update(&state, (const xxh_u8*)input, len);
  1699. return XXH64_digest(&state);
  1700. #else
  1701. if (XXH_FORCE_ALIGN_CHECK) {
  1702. if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
  1703. return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
  1704. } }
  1705. return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
  1706. #endif
  1707. }
  1708. /******* Hash Streaming *******/
  1709. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
  1710. {
  1711. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  1712. }
  1713. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  1714. {
  1715. XXH_free(statePtr);
  1716. return XXH_OK;
  1717. }
  1718. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dstState, const XXH64_state_t* srcState)
  1719. {
  1720. memcpy(dstState, srcState, sizeof(*dstState));
  1721. }
  1722. XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, XXH64_hash_t seed)
  1723. {
  1724. XXH64_state_t state; /* use a local state to memcpy() in order to avoid strict-aliasing warnings */
  1725. memset(&state, 0, sizeof(state));
  1726. state.v1 = seed + XXH_PRIME64_1 + XXH_PRIME64_2;
  1727. state.v2 = seed + XXH_PRIME64_2;
  1728. state.v3 = seed + 0;
  1729. state.v4 = seed - XXH_PRIME64_1;
  1730. /* do not write into reserved64, might be removed in a future version */
  1731. memcpy(statePtr, &state, sizeof(state) - sizeof(state.reserved64));
  1732. return XXH_OK;
  1733. }
  1734. XXH_PUBLIC_API XXH_errorcode
  1735. XXH64_update (XXH64_state_t* state, const void* input, size_t len)
  1736. {
  1737. if (input==NULL)
  1738. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  1739. return XXH_OK;
  1740. #else
  1741. return XXH_ERROR;
  1742. #endif
  1743. { const xxh_u8* p = (const xxh_u8*)input;
  1744. const xxh_u8* const bEnd = p + len;
  1745. state->total_len += len;
  1746. if (state->memsize + len < 32) { /* fill in tmp buffer */
  1747. XXH_memcpy(((xxh_u8*)state->mem64) + state->memsize, input, len);
  1748. state->memsize += (xxh_u32)len;
  1749. return XXH_OK;
  1750. }
  1751. if (state->memsize) { /* tmp buffer is full */
  1752. XXH_memcpy(((xxh_u8*)state->mem64) + state->memsize, input, 32-state->memsize);
  1753. state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0));
  1754. state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1));
  1755. state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2));
  1756. state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3));
  1757. p += 32-state->memsize;
  1758. state->memsize = 0;
  1759. }
  1760. if (p+32 <= bEnd) {
  1761. const xxh_u8* const limit = bEnd - 32;
  1762. xxh_u64 v1 = state->v1;
  1763. xxh_u64 v2 = state->v2;
  1764. xxh_u64 v3 = state->v3;
  1765. xxh_u64 v4 = state->v4;
  1766. do {
  1767. v1 = XXH64_round(v1, XXH_readLE64(p)); p+=8;
  1768. v2 = XXH64_round(v2, XXH_readLE64(p)); p+=8;
  1769. v3 = XXH64_round(v3, XXH_readLE64(p)); p+=8;
  1770. v4 = XXH64_round(v4, XXH_readLE64(p)); p+=8;
  1771. } while (p<=limit);
  1772. state->v1 = v1;
  1773. state->v2 = v2;
  1774. state->v3 = v3;
  1775. state->v4 = v4;
  1776. }
  1777. if (p < bEnd) {
  1778. XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
  1779. state->memsize = (unsigned)(bEnd-p);
  1780. }
  1781. }
  1782. return XXH_OK;
  1783. }
  1784. XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* state)
  1785. {
  1786. xxh_u64 h64;
  1787. if (state->total_len >= 32) {
  1788. xxh_u64 const v1 = state->v1;
  1789. xxh_u64 const v2 = state->v2;
  1790. xxh_u64 const v3 = state->v3;
  1791. xxh_u64 const v4 = state->v4;
  1792. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  1793. h64 = XXH64_mergeRound(h64, v1);
  1794. h64 = XXH64_mergeRound(h64, v2);
  1795. h64 = XXH64_mergeRound(h64, v3);
  1796. h64 = XXH64_mergeRound(h64, v4);
  1797. } else {
  1798. h64 = state->v3 /*seed*/ + XXH_PRIME64_5;
  1799. }
  1800. h64 += (xxh_u64) state->total_len;
  1801. return XXH64_finalize(h64, (const xxh_u8*)state->mem64, (size_t)state->total_len, XXH_aligned);
  1802. }
  1803. /******* Canonical representation *******/
  1804. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
  1805. {
  1806. XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
  1807. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
  1808. memcpy(dst, &hash, sizeof(*dst));
  1809. }
  1810. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)
  1811. {
  1812. return XXH_readBE64(src);
  1813. }
  1814. /* *********************************************************************
  1815. * XXH3
  1816. * New generation hash designed for speed on small keys and vectorization
  1817. ************************************************************************ */
  1818. /* === Compiler specifics === */
  1819. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* >= C99 */
  1820. # define XXH_RESTRICT restrict
  1821. #else
  1822. /* Note: it might be useful to define __restrict or __restrict__ for some C++ compilers */
  1823. # define XXH_RESTRICT /* disable */
  1824. #endif
  1825. #if (defined(__GNUC__) && (__GNUC__ >= 3)) \
  1826. || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) \
  1827. || defined(__clang__)
  1828. # define XXH_likely(x) __builtin_expect(x, 1)
  1829. # define XXH_unlikely(x) __builtin_expect(x, 0)
  1830. #else
  1831. # define XXH_likely(x) (x)
  1832. # define XXH_unlikely(x) (x)
  1833. #endif
  1834. #if defined(__GNUC__)
  1835. # if defined(__AVX2__)
  1836. # include <immintrin.h>
  1837. # elif defined(__SSE2__)
  1838. # include <emmintrin.h>
  1839. # elif defined(__ARM_NEON__) || defined(__ARM_NEON)
  1840. # define inline __inline__ /* circumvent a clang bug */
  1841. # include <arm_neon.h>
  1842. # undef inline
  1843. # endif
  1844. #elif defined(_MSC_VER)
  1845. # include <intrin.h>
  1846. #endif
  1847. /*
  1848. * One goal of XXH3 is to make it fast on both 32-bit and 64-bit, while
  1849. * remaining a true 64-bit/128-bit hash function.
  1850. *
  1851. * This is done by prioritizing a subset of 64-bit operations that can be
  1852. * emulated without too many steps on the average 32-bit machine.
  1853. *
  1854. * For example, these two lines seem similar, and run equally fast on 64-bit:
  1855. *
  1856. * xxh_u64 x;
  1857. * x ^= (x >> 47); // good
  1858. * x ^= (x >> 13); // bad
  1859. *
  1860. * However, to a 32-bit machine, there is a major difference.
  1861. *
  1862. * x ^= (x >> 47) looks like this:
  1863. *
  1864. * x.lo ^= (x.hi >> (47 - 32));
  1865. *
  1866. * while x ^= (x >> 13) looks like this:
  1867. *
  1868. * // note: funnel shifts are not usually cheap.
  1869. * x.lo ^= (x.lo >> 13) | (x.hi << (32 - 13));
  1870. * x.hi ^= (x.hi >> 13);
  1871. *
  1872. * The first one is significantly faster than the second, simply because the
  1873. * shift is larger than 32. This means:
  1874. * - All the bits we need are in the upper 32 bits, so we can ignore the lower
  1875. * 32 bits in the shift.
  1876. * - The shift result will always fit in the lower 32 bits, and therefore,
  1877. * we can ignore the upper 32 bits in the xor.
  1878. *
  1879. * Thanks to this optimization, XXH3 only requires these features to be efficient:
  1880. *
  1881. * - Usable unaligned access
  1882. * - A 32-bit or 64-bit ALU
  1883. * - If 32-bit, a decent ADC instruction
  1884. * - A 32 or 64-bit multiply with a 64-bit result
  1885. * - For the 128-bit variant, a decent byteswap helps short inputs.
  1886. *
  1887. * The first two are already required by XXH32, and almost all 32-bit and 64-bit
  1888. * platforms which can run XXH32 can run XXH3 efficiently.
  1889. *
  1890. * Thumb-1, the classic 16-bit only subset of ARM's instruction set, is one
  1891. * notable exception.
  1892. *
  1893. * First of all, Thumb-1 lacks support for the UMULL instruction which
  1894. * performs the important long multiply. This means numerous __aeabi_lmul
  1895. * calls.
  1896. *
  1897. * Second of all, the 8 functional registers are just not enough.
  1898. * Setup for __aeabi_lmul, byteshift loads, pointers, and all arithmetic need
  1899. * Lo registers, and this shuffling results in thousands more MOVs than A32.
  1900. *
  1901. * A32 and T32 don't have this limitation. They can access all 14 registers,
  1902. * do a 32->64 multiply with UMULL, and the flexible operand allowing free
  1903. * shifts is helpful, too.
  1904. *
  1905. * Therefore, we do a quick sanity check.
  1906. *
  1907. * If compiling Thumb-1 for a target which supports ARM instructions, we will
  1908. * emit a warning, as it is not a "sane" platform to compile for.
  1909. *
  1910. * Usually, if this happens, it is because of an accident and you probably need
  1911. * to specify -march, as you likely meant to compile for a newer architecture.
  1912. *
  1913. * Credit: large sections of the vectorial and asm source code paths
  1914. * have been contributed by @easyaspi314
  1915. */
  1916. #if defined(__thumb__) && !defined(__thumb2__) && defined(__ARM_ARCH_ISA_ARM)
  1917. # warning "XXH3 is highly inefficient without ARM or Thumb-2."
  1918. #endif
  1919. /* ==========================================
  1920. * Vectorization detection
  1921. * ========================================== */
  1922. #define XXH_SCALAR 0 /* Portable scalar version */
  1923. #define XXH_SSE2 1 /* SSE2 for Pentium 4 and all x86_64 */
  1924. #define XXH_AVX2 2 /* AVX2 for Haswell and Bulldozer */
  1925. #define XXH_AVX512 3 /* AVX512 for Skylake and Icelake */
  1926. #define XXH_NEON 4 /* NEON for most ARMv7-A and all AArch64 */
  1927. #define XXH_VSX 5 /* VSX and ZVector for POWER8/z13 */
  1928. #ifndef XXH_VECTOR /* can be defined on command line */
  1929. # if defined(__AVX512F__)
  1930. # define XXH_VECTOR XXH_AVX512
  1931. # elif defined(__AVX2__)
  1932. # define XXH_VECTOR XXH_AVX2
  1933. # elif defined(__SSE2__) || defined(_M_AMD64) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP == 2))
  1934. # define XXH_VECTOR XXH_SSE2
  1935. # elif defined(__GNUC__) /* msvc support maybe later */ \
  1936. && (defined(__ARM_NEON__) || defined(__ARM_NEON)) \
  1937. && (defined(__LITTLE_ENDIAN__) /* We only support little endian NEON */ \
  1938. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  1939. # define XXH_VECTOR XXH_NEON
  1940. # elif (defined(__PPC64__) && defined(__POWER8_VECTOR__)) \
  1941. || (defined(__s390x__) && defined(__VEC__)) \
  1942. && defined(__GNUC__) /* TODO: IBM XL */
  1943. # define XXH_VECTOR XXH_VSX
  1944. # else
  1945. # define XXH_VECTOR XXH_SCALAR
  1946. # endif
  1947. #endif
  1948. /*
  1949. * Controls the alignment of the accumulator,
  1950. * for compatibility with aligned vector loads, which are usually faster.
  1951. */
  1952. #ifndef XXH_ACC_ALIGN
  1953. # if defined(XXH_X86DISPATCH)
  1954. # define XXH_ACC_ALIGN 64 /* for compatibility with avx512 */
  1955. # elif XXH_VECTOR == XXH_SCALAR /* scalar */
  1956. # define XXH_ACC_ALIGN 8
  1957. # elif XXH_VECTOR == XXH_SSE2 /* sse2 */
  1958. # define XXH_ACC_ALIGN 16
  1959. # elif XXH_VECTOR == XXH_AVX2 /* avx2 */
  1960. # define XXH_ACC_ALIGN 32
  1961. # elif XXH_VECTOR == XXH_NEON /* neon */
  1962. # define XXH_ACC_ALIGN 16
  1963. # elif XXH_VECTOR == XXH_VSX /* vsx */
  1964. # define XXH_ACC_ALIGN 16
  1965. # elif XXH_VECTOR == XXH_AVX512 /* avx512 */
  1966. # define XXH_ACC_ALIGN 64
  1967. # endif
  1968. #endif
  1969. #if defined(XXH_X86DISPATCH) || XXH_VECTOR == XXH_SSE2 \
  1970. || XXH_VECTOR == XXH_AVX2 || XXH_VECTOR == XXH_AVX512
  1971. # define XXH_SEC_ALIGN XXH_ACC_ALIGN
  1972. #else
  1973. # define XXH_SEC_ALIGN 8
  1974. #endif
  1975. /*
  1976. * UGLY HACK:
  1977. * GCC usually generates the best code with -O3 for xxHash.
  1978. *
  1979. * However, when targeting AVX2, it is overzealous in its unrolling resulting
  1980. * in code roughly 3/4 the speed of Clang.
  1981. *
  1982. * There are other issues, such as GCC splitting _mm256_loadu_si256 into
  1983. * _mm_loadu_si128 + _mm256_inserti128_si256. This is an optimization which
  1984. * only applies to Sandy and Ivy Bridge... which don't even support AVX2.
  1985. *
  1986. * That is why when compiling the AVX2 version, it is recommended to use either
  1987. * -O2 -mavx2 -march=haswell
  1988. * or
  1989. * -O2 -mavx2 -mno-avx256-split-unaligned-load
  1990. * for decent performance, or to use Clang instead.
  1991. *
  1992. * Fortunately, we can control the first one with a pragma that forces GCC into
  1993. * -O2, but the other one we can't control without "failed to inline always
  1994. * inline function due to target mismatch" warnings.
  1995. */
  1996. #if XXH_VECTOR == XXH_AVX2 /* AVX2 */ \
  1997. && defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
  1998. && defined(__OPTIMIZE__) && !defined(__OPTIMIZE_SIZE__) /* respect -O0 and -Os */
  1999. # pragma GCC push_options
  2000. # pragma GCC optimize("-O2")
  2001. #endif
  2002. #if XXH_VECTOR == XXH_NEON
  2003. /*
  2004. * NEON's setup for vmlal_u32 is a little more complicated than it is on
  2005. * SSE2, AVX2, and VSX.
  2006. *
  2007. * While PMULUDQ and VMULEUW both perform a mask, VMLAL.U32 performs an upcast.
  2008. *
  2009. * To do the same operation, the 128-bit 'Q' register needs to be split into
  2010. * two 64-bit 'D' registers, performing this operation::
  2011. *
  2012. * [ a | b ]
  2013. * | '---------. .--------' |
  2014. * | x |
  2015. * | .---------' '--------. |
  2016. * [ a & 0xFFFFFFFF | b & 0xFFFFFFFF ],[ a >> 32 | b >> 32 ]
  2017. *
  2018. * Due to significant changes in aarch64, the fastest method for aarch64 is
  2019. * completely different than the fastest method for ARMv7-A.
  2020. *
  2021. * ARMv7-A treats D registers as unions overlaying Q registers, so modifying
  2022. * D11 will modify the high half of Q5. This is similar to how modifying AH
  2023. * will only affect bits 8-15 of AX on x86.
  2024. *
  2025. * VZIP takes two registers, and puts even lanes in one register and odd lanes
  2026. * in the other.
  2027. *
  2028. * On ARMv7-A, this strangely modifies both parameters in place instead of
  2029. * taking the usual 3-operand form.
  2030. *
  2031. * Therefore, if we want to do this, we can simply use a D-form VZIP.32 on the
  2032. * lower and upper halves of the Q register to end up with the high and low
  2033. * halves where we want - all in one instruction.
  2034. *
  2035. * vzip.32 d10, d11 @ d10 = { d10[0], d11[0] }; d11 = { d10[1], d11[1] }
  2036. *
  2037. * Unfortunately we need inline assembly for this: Instructions modifying two
  2038. * registers at once is not possible in GCC or Clang's IR, and they have to
  2039. * create a copy.
  2040. *
  2041. * aarch64 requires a different approach.
  2042. *
  2043. * In order to make it easier to write a decent compiler for aarch64, many
  2044. * quirks were removed, such as conditional execution.
  2045. *
  2046. * NEON was also affected by this.
  2047. *
  2048. * aarch64 cannot access the high bits of a Q-form register, and writes to a
  2049. * D-form register zero the high bits, similar to how writes to W-form scalar
  2050. * registers (or DWORD registers on x86_64) work.
  2051. *
  2052. * The formerly free vget_high intrinsics now require a vext (with a few
  2053. * exceptions)
  2054. *
  2055. * Additionally, VZIP was replaced by ZIP1 and ZIP2, which are the equivalent
  2056. * of PUNPCKL* and PUNPCKH* in SSE, respectively, in order to only modify one
  2057. * operand.
  2058. *
  2059. * The equivalent of the VZIP.32 on the lower and upper halves would be this
  2060. * mess:
  2061. *
  2062. * ext v2.4s, v0.4s, v0.4s, #2 // v2 = { v0[2], v0[3], v0[0], v0[1] }
  2063. * zip1 v1.2s, v0.2s, v2.2s // v1 = { v0[0], v2[0] }
  2064. * zip2 v0.2s, v0.2s, v1.2s // v0 = { v0[1], v2[1] }
  2065. *
  2066. * Instead, we use a literal downcast, vmovn_u64 (XTN), and vshrn_n_u64 (SHRN):
  2067. *
  2068. * shrn v1.2s, v0.2d, #32 // v1 = (uint32x2_t)(v0 >> 32);
  2069. * xtn v0.2s, v0.2d // v0 = (uint32x2_t)(v0 & 0xFFFFFFFF);
  2070. *
  2071. * This is available on ARMv7-A, but is less efficient than a single VZIP.32.
  2072. */
  2073. /*
  2074. * Function-like macro:
  2075. * void XXH_SPLIT_IN_PLACE(uint64x2_t &in, uint32x2_t &outLo, uint32x2_t &outHi)
  2076. * {
  2077. * outLo = (uint32x2_t)(in & 0xFFFFFFFF);
  2078. * outHi = (uint32x2_t)(in >> 32);
  2079. * in = UNDEFINED;
  2080. * }
  2081. */
  2082. # if !defined(XXH_NO_VZIP_HACK) /* define to disable */ \
  2083. && defined(__GNUC__) \
  2084. && !defined(__aarch64__) && !defined(__arm64__)
  2085. # define XXH_SPLIT_IN_PLACE(in, outLo, outHi) \
  2086. do { \
  2087. /* Undocumented GCC/Clang operand modifier: %e0 = lower D half, %f0 = upper D half */ \
  2088. /* https://github.com/gcc-mirror/gcc/blob/38cf91e5/gcc/config/arm/arm.c#L22486 */ \
  2089. /* https://github.com/llvm-mirror/llvm/blob/2c4ca683/lib/Target/ARM/ARMAsmPrinter.cpp#L399 */ \
  2090. __asm__("vzip.32 %e0, %f0" : "+w" (in)); \
  2091. (outLo) = vget_low_u32 (vreinterpretq_u32_u64(in)); \
  2092. (outHi) = vget_high_u32(vreinterpretq_u32_u64(in)); \
  2093. } while (0)
  2094. # else
  2095. # define XXH_SPLIT_IN_PLACE(in, outLo, outHi) \
  2096. do { \
  2097. (outLo) = vmovn_u64 (in); \
  2098. (outHi) = vshrn_n_u64 ((in), 32); \
  2099. } while (0)
  2100. # endif
  2101. #endif /* XXH_VECTOR == XXH_NEON */
  2102. /*
  2103. * VSX and Z Vector helpers.
  2104. *
  2105. * This is very messy, and any pull requests to clean this up are welcome.
  2106. *
  2107. * There are a lot of problems with supporting VSX and s390x, due to
  2108. * inconsistent intrinsics, spotty coverage, and multiple endiannesses.
  2109. */
  2110. #if XXH_VECTOR == XXH_VSX
  2111. # if defined(__s390x__)
  2112. # include <s390intrin.h>
  2113. # else
  2114. /* gcc's altivec.h can have the unwanted consequence to unconditionally
  2115. * #define bool, vector, and pixel keywords,
  2116. * with bad consequences for programs already using these keywords for other purposes.
  2117. * The paragraph defining these macros is skipped when __APPLE_ALTIVEC__ is defined.
  2118. * __APPLE_ALTIVEC__ is _generally_ defined automatically by the compiler,
  2119. * but it seems that, in some cases, it isn't.
  2120. * Force the build macro to be defined, so that keywords are not altered.
  2121. */
  2122. # if defined(__GNUC__) && !defined(__APPLE_ALTIVEC__)
  2123. # define __APPLE_ALTIVEC__
  2124. # endif
  2125. # include <altivec.h>
  2126. # endif
  2127. typedef __vector unsigned long long xxh_u64x2;
  2128. typedef __vector unsigned char xxh_u8x16;
  2129. typedef __vector unsigned xxh_u32x4;
  2130. # ifndef XXH_VSX_BE
  2131. # if defined(__BIG_ENDIAN__) \
  2132. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  2133. # define XXH_VSX_BE 1
  2134. # elif defined(__VEC_ELEMENT_REG_ORDER__) && __VEC_ELEMENT_REG_ORDER__ == __ORDER_BIG_ENDIAN__
  2135. # warning "-maltivec=be is not recommended. Please use native endianness."
  2136. # define XXH_VSX_BE 1
  2137. # else
  2138. # define XXH_VSX_BE 0
  2139. # endif
  2140. # endif /* !defined(XXH_VSX_BE) */
  2141. # if XXH_VSX_BE
  2142. /* A wrapper for POWER9's vec_revb. */
  2143. # if defined(__POWER9_VECTOR__) || (defined(__clang__) && defined(__s390x__))
  2144. # define XXH_vec_revb vec_revb
  2145. # else
  2146. XXH_FORCE_INLINE xxh_u64x2 XXH_vec_revb(xxh_u64x2 val)
  2147. {
  2148. xxh_u8x16 const vByteSwap = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
  2149. 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08 };
  2150. return vec_perm(val, val, vByteSwap);
  2151. }
  2152. # endif
  2153. # endif /* XXH_VSX_BE */
  2154. /*
  2155. * Performs an unaligned load and byte swaps it on big endian.
  2156. */
  2157. XXH_FORCE_INLINE xxh_u64x2 XXH_vec_loadu(const void *ptr)
  2158. {
  2159. xxh_u64x2 ret;
  2160. memcpy(&ret, ptr, sizeof(xxh_u64x2));
  2161. # if XXH_VSX_BE
  2162. ret = XXH_vec_revb(ret);
  2163. # endif
  2164. return ret;
  2165. }
  2166. /*
  2167. * vec_mulo and vec_mule are very problematic intrinsics on PowerPC
  2168. *
  2169. * These intrinsics weren't added until GCC 8, despite existing for a while,
  2170. * and they are endian dependent. Also, their meaning swap depending on version.
  2171. * */
  2172. # if defined(__s390x__)
  2173. /* s390x is always big endian, no issue on this platform */
  2174. # define XXH_vec_mulo vec_mulo
  2175. # define XXH_vec_mule vec_mule
  2176. # elif defined(__clang__) && XXH_HAS_BUILTIN(__builtin_altivec_vmuleuw)
  2177. /* Clang has a better way to control this, we can just use the builtin which doesn't swap. */
  2178. # define XXH_vec_mulo __builtin_altivec_vmulouw
  2179. # define XXH_vec_mule __builtin_altivec_vmuleuw
  2180. # else
  2181. /* gcc needs inline assembly */
  2182. /* Adapted from https://github.com/google/highwayhash/blob/master/highwayhash/hh_vsx.h. */
  2183. XXH_FORCE_INLINE xxh_u64x2 XXH_vec_mulo(xxh_u32x4 a, xxh_u32x4 b)
  2184. {
  2185. xxh_u64x2 result;
  2186. __asm__("vmulouw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
  2187. return result;
  2188. }
  2189. XXH_FORCE_INLINE xxh_u64x2 XXH_vec_mule(xxh_u32x4 a, xxh_u32x4 b)
  2190. {
  2191. xxh_u64x2 result;
  2192. __asm__("vmuleuw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
  2193. return result;
  2194. }
  2195. # endif /* XXH_vec_mulo, XXH_vec_mule */
  2196. #endif /* XXH_VECTOR == XXH_VSX */
  2197. /* prefetch
  2198. * can be disabled, by declaring XXH_NO_PREFETCH build macro */
  2199. #if defined(XXH_NO_PREFETCH)
  2200. # define XXH_PREFETCH(ptr) (void)(ptr) /* disabled */
  2201. #else
  2202. # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
  2203. # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
  2204. # define XXH_PREFETCH(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
  2205. # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  2206. # define XXH_PREFETCH(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  2207. # else
  2208. # define XXH_PREFETCH(ptr) (void)(ptr) /* disabled */
  2209. # endif
  2210. #endif /* XXH_NO_PREFETCH */
  2211. /* ==========================================
  2212. * XXH3 default settings
  2213. * ========================================== */
  2214. #define XXH_SECRET_DEFAULT_SIZE 192 /* minimum XXH3_SECRET_SIZE_MIN */
  2215. #if (XXH_SECRET_DEFAULT_SIZE < XXH3_SECRET_SIZE_MIN)
  2216. # error "default keyset is not large enough"
  2217. #endif
  2218. /* Pseudorandom secret taken directly from FARSH */
  2219. XXH_ALIGN(64) static const xxh_u8 XXH3_kSecret[XXH_SECRET_DEFAULT_SIZE] = {
  2220. 0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c,
  2221. 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f,
  2222. 0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21,
  2223. 0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c,
  2224. 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3,
  2225. 0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8,
  2226. 0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d,
  2227. 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64,
  2228. 0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb,
  2229. 0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e,
  2230. 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce,
  2231. 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e,
  2232. };
  2233. #ifdef XXH_OLD_NAMES
  2234. # define kSecret XXH3_kSecret
  2235. #endif
  2236. /*
  2237. * Calculates a 32-bit to 64-bit long multiply.
  2238. *
  2239. * Wraps __emulu on MSVC x86 because it tends to call __allmul when it doesn't
  2240. * need to (but it shouldn't need to anyways, it is about 7 instructions to do
  2241. * a 64x64 multiply...). Since we know that this will _always_ emit MULL, we
  2242. * use that instead of the normal method.
  2243. *
  2244. * If you are compiling for platforms like Thumb-1 and don't have a better option,
  2245. * you may also want to write your own long multiply routine here.
  2246. *
  2247. * XXH_FORCE_INLINE xxh_u64 XXH_mult32to64(xxh_u64 x, xxh_u64 y)
  2248. * {
  2249. * return (x & 0xFFFFFFFF) * (y & 0xFFFFFFFF);
  2250. * }
  2251. */
  2252. #if defined(_MSC_VER) && defined(_M_IX86)
  2253. # include <intrin.h>
  2254. # define XXH_mult32to64(x, y) __emulu((unsigned)(x), (unsigned)(y))
  2255. #else
  2256. /*
  2257. * Downcast + upcast is usually better than masking on older compilers like
  2258. * GCC 4.2 (especially 32-bit ones), all without affecting newer compilers.
  2259. *
  2260. * The other method, (x & 0xFFFFFFFF) * (y & 0xFFFFFFFF), will AND both operands
  2261. * and perform a full 64x64 multiply -- entirely redundant on 32-bit.
  2262. */
  2263. # define XXH_mult32to64(x, y) ((xxh_u64)(xxh_u32)(x) * (xxh_u64)(xxh_u32)(y))
  2264. #endif
  2265. /*
  2266. * Calculates a 64->128-bit long multiply.
  2267. *
  2268. * Uses __uint128_t and _umul128 if available, otherwise uses a scalar version.
  2269. */
  2270. static XXH128_hash_t
  2271. XXH_mult64to128(xxh_u64 lhs, xxh_u64 rhs)
  2272. {
  2273. /*
  2274. * GCC/Clang __uint128_t method.
  2275. *
  2276. * On most 64-bit targets, GCC and Clang define a __uint128_t type.
  2277. * This is usually the best way as it usually uses a native long 64-bit
  2278. * multiply, such as MULQ on x86_64 or MUL + UMULH on aarch64.
  2279. *
  2280. * Usually.
  2281. *
  2282. * Despite being a 32-bit platform, Clang (and emscripten) define this type
  2283. * despite not having the arithmetic for it. This results in a laggy
  2284. * compiler builtin call which calculates a full 128-bit multiply.
  2285. * In that case it is best to use the portable one.
  2286. * https://github.com/Cyan4973/xxHash/issues/211#issuecomment-515575677
  2287. */
  2288. #if defined(__GNUC__) && !defined(__wasm__) \
  2289. && defined(__SIZEOF_INT128__) \
  2290. || (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  2291. __uint128_t const product = (__uint128_t)lhs * (__uint128_t)rhs;
  2292. XXH128_hash_t r128;
  2293. r128.low64 = (xxh_u64)(product);
  2294. r128.high64 = (xxh_u64)(product >> 64);
  2295. return r128;
  2296. /*
  2297. * MSVC for x64's _umul128 method.
  2298. *
  2299. * xxh_u64 _umul128(xxh_u64 Multiplier, xxh_u64 Multiplicand, xxh_u64 *HighProduct);
  2300. *
  2301. * This compiles to single operand MUL on x64.
  2302. */
  2303. #elif defined(_M_X64) || defined(_M_IA64)
  2304. #ifndef _MSC_VER
  2305. # pragma intrinsic(_umul128)
  2306. #endif
  2307. xxh_u64 product_high;
  2308. xxh_u64 const product_low = _umul128(lhs, rhs, &product_high);
  2309. XXH128_hash_t r128;
  2310. r128.low64 = product_low;
  2311. r128.high64 = product_high;
  2312. return r128;
  2313. #else
  2314. /*
  2315. * Portable scalar method. Optimized for 32-bit and 64-bit ALUs.
  2316. *
  2317. * This is a fast and simple grade school multiply, which is shown below
  2318. * with base 10 arithmetic instead of base 0x100000000.
  2319. *
  2320. * 9 3 // D2 lhs = 93
  2321. * x 7 5 // D2 rhs = 75
  2322. * ----------
  2323. * 1 5 // D2 lo_lo = (93 % 10) * (75 % 10) = 15
  2324. * 4 5 | // D2 hi_lo = (93 / 10) * (75 % 10) = 45
  2325. * 2 1 | // D2 lo_hi = (93 % 10) * (75 / 10) = 21
  2326. * + 6 3 | | // D2 hi_hi = (93 / 10) * (75 / 10) = 63
  2327. * ---------
  2328. * 2 7 | // D2 cross = (15 / 10) + (45 % 10) + 21 = 27
  2329. * + 6 7 | | // D2 upper = (27 / 10) + (45 / 10) + 63 = 67
  2330. * ---------
  2331. * 6 9 7 5 // D4 res = (27 * 10) + (15 % 10) + (67 * 100) = 6975
  2332. *
  2333. * The reasons for adding the products like this are:
  2334. * 1. It avoids manual carry tracking. Just like how
  2335. * (9 * 9) + 9 + 9 = 99, the same applies with this for UINT64_MAX.
  2336. * This avoids a lot of complexity.
  2337. *
  2338. * 2. It hints for, and on Clang, compiles to, the powerful UMAAL
  2339. * instruction available in ARM's Digital Signal Processing extension
  2340. * in 32-bit ARMv6 and later, which is shown below:
  2341. *
  2342. * void UMAAL(xxh_u32 *RdLo, xxh_u32 *RdHi, xxh_u32 Rn, xxh_u32 Rm)
  2343. * {
  2344. * xxh_u64 product = (xxh_u64)*RdLo * (xxh_u64)*RdHi + Rn + Rm;
  2345. * *RdLo = (xxh_u32)(product & 0xFFFFFFFF);
  2346. * *RdHi = (xxh_u32)(product >> 32);
  2347. * }
  2348. *
  2349. * This instruction was designed for efficient long multiplication, and
  2350. * allows this to be calculated in only 4 instructions at speeds
  2351. * comparable to some 64-bit ALUs.
  2352. *
  2353. * 3. It isn't terrible on other platforms. Usually this will be a couple
  2354. * of 32-bit ADD/ADCs.
  2355. */
  2356. /* First calculate all of the cross products. */
  2357. xxh_u64 const lo_lo = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs & 0xFFFFFFFF);
  2358. xxh_u64 const hi_lo = XXH_mult32to64(lhs >> 32, rhs & 0xFFFFFFFF);
  2359. xxh_u64 const lo_hi = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs >> 32);
  2360. xxh_u64 const hi_hi = XXH_mult32to64(lhs >> 32, rhs >> 32);
  2361. /* Now add the products together. These will never overflow. */
  2362. xxh_u64 const cross = (lo_lo >> 32) + (hi_lo & 0xFFFFFFFF) + lo_hi;
  2363. xxh_u64 const upper = (hi_lo >> 32) + (cross >> 32) + hi_hi;
  2364. xxh_u64 const lower = (cross << 32) | (lo_lo & 0xFFFFFFFF);
  2365. XXH128_hash_t r128;
  2366. r128.low64 = lower;
  2367. r128.high64 = upper;
  2368. return r128;
  2369. #endif
  2370. }
  2371. /*
  2372. * Does a 64-bit to 128-bit multiply, then XOR folds it.
  2373. *
  2374. * The reason for the separate function is to prevent passing too many structs
  2375. * around by value. This will hopefully inline the multiply, but we don't force it.
  2376. */
  2377. static xxh_u64
  2378. XXH3_mul128_fold64(xxh_u64 lhs, xxh_u64 rhs)
  2379. {
  2380. XXH128_hash_t product = XXH_mult64to128(lhs, rhs);
  2381. return product.low64 ^ product.high64;
  2382. }
  2383. /* Seems to produce slightly better code on GCC for some reason. */
  2384. XXH_FORCE_INLINE xxh_u64 XXH_xorshift64(xxh_u64 v64, int shift)
  2385. {
  2386. XXH_ASSERT(0 <= shift && shift < 64);
  2387. return v64 ^ (v64 >> shift);
  2388. }
  2389. /*
  2390. * This is a fast avalanche stage,
  2391. * suitable when input bits are already partially mixed
  2392. */
  2393. static XXH64_hash_t XXH3_avalanche(xxh_u64 h64)
  2394. {
  2395. h64 = XXH_xorshift64(h64, 37);
  2396. h64 *= 0x165667919E3779F9ULL;
  2397. h64 = XXH_xorshift64(h64, 32);
  2398. return h64;
  2399. }
  2400. /*
  2401. * This is a stronger avalanche,
  2402. * inspired by Pelle Evensen's rrmxmx
  2403. * preferable when input has not been previously mixed
  2404. */
  2405. static XXH64_hash_t XXH3_rrmxmx(xxh_u64 h64, xxh_u64 len)
  2406. {
  2407. /* this mix is inspired by Pelle Evensen's rrmxmx */
  2408. h64 ^= XXH_rotl64(h64, 49) ^ XXH_rotl64(h64, 24);
  2409. h64 *= 0x9FB21C651E98DF25ULL;
  2410. h64 ^= (h64 >> 35) + len ;
  2411. h64 *= 0x9FB21C651E98DF25ULL;
  2412. return XXH_xorshift64(h64, 28);
  2413. }
  2414. /* ==========================================
  2415. * Short keys
  2416. * ==========================================
  2417. * One of the shortcomings of XXH32 and XXH64 was that their performance was
  2418. * sub-optimal on short lengths. It used an iterative algorithm which strongly
  2419. * favored lengths that were a multiple of 4 or 8.
  2420. *
  2421. * Instead of iterating over individual inputs, we use a set of single shot
  2422. * functions which piece together a range of lengths and operate in constant time.
  2423. *
  2424. * Additionally, the number of multiplies has been significantly reduced. This
  2425. * reduces latency, especially when emulating 64-bit multiplies on 32-bit.
  2426. *
  2427. * Depending on the platform, this may or may not be faster than XXH32, but it
  2428. * is almost guaranteed to be faster than XXH64.
  2429. */
  2430. /*
  2431. * At very short lengths, there isn't enough input to fully hide secrets, or use
  2432. * the entire secret.
  2433. *
  2434. * There is also only a limited amount of mixing we can do before significantly
  2435. * impacting performance.
  2436. *
  2437. * Therefore, we use different sections of the secret and always mix two secret
  2438. * samples with an XOR. This should have no effect on performance on the
  2439. * seedless or withSeed variants because everything _should_ be constant folded
  2440. * by modern compilers.
  2441. *
  2442. * The XOR mixing hides individual parts of the secret and increases entropy.
  2443. *
  2444. * This adds an extra layer of strength for custom secrets.
  2445. */
  2446. XXH_FORCE_INLINE XXH64_hash_t
  2447. XXH3_len_1to3_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  2448. {
  2449. XXH_ASSERT(input != NULL);
  2450. XXH_ASSERT(1 <= len && len <= 3);
  2451. XXH_ASSERT(secret != NULL);
  2452. /*
  2453. * len = 1: combined = { input[0], 0x01, input[0], input[0] }
  2454. * len = 2: combined = { input[1], 0x02, input[0], input[1] }
  2455. * len = 3: combined = { input[2], 0x03, input[0], input[1] }
  2456. */
  2457. { xxh_u8 const c1 = input[0];
  2458. xxh_u8 const c2 = input[len >> 1];
  2459. xxh_u8 const c3 = input[len - 1];
  2460. xxh_u32 const combined = ((xxh_u32)c1 << 16) | ((xxh_u32)c2 << 24)
  2461. | ((xxh_u32)c3 << 0) | ((xxh_u32)len << 8);
  2462. xxh_u64 const bitflip = (XXH_readLE32(secret) ^ XXH_readLE32(secret+4)) + seed;
  2463. xxh_u64 const keyed = (xxh_u64)combined ^ bitflip;
  2464. return XXH64_avalanche(keyed);
  2465. }
  2466. }
  2467. XXH_FORCE_INLINE XXH64_hash_t
  2468. XXH3_len_4to8_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  2469. {
  2470. XXH_ASSERT(input != NULL);
  2471. XXH_ASSERT(secret != NULL);
  2472. XXH_ASSERT(4 <= len && len < 8);
  2473. seed ^= (xxh_u64)XXH_swap32((xxh_u32)seed) << 32;
  2474. { xxh_u32 const input1 = XXH_readLE32(input);
  2475. xxh_u32 const input2 = XXH_readLE32(input + len - 4);
  2476. xxh_u64 const bitflip = (XXH_readLE64(secret+8) ^ XXH_readLE64(secret+16)) - seed;
  2477. xxh_u64 const input64 = input2 + (((xxh_u64)input1) << 32);
  2478. xxh_u64 const keyed = input64 ^ bitflip;
  2479. return XXH3_rrmxmx(keyed, len);
  2480. }
  2481. }
  2482. XXH_FORCE_INLINE XXH64_hash_t
  2483. XXH3_len_9to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  2484. {
  2485. XXH_ASSERT(input != NULL);
  2486. XXH_ASSERT(secret != NULL);
  2487. XXH_ASSERT(8 <= len && len <= 16);
  2488. { xxh_u64 const bitflip1 = (XXH_readLE64(secret+24) ^ XXH_readLE64(secret+32)) + seed;
  2489. xxh_u64 const bitflip2 = (XXH_readLE64(secret+40) ^ XXH_readLE64(secret+48)) - seed;
  2490. xxh_u64 const input_lo = XXH_readLE64(input) ^ bitflip1;
  2491. xxh_u64 const input_hi = XXH_readLE64(input + len - 8) ^ bitflip2;
  2492. xxh_u64 const acc = len
  2493. + XXH_swap64(input_lo) + input_hi
  2494. + XXH3_mul128_fold64(input_lo, input_hi);
  2495. return XXH3_avalanche(acc);
  2496. }
  2497. }
  2498. XXH_FORCE_INLINE XXH64_hash_t
  2499. XXH3_len_0to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  2500. {
  2501. XXH_ASSERT(len <= 16);
  2502. { if (XXH_likely(len > 8)) return XXH3_len_9to16_64b(input, len, secret, seed);
  2503. if (XXH_likely(len >= 4)) return XXH3_len_4to8_64b(input, len, secret, seed);
  2504. if (len) return XXH3_len_1to3_64b(input, len, secret, seed);
  2505. return XXH64_avalanche(seed ^ (XXH_readLE64(secret+56) ^ XXH_readLE64(secret+64)));
  2506. }
  2507. }
  2508. /*
  2509. * DISCLAIMER: There are known *seed-dependent* multicollisions here due to
  2510. * multiplication by zero, affecting hashes of lengths 17 to 240.
  2511. *
  2512. * However, they are very unlikely.
  2513. *
  2514. * Keep this in mind when using the unseeded XXH3_64bits() variant: As with all
  2515. * unseeded non-cryptographic hashes, it does not attempt to defend itself
  2516. * against specially crafted inputs, only random inputs.
  2517. *
  2518. * Compared to classic UMAC where a 1 in 2^31 chance of 4 consecutive bytes
  2519. * cancelling out the secret is taken an arbitrary number of times (addressed
  2520. * in XXH3_accumulate_512), this collision is very unlikely with random inputs
  2521. * and/or proper seeding:
  2522. *
  2523. * This only has a 1 in 2^63 chance of 8 consecutive bytes cancelling out, in a
  2524. * function that is only called up to 16 times per hash with up to 240 bytes of
  2525. * input.
  2526. *
  2527. * This is not too bad for a non-cryptographic hash function, especially with
  2528. * only 64 bit outputs.
  2529. *
  2530. * The 128-bit variant (which trades some speed for strength) is NOT affected
  2531. * by this, although it is always a good idea to use a proper seed if you care
  2532. * about strength.
  2533. */
  2534. XXH_FORCE_INLINE xxh_u64 XXH3_mix16B(const xxh_u8* XXH_RESTRICT input,
  2535. const xxh_u8* XXH_RESTRICT secret, xxh_u64 seed64)
  2536. {
  2537. #if defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
  2538. && defined(__i386__) && defined(__SSE2__) /* x86 + SSE2 */ \
  2539. && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable like XXH32 hack */
  2540. /*
  2541. * UGLY HACK:
  2542. * GCC for x86 tends to autovectorize the 128-bit multiply, resulting in
  2543. * slower code.
  2544. *
  2545. * By forcing seed64 into a register, we disrupt the cost model and
  2546. * cause it to scalarize. See `XXH32_round()`
  2547. *
  2548. * FIXME: Clang's output is still _much_ faster -- On an AMD Ryzen 3600,
  2549. * XXH3_64bits @ len=240 runs at 4.6 GB/s with Clang 9, but 3.3 GB/s on
  2550. * GCC 9.2, despite both emitting scalar code.
  2551. *
  2552. * GCC generates much better scalar code than Clang for the rest of XXH3,
  2553. * which is why finding a more optimal codepath is an interest.
  2554. */
  2555. __asm__ ("" : "+r" (seed64));
  2556. #endif
  2557. { xxh_u64 const input_lo = XXH_readLE64(input);
  2558. xxh_u64 const input_hi = XXH_readLE64(input+8);
  2559. return XXH3_mul128_fold64(
  2560. input_lo ^ (XXH_readLE64(secret) + seed64),
  2561. input_hi ^ (XXH_readLE64(secret+8) - seed64)
  2562. );
  2563. }
  2564. }
  2565. /* For mid range keys, XXH3 uses a Mum-hash variant. */
  2566. XXH_FORCE_INLINE XXH64_hash_t
  2567. XXH3_len_17to128_64b(const xxh_u8* XXH_RESTRICT input, size_t len,
  2568. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  2569. XXH64_hash_t seed)
  2570. {
  2571. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
  2572. XXH_ASSERT(16 < len && len <= 128);
  2573. { xxh_u64 acc = len * XXH_PRIME64_1;
  2574. if (len > 32) {
  2575. if (len > 64) {
  2576. if (len > 96) {
  2577. acc += XXH3_mix16B(input+48, secret+96, seed);
  2578. acc += XXH3_mix16B(input+len-64, secret+112, seed);
  2579. }
  2580. acc += XXH3_mix16B(input+32, secret+64, seed);
  2581. acc += XXH3_mix16B(input+len-48, secret+80, seed);
  2582. }
  2583. acc += XXH3_mix16B(input+16, secret+32, seed);
  2584. acc += XXH3_mix16B(input+len-32, secret+48, seed);
  2585. }
  2586. acc += XXH3_mix16B(input+0, secret+0, seed);
  2587. acc += XXH3_mix16B(input+len-16, secret+16, seed);
  2588. return XXH3_avalanche(acc);
  2589. }
  2590. }
  2591. #define XXH3_MIDSIZE_MAX 240
  2592. XXH_NO_INLINE XXH64_hash_t
  2593. XXH3_len_129to240_64b(const xxh_u8* XXH_RESTRICT input, size_t len,
  2594. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  2595. XXH64_hash_t seed)
  2596. {
  2597. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
  2598. XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
  2599. #define XXH3_MIDSIZE_STARTOFFSET 3
  2600. #define XXH3_MIDSIZE_LASTOFFSET 17
  2601. { xxh_u64 acc = len * XXH_PRIME64_1;
  2602. int const nbRounds = (int)len / 16;
  2603. int i;
  2604. for (i=0; i<8; i++) {
  2605. acc += XXH3_mix16B(input+(16*i), secret+(16*i), seed);
  2606. }
  2607. acc = XXH3_avalanche(acc);
  2608. XXH_ASSERT(nbRounds >= 8);
  2609. #if defined(__clang__) /* Clang */ \
  2610. && (defined(__ARM_NEON) || defined(__ARM_NEON__)) /* NEON */ \
  2611. && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable */
  2612. /*
  2613. * UGLY HACK:
  2614. * Clang for ARMv7-A tries to vectorize this loop, similar to GCC x86.
  2615. * In everywhere else, it uses scalar code.
  2616. *
  2617. * For 64->128-bit multiplies, even if the NEON was 100% optimal, it
  2618. * would still be slower than UMAAL (see XXH_mult64to128).
  2619. *
  2620. * Unfortunately, Clang doesn't handle the long multiplies properly and
  2621. * converts them to the nonexistent "vmulq_u64" intrinsic, which is then
  2622. * scalarized into an ugly mess of VMOV.32 instructions.
  2623. *
  2624. * This mess is difficult to avoid without turning autovectorization
  2625. * off completely, but they are usually relatively minor and/or not
  2626. * worth it to fix.
  2627. *
  2628. * This loop is the easiest to fix, as unlike XXH32, this pragma
  2629. * _actually works_ because it is a loop vectorization instead of an
  2630. * SLP vectorization.
  2631. */
  2632. #pragma clang loop vectorize(disable)
  2633. #endif
  2634. for (i=8 ; i < nbRounds; i++) {
  2635. acc += XXH3_mix16B(input+(16*i), secret+(16*(i-8)) + XXH3_MIDSIZE_STARTOFFSET, seed);
  2636. }
  2637. /* last bytes */
  2638. acc += XXH3_mix16B(input + len - 16, secret + XXH3_SECRET_SIZE_MIN - XXH3_MIDSIZE_LASTOFFSET, seed);
  2639. return XXH3_avalanche(acc);
  2640. }
  2641. }
  2642. /* ======= Long Keys ======= */
  2643. #define XXH_STRIPE_LEN 64
  2644. #define XXH_SECRET_CONSUME_RATE 8 /* nb of secret bytes consumed at each accumulation */
  2645. #define XXH_ACC_NB (XXH_STRIPE_LEN / sizeof(xxh_u64))
  2646. #ifdef XXH_OLD_NAMES
  2647. # define STRIPE_LEN XXH_STRIPE_LEN
  2648. # define ACC_NB XXH_ACC_NB
  2649. #endif
  2650. XXH_FORCE_INLINE void XXH_writeLE64(void* dst, xxh_u64 v64)
  2651. {
  2652. if (!XXH_CPU_LITTLE_ENDIAN) v64 = XXH_swap64(v64);
  2653. memcpy(dst, &v64, sizeof(v64));
  2654. }
  2655. /* Several intrinsic functions below are supposed to accept __int64 as argument,
  2656. * as documented in https://software.intel.com/sites/landingpage/IntrinsicsGuide/ .
  2657. * However, several environments do not define __int64 type,
  2658. * requiring a workaround.
  2659. */
  2660. #if !defined (__VMS) \
  2661. && (defined (__cplusplus) \
  2662. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  2663. typedef int64_t xxh_i64;
  2664. #else
  2665. /* the following type must have a width of 64-bit */
  2666. typedef long long xxh_i64;
  2667. #endif
  2668. /*
  2669. * XXH3_accumulate_512 is the tightest loop for long inputs, and it is the most optimized.
  2670. *
  2671. * It is a hardened version of UMAC, based off of FARSH's implementation.
  2672. *
  2673. * This was chosen because it adapts quite well to 32-bit, 64-bit, and SIMD
  2674. * implementations, and it is ridiculously fast.
  2675. *
  2676. * We harden it by mixing the original input to the accumulators as well as the product.
  2677. *
  2678. * This means that in the (relatively likely) case of a multiply by zero, the
  2679. * original input is preserved.
  2680. *
  2681. * On 128-bit inputs, we swap 64-bit pairs when we add the input to improve
  2682. * cross-pollination, as otherwise the upper and lower halves would be
  2683. * essentially independent.
  2684. *
  2685. * This doesn't matter on 64-bit hashes since they all get merged together in
  2686. * the end, so we skip the extra step.
  2687. *
  2688. * Both XXH3_64bits and XXH3_128bits use this subroutine.
  2689. */
  2690. #if (XXH_VECTOR == XXH_AVX512) || defined(XXH_X86DISPATCH)
  2691. #ifndef XXH_TARGET_AVX512
  2692. # define XXH_TARGET_AVX512 /* disable attribute target */
  2693. #endif
  2694. XXH_FORCE_INLINE XXH_TARGET_AVX512 void
  2695. XXH3_accumulate_512_avx512(void* XXH_RESTRICT acc,
  2696. const void* XXH_RESTRICT input,
  2697. const void* XXH_RESTRICT secret)
  2698. {
  2699. XXH_ALIGN(64) __m512i* const xacc = (__m512i *) acc;
  2700. XXH_ASSERT((((size_t)acc) & 63) == 0);
  2701. XXH_STATIC_ASSERT(XXH_STRIPE_LEN == sizeof(__m512i));
  2702. {
  2703. /* data_vec = input[0]; */
  2704. __m512i const data_vec = _mm512_loadu_si512 (input);
  2705. /* key_vec = secret[0]; */
  2706. __m512i const key_vec = _mm512_loadu_si512 (secret);
  2707. /* data_key = data_vec ^ key_vec; */
  2708. __m512i const data_key = _mm512_xor_si512 (data_vec, key_vec);
  2709. /* data_key_lo = data_key >> 32; */
  2710. __m512i const data_key_lo = _mm512_shuffle_epi32 (data_key, (_MM_PERM_ENUM)_MM_SHUFFLE(0, 3, 0, 1));
  2711. /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
  2712. __m512i const product = _mm512_mul_epu32 (data_key, data_key_lo);
  2713. /* xacc[0] += swap(data_vec); */
  2714. __m512i const data_swap = _mm512_shuffle_epi32(data_vec, (_MM_PERM_ENUM)_MM_SHUFFLE(1, 0, 3, 2));
  2715. __m512i const sum = _mm512_add_epi64(*xacc, data_swap);
  2716. /* xacc[0] += product; */
  2717. *xacc = _mm512_add_epi64(product, sum);
  2718. }
  2719. }
  2720. /*
  2721. * XXH3_scrambleAcc: Scrambles the accumulators to improve mixing.
  2722. *
  2723. * Multiplication isn't perfect, as explained by Google in HighwayHash:
  2724. *
  2725. * // Multiplication mixes/scrambles bytes 0-7 of the 64-bit result to
  2726. * // varying degrees. In descending order of goodness, bytes
  2727. * // 3 4 2 5 1 6 0 7 have quality 228 224 164 160 100 96 36 32.
  2728. * // As expected, the upper and lower bytes are much worse.
  2729. *
  2730. * Source: https://github.com/google/highwayhash/blob/0aaf66b/highwayhash/hh_avx2.h#L291
  2731. *
  2732. * Since our algorithm uses a pseudorandom secret to add some variance into the
  2733. * mix, we don't need to (or want to) mix as often or as much as HighwayHash does.
  2734. *
  2735. * This isn't as tight as XXH3_accumulate, but still written in SIMD to avoid
  2736. * extraction.
  2737. *
  2738. * Both XXH3_64bits and XXH3_128bits use this subroutine.
  2739. */
  2740. XXH_FORCE_INLINE XXH_TARGET_AVX512 void
  2741. XXH3_scrambleAcc_avx512(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  2742. {
  2743. XXH_ASSERT((((size_t)acc) & 63) == 0);
  2744. XXH_STATIC_ASSERT(XXH_STRIPE_LEN == sizeof(__m512i));
  2745. { XXH_ALIGN(64) __m512i* const xacc = (__m512i*) acc;
  2746. const __m512i prime32 = _mm512_set1_epi32((int)XXH_PRIME32_1);
  2747. /* xacc[0] ^= (xacc[0] >> 47) */
  2748. __m512i const acc_vec = *xacc;
  2749. __m512i const shifted = _mm512_srli_epi64 (acc_vec, 47);
  2750. __m512i const data_vec = _mm512_xor_si512 (acc_vec, shifted);
  2751. /* xacc[0] ^= secret; */
  2752. __m512i const key_vec = _mm512_loadu_si512 (secret);
  2753. __m512i const data_key = _mm512_xor_si512 (data_vec, key_vec);
  2754. /* xacc[0] *= XXH_PRIME32_1; */
  2755. __m512i const data_key_hi = _mm512_shuffle_epi32 (data_key, (_MM_PERM_ENUM)_MM_SHUFFLE(0, 3, 0, 1));
  2756. __m512i const prod_lo = _mm512_mul_epu32 (data_key, prime32);
  2757. __m512i const prod_hi = _mm512_mul_epu32 (data_key_hi, prime32);
  2758. *xacc = _mm512_add_epi64(prod_lo, _mm512_slli_epi64(prod_hi, 32));
  2759. }
  2760. }
  2761. XXH_FORCE_INLINE XXH_TARGET_AVX512 void
  2762. XXH3_initCustomSecret_avx512(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
  2763. {
  2764. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 63) == 0);
  2765. XXH_STATIC_ASSERT(XXH_SEC_ALIGN == 64);
  2766. XXH_ASSERT(((size_t)customSecret & 63) == 0);
  2767. (void)(&XXH_writeLE64);
  2768. { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / sizeof(__m512i);
  2769. __m512i const seed = _mm512_mask_set1_epi64(_mm512_set1_epi64((xxh_i64)seed64), 0xAA, -(xxh_i64)seed64);
  2770. XXH_ALIGN(64) const __m512i* const src = (const __m512i*) XXH3_kSecret;
  2771. XXH_ALIGN(64) __m512i* const dest = ( __m512i*) customSecret;
  2772. int i;
  2773. for (i=0; i < nbRounds; ++i) {
  2774. /* GCC has a bug, _mm512_stream_load_si512 accepts 'void*', not 'void const*',
  2775. * this will warn "discards ‘const’ qualifier". */
  2776. union {
  2777. XXH_ALIGN(64) const __m512i* cp;
  2778. XXH_ALIGN(64) void* p;
  2779. } remote_const_void;
  2780. remote_const_void.cp = src + i;
  2781. dest[i] = _mm512_add_epi64(_mm512_stream_load_si512(remote_const_void.p), seed);
  2782. } }
  2783. }
  2784. #endif
  2785. #if (XXH_VECTOR == XXH_AVX2) || defined(XXH_X86DISPATCH)
  2786. #ifndef XXH_TARGET_AVX2
  2787. # define XXH_TARGET_AVX2 /* disable attribute target */
  2788. #endif
  2789. XXH_FORCE_INLINE XXH_TARGET_AVX2 void
  2790. XXH3_accumulate_512_avx2( void* XXH_RESTRICT acc,
  2791. const void* XXH_RESTRICT input,
  2792. const void* XXH_RESTRICT secret)
  2793. {
  2794. XXH_ASSERT((((size_t)acc) & 31) == 0);
  2795. { XXH_ALIGN(32) __m256i* const xacc = (__m256i *) acc;
  2796. /* Unaligned. This is mainly for pointer arithmetic, and because
  2797. * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
  2798. const __m256i* const xinput = (const __m256i *) input;
  2799. /* Unaligned. This is mainly for pointer arithmetic, and because
  2800. * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
  2801. const __m256i* const xsecret = (const __m256i *) secret;
  2802. size_t i;
  2803. for (i=0; i < XXH_STRIPE_LEN/sizeof(__m256i); i++) {
  2804. /* data_vec = xinput[i]; */
  2805. __m256i const data_vec = _mm256_loadu_si256 (xinput+i);
  2806. /* key_vec = xsecret[i]; */
  2807. __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
  2808. /* data_key = data_vec ^ key_vec; */
  2809. __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
  2810. /* data_key_lo = data_key >> 32; */
  2811. __m256i const data_key_lo = _mm256_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
  2812. /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
  2813. __m256i const product = _mm256_mul_epu32 (data_key, data_key_lo);
  2814. /* xacc[i] += swap(data_vec); */
  2815. __m256i const data_swap = _mm256_shuffle_epi32(data_vec, _MM_SHUFFLE(1, 0, 3, 2));
  2816. __m256i const sum = _mm256_add_epi64(xacc[i], data_swap);
  2817. /* xacc[i] += product; */
  2818. xacc[i] = _mm256_add_epi64(product, sum);
  2819. } }
  2820. }
  2821. XXH_FORCE_INLINE XXH_TARGET_AVX2 void
  2822. XXH3_scrambleAcc_avx2(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  2823. {
  2824. XXH_ASSERT((((size_t)acc) & 31) == 0);
  2825. { XXH_ALIGN(32) __m256i* const xacc = (__m256i*) acc;
  2826. /* Unaligned. This is mainly for pointer arithmetic, and because
  2827. * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
  2828. const __m256i* const xsecret = (const __m256i *) secret;
  2829. const __m256i prime32 = _mm256_set1_epi32((int)XXH_PRIME32_1);
  2830. size_t i;
  2831. for (i=0; i < XXH_STRIPE_LEN/sizeof(__m256i); i++) {
  2832. /* xacc[i] ^= (xacc[i] >> 47) */
  2833. __m256i const acc_vec = xacc[i];
  2834. __m256i const shifted = _mm256_srli_epi64 (acc_vec, 47);
  2835. __m256i const data_vec = _mm256_xor_si256 (acc_vec, shifted);
  2836. /* xacc[i] ^= xsecret; */
  2837. __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
  2838. __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
  2839. /* xacc[i] *= XXH_PRIME32_1; */
  2840. __m256i const data_key_hi = _mm256_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
  2841. __m256i const prod_lo = _mm256_mul_epu32 (data_key, prime32);
  2842. __m256i const prod_hi = _mm256_mul_epu32 (data_key_hi, prime32);
  2843. xacc[i] = _mm256_add_epi64(prod_lo, _mm256_slli_epi64(prod_hi, 32));
  2844. }
  2845. }
  2846. }
  2847. XXH_FORCE_INLINE XXH_TARGET_AVX2 void XXH3_initCustomSecret_avx2(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
  2848. {
  2849. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 31) == 0);
  2850. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE / sizeof(__m256i)) == 6);
  2851. XXH_STATIC_ASSERT(XXH_SEC_ALIGN <= 64);
  2852. (void)(&XXH_writeLE64);
  2853. XXH_PREFETCH(customSecret);
  2854. { __m256i const seed = _mm256_set_epi64x(-(xxh_i64)seed64, (xxh_i64)seed64, -(xxh_i64)seed64, (xxh_i64)seed64);
  2855. XXH_ALIGN(64) const __m256i* const src = (const __m256i*) XXH3_kSecret;
  2856. XXH_ALIGN(64) __m256i* dest = ( __m256i*) customSecret;
  2857. # if defined(__GNUC__) || defined(__clang__)
  2858. /*
  2859. * On GCC & Clang, marking 'dest' as modified will cause the compiler:
  2860. * - do not extract the secret from sse registers in the internal loop
  2861. * - use less common registers, and avoid pushing these reg into stack
  2862. * The asm hack causes Clang to assume that XXH3_kSecretPtr aliases with
  2863. * customSecret, and on aarch64, this prevented LDP from merging two
  2864. * loads together for free. Putting the loads together before the stores
  2865. * properly generates LDP.
  2866. */
  2867. __asm__("" : "+r" (dest));
  2868. # endif
  2869. /* GCC -O2 need unroll loop manually */
  2870. dest[0] = _mm256_add_epi64(_mm256_stream_load_si256(src+0), seed);
  2871. dest[1] = _mm256_add_epi64(_mm256_stream_load_si256(src+1), seed);
  2872. dest[2] = _mm256_add_epi64(_mm256_stream_load_si256(src+2), seed);
  2873. dest[3] = _mm256_add_epi64(_mm256_stream_load_si256(src+3), seed);
  2874. dest[4] = _mm256_add_epi64(_mm256_stream_load_si256(src+4), seed);
  2875. dest[5] = _mm256_add_epi64(_mm256_stream_load_si256(src+5), seed);
  2876. }
  2877. }
  2878. #endif
  2879. #if (XXH_VECTOR == XXH_SSE2) || defined(XXH_X86DISPATCH)
  2880. #ifndef XXH_TARGET_SSE2
  2881. # define XXH_TARGET_SSE2 /* disable attribute target */
  2882. #endif
  2883. XXH_FORCE_INLINE XXH_TARGET_SSE2 void
  2884. XXH3_accumulate_512_sse2( void* XXH_RESTRICT acc,
  2885. const void* XXH_RESTRICT input,
  2886. const void* XXH_RESTRICT secret)
  2887. {
  2888. /* SSE2 is just a half-scale version of the AVX2 version. */
  2889. XXH_ASSERT((((size_t)acc) & 15) == 0);
  2890. { XXH_ALIGN(16) __m128i* const xacc = (__m128i *) acc;
  2891. /* Unaligned. This is mainly for pointer arithmetic, and because
  2892. * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
  2893. const __m128i* const xinput = (const __m128i *) input;
  2894. /* Unaligned. This is mainly for pointer arithmetic, and because
  2895. * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
  2896. const __m128i* const xsecret = (const __m128i *) secret;
  2897. size_t i;
  2898. for (i=0; i < XXH_STRIPE_LEN/sizeof(__m128i); i++) {
  2899. /* data_vec = xinput[i]; */
  2900. __m128i const data_vec = _mm_loadu_si128 (xinput+i);
  2901. /* key_vec = xsecret[i]; */
  2902. __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
  2903. /* data_key = data_vec ^ key_vec; */
  2904. __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
  2905. /* data_key_lo = data_key >> 32; */
  2906. __m128i const data_key_lo = _mm_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
  2907. /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
  2908. __m128i const product = _mm_mul_epu32 (data_key, data_key_lo);
  2909. /* xacc[i] += swap(data_vec); */
  2910. __m128i const data_swap = _mm_shuffle_epi32(data_vec, _MM_SHUFFLE(1,0,3,2));
  2911. __m128i const sum = _mm_add_epi64(xacc[i], data_swap);
  2912. /* xacc[i] += product; */
  2913. xacc[i] = _mm_add_epi64(product, sum);
  2914. } }
  2915. }
  2916. XXH_FORCE_INLINE XXH_TARGET_SSE2 void
  2917. XXH3_scrambleAcc_sse2(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  2918. {
  2919. XXH_ASSERT((((size_t)acc) & 15) == 0);
  2920. { XXH_ALIGN(16) __m128i* const xacc = (__m128i*) acc;
  2921. /* Unaligned. This is mainly for pointer arithmetic, and because
  2922. * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
  2923. const __m128i* const xsecret = (const __m128i *) secret;
  2924. const __m128i prime32 = _mm_set1_epi32((int)XXH_PRIME32_1);
  2925. size_t i;
  2926. for (i=0; i < XXH_STRIPE_LEN/sizeof(__m128i); i++) {
  2927. /* xacc[i] ^= (xacc[i] >> 47) */
  2928. __m128i const acc_vec = xacc[i];
  2929. __m128i const shifted = _mm_srli_epi64 (acc_vec, 47);
  2930. __m128i const data_vec = _mm_xor_si128 (acc_vec, shifted);
  2931. /* xacc[i] ^= xsecret[i]; */
  2932. __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
  2933. __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
  2934. /* xacc[i] *= XXH_PRIME32_1; */
  2935. __m128i const data_key_hi = _mm_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
  2936. __m128i const prod_lo = _mm_mul_epu32 (data_key, prime32);
  2937. __m128i const prod_hi = _mm_mul_epu32 (data_key_hi, prime32);
  2938. xacc[i] = _mm_add_epi64(prod_lo, _mm_slli_epi64(prod_hi, 32));
  2939. }
  2940. }
  2941. }
  2942. XXH_FORCE_INLINE XXH_TARGET_SSE2 void XXH3_initCustomSecret_sse2(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
  2943. {
  2944. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 15) == 0);
  2945. (void)(&XXH_writeLE64);
  2946. { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / sizeof(__m128i);
  2947. # if defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER < 1900
  2948. // MSVC 32bit mode does not support _mm_set_epi64x before 2015
  2949. XXH_ALIGN(16) const xxh_i64 seed64x2[2] = { (xxh_i64)seed64, -(xxh_i64)seed64 };
  2950. __m128i const seed = _mm_load_si128((__m128i const*)seed64x2);
  2951. # else
  2952. __m128i const seed = _mm_set_epi64x(-(xxh_i64)seed64, (xxh_i64)seed64);
  2953. # endif
  2954. int i;
  2955. XXH_ALIGN(64) const float* const src = (float const*) XXH3_kSecret;
  2956. XXH_ALIGN(XXH_SEC_ALIGN) __m128i* dest = (__m128i*) customSecret;
  2957. # if defined(__GNUC__) || defined(__clang__)
  2958. /*
  2959. * On GCC & Clang, marking 'dest' as modified will cause the compiler:
  2960. * - do not extract the secret from sse registers in the internal loop
  2961. * - use less common registers, and avoid pushing these reg into stack
  2962. */
  2963. __asm__("" : "+r" (dest));
  2964. # endif
  2965. for (i=0; i < nbRounds; ++i) {
  2966. dest[i] = _mm_add_epi64(_mm_castps_si128(_mm_load_ps(src+i*4)), seed);
  2967. } }
  2968. }
  2969. #endif
  2970. #if (XXH_VECTOR == XXH_NEON)
  2971. XXH_FORCE_INLINE void
  2972. XXH3_accumulate_512_neon( void* XXH_RESTRICT acc,
  2973. const void* XXH_RESTRICT input,
  2974. const void* XXH_RESTRICT secret)
  2975. {
  2976. XXH_ASSERT((((size_t)acc) & 15) == 0);
  2977. {
  2978. XXH_ALIGN(16) uint64x2_t* const xacc = (uint64x2_t *) acc;
  2979. /* We don't use a uint32x4_t pointer because it causes bus errors on ARMv7. */
  2980. uint8_t const* const xinput = (const uint8_t *) input;
  2981. uint8_t const* const xsecret = (const uint8_t *) secret;
  2982. size_t i;
  2983. for (i=0; i < XXH_STRIPE_LEN / sizeof(uint64x2_t); i++) {
  2984. /* data_vec = xinput[i]; */
  2985. uint8x16_t data_vec = vld1q_u8(xinput + (i * 16));
  2986. /* key_vec = xsecret[i]; */
  2987. uint8x16_t key_vec = vld1q_u8(xsecret + (i * 16));
  2988. uint64x2_t data_key;
  2989. uint32x2_t data_key_lo, data_key_hi;
  2990. /* xacc[i] += swap(data_vec); */
  2991. uint64x2_t const data64 = vreinterpretq_u64_u8(data_vec);
  2992. uint64x2_t const swapped = vextq_u64(data64, data64, 1);
  2993. xacc[i] = vaddq_u64 (xacc[i], swapped);
  2994. /* data_key = data_vec ^ key_vec; */
  2995. data_key = vreinterpretq_u64_u8(veorq_u8(data_vec, key_vec));
  2996. /* data_key_lo = (uint32x2_t) (data_key & 0xFFFFFFFF);
  2997. * data_key_hi = (uint32x2_t) (data_key >> 32);
  2998. * data_key = UNDEFINED; */
  2999. XXH_SPLIT_IN_PLACE(data_key, data_key_lo, data_key_hi);
  3000. /* xacc[i] += (uint64x2_t) data_key_lo * (uint64x2_t) data_key_hi; */
  3001. xacc[i] = vmlal_u32 (xacc[i], data_key_lo, data_key_hi);
  3002. }
  3003. }
  3004. }
  3005. XXH_FORCE_INLINE void
  3006. XXH3_scrambleAcc_neon(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  3007. {
  3008. XXH_ASSERT((((size_t)acc) & 15) == 0);
  3009. { uint64x2_t* xacc = (uint64x2_t*) acc;
  3010. uint8_t const* xsecret = (uint8_t const*) secret;
  3011. uint32x2_t prime = vdup_n_u32 (XXH_PRIME32_1);
  3012. size_t i;
  3013. for (i=0; i < XXH_STRIPE_LEN/sizeof(uint64x2_t); i++) {
  3014. /* xacc[i] ^= (xacc[i] >> 47); */
  3015. uint64x2_t acc_vec = xacc[i];
  3016. uint64x2_t shifted = vshrq_n_u64 (acc_vec, 47);
  3017. uint64x2_t data_vec = veorq_u64 (acc_vec, shifted);
  3018. /* xacc[i] ^= xsecret[i]; */
  3019. uint8x16_t key_vec = vld1q_u8(xsecret + (i * 16));
  3020. uint64x2_t data_key = veorq_u64(data_vec, vreinterpretq_u64_u8(key_vec));
  3021. /* xacc[i] *= XXH_PRIME32_1 */
  3022. uint32x2_t data_key_lo, data_key_hi;
  3023. /* data_key_lo = (uint32x2_t) (xacc[i] & 0xFFFFFFFF);
  3024. * data_key_hi = (uint32x2_t) (xacc[i] >> 32);
  3025. * xacc[i] = UNDEFINED; */
  3026. XXH_SPLIT_IN_PLACE(data_key, data_key_lo, data_key_hi);
  3027. { /*
  3028. * prod_hi = (data_key >> 32) * XXH_PRIME32_1;
  3029. *
  3030. * Avoid vmul_u32 + vshll_n_u32 since Clang 6 and 7 will
  3031. * incorrectly "optimize" this:
  3032. * tmp = vmul_u32(vmovn_u64(a), vmovn_u64(b));
  3033. * shifted = vshll_n_u32(tmp, 32);
  3034. * to this:
  3035. * tmp = "vmulq_u64"(a, b); // no such thing!
  3036. * shifted = vshlq_n_u64(tmp, 32);
  3037. *
  3038. * However, unlike SSE, Clang lacks a 64-bit multiply routine
  3039. * for NEON, and it scalarizes two 64-bit multiplies instead.
  3040. *
  3041. * vmull_u32 has the same timing as vmul_u32, and it avoids
  3042. * this bug completely.
  3043. * See https://bugs.llvm.org/show_bug.cgi?id=39967
  3044. */
  3045. uint64x2_t prod_hi = vmull_u32 (data_key_hi, prime);
  3046. /* xacc[i] = prod_hi << 32; */
  3047. xacc[i] = vshlq_n_u64(prod_hi, 32);
  3048. /* xacc[i] += (prod_hi & 0xFFFFFFFF) * XXH_PRIME32_1; */
  3049. xacc[i] = vmlal_u32(xacc[i], data_key_lo, prime);
  3050. }
  3051. } }
  3052. }
  3053. #endif
  3054. #if (XXH_VECTOR == XXH_VSX)
  3055. XXH_FORCE_INLINE void
  3056. XXH3_accumulate_512_vsx( void* XXH_RESTRICT acc,
  3057. const void* XXH_RESTRICT input,
  3058. const void* XXH_RESTRICT secret)
  3059. {
  3060. xxh_u64x2* const xacc = (xxh_u64x2*) acc; /* presumed aligned */
  3061. xxh_u64x2 const* const xinput = (xxh_u64x2 const*) input; /* no alignment restriction */
  3062. xxh_u64x2 const* const xsecret = (xxh_u64x2 const*) secret; /* no alignment restriction */
  3063. xxh_u64x2 const v32 = { 32, 32 };
  3064. size_t i;
  3065. for (i = 0; i < XXH_STRIPE_LEN / sizeof(xxh_u64x2); i++) {
  3066. /* data_vec = xinput[i]; */
  3067. xxh_u64x2 const data_vec = XXH_vec_loadu(xinput + i);
  3068. /* key_vec = xsecret[i]; */
  3069. xxh_u64x2 const key_vec = XXH_vec_loadu(xsecret + i);
  3070. xxh_u64x2 const data_key = data_vec ^ key_vec;
  3071. /* shuffled = (data_key << 32) | (data_key >> 32); */
  3072. xxh_u32x4 const shuffled = (xxh_u32x4)vec_rl(data_key, v32);
  3073. /* product = ((xxh_u64x2)data_key & 0xFFFFFFFF) * ((xxh_u64x2)shuffled & 0xFFFFFFFF); */
  3074. xxh_u64x2 const product = XXH_vec_mulo((xxh_u32x4)data_key, shuffled);
  3075. xacc[i] += product;
  3076. /* swap high and low halves */
  3077. #ifdef __s390x__
  3078. xacc[i] += vec_permi(data_vec, data_vec, 2);
  3079. #else
  3080. xacc[i] += vec_xxpermdi(data_vec, data_vec, 2);
  3081. #endif
  3082. }
  3083. }
  3084. XXH_FORCE_INLINE void
  3085. XXH3_scrambleAcc_vsx(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  3086. {
  3087. XXH_ASSERT((((size_t)acc) & 15) == 0);
  3088. { xxh_u64x2* const xacc = (xxh_u64x2*) acc;
  3089. const xxh_u64x2* const xsecret = (const xxh_u64x2*) secret;
  3090. /* constants */
  3091. xxh_u64x2 const v32 = { 32, 32 };
  3092. xxh_u64x2 const v47 = { 47, 47 };
  3093. xxh_u32x4 const prime = { XXH_PRIME32_1, XXH_PRIME32_1, XXH_PRIME32_1, XXH_PRIME32_1 };
  3094. size_t i;
  3095. for (i = 0; i < XXH_STRIPE_LEN / sizeof(xxh_u64x2); i++) {
  3096. /* xacc[i] ^= (xacc[i] >> 47); */
  3097. xxh_u64x2 const acc_vec = xacc[i];
  3098. xxh_u64x2 const data_vec = acc_vec ^ (acc_vec >> v47);
  3099. /* xacc[i] ^= xsecret[i]; */
  3100. xxh_u64x2 const key_vec = XXH_vec_loadu(xsecret + i);
  3101. xxh_u64x2 const data_key = data_vec ^ key_vec;
  3102. /* xacc[i] *= XXH_PRIME32_1 */
  3103. /* prod_lo = ((xxh_u64x2)data_key & 0xFFFFFFFF) * ((xxh_u64x2)prime & 0xFFFFFFFF); */
  3104. xxh_u64x2 const prod_even = XXH_vec_mule((xxh_u32x4)data_key, prime);
  3105. /* prod_hi = ((xxh_u64x2)data_key >> 32) * ((xxh_u64x2)prime >> 32); */
  3106. xxh_u64x2 const prod_odd = XXH_vec_mulo((xxh_u32x4)data_key, prime);
  3107. xacc[i] = prod_odd + (prod_even << v32);
  3108. } }
  3109. }
  3110. #endif
  3111. /* scalar variants - universal */
  3112. XXH_FORCE_INLINE void
  3113. XXH3_accumulate_512_scalar(void* XXH_RESTRICT acc,
  3114. const void* XXH_RESTRICT input,
  3115. const void* XXH_RESTRICT secret)
  3116. {
  3117. XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64* const xacc = (xxh_u64*) acc; /* presumed aligned */
  3118. const xxh_u8* const xinput = (const xxh_u8*) input; /* no alignment restriction */
  3119. const xxh_u8* const xsecret = (const xxh_u8*) secret; /* no alignment restriction */
  3120. size_t i;
  3121. XXH_ASSERT(((size_t)acc & (XXH_ACC_ALIGN-1)) == 0);
  3122. for (i=0; i < XXH_ACC_NB; i++) {
  3123. xxh_u64 const data_val = XXH_readLE64(xinput + 8*i);
  3124. xxh_u64 const data_key = data_val ^ XXH_readLE64(xsecret + i*8);
  3125. xacc[i ^ 1] += data_val; /* swap adjacent lanes */
  3126. xacc[i] += XXH_mult32to64(data_key & 0xFFFFFFFF, data_key >> 32);
  3127. }
  3128. }
  3129. XXH_FORCE_INLINE void
  3130. XXH3_scrambleAcc_scalar(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  3131. {
  3132. XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64* const xacc = (xxh_u64*) acc; /* presumed aligned */
  3133. const xxh_u8* const xsecret = (const xxh_u8*) secret; /* no alignment restriction */
  3134. size_t i;
  3135. XXH_ASSERT((((size_t)acc) & (XXH_ACC_ALIGN-1)) == 0);
  3136. for (i=0; i < XXH_ACC_NB; i++) {
  3137. xxh_u64 const key64 = XXH_readLE64(xsecret + 8*i);
  3138. xxh_u64 acc64 = xacc[i];
  3139. acc64 = XXH_xorshift64(acc64, 47);
  3140. acc64 ^= key64;
  3141. acc64 *= XXH_PRIME32_1;
  3142. xacc[i] = acc64;
  3143. }
  3144. }
  3145. XXH_FORCE_INLINE void
  3146. XXH3_initCustomSecret_scalar(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
  3147. {
  3148. /*
  3149. * We need a separate pointer for the hack below,
  3150. * which requires a non-const pointer.
  3151. * Any decent compiler will optimize this out otherwise.
  3152. */
  3153. const xxh_u8* kSecretPtr = XXH3_kSecret;
  3154. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 15) == 0);
  3155. #if defined(__clang__) && defined(__aarch64__)
  3156. /*
  3157. * UGLY HACK:
  3158. * Clang generates a bunch of MOV/MOVK pairs for aarch64, and they are
  3159. * placed sequentially, in order, at the top of the unrolled loop.
  3160. *
  3161. * While MOVK is great for generating constants (2 cycles for a 64-bit
  3162. * constant compared to 4 cycles for LDR), long MOVK chains stall the
  3163. * integer pipelines:
  3164. * I L S
  3165. * MOVK
  3166. * MOVK
  3167. * MOVK
  3168. * MOVK
  3169. * ADD
  3170. * SUB STR
  3171. * STR
  3172. * By forcing loads from memory (as the asm line causes Clang to assume
  3173. * that XXH3_kSecretPtr has been changed), the pipelines are used more
  3174. * efficiently:
  3175. * I L S
  3176. * LDR
  3177. * ADD LDR
  3178. * SUB STR
  3179. * STR
  3180. * XXH3_64bits_withSeed, len == 256, Snapdragon 835
  3181. * without hack: 2654.4 MB/s
  3182. * with hack: 3202.9 MB/s
  3183. */
  3184. __asm__("" : "+r" (kSecretPtr));
  3185. #endif
  3186. /*
  3187. * Note: in debug mode, this overrides the asm optimization
  3188. * and Clang will emit MOVK chains again.
  3189. */
  3190. XXH_ASSERT(kSecretPtr == XXH3_kSecret);
  3191. { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / 16;
  3192. int i;
  3193. for (i=0; i < nbRounds; i++) {
  3194. /*
  3195. * The asm hack causes Clang to assume that kSecretPtr aliases with
  3196. * customSecret, and on aarch64, this prevented LDP from merging two
  3197. * loads together for free. Putting the loads together before the stores
  3198. * properly generates LDP.
  3199. */
  3200. xxh_u64 lo = XXH_readLE64(kSecretPtr + 16*i) + seed64;
  3201. xxh_u64 hi = XXH_readLE64(kSecretPtr + 16*i + 8) - seed64;
  3202. XXH_writeLE64((xxh_u8*)customSecret + 16*i, lo);
  3203. XXH_writeLE64((xxh_u8*)customSecret + 16*i + 8, hi);
  3204. } }
  3205. }
  3206. typedef void (*XXH3_f_accumulate_512)(void* XXH_RESTRICT, const void*, const void*);
  3207. typedef void (*XXH3_f_scrambleAcc)(void* XXH_RESTRICT, const void*);
  3208. typedef void (*XXH3_f_initCustomSecret)(void* XXH_RESTRICT, xxh_u64);
  3209. #if (XXH_VECTOR == XXH_AVX512)
  3210. #define XXH3_accumulate_512 XXH3_accumulate_512_avx512
  3211. #define XXH3_scrambleAcc XXH3_scrambleAcc_avx512
  3212. #define XXH3_initCustomSecret XXH3_initCustomSecret_avx512
  3213. #elif (XXH_VECTOR == XXH_AVX2)
  3214. #define XXH3_accumulate_512 XXH3_accumulate_512_avx2
  3215. #define XXH3_scrambleAcc XXH3_scrambleAcc_avx2
  3216. #define XXH3_initCustomSecret XXH3_initCustomSecret_avx2
  3217. #elif (XXH_VECTOR == XXH_SSE2)
  3218. #define XXH3_accumulate_512 XXH3_accumulate_512_sse2
  3219. #define XXH3_scrambleAcc XXH3_scrambleAcc_sse2
  3220. #define XXH3_initCustomSecret XXH3_initCustomSecret_sse2
  3221. #elif (XXH_VECTOR == XXH_NEON)
  3222. #define XXH3_accumulate_512 XXH3_accumulate_512_neon
  3223. #define XXH3_scrambleAcc XXH3_scrambleAcc_neon
  3224. #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  3225. #elif (XXH_VECTOR == XXH_VSX)
  3226. #define XXH3_accumulate_512 XXH3_accumulate_512_vsx
  3227. #define XXH3_scrambleAcc XXH3_scrambleAcc_vsx
  3228. #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  3229. #else /* scalar */
  3230. #define XXH3_accumulate_512 XXH3_accumulate_512_scalar
  3231. #define XXH3_scrambleAcc XXH3_scrambleAcc_scalar
  3232. #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  3233. #endif
  3234. #ifndef XXH_PREFETCH_DIST
  3235. # ifdef __clang__
  3236. # define XXH_PREFETCH_DIST 320
  3237. # else
  3238. # if (XXH_VECTOR == XXH_AVX512)
  3239. # define XXH_PREFETCH_DIST 512
  3240. # else
  3241. # define XXH_PREFETCH_DIST 384
  3242. # endif
  3243. # endif /* __clang__ */
  3244. #endif /* XXH_PREFETCH_DIST */
  3245. /*
  3246. * XXH3_accumulate()
  3247. * Loops over XXH3_accumulate_512().
  3248. * Assumption: nbStripes will not overflow the secret size
  3249. */
  3250. XXH_FORCE_INLINE void
  3251. XXH3_accumulate( xxh_u64* XXH_RESTRICT acc,
  3252. const xxh_u8* XXH_RESTRICT input,
  3253. const xxh_u8* XXH_RESTRICT secret,
  3254. size_t nbStripes,
  3255. XXH3_f_accumulate_512 f_acc512)
  3256. {
  3257. size_t n;
  3258. for (n = 0; n < nbStripes; n++ ) {
  3259. const xxh_u8* const in = input + n*XXH_STRIPE_LEN;
  3260. XXH_PREFETCH(in + XXH_PREFETCH_DIST);
  3261. f_acc512(acc,
  3262. in,
  3263. secret + n*XXH_SECRET_CONSUME_RATE);
  3264. }
  3265. }
  3266. XXH_FORCE_INLINE void
  3267. XXH3_hashLong_internal_loop(xxh_u64* XXH_RESTRICT acc,
  3268. const xxh_u8* XXH_RESTRICT input, size_t len,
  3269. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  3270. XXH3_f_accumulate_512 f_acc512,
  3271. XXH3_f_scrambleAcc f_scramble)
  3272. {
  3273. size_t const nbStripesPerBlock = (secretSize - XXH_STRIPE_LEN) / XXH_SECRET_CONSUME_RATE;
  3274. size_t const block_len = XXH_STRIPE_LEN * nbStripesPerBlock;
  3275. size_t const nb_blocks = (len - 1) / block_len;
  3276. size_t n;
  3277. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
  3278. for (n = 0; n < nb_blocks; n++) {
  3279. XXH3_accumulate(acc, input + n*block_len, secret, nbStripesPerBlock, f_acc512);
  3280. f_scramble(acc, secret + secretSize - XXH_STRIPE_LEN);
  3281. }
  3282. /* last partial block */
  3283. XXH_ASSERT(len > XXH_STRIPE_LEN);
  3284. { size_t const nbStripes = ((len - 1) - (block_len * nb_blocks)) / XXH_STRIPE_LEN;
  3285. XXH_ASSERT(nbStripes <= (secretSize / XXH_SECRET_CONSUME_RATE));
  3286. XXH3_accumulate(acc, input + nb_blocks*block_len, secret, nbStripes, f_acc512);
  3287. /* last stripe */
  3288. { const xxh_u8* const p = input + len - XXH_STRIPE_LEN;
  3289. #define XXH_SECRET_LASTACC_START 7 /* not aligned on 8, last secret is different from acc & scrambler */
  3290. f_acc512(acc, p, secret + secretSize - XXH_STRIPE_LEN - XXH_SECRET_LASTACC_START);
  3291. } }
  3292. }
  3293. XXH_FORCE_INLINE xxh_u64
  3294. XXH3_mix2Accs(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret)
  3295. {
  3296. return XXH3_mul128_fold64(
  3297. acc[0] ^ XXH_readLE64(secret),
  3298. acc[1] ^ XXH_readLE64(secret+8) );
  3299. }
  3300. static XXH64_hash_t
  3301. XXH3_mergeAccs(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret, xxh_u64 start)
  3302. {
  3303. xxh_u64 result64 = start;
  3304. size_t i = 0;
  3305. for (i = 0; i < 4; i++) {
  3306. result64 += XXH3_mix2Accs(acc+2*i, secret + 16*i);
  3307. #if defined(__clang__) /* Clang */ \
  3308. && (defined(__arm__) || defined(__thumb__)) /* ARMv7 */ \
  3309. && (defined(__ARM_NEON) || defined(__ARM_NEON__)) /* NEON */ \
  3310. && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable */
  3311. /*
  3312. * UGLY HACK:
  3313. * Prevent autovectorization on Clang ARMv7-a. Exact same problem as
  3314. * the one in XXH3_len_129to240_64b. Speeds up shorter keys > 240b.
  3315. * XXH3_64bits, len == 256, Snapdragon 835:
  3316. * without hack: 2063.7 MB/s
  3317. * with hack: 2560.7 MB/s
  3318. */
  3319. __asm__("" : "+r" (result64));
  3320. #endif
  3321. }
  3322. return XXH3_avalanche(result64);
  3323. }
  3324. #define XXH3_INIT_ACC { XXH_PRIME32_3, XXH_PRIME64_1, XXH_PRIME64_2, XXH_PRIME64_3, \
  3325. XXH_PRIME64_4, XXH_PRIME32_2, XXH_PRIME64_5, XXH_PRIME32_1 }
  3326. XXH_FORCE_INLINE XXH64_hash_t
  3327. XXH3_hashLong_64b_internal(const void* XXH_RESTRICT input, size_t len,
  3328. const void* XXH_RESTRICT secret, size_t secretSize,
  3329. XXH3_f_accumulate_512 f_acc512,
  3330. XXH3_f_scrambleAcc f_scramble)
  3331. {
  3332. XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[XXH_ACC_NB] = XXH3_INIT_ACC;
  3333. XXH3_hashLong_internal_loop(acc, (const xxh_u8*)input, len, (const xxh_u8*)secret, secretSize, f_acc512, f_scramble);
  3334. /* converge into final hash */
  3335. XXH_STATIC_ASSERT(sizeof(acc) == 64);
  3336. /* do not align on 8, so that the secret is different from the accumulator */
  3337. #define XXH_SECRET_MERGEACCS_START 11
  3338. XXH_ASSERT(secretSize >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
  3339. return XXH3_mergeAccs(acc, (const xxh_u8*)secret + XXH_SECRET_MERGEACCS_START, (xxh_u64)len * XXH_PRIME64_1);
  3340. }
  3341. /*
  3342. * It's important for performance that XXH3_hashLong is not inlined.
  3343. */
  3344. XXH_NO_INLINE XXH64_hash_t
  3345. XXH3_hashLong_64b_withSecret(const void* XXH_RESTRICT input, size_t len,
  3346. XXH64_hash_t seed64, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
  3347. {
  3348. (void)seed64;
  3349. return XXH3_hashLong_64b_internal(input, len, secret, secretLen, XXH3_accumulate_512, XXH3_scrambleAcc);
  3350. }
  3351. /*
  3352. * It's important for performance that XXH3_hashLong is not inlined.
  3353. * Since the function is not inlined, the compiler may not be able to understand that,
  3354. * in some scenarios, its `secret` argument is actually a compile time constant.
  3355. * This variant enforces that the compiler can detect that,
  3356. * and uses this opportunity to streamline the generated code for better performance.
  3357. */
  3358. XXH_NO_INLINE XXH64_hash_t
  3359. XXH3_hashLong_64b_default(const void* XXH_RESTRICT input, size_t len,
  3360. XXH64_hash_t seed64, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
  3361. {
  3362. (void)seed64; (void)secret; (void)secretLen;
  3363. return XXH3_hashLong_64b_internal(input, len, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_accumulate_512, XXH3_scrambleAcc);
  3364. }
  3365. /*
  3366. * XXH3_hashLong_64b_withSeed():
  3367. * Generate a custom key based on alteration of default XXH3_kSecret with the seed,
  3368. * and then use this key for long mode hashing.
  3369. *
  3370. * This operation is decently fast but nonetheless costs a little bit of time.
  3371. * Try to avoid it whenever possible (typically when seed==0).
  3372. *
  3373. * It's important for performance that XXH3_hashLong is not inlined. Not sure
  3374. * why (uop cache maybe?), but the difference is large and easily measurable.
  3375. */
  3376. XXH_FORCE_INLINE XXH64_hash_t
  3377. XXH3_hashLong_64b_withSeed_internal(const void* input, size_t len,
  3378. XXH64_hash_t seed,
  3379. XXH3_f_accumulate_512 f_acc512,
  3380. XXH3_f_scrambleAcc f_scramble,
  3381. XXH3_f_initCustomSecret f_initSec)
  3382. {
  3383. if (seed == 0)
  3384. return XXH3_hashLong_64b_internal(input, len,
  3385. XXH3_kSecret, sizeof(XXH3_kSecret),
  3386. f_acc512, f_scramble);
  3387. { XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
  3388. f_initSec(secret, seed);
  3389. return XXH3_hashLong_64b_internal(input, len, secret, sizeof(secret),
  3390. f_acc512, f_scramble);
  3391. }
  3392. }
  3393. /*
  3394. * It's important for performance that XXH3_hashLong is not inlined.
  3395. */
  3396. XXH_NO_INLINE XXH64_hash_t
  3397. XXH3_hashLong_64b_withSeed(const void* input, size_t len,
  3398. XXH64_hash_t seed, const xxh_u8* secret, size_t secretLen)
  3399. {
  3400. (void)secret; (void)secretLen;
  3401. return XXH3_hashLong_64b_withSeed_internal(input, len, seed,
  3402. XXH3_accumulate_512, XXH3_scrambleAcc, XXH3_initCustomSecret);
  3403. }
  3404. typedef XXH64_hash_t (*XXH3_hashLong64_f)(const void* XXH_RESTRICT, size_t,
  3405. XXH64_hash_t, const xxh_u8* XXH_RESTRICT, size_t);
  3406. XXH_FORCE_INLINE XXH64_hash_t
  3407. XXH3_64bits_internal(const void* XXH_RESTRICT input, size_t len,
  3408. XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen,
  3409. XXH3_hashLong64_f f_hashLong)
  3410. {
  3411. XXH_ASSERT(secretLen >= XXH3_SECRET_SIZE_MIN);
  3412. /*
  3413. * If an action is to be taken if `secretLen` condition is not respected,
  3414. * it should be done here.
  3415. * For now, it's a contract pre-condition.
  3416. * Adding a check and a branch here would cost performance at every hash.
  3417. * Also, note that function signature doesn't offer room to return an error.
  3418. */
  3419. if (len <= 16)
  3420. return XXH3_len_0to16_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, seed64);
  3421. if (len <= 128)
  3422. return XXH3_len_17to128_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
  3423. if (len <= XXH3_MIDSIZE_MAX)
  3424. return XXH3_len_129to240_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
  3425. return f_hashLong(input, len, seed64, (const xxh_u8*)secret, secretLen);
  3426. }
  3427. /* === Public entry point === */
  3428. XXH_PUBLIC_API XXH64_hash_t XXH3_64bits(const void* input, size_t len)
  3429. {
  3430. return XXH3_64bits_internal(input, len, 0, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_hashLong_64b_default);
  3431. }
  3432. XXH_PUBLIC_API XXH64_hash_t
  3433. XXH3_64bits_withSecret(const void* input, size_t len, const void* secret, size_t secretSize)
  3434. {
  3435. return XXH3_64bits_internal(input, len, 0, secret, secretSize, XXH3_hashLong_64b_withSecret);
  3436. }
  3437. XXH_PUBLIC_API XXH64_hash_t
  3438. XXH3_64bits_withSeed(const void* input, size_t len, XXH64_hash_t seed)
  3439. {
  3440. return XXH3_64bits_internal(input, len, seed, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_hashLong_64b_withSeed);
  3441. }
  3442. /* === XXH3 streaming === */
  3443. /*
  3444. * Malloc's a pointer that is always aligned to align.
  3445. *
  3446. * This must be freed with `XXH_alignedFree()`.
  3447. *
  3448. * malloc typically guarantees 16 byte alignment on 64-bit systems and 8 byte
  3449. * alignment on 32-bit. This isn't enough for the 32 byte aligned loads in AVX2
  3450. * or on 32-bit, the 16 byte aligned loads in SSE2 and NEON.
  3451. *
  3452. * This underalignment previously caused a rather obvious crash which went
  3453. * completely unnoticed due to XXH3_createState() not actually being tested.
  3454. * Credit to RedSpah for noticing this bug.
  3455. *
  3456. * The alignment is done manually: Functions like posix_memalign or _mm_malloc
  3457. * are avoided: To maintain portability, we would have to write a fallback
  3458. * like this anyways, and besides, testing for the existence of library
  3459. * functions without relying on external build tools is impossible.
  3460. *
  3461. * The method is simple: Overallocate, manually align, and store the offset
  3462. * to the original behind the returned pointer.
  3463. *
  3464. * Align must be a power of 2 and 8 <= align <= 128.
  3465. */
  3466. static void* XXH_alignedMalloc(size_t s, size_t align)
  3467. {
  3468. XXH_ASSERT(align <= 128 && align >= 8); /* range check */
  3469. XXH_ASSERT((align & (align-1)) == 0); /* power of 2 */
  3470. XXH_ASSERT(s != 0 && s < (s + align)); /* empty/overflow */
  3471. { /* Overallocate to make room for manual realignment and an offset byte */
  3472. xxh_u8* base = (xxh_u8*)XXH_malloc(s + align);
  3473. if (base != NULL) {
  3474. /*
  3475. * Get the offset needed to align this pointer.
  3476. *
  3477. * Even if the returned pointer is aligned, there will always be
  3478. * at least one byte to store the offset to the original pointer.
  3479. */
  3480. size_t offset = align - ((size_t)base & (align - 1)); /* base % align */
  3481. /* Add the offset for the now-aligned pointer */
  3482. xxh_u8* ptr = base + offset;
  3483. XXH_ASSERT((size_t)ptr % align == 0);
  3484. /* Store the offset immediately before the returned pointer. */
  3485. ptr[-1] = (xxh_u8)offset;
  3486. return ptr;
  3487. }
  3488. return NULL;
  3489. }
  3490. }
  3491. /*
  3492. * Frees an aligned pointer allocated by XXH_alignedMalloc(). Don't pass
  3493. * normal malloc'd pointers, XXH_alignedMalloc has a specific data layout.
  3494. */
  3495. static void XXH_alignedFree(void* p)
  3496. {
  3497. if (p != NULL) {
  3498. xxh_u8* ptr = (xxh_u8*)p;
  3499. /* Get the offset byte we added in XXH_malloc. */
  3500. xxh_u8 offset = ptr[-1];
  3501. /* Free the original malloc'd pointer */
  3502. xxh_u8* base = ptr - offset;
  3503. XXH_free(base);
  3504. }
  3505. }
  3506. XXH_PUBLIC_API XXH3_state_t* XXH3_createState(void)
  3507. {
  3508. XXH3_state_t* const state = (XXH3_state_t*)XXH_alignedMalloc(sizeof(XXH3_state_t), 64);
  3509. if (state==NULL) return NULL;
  3510. XXH3_INITSTATE(state);
  3511. return state;
  3512. }
  3513. XXH_PUBLIC_API XXH_errorcode XXH3_freeState(XXH3_state_t* statePtr)
  3514. {
  3515. XXH_alignedFree(statePtr);
  3516. return XXH_OK;
  3517. }
  3518. XXH_PUBLIC_API void
  3519. XXH3_copyState(XXH3_state_t* dst_state, const XXH3_state_t* src_state)
  3520. {
  3521. memcpy(dst_state, src_state, sizeof(*dst_state));
  3522. }
  3523. static void
  3524. XXH3_64bits_reset_internal(XXH3_state_t* statePtr,
  3525. XXH64_hash_t seed,
  3526. const void* secret, size_t secretSize)
  3527. {
  3528. size_t const initStart = offsetof(XXH3_state_t, bufferedSize);
  3529. size_t const initLength = offsetof(XXH3_state_t, nbStripesPerBlock) - initStart;
  3530. XXH_ASSERT(offsetof(XXH3_state_t, nbStripesPerBlock) > initStart);
  3531. XXH_ASSERT(statePtr != NULL);
  3532. /* set members from bufferedSize to nbStripesPerBlock (excluded) to 0 */
  3533. memset((char*)statePtr + initStart, 0, initLength);
  3534. statePtr->acc[0] = XXH_PRIME32_3;
  3535. statePtr->acc[1] = XXH_PRIME64_1;
  3536. statePtr->acc[2] = XXH_PRIME64_2;
  3537. statePtr->acc[3] = XXH_PRIME64_3;
  3538. statePtr->acc[4] = XXH_PRIME64_4;
  3539. statePtr->acc[5] = XXH_PRIME32_2;
  3540. statePtr->acc[6] = XXH_PRIME64_5;
  3541. statePtr->acc[7] = XXH_PRIME32_1;
  3542. statePtr->seed = seed;
  3543. statePtr->extSecret = (const unsigned char*)secret;
  3544. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
  3545. statePtr->secretLimit = secretSize - XXH_STRIPE_LEN;
  3546. statePtr->nbStripesPerBlock = statePtr->secretLimit / XXH_SECRET_CONSUME_RATE;
  3547. }
  3548. XXH_PUBLIC_API XXH_errorcode
  3549. XXH3_64bits_reset(XXH3_state_t* statePtr)
  3550. {
  3551. if (statePtr == NULL) return XXH_ERROR;
  3552. XXH3_64bits_reset_internal(statePtr, 0, XXH3_kSecret, XXH_SECRET_DEFAULT_SIZE);
  3553. return XXH_OK;
  3554. }
  3555. XXH_PUBLIC_API XXH_errorcode
  3556. XXH3_64bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize)
  3557. {
  3558. if (statePtr == NULL) return XXH_ERROR;
  3559. XXH3_64bits_reset_internal(statePtr, 0, secret, secretSize);
  3560. if (secret == NULL) return XXH_ERROR;
  3561. if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
  3562. return XXH_OK;
  3563. }
  3564. XXH_PUBLIC_API XXH_errorcode
  3565. XXH3_64bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed)
  3566. {
  3567. if (statePtr == NULL) return XXH_ERROR;
  3568. if (seed==0) return XXH3_64bits_reset(statePtr);
  3569. if (seed != statePtr->seed) XXH3_initCustomSecret(statePtr->customSecret, seed);
  3570. XXH3_64bits_reset_internal(statePtr, seed, NULL, XXH_SECRET_DEFAULT_SIZE);
  3571. return XXH_OK;
  3572. }
  3573. /* Note : when XXH3_consumeStripes() is invoked,
  3574. * there must be a guarantee that at least one more byte must be consumed from input
  3575. * so that the function can blindly consume all stripes using the "normal" secret segment */
  3576. XXH_FORCE_INLINE void
  3577. XXH3_consumeStripes(xxh_u64* XXH_RESTRICT acc,
  3578. size_t* XXH_RESTRICT nbStripesSoFarPtr, size_t nbStripesPerBlock,
  3579. const xxh_u8* XXH_RESTRICT input, size_t nbStripes,
  3580. const xxh_u8* XXH_RESTRICT secret, size_t secretLimit,
  3581. XXH3_f_accumulate_512 f_acc512,
  3582. XXH3_f_scrambleAcc f_scramble)
  3583. {
  3584. XXH_ASSERT(nbStripes <= nbStripesPerBlock); /* can handle max 1 scramble per invocation */
  3585. XXH_ASSERT(*nbStripesSoFarPtr < nbStripesPerBlock);
  3586. if (nbStripesPerBlock - *nbStripesSoFarPtr <= nbStripes) {
  3587. /* need a scrambling operation */
  3588. size_t const nbStripesToEndofBlock = nbStripesPerBlock - *nbStripesSoFarPtr;
  3589. size_t const nbStripesAfterBlock = nbStripes - nbStripesToEndofBlock;
  3590. XXH3_accumulate(acc, input, secret + nbStripesSoFarPtr[0] * XXH_SECRET_CONSUME_RATE, nbStripesToEndofBlock, f_acc512);
  3591. f_scramble(acc, secret + secretLimit);
  3592. XXH3_accumulate(acc, input + nbStripesToEndofBlock * XXH_STRIPE_LEN, secret, nbStripesAfterBlock, f_acc512);
  3593. *nbStripesSoFarPtr = nbStripesAfterBlock;
  3594. } else {
  3595. XXH3_accumulate(acc, input, secret + nbStripesSoFarPtr[0] * XXH_SECRET_CONSUME_RATE, nbStripes, f_acc512);
  3596. *nbStripesSoFarPtr += nbStripes;
  3597. }
  3598. }
  3599. /*
  3600. * Both XXH3_64bits_update and XXH3_128bits_update use this routine.
  3601. */
  3602. XXH_FORCE_INLINE XXH_errorcode
  3603. XXH3_update(XXH3_state_t* state,
  3604. const xxh_u8* input, size_t len,
  3605. XXH3_f_accumulate_512 f_acc512,
  3606. XXH3_f_scrambleAcc f_scramble)
  3607. {
  3608. if (input==NULL)
  3609. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  3610. return XXH_OK;
  3611. #else
  3612. return XXH_ERROR;
  3613. #endif
  3614. { const xxh_u8* const bEnd = input + len;
  3615. const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
  3616. state->totalLen += len;
  3617. if (state->bufferedSize + len <= XXH3_INTERNALBUFFER_SIZE) { /* fill in tmp buffer */
  3618. XXH_memcpy(state->buffer + state->bufferedSize, input, len);
  3619. state->bufferedSize += (XXH32_hash_t)len;
  3620. return XXH_OK;
  3621. }
  3622. /* total input is now > XXH3_INTERNALBUFFER_SIZE */
  3623. #define XXH3_INTERNALBUFFER_STRIPES (XXH3_INTERNALBUFFER_SIZE / XXH_STRIPE_LEN)
  3624. XXH_STATIC_ASSERT(XXH3_INTERNALBUFFER_SIZE % XXH_STRIPE_LEN == 0); /* clean multiple */
  3625. /*
  3626. * Internal buffer is partially filled (always, except at beginning)
  3627. * Complete it, then consume it.
  3628. */
  3629. if (state->bufferedSize) {
  3630. size_t const loadSize = XXH3_INTERNALBUFFER_SIZE - state->bufferedSize;
  3631. XXH_memcpy(state->buffer + state->bufferedSize, input, loadSize);
  3632. input += loadSize;
  3633. XXH3_consumeStripes(state->acc,
  3634. &state->nbStripesSoFar, state->nbStripesPerBlock,
  3635. state->buffer, XXH3_INTERNALBUFFER_STRIPES,
  3636. secret, state->secretLimit,
  3637. f_acc512, f_scramble);
  3638. state->bufferedSize = 0;
  3639. }
  3640. XXH_ASSERT(input < bEnd);
  3641. /* Consume input by a multiple of internal buffer size */
  3642. if (input+XXH3_INTERNALBUFFER_SIZE < bEnd) {
  3643. const xxh_u8* const limit = bEnd - XXH3_INTERNALBUFFER_SIZE;
  3644. do {
  3645. XXH3_consumeStripes(state->acc,
  3646. &state->nbStripesSoFar, state->nbStripesPerBlock,
  3647. input, XXH3_INTERNALBUFFER_STRIPES,
  3648. secret, state->secretLimit,
  3649. f_acc512, f_scramble);
  3650. input += XXH3_INTERNALBUFFER_SIZE;
  3651. } while (input<limit);
  3652. /* for last partial stripe */
  3653. memcpy(state->buffer + sizeof(state->buffer) - XXH_STRIPE_LEN, input - XXH_STRIPE_LEN, XXH_STRIPE_LEN);
  3654. }
  3655. XXH_ASSERT(input < bEnd);
  3656. /* Some remaining input (always) : buffer it */
  3657. XXH_memcpy(state->buffer, input, (size_t)(bEnd-input));
  3658. state->bufferedSize = (XXH32_hash_t)(bEnd-input);
  3659. }
  3660. return XXH_OK;
  3661. }
  3662. XXH_PUBLIC_API XXH_errorcode
  3663. XXH3_64bits_update(XXH3_state_t* state, const void* input, size_t len)
  3664. {
  3665. return XXH3_update(state, (const xxh_u8*)input, len,
  3666. XXH3_accumulate_512, XXH3_scrambleAcc);
  3667. }
  3668. XXH_FORCE_INLINE void
  3669. XXH3_digest_long (XXH64_hash_t* acc,
  3670. const XXH3_state_t* state,
  3671. const unsigned char* secret)
  3672. {
  3673. /*
  3674. * Digest on a local copy. This way, the state remains unaltered, and it can
  3675. * continue ingesting more input afterwards.
  3676. */
  3677. memcpy(acc, state->acc, sizeof(state->acc));
  3678. if (state->bufferedSize >= XXH_STRIPE_LEN) {
  3679. size_t const nbStripes = (state->bufferedSize - 1) / XXH_STRIPE_LEN;
  3680. size_t nbStripesSoFar = state->nbStripesSoFar;
  3681. XXH3_consumeStripes(acc,
  3682. &nbStripesSoFar, state->nbStripesPerBlock,
  3683. state->buffer, nbStripes,
  3684. secret, state->secretLimit,
  3685. XXH3_accumulate_512, XXH3_scrambleAcc);
  3686. /* last stripe */
  3687. XXH3_accumulate_512(acc,
  3688. state->buffer + state->bufferedSize - XXH_STRIPE_LEN,
  3689. secret + state->secretLimit - XXH_SECRET_LASTACC_START);
  3690. } else { /* bufferedSize < XXH_STRIPE_LEN */
  3691. xxh_u8 lastStripe[XXH_STRIPE_LEN];
  3692. size_t const catchupSize = XXH_STRIPE_LEN - state->bufferedSize;
  3693. XXH_ASSERT(state->bufferedSize > 0); /* there is always some input buffered */
  3694. memcpy(lastStripe, state->buffer + sizeof(state->buffer) - catchupSize, catchupSize);
  3695. memcpy(lastStripe + catchupSize, state->buffer, state->bufferedSize);
  3696. XXH3_accumulate_512(acc,
  3697. lastStripe,
  3698. secret + state->secretLimit - XXH_SECRET_LASTACC_START);
  3699. }
  3700. }
  3701. XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_digest (const XXH3_state_t* state)
  3702. {
  3703. const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
  3704. if (state->totalLen > XXH3_MIDSIZE_MAX) {
  3705. XXH_ALIGN(XXH_ACC_ALIGN) XXH64_hash_t acc[XXH_ACC_NB];
  3706. XXH3_digest_long(acc, state, secret);
  3707. return XXH3_mergeAccs(acc,
  3708. secret + XXH_SECRET_MERGEACCS_START,
  3709. (xxh_u64)state->totalLen * XXH_PRIME64_1);
  3710. }
  3711. /* totalLen <= XXH3_MIDSIZE_MAX: digesting a short input */
  3712. if (state->seed)
  3713. return XXH3_64bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
  3714. return XXH3_64bits_withSecret(state->buffer, (size_t)(state->totalLen),
  3715. secret, state->secretLimit + XXH_STRIPE_LEN);
  3716. }
  3717. #define XXH_MIN(x, y) (((x) > (y)) ? (y) : (x))
  3718. XXH_PUBLIC_API void
  3719. XXH3_generateSecret(void* secretBuffer, const void* customSeed, size_t customSeedSize)
  3720. {
  3721. XXH_ASSERT(secretBuffer != NULL);
  3722. if (customSeedSize == 0) {
  3723. memcpy(secretBuffer, XXH3_kSecret, XXH_SECRET_DEFAULT_SIZE);
  3724. return;
  3725. }
  3726. XXH_ASSERT(customSeed != NULL);
  3727. { size_t const segmentSize = sizeof(XXH128_hash_t);
  3728. size_t const nbSegments = XXH_SECRET_DEFAULT_SIZE / segmentSize;
  3729. XXH128_canonical_t scrambler;
  3730. XXH64_hash_t seeds[12];
  3731. size_t segnb;
  3732. XXH_ASSERT(nbSegments == 12);
  3733. XXH_ASSERT(segmentSize * nbSegments == XXH_SECRET_DEFAULT_SIZE); /* exact multiple */
  3734. XXH128_canonicalFromHash(&scrambler, XXH128(customSeed, customSeedSize, 0));
  3735. /*
  3736. * Copy customSeed to seeds[], truncating or repeating as necessary.
  3737. */
  3738. { size_t toFill = XXH_MIN(customSeedSize, sizeof(seeds));
  3739. size_t filled = toFill;
  3740. memcpy(seeds, customSeed, toFill);
  3741. while (filled < sizeof(seeds)) {
  3742. toFill = XXH_MIN(filled, sizeof(seeds) - filled);
  3743. memcpy((char*)seeds + filled, seeds, toFill);
  3744. filled += toFill;
  3745. } }
  3746. /* generate secret */
  3747. memcpy(secretBuffer, &scrambler, sizeof(scrambler));
  3748. for (segnb=1; segnb < nbSegments; segnb++) {
  3749. size_t const segmentStart = segnb * segmentSize;
  3750. XXH128_canonical_t segment;
  3751. XXH128_canonicalFromHash(&segment,
  3752. XXH128(&scrambler, sizeof(scrambler), XXH_readLE64(seeds + segnb) + segnb) );
  3753. memcpy((char*)secretBuffer + segmentStart, &segment, sizeof(segment));
  3754. } }
  3755. }
  3756. /* ==========================================
  3757. * XXH3 128 bits (a.k.a XXH128)
  3758. * ==========================================
  3759. * XXH3's 128-bit variant has better mixing and strength than the 64-bit variant,
  3760. * even without counting the significantly larger output size.
  3761. *
  3762. * For example, extra steps are taken to avoid the seed-dependent collisions
  3763. * in 17-240 byte inputs (See XXH3_mix16B and XXH128_mix32B).
  3764. *
  3765. * This strength naturally comes at the cost of some speed, especially on short
  3766. * lengths. Note that longer hashes are about as fast as the 64-bit version
  3767. * due to it using only a slight modification of the 64-bit loop.
  3768. *
  3769. * XXH128 is also more oriented towards 64-bit machines. It is still extremely
  3770. * fast for a _128-bit_ hash on 32-bit (it usually clears XXH64).
  3771. */
  3772. XXH_FORCE_INLINE XXH128_hash_t
  3773. XXH3_len_1to3_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  3774. {
  3775. /* A doubled version of 1to3_64b with different constants. */
  3776. XXH_ASSERT(input != NULL);
  3777. XXH_ASSERT(1 <= len && len <= 3);
  3778. XXH_ASSERT(secret != NULL);
  3779. /*
  3780. * len = 1: combinedl = { input[0], 0x01, input[0], input[0] }
  3781. * len = 2: combinedl = { input[1], 0x02, input[0], input[1] }
  3782. * len = 3: combinedl = { input[2], 0x03, input[0], input[1] }
  3783. */
  3784. { xxh_u8 const c1 = input[0];
  3785. xxh_u8 const c2 = input[len >> 1];
  3786. xxh_u8 const c3 = input[len - 1];
  3787. xxh_u32 const combinedl = ((xxh_u32)c1 <<16) | ((xxh_u32)c2 << 24)
  3788. | ((xxh_u32)c3 << 0) | ((xxh_u32)len << 8);
  3789. xxh_u32 const combinedh = XXH_rotl32(XXH_swap32(combinedl), 13);
  3790. xxh_u64 const bitflipl = (XXH_readLE32(secret) ^ XXH_readLE32(secret+4)) + seed;
  3791. xxh_u64 const bitfliph = (XXH_readLE32(secret+8) ^ XXH_readLE32(secret+12)) - seed;
  3792. xxh_u64 const keyed_lo = (xxh_u64)combinedl ^ bitflipl;
  3793. xxh_u64 const keyed_hi = (xxh_u64)combinedh ^ bitfliph;
  3794. XXH128_hash_t h128;
  3795. h128.low64 = XXH64_avalanche(keyed_lo);
  3796. h128.high64 = XXH64_avalanche(keyed_hi);
  3797. return h128;
  3798. }
  3799. }
  3800. XXH_FORCE_INLINE XXH128_hash_t
  3801. XXH3_len_4to8_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  3802. {
  3803. XXH_ASSERT(input != NULL);
  3804. XXH_ASSERT(secret != NULL);
  3805. XXH_ASSERT(4 <= len && len <= 8);
  3806. seed ^= (xxh_u64)XXH_swap32((xxh_u32)seed) << 32;
  3807. { xxh_u32 const input_lo = XXH_readLE32(input);
  3808. xxh_u32 const input_hi = XXH_readLE32(input + len - 4);
  3809. xxh_u64 const input_64 = input_lo + ((xxh_u64)input_hi << 32);
  3810. xxh_u64 const bitflip = (XXH_readLE64(secret+16) ^ XXH_readLE64(secret+24)) + seed;
  3811. xxh_u64 const keyed = input_64 ^ bitflip;
  3812. /* Shift len to the left to ensure it is even, this avoids even multiplies. */
  3813. XXH128_hash_t m128 = XXH_mult64to128(keyed, XXH_PRIME64_1 + (len << 2));
  3814. m128.high64 += (m128.low64 << 1);
  3815. m128.low64 ^= (m128.high64 >> 3);
  3816. m128.low64 = XXH_xorshift64(m128.low64, 35);
  3817. m128.low64 *= 0x9FB21C651E98DF25ULL;
  3818. m128.low64 = XXH_xorshift64(m128.low64, 28);
  3819. m128.high64 = XXH3_avalanche(m128.high64);
  3820. return m128;
  3821. }
  3822. }
  3823. XXH_FORCE_INLINE XXH128_hash_t
  3824. XXH3_len_9to16_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  3825. {
  3826. XXH_ASSERT(input != NULL);
  3827. XXH_ASSERT(secret != NULL);
  3828. XXH_ASSERT(9 <= len && len <= 16);
  3829. { xxh_u64 const bitflipl = (XXH_readLE64(secret+32) ^ XXH_readLE64(secret+40)) - seed;
  3830. xxh_u64 const bitfliph = (XXH_readLE64(secret+48) ^ XXH_readLE64(secret+56)) + seed;
  3831. xxh_u64 const input_lo = XXH_readLE64(input);
  3832. xxh_u64 input_hi = XXH_readLE64(input + len - 8);
  3833. XXH128_hash_t m128 = XXH_mult64to128(input_lo ^ input_hi ^ bitflipl, XXH_PRIME64_1);
  3834. /*
  3835. * Put len in the middle of m128 to ensure that the length gets mixed to
  3836. * both the low and high bits in the 128x64 multiply below.
  3837. */
  3838. m128.low64 += (xxh_u64)(len - 1) << 54;
  3839. input_hi ^= bitfliph;
  3840. /*
  3841. * Add the high 32 bits of input_hi to the high 32 bits of m128, then
  3842. * add the long product of the low 32 bits of input_hi and XXH_PRIME32_2 to
  3843. * the high 64 bits of m128.
  3844. *
  3845. * The best approach to this operation is different on 32-bit and 64-bit.
  3846. */
  3847. if (sizeof(void *) < sizeof(xxh_u64)) { /* 32-bit */
  3848. /*
  3849. * 32-bit optimized version, which is more readable.
  3850. *
  3851. * On 32-bit, it removes an ADC and delays a dependency between the two
  3852. * halves of m128.high64, but it generates an extra mask on 64-bit.
  3853. */
  3854. m128.high64 += (input_hi & 0xFFFFFFFF00000000ULL) + XXH_mult32to64((xxh_u32)input_hi, XXH_PRIME32_2);
  3855. } else {
  3856. /*
  3857. * 64-bit optimized (albeit more confusing) version.
  3858. *
  3859. * Uses some properties of addition and multiplication to remove the mask:
  3860. *
  3861. * Let:
  3862. * a = input_hi.lo = (input_hi & 0x00000000FFFFFFFF)
  3863. * b = input_hi.hi = (input_hi & 0xFFFFFFFF00000000)
  3864. * c = XXH_PRIME32_2
  3865. *
  3866. * a + (b * c)
  3867. * Inverse Property: x + y - x == y
  3868. * a + (b * (1 + c - 1))
  3869. * Distributive Property: x * (y + z) == (x * y) + (x * z)
  3870. * a + (b * 1) + (b * (c - 1))
  3871. * Identity Property: x * 1 == x
  3872. * a + b + (b * (c - 1))
  3873. *
  3874. * Substitute a, b, and c:
  3875. * input_hi.hi + input_hi.lo + ((xxh_u64)input_hi.lo * (XXH_PRIME32_2 - 1))
  3876. *
  3877. * Since input_hi.hi + input_hi.lo == input_hi, we get this:
  3878. * input_hi + ((xxh_u64)input_hi.lo * (XXH_PRIME32_2 - 1))
  3879. */
  3880. m128.high64 += input_hi + XXH_mult32to64((xxh_u32)input_hi, XXH_PRIME32_2 - 1);
  3881. }
  3882. /* m128 ^= XXH_swap64(m128 >> 64); */
  3883. m128.low64 ^= XXH_swap64(m128.high64);
  3884. { /* 128x64 multiply: h128 = m128 * XXH_PRIME64_2; */
  3885. XXH128_hash_t h128 = XXH_mult64to128(m128.low64, XXH_PRIME64_2);
  3886. h128.high64 += m128.high64 * XXH_PRIME64_2;
  3887. h128.low64 = XXH3_avalanche(h128.low64);
  3888. h128.high64 = XXH3_avalanche(h128.high64);
  3889. return h128;
  3890. } }
  3891. }
  3892. /*
  3893. * Assumption: `secret` size is >= XXH3_SECRET_SIZE_MIN
  3894. */
  3895. XXH_FORCE_INLINE XXH128_hash_t
  3896. XXH3_len_0to16_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  3897. {
  3898. XXH_ASSERT(len <= 16);
  3899. { if (len > 8) return XXH3_len_9to16_128b(input, len, secret, seed);
  3900. if (len >= 4) return XXH3_len_4to8_128b(input, len, secret, seed);
  3901. if (len) return XXH3_len_1to3_128b(input, len, secret, seed);
  3902. { XXH128_hash_t h128;
  3903. xxh_u64 const bitflipl = XXH_readLE64(secret+64) ^ XXH_readLE64(secret+72);
  3904. xxh_u64 const bitfliph = XXH_readLE64(secret+80) ^ XXH_readLE64(secret+88);
  3905. h128.low64 = XXH64_avalanche(seed ^ bitflipl);
  3906. h128.high64 = XXH64_avalanche( seed ^ bitfliph);
  3907. return h128;
  3908. } }
  3909. }
  3910. /*
  3911. * A bit slower than XXH3_mix16B, but handles multiply by zero better.
  3912. */
  3913. XXH_FORCE_INLINE XXH128_hash_t
  3914. XXH128_mix32B(XXH128_hash_t acc, const xxh_u8* input_1, const xxh_u8* input_2,
  3915. const xxh_u8* secret, XXH64_hash_t seed)
  3916. {
  3917. acc.low64 += XXH3_mix16B (input_1, secret+0, seed);
  3918. acc.low64 ^= XXH_readLE64(input_2) + XXH_readLE64(input_2 + 8);
  3919. acc.high64 += XXH3_mix16B (input_2, secret+16, seed);
  3920. acc.high64 ^= XXH_readLE64(input_1) + XXH_readLE64(input_1 + 8);
  3921. return acc;
  3922. }
  3923. XXH_FORCE_INLINE XXH128_hash_t
  3924. XXH3_len_17to128_128b(const xxh_u8* XXH_RESTRICT input, size_t len,
  3925. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  3926. XXH64_hash_t seed)
  3927. {
  3928. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
  3929. XXH_ASSERT(16 < len && len <= 128);
  3930. { XXH128_hash_t acc;
  3931. acc.low64 = len * XXH_PRIME64_1;
  3932. acc.high64 = 0;
  3933. if (len > 32) {
  3934. if (len > 64) {
  3935. if (len > 96) {
  3936. acc = XXH128_mix32B(acc, input+48, input+len-64, secret+96, seed);
  3937. }
  3938. acc = XXH128_mix32B(acc, input+32, input+len-48, secret+64, seed);
  3939. }
  3940. acc = XXH128_mix32B(acc, input+16, input+len-32, secret+32, seed);
  3941. }
  3942. acc = XXH128_mix32B(acc, input, input+len-16, secret, seed);
  3943. { XXH128_hash_t h128;
  3944. h128.low64 = acc.low64 + acc.high64;
  3945. h128.high64 = (acc.low64 * XXH_PRIME64_1)
  3946. + (acc.high64 * XXH_PRIME64_4)
  3947. + ((len - seed) * XXH_PRIME64_2);
  3948. h128.low64 = XXH3_avalanche(h128.low64);
  3949. h128.high64 = (XXH64_hash_t)0 - XXH3_avalanche(h128.high64);
  3950. return h128;
  3951. }
  3952. }
  3953. }
  3954. XXH_NO_INLINE XXH128_hash_t
  3955. XXH3_len_129to240_128b(const xxh_u8* XXH_RESTRICT input, size_t len,
  3956. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  3957. XXH64_hash_t seed)
  3958. {
  3959. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
  3960. XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
  3961. { XXH128_hash_t acc;
  3962. int const nbRounds = (int)len / 32;
  3963. int i;
  3964. acc.low64 = len * XXH_PRIME64_1;
  3965. acc.high64 = 0;
  3966. for (i=0; i<4; i++) {
  3967. acc = XXH128_mix32B(acc,
  3968. input + (32 * i),
  3969. input + (32 * i) + 16,
  3970. secret + (32 * i),
  3971. seed);
  3972. }
  3973. acc.low64 = XXH3_avalanche(acc.low64);
  3974. acc.high64 = XXH3_avalanche(acc.high64);
  3975. XXH_ASSERT(nbRounds >= 4);
  3976. for (i=4 ; i < nbRounds; i++) {
  3977. acc = XXH128_mix32B(acc,
  3978. input + (32 * i),
  3979. input + (32 * i) + 16,
  3980. secret + XXH3_MIDSIZE_STARTOFFSET + (32 * (i - 4)),
  3981. seed);
  3982. }
  3983. /* last bytes */
  3984. acc = XXH128_mix32B(acc,
  3985. input + len - 16,
  3986. input + len - 32,
  3987. secret + XXH3_SECRET_SIZE_MIN - XXH3_MIDSIZE_LASTOFFSET - 16,
  3988. 0ULL - seed);
  3989. { XXH128_hash_t h128;
  3990. h128.low64 = acc.low64 + acc.high64;
  3991. h128.high64 = (acc.low64 * XXH_PRIME64_1)
  3992. + (acc.high64 * XXH_PRIME64_4)
  3993. + ((len - seed) * XXH_PRIME64_2);
  3994. h128.low64 = XXH3_avalanche(h128.low64);
  3995. h128.high64 = (XXH64_hash_t)0 - XXH3_avalanche(h128.high64);
  3996. return h128;
  3997. }
  3998. }
  3999. }
  4000. XXH_FORCE_INLINE XXH128_hash_t
  4001. XXH3_hashLong_128b_internal(const void* XXH_RESTRICT input, size_t len,
  4002. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  4003. XXH3_f_accumulate_512 f_acc512,
  4004. XXH3_f_scrambleAcc f_scramble)
  4005. {
  4006. XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[XXH_ACC_NB] = XXH3_INIT_ACC;
  4007. XXH3_hashLong_internal_loop(acc, (const xxh_u8*)input, len, secret, secretSize, f_acc512, f_scramble);
  4008. /* converge into final hash */
  4009. XXH_STATIC_ASSERT(sizeof(acc) == 64);
  4010. XXH_ASSERT(secretSize >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
  4011. { XXH128_hash_t h128;
  4012. h128.low64 = XXH3_mergeAccs(acc,
  4013. secret + XXH_SECRET_MERGEACCS_START,
  4014. (xxh_u64)len * XXH_PRIME64_1);
  4015. h128.high64 = XXH3_mergeAccs(acc,
  4016. secret + secretSize
  4017. - sizeof(acc) - XXH_SECRET_MERGEACCS_START,
  4018. ~((xxh_u64)len * XXH_PRIME64_2));
  4019. return h128;
  4020. }
  4021. }
  4022. /*
  4023. * It's important for performance that XXH3_hashLong is not inlined.
  4024. */
  4025. XXH_NO_INLINE XXH128_hash_t
  4026. XXH3_hashLong_128b_default(const void* XXH_RESTRICT input, size_t len,
  4027. XXH64_hash_t seed64,
  4028. const void* XXH_RESTRICT secret, size_t secretLen)
  4029. {
  4030. (void)seed64; (void)secret; (void)secretLen;
  4031. return XXH3_hashLong_128b_internal(input, len, XXH3_kSecret, sizeof(XXH3_kSecret),
  4032. XXH3_accumulate_512, XXH3_scrambleAcc);
  4033. }
  4034. /*
  4035. * It's important for performance that XXH3_hashLong is not inlined.
  4036. */
  4037. XXH_NO_INLINE XXH128_hash_t
  4038. XXH3_hashLong_128b_withSecret(const void* XXH_RESTRICT input, size_t len,
  4039. XXH64_hash_t seed64,
  4040. const void* XXH_RESTRICT secret, size_t secretLen)
  4041. {
  4042. (void)seed64;
  4043. return XXH3_hashLong_128b_internal(input, len, (const xxh_u8*)secret, secretLen,
  4044. XXH3_accumulate_512, XXH3_scrambleAcc);
  4045. }
  4046. XXH_FORCE_INLINE XXH128_hash_t
  4047. XXH3_hashLong_128b_withSeed_internal(const void* XXH_RESTRICT input, size_t len,
  4048. XXH64_hash_t seed64,
  4049. XXH3_f_accumulate_512 f_acc512,
  4050. XXH3_f_scrambleAcc f_scramble,
  4051. XXH3_f_initCustomSecret f_initSec)
  4052. {
  4053. if (seed64 == 0)
  4054. return XXH3_hashLong_128b_internal(input, len,
  4055. XXH3_kSecret, sizeof(XXH3_kSecret),
  4056. f_acc512, f_scramble);
  4057. { XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
  4058. f_initSec(secret, seed64);
  4059. return XXH3_hashLong_128b_internal(input, len, (const xxh_u8*)secret, sizeof(secret),
  4060. f_acc512, f_scramble);
  4061. }
  4062. }
  4063. /*
  4064. * It's important for performance that XXH3_hashLong is not inlined.
  4065. */
  4066. XXH_NO_INLINE XXH128_hash_t
  4067. XXH3_hashLong_128b_withSeed(const void* input, size_t len,
  4068. XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen)
  4069. {
  4070. (void)secret; (void)secretLen;
  4071. return XXH3_hashLong_128b_withSeed_internal(input, len, seed64,
  4072. XXH3_accumulate_512, XXH3_scrambleAcc, XXH3_initCustomSecret);
  4073. }
  4074. typedef XXH128_hash_t (*XXH3_hashLong128_f)(const void* XXH_RESTRICT, size_t,
  4075. XXH64_hash_t, const void* XXH_RESTRICT, size_t);
  4076. XXH_FORCE_INLINE XXH128_hash_t
  4077. XXH3_128bits_internal(const void* input, size_t len,
  4078. XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen,
  4079. XXH3_hashLong128_f f_hl128)
  4080. {
  4081. XXH_ASSERT(secretLen >= XXH3_SECRET_SIZE_MIN);
  4082. /*
  4083. * If an action is to be taken if `secret` conditions are not respected,
  4084. * it should be done here.
  4085. * For now, it's a contract pre-condition.
  4086. * Adding a check and a branch here would cost performance at every hash.
  4087. */
  4088. if (len <= 16)
  4089. return XXH3_len_0to16_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, seed64);
  4090. if (len <= 128)
  4091. return XXH3_len_17to128_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
  4092. if (len <= XXH3_MIDSIZE_MAX)
  4093. return XXH3_len_129to240_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
  4094. return f_hl128(input, len, seed64, secret, secretLen);
  4095. }
  4096. /* === Public XXH128 API === */
  4097. XXH_PUBLIC_API XXH128_hash_t XXH3_128bits(const void* input, size_t len)
  4098. {
  4099. return XXH3_128bits_internal(input, len, 0,
  4100. XXH3_kSecret, sizeof(XXH3_kSecret),
  4101. XXH3_hashLong_128b_default);
  4102. }
  4103. XXH_PUBLIC_API XXH128_hash_t
  4104. XXH3_128bits_withSecret(const void* input, size_t len, const void* secret, size_t secretSize)
  4105. {
  4106. return XXH3_128bits_internal(input, len, 0,
  4107. (const xxh_u8*)secret, secretSize,
  4108. XXH3_hashLong_128b_withSecret);
  4109. }
  4110. XXH_PUBLIC_API XXH128_hash_t
  4111. XXH3_128bits_withSeed(const void* input, size_t len, XXH64_hash_t seed)
  4112. {
  4113. return XXH3_128bits_internal(input, len, seed,
  4114. XXH3_kSecret, sizeof(XXH3_kSecret),
  4115. XXH3_hashLong_128b_withSeed);
  4116. }
  4117. XXH_PUBLIC_API XXH128_hash_t
  4118. XXH128(const void* input, size_t len, XXH64_hash_t seed)
  4119. {
  4120. return XXH3_128bits_withSeed(input, len, seed);
  4121. }
  4122. /* === XXH3 128-bit streaming === */
  4123. /*
  4124. * All the functions are actually the same as for 64-bit streaming variant.
  4125. * The only difference is the finalizatiom routine.
  4126. */
  4127. static void
  4128. XXH3_128bits_reset_internal(XXH3_state_t* statePtr,
  4129. XXH64_hash_t seed,
  4130. const void* secret, size_t secretSize)
  4131. {
  4132. XXH3_64bits_reset_internal(statePtr, seed, secret, secretSize);
  4133. }
  4134. XXH_PUBLIC_API XXH_errorcode
  4135. XXH3_128bits_reset(XXH3_state_t* statePtr)
  4136. {
  4137. if (statePtr == NULL) return XXH_ERROR;
  4138. XXH3_128bits_reset_internal(statePtr, 0, XXH3_kSecret, XXH_SECRET_DEFAULT_SIZE);
  4139. return XXH_OK;
  4140. }
  4141. XXH_PUBLIC_API XXH_errorcode
  4142. XXH3_128bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize)
  4143. {
  4144. if (statePtr == NULL) return XXH_ERROR;
  4145. XXH3_128bits_reset_internal(statePtr, 0, secret, secretSize);
  4146. if (secret == NULL) return XXH_ERROR;
  4147. if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
  4148. return XXH_OK;
  4149. }
  4150. XXH_PUBLIC_API XXH_errorcode
  4151. XXH3_128bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed)
  4152. {
  4153. if (statePtr == NULL) return XXH_ERROR;
  4154. if (seed==0) return XXH3_128bits_reset(statePtr);
  4155. if (seed != statePtr->seed) XXH3_initCustomSecret(statePtr->customSecret, seed);
  4156. XXH3_128bits_reset_internal(statePtr, seed, NULL, XXH_SECRET_DEFAULT_SIZE);
  4157. return XXH_OK;
  4158. }
  4159. XXH_PUBLIC_API XXH_errorcode
  4160. XXH3_128bits_update(XXH3_state_t* state, const void* input, size_t len)
  4161. {
  4162. return XXH3_update(state, (const xxh_u8*)input, len,
  4163. XXH3_accumulate_512, XXH3_scrambleAcc);
  4164. }
  4165. XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_digest (const XXH3_state_t* state)
  4166. {
  4167. const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
  4168. if (state->totalLen > XXH3_MIDSIZE_MAX) {
  4169. XXH_ALIGN(XXH_ACC_ALIGN) XXH64_hash_t acc[XXH_ACC_NB];
  4170. XXH3_digest_long(acc, state, secret);
  4171. XXH_ASSERT(state->secretLimit + XXH_STRIPE_LEN >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
  4172. { XXH128_hash_t h128;
  4173. h128.low64 = XXH3_mergeAccs(acc,
  4174. secret + XXH_SECRET_MERGEACCS_START,
  4175. (xxh_u64)state->totalLen * XXH_PRIME64_1);
  4176. h128.high64 = XXH3_mergeAccs(acc,
  4177. secret + state->secretLimit + XXH_STRIPE_LEN
  4178. - sizeof(acc) - XXH_SECRET_MERGEACCS_START,
  4179. ~((xxh_u64)state->totalLen * XXH_PRIME64_2));
  4180. return h128;
  4181. }
  4182. }
  4183. /* len <= XXH3_MIDSIZE_MAX : short code */
  4184. if (state->seed)
  4185. return XXH3_128bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
  4186. return XXH3_128bits_withSecret(state->buffer, (size_t)(state->totalLen),
  4187. secret, state->secretLimit + XXH_STRIPE_LEN);
  4188. }
  4189. /* 128-bit utility functions */
  4190. #include <string.h> /* memcmp, memcpy */
  4191. /* return : 1 is equal, 0 if different */
  4192. XXH_PUBLIC_API int XXH128_isEqual(XXH128_hash_t h1, XXH128_hash_t h2)
  4193. {
  4194. /* note : XXH128_hash_t is compact, it has no padding byte */
  4195. return !(memcmp(&h1, &h2, sizeof(h1)));
  4196. }
  4197. /* This prototype is compatible with stdlib's qsort().
  4198. * return : >0 if *h128_1 > *h128_2
  4199. * <0 if *h128_1 < *h128_2
  4200. * =0 if *h128_1 == *h128_2 */
  4201. XXH_PUBLIC_API int XXH128_cmp(const void* h128_1, const void* h128_2)
  4202. {
  4203. XXH128_hash_t const h1 = *(const XXH128_hash_t*)h128_1;
  4204. XXH128_hash_t const h2 = *(const XXH128_hash_t*)h128_2;
  4205. int const hcmp = (h1.high64 > h2.high64) - (h2.high64 > h1.high64);
  4206. /* note : bets that, in most cases, hash values are different */
  4207. if (hcmp) return hcmp;
  4208. return (h1.low64 > h2.low64) - (h2.low64 > h1.low64);
  4209. }
  4210. /*====== Canonical representation ======*/
  4211. XXH_PUBLIC_API void
  4212. XXH128_canonicalFromHash(XXH128_canonical_t* dst, XXH128_hash_t hash)
  4213. {
  4214. XXH_STATIC_ASSERT(sizeof(XXH128_canonical_t) == sizeof(XXH128_hash_t));
  4215. if (XXH_CPU_LITTLE_ENDIAN) {
  4216. hash.high64 = XXH_swap64(hash.high64);
  4217. hash.low64 = XXH_swap64(hash.low64);
  4218. }
  4219. memcpy(dst, &hash.high64, sizeof(hash.high64));
  4220. memcpy((char*)dst + sizeof(hash.high64), &hash.low64, sizeof(hash.low64));
  4221. }
  4222. XXH_PUBLIC_API XXH128_hash_t
  4223. XXH128_hashFromCanonical(const XXH128_canonical_t* src)
  4224. {
  4225. XXH128_hash_t h;
  4226. h.high64 = XXH_readBE64(src);
  4227. h.low64 = XXH_readBE64(src->digest + 8);
  4228. return h;
  4229. }
  4230. /* Pop our optimization override from above */
  4231. #if XXH_VECTOR == XXH_AVX2 /* AVX2 */ \
  4232. && defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
  4233. && defined(__OPTIMIZE__) && !defined(__OPTIMIZE_SIZE__) /* respect -O0 and -Os */
  4234. # pragma GCC pop_options
  4235. #endif
  4236. #endif /* XXH_NO_LONG_LONG */
  4237. #endif /* XXH_IMPLEMENTATION */
  4238. #if defined (__cplusplus)
  4239. }
  4240. #endif