xxhash.h 208 KB

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