xxhash.h 262 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238
  1. /*
  2. * xxHash - Extremely Fast Hash algorithm
  3. * Header File
  4. * Copyright (C) 2012-2023 Yann Collet
  5. *
  6. * BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * You can contact the author at:
  32. * - xxHash homepage: https://www.xxhash.com
  33. * - xxHash source repository: https://github.com/Cyan4973/xxHash
  34. */
  35. /*!
  36. * @mainpage xxHash
  37. *
  38. * xxHash is an extremely fast non-cryptographic hash algorithm, working at RAM speed
  39. * limits.
  40. *
  41. * It is proposed in four flavors, in three families:
  42. * 1. @ref XXH32_family
  43. * - Classic 32-bit hash function. Simple, compact, and runs on almost all
  44. * 32-bit and 64-bit systems.
  45. * 2. @ref XXH64_family
  46. * - Classic 64-bit adaptation of XXH32. Just as simple, and runs well on most
  47. * 64-bit systems (but _not_ 32-bit systems).
  48. * 3. @ref XXH3_family
  49. * - Modern 64-bit and 128-bit hash function family which features improved
  50. * strength and performance across the board, especially on smaller data.
  51. * It benefits greatly from SIMD and 64-bit without requiring it.
  52. *
  53. * Benchmarks
  54. * ---
  55. * The reference system uses an Intel i7-9700K CPU, and runs Ubuntu x64 20.04.
  56. * The open source benchmark program is compiled with clang v10.0 using -O3 flag.
  57. *
  58. * | Hash Name | ISA ext | Width | Large Data Speed | Small Data Velocity |
  59. * | -------------------- | ------- | ----: | ---------------: | ------------------: |
  60. * | XXH3_64bits() | @b AVX2 | 64 | 59.4 GB/s | 133.1 |
  61. * | MeowHash | AES-NI | 128 | 58.2 GB/s | 52.5 |
  62. * | XXH3_128bits() | @b AVX2 | 128 | 57.9 GB/s | 118.1 |
  63. * | CLHash | PCLMUL | 64 | 37.1 GB/s | 58.1 |
  64. * | XXH3_64bits() | @b SSE2 | 64 | 31.5 GB/s | 133.1 |
  65. * | XXH3_128bits() | @b SSE2 | 128 | 29.6 GB/s | 118.1 |
  66. * | RAM sequential read | | N/A | 28.0 GB/s | N/A |
  67. * | ahash | AES-NI | 64 | 22.5 GB/s | 107.2 |
  68. * | City64 | | 64 | 22.0 GB/s | 76.6 |
  69. * | T1ha2 | | 64 | 22.0 GB/s | 99.0 |
  70. * | City128 | | 128 | 21.7 GB/s | 57.7 |
  71. * | FarmHash | AES-NI | 64 | 21.3 GB/s | 71.9 |
  72. * | XXH64() | | 64 | 19.4 GB/s | 71.0 |
  73. * | SpookyHash | | 64 | 19.3 GB/s | 53.2 |
  74. * | Mum | | 64 | 18.0 GB/s | 67.0 |
  75. * | CRC32C | SSE4.2 | 32 | 13.0 GB/s | 57.9 |
  76. * | XXH32() | | 32 | 9.7 GB/s | 71.9 |
  77. * | City32 | | 32 | 9.1 GB/s | 66.0 |
  78. * | Blake3* | @b AVX2 | 256 | 4.4 GB/s | 8.1 |
  79. * | Murmur3 | | 32 | 3.9 GB/s | 56.1 |
  80. * | SipHash* | | 64 | 3.0 GB/s | 43.2 |
  81. * | Blake3* | @b SSE2 | 256 | 2.4 GB/s | 8.1 |
  82. * | HighwayHash | | 64 | 1.4 GB/s | 6.0 |
  83. * | FNV64 | | 64 | 1.2 GB/s | 62.7 |
  84. * | Blake2* | | 256 | 1.1 GB/s | 5.1 |
  85. * | SHA1* | | 160 | 0.8 GB/s | 5.6 |
  86. * | MD5* | | 128 | 0.6 GB/s | 7.8 |
  87. * @note
  88. * - Hashes which require a specific ISA extension are noted. SSE2 is also noted,
  89. * even though it is mandatory on x64.
  90. * - Hashes with an asterisk are cryptographic. Note that MD5 is non-cryptographic
  91. * by modern standards.
  92. * - Small data velocity is a rough average of algorithm's efficiency for small
  93. * data. For more accurate information, see the wiki.
  94. * - More benchmarks and strength tests are found on the wiki:
  95. * https://github.com/Cyan4973/xxHash/wiki
  96. *
  97. * Usage
  98. * ------
  99. * All xxHash variants use a similar API. Changing the algorithm is a trivial
  100. * substitution.
  101. *
  102. * @pre
  103. * For functions which take an input and length parameter, the following
  104. * requirements are assumed:
  105. * - The range from [`input`, `input + length`) is valid, readable memory.
  106. * - The only exception is if the `length` is `0`, `input` may be `NULL`.
  107. * - For C++, the objects must have the *TriviallyCopyable* property, as the
  108. * functions access bytes directly as if it was an array of `unsigned char`.
  109. *
  110. * @anchor single_shot_example
  111. * **Single Shot**
  112. *
  113. * These functions are stateless functions which hash a contiguous block of memory,
  114. * immediately returning the result. They are the easiest and usually the fastest
  115. * option.
  116. *
  117. * XXH32(), XXH64(), XXH3_64bits(), XXH3_128bits()
  118. *
  119. * @code{.c}
  120. * #include <string.h>
  121. * #include "xxhash.h"
  122. *
  123. * // Example for a function which hashes a null terminated string with XXH32().
  124. * XXH32_hash_t hash_string(const char* string, XXH32_hash_t seed)
  125. * {
  126. * // NULL pointers are only valid if the length is zero
  127. * size_t length = (string == NULL) ? 0 : strlen(string);
  128. * return XXH32(string, length, seed);
  129. * }
  130. * @endcode
  131. *
  132. *
  133. * @anchor streaming_example
  134. * **Streaming**
  135. *
  136. * These groups of functions allow incremental hashing of unknown size, even
  137. * more than what would fit in a size_t.
  138. *
  139. * XXH32_reset(), XXH64_reset(), XXH3_64bits_reset(), XXH3_128bits_reset()
  140. *
  141. * @code{.c}
  142. * #include <stdio.h>
  143. * #include <assert.h>
  144. * #include "xxhash.h"
  145. * // Example for a function which hashes a FILE incrementally with XXH3_64bits().
  146. * XXH64_hash_t hashFile(FILE* f)
  147. * {
  148. * // Allocate a state struct. Do not just use malloc() or new.
  149. * XXH3_state_t* state = XXH3_createState();
  150. * assert(state != NULL && "Out of memory!");
  151. * // Reset the state to start a new hashing session.
  152. * XXH3_64bits_reset(state);
  153. * char buffer[4096];
  154. * size_t count;
  155. * // Read the file in chunks
  156. * while ((count = fread(buffer, 1, sizeof(buffer), f)) != 0) {
  157. * // Run update() as many times as necessary to process the data
  158. * XXH3_64bits_update(state, buffer, count);
  159. * }
  160. * // Retrieve the finalized hash. This will not change the state.
  161. * XXH64_hash_t result = XXH3_64bits_digest(state);
  162. * // Free the state. Do not use free().
  163. * XXH3_freeState(state);
  164. * return result;
  165. * }
  166. * @endcode
  167. *
  168. * Streaming functions generate the xxHash value from an incremental input.
  169. * This method is slower than single-call functions, due to state management.
  170. * For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized.
  171. *
  172. * An XXH state must first be allocated using `XXH*_createState()`.
  173. *
  174. * Start a new hash by initializing the state with a seed using `XXH*_reset()`.
  175. *
  176. * Then, feed the hash state by calling `XXH*_update()` as many times as necessary.
  177. *
  178. * The function returns an error code, with 0 meaning OK, and any other value
  179. * meaning there is an error.
  180. *
  181. * Finally, a hash value can be produced anytime, by using `XXH*_digest()`.
  182. * This function returns the nn-bits hash as an int or long long.
  183. *
  184. * It's still possible to continue inserting input into the hash state after a
  185. * digest, and generate new hash values later on by invoking `XXH*_digest()`.
  186. *
  187. * When done, release the state using `XXH*_freeState()`.
  188. *
  189. *
  190. * @anchor canonical_representation_example
  191. * **Canonical Representation**
  192. *
  193. * The default return values from XXH functions are unsigned 32, 64 and 128 bit
  194. * integers.
  195. * This the simplest and fastest format for further post-processing.
  196. *
  197. * However, this leaves open the question of what is the order on the byte level,
  198. * since little and big endian conventions will store the same number differently.
  199. *
  200. * The canonical representation settles this issue by mandating big-endian
  201. * convention, the same convention as human-readable numbers (large digits first).
  202. *
  203. * When writing hash values to storage, sending them over a network, or printing
  204. * them, it's highly recommended to use the canonical representation to ensure
  205. * portability across a wider range of systems, present and future.
  206. *
  207. * The following functions allow transformation of hash values to and from
  208. * canonical format.
  209. *
  210. * XXH32_canonicalFromHash(), XXH32_hashFromCanonical(),
  211. * XXH64_canonicalFromHash(), XXH64_hashFromCanonical(),
  212. * XXH128_canonicalFromHash(), XXH128_hashFromCanonical(),
  213. *
  214. * @code{.c}
  215. * #include <stdio.h>
  216. * #include "xxhash.h"
  217. *
  218. * // Example for a function which prints XXH32_hash_t in human readable format
  219. * void printXxh32(XXH32_hash_t hash)
  220. * {
  221. * XXH32_canonical_t cano;
  222. * XXH32_canonicalFromHash(&cano, hash);
  223. * size_t i;
  224. * for(i = 0; i < sizeof(cano.digest); ++i) {
  225. * printf("%02x", cano.digest[i]);
  226. * }
  227. * printf("\n");
  228. * }
  229. *
  230. * // Example for a function which converts XXH32_canonical_t to XXH32_hash_t
  231. * XXH32_hash_t convertCanonicalToXxh32(XXH32_canonical_t cano)
  232. * {
  233. * XXH32_hash_t hash = XXH32_hashFromCanonical(&cano);
  234. * return hash;
  235. * }
  236. * @endcode
  237. *
  238. *
  239. * @file xxhash.h
  240. * xxHash prototypes and implementation
  241. */
  242. #if defined (__cplusplus)
  243. extern "C" {
  244. #endif
  245. /* ****************************
  246. * INLINE mode
  247. ******************************/
  248. /*!
  249. * @defgroup public Public API
  250. * Contains details on the public xxHash functions.
  251. * @{
  252. */
  253. #ifdef XXH_DOXYGEN
  254. /*!
  255. * @brief Gives access to internal state declaration, required for static allocation.
  256. *
  257. * Incompatible with dynamic linking, due to risks of ABI changes.
  258. *
  259. * Usage:
  260. * @code{.c}
  261. * #define XXH_STATIC_LINKING_ONLY
  262. * #include "xxhash.h"
  263. * @endcode
  264. */
  265. # define XXH_STATIC_LINKING_ONLY
  266. /* Do not undef XXH_STATIC_LINKING_ONLY for Doxygen */
  267. /*!
  268. * @brief Gives access to internal definitions.
  269. *
  270. * Usage:
  271. * @code{.c}
  272. * #define XXH_STATIC_LINKING_ONLY
  273. * #define XXH_IMPLEMENTATION
  274. * #include "xxhash.h"
  275. * @endcode
  276. */
  277. # define XXH_IMPLEMENTATION
  278. /* Do not undef XXH_IMPLEMENTATION for Doxygen */
  279. /*!
  280. * @brief Exposes the implementation and marks all functions as `inline`.
  281. *
  282. * Use these build macros to inline xxhash into the target unit.
  283. * Inlining improves performance on small inputs, especially when the length is
  284. * expressed as a compile-time constant:
  285. *
  286. * https://fastcompression.blogspot.com/2018/03/xxhash-for-small-keys-impressive-power.html
  287. *
  288. * It also keeps xxHash symbols private to the unit, so they are not exported.
  289. *
  290. * Usage:
  291. * @code{.c}
  292. * #define XXH_INLINE_ALL
  293. * #include "xxhash.h"
  294. * @endcode
  295. * Do not compile and link xxhash.o as a separate object, as it is not useful.
  296. */
  297. # define XXH_INLINE_ALL
  298. # undef XXH_INLINE_ALL
  299. /*!
  300. * @brief Exposes the implementation without marking functions as inline.
  301. */
  302. # define XXH_PRIVATE_API
  303. # undef XXH_PRIVATE_API
  304. /*!
  305. * @brief Emulate a namespace by transparently prefixing all symbols.
  306. *
  307. * If you want to include _and expose_ xxHash functions from within your own
  308. * library, but also want to avoid symbol collisions with other libraries which
  309. * may also include xxHash, you can use @ref XXH_NAMESPACE to automatically prefix
  310. * any public symbol from xxhash library with the value of @ref XXH_NAMESPACE
  311. * (therefore, avoid empty or numeric values).
  312. *
  313. * Note that no change is required within the calling program as long as it
  314. * includes `xxhash.h`: Regular symbol names will be automatically translated
  315. * by this header.
  316. */
  317. # define XXH_NAMESPACE /* YOUR NAME HERE */
  318. # undef XXH_NAMESPACE
  319. #endif
  320. #if (defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)) \
  321. && !defined(XXH_INLINE_ALL_31684351384)
  322. /* this section should be traversed only once */
  323. # define XXH_INLINE_ALL_31684351384
  324. /* give access to the advanced API, required to compile implementations */
  325. # undef XXH_STATIC_LINKING_ONLY /* avoid macro redef */
  326. # define XXH_STATIC_LINKING_ONLY
  327. /* make all functions private */
  328. # undef XXH_PUBLIC_API
  329. # if defined(__GNUC__)
  330. # define XXH_PUBLIC_API static __inline __attribute__((__unused__))
  331. # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  332. # define XXH_PUBLIC_API static inline
  333. # elif defined(_MSC_VER)
  334. # define XXH_PUBLIC_API static __inline
  335. # else
  336. /* note: this version may generate warnings for unused static functions */
  337. # define XXH_PUBLIC_API static
  338. # endif
  339. /*
  340. * This part deals with the special case where a unit wants to inline xxHash,
  341. * but "xxhash.h" has previously been included without XXH_INLINE_ALL,
  342. * such as part of some previously included *.h header file.
  343. * Without further action, the new include would just be ignored,
  344. * and functions would effectively _not_ be inlined (silent failure).
  345. * The following macros solve this situation by prefixing all inlined names,
  346. * avoiding naming collision with previous inclusions.
  347. */
  348. /* Before that, we unconditionally #undef all symbols,
  349. * in case they were already defined with XXH_NAMESPACE.
  350. * They will then be redefined for XXH_INLINE_ALL
  351. */
  352. # undef XXH_versionNumber
  353. /* XXH32 */
  354. # undef XXH32
  355. # undef XXH32_createState
  356. # undef XXH32_freeState
  357. # undef XXH32_reset
  358. # undef XXH32_update
  359. # undef XXH32_digest
  360. # undef XXH32_copyState
  361. # undef XXH32_canonicalFromHash
  362. # undef XXH32_hashFromCanonical
  363. /* XXH64 */
  364. # undef XXH64
  365. # undef XXH64_createState
  366. # undef XXH64_freeState
  367. # undef XXH64_reset
  368. # undef XXH64_update
  369. # undef XXH64_digest
  370. # undef XXH64_copyState
  371. # undef XXH64_canonicalFromHash
  372. # undef XXH64_hashFromCanonical
  373. /* XXH3_64bits */
  374. # undef XXH3_64bits
  375. # undef XXH3_64bits_withSecret
  376. # undef XXH3_64bits_withSeed
  377. # undef XXH3_64bits_withSecretandSeed
  378. # undef XXH3_createState
  379. # undef XXH3_freeState
  380. # undef XXH3_copyState
  381. # undef XXH3_64bits_reset
  382. # undef XXH3_64bits_reset_withSeed
  383. # undef XXH3_64bits_reset_withSecret
  384. # undef XXH3_64bits_update
  385. # undef XXH3_64bits_digest
  386. # undef XXH3_generateSecret
  387. /* XXH3_128bits */
  388. # undef XXH128
  389. # undef XXH3_128bits
  390. # undef XXH3_128bits_withSeed
  391. # undef XXH3_128bits_withSecret
  392. # undef XXH3_128bits_reset
  393. # undef XXH3_128bits_reset_withSeed
  394. # undef XXH3_128bits_reset_withSecret
  395. # undef XXH3_128bits_reset_withSecretandSeed
  396. # undef XXH3_128bits_update
  397. # undef XXH3_128bits_digest
  398. # undef XXH128_isEqual
  399. # undef XXH128_cmp
  400. # undef XXH128_canonicalFromHash
  401. # undef XXH128_hashFromCanonical
  402. /* Finally, free the namespace itself */
  403. # undef XXH_NAMESPACE
  404. /* employ the namespace for XXH_INLINE_ALL */
  405. # define XXH_NAMESPACE XXH_INLINE_
  406. /*
  407. * Some identifiers (enums, type names) are not symbols,
  408. * but they must nonetheless be renamed to avoid redeclaration.
  409. * Alternative solution: do not redeclare them.
  410. * However, this requires some #ifdefs, and has a more dispersed impact.
  411. * Meanwhile, renaming can be achieved in a single place.
  412. */
  413. # define XXH_IPREF(Id) XXH_NAMESPACE ## Id
  414. # define XXH_OK XXH_IPREF(XXH_OK)
  415. # define XXH_ERROR XXH_IPREF(XXH_ERROR)
  416. # define XXH_errorcode XXH_IPREF(XXH_errorcode)
  417. # define XXH32_canonical_t XXH_IPREF(XXH32_canonical_t)
  418. # define XXH64_canonical_t XXH_IPREF(XXH64_canonical_t)
  419. # define XXH128_canonical_t XXH_IPREF(XXH128_canonical_t)
  420. # define XXH32_state_s XXH_IPREF(XXH32_state_s)
  421. # define XXH32_state_t XXH_IPREF(XXH32_state_t)
  422. # define XXH64_state_s XXH_IPREF(XXH64_state_s)
  423. # define XXH64_state_t XXH_IPREF(XXH64_state_t)
  424. # define XXH3_state_s XXH_IPREF(XXH3_state_s)
  425. # define XXH3_state_t XXH_IPREF(XXH3_state_t)
  426. # define XXH128_hash_t XXH_IPREF(XXH128_hash_t)
  427. /* Ensure the header is parsed again, even if it was previously included */
  428. # undef XXHASH_H_5627135585666179
  429. # undef XXHASH_H_STATIC_13879238742
  430. #endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */
  431. /* ****************************************************************
  432. * Stable API
  433. *****************************************************************/
  434. #ifndef XXHASH_H_5627135585666179
  435. #define XXHASH_H_5627135585666179 1
  436. /*! @brief Marks a global symbol. */
  437. #if !defined(XXH_INLINE_ALL) && !defined(XXH_PRIVATE_API)
  438. # if defined(_WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT))
  439. # ifdef XXH_EXPORT
  440. # define XXH_PUBLIC_API __declspec(dllexport)
  441. # elif XXH_IMPORT
  442. # define XXH_PUBLIC_API __declspec(dllimport)
  443. # endif
  444. # else
  445. # define XXH_PUBLIC_API /* do nothing */
  446. # endif
  447. #endif
  448. #ifdef XXH_NAMESPACE
  449. # define XXH_CAT(A,B) A##B
  450. # define XXH_NAME2(A,B) XXH_CAT(A,B)
  451. # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
  452. /* XXH32 */
  453. # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
  454. # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
  455. # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
  456. # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
  457. # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
  458. # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
  459. # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
  460. # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
  461. # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
  462. /* XXH64 */
  463. # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
  464. # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
  465. # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
  466. # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
  467. # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
  468. # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
  469. # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
  470. # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
  471. # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
  472. /* XXH3_64bits */
  473. # define XXH3_64bits XXH_NAME2(XXH_NAMESPACE, XXH3_64bits)
  474. # define XXH3_64bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSecret)
  475. # define XXH3_64bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSeed)
  476. # define XXH3_64bits_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSecretandSeed)
  477. # define XXH3_createState XXH_NAME2(XXH_NAMESPACE, XXH3_createState)
  478. # define XXH3_freeState XXH_NAME2(XXH_NAMESPACE, XXH3_freeState)
  479. # define XXH3_copyState XXH_NAME2(XXH_NAMESPACE, XXH3_copyState)
  480. # define XXH3_64bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset)
  481. # define XXH3_64bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSeed)
  482. # define XXH3_64bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSecret)
  483. # define XXH3_64bits_reset_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSecretandSeed)
  484. # define XXH3_64bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_update)
  485. # define XXH3_64bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_digest)
  486. # define XXH3_generateSecret XXH_NAME2(XXH_NAMESPACE, XXH3_generateSecret)
  487. # define XXH3_generateSecret_fromSeed XXH_NAME2(XXH_NAMESPACE, XXH3_generateSecret_fromSeed)
  488. /* XXH3_128bits */
  489. # define XXH128 XXH_NAME2(XXH_NAMESPACE, XXH128)
  490. # define XXH3_128bits XXH_NAME2(XXH_NAMESPACE, XXH3_128bits)
  491. # define XXH3_128bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSeed)
  492. # define XXH3_128bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSecret)
  493. # define XXH3_128bits_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSecretandSeed)
  494. # define XXH3_128bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset)
  495. # define XXH3_128bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSeed)
  496. # define XXH3_128bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSecret)
  497. # define XXH3_128bits_reset_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSecretandSeed)
  498. # define XXH3_128bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_update)
  499. # define XXH3_128bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_digest)
  500. # define XXH128_isEqual XXH_NAME2(XXH_NAMESPACE, XXH128_isEqual)
  501. # define XXH128_cmp XXH_NAME2(XXH_NAMESPACE, XXH128_cmp)
  502. # define XXH128_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH128_canonicalFromHash)
  503. # define XXH128_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH128_hashFromCanonical)
  504. #endif
  505. /* *************************************
  506. * Compiler specifics
  507. ***************************************/
  508. /* specific declaration modes for Windows */
  509. #if !defined(XXH_INLINE_ALL) && !defined(XXH_PRIVATE_API)
  510. # if defined(_WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT))
  511. # ifdef XXH_EXPORT
  512. # define XXH_PUBLIC_API __declspec(dllexport)
  513. # elif XXH_IMPORT
  514. # define XXH_PUBLIC_API __declspec(dllimport)
  515. # endif
  516. # else
  517. # define XXH_PUBLIC_API /* do nothing */
  518. # endif
  519. #endif
  520. #if defined (__GNUC__)
  521. # define XXH_CONSTF __attribute__((__const__))
  522. # define XXH_PUREF __attribute__((__pure__))
  523. # define XXH_MALLOCF __attribute__((__malloc__))
  524. #else
  525. # define XXH_CONSTF /* disable */
  526. # define XXH_PUREF
  527. # define XXH_MALLOCF
  528. #endif
  529. /* *************************************
  530. * Version
  531. ***************************************/
  532. #define XXH_VERSION_MAJOR 0
  533. #define XXH_VERSION_MINOR 8
  534. #define XXH_VERSION_RELEASE 3
  535. /*! @brief Version number, encoded as two digits each */
  536. #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
  537. /*!
  538. * @brief Obtains the xxHash version.
  539. *
  540. * This is mostly useful when xxHash is compiled as a shared library,
  541. * since the returned value comes from the library, as opposed to header file.
  542. *
  543. * @return @ref XXH_VERSION_NUMBER of the invoked library.
  544. */
  545. XXH_PUBLIC_API XXH_CONSTF unsigned XXH_versionNumber (void);
  546. /* ****************************
  547. * Common basic types
  548. ******************************/
  549. #include <stddef.h> /* size_t */
  550. /*!
  551. * @brief Exit code for the streaming API.
  552. */
  553. typedef enum {
  554. XXH_OK = 0, /*!< OK */
  555. XXH_ERROR /*!< Error */
  556. } XXH_errorcode;
  557. /*-**********************************************************************
  558. * 32-bit hash
  559. ************************************************************************/
  560. #if defined(XXH_DOXYGEN) /* Don't show <stdint.h> include */
  561. /*!
  562. * @brief An unsigned 32-bit integer.
  563. *
  564. * Not necessarily defined to `uint32_t` but functionally equivalent.
  565. */
  566. typedef uint32_t XXH32_hash_t;
  567. #elif !defined (__VMS) \
  568. && (defined (__cplusplus) \
  569. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  570. # ifdef _AIX
  571. # include <inttypes.h>
  572. # else
  573. # include <stdint.h>
  574. # endif
  575. typedef uint32_t XXH32_hash_t;
  576. #else
  577. # include <limits.h>
  578. # if UINT_MAX == 0xFFFFFFFFUL
  579. typedef unsigned int XXH32_hash_t;
  580. # elif ULONG_MAX == 0xFFFFFFFFUL
  581. typedef unsigned long XXH32_hash_t;
  582. # else
  583. # error "unsupported platform: need a 32-bit type"
  584. # endif
  585. #endif
  586. /*!
  587. * @}
  588. *
  589. * @defgroup XXH32_family XXH32 family
  590. * @ingroup public
  591. * Contains functions used in the classic 32-bit xxHash algorithm.
  592. *
  593. * @note
  594. * XXH32 is useful for older platforms, with no or poor 64-bit performance.
  595. * Note that the @ref XXH3_family provides competitive speed for both 32-bit
  596. * and 64-bit systems, and offers true 64/128 bit hash results.
  597. *
  598. * @see @ref XXH64_family, @ref XXH3_family : Other xxHash families
  599. * @see @ref XXH32_impl for implementation details
  600. * @{
  601. */
  602. /*!
  603. * @brief Calculates the 32-bit hash of @p input using xxHash32.
  604. *
  605. * @param input The block of data to be hashed, at least @p length bytes in size.
  606. * @param length The length of @p input, in bytes.
  607. * @param seed The 32-bit seed to alter the hash's output predictably.
  608. *
  609. * @pre
  610. * The memory between @p input and @p input + @p length must be valid,
  611. * readable, contiguous memory. However, if @p length is `0`, @p input may be
  612. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  613. *
  614. * @return The calculated 32-bit xxHash32 value.
  615. *
  616. * @see @ref single_shot_example "Single Shot Example" for an example.
  617. */
  618. XXH_PUBLIC_API XXH_PUREF XXH32_hash_t XXH32 (const void* input, size_t length, XXH32_hash_t seed);
  619. #ifndef XXH_NO_STREAM
  620. /*!
  621. * @typedef struct XXH32_state_s XXH32_state_t
  622. * @brief The opaque state struct for the XXH32 streaming API.
  623. *
  624. * @see XXH32_state_s for details.
  625. * @see @ref streaming_example "Streaming Example"
  626. */
  627. typedef struct XXH32_state_s XXH32_state_t;
  628. /*!
  629. * @brief Allocates an @ref XXH32_state_t.
  630. *
  631. * @return An allocated pointer of @ref XXH32_state_t on success.
  632. * @return `NULL` on failure.
  633. *
  634. * @note Must be freed with XXH32_freeState().
  635. *
  636. * @see @ref streaming_example "Streaming Example"
  637. */
  638. XXH_PUBLIC_API XXH_MALLOCF XXH32_state_t* XXH32_createState(void);
  639. /*!
  640. * @brief Frees an @ref XXH32_state_t.
  641. *
  642. * @param statePtr A pointer to an @ref XXH32_state_t allocated with @ref XXH32_createState().
  643. *
  644. * @return @ref XXH_OK.
  645. *
  646. * @note @p statePtr must be allocated with XXH32_createState().
  647. *
  648. * @see @ref streaming_example "Streaming Example"
  649. *
  650. */
  651. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
  652. /*!
  653. * @brief Copies one @ref XXH32_state_t to another.
  654. *
  655. * @param dst_state The state to copy to.
  656. * @param src_state The state to copy from.
  657. * @pre
  658. * @p dst_state and @p src_state must not be `NULL` and must not overlap.
  659. */
  660. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state);
  661. /*!
  662. * @brief Resets an @ref XXH32_state_t to begin a new hash.
  663. *
  664. * @param statePtr The state struct to reset.
  665. * @param seed The 32-bit seed to alter the hash result predictably.
  666. *
  667. * @pre
  668. * @p statePtr must not be `NULL`.
  669. *
  670. * @return @ref XXH_OK on success.
  671. * @return @ref XXH_ERROR on failure.
  672. *
  673. * @note This function resets and seeds a state. Call it before @ref XXH32_update().
  674. *
  675. * @see @ref streaming_example "Streaming Example"
  676. */
  677. XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, XXH32_hash_t seed);
  678. /*!
  679. * @brief Consumes a block of @p input to an @ref XXH32_state_t.
  680. *
  681. * @param statePtr The state struct to update.
  682. * @param input The block of data to be hashed, at least @p length bytes in size.
  683. * @param length The length of @p input, in bytes.
  684. *
  685. * @pre
  686. * @p statePtr must not be `NULL`.
  687. * @pre
  688. * The memory between @p input and @p input + @p length must be valid,
  689. * readable, contiguous memory. However, if @p length is `0`, @p input may be
  690. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  691. *
  692. * @return @ref XXH_OK on success.
  693. * @return @ref XXH_ERROR on failure.
  694. *
  695. * @note Call this to incrementally consume blocks of data.
  696. *
  697. * @see @ref streaming_example "Streaming Example"
  698. */
  699. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
  700. /*!
  701. * @brief Returns the calculated hash value from an @ref XXH32_state_t.
  702. *
  703. * @param statePtr The state struct to calculate the hash from.
  704. *
  705. * @pre
  706. * @p statePtr must not be `NULL`.
  707. *
  708. * @return The calculated 32-bit xxHash32 value from that state.
  709. *
  710. * @note
  711. * Calling XXH32_digest() will not affect @p statePtr, so you can update,
  712. * digest, and update again.
  713. *
  714. * @see @ref streaming_example "Streaming Example"
  715. */
  716. XXH_PUBLIC_API XXH_PUREF XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
  717. #endif /* !XXH_NO_STREAM */
  718. /******* Canonical representation *******/
  719. /*!
  720. * @brief Canonical (big endian) representation of @ref XXH32_hash_t.
  721. */
  722. typedef struct {
  723. unsigned char digest[4]; /*!< Hash bytes, big endian */
  724. } XXH32_canonical_t;
  725. /*!
  726. * @brief Converts an @ref XXH32_hash_t to a big endian @ref XXH32_canonical_t.
  727. *
  728. * @param dst The @ref XXH32_canonical_t pointer to be stored to.
  729. * @param hash The @ref XXH32_hash_t to be converted.
  730. *
  731. * @pre
  732. * @p dst must not be `NULL`.
  733. *
  734. * @see @ref canonical_representation_example "Canonical Representation Example"
  735. */
  736. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);
  737. /*!
  738. * @brief Converts an @ref XXH32_canonical_t to a native @ref XXH32_hash_t.
  739. *
  740. * @param src The @ref XXH32_canonical_t to convert.
  741. *
  742. * @pre
  743. * @p src must not be `NULL`.
  744. *
  745. * @return The converted hash.
  746. *
  747. * @see @ref canonical_representation_example "Canonical Representation Example"
  748. */
  749. XXH_PUBLIC_API XXH_PUREF XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);
  750. /*! @cond Doxygen ignores this part */
  751. #ifdef __has_attribute
  752. # define XXH_HAS_ATTRIBUTE(x) __has_attribute(x)
  753. #else
  754. # define XXH_HAS_ATTRIBUTE(x) 0
  755. #endif
  756. /*! @endcond */
  757. /*! @cond Doxygen ignores this part */
  758. /*
  759. * C23 __STDC_VERSION__ number hasn't been specified yet. For now
  760. * leave as `201711L` (C17 + 1).
  761. * TODO: Update to correct value when its been specified.
  762. */
  763. #define XXH_C23_VN 201711L
  764. /*! @endcond */
  765. /*! @cond Doxygen ignores this part */
  766. /* C-language Attributes are added in C23. */
  767. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= XXH_C23_VN) && defined(__has_c_attribute)
  768. # define XXH_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
  769. #else
  770. # define XXH_HAS_C_ATTRIBUTE(x) 0
  771. #endif
  772. /*! @endcond */
  773. /*! @cond Doxygen ignores this part */
  774. #if defined(__cplusplus) && defined(__has_cpp_attribute)
  775. # define XXH_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
  776. #else
  777. # define XXH_HAS_CPP_ATTRIBUTE(x) 0
  778. #endif
  779. /*! @endcond */
  780. /*! @cond Doxygen ignores this part */
  781. /*
  782. * Define XXH_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute
  783. * introduced in CPP17 and C23.
  784. * CPP17 : https://en.cppreference.com/w/cpp/language/attributes/fallthrough
  785. * C23 : https://en.cppreference.com/w/c/language/attributes/fallthrough
  786. */
  787. #if XXH_HAS_C_ATTRIBUTE(fallthrough) || XXH_HAS_CPP_ATTRIBUTE(fallthrough)
  788. # define XXH_FALLTHROUGH [[fallthrough]]
  789. #elif XXH_HAS_ATTRIBUTE(__fallthrough__)
  790. # define XXH_FALLTHROUGH __attribute__ ((__fallthrough__))
  791. #else
  792. # define XXH_FALLTHROUGH /* fallthrough */
  793. #endif
  794. /*! @endcond */
  795. /*! @cond Doxygen ignores this part */
  796. /*
  797. * Define XXH_NOESCAPE for annotated pointers in public API.
  798. * https://clang.llvm.org/docs/AttributeReference.html#noescape
  799. * As of writing this, only supported by clang.
  800. */
  801. #if XXH_HAS_ATTRIBUTE(noescape)
  802. # define XXH_NOESCAPE __attribute__((__noescape__))
  803. #else
  804. # define XXH_NOESCAPE
  805. #endif
  806. /*! @endcond */
  807. /*!
  808. * @}
  809. * @ingroup public
  810. * @{
  811. */
  812. #ifndef XXH_NO_LONG_LONG
  813. /*-**********************************************************************
  814. * 64-bit hash
  815. ************************************************************************/
  816. #if defined(XXH_DOXYGEN) /* don't include <stdint.h> */
  817. /*!
  818. * @brief An unsigned 64-bit integer.
  819. *
  820. * Not necessarily defined to `uint64_t` but functionally equivalent.
  821. */
  822. typedef uint64_t XXH64_hash_t;
  823. #elif !defined (__VMS) \
  824. && (defined (__cplusplus) \
  825. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  826. # ifdef _AIX
  827. # include <inttypes.h>
  828. # else
  829. # include <stdint.h>
  830. # endif
  831. typedef uint64_t XXH64_hash_t;
  832. #else
  833. # include <limits.h>
  834. # if defined(__LP64__) && ULONG_MAX == 0xFFFFFFFFFFFFFFFFULL
  835. /* LP64 ABI says uint64_t is unsigned long */
  836. typedef unsigned long XXH64_hash_t;
  837. # else
  838. /* the following type must have a width of 64-bit */
  839. typedef unsigned long long XXH64_hash_t;
  840. # endif
  841. #endif
  842. /*!
  843. * @}
  844. *
  845. * @defgroup XXH64_family XXH64 family
  846. * @ingroup public
  847. * @{
  848. * Contains functions used in the classic 64-bit xxHash algorithm.
  849. *
  850. * @note
  851. * XXH3 provides competitive speed for both 32-bit and 64-bit systems,
  852. * and offers true 64/128 bit hash results.
  853. * It provides better speed for systems with vector processing capabilities.
  854. */
  855. /*!
  856. * @brief Calculates the 64-bit hash of @p input using xxHash64.
  857. *
  858. * @param input The block of data to be hashed, at least @p length bytes in size.
  859. * @param length The length of @p input, in bytes.
  860. * @param seed The 64-bit seed to alter the hash's output predictably.
  861. *
  862. * @pre
  863. * The memory between @p input and @p input + @p length must be valid,
  864. * readable, contiguous memory. However, if @p length is `0`, @p input may be
  865. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  866. *
  867. * @return The calculated 64-bit xxHash64 value.
  868. *
  869. * @see @ref single_shot_example "Single Shot Example" for an example.
  870. */
  871. XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH64(XXH_NOESCAPE const void* input, size_t length, XXH64_hash_t seed);
  872. /******* Streaming *******/
  873. #ifndef XXH_NO_STREAM
  874. /*!
  875. * @brief The opaque state struct for the XXH64 streaming API.
  876. *
  877. * @see XXH64_state_s for details.
  878. * @see @ref streaming_example "Streaming Example"
  879. */
  880. typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
  881. /*!
  882. * @brief Allocates an @ref XXH64_state_t.
  883. *
  884. * @return An allocated pointer of @ref XXH64_state_t on success.
  885. * @return `NULL` on failure.
  886. *
  887. * @note Must be freed with XXH64_freeState().
  888. *
  889. * @see @ref streaming_example "Streaming Example"
  890. */
  891. XXH_PUBLIC_API XXH_MALLOCF XXH64_state_t* XXH64_createState(void);
  892. /*!
  893. * @brief Frees an @ref XXH64_state_t.
  894. *
  895. * @param statePtr A pointer to an @ref XXH64_state_t allocated with @ref XXH64_createState().
  896. *
  897. * @return @ref XXH_OK.
  898. *
  899. * @note @p statePtr must be allocated with XXH64_createState().
  900. *
  901. * @see @ref streaming_example "Streaming Example"
  902. */
  903. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);
  904. /*!
  905. * @brief Copies one @ref XXH64_state_t to another.
  906. *
  907. * @param dst_state The state to copy to.
  908. * @param src_state The state to copy from.
  909. * @pre
  910. * @p dst_state and @p src_state must not be `NULL` and must not overlap.
  911. */
  912. XXH_PUBLIC_API void XXH64_copyState(XXH_NOESCAPE XXH64_state_t* dst_state, const XXH64_state_t* src_state);
  913. /*!
  914. * @brief Resets an @ref XXH64_state_t to begin a new hash.
  915. *
  916. * @param statePtr The state struct to reset.
  917. * @param seed The 64-bit seed to alter the hash result predictably.
  918. *
  919. * @pre
  920. * @p statePtr must not be `NULL`.
  921. *
  922. * @return @ref XXH_OK on success.
  923. * @return @ref XXH_ERROR on failure.
  924. *
  925. * @note This function resets and seeds a state. Call it before @ref XXH64_update().
  926. *
  927. * @see @ref streaming_example "Streaming Example"
  928. */
  929. XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH_NOESCAPE XXH64_state_t* statePtr, XXH64_hash_t seed);
  930. /*!
  931. * @brief Consumes a block of @p input to an @ref XXH64_state_t.
  932. *
  933. * @param statePtr The state struct to update.
  934. * @param input The block of data to be hashed, at least @p length bytes in size.
  935. * @param length The length of @p input, in bytes.
  936. *
  937. * @pre
  938. * @p statePtr must not be `NULL`.
  939. * @pre
  940. * The memory between @p input and @p input + @p length must be valid,
  941. * readable, contiguous memory. However, if @p length is `0`, @p input may be
  942. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  943. *
  944. * @return @ref XXH_OK on success.
  945. * @return @ref XXH_ERROR on failure.
  946. *
  947. * @note Call this to incrementally consume blocks of data.
  948. *
  949. * @see @ref streaming_example "Streaming Example"
  950. */
  951. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH_NOESCAPE XXH64_state_t* statePtr, XXH_NOESCAPE const void* input, size_t length);
  952. /*!
  953. * @brief Returns the calculated hash value from an @ref XXH64_state_t.
  954. *
  955. * @param statePtr The state struct to calculate the hash from.
  956. *
  957. * @pre
  958. * @p statePtr must not be `NULL`.
  959. *
  960. * @return The calculated 64-bit xxHash64 value from that state.
  961. *
  962. * @note
  963. * Calling XXH64_digest() will not affect @p statePtr, so you can update,
  964. * digest, and update again.
  965. *
  966. * @see @ref streaming_example "Streaming Example"
  967. */
  968. XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH64_digest (XXH_NOESCAPE const XXH64_state_t* statePtr);
  969. #endif /* !XXH_NO_STREAM */
  970. /******* Canonical representation *******/
  971. /*!
  972. * @brief Canonical (big endian) representation of @ref XXH64_hash_t.
  973. */
  974. typedef struct { unsigned char digest[sizeof(XXH64_hash_t)]; } XXH64_canonical_t;
  975. /*!
  976. * @brief Converts an @ref XXH64_hash_t to a big endian @ref XXH64_canonical_t.
  977. *
  978. * @param dst The @ref XXH64_canonical_t pointer to be stored to.
  979. * @param hash The @ref XXH64_hash_t to be converted.
  980. *
  981. * @pre
  982. * @p dst must not be `NULL`.
  983. *
  984. * @see @ref canonical_representation_example "Canonical Representation Example"
  985. */
  986. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH_NOESCAPE XXH64_canonical_t* dst, XXH64_hash_t hash);
  987. /*!
  988. * @brief Converts an @ref XXH64_canonical_t to a native @ref XXH64_hash_t.
  989. *
  990. * @param src The @ref XXH64_canonical_t to convert.
  991. *
  992. * @pre
  993. * @p src must not be `NULL`.
  994. *
  995. * @return The converted hash.
  996. *
  997. * @see @ref canonical_representation_example "Canonical Representation Example"
  998. */
  999. XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH64_hashFromCanonical(XXH_NOESCAPE const XXH64_canonical_t* src);
  1000. #ifndef XXH_NO_XXH3
  1001. /*!
  1002. * @}
  1003. * ************************************************************************
  1004. * @defgroup XXH3_family XXH3 family
  1005. * @ingroup public
  1006. * @{
  1007. *
  1008. * XXH3 is a more recent hash algorithm featuring:
  1009. * - Improved speed for both small and large inputs
  1010. * - True 64-bit and 128-bit outputs
  1011. * - SIMD acceleration
  1012. * - Improved 32-bit viability
  1013. *
  1014. * Speed analysis methodology is explained here:
  1015. *
  1016. * https://fastcompression.blogspot.com/2019/03/presenting-xxh3.html
  1017. *
  1018. * Compared to XXH64, expect XXH3 to run approximately
  1019. * ~2x faster on large inputs and >3x faster on small ones,
  1020. * exact differences vary depending on platform.
  1021. *
  1022. * XXH3's speed benefits greatly from SIMD and 64-bit arithmetic,
  1023. * but does not require it.
  1024. * Most 32-bit and 64-bit targets that can run XXH32 smoothly can run XXH3
  1025. * at competitive speeds, even without vector support. Further details are
  1026. * explained in the implementation.
  1027. *
  1028. * XXH3 has a fast scalar implementation, but it also includes accelerated SIMD
  1029. * implementations for many common platforms:
  1030. * - AVX512
  1031. * - AVX2
  1032. * - SSE2
  1033. * - ARM NEON
  1034. * - WebAssembly SIMD128
  1035. * - POWER8 VSX
  1036. * - s390x ZVector
  1037. * This can be controlled via the @ref XXH_VECTOR macro, but it automatically
  1038. * selects the best version according to predefined macros. For the x86 family, an
  1039. * automatic runtime dispatcher is included separately in @ref xxh_x86dispatch.c.
  1040. *
  1041. * XXH3 implementation is portable:
  1042. * it has a generic C90 formulation that can be compiled on any platform,
  1043. * all implementations generate exactly the same hash value on all platforms.
  1044. * Starting from v0.8.0, it's also labelled "stable", meaning that
  1045. * any future version will also generate the same hash value.
  1046. *
  1047. * XXH3 offers 2 variants, _64bits and _128bits.
  1048. *
  1049. * When only 64 bits are needed, prefer invoking the _64bits variant, as it
  1050. * reduces the amount of mixing, resulting in faster speed on small inputs.
  1051. * It's also generally simpler to manipulate a scalar return type than a struct.
  1052. *
  1053. * The API supports one-shot hashing, streaming mode, and custom secrets.
  1054. */
  1055. /*!
  1056. * @ingroup tuning
  1057. * @brief Possible values for @ref XXH_VECTOR.
  1058. *
  1059. * Unless set explicitly, determined automatically.
  1060. */
  1061. # define XXH_SCALAR 0 /*!< Portable scalar version */
  1062. # define XXH_SSE2 1 /*!< SSE2 for Pentium 4, Opteron, all x86_64. */
  1063. # define XXH_AVX2 2 /*!< AVX2 for Haswell and Bulldozer */
  1064. # define XXH_AVX512 3 /*!< AVX512 for Skylake and Icelake */
  1065. # define XXH_NEON 4 /*!< NEON for most ARMv7-A, all AArch64, and WASM SIMD128 */
  1066. # define XXH_VSX 5 /*!< VSX and ZVector for POWER8/z13 (64-bit) */
  1067. # define XXH_SVE 6 /*!< SVE for some ARMv8-A and ARMv9-A */
  1068. # define XXH_LSX 7 /*!< LSX (128-bit SIMD) for LoongArch64 */
  1069. /*-**********************************************************************
  1070. * XXH3 64-bit variant
  1071. ************************************************************************/
  1072. /*!
  1073. * @brief Calculates 64-bit unseeded variant of XXH3 hash of @p input.
  1074. *
  1075. * @param input The block of data to be hashed, at least @p length bytes in size.
  1076. * @param length The length of @p input, in bytes.
  1077. *
  1078. * @pre
  1079. * The memory between @p input and @p input + @p length must be valid,
  1080. * readable, contiguous memory. However, if @p length is `0`, @p input may be
  1081. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  1082. *
  1083. * @return The calculated 64-bit XXH3 hash value.
  1084. *
  1085. * @note
  1086. * This is equivalent to @ref XXH3_64bits_withSeed() with a seed of `0`, however
  1087. * it may have slightly better performance due to constant propagation of the
  1088. * defaults.
  1089. *
  1090. * @see
  1091. * XXH3_64bits_withSeed(), XXH3_64bits_withSecret(): other seeding variants
  1092. * @see @ref single_shot_example "Single Shot Example" for an example.
  1093. */
  1094. XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits(XXH_NOESCAPE const void* input, size_t length);
  1095. /*!
  1096. * @brief Calculates 64-bit seeded variant of XXH3 hash of @p input.
  1097. *
  1098. * @param input The block of data to be hashed, at least @p length bytes in size.
  1099. * @param length The length of @p input, in bytes.
  1100. * @param seed The 64-bit seed to alter the hash result predictably.
  1101. *
  1102. * @pre
  1103. * The memory between @p input and @p input + @p length must be valid,
  1104. * readable, contiguous memory. However, if @p length is `0`, @p input may be
  1105. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  1106. *
  1107. * @return The calculated 64-bit XXH3 hash value.
  1108. *
  1109. * @note
  1110. * seed == 0 produces the same results as @ref XXH3_64bits().
  1111. *
  1112. * This variant generates a custom secret on the fly based on default secret
  1113. * altered using the @p seed value.
  1114. *
  1115. * While this operation is decently fast, note that it's not completely free.
  1116. *
  1117. * @see @ref single_shot_example "Single Shot Example" for an example.
  1118. */
  1119. XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_withSeed(XXH_NOESCAPE const void* input, size_t length, XXH64_hash_t seed);
  1120. /*!
  1121. * The bare minimum size for a custom secret.
  1122. *
  1123. * @see
  1124. * XXH3_64bits_withSecret(), XXH3_64bits_reset_withSecret(),
  1125. * XXH3_128bits_withSecret(), XXH3_128bits_reset_withSecret().
  1126. */
  1127. #define XXH3_SECRET_SIZE_MIN 136
  1128. /*!
  1129. * @brief Calculates 64-bit variant of XXH3 with a custom "secret".
  1130. *
  1131. * @param data The block of data to be hashed, at least @p len bytes in size.
  1132. * @param len The length of @p data, in bytes.
  1133. * @param secret The secret data.
  1134. * @param secretSize The length of @p secret, in bytes.
  1135. *
  1136. * @return The calculated 64-bit XXH3 hash value.
  1137. *
  1138. * @pre
  1139. * The memory between @p data and @p data + @p len must be valid,
  1140. * readable, contiguous memory. However, if @p length is `0`, @p data may be
  1141. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  1142. *
  1143. * It's possible to provide any blob of bytes as a "secret" to generate the hash.
  1144. * This makes it more difficult for an external actor to prepare an intentional collision.
  1145. * The main condition is that @p secretSize *must* be large enough (>= @ref XXH3_SECRET_SIZE_MIN).
  1146. * However, the quality of the secret impacts the dispersion of the hash algorithm.
  1147. * Therefore, the secret _must_ look like a bunch of random bytes.
  1148. * Avoid "trivial" or structured data such as repeated sequences or a text document.
  1149. * Whenever in doubt about the "randomness" of the blob of bytes,
  1150. * consider employing @ref XXH3_generateSecret() instead (see below).
  1151. * It will generate a proper high entropy secret derived from the blob of bytes.
  1152. * Another advantage of using XXH3_generateSecret() is that
  1153. * it guarantees that all bits within the initial blob of bytes
  1154. * will impact every bit of the output.
  1155. * This is not necessarily the case when using the blob of bytes directly
  1156. * because, when hashing _small_ inputs, only a portion of the secret is employed.
  1157. *
  1158. * @see @ref single_shot_example "Single Shot Example" for an example.
  1159. */
  1160. XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_withSecret(XXH_NOESCAPE const void* data, size_t len, XXH_NOESCAPE const void* secret, size_t secretSize);
  1161. /******* Streaming *******/
  1162. #ifndef XXH_NO_STREAM
  1163. /*
  1164. * Streaming requires state maintenance.
  1165. * This operation costs memory and CPU.
  1166. * As a consequence, streaming is slower than one-shot hashing.
  1167. * For better performance, prefer one-shot functions whenever applicable.
  1168. */
  1169. /*!
  1170. * @brief The opaque state struct for the XXH3 streaming API.
  1171. *
  1172. * @see XXH3_state_s for details.
  1173. * @see @ref streaming_example "Streaming Example"
  1174. */
  1175. typedef struct XXH3_state_s XXH3_state_t;
  1176. XXH_PUBLIC_API XXH_MALLOCF XXH3_state_t* XXH3_createState(void);
  1177. XXH_PUBLIC_API XXH_errorcode XXH3_freeState(XXH3_state_t* statePtr);
  1178. /*!
  1179. * @brief Copies one @ref XXH3_state_t to another.
  1180. *
  1181. * @param dst_state The state to copy to.
  1182. * @param src_state The state to copy from.
  1183. * @pre
  1184. * @p dst_state and @p src_state must not be `NULL` and must not overlap.
  1185. */
  1186. XXH_PUBLIC_API void XXH3_copyState(XXH_NOESCAPE XXH3_state_t* dst_state, XXH_NOESCAPE const XXH3_state_t* src_state);
  1187. /*!
  1188. * @brief Resets an @ref XXH3_state_t to begin a new hash.
  1189. *
  1190. * @param statePtr The state struct to reset.
  1191. *
  1192. * @pre
  1193. * @p statePtr must not be `NULL`.
  1194. *
  1195. * @return @ref XXH_OK on success.
  1196. * @return @ref XXH_ERROR on failure.
  1197. *
  1198. * @note
  1199. * - This function resets `statePtr` and generate a secret with default parameters.
  1200. * - Call this function before @ref XXH3_64bits_update().
  1201. * - Digest will be equivalent to `XXH3_64bits()`.
  1202. *
  1203. * @see @ref streaming_example "Streaming Example"
  1204. *
  1205. */
  1206. XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset(XXH_NOESCAPE XXH3_state_t* statePtr);
  1207. /*!
  1208. * @brief Resets an @ref XXH3_state_t with 64-bit seed to begin a new hash.
  1209. *
  1210. * @param statePtr The state struct to reset.
  1211. * @param seed The 64-bit seed to alter the hash result predictably.
  1212. *
  1213. * @pre
  1214. * @p statePtr must not be `NULL`.
  1215. *
  1216. * @return @ref XXH_OK on success.
  1217. * @return @ref XXH_ERROR on failure.
  1218. *
  1219. * @note
  1220. * - This function resets `statePtr` and generate a secret from `seed`.
  1221. * - Call this function before @ref XXH3_64bits_update().
  1222. * - Digest will be equivalent to `XXH3_64bits_withSeed()`.
  1223. *
  1224. * @see @ref streaming_example "Streaming Example"
  1225. *
  1226. */
  1227. XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSeed(XXH_NOESCAPE XXH3_state_t* statePtr, XXH64_hash_t seed);
  1228. /*!
  1229. * @brief Resets an @ref XXH3_state_t with secret data to begin a new hash.
  1230. *
  1231. * @param statePtr The state struct to reset.
  1232. * @param secret The secret data.
  1233. * @param secretSize The length of @p secret, in bytes.
  1234. *
  1235. * @pre
  1236. * @p statePtr must not be `NULL`.
  1237. *
  1238. * @return @ref XXH_OK on success.
  1239. * @return @ref XXH_ERROR on failure.
  1240. *
  1241. * @note
  1242. * `secret` is referenced, it _must outlive_ the hash streaming session.
  1243. *
  1244. * Similar to one-shot API, `secretSize` must be >= @ref XXH3_SECRET_SIZE_MIN,
  1245. * and the quality of produced hash values depends on secret's entropy
  1246. * (secret's content should look like a bunch of random bytes).
  1247. * When in doubt about the randomness of a candidate `secret`,
  1248. * consider employing `XXH3_generateSecret()` instead (see below).
  1249. *
  1250. * @see @ref streaming_example "Streaming Example"
  1251. */
  1252. XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSecret(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize);
  1253. /*!
  1254. * @brief Consumes a block of @p input to an @ref XXH3_state_t.
  1255. *
  1256. * @param statePtr The state struct to update.
  1257. * @param input The block of data to be hashed, at least @p length bytes in size.
  1258. * @param length The length of @p input, in bytes.
  1259. *
  1260. * @pre
  1261. * @p statePtr must not be `NULL`.
  1262. * @pre
  1263. * The memory between @p input and @p input + @p length must be valid,
  1264. * readable, contiguous memory. However, if @p length is `0`, @p input may be
  1265. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  1266. *
  1267. * @return @ref XXH_OK on success.
  1268. * @return @ref XXH_ERROR on failure.
  1269. *
  1270. * @note Call this to incrementally consume blocks of data.
  1271. *
  1272. * @see @ref streaming_example "Streaming Example"
  1273. */
  1274. XXH_PUBLIC_API XXH_errorcode XXH3_64bits_update (XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* input, size_t length);
  1275. /*!
  1276. * @brief Returns the calculated XXH3 64-bit hash value from an @ref XXH3_state_t.
  1277. *
  1278. * @param statePtr The state struct to calculate the hash from.
  1279. *
  1280. * @pre
  1281. * @p statePtr must not be `NULL`.
  1282. *
  1283. * @return The calculated XXH3 64-bit hash value from that state.
  1284. *
  1285. * @note
  1286. * Calling XXH3_64bits_digest() will not affect @p statePtr, so you can update,
  1287. * digest, and update again.
  1288. *
  1289. * @see @ref streaming_example "Streaming Example"
  1290. */
  1291. XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_digest (XXH_NOESCAPE const XXH3_state_t* statePtr);
  1292. #endif /* !XXH_NO_STREAM */
  1293. /* note : canonical representation of XXH3 is the same as XXH64
  1294. * since they both produce XXH64_hash_t values */
  1295. /*-**********************************************************************
  1296. * XXH3 128-bit variant
  1297. ************************************************************************/
  1298. /*!
  1299. * @brief The return value from 128-bit hashes.
  1300. *
  1301. * Stored in little endian order, although the fields themselves are in native
  1302. * endianness.
  1303. */
  1304. typedef struct {
  1305. XXH64_hash_t low64; /*!< `value & 0xFFFFFFFFFFFFFFFF` */
  1306. XXH64_hash_t high64; /*!< `value >> 64` */
  1307. } XXH128_hash_t;
  1308. /*!
  1309. * @brief Calculates 128-bit unseeded variant of XXH3 of @p data.
  1310. *
  1311. * @param data The block of data to be hashed, at least @p length bytes in size.
  1312. * @param len The length of @p data, in bytes.
  1313. *
  1314. * @return The calculated 128-bit variant of XXH3 value.
  1315. *
  1316. * The 128-bit variant of XXH3 has more strength, but it has a bit of overhead
  1317. * for shorter inputs.
  1318. *
  1319. * This is equivalent to @ref XXH3_128bits_withSeed() with a seed of `0`, however
  1320. * it may have slightly better performance due to constant propagation of the
  1321. * defaults.
  1322. *
  1323. * @see XXH3_128bits_withSeed(), XXH3_128bits_withSecret(): other seeding variants
  1324. * @see @ref single_shot_example "Single Shot Example" for an example.
  1325. */
  1326. XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits(XXH_NOESCAPE const void* data, size_t len);
  1327. /*! @brief Calculates 128-bit seeded variant of XXH3 hash of @p data.
  1328. *
  1329. * @param data The block of data to be hashed, at least @p length bytes in size.
  1330. * @param len The length of @p data, in bytes.
  1331. * @param seed The 64-bit seed to alter the hash result predictably.
  1332. *
  1333. * @return The calculated 128-bit variant of XXH3 value.
  1334. *
  1335. * @note
  1336. * seed == 0 produces the same results as @ref XXH3_64bits().
  1337. *
  1338. * This variant generates a custom secret on the fly based on default secret
  1339. * altered using the @p seed value.
  1340. *
  1341. * While this operation is decently fast, note that it's not completely free.
  1342. *
  1343. * @see XXH3_128bits(), XXH3_128bits_withSecret(): other seeding variants
  1344. * @see @ref single_shot_example "Single Shot Example" for an example.
  1345. */
  1346. XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits_withSeed(XXH_NOESCAPE const void* data, size_t len, XXH64_hash_t seed);
  1347. /*!
  1348. * @brief Calculates 128-bit variant of XXH3 with a custom "secret".
  1349. *
  1350. * @param data The block of data to be hashed, at least @p len bytes in size.
  1351. * @param len The length of @p data, in bytes.
  1352. * @param secret The secret data.
  1353. * @param secretSize The length of @p secret, in bytes.
  1354. *
  1355. * @return The calculated 128-bit variant of XXH3 value.
  1356. *
  1357. * It's possible to provide any blob of bytes as a "secret" to generate the hash.
  1358. * This makes it more difficult for an external actor to prepare an intentional collision.
  1359. * The main condition is that @p secretSize *must* be large enough (>= @ref XXH3_SECRET_SIZE_MIN).
  1360. * However, the quality of the secret impacts the dispersion of the hash algorithm.
  1361. * Therefore, the secret _must_ look like a bunch of random bytes.
  1362. * Avoid "trivial" or structured data such as repeated sequences or a text document.
  1363. * Whenever in doubt about the "randomness" of the blob of bytes,
  1364. * consider employing @ref XXH3_generateSecret() instead (see below).
  1365. * It will generate a proper high entropy secret derived from the blob of bytes.
  1366. * Another advantage of using XXH3_generateSecret() is that
  1367. * it guarantees that all bits within the initial blob of bytes
  1368. * will impact every bit of the output.
  1369. * This is not necessarily the case when using the blob of bytes directly
  1370. * because, when hashing _small_ inputs, only a portion of the secret is employed.
  1371. *
  1372. * @see @ref single_shot_example "Single Shot Example" for an example.
  1373. */
  1374. XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits_withSecret(XXH_NOESCAPE const void* data, size_t len, XXH_NOESCAPE const void* secret, size_t secretSize);
  1375. /******* Streaming *******/
  1376. #ifndef XXH_NO_STREAM
  1377. /*
  1378. * Streaming requires state maintenance.
  1379. * This operation costs memory and CPU.
  1380. * As a consequence, streaming is slower than one-shot hashing.
  1381. * For better performance, prefer one-shot functions whenever applicable.
  1382. *
  1383. * XXH3_128bits uses the same XXH3_state_t as XXH3_64bits().
  1384. * Use already declared XXH3_createState() and XXH3_freeState().
  1385. *
  1386. * All reset and streaming functions have same meaning as their 64-bit counterpart.
  1387. */
  1388. /*!
  1389. * @brief Resets an @ref XXH3_state_t to begin a new hash.
  1390. *
  1391. * @param statePtr The state struct to reset.
  1392. *
  1393. * @pre
  1394. * @p statePtr must not be `NULL`.
  1395. *
  1396. * @return @ref XXH_OK on success.
  1397. * @return @ref XXH_ERROR on failure.
  1398. *
  1399. * @note
  1400. * - This function resets `statePtr` and generate a secret with default parameters.
  1401. * - Call it before @ref XXH3_128bits_update().
  1402. * - Digest will be equivalent to `XXH3_128bits()`.
  1403. *
  1404. * @see @ref streaming_example "Streaming Example"
  1405. */
  1406. XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset(XXH_NOESCAPE XXH3_state_t* statePtr);
  1407. /*!
  1408. * @brief Resets an @ref XXH3_state_t with 64-bit seed to begin a new hash.
  1409. *
  1410. * @param statePtr The state struct to reset.
  1411. * @param seed The 64-bit seed to alter the hash result predictably.
  1412. *
  1413. * @pre
  1414. * @p statePtr must not be `NULL`.
  1415. *
  1416. * @return @ref XXH_OK on success.
  1417. * @return @ref XXH_ERROR on failure.
  1418. *
  1419. * @note
  1420. * - This function resets `statePtr` and generate a secret from `seed`.
  1421. * - Call it before @ref XXH3_128bits_update().
  1422. * - Digest will be equivalent to `XXH3_128bits_withSeed()`.
  1423. *
  1424. * @see @ref streaming_example "Streaming Example"
  1425. */
  1426. XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSeed(XXH_NOESCAPE XXH3_state_t* statePtr, XXH64_hash_t seed);
  1427. /*!
  1428. * @brief Resets an @ref XXH3_state_t with secret data to begin a new hash.
  1429. *
  1430. * @param statePtr The state struct to reset.
  1431. * @param secret The secret data.
  1432. * @param secretSize The length of @p secret, in bytes.
  1433. *
  1434. * @pre
  1435. * @p statePtr must not be `NULL`.
  1436. *
  1437. * @return @ref XXH_OK on success.
  1438. * @return @ref XXH_ERROR on failure.
  1439. *
  1440. * `secret` is referenced, it _must outlive_ the hash streaming session.
  1441. * Similar to one-shot API, `secretSize` must be >= @ref XXH3_SECRET_SIZE_MIN,
  1442. * and the quality of produced hash values depends on secret's entropy
  1443. * (secret's content should look like a bunch of random bytes).
  1444. * When in doubt about the randomness of a candidate `secret`,
  1445. * consider employing `XXH3_generateSecret()` instead (see below).
  1446. *
  1447. * @see @ref streaming_example "Streaming Example"
  1448. */
  1449. XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSecret(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize);
  1450. /*!
  1451. * @brief Consumes a block of @p input to an @ref XXH3_state_t.
  1452. *
  1453. * Call this to incrementally consume blocks of data.
  1454. *
  1455. * @param statePtr The state struct to update.
  1456. * @param input The block of data to be hashed, at least @p length bytes in size.
  1457. * @param length The length of @p input, in bytes.
  1458. *
  1459. * @pre
  1460. * @p statePtr must not be `NULL`.
  1461. *
  1462. * @return @ref XXH_OK on success.
  1463. * @return @ref XXH_ERROR on failure.
  1464. *
  1465. * @note
  1466. * The memory between @p input and @p input + @p length must be valid,
  1467. * readable, contiguous memory. However, if @p length is `0`, @p input may be
  1468. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  1469. *
  1470. */
  1471. XXH_PUBLIC_API XXH_errorcode XXH3_128bits_update (XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* input, size_t length);
  1472. /*!
  1473. * @brief Returns the calculated XXH3 128-bit hash value from an @ref XXH3_state_t.
  1474. *
  1475. * @param statePtr The state struct to calculate the hash from.
  1476. *
  1477. * @pre
  1478. * @p statePtr must not be `NULL`.
  1479. *
  1480. * @return The calculated XXH3 128-bit hash value from that state.
  1481. *
  1482. * @note
  1483. * Calling XXH3_128bits_digest() will not affect @p statePtr, so you can update,
  1484. * digest, and update again.
  1485. *
  1486. */
  1487. XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits_digest (XXH_NOESCAPE const XXH3_state_t* statePtr);
  1488. #endif /* !XXH_NO_STREAM */
  1489. /* Following helper functions make it possible to compare XXH128_hast_t values.
  1490. * Since XXH128_hash_t is a structure, this capability is not offered by the language.
  1491. * Note: For better performance, these functions can be inlined using XXH_INLINE_ALL */
  1492. /*!
  1493. * @brief Check equality of two XXH128_hash_t values
  1494. *
  1495. * @param h1 The 128-bit hash value.
  1496. * @param h2 Another 128-bit hash value.
  1497. *
  1498. * @return `1` if `h1` and `h2` are equal.
  1499. * @return `0` if they are not.
  1500. */
  1501. XXH_PUBLIC_API XXH_PUREF int XXH128_isEqual(XXH128_hash_t h1, XXH128_hash_t h2);
  1502. /*!
  1503. * @brief Compares two @ref XXH128_hash_t
  1504. *
  1505. * This comparator is compatible with stdlib's `qsort()`/`bsearch()`.
  1506. *
  1507. * @param h128_1 Left-hand side value
  1508. * @param h128_2 Right-hand side value
  1509. *
  1510. * @return >0 if @p h128_1 > @p h128_2
  1511. * @return =0 if @p h128_1 == @p h128_2
  1512. * @return <0 if @p h128_1 < @p h128_2
  1513. */
  1514. XXH_PUBLIC_API XXH_PUREF int XXH128_cmp(XXH_NOESCAPE const void* h128_1, XXH_NOESCAPE const void* h128_2);
  1515. /******* Canonical representation *******/
  1516. typedef struct { unsigned char digest[sizeof(XXH128_hash_t)]; } XXH128_canonical_t;
  1517. /*!
  1518. * @brief Converts an @ref XXH128_hash_t to a big endian @ref XXH128_canonical_t.
  1519. *
  1520. * @param dst The @ref XXH128_canonical_t pointer to be stored to.
  1521. * @param hash The @ref XXH128_hash_t to be converted.
  1522. *
  1523. * @pre
  1524. * @p dst must not be `NULL`.
  1525. * @see @ref canonical_representation_example "Canonical Representation Example"
  1526. */
  1527. XXH_PUBLIC_API void XXH128_canonicalFromHash(XXH_NOESCAPE XXH128_canonical_t* dst, XXH128_hash_t hash);
  1528. /*!
  1529. * @brief Converts an @ref XXH128_canonical_t to a native @ref XXH128_hash_t.
  1530. *
  1531. * @param src The @ref XXH128_canonical_t to convert.
  1532. *
  1533. * @pre
  1534. * @p src must not be `NULL`.
  1535. *
  1536. * @return The converted hash.
  1537. * @see @ref canonical_representation_example "Canonical Representation Example"
  1538. */
  1539. XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH128_hashFromCanonical(XXH_NOESCAPE const XXH128_canonical_t* src);
  1540. #endif /* !XXH_NO_XXH3 */
  1541. #endif /* XXH_NO_LONG_LONG */
  1542. /*!
  1543. * @}
  1544. */
  1545. #endif /* XXHASH_H_5627135585666179 */
  1546. #if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXHASH_H_STATIC_13879238742)
  1547. #define XXHASH_H_STATIC_13879238742
  1548. /* ****************************************************************************
  1549. * This section contains declarations which are not guaranteed to remain stable.
  1550. * They may change in future versions, becoming incompatible with a different
  1551. * version of the library.
  1552. * These declarations should only be used with static linking.
  1553. * Never use them in association with dynamic linking!
  1554. ***************************************************************************** */
  1555. /*
  1556. * These definitions are only present to allow static allocation
  1557. * of XXH states, on stack or in a struct, for example.
  1558. * Never **ever** access their members directly.
  1559. */
  1560. /*!
  1561. * @internal
  1562. * @brief Structure for XXH32 streaming API.
  1563. *
  1564. * @note This is only defined when @ref XXH_STATIC_LINKING_ONLY,
  1565. * @ref XXH_INLINE_ALL, or @ref XXH_IMPLEMENTATION is defined. Otherwise it is
  1566. * an opaque type. This allows fields to safely be changed.
  1567. *
  1568. * Typedef'd to @ref XXH32_state_t.
  1569. * Do not access the members of this struct directly.
  1570. * @see XXH64_state_s, XXH3_state_s
  1571. */
  1572. struct XXH32_state_s {
  1573. XXH32_hash_t total_len_32; /*!< Total length hashed, modulo 2^32 */
  1574. XXH32_hash_t large_len; /*!< Whether the hash is >= 16 (handles @ref total_len_32 overflow) */
  1575. XXH32_hash_t acc[4]; /*!< Accumulator lanes */
  1576. unsigned char buffer[16]; /*!< Internal buffer for partial reads. */
  1577. XXH32_hash_t bufferedSize; /*!< Amount of data in @ref buffer */
  1578. XXH32_hash_t reserved; /*!< Reserved field. Do not read nor write to it. */
  1579. }; /* typedef'd to XXH32_state_t */
  1580. #ifndef XXH_NO_LONG_LONG /* defined when there is no 64-bit support */
  1581. /*!
  1582. * @internal
  1583. * @brief Structure for XXH64 streaming API.
  1584. *
  1585. * @note This is only defined when @ref XXH_STATIC_LINKING_ONLY,
  1586. * @ref XXH_INLINE_ALL, or @ref XXH_IMPLEMENTATION is defined. Otherwise it is
  1587. * an opaque type. This allows fields to safely be changed.
  1588. *
  1589. * Typedef'd to @ref XXH64_state_t.
  1590. * Do not access the members of this struct directly.
  1591. * @see XXH32_state_s, XXH3_state_s
  1592. */
  1593. struct XXH64_state_s {
  1594. XXH64_hash_t total_len; /*!< Total length hashed. This is always 64-bit. */
  1595. XXH64_hash_t acc[4]; /*!< Accumulator lanes */
  1596. unsigned char buffer[32]; /*!< Internal buffer for partial reads.. */
  1597. XXH32_hash_t bufferedSize; /*!< Amount of data in @ref buffer */
  1598. XXH32_hash_t reserved32; /*!< Reserved field, needed for padding anyways*/
  1599. XXH64_hash_t reserved64; /*!< Reserved field. Do not read or write to it. */
  1600. }; /* typedef'd to XXH64_state_t */
  1601. #ifndef XXH_NO_XXH3
  1602. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* >= C11 */
  1603. # define XXH_ALIGN(n) _Alignas(n)
  1604. #elif defined(__cplusplus) && (__cplusplus >= 201103L) /* >= C++11 */
  1605. /* In C++ alignas() is a keyword */
  1606. # define XXH_ALIGN(n) alignas(n)
  1607. #elif defined(__GNUC__)
  1608. # define XXH_ALIGN(n) __attribute__ ((aligned(n)))
  1609. #elif defined(_MSC_VER)
  1610. # define XXH_ALIGN(n) __declspec(align(n))
  1611. #else
  1612. # define XXH_ALIGN(n) /* disabled */
  1613. #endif
  1614. /* Old GCC versions only accept the attribute after the type in structures. */
  1615. #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) /* C11+ */ \
  1616. && ! (defined(__cplusplus) && (__cplusplus >= 201103L)) /* >= C++11 */ \
  1617. && defined(__GNUC__)
  1618. # define XXH_ALIGN_MEMBER(align, type) type XXH_ALIGN(align)
  1619. #else
  1620. # define XXH_ALIGN_MEMBER(align, type) XXH_ALIGN(align) type
  1621. #endif
  1622. /*!
  1623. * @brief The size of the internal XXH3 buffer.
  1624. *
  1625. * This is the optimal update size for incremental hashing.
  1626. *
  1627. * @see XXH3_64b_update(), XXH3_128b_update().
  1628. */
  1629. #define XXH3_INTERNALBUFFER_SIZE 256
  1630. /*!
  1631. * @internal
  1632. * @brief Default size of the secret buffer (and @ref XXH3_kSecret).
  1633. *
  1634. * This is the size used in @ref XXH3_kSecret and the seeded functions.
  1635. *
  1636. * Not to be confused with @ref XXH3_SECRET_SIZE_MIN.
  1637. */
  1638. #define XXH3_SECRET_DEFAULT_SIZE 192
  1639. /*!
  1640. * @internal
  1641. * @brief Structure for XXH3 streaming API.
  1642. *
  1643. * @note This is only defined when @ref XXH_STATIC_LINKING_ONLY,
  1644. * @ref XXH_INLINE_ALL, or @ref XXH_IMPLEMENTATION is defined.
  1645. * Otherwise it is an opaque type.
  1646. * Never use this definition in combination with dynamic library.
  1647. * This allows fields to safely be changed in the future.
  1648. *
  1649. * @note ** This structure has a strict alignment requirement of 64 bytes!! **
  1650. * Do not allocate this with `malloc()` or `new`,
  1651. * it will not be sufficiently aligned.
  1652. * Use @ref XXH3_createState() and @ref XXH3_freeState(), or stack allocation.
  1653. *
  1654. * Typedef'd to @ref XXH3_state_t.
  1655. * Do never access the members of this struct directly.
  1656. *
  1657. * @see XXH3_INITSTATE() for stack initialization.
  1658. * @see XXH3_createState(), XXH3_freeState().
  1659. * @see XXH32_state_s, XXH64_state_s
  1660. */
  1661. struct XXH3_state_s {
  1662. XXH_ALIGN_MEMBER(64, XXH64_hash_t acc[8]);
  1663. /*!< The 8 accumulators. See @ref XXH32_state_s::v and @ref XXH64_state_s::v */
  1664. XXH_ALIGN_MEMBER(64, unsigned char customSecret[XXH3_SECRET_DEFAULT_SIZE]);
  1665. /*!< Used to store a custom secret generated from a seed. */
  1666. XXH_ALIGN_MEMBER(64, unsigned char buffer[XXH3_INTERNALBUFFER_SIZE]);
  1667. /*!< The internal buffer. @see XXH32_state_s::mem32 */
  1668. XXH32_hash_t bufferedSize;
  1669. /*!< The amount of memory in @ref buffer, @see XXH32_state_s::memsize */
  1670. XXH32_hash_t useSeed;
  1671. /*!< Reserved field. Needed for padding on 64-bit. */
  1672. size_t nbStripesSoFar;
  1673. /*!< Number or stripes processed. */
  1674. XXH64_hash_t totalLen;
  1675. /*!< Total length hashed. 64-bit even on 32-bit targets. */
  1676. size_t nbStripesPerBlock;
  1677. /*!< Number of stripes per block. */
  1678. size_t secretLimit;
  1679. /*!< Size of @ref customSecret or @ref extSecret */
  1680. XXH64_hash_t seed;
  1681. /*!< Seed for _withSeed variants. Must be zero otherwise, @see XXH3_INITSTATE() */
  1682. XXH64_hash_t reserved64;
  1683. /*!< Reserved field. */
  1684. const unsigned char* extSecret;
  1685. /*!< Reference to an external secret for the _withSecret variants, NULL
  1686. * for other variants. */
  1687. /* note: there may be some padding at the end due to alignment on 64 bytes */
  1688. }; /* typedef'd to XXH3_state_t */
  1689. #undef XXH_ALIGN_MEMBER
  1690. /*!
  1691. * @brief Initializes a stack-allocated `XXH3_state_s`.
  1692. *
  1693. * When the @ref XXH3_state_t structure is merely emplaced on stack,
  1694. * it should be initialized with XXH3_INITSTATE() or a memset()
  1695. * in case its first reset uses XXH3_NNbits_reset_withSeed().
  1696. * This init can be omitted if the first reset uses default or _withSecret mode.
  1697. * This operation isn't necessary when the state is created with XXH3_createState().
  1698. * Note that this doesn't prepare the state for a streaming operation,
  1699. * it's still necessary to use XXH3_NNbits_reset*() afterwards.
  1700. */
  1701. #define XXH3_INITSTATE(XXH3_state_ptr) \
  1702. do { \
  1703. XXH3_state_t* tmp_xxh3_state_ptr = (XXH3_state_ptr); \
  1704. tmp_xxh3_state_ptr->seed = 0; \
  1705. tmp_xxh3_state_ptr->extSecret = NULL; \
  1706. } while(0)
  1707. /*!
  1708. * @brief Calculates the 128-bit hash of @p data using XXH3.
  1709. *
  1710. * @param data The block of data to be hashed, at least @p len bytes in size.
  1711. * @param len The length of @p data, in bytes.
  1712. * @param seed The 64-bit seed to alter the hash's output predictably.
  1713. *
  1714. * @pre
  1715. * The memory between @p data and @p data + @p len must be valid,
  1716. * readable, contiguous memory. However, if @p len is `0`, @p data may be
  1717. * `NULL`. In C++, this also must be *TriviallyCopyable*.
  1718. *
  1719. * @return The calculated 128-bit XXH3 value.
  1720. *
  1721. * @see @ref single_shot_example "Single Shot Example" for an example.
  1722. */
  1723. XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH128(XXH_NOESCAPE const void* data, size_t len, XXH64_hash_t seed);
  1724. /* === Experimental API === */
  1725. /* Symbols defined below must be considered tied to a specific library version. */
  1726. /*!
  1727. * @brief Derive a high-entropy secret from any user-defined content, named customSeed.
  1728. *
  1729. * @param secretBuffer A writable buffer for derived high-entropy secret data.
  1730. * @param secretSize Size of secretBuffer, in bytes. Must be >= XXH3_SECRET_SIZE_MIN.
  1731. * @param customSeed A user-defined content.
  1732. * @param customSeedSize Size of customSeed, in bytes.
  1733. *
  1734. * @return @ref XXH_OK on success.
  1735. * @return @ref XXH_ERROR on failure.
  1736. *
  1737. * The generated secret can be used in combination with `*_withSecret()` functions.
  1738. * The `_withSecret()` variants are useful to provide a higher level of protection
  1739. * than 64-bit seed, as it becomes much more difficult for an external actor to
  1740. * guess how to impact the calculation logic.
  1741. *
  1742. * The function accepts as input a custom seed of any length and any content,
  1743. * and derives from it a high-entropy secret of length @p secretSize into an
  1744. * already allocated buffer @p secretBuffer.
  1745. *
  1746. * The generated secret can then be used with any `*_withSecret()` variant.
  1747. * The functions @ref XXH3_128bits_withSecret(), @ref XXH3_64bits_withSecret(),
  1748. * @ref XXH3_128bits_reset_withSecret() and @ref XXH3_64bits_reset_withSecret()
  1749. * are part of this list. They all accept a `secret` parameter
  1750. * which must be large enough for implementation reasons (>= @ref XXH3_SECRET_SIZE_MIN)
  1751. * _and_ feature very high entropy (consist of random-looking bytes).
  1752. * These conditions can be a high bar to meet, so @ref XXH3_generateSecret() can
  1753. * be employed to ensure proper quality.
  1754. *
  1755. * @p customSeed can be anything. It can have any size, even small ones,
  1756. * and its content can be anything, even "poor entropy" sources such as a bunch
  1757. * of zeroes. The resulting `secret` will nonetheless provide all required qualities.
  1758. *
  1759. * @pre
  1760. * - @p secretSize must be >= @ref XXH3_SECRET_SIZE_MIN
  1761. * - When @p customSeedSize > 0, supplying NULL as customSeed is undefined behavior.
  1762. *
  1763. * Example code:
  1764. * @code{.c}
  1765. * #include <stdio.h>
  1766. * #include <stdlib.h>
  1767. * #include <string.h>
  1768. * #define XXH_STATIC_LINKING_ONLY // expose unstable API
  1769. * #include "xxhash.h"
  1770. * // Hashes argv[2] using the entropy from argv[1].
  1771. * int main(int argc, char* argv[])
  1772. * {
  1773. * char secret[XXH3_SECRET_SIZE_MIN];
  1774. * if (argv != 3) { return 1; }
  1775. * XXH3_generateSecret(secret, sizeof(secret), argv[1], strlen(argv[1]));
  1776. * XXH64_hash_t h = XXH3_64bits_withSecret(
  1777. * argv[2], strlen(argv[2]),
  1778. * secret, sizeof(secret)
  1779. * );
  1780. * printf("%016llx\n", (unsigned long long) h);
  1781. * }
  1782. * @endcode
  1783. */
  1784. XXH_PUBLIC_API XXH_errorcode XXH3_generateSecret(XXH_NOESCAPE void* secretBuffer, size_t secretSize, XXH_NOESCAPE const void* customSeed, size_t customSeedSize);
  1785. /*!
  1786. * @brief Generate the same secret as the _withSeed() variants.
  1787. *
  1788. * @param secretBuffer A writable buffer of @ref XXH3_SECRET_DEFAULT_SIZE bytes
  1789. * @param seed The 64-bit seed to alter the hash result predictably.
  1790. *
  1791. * The generated secret can be used in combination with
  1792. *`*_withSecret()` and `_withSecretandSeed()` variants.
  1793. *
  1794. * Example C++ `std::string` hash class:
  1795. * @code{.cpp}
  1796. * #include <string>
  1797. * #define XXH_STATIC_LINKING_ONLY // expose unstable API
  1798. * #include "xxhash.h"
  1799. * // Slow, seeds each time
  1800. * class HashSlow {
  1801. * XXH64_hash_t seed;
  1802. * public:
  1803. * HashSlow(XXH64_hash_t s) : seed{s} {}
  1804. * size_t operator()(const std::string& x) const {
  1805. * return size_t{XXH3_64bits_withSeed(x.c_str(), x.length(), seed)};
  1806. * }
  1807. * };
  1808. * // Fast, caches the seeded secret for future uses.
  1809. * class HashFast {
  1810. * unsigned char secret[XXH3_SECRET_DEFAULT_SIZE];
  1811. * public:
  1812. * HashFast(XXH64_hash_t s) {
  1813. * XXH3_generateSecret_fromSeed(secret, seed);
  1814. * }
  1815. * size_t operator()(const std::string& x) const {
  1816. * return size_t{
  1817. * XXH3_64bits_withSecret(x.c_str(), x.length(), secret, sizeof(secret))
  1818. * };
  1819. * }
  1820. * };
  1821. * @endcode
  1822. */
  1823. XXH_PUBLIC_API void XXH3_generateSecret_fromSeed(XXH_NOESCAPE void* secretBuffer, XXH64_hash_t seed);
  1824. /*!
  1825. * @brief Maximum size of "short" key in bytes.
  1826. */
  1827. #define XXH3_MIDSIZE_MAX 240
  1828. /*!
  1829. * @brief Calculates 64/128-bit seeded variant of XXH3 hash of @p data.
  1830. *
  1831. * @param data The block of data to be hashed, at least @p len bytes in size.
  1832. * @param len The length of @p data, in bytes.
  1833. * @param secret The secret data.
  1834. * @param secretSize The length of @p secret, in bytes.
  1835. * @param seed The 64-bit seed to alter the hash result predictably.
  1836. *
  1837. * These variants generate hash values using either:
  1838. * - @p seed for "short" keys (< @ref XXH3_MIDSIZE_MAX = 240 bytes)
  1839. * - @p secret for "large" keys (>= @ref XXH3_MIDSIZE_MAX).
  1840. *
  1841. * This generally benefits speed, compared to `_withSeed()` or `_withSecret()`.
  1842. * `_withSeed()` has to generate the secret on the fly for "large" keys.
  1843. * It's fast, but can be perceptible for "not so large" keys (< 1 KB).
  1844. * `_withSecret()` has to generate the masks on the fly for "small" keys,
  1845. * which requires more instructions than _withSeed() variants.
  1846. * Therefore, _withSecretandSeed variant combines the best of both worlds.
  1847. *
  1848. * When @p secret has been generated by XXH3_generateSecret_fromSeed(),
  1849. * this variant produces *exactly* the same results as `_withSeed()` variant,
  1850. * hence offering only a pure speed benefit on "large" input,
  1851. * by skipping the need to regenerate the secret for every large input.
  1852. *
  1853. * Another usage scenario is to hash the secret to a 64-bit hash value,
  1854. * for example with XXH3_64bits(), which then becomes the seed,
  1855. * and then employ both the seed and the secret in _withSecretandSeed().
  1856. * On top of speed, an added benefit is that each bit in the secret
  1857. * has a 50% chance to swap each bit in the output, via its impact to the seed.
  1858. *
  1859. * This is not guaranteed when using the secret directly in "small data" scenarios,
  1860. * because only portions of the secret are employed for small data.
  1861. */
  1862. XXH_PUBLIC_API XXH_PUREF XXH64_hash_t
  1863. XXH3_64bits_withSecretandSeed(XXH_NOESCAPE const void* data, size_t len,
  1864. XXH_NOESCAPE const void* secret, size_t secretSize,
  1865. XXH64_hash_t seed);
  1866. /*!
  1867. * @brief Calculates 128-bit seeded variant of XXH3 hash of @p data.
  1868. *
  1869. * @param data The memory segment to be hashed, at least @p len bytes in size.
  1870. * @param length The length of @p data, in bytes.
  1871. * @param secret The secret used to alter hash result predictably.
  1872. * @param secretSize The length of @p secret, in bytes (must be >= XXH3_SECRET_SIZE_MIN)
  1873. * @param seed64 The 64-bit seed to alter the hash result predictably.
  1874. *
  1875. * @return @ref XXH_OK on success.
  1876. * @return @ref XXH_ERROR on failure.
  1877. *
  1878. * @see XXH3_64bits_withSecretandSeed(): contract is the same.
  1879. */
  1880. XXH_PUBLIC_API XXH_PUREF XXH128_hash_t
  1881. XXH3_128bits_withSecretandSeed(XXH_NOESCAPE const void* input, size_t length,
  1882. XXH_NOESCAPE const void* secret, size_t secretSize,
  1883. XXH64_hash_t seed64);
  1884. #ifndef XXH_NO_STREAM
  1885. /*!
  1886. * @brief Resets an @ref XXH3_state_t with secret data to begin a new hash.
  1887. *
  1888. * @param statePtr A pointer to an @ref XXH3_state_t allocated with @ref XXH3_createState().
  1889. * @param secret The secret data.
  1890. * @param secretSize The length of @p secret, in bytes.
  1891. * @param seed64 The 64-bit seed to alter the hash result predictably.
  1892. *
  1893. * @return @ref XXH_OK on success.
  1894. * @return @ref XXH_ERROR on failure.
  1895. *
  1896. * @see XXH3_64bits_withSecretandSeed(). Contract is identical.
  1897. */
  1898. XXH_PUBLIC_API XXH_errorcode
  1899. XXH3_64bits_reset_withSecretandSeed(XXH_NOESCAPE XXH3_state_t* statePtr,
  1900. XXH_NOESCAPE const void* secret, size_t secretSize,
  1901. XXH64_hash_t seed64);
  1902. /*!
  1903. * @brief Resets an @ref XXH3_state_t with secret data to begin a new hash.
  1904. *
  1905. * @param statePtr A pointer to an @ref XXH3_state_t allocated with @ref XXH3_createState().
  1906. * @param secret The secret data.
  1907. * @param secretSize The length of @p secret, in bytes.
  1908. * @param seed64 The 64-bit seed to alter the hash result predictably.
  1909. *
  1910. * @return @ref XXH_OK on success.
  1911. * @return @ref XXH_ERROR on failure.
  1912. *
  1913. * @see XXH3_64bits_withSecretandSeed(). Contract is identical.
  1914. *
  1915. * Note: there was a bug in an earlier version of this function (<= v0.8.2)
  1916. * that would make it generate an incorrect hash value
  1917. * when @p seed == 0 and @p length < XXH3_MIDSIZE_MAX
  1918. * and @p secret is different from XXH3_generateSecret_fromSeed().
  1919. * As stated in the contract, the correct hash result must be
  1920. * the same as XXH3_128bits_withSeed() when @p length <= XXH3_MIDSIZE_MAX.
  1921. * Results generated by this older version are wrong, hence not comparable.
  1922. */
  1923. XXH_PUBLIC_API XXH_errorcode
  1924. XXH3_128bits_reset_withSecretandSeed(XXH_NOESCAPE XXH3_state_t* statePtr,
  1925. XXH_NOESCAPE const void* secret, size_t secretSize,
  1926. XXH64_hash_t seed64);
  1927. #endif /* !XXH_NO_STREAM */
  1928. #endif /* !XXH_NO_XXH3 */
  1929. #endif /* XXH_NO_LONG_LONG */
  1930. #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
  1931. # define XXH_IMPLEMENTATION
  1932. #endif
  1933. #endif /* defined(XXH_STATIC_LINKING_ONLY) && !defined(XXHASH_H_STATIC_13879238742) */
  1934. /* ======================================================================== */
  1935. /* ======================================================================== */
  1936. /* ======================================================================== */
  1937. /*-**********************************************************************
  1938. * xxHash implementation
  1939. *-**********************************************************************
  1940. * xxHash's implementation used to be hosted inside xxhash.c.
  1941. *
  1942. * However, inlining requires implementation to be visible to the compiler,
  1943. * hence be included alongside the header.
  1944. * Previously, implementation was hosted inside xxhash.c,
  1945. * which was then #included when inlining was activated.
  1946. * This construction created issues with a few build and install systems,
  1947. * as it required xxhash.c to be stored in /include directory.
  1948. *
  1949. * xxHash implementation is now directly integrated within xxhash.h.
  1950. * As a consequence, xxhash.c is no longer needed in /include.
  1951. *
  1952. * xxhash.c is still available and is still useful.
  1953. * In a "normal" setup, when xxhash is not inlined,
  1954. * xxhash.h only exposes the prototypes and public symbols,
  1955. * while xxhash.c can be built into an object file xxhash.o
  1956. * which can then be linked into the final binary.
  1957. ************************************************************************/
  1958. #if ( defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) \
  1959. || defined(XXH_IMPLEMENTATION) ) && !defined(XXH_IMPLEM_13a8737387)
  1960. # define XXH_IMPLEM_13a8737387
  1961. /* *************************************
  1962. * Tuning parameters
  1963. ***************************************/
  1964. /*!
  1965. * @defgroup tuning Tuning parameters
  1966. * @{
  1967. *
  1968. * Various macros to control xxHash's behavior.
  1969. */
  1970. #ifdef XXH_DOXYGEN
  1971. /*!
  1972. * @brief Define this to disable 64-bit code.
  1973. *
  1974. * Useful if only using the @ref XXH32_family and you have a strict C90 compiler.
  1975. */
  1976. # define XXH_NO_LONG_LONG
  1977. # undef XXH_NO_LONG_LONG /* don't actually */
  1978. /*!
  1979. * @brief Controls how unaligned memory is accessed.
  1980. *
  1981. * By default, access to unaligned memory is controlled by `memcpy()`, which is
  1982. * safe and portable.
  1983. *
  1984. * Unfortunately, on some target/compiler combinations, the generated assembly
  1985. * is sub-optimal.
  1986. *
  1987. * The below switch allow selection of a different access method
  1988. * in the search for improved performance.
  1989. *
  1990. * @par Possible options:
  1991. *
  1992. * - `XXH_FORCE_MEMORY_ACCESS=0` (default): `memcpy`
  1993. * @par
  1994. * Use `memcpy()`. Safe and portable. Note that most modern compilers will
  1995. * eliminate the function call and treat it as an unaligned access.
  1996. *
  1997. * - `XXH_FORCE_MEMORY_ACCESS=1`: `__attribute__((aligned(1)))`
  1998. * @par
  1999. * Depends on compiler extensions and is therefore not portable.
  2000. * This method is safe _if_ your compiler supports it,
  2001. * and *generally* as fast or faster than `memcpy`.
  2002. *
  2003. * - `XXH_FORCE_MEMORY_ACCESS=2`: Direct cast
  2004. * @par
  2005. * Casts directly and dereferences. This method doesn't depend on the
  2006. * compiler, but it violates the C standard as it directly dereferences an
  2007. * unaligned pointer. It can generate buggy code on targets which do not
  2008. * support unaligned memory accesses, but in some circumstances, it's the
  2009. * only known way to get the most performance.
  2010. *
  2011. * - `XXH_FORCE_MEMORY_ACCESS=3`: Byteshift
  2012. * @par
  2013. * Also portable. This can generate the best code on old compilers which don't
  2014. * inline small `memcpy()` calls, and it might also be faster on big-endian
  2015. * systems which lack a native byteswap instruction. However, some compilers
  2016. * will emit literal byteshifts even if the target supports unaligned access.
  2017. *
  2018. *
  2019. * @warning
  2020. * Methods 1 and 2 rely on implementation-defined behavior. Use these with
  2021. * care, as what works on one compiler/platform/optimization level may cause
  2022. * another to read garbage data or even crash.
  2023. *
  2024. * See https://fastcompression.blogspot.com/2015/08/accessing-unaligned-memory.html for details.
  2025. *
  2026. * Prefer these methods in priority order (0 > 3 > 1 > 2)
  2027. */
  2028. # define XXH_FORCE_MEMORY_ACCESS 0
  2029. /*!
  2030. * @def XXH_SIZE_OPT
  2031. * @brief Controls how much xxHash optimizes for size.
  2032. *
  2033. * xxHash, when compiled, tends to result in a rather large binary size. This
  2034. * is mostly due to heavy usage to forced inlining and constant folding of the
  2035. * @ref XXH3_family to increase performance.
  2036. *
  2037. * However, some developers prefer size over speed. This option can
  2038. * significantly reduce the size of the generated code. When using the `-Os`
  2039. * or `-Oz` options on GCC or Clang, this is defined to 1 by default,
  2040. * otherwise it is defined to 0.
  2041. *
  2042. * Most of these size optimizations can be controlled manually.
  2043. *
  2044. * This is a number from 0-2.
  2045. * - `XXH_SIZE_OPT` == 0: Default. xxHash makes no size optimizations. Speed
  2046. * comes first.
  2047. * - `XXH_SIZE_OPT` == 1: Default for `-Os` and `-Oz`. xxHash is more
  2048. * conservative and disables hacks that increase code size. It implies the
  2049. * options @ref XXH_NO_INLINE_HINTS == 1, @ref XXH_FORCE_ALIGN_CHECK == 0,
  2050. * and @ref XXH3_NEON_LANES == 8 if they are not already defined.
  2051. * - `XXH_SIZE_OPT` == 2: xxHash tries to make itself as small as possible.
  2052. * Performance may cry. For example, the single shot functions just use the
  2053. * streaming API.
  2054. */
  2055. # define XXH_SIZE_OPT 0
  2056. /*!
  2057. * @def XXH_FORCE_ALIGN_CHECK
  2058. * @brief If defined to non-zero, adds a special path for aligned inputs (XXH32()
  2059. * and XXH64() only).
  2060. *
  2061. * This is an important performance trick for architectures without decent
  2062. * unaligned memory access performance.
  2063. *
  2064. * It checks for input alignment, and when conditions are met, uses a "fast
  2065. * path" employing direct 32-bit/64-bit reads, resulting in _dramatically
  2066. * faster_ read speed.
  2067. *
  2068. * The check costs one initial branch per hash, which is generally negligible,
  2069. * but not zero.
  2070. *
  2071. * Moreover, it's not useful to generate an additional code path if memory
  2072. * access uses the same instruction for both aligned and unaligned
  2073. * addresses (e.g. x86 and aarch64).
  2074. *
  2075. * In these cases, the alignment check can be removed by setting this macro to 0.
  2076. * Then the code will always use unaligned memory access.
  2077. * Align check is automatically disabled on x86, x64, ARM64, and some ARM chips
  2078. * which are platforms known to offer good unaligned memory accesses performance.
  2079. *
  2080. * It is also disabled by default when @ref XXH_SIZE_OPT >= 1.
  2081. *
  2082. * This option does not affect XXH3 (only XXH32 and XXH64).
  2083. */
  2084. # define XXH_FORCE_ALIGN_CHECK 0
  2085. /*!
  2086. * @def XXH_NO_INLINE_HINTS
  2087. * @brief When non-zero, sets all functions to `static`.
  2088. *
  2089. * By default, xxHash tries to force the compiler to inline almost all internal
  2090. * functions.
  2091. *
  2092. * This can usually improve performance due to reduced jumping and improved
  2093. * constant folding, but significantly increases the size of the binary which
  2094. * might not be favorable.
  2095. *
  2096. * Additionally, sometimes the forced inlining can be detrimental to performance,
  2097. * depending on the architecture.
  2098. *
  2099. * XXH_NO_INLINE_HINTS marks all internal functions as static, giving the
  2100. * compiler full control on whether to inline or not.
  2101. *
  2102. * When not optimizing (-O0), using `-fno-inline` with GCC or Clang, or if
  2103. * @ref XXH_SIZE_OPT >= 1, this will automatically be defined.
  2104. */
  2105. # define XXH_NO_INLINE_HINTS 0
  2106. /*!
  2107. * @def XXH3_INLINE_SECRET
  2108. * @brief Determines whether to inline the XXH3 withSecret code.
  2109. *
  2110. * When the secret size is known, the compiler can improve the performance
  2111. * of XXH3_64bits_withSecret() and XXH3_128bits_withSecret().
  2112. *
  2113. * However, if the secret size is not known, it doesn't have any benefit. This
  2114. * happens when xxHash is compiled into a global symbol. Therefore, if
  2115. * @ref XXH_INLINE_ALL is *not* defined, this will be defined to 0.
  2116. *
  2117. * Additionally, this defaults to 0 on GCC 12+, which has an issue with function pointers
  2118. * that are *sometimes* force inline on -Og, and it is impossible to automatically
  2119. * detect this optimization level.
  2120. */
  2121. # define XXH3_INLINE_SECRET 0
  2122. /*!
  2123. * @def XXH32_ENDJMP
  2124. * @brief Whether to use a jump for `XXH32_finalize`.
  2125. *
  2126. * For performance, `XXH32_finalize` uses multiple branches in the finalizer.
  2127. * This is generally preferable for performance,
  2128. * but depending on exact architecture, a jmp may be preferable.
  2129. *
  2130. * This setting is only possibly making a difference for very small inputs.
  2131. */
  2132. # define XXH32_ENDJMP 0
  2133. /*!
  2134. * @internal
  2135. * @brief Redefines old internal names.
  2136. *
  2137. * For compatibility with code that uses xxHash's internals before the names
  2138. * were changed to improve namespacing. There is no other reason to use this.
  2139. */
  2140. # define XXH_OLD_NAMES
  2141. # undef XXH_OLD_NAMES /* don't actually use, it is ugly. */
  2142. /*!
  2143. * @def XXH_NO_STREAM
  2144. * @brief Disables the streaming API.
  2145. *
  2146. * When xxHash is not inlined and the streaming functions are not used, disabling
  2147. * the streaming functions can improve code size significantly, especially with
  2148. * the @ref XXH3_family which tends to make constant folded copies of itself.
  2149. */
  2150. # define XXH_NO_STREAM
  2151. # undef XXH_NO_STREAM /* don't actually */
  2152. #endif /* XXH_DOXYGEN */
  2153. /*!
  2154. * @}
  2155. */
  2156. #ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  2157. /* prefer __packed__ structures (method 1) for GCC
  2158. * < ARMv7 with unaligned access (e.g. Raspbian armhf) still uses byte shifting, so we use memcpy
  2159. * which for some reason does unaligned loads. */
  2160. # if defined(__GNUC__) && !(defined(__ARM_ARCH) && __ARM_ARCH < 7 && defined(__ARM_FEATURE_UNALIGNED))
  2161. # define XXH_FORCE_MEMORY_ACCESS 1
  2162. # endif
  2163. #endif
  2164. #ifndef XXH_SIZE_OPT
  2165. /* default to 1 for -Os or -Oz */
  2166. # if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE_SIZE__)
  2167. # define XXH_SIZE_OPT 1
  2168. # else
  2169. # define XXH_SIZE_OPT 0
  2170. # endif
  2171. #endif
  2172. #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
  2173. /* don't check on sizeopt, x86, aarch64, or arm when unaligned access is available */
  2174. # if XXH_SIZE_OPT >= 1 || \
  2175. defined(__i386) || defined(__x86_64__) || defined(__aarch64__) || defined(__ARM_FEATURE_UNALIGNED) \
  2176. || defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64) || defined(_M_ARM) /* visual */
  2177. # define XXH_FORCE_ALIGN_CHECK 0
  2178. # else
  2179. # define XXH_FORCE_ALIGN_CHECK 1
  2180. # endif
  2181. #endif
  2182. #ifndef XXH_NO_INLINE_HINTS
  2183. # if XXH_SIZE_OPT >= 1 || defined(__NO_INLINE__) /* -O0, -fno-inline */
  2184. # define XXH_NO_INLINE_HINTS 1
  2185. # else
  2186. # define XXH_NO_INLINE_HINTS 0
  2187. # endif
  2188. #endif
  2189. #ifndef XXH3_INLINE_SECRET
  2190. # if (defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12) \
  2191. || !defined(XXH_INLINE_ALL)
  2192. # define XXH3_INLINE_SECRET 0
  2193. # else
  2194. # define XXH3_INLINE_SECRET 1
  2195. # endif
  2196. #endif
  2197. #ifndef XXH32_ENDJMP
  2198. /* generally preferable for performance */
  2199. # define XXH32_ENDJMP 0
  2200. #endif
  2201. /*!
  2202. * @defgroup impl Implementation
  2203. * @{
  2204. */
  2205. /* *************************************
  2206. * Includes & Memory related functions
  2207. ***************************************/
  2208. #if defined(XXH_NO_STREAM)
  2209. /* nothing */
  2210. #elif defined(XXH_NO_STDLIB)
  2211. /* When requesting to disable any mention of stdlib,
  2212. * the library loses the ability to invoked malloc / free.
  2213. * In practice, it means that functions like `XXH*_createState()`
  2214. * will always fail, and return NULL.
  2215. * This flag is useful in situations where
  2216. * xxhash.h is integrated into some kernel, embedded or limited environment
  2217. * without access to dynamic allocation.
  2218. */
  2219. static XXH_CONSTF void* XXH_malloc(size_t s) { (void)s; return NULL; }
  2220. static void XXH_free(void* p) { (void)p; }
  2221. #else
  2222. /*
  2223. * Modify the local functions below should you wish to use
  2224. * different memory routines for malloc() and free()
  2225. */
  2226. #include <stdlib.h>
  2227. /*!
  2228. * @internal
  2229. * @brief Modify this function to use a different routine than malloc().
  2230. */
  2231. static XXH_MALLOCF void* XXH_malloc(size_t s) { return malloc(s); }
  2232. /*!
  2233. * @internal
  2234. * @brief Modify this function to use a different routine than free().
  2235. */
  2236. static void XXH_free(void* p) { free(p); }
  2237. #endif /* XXH_NO_STDLIB */
  2238. #include <string.h>
  2239. /*!
  2240. * @internal
  2241. * @brief Modify this function to use a different routine than memcpy().
  2242. */
  2243. static void* XXH_memcpy(void* dest, const void* src, size_t size)
  2244. {
  2245. return memcpy(dest,src,size);
  2246. }
  2247. #include <limits.h> /* ULLONG_MAX */
  2248. /* *************************************
  2249. * Compiler Specific Options
  2250. ***************************************/
  2251. #ifdef _MSC_VER /* Visual Studio warning fix */
  2252. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  2253. #endif
  2254. #if XXH_NO_INLINE_HINTS /* disable inlining hints */
  2255. # if defined(__GNUC__) || defined(__clang__)
  2256. # define XXH_FORCE_INLINE static __attribute__((__unused__))
  2257. # else
  2258. # define XXH_FORCE_INLINE static
  2259. # endif
  2260. # define XXH_NO_INLINE static
  2261. /* enable inlining hints */
  2262. #elif defined(__GNUC__) || defined(__clang__)
  2263. # define XXH_FORCE_INLINE static __inline__ __attribute__((__always_inline__, __unused__))
  2264. # define XXH_NO_INLINE static __attribute__((__noinline__))
  2265. #elif defined(_MSC_VER) /* Visual Studio */
  2266. # define XXH_FORCE_INLINE static __forceinline
  2267. # define XXH_NO_INLINE static __declspec(noinline)
  2268. #elif defined (__cplusplus) \
  2269. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* C99 */
  2270. # define XXH_FORCE_INLINE static inline
  2271. # define XXH_NO_INLINE static
  2272. #else
  2273. # define XXH_FORCE_INLINE static
  2274. # define XXH_NO_INLINE static
  2275. #endif
  2276. #if defined(XXH_INLINE_ALL)
  2277. # define XXH_STATIC XXH_FORCE_INLINE
  2278. #else
  2279. # define XXH_STATIC static
  2280. #endif
  2281. #if XXH3_INLINE_SECRET
  2282. # define XXH3_WITH_SECRET_INLINE XXH_FORCE_INLINE
  2283. #else
  2284. # define XXH3_WITH_SECRET_INLINE XXH_NO_INLINE
  2285. #endif
  2286. #if ((defined(sun) || defined(__sun)) && __cplusplus) /* Solaris includes __STDC_VERSION__ with C++. Tested with GCC 5.5 */
  2287. # define XXH_RESTRICT /* disable */
  2288. #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* >= C99 */
  2289. # define XXH_RESTRICT restrict
  2290. #elif (defined (__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))) \
  2291. || (defined (__clang__)) \
  2292. || (defined (_MSC_VER) && (_MSC_VER >= 1400)) \
  2293. || (defined (__INTEL_COMPILER) && (__INTEL_COMPILER >= 1300))
  2294. /*
  2295. * There are a LOT more compilers that recognize __restrict but this
  2296. * covers the major ones.
  2297. */
  2298. # define XXH_RESTRICT __restrict
  2299. #else
  2300. # define XXH_RESTRICT /* disable */
  2301. #endif
  2302. /* *************************************
  2303. * Debug
  2304. ***************************************/
  2305. /*!
  2306. * @ingroup tuning
  2307. * @def XXH_DEBUGLEVEL
  2308. * @brief Sets the debugging level.
  2309. *
  2310. * XXH_DEBUGLEVEL is expected to be defined externally, typically via the
  2311. * compiler's command line options. The value must be a number.
  2312. */
  2313. #ifndef XXH_DEBUGLEVEL
  2314. # ifdef DEBUGLEVEL /* backwards compat */
  2315. # define XXH_DEBUGLEVEL DEBUGLEVEL
  2316. # else
  2317. # define XXH_DEBUGLEVEL 0
  2318. # endif
  2319. #endif
  2320. #if (XXH_DEBUGLEVEL>=1)
  2321. # include <assert.h> /* note: can still be disabled with NDEBUG */
  2322. # define XXH_ASSERT(c) assert(c)
  2323. #else
  2324. # if defined(__INTEL_COMPILER)
  2325. # define XXH_ASSERT(c) XXH_ASSUME((unsigned char) (c))
  2326. # else
  2327. # define XXH_ASSERT(c) XXH_ASSUME(c)
  2328. # endif
  2329. #endif
  2330. /* note: use after variable declarations */
  2331. #ifndef XXH_STATIC_ASSERT
  2332. # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */
  2333. # define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { _Static_assert((c),m); } while(0)
  2334. # elif defined(__cplusplus) && (__cplusplus >= 201103L) /* C++11 */
  2335. # define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { static_assert((c),m); } while(0)
  2336. # else
  2337. # define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { struct xxh_sa { char x[(c) ? 1 : -1]; }; } while(0)
  2338. # endif
  2339. # define XXH_STATIC_ASSERT(c) XXH_STATIC_ASSERT_WITH_MESSAGE((c),#c)
  2340. #endif
  2341. /*!
  2342. * @internal
  2343. * @def XXH_COMPILER_GUARD(var)
  2344. * @brief Used to prevent unwanted optimizations for @p var.
  2345. *
  2346. * It uses an empty GCC inline assembly statement with a register constraint
  2347. * which forces @p var into a general purpose register (eg eax, ebx, ecx
  2348. * on x86) and marks it as modified.
  2349. *
  2350. * This is used in a few places to avoid unwanted autovectorization (e.g.
  2351. * XXH32_round()). All vectorization we want is explicit via intrinsics,
  2352. * and _usually_ isn't wanted elsewhere.
  2353. *
  2354. * We also use it to prevent unwanted constant folding for AArch64 in
  2355. * XXH3_initCustomSecret_scalar().
  2356. */
  2357. #if defined(__GNUC__) || defined(__clang__)
  2358. # define XXH_COMPILER_GUARD(var) __asm__("" : "+r" (var))
  2359. #else
  2360. # define XXH_COMPILER_GUARD(var) ((void)0)
  2361. #endif
  2362. /* Specifically for NEON vectors which use the "w" constraint, on
  2363. * Clang. */
  2364. #if defined(__clang__) && defined(__ARM_ARCH) && !defined(__wasm__)
  2365. # define XXH_COMPILER_GUARD_CLANG_NEON(var) __asm__("" : "+w" (var))
  2366. #else
  2367. # define XXH_COMPILER_GUARD_CLANG_NEON(var) ((void)0)
  2368. #endif
  2369. /* *************************************
  2370. * Basic Types
  2371. ***************************************/
  2372. #if !defined (__VMS) \
  2373. && (defined (__cplusplus) \
  2374. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  2375. # ifdef _AIX
  2376. # include <inttypes.h>
  2377. # else
  2378. # include <stdint.h>
  2379. # endif
  2380. typedef uint8_t xxh_u8;
  2381. #else
  2382. typedef unsigned char xxh_u8;
  2383. #endif
  2384. typedef XXH32_hash_t xxh_u32;
  2385. #ifdef XXH_OLD_NAMES
  2386. # warning "XXH_OLD_NAMES is planned to be removed starting v0.9. If the program depends on it, consider moving away from it by employing newer type names directly"
  2387. # define BYTE xxh_u8
  2388. # define U8 xxh_u8
  2389. # define U32 xxh_u32
  2390. #endif
  2391. /* *** Memory access *** */
  2392. /*!
  2393. * @internal
  2394. * @fn xxh_u32 XXH_read32(const void* ptr)
  2395. * @brief Reads an unaligned 32-bit integer from @p ptr in native endianness.
  2396. *
  2397. * Affected by @ref XXH_FORCE_MEMORY_ACCESS.
  2398. *
  2399. * @param ptr The pointer to read from.
  2400. * @return The 32-bit native endian integer from the bytes at @p ptr.
  2401. */
  2402. /*!
  2403. * @internal
  2404. * @fn xxh_u32 XXH_readLE32(const void* ptr)
  2405. * @brief Reads an unaligned 32-bit little endian integer from @p ptr.
  2406. *
  2407. * Affected by @ref XXH_FORCE_MEMORY_ACCESS.
  2408. *
  2409. * @param ptr The pointer to read from.
  2410. * @return The 32-bit little endian integer from the bytes at @p ptr.
  2411. */
  2412. /*!
  2413. * @internal
  2414. * @fn xxh_u32 XXH_readBE32(const void* ptr)
  2415. * @brief Reads an unaligned 32-bit big endian integer from @p ptr.
  2416. *
  2417. * Affected by @ref XXH_FORCE_MEMORY_ACCESS.
  2418. *
  2419. * @param ptr The pointer to read from.
  2420. * @return The 32-bit big endian integer from the bytes at @p ptr.
  2421. */
  2422. /*!
  2423. * @internal
  2424. * @fn xxh_u32 XXH_readLE32_align(const void* ptr, XXH_alignment align)
  2425. * @brief Like @ref XXH_readLE32(), but has an option for aligned reads.
  2426. *
  2427. * Affected by @ref XXH_FORCE_MEMORY_ACCESS.
  2428. * Note that when @ref XXH_FORCE_ALIGN_CHECK == 0, the @p align parameter is
  2429. * always @ref XXH_alignment::XXH_unaligned.
  2430. *
  2431. * @param ptr The pointer to read from.
  2432. * @param align Whether @p ptr is aligned.
  2433. * @pre
  2434. * If @p align == @ref XXH_alignment::XXH_aligned, @p ptr must be 4 byte
  2435. * aligned.
  2436. * @return The 32-bit little endian integer from the bytes at @p ptr.
  2437. */
  2438. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
  2439. /*
  2440. * Manual byteshift. Best for old compilers which don't inline memcpy.
  2441. * We actually directly use XXH_readLE32 and XXH_readBE32.
  2442. */
  2443. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  2444. /*
  2445. * Force direct memory access. Only works on CPU which support unaligned memory
  2446. * access in hardware.
  2447. */
  2448. static xxh_u32 XXH_read32(const void* memPtr) { return *(const xxh_u32*) memPtr; }
  2449. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  2450. /*
  2451. * __attribute__((aligned(1))) is supported by gcc and clang. Originally the
  2452. * documentation claimed that it only increased the alignment, but actually it
  2453. * can decrease it on gcc, clang, and icc:
  2454. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69502,
  2455. * https://gcc.godbolt.org/z/xYez1j67Y.
  2456. */
  2457. #ifdef XXH_OLD_NAMES
  2458. typedef union { xxh_u32 u32; } __attribute__((__packed__)) unalign;
  2459. #endif
  2460. static xxh_u32 XXH_read32(const void* ptr)
  2461. {
  2462. typedef __attribute__((__aligned__(1))) xxh_u32 xxh_unalign32;
  2463. return *((const xxh_unalign32*)ptr);
  2464. }
  2465. #else
  2466. /*
  2467. * Portable and safe solution. Generally efficient.
  2468. * see: https://fastcompression.blogspot.com/2015/08/accessing-unaligned-memory.html
  2469. */
  2470. static xxh_u32 XXH_read32(const void* memPtr)
  2471. {
  2472. xxh_u32 val;
  2473. XXH_memcpy(&val, memPtr, sizeof(val));
  2474. return val;
  2475. }
  2476. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  2477. /* *** Endianness *** */
  2478. /*!
  2479. * @ingroup tuning
  2480. * @def XXH_CPU_LITTLE_ENDIAN
  2481. * @brief Whether the target is little endian.
  2482. *
  2483. * Defined to 1 if the target is little endian, or 0 if it is big endian.
  2484. * It can be defined externally, for example on the compiler command line.
  2485. *
  2486. * If it is not defined,
  2487. * a runtime check (which is usually constant folded) is used instead.
  2488. *
  2489. * @note
  2490. * This is not necessarily defined to an integer constant.
  2491. *
  2492. * @see XXH_isLittleEndian() for the runtime check.
  2493. */
  2494. #ifndef XXH_CPU_LITTLE_ENDIAN
  2495. /*
  2496. * Try to detect endianness automatically, to avoid the nonstandard behavior
  2497. * in `XXH_isLittleEndian()`
  2498. */
  2499. # if defined(_WIN32) /* Windows is always little endian */ \
  2500. || defined(__LITTLE_ENDIAN__) \
  2501. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  2502. # define XXH_CPU_LITTLE_ENDIAN 1
  2503. # elif defined(__BIG_ENDIAN__) \
  2504. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  2505. # define XXH_CPU_LITTLE_ENDIAN 0
  2506. # else
  2507. /*!
  2508. * @internal
  2509. * @brief Runtime check for @ref XXH_CPU_LITTLE_ENDIAN.
  2510. *
  2511. * Most compilers will constant fold this.
  2512. */
  2513. static int XXH_isLittleEndian(void)
  2514. {
  2515. /*
  2516. * Portable and well-defined behavior.
  2517. * Don't use static: it is detrimental to performance.
  2518. */
  2519. const union { xxh_u32 u; xxh_u8 c[4]; } one = { 1 };
  2520. return one.c[0];
  2521. }
  2522. # define XXH_CPU_LITTLE_ENDIAN XXH_isLittleEndian()
  2523. # endif
  2524. #endif
  2525. /* ****************************************
  2526. * Compiler-specific Functions and Macros
  2527. ******************************************/
  2528. #define XXH_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  2529. #ifdef __has_builtin
  2530. # define XXH_HAS_BUILTIN(x) __has_builtin(x)
  2531. #else
  2532. # define XXH_HAS_BUILTIN(x) 0
  2533. #endif
  2534. /*
  2535. * C23 and future versions have standard "unreachable()".
  2536. * Once it has been implemented reliably we can add it as an
  2537. * additional case:
  2538. *
  2539. * ```
  2540. * #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= XXH_C23_VN)
  2541. * # include <stddef.h>
  2542. * # ifdef unreachable
  2543. * # define XXH_UNREACHABLE() unreachable()
  2544. * # endif
  2545. * #endif
  2546. * ```
  2547. *
  2548. * Note C++23 also has std::unreachable() which can be detected
  2549. * as follows:
  2550. * ```
  2551. * #if defined(__cpp_lib_unreachable) && (__cpp_lib_unreachable >= 202202L)
  2552. * # include <utility>
  2553. * # define XXH_UNREACHABLE() std::unreachable()
  2554. * #endif
  2555. * ```
  2556. * NB: `__cpp_lib_unreachable` is defined in the `<version>` header.
  2557. * We don't use that as including `<utility>` in `extern "C"` blocks
  2558. * doesn't work on GCC12
  2559. */
  2560. #if XXH_HAS_BUILTIN(__builtin_unreachable)
  2561. # define XXH_UNREACHABLE() __builtin_unreachable()
  2562. #elif defined(_MSC_VER)
  2563. # define XXH_UNREACHABLE() __assume(0)
  2564. #else
  2565. # define XXH_UNREACHABLE()
  2566. #endif
  2567. #if XXH_HAS_BUILTIN(__builtin_assume)
  2568. # define XXH_ASSUME(c) __builtin_assume(c)
  2569. #else
  2570. # define XXH_ASSUME(c) if (!(c)) { XXH_UNREACHABLE(); }
  2571. #endif
  2572. /*!
  2573. * @internal
  2574. * @def XXH_rotl32(x,r)
  2575. * @brief 32-bit rotate left.
  2576. *
  2577. * @param x The 32-bit integer to be rotated.
  2578. * @param r The number of bits to rotate.
  2579. * @pre
  2580. * @p r > 0 && @p r < 32
  2581. * @note
  2582. * @p x and @p r may be evaluated multiple times.
  2583. * @return The rotated result.
  2584. */
  2585. #if !defined(NO_CLANG_BUILTIN) && XXH_HAS_BUILTIN(__builtin_rotateleft32) \
  2586. && XXH_HAS_BUILTIN(__builtin_rotateleft64)
  2587. # define XXH_rotl32 __builtin_rotateleft32
  2588. # define XXH_rotl64 __builtin_rotateleft64
  2589. #elif XXH_HAS_BUILTIN(__builtin_stdc_rotate_left)
  2590. # define XXH_rotl32 __builtin_stdc_rotate_left
  2591. # define XXH_rotl64 __builtin_stdc_rotate_left
  2592. /* Note: although _rotl exists for minGW (GCC under windows), performance seems poor */
  2593. #elif defined(_MSC_VER)
  2594. # define XXH_rotl32(x,r) _rotl(x,r)
  2595. # define XXH_rotl64(x,r) _rotl64(x,r)
  2596. #else
  2597. # define XXH_rotl32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
  2598. # define XXH_rotl64(x,r) (((x) << (r)) | ((x) >> (64 - (r))))
  2599. #endif
  2600. /*!
  2601. * @internal
  2602. * @fn xxh_u32 XXH_swap32(xxh_u32 x)
  2603. * @brief A 32-bit byteswap.
  2604. *
  2605. * @param x The 32-bit integer to byteswap.
  2606. * @return @p x, byteswapped.
  2607. */
  2608. #if defined(_MSC_VER) /* Visual Studio */
  2609. # define XXH_swap32 _byteswap_ulong
  2610. #elif XXH_GCC_VERSION >= 403
  2611. # define XXH_swap32 __builtin_bswap32
  2612. #else
  2613. static xxh_u32 XXH_swap32 (xxh_u32 x)
  2614. {
  2615. return ((x << 24) & 0xff000000 ) |
  2616. ((x << 8) & 0x00ff0000 ) |
  2617. ((x >> 8) & 0x0000ff00 ) |
  2618. ((x >> 24) & 0x000000ff );
  2619. }
  2620. #endif
  2621. /* ***************************
  2622. * Memory reads
  2623. *****************************/
  2624. /*!
  2625. * @internal
  2626. * @brief Enum to indicate whether a pointer is aligned.
  2627. */
  2628. typedef enum {
  2629. XXH_aligned, /*!< Aligned */
  2630. XXH_unaligned /*!< Possibly unaligned */
  2631. } XXH_alignment;
  2632. /*
  2633. * XXH_FORCE_MEMORY_ACCESS==3 is an endian-independent byteshift load.
  2634. *
  2635. * This is ideal for older compilers which don't inline memcpy.
  2636. */
  2637. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
  2638. XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* memPtr)
  2639. {
  2640. const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
  2641. return bytePtr[0]
  2642. | ((xxh_u32)bytePtr[1] << 8)
  2643. | ((xxh_u32)bytePtr[2] << 16)
  2644. | ((xxh_u32)bytePtr[3] << 24);
  2645. }
  2646. XXH_FORCE_INLINE xxh_u32 XXH_readBE32(const void* memPtr)
  2647. {
  2648. const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
  2649. return bytePtr[3]
  2650. | ((xxh_u32)bytePtr[2] << 8)
  2651. | ((xxh_u32)bytePtr[1] << 16)
  2652. | ((xxh_u32)bytePtr[0] << 24);
  2653. }
  2654. #else
  2655. XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* ptr)
  2656. {
  2657. return XXH_CPU_LITTLE_ENDIAN ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
  2658. }
  2659. static xxh_u32 XXH_readBE32(const void* ptr)
  2660. {
  2661. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
  2662. }
  2663. #endif
  2664. XXH_FORCE_INLINE xxh_u32
  2665. XXH_readLE32_align(const void* ptr, XXH_alignment align)
  2666. {
  2667. if (align==XXH_unaligned) {
  2668. return XXH_readLE32(ptr);
  2669. } else {
  2670. return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u32*)ptr : XXH_swap32(*(const xxh_u32*)ptr);
  2671. }
  2672. }
  2673. /* *************************************
  2674. * Misc
  2675. ***************************************/
  2676. /*! @ingroup public */
  2677. XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
  2678. /* *******************************************************************
  2679. * 32-bit hash functions
  2680. *********************************************************************/
  2681. /*!
  2682. * @}
  2683. * @defgroup XXH32_impl XXH32 implementation
  2684. * @ingroup impl
  2685. *
  2686. * Details on the XXH32 implementation.
  2687. * @{
  2688. */
  2689. /* #define instead of static const, to be used as initializers */
  2690. #define XXH_PRIME32_1 0x9E3779B1U /*!< 0b10011110001101110111100110110001 */
  2691. #define XXH_PRIME32_2 0x85EBCA77U /*!< 0b10000101111010111100101001110111 */
  2692. #define XXH_PRIME32_3 0xC2B2AE3DU /*!< 0b11000010101100101010111000111101 */
  2693. #define XXH_PRIME32_4 0x27D4EB2FU /*!< 0b00100111110101001110101100101111 */
  2694. #define XXH_PRIME32_5 0x165667B1U /*!< 0b00010110010101100110011110110001 */
  2695. #ifdef XXH_OLD_NAMES
  2696. # define PRIME32_1 XXH_PRIME32_1
  2697. # define PRIME32_2 XXH_PRIME32_2
  2698. # define PRIME32_3 XXH_PRIME32_3
  2699. # define PRIME32_4 XXH_PRIME32_4
  2700. # define PRIME32_5 XXH_PRIME32_5
  2701. #endif
  2702. /*!
  2703. * @internal
  2704. * @brief Normal stripe processing routine.
  2705. *
  2706. * This shuffles the bits so that any bit from @p input impacts several bits in
  2707. * @p acc.
  2708. *
  2709. * @param acc The accumulator lane.
  2710. * @param input The stripe of input to mix.
  2711. * @return The mixed accumulator lane.
  2712. */
  2713. static xxh_u32 XXH32_round(xxh_u32 acc, xxh_u32 input)
  2714. {
  2715. acc += input * XXH_PRIME32_2;
  2716. acc = XXH_rotl32(acc, 13);
  2717. acc *= XXH_PRIME32_1;
  2718. #if (defined(__SSE4_1__) || defined(__aarch64__) || defined(__wasm_simd128__)) && !defined(XXH_ENABLE_AUTOVECTORIZE)
  2719. /*
  2720. * UGLY HACK:
  2721. * A compiler fence is used to prevent GCC and Clang from
  2722. * autovectorizing the XXH32 loop (pragmas and attributes don't work for some
  2723. * reason) without globally disabling SSE4.1.
  2724. *
  2725. * The reason we want to avoid vectorization is because despite working on
  2726. * 4 integers at a time, there are multiple factors slowing XXH32 down on
  2727. * SSE4:
  2728. * - There's a ridiculous amount of lag from pmulld (10 cycles of latency on
  2729. * newer chips!) making it slightly slower to multiply four integers at
  2730. * once compared to four integers independently. Even when pmulld was
  2731. * fastest, Sandy/Ivy Bridge, it is still not worth it to go into SSE
  2732. * just to multiply unless doing a long operation.
  2733. *
  2734. * - Four instructions are required to rotate,
  2735. * movqda tmp, v // not required with VEX encoding
  2736. * pslld tmp, 13 // tmp <<= 13
  2737. * psrld v, 19 // x >>= 19
  2738. * por v, tmp // x |= tmp
  2739. * compared to one for scalar:
  2740. * roll v, 13 // reliably fast across the board
  2741. * shldl v, v, 13 // Sandy Bridge and later prefer this for some reason
  2742. *
  2743. * - Instruction level parallelism is actually more beneficial here because
  2744. * the SIMD actually serializes this operation: While v1 is rotating, v2
  2745. * can load data, while v3 can multiply. SSE forces them to operate
  2746. * together.
  2747. *
  2748. * This is also enabled on AArch64, as Clang is *very aggressive* in vectorizing
  2749. * the loop. NEON is only faster on the A53, and with the newer cores, it is less
  2750. * than half the speed.
  2751. *
  2752. * Additionally, this is used on WASM SIMD128 because it JITs to the same
  2753. * SIMD instructions and has the same issue.
  2754. */
  2755. XXH_COMPILER_GUARD(acc);
  2756. #endif
  2757. return acc;
  2758. }
  2759. /*!
  2760. * @internal
  2761. * @brief Mixes all bits to finalize the hash.
  2762. *
  2763. * The final mix ensures that all input bits have a chance to impact any bit in
  2764. * the output digest, resulting in an unbiased distribution.
  2765. *
  2766. * @param hash The hash to avalanche.
  2767. * @return The avalanched hash.
  2768. */
  2769. static xxh_u32 XXH32_avalanche(xxh_u32 hash)
  2770. {
  2771. hash ^= hash >> 15;
  2772. hash *= XXH_PRIME32_2;
  2773. hash ^= hash >> 13;
  2774. hash *= XXH_PRIME32_3;
  2775. hash ^= hash >> 16;
  2776. return hash;
  2777. }
  2778. #define XXH_get32bits(p) XXH_readLE32_align(p, align)
  2779. /*!
  2780. * @internal
  2781. * @brief Sets up the initial accumulator state for XXH32().
  2782. */
  2783. XXH_FORCE_INLINE void
  2784. XXH32_initAccs(xxh_u32 *acc, xxh_u32 seed)
  2785. {
  2786. XXH_ASSERT(acc != NULL);
  2787. acc[0] = seed + XXH_PRIME32_1 + XXH_PRIME32_2;
  2788. acc[1] = seed + XXH_PRIME32_2;
  2789. acc[2] = seed + 0;
  2790. acc[3] = seed - XXH_PRIME32_1;
  2791. }
  2792. /*!
  2793. * @internal
  2794. * @brief Consumes a block of data for XXH32().
  2795. *
  2796. * @return the end input pointer.
  2797. */
  2798. XXH_FORCE_INLINE const xxh_u8 *
  2799. XXH32_consumeLong(
  2800. xxh_u32 *XXH_RESTRICT acc,
  2801. xxh_u8 const *XXH_RESTRICT input,
  2802. size_t len,
  2803. XXH_alignment align
  2804. )
  2805. {
  2806. const xxh_u8* const bEnd = input + len;
  2807. const xxh_u8* const limit = bEnd - 15;
  2808. XXH_ASSERT(acc != NULL);
  2809. XXH_ASSERT(input != NULL);
  2810. XXH_ASSERT(len >= 16);
  2811. do {
  2812. acc[0] = XXH32_round(acc[0], XXH_get32bits(input)); input += 4;
  2813. acc[1] = XXH32_round(acc[1], XXH_get32bits(input)); input += 4;
  2814. acc[2] = XXH32_round(acc[2], XXH_get32bits(input)); input += 4;
  2815. acc[3] = XXH32_round(acc[3], XXH_get32bits(input)); input += 4;
  2816. } while (input < limit);
  2817. return input;
  2818. }
  2819. /*!
  2820. * @internal
  2821. * @brief Merges the accumulator lanes together for XXH32()
  2822. */
  2823. XXH_FORCE_INLINE XXH_PUREF xxh_u32
  2824. XXH32_mergeAccs(const xxh_u32 *acc)
  2825. {
  2826. XXH_ASSERT(acc != NULL);
  2827. return XXH_rotl32(acc[0], 1) + XXH_rotl32(acc[1], 7)
  2828. + XXH_rotl32(acc[2], 12) + XXH_rotl32(acc[3], 18);
  2829. }
  2830. /*!
  2831. * @internal
  2832. * @brief Processes the last 0-15 bytes of @p ptr.
  2833. *
  2834. * There may be up to 15 bytes remaining to consume from the input.
  2835. * This final stage will digest them to ensure that all input bytes are present
  2836. * in the final mix.
  2837. *
  2838. * @param hash The hash to finalize.
  2839. * @param ptr The pointer to the remaining input.
  2840. * @param len The remaining length, modulo 16.
  2841. * @param align Whether @p ptr is aligned.
  2842. * @return The finalized hash.
  2843. * @see XXH64_finalize().
  2844. */
  2845. static XXH_PUREF xxh_u32
  2846. XXH32_finalize(xxh_u32 hash, const xxh_u8* ptr, size_t len, XXH_alignment align)
  2847. {
  2848. #define XXH_PROCESS1 do { \
  2849. hash += (*ptr++) * XXH_PRIME32_5; \
  2850. hash = XXH_rotl32(hash, 11) * XXH_PRIME32_1; \
  2851. } while (0)
  2852. #define XXH_PROCESS4 do { \
  2853. hash += XXH_get32bits(ptr) * XXH_PRIME32_3; \
  2854. ptr += 4; \
  2855. hash = XXH_rotl32(hash, 17) * XXH_PRIME32_4; \
  2856. } while (0)
  2857. if (ptr==NULL) XXH_ASSERT(len == 0);
  2858. /* Compact rerolled version; generally faster */
  2859. if (!XXH32_ENDJMP) {
  2860. len &= 15;
  2861. while (len >= 4) {
  2862. XXH_PROCESS4;
  2863. len -= 4;
  2864. }
  2865. while (len > 0) {
  2866. XXH_PROCESS1;
  2867. --len;
  2868. }
  2869. return XXH32_avalanche(hash);
  2870. } else {
  2871. switch(len&15) /* or switch(bEnd - p) */ {
  2872. case 12: XXH_PROCESS4;
  2873. XXH_FALLTHROUGH; /* fallthrough */
  2874. case 8: XXH_PROCESS4;
  2875. XXH_FALLTHROUGH; /* fallthrough */
  2876. case 4: XXH_PROCESS4;
  2877. return XXH32_avalanche(hash);
  2878. case 13: XXH_PROCESS4;
  2879. XXH_FALLTHROUGH; /* fallthrough */
  2880. case 9: XXH_PROCESS4;
  2881. XXH_FALLTHROUGH; /* fallthrough */
  2882. case 5: XXH_PROCESS4;
  2883. XXH_PROCESS1;
  2884. return XXH32_avalanche(hash);
  2885. case 14: XXH_PROCESS4;
  2886. XXH_FALLTHROUGH; /* fallthrough */
  2887. case 10: XXH_PROCESS4;
  2888. XXH_FALLTHROUGH; /* fallthrough */
  2889. case 6: XXH_PROCESS4;
  2890. XXH_PROCESS1;
  2891. XXH_PROCESS1;
  2892. return XXH32_avalanche(hash);
  2893. case 15: XXH_PROCESS4;
  2894. XXH_FALLTHROUGH; /* fallthrough */
  2895. case 11: XXH_PROCESS4;
  2896. XXH_FALLTHROUGH; /* fallthrough */
  2897. case 7: XXH_PROCESS4;
  2898. XXH_FALLTHROUGH; /* fallthrough */
  2899. case 3: XXH_PROCESS1;
  2900. XXH_FALLTHROUGH; /* fallthrough */
  2901. case 2: XXH_PROCESS1;
  2902. XXH_FALLTHROUGH; /* fallthrough */
  2903. case 1: XXH_PROCESS1;
  2904. XXH_FALLTHROUGH; /* fallthrough */
  2905. case 0: return XXH32_avalanche(hash);
  2906. }
  2907. XXH_ASSERT(0);
  2908. return hash; /* reaching this point is deemed impossible */
  2909. }
  2910. }
  2911. #ifdef XXH_OLD_NAMES
  2912. # define PROCESS1 XXH_PROCESS1
  2913. # define PROCESS4 XXH_PROCESS4
  2914. #else
  2915. # undef XXH_PROCESS1
  2916. # undef XXH_PROCESS4
  2917. #endif
  2918. /*!
  2919. * @internal
  2920. * @brief The implementation for @ref XXH32().
  2921. *
  2922. * @param input , len , seed Directly passed from @ref XXH32().
  2923. * @param align Whether @p input is aligned.
  2924. * @return The calculated hash.
  2925. */
  2926. XXH_FORCE_INLINE XXH_PUREF xxh_u32
  2927. XXH32_endian_align(const xxh_u8* input, size_t len, xxh_u32 seed, XXH_alignment align)
  2928. {
  2929. xxh_u32 h32;
  2930. if (input==NULL) XXH_ASSERT(len == 0);
  2931. if (len>=16) {
  2932. xxh_u32 acc[4];
  2933. XXH32_initAccs(acc, seed);
  2934. input = XXH32_consumeLong(acc, input, len, align);
  2935. h32 = XXH32_mergeAccs(acc);
  2936. } else {
  2937. h32 = seed + XXH_PRIME32_5;
  2938. }
  2939. h32 += (xxh_u32)len;
  2940. return XXH32_finalize(h32, input, len&15, align);
  2941. }
  2942. /*! @ingroup XXH32_family */
  2943. XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t len, XXH32_hash_t seed)
  2944. {
  2945. #if !defined(XXH_NO_STREAM) && XXH_SIZE_OPT >= 2
  2946. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  2947. XXH32_state_t state;
  2948. XXH32_reset(&state, seed);
  2949. XXH32_update(&state, (const xxh_u8*)input, len);
  2950. return XXH32_digest(&state);
  2951. #else
  2952. if (XXH_FORCE_ALIGN_CHECK) {
  2953. if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
  2954. return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
  2955. } }
  2956. return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
  2957. #endif
  2958. }
  2959. /******* Hash streaming *******/
  2960. #ifndef XXH_NO_STREAM
  2961. /*! @ingroup XXH32_family */
  2962. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)
  2963. {
  2964. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  2965. }
  2966. /*! @ingroup XXH32_family */
  2967. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  2968. {
  2969. XXH_free(statePtr);
  2970. return XXH_OK;
  2971. }
  2972. /*! @ingroup XXH32_family */
  2973. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dstState, const XXH32_state_t* srcState)
  2974. {
  2975. XXH_memcpy(dstState, srcState, sizeof(*dstState));
  2976. }
  2977. /*! @ingroup XXH32_family */
  2978. XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, XXH32_hash_t seed)
  2979. {
  2980. XXH_ASSERT(statePtr != NULL);
  2981. memset(statePtr, 0, sizeof(*statePtr));
  2982. XXH32_initAccs(statePtr->acc, seed);
  2983. return XXH_OK;
  2984. }
  2985. /*! @ingroup XXH32_family */
  2986. XXH_PUBLIC_API XXH_errorcode
  2987. XXH32_update(XXH32_state_t* state, const void* input, size_t len)
  2988. {
  2989. if (input==NULL) {
  2990. XXH_ASSERT(len == 0);
  2991. return XXH_OK;
  2992. }
  2993. state->total_len_32 += (XXH32_hash_t)len;
  2994. state->large_len |= (XXH32_hash_t)((len>=16) | (state->total_len_32>=16));
  2995. XXH_ASSERT(state->bufferedSize < sizeof(state->buffer));
  2996. if (len < sizeof(state->buffer) - state->bufferedSize) { /* fill in tmp buffer */
  2997. XXH_memcpy(state->buffer + state->bufferedSize, input, len);
  2998. state->bufferedSize += (XXH32_hash_t)len;
  2999. return XXH_OK;
  3000. }
  3001. { const xxh_u8* xinput = (const xxh_u8*)input;
  3002. const xxh_u8* const bEnd = xinput + len;
  3003. if (state->bufferedSize) { /* non-empty buffer: complete first */
  3004. XXH_memcpy(state->buffer + state->bufferedSize, xinput, sizeof(state->buffer) - state->bufferedSize);
  3005. xinput += sizeof(state->buffer) - state->bufferedSize;
  3006. /* then process one round */
  3007. (void)XXH32_consumeLong(state->acc, state->buffer, sizeof(state->buffer), XXH_aligned);
  3008. state->bufferedSize = 0;
  3009. }
  3010. XXH_ASSERT(xinput <= bEnd);
  3011. if ((size_t)(bEnd - xinput) >= sizeof(state->buffer)) {
  3012. /* Process the remaining data */
  3013. xinput = XXH32_consumeLong(state->acc, xinput, (size_t)(bEnd - xinput), XXH_unaligned);
  3014. }
  3015. if (xinput < bEnd) {
  3016. /* Copy the leftover to the tmp buffer */
  3017. XXH_memcpy(state->buffer, xinput, (size_t)(bEnd-xinput));
  3018. state->bufferedSize = (unsigned)(bEnd-xinput);
  3019. }
  3020. }
  3021. return XXH_OK;
  3022. }
  3023. /*! @ingroup XXH32_family */
  3024. XXH_PUBLIC_API XXH32_hash_t XXH32_digest(const XXH32_state_t* state)
  3025. {
  3026. xxh_u32 h32;
  3027. if (state->large_len) {
  3028. h32 = XXH32_mergeAccs(state->acc);
  3029. } else {
  3030. h32 = state->acc[2] /* == seed */ + XXH_PRIME32_5;
  3031. }
  3032. h32 += state->total_len_32;
  3033. return XXH32_finalize(h32, state->buffer, state->bufferedSize, XXH_aligned);
  3034. }
  3035. #endif /* !XXH_NO_STREAM */
  3036. /******* Canonical representation *******/
  3037. /*! @ingroup XXH32_family */
  3038. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)
  3039. {
  3040. XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
  3041. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
  3042. XXH_memcpy(dst, &hash, sizeof(*dst));
  3043. }
  3044. /*! @ingroup XXH32_family */
  3045. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
  3046. {
  3047. return XXH_readBE32(src);
  3048. }
  3049. #ifndef XXH_NO_LONG_LONG
  3050. /* *******************************************************************
  3051. * 64-bit hash functions
  3052. *********************************************************************/
  3053. /*!
  3054. * @}
  3055. * @ingroup impl
  3056. * @{
  3057. */
  3058. /******* Memory access *******/
  3059. typedef XXH64_hash_t xxh_u64;
  3060. #ifdef XXH_OLD_NAMES
  3061. # define U64 xxh_u64
  3062. #endif
  3063. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
  3064. /*
  3065. * Manual byteshift. Best for old compilers which don't inline memcpy.
  3066. * We actually directly use XXH_readLE64 and XXH_readBE64.
  3067. */
  3068. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  3069. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  3070. static xxh_u64 XXH_read64(const void* memPtr)
  3071. {
  3072. return *(const xxh_u64*) memPtr;
  3073. }
  3074. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  3075. /*
  3076. * __attribute__((aligned(1))) is supported by gcc and clang. Originally the
  3077. * documentation claimed that it only increased the alignment, but actually it
  3078. * can decrease it on gcc, clang, and icc:
  3079. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69502,
  3080. * https://gcc.godbolt.org/z/xYez1j67Y.
  3081. */
  3082. #ifdef XXH_OLD_NAMES
  3083. typedef union { xxh_u32 u32; xxh_u64 u64; } __attribute__((__packed__)) unalign64;
  3084. #endif
  3085. static xxh_u64 XXH_read64(const void* ptr)
  3086. {
  3087. typedef __attribute__((__aligned__(1))) xxh_u64 xxh_unalign64;
  3088. return *((const xxh_unalign64*)ptr);
  3089. }
  3090. #else
  3091. /*
  3092. * Portable and safe solution. Generally efficient.
  3093. * see: https://fastcompression.blogspot.com/2015/08/accessing-unaligned-memory.html
  3094. */
  3095. static xxh_u64 XXH_read64(const void* memPtr)
  3096. {
  3097. xxh_u64 val;
  3098. XXH_memcpy(&val, memPtr, sizeof(val));
  3099. return val;
  3100. }
  3101. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  3102. #if defined(_MSC_VER) /* Visual Studio */
  3103. # define XXH_swap64 _byteswap_uint64
  3104. #elif XXH_GCC_VERSION >= 403
  3105. # define XXH_swap64 __builtin_bswap64
  3106. #else
  3107. static xxh_u64 XXH_swap64(xxh_u64 x)
  3108. {
  3109. return ((x << 56) & 0xff00000000000000ULL) |
  3110. ((x << 40) & 0x00ff000000000000ULL) |
  3111. ((x << 24) & 0x0000ff0000000000ULL) |
  3112. ((x << 8) & 0x000000ff00000000ULL) |
  3113. ((x >> 8) & 0x00000000ff000000ULL) |
  3114. ((x >> 24) & 0x0000000000ff0000ULL) |
  3115. ((x >> 40) & 0x000000000000ff00ULL) |
  3116. ((x >> 56) & 0x00000000000000ffULL);
  3117. }
  3118. #endif
  3119. /* XXH_FORCE_MEMORY_ACCESS==3 is an endian-independent byteshift load. */
  3120. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
  3121. XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* memPtr)
  3122. {
  3123. const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
  3124. return bytePtr[0]
  3125. | ((xxh_u64)bytePtr[1] << 8)
  3126. | ((xxh_u64)bytePtr[2] << 16)
  3127. | ((xxh_u64)bytePtr[3] << 24)
  3128. | ((xxh_u64)bytePtr[4] << 32)
  3129. | ((xxh_u64)bytePtr[5] << 40)
  3130. | ((xxh_u64)bytePtr[6] << 48)
  3131. | ((xxh_u64)bytePtr[7] << 56);
  3132. }
  3133. XXH_FORCE_INLINE xxh_u64 XXH_readBE64(const void* memPtr)
  3134. {
  3135. const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
  3136. return bytePtr[7]
  3137. | ((xxh_u64)bytePtr[6] << 8)
  3138. | ((xxh_u64)bytePtr[5] << 16)
  3139. | ((xxh_u64)bytePtr[4] << 24)
  3140. | ((xxh_u64)bytePtr[3] << 32)
  3141. | ((xxh_u64)bytePtr[2] << 40)
  3142. | ((xxh_u64)bytePtr[1] << 48)
  3143. | ((xxh_u64)bytePtr[0] << 56);
  3144. }
  3145. #else
  3146. XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* ptr)
  3147. {
  3148. return XXH_CPU_LITTLE_ENDIAN ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
  3149. }
  3150. static xxh_u64 XXH_readBE64(const void* ptr)
  3151. {
  3152. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
  3153. }
  3154. #endif
  3155. XXH_FORCE_INLINE xxh_u64
  3156. XXH_readLE64_align(const void* ptr, XXH_alignment align)
  3157. {
  3158. if (align==XXH_unaligned)
  3159. return XXH_readLE64(ptr);
  3160. else
  3161. return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u64*)ptr : XXH_swap64(*(const xxh_u64*)ptr);
  3162. }
  3163. /******* xxh64 *******/
  3164. /*!
  3165. * @}
  3166. * @defgroup XXH64_impl XXH64 implementation
  3167. * @ingroup impl
  3168. *
  3169. * Details on the XXH64 implementation.
  3170. * @{
  3171. */
  3172. /* #define rather that static const, to be used as initializers */
  3173. #define XXH_PRIME64_1 0x9E3779B185EBCA87ULL /*!< 0b1001111000110111011110011011000110000101111010111100101010000111 */
  3174. #define XXH_PRIME64_2 0xC2B2AE3D27D4EB4FULL /*!< 0b1100001010110010101011100011110100100111110101001110101101001111 */
  3175. #define XXH_PRIME64_3 0x165667B19E3779F9ULL /*!< 0b0001011001010110011001111011000110011110001101110111100111111001 */
  3176. #define XXH_PRIME64_4 0x85EBCA77C2B2AE63ULL /*!< 0b1000010111101011110010100111011111000010101100101010111001100011 */
  3177. #define XXH_PRIME64_5 0x27D4EB2F165667C5ULL /*!< 0b0010011111010100111010110010111100010110010101100110011111000101 */
  3178. #ifdef XXH_OLD_NAMES
  3179. # define PRIME64_1 XXH_PRIME64_1
  3180. # define PRIME64_2 XXH_PRIME64_2
  3181. # define PRIME64_3 XXH_PRIME64_3
  3182. # define PRIME64_4 XXH_PRIME64_4
  3183. # define PRIME64_5 XXH_PRIME64_5
  3184. #endif
  3185. /*! @copydoc XXH32_round */
  3186. static xxh_u64 XXH64_round(xxh_u64 acc, xxh_u64 input)
  3187. {
  3188. acc += input * XXH_PRIME64_2;
  3189. acc = XXH_rotl64(acc, 31);
  3190. acc *= XXH_PRIME64_1;
  3191. #if (defined(__AVX512F__)) && !defined(XXH_ENABLE_AUTOVECTORIZE)
  3192. /*
  3193. * DISABLE AUTOVECTORIZATION:
  3194. * A compiler fence is used to prevent GCC and Clang from
  3195. * autovectorizing the XXH64 loop (pragmas and attributes don't work for some
  3196. * reason) without globally disabling AVX512.
  3197. *
  3198. * Autovectorization of XXH64 tends to be detrimental,
  3199. * though the exact outcome may change depending on exact cpu and compiler version.
  3200. * For information, it has been reported as detrimental for Skylake-X,
  3201. * but possibly beneficial for Zen4.
  3202. *
  3203. * The default is to disable auto-vectorization,
  3204. * but you can select to enable it instead using `XXH_ENABLE_AUTOVECTORIZE` build variable.
  3205. */
  3206. XXH_COMPILER_GUARD(acc);
  3207. #endif
  3208. return acc;
  3209. }
  3210. static xxh_u64 XXH64_mergeRound(xxh_u64 acc, xxh_u64 val)
  3211. {
  3212. val = XXH64_round(0, val);
  3213. acc ^= val;
  3214. acc = acc * XXH_PRIME64_1 + XXH_PRIME64_4;
  3215. return acc;
  3216. }
  3217. /*! @copydoc XXH32_avalanche */
  3218. static xxh_u64 XXH64_avalanche(xxh_u64 hash)
  3219. {
  3220. hash ^= hash >> 33;
  3221. hash *= XXH_PRIME64_2;
  3222. hash ^= hash >> 29;
  3223. hash *= XXH_PRIME64_3;
  3224. hash ^= hash >> 32;
  3225. return hash;
  3226. }
  3227. #define XXH_get64bits(p) XXH_readLE64_align(p, align)
  3228. /*!
  3229. * @internal
  3230. * @brief Sets up the initial accumulator state for XXH64().
  3231. */
  3232. XXH_FORCE_INLINE void
  3233. XXH64_initAccs(xxh_u64 *acc, xxh_u64 seed)
  3234. {
  3235. XXH_ASSERT(acc != NULL);
  3236. acc[0] = seed + XXH_PRIME64_1 + XXH_PRIME64_2;
  3237. acc[1] = seed + XXH_PRIME64_2;
  3238. acc[2] = seed + 0;
  3239. acc[3] = seed - XXH_PRIME64_1;
  3240. }
  3241. /*!
  3242. * @internal
  3243. * @brief Consumes a block of data for XXH64().
  3244. *
  3245. * @return the end input pointer.
  3246. */
  3247. XXH_FORCE_INLINE const xxh_u8 *
  3248. XXH64_consumeLong(
  3249. xxh_u64 *XXH_RESTRICT acc,
  3250. xxh_u8 const *XXH_RESTRICT input,
  3251. size_t len,
  3252. XXH_alignment align
  3253. )
  3254. {
  3255. const xxh_u8* const bEnd = input + len;
  3256. const xxh_u8* const limit = bEnd - 31;
  3257. XXH_ASSERT(acc != NULL);
  3258. XXH_ASSERT(input != NULL);
  3259. XXH_ASSERT(len >= 32);
  3260. do {
  3261. /* reroll on 32-bit */
  3262. if (sizeof(void *) < sizeof(xxh_u64)) {
  3263. size_t i;
  3264. for (i = 0; i < 4; i++) {
  3265. acc[i] = XXH64_round(acc[i], XXH_get64bits(input));
  3266. input += 8;
  3267. }
  3268. } else {
  3269. acc[0] = XXH64_round(acc[0], XXH_get64bits(input)); input += 8;
  3270. acc[1] = XXH64_round(acc[1], XXH_get64bits(input)); input += 8;
  3271. acc[2] = XXH64_round(acc[2], XXH_get64bits(input)); input += 8;
  3272. acc[3] = XXH64_round(acc[3], XXH_get64bits(input)); input += 8;
  3273. }
  3274. } while (input < limit);
  3275. return input;
  3276. }
  3277. /*!
  3278. * @internal
  3279. * @brief Merges the accumulator lanes together for XXH64()
  3280. */
  3281. XXH_FORCE_INLINE XXH_PUREF xxh_u64
  3282. XXH64_mergeAccs(const xxh_u64 *acc)
  3283. {
  3284. XXH_ASSERT(acc != NULL);
  3285. {
  3286. xxh_u64 h64 = XXH_rotl64(acc[0], 1) + XXH_rotl64(acc[1], 7)
  3287. + XXH_rotl64(acc[2], 12) + XXH_rotl64(acc[3], 18);
  3288. /* reroll on 32-bit */
  3289. if (sizeof(void *) < sizeof(xxh_u64)) {
  3290. size_t i;
  3291. for (i = 0; i < 4; i++) {
  3292. h64 = XXH64_mergeRound(h64, acc[i]);
  3293. }
  3294. } else {
  3295. h64 = XXH64_mergeRound(h64, acc[0]);
  3296. h64 = XXH64_mergeRound(h64, acc[1]);
  3297. h64 = XXH64_mergeRound(h64, acc[2]);
  3298. h64 = XXH64_mergeRound(h64, acc[3]);
  3299. }
  3300. return h64;
  3301. }
  3302. }
  3303. /*!
  3304. * @internal
  3305. * @brief Processes the last 0-31 bytes of @p ptr.
  3306. *
  3307. * There may be up to 31 bytes remaining to consume from the input.
  3308. * This final stage will digest them to ensure that all input bytes are present
  3309. * in the final mix.
  3310. *
  3311. * @param hash The hash to finalize.
  3312. * @param ptr The pointer to the remaining input.
  3313. * @param len The remaining length, modulo 32.
  3314. * @param align Whether @p ptr is aligned.
  3315. * @return The finalized hash
  3316. * @see XXH32_finalize().
  3317. */
  3318. XXH_STATIC XXH_PUREF xxh_u64
  3319. XXH64_finalize(xxh_u64 hash, const xxh_u8* ptr, size_t len, XXH_alignment align)
  3320. {
  3321. if (ptr==NULL) XXH_ASSERT(len == 0);
  3322. len &= 31;
  3323. while (len >= 8) {
  3324. xxh_u64 const k1 = XXH64_round(0, XXH_get64bits(ptr));
  3325. ptr += 8;
  3326. hash ^= k1;
  3327. hash = XXH_rotl64(hash,27) * XXH_PRIME64_1 + XXH_PRIME64_4;
  3328. len -= 8;
  3329. }
  3330. if (len >= 4) {
  3331. hash ^= (xxh_u64)(XXH_get32bits(ptr)) * XXH_PRIME64_1;
  3332. ptr += 4;
  3333. hash = XXH_rotl64(hash, 23) * XXH_PRIME64_2 + XXH_PRIME64_3;
  3334. len -= 4;
  3335. }
  3336. while (len > 0) {
  3337. hash ^= (*ptr++) * XXH_PRIME64_5;
  3338. hash = XXH_rotl64(hash, 11) * XXH_PRIME64_1;
  3339. --len;
  3340. }
  3341. return XXH64_avalanche(hash);
  3342. }
  3343. #ifdef XXH_OLD_NAMES
  3344. # define PROCESS1_64 XXH_PROCESS1_64
  3345. # define PROCESS4_64 XXH_PROCESS4_64
  3346. # define PROCESS8_64 XXH_PROCESS8_64
  3347. #else
  3348. # undef XXH_PROCESS1_64
  3349. # undef XXH_PROCESS4_64
  3350. # undef XXH_PROCESS8_64
  3351. #endif
  3352. /*!
  3353. * @internal
  3354. * @brief The implementation for @ref XXH64().
  3355. *
  3356. * @param input , len , seed Directly passed from @ref XXH64().
  3357. * @param align Whether @p input is aligned.
  3358. * @return The calculated hash.
  3359. */
  3360. XXH_FORCE_INLINE XXH_PUREF xxh_u64
  3361. XXH64_endian_align(const xxh_u8* input, size_t len, xxh_u64 seed, XXH_alignment align)
  3362. {
  3363. xxh_u64 h64;
  3364. if (input==NULL) XXH_ASSERT(len == 0);
  3365. if (len>=32) { /* Process a large block of data */
  3366. xxh_u64 acc[4];
  3367. XXH64_initAccs(acc, seed);
  3368. input = XXH64_consumeLong(acc, input, len, align);
  3369. h64 = XXH64_mergeAccs(acc);
  3370. } else {
  3371. h64 = seed + XXH_PRIME64_5;
  3372. }
  3373. h64 += (xxh_u64) len;
  3374. return XXH64_finalize(h64, input, len, align);
  3375. }
  3376. /*! @ingroup XXH64_family */
  3377. XXH_PUBLIC_API XXH64_hash_t XXH64 (XXH_NOESCAPE const void* input, size_t len, XXH64_hash_t seed)
  3378. {
  3379. #if !defined(XXH_NO_STREAM) && XXH_SIZE_OPT >= 2
  3380. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  3381. XXH64_state_t state;
  3382. XXH64_reset(&state, seed);
  3383. XXH64_update(&state, (const xxh_u8*)input, len);
  3384. return XXH64_digest(&state);
  3385. #else
  3386. if (XXH_FORCE_ALIGN_CHECK) {
  3387. if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
  3388. return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
  3389. } }
  3390. return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
  3391. #endif
  3392. }
  3393. /******* Hash Streaming *******/
  3394. #ifndef XXH_NO_STREAM
  3395. /*! @ingroup XXH64_family*/
  3396. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
  3397. {
  3398. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  3399. }
  3400. /*! @ingroup XXH64_family */
  3401. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  3402. {
  3403. XXH_free(statePtr);
  3404. return XXH_OK;
  3405. }
  3406. /*! @ingroup XXH64_family */
  3407. XXH_PUBLIC_API void XXH64_copyState(XXH_NOESCAPE XXH64_state_t* dstState, const XXH64_state_t* srcState)
  3408. {
  3409. XXH_memcpy(dstState, srcState, sizeof(*dstState));
  3410. }
  3411. /*! @ingroup XXH64_family */
  3412. XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH_NOESCAPE XXH64_state_t* statePtr, XXH64_hash_t seed)
  3413. {
  3414. XXH_ASSERT(statePtr != NULL);
  3415. memset(statePtr, 0, sizeof(*statePtr));
  3416. XXH64_initAccs(statePtr->acc, seed);
  3417. return XXH_OK;
  3418. }
  3419. /*! @ingroup XXH64_family */
  3420. XXH_PUBLIC_API XXH_errorcode
  3421. XXH64_update (XXH_NOESCAPE XXH64_state_t* state, XXH_NOESCAPE const void* input, size_t len)
  3422. {
  3423. if (input==NULL) {
  3424. XXH_ASSERT(len == 0);
  3425. return XXH_OK;
  3426. }
  3427. state->total_len += len;
  3428. XXH_ASSERT(state->bufferedSize <= sizeof(state->buffer));
  3429. if (len < sizeof(state->buffer) - state->bufferedSize) { /* fill in tmp buffer */
  3430. XXH_memcpy(state->buffer + state->bufferedSize, input, len);
  3431. state->bufferedSize += (XXH32_hash_t)len;
  3432. return XXH_OK;
  3433. }
  3434. { const xxh_u8* xinput = (const xxh_u8*)input;
  3435. const xxh_u8* const bEnd = xinput + len;
  3436. if (state->bufferedSize) { /* non-empty buffer => complete first */
  3437. XXH_memcpy(state->buffer + state->bufferedSize, xinput, sizeof(state->buffer) - state->bufferedSize);
  3438. xinput += sizeof(state->buffer) - state->bufferedSize;
  3439. /* and process one round */
  3440. (void)XXH64_consumeLong(state->acc, state->buffer, sizeof(state->buffer), XXH_aligned);
  3441. state->bufferedSize = 0;
  3442. }
  3443. XXH_ASSERT(xinput <= bEnd);
  3444. if ((size_t)(bEnd - xinput) >= sizeof(state->buffer)) {
  3445. /* Process the remaining data */
  3446. xinput = XXH64_consumeLong(state->acc, xinput, (size_t)(bEnd - xinput), XXH_unaligned);
  3447. }
  3448. if (xinput < bEnd) {
  3449. /* Copy the leftover to the tmp buffer */
  3450. XXH_memcpy(state->buffer, xinput, (size_t)(bEnd-xinput));
  3451. state->bufferedSize = (unsigned)(bEnd-xinput);
  3452. }
  3453. }
  3454. return XXH_OK;
  3455. }
  3456. /*! @ingroup XXH64_family */
  3457. XXH_PUBLIC_API XXH64_hash_t XXH64_digest(XXH_NOESCAPE const XXH64_state_t* state)
  3458. {
  3459. xxh_u64 h64;
  3460. if (state->total_len >= 32) {
  3461. h64 = XXH64_mergeAccs(state->acc);
  3462. } else {
  3463. h64 = state->acc[2] /*seed*/ + XXH_PRIME64_5;
  3464. }
  3465. h64 += (xxh_u64) state->total_len;
  3466. return XXH64_finalize(h64, state->buffer, (size_t)state->total_len, XXH_aligned);
  3467. }
  3468. #endif /* !XXH_NO_STREAM */
  3469. /******* Canonical representation *******/
  3470. /*! @ingroup XXH64_family */
  3471. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH_NOESCAPE XXH64_canonical_t* dst, XXH64_hash_t hash)
  3472. {
  3473. XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
  3474. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
  3475. XXH_memcpy(dst, &hash, sizeof(*dst));
  3476. }
  3477. /*! @ingroup XXH64_family */
  3478. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(XXH_NOESCAPE const XXH64_canonical_t* src)
  3479. {
  3480. return XXH_readBE64(src);
  3481. }
  3482. #ifndef XXH_NO_XXH3
  3483. /* *********************************************************************
  3484. * XXH3
  3485. * New generation hash designed for speed on small keys and vectorization
  3486. ************************************************************************ */
  3487. /*!
  3488. * @}
  3489. * @defgroup XXH3_impl XXH3 implementation
  3490. * @ingroup impl
  3491. * @{
  3492. */
  3493. /* === Compiler specifics === */
  3494. #if (defined(__GNUC__) && (__GNUC__ >= 3)) \
  3495. || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) \
  3496. || defined(__clang__)
  3497. # define XXH_likely(x) __builtin_expect(x, 1)
  3498. # define XXH_unlikely(x) __builtin_expect(x, 0)
  3499. #else
  3500. # define XXH_likely(x) (x)
  3501. # define XXH_unlikely(x) (x)
  3502. #endif
  3503. #ifndef XXH_HAS_INCLUDE
  3504. # ifdef __has_include
  3505. /*
  3506. * Not defined as XXH_HAS_INCLUDE(x) (function-like) because
  3507. * this causes segfaults in Apple Clang 4.2 (on Mac OS X 10.7 Lion)
  3508. */
  3509. # define XXH_HAS_INCLUDE __has_include
  3510. # else
  3511. # define XXH_HAS_INCLUDE(x) 0
  3512. # endif
  3513. #endif
  3514. #if defined(__GNUC__) || defined(__clang__)
  3515. # if defined(__ARM_FEATURE_SVE)
  3516. # error #include <arm_sve.h>
  3517. # endif
  3518. # if defined(__ARM_NEON__) || defined(__ARM_NEON) \
  3519. || (defined(_M_ARM) && _M_ARM >= 7) \
  3520. || defined(_M_ARM64) || defined(_M_ARM64EC) \
  3521. || (defined(__wasm_simd128__) && XXH_HAS_INCLUDE(<arm_neon.h>)) /* WASM SIMD128 via SIMDe */
  3522. # define inline __inline__ /* circumvent a clang bug */
  3523. # include <arm_neon.h>
  3524. # undef inline
  3525. # elif defined(__AVX2__)
  3526. # include <immintrin.h>
  3527. # elif defined(__SSE2__)
  3528. # include <emmintrin.h>
  3529. # elif defined(__loongarch_sx)
  3530. # include <lsxintrin.h>
  3531. # endif
  3532. #endif
  3533. #if defined(_MSC_VER)
  3534. # include <intrin.h>
  3535. #endif
  3536. /*
  3537. * One goal of XXH3 is to make it fast on both 32-bit and 64-bit, while
  3538. * remaining a true 64-bit/128-bit hash function.
  3539. *
  3540. * This is done by prioritizing a subset of 64-bit operations that can be
  3541. * emulated without too many steps on the average 32-bit machine.
  3542. *
  3543. * For example, these two lines seem similar, and run equally fast on 64-bit:
  3544. *
  3545. * xxh_u64 x;
  3546. * x ^= (x >> 47); // good
  3547. * x ^= (x >> 13); // bad
  3548. *
  3549. * However, to a 32-bit machine, there is a major difference.
  3550. *
  3551. * x ^= (x >> 47) looks like this:
  3552. *
  3553. * x.lo ^= (x.hi >> (47 - 32));
  3554. *
  3555. * while x ^= (x >> 13) looks like this:
  3556. *
  3557. * // note: funnel shifts are not usually cheap.
  3558. * x.lo ^= (x.lo >> 13) | (x.hi << (32 - 13));
  3559. * x.hi ^= (x.hi >> 13);
  3560. *
  3561. * The first one is significantly faster than the second, simply because the
  3562. * shift is larger than 32. This means:
  3563. * - All the bits we need are in the upper 32 bits, so we can ignore the lower
  3564. * 32 bits in the shift.
  3565. * - The shift result will always fit in the lower 32 bits, and therefore,
  3566. * we can ignore the upper 32 bits in the xor.
  3567. *
  3568. * Thanks to this optimization, XXH3 only requires these features to be efficient:
  3569. *
  3570. * - Usable unaligned access
  3571. * - A 32-bit or 64-bit ALU
  3572. * - If 32-bit, a decent ADC instruction
  3573. * - A 32 or 64-bit multiply with a 64-bit result
  3574. * - For the 128-bit variant, a decent byteswap helps short inputs.
  3575. *
  3576. * The first two are already required by XXH32, and almost all 32-bit and 64-bit
  3577. * platforms which can run XXH32 can run XXH3 efficiently.
  3578. *
  3579. * Thumb-1, the classic 16-bit only subset of ARM's instruction set, is one
  3580. * notable exception.
  3581. *
  3582. * First of all, Thumb-1 lacks support for the UMULL instruction which
  3583. * performs the important long multiply. This means numerous __aeabi_lmul
  3584. * calls.
  3585. *
  3586. * Second of all, the 8 functional registers are just not enough.
  3587. * Setup for __aeabi_lmul, byteshift loads, pointers, and all arithmetic need
  3588. * Lo registers, and this shuffling results in thousands more MOVs than A32.
  3589. *
  3590. * A32 and T32 don't have this limitation. They can access all 14 registers,
  3591. * do a 32->64 multiply with UMULL, and the flexible operand allowing free
  3592. * shifts is helpful, too.
  3593. *
  3594. * Therefore, we do a quick sanity check.
  3595. *
  3596. * If compiling Thumb-1 for a target which supports ARM instructions, we will
  3597. * emit a warning, as it is not a "sane" platform to compile for.
  3598. *
  3599. * Usually, if this happens, it is because of an accident and you probably need
  3600. * to specify -march, as you likely meant to compile for a newer architecture.
  3601. *
  3602. * Credit: large sections of the vectorial and asm source code paths
  3603. * have been contributed by @easyaspi314
  3604. */
  3605. #if defined(__thumb__) && !defined(__thumb2__) && defined(__ARM_ARCH_ISA_ARM)
  3606. # warning "XXH3 is highly inefficient without ARM or Thumb-2."
  3607. #endif
  3608. /* ==========================================
  3609. * Vectorization detection
  3610. * ========================================== */
  3611. #ifdef XXH_DOXYGEN
  3612. /*!
  3613. * @ingroup tuning
  3614. * @brief Overrides the vectorization implementation chosen for XXH3.
  3615. *
  3616. * Can be defined to 0 to disable SIMD or any of the values mentioned in
  3617. * @ref XXH_VECTOR_TYPE.
  3618. *
  3619. * If this is not defined, it uses predefined macros to determine the best
  3620. * implementation.
  3621. */
  3622. # define XXH_VECTOR XXH_SCALAR
  3623. /*!
  3624. * @ingroup tuning
  3625. * @brief Selects the minimum alignment for XXH3's accumulators.
  3626. *
  3627. * When using SIMD, this should match the alignment required for said vector
  3628. * type, so, for example, 32 for AVX2.
  3629. *
  3630. * Default: Auto detected.
  3631. */
  3632. # define XXH_ACC_ALIGN 8
  3633. #endif
  3634. /* Actual definition */
  3635. #ifndef XXH_DOXYGEN
  3636. #endif
  3637. #ifndef XXH_VECTOR /* can be defined on command line */
  3638. # if defined(__ARM_FEATURE_SVE)
  3639. # define XXH_VECTOR XXH_SVE
  3640. # elif ( \
  3641. defined(__ARM_NEON__) || defined(__ARM_NEON) /* gcc */ \
  3642. || defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC) /* msvc */ \
  3643. || (defined(__wasm_simd128__) && XXH_HAS_INCLUDE(<arm_neon.h>)) /* wasm simd128 via SIMDe */ \
  3644. ) && ( \
  3645. defined(_WIN32) || defined(__LITTLE_ENDIAN__) /* little endian only */ \
  3646. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
  3647. )
  3648. # define XXH_VECTOR XXH_NEON
  3649. # elif defined(__AVX512F__)
  3650. # define XXH_VECTOR XXH_AVX512
  3651. # elif defined(__AVX2__)
  3652. # define XXH_VECTOR XXH_AVX2
  3653. # elif defined(__SSE2__) || defined(_M_AMD64) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP == 2))
  3654. # define XXH_VECTOR XXH_SSE2
  3655. # elif (defined(__PPC64__) && defined(__POWER8_VECTOR__)) \
  3656. || (defined(__s390x__) && defined(__VEC__)) \
  3657. && defined(__GNUC__) /* TODO: IBM XL */
  3658. # define XXH_VECTOR XXH_VSX
  3659. # elif defined(__loongarch_sx)
  3660. # define XXH_VECTOR XXH_LSX
  3661. # else
  3662. # define XXH_VECTOR XXH_SCALAR
  3663. # endif
  3664. #endif
  3665. /* __ARM_FEATURE_SVE is only supported by GCC & Clang. */
  3666. #if (XXH_VECTOR == XXH_SVE) && !defined(__ARM_FEATURE_SVE)
  3667. # ifdef _MSC_VER
  3668. # pragma warning(once : 4606)
  3669. # else
  3670. # warning "__ARM_FEATURE_SVE isn't supported. Use SCALAR instead."
  3671. # endif
  3672. # undef XXH_VECTOR
  3673. # define XXH_VECTOR XXH_SCALAR
  3674. #endif
  3675. /*
  3676. * Controls the alignment of the accumulator,
  3677. * for compatibility with aligned vector loads, which are usually faster.
  3678. */
  3679. #ifndef XXH_ACC_ALIGN
  3680. # if defined(XXH_X86DISPATCH)
  3681. # define XXH_ACC_ALIGN 64 /* for compatibility with avx512 */
  3682. # elif XXH_VECTOR == XXH_SCALAR /* scalar */
  3683. # define XXH_ACC_ALIGN 8
  3684. # elif XXH_VECTOR == XXH_SSE2 /* sse2 */
  3685. # define XXH_ACC_ALIGN 16
  3686. # elif XXH_VECTOR == XXH_AVX2 /* avx2 */
  3687. # define XXH_ACC_ALIGN 32
  3688. # elif XXH_VECTOR == XXH_NEON /* neon */
  3689. # define XXH_ACC_ALIGN 16
  3690. # elif XXH_VECTOR == XXH_VSX /* vsx */
  3691. # define XXH_ACC_ALIGN 16
  3692. # elif XXH_VECTOR == XXH_AVX512 /* avx512 */
  3693. # define XXH_ACC_ALIGN 64
  3694. # elif XXH_VECTOR == XXH_SVE /* sve */
  3695. # define XXH_ACC_ALIGN 64
  3696. # elif XXH_VECTOR == XXH_LSX /* lsx */
  3697. # define XXH_ACC_ALIGN 64
  3698. # endif
  3699. #endif
  3700. #if defined(XXH_X86DISPATCH) || XXH_VECTOR == XXH_SSE2 \
  3701. || XXH_VECTOR == XXH_AVX2 || XXH_VECTOR == XXH_AVX512
  3702. # define XXH_SEC_ALIGN XXH_ACC_ALIGN
  3703. #elif XXH_VECTOR == XXH_SVE
  3704. # define XXH_SEC_ALIGN XXH_ACC_ALIGN
  3705. #else
  3706. # define XXH_SEC_ALIGN 8
  3707. #endif
  3708. #if defined(__GNUC__) || defined(__clang__)
  3709. # define XXH_ALIASING __attribute__((__may_alias__))
  3710. #else
  3711. # define XXH_ALIASING /* nothing */
  3712. #endif
  3713. /*
  3714. * UGLY HACK:
  3715. * GCC usually generates the best code with -O3 for xxHash.
  3716. *
  3717. * However, when targeting AVX2, it is overzealous in its unrolling resulting
  3718. * in code roughly 3/4 the speed of Clang.
  3719. *
  3720. * There are other issues, such as GCC splitting _mm256_loadu_si256 into
  3721. * _mm_loadu_si128 + _mm256_inserti128_si256. This is an optimization which
  3722. * only applies to Sandy and Ivy Bridge... which don't even support AVX2.
  3723. *
  3724. * That is why when compiling the AVX2 version, it is recommended to use either
  3725. * -O2 -mavx2 -march=haswell
  3726. * or
  3727. * -O2 -mavx2 -mno-avx256-split-unaligned-load
  3728. * for decent performance, or to use Clang instead.
  3729. *
  3730. * Fortunately, we can control the first one with a pragma that forces GCC into
  3731. * -O2, but the other one we can't control without "failed to inline always
  3732. * inline function due to target mismatch" warnings.
  3733. */
  3734. #if XXH_VECTOR == XXH_AVX2 /* AVX2 */ \
  3735. && defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
  3736. && defined(__OPTIMIZE__) && XXH_SIZE_OPT <= 0 /* respect -O0 and -Os */
  3737. # pragma GCC push_options
  3738. # pragma GCC optimize("-O2")
  3739. #endif
  3740. #if XXH_VECTOR == XXH_NEON
  3741. /*
  3742. * UGLY HACK: While AArch64 GCC on Linux does not seem to care, on macOS, GCC -O3
  3743. * optimizes out the entire hashLong loop because of the aliasing violation.
  3744. *
  3745. * However, GCC is also inefficient at load-store optimization with vld1q/vst1q,
  3746. * so the only option is to mark it as aliasing.
  3747. */
  3748. typedef uint64x2_t xxh_aliasing_uint64x2_t XXH_ALIASING;
  3749. /*!
  3750. * @internal
  3751. * @brief `vld1q_u64` but faster and alignment-safe.
  3752. *
  3753. * On AArch64, unaligned access is always safe, but on ARMv7-a, it is only
  3754. * *conditionally* safe (`vld1` has an alignment bit like `movdq[ua]` in x86).
  3755. *
  3756. * GCC for AArch64 sees `vld1q_u8` as an intrinsic instead of a load, so it
  3757. * prohibits load-store optimizations. Therefore, a direct dereference is used.
  3758. *
  3759. * Otherwise, `vld1q_u8` is used with `vreinterpretq_u8_u64` to do a safe
  3760. * unaligned load.
  3761. */
  3762. #if defined(__aarch64__) && defined(__GNUC__) && !defined(__clang__)
  3763. XXH_FORCE_INLINE uint64x2_t XXH_vld1q_u64(void const* ptr) /* silence -Wcast-align */
  3764. {
  3765. return *(xxh_aliasing_uint64x2_t const *)ptr;
  3766. }
  3767. #else
  3768. XXH_FORCE_INLINE uint64x2_t XXH_vld1q_u64(void const* ptr)
  3769. {
  3770. return vreinterpretq_u64_u8(vld1q_u8((uint8_t const*)ptr));
  3771. }
  3772. #endif
  3773. /*!
  3774. * @internal
  3775. * @brief `vmlal_u32` on low and high halves of a vector.
  3776. *
  3777. * This is a workaround for AArch64 GCC < 11 which implemented arm_neon.h with
  3778. * inline assembly and were therefore incapable of merging the `vget_{low, high}_u32`
  3779. * with `vmlal_u32`.
  3780. */
  3781. #if defined(__aarch64__) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 11
  3782. XXH_FORCE_INLINE uint64x2_t
  3783. XXH_vmlal_low_u32(uint64x2_t acc, uint32x4_t lhs, uint32x4_t rhs)
  3784. {
  3785. /* Inline assembly is the only way */
  3786. __asm__("umlal %0.2d, %1.2s, %2.2s" : "+w" (acc) : "w" (lhs), "w" (rhs));
  3787. return acc;
  3788. }
  3789. XXH_FORCE_INLINE uint64x2_t
  3790. XXH_vmlal_high_u32(uint64x2_t acc, uint32x4_t lhs, uint32x4_t rhs)
  3791. {
  3792. /* This intrinsic works as expected */
  3793. return vmlal_high_u32(acc, lhs, rhs);
  3794. }
  3795. #else
  3796. /* Portable intrinsic versions */
  3797. XXH_FORCE_INLINE uint64x2_t
  3798. XXH_vmlal_low_u32(uint64x2_t acc, uint32x4_t lhs, uint32x4_t rhs)
  3799. {
  3800. return vmlal_u32(acc, vget_low_u32(lhs), vget_low_u32(rhs));
  3801. }
  3802. /*! @copydoc XXH_vmlal_low_u32
  3803. * Assume the compiler converts this to vmlal_high_u32 on aarch64 */
  3804. XXH_FORCE_INLINE uint64x2_t
  3805. XXH_vmlal_high_u32(uint64x2_t acc, uint32x4_t lhs, uint32x4_t rhs)
  3806. {
  3807. return vmlal_u32(acc, vget_high_u32(lhs), vget_high_u32(rhs));
  3808. }
  3809. #endif
  3810. /*!
  3811. * @ingroup tuning
  3812. * @brief Controls the NEON to scalar ratio for XXH3
  3813. *
  3814. * This can be set to 2, 4, 6, or 8.
  3815. *
  3816. * ARM Cortex CPUs are _very_ sensitive to how their pipelines are used.
  3817. *
  3818. * For example, the Cortex-A73 can dispatch 3 micro-ops per cycle, but only 2 of those
  3819. * can be NEON. If you are only using NEON instructions, you are only using 2/3 of the CPU
  3820. * bandwidth.
  3821. *
  3822. * This is even more noticeable on the more advanced cores like the Cortex-A76 which
  3823. * can dispatch 8 micro-ops per cycle, but still only 2 NEON micro-ops at once.
  3824. *
  3825. * Therefore, to make the most out of the pipeline, it is beneficial to run 6 NEON lanes
  3826. * and 2 scalar lanes, which is chosen by default.
  3827. *
  3828. * This does not apply to Apple processors or 32-bit processors, which run better with
  3829. * full NEON. These will default to 8. Additionally, size-optimized builds run 8 lanes.
  3830. *
  3831. * This change benefits CPUs with large micro-op buffers without negatively affecting
  3832. * most other CPUs:
  3833. *
  3834. * | Chipset | Dispatch type | NEON only | 6:2 hybrid | Diff. |
  3835. * |:----------------------|:--------------------|----------:|-----------:|------:|
  3836. * | Snapdragon 730 (A76) | 2 NEON/8 micro-ops | 8.8 GB/s | 10.1 GB/s | ~16% |
  3837. * | Snapdragon 835 (A73) | 2 NEON/3 micro-ops | 5.1 GB/s | 5.3 GB/s | ~5% |
  3838. * | Marvell PXA1928 (A53) | In-order dual-issue | 1.9 GB/s | 1.9 GB/s | 0% |
  3839. * | Apple M1 | 4 NEON/8 micro-ops | 37.3 GB/s | 36.1 GB/s | ~-3% |
  3840. *
  3841. * It also seems to fix some bad codegen on GCC, making it almost as fast as clang.
  3842. *
  3843. * When using WASM SIMD128, if this is 2 or 6, SIMDe will scalarize 2 of the lanes meaning
  3844. * it effectively becomes worse 4.
  3845. *
  3846. * @see XXH3_accumulate_512_neon()
  3847. */
  3848. # ifndef XXH3_NEON_LANES
  3849. # if (defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) || defined(_M_ARM64EC)) \
  3850. && !defined(__APPLE__) && XXH_SIZE_OPT <= 0
  3851. # define XXH3_NEON_LANES 6
  3852. # else
  3853. # define XXH3_NEON_LANES XXH_ACC_NB
  3854. # endif
  3855. # endif
  3856. #endif /* XXH_VECTOR == XXH_NEON */
  3857. /*
  3858. * VSX and Z Vector helpers.
  3859. *
  3860. * This is very messy, and any pull requests to clean this up are welcome.
  3861. *
  3862. * There are a lot of problems with supporting VSX and s390x, due to
  3863. * inconsistent intrinsics, spotty coverage, and multiple endiannesses.
  3864. */
  3865. #if XXH_VECTOR == XXH_VSX
  3866. /* Annoyingly, these headers _may_ define three macros: `bool`, `vector`,
  3867. * and `pixel`. This is a problem for obvious reasons.
  3868. *
  3869. * These keywords are unnecessary; the spec literally says they are
  3870. * equivalent to `__bool`, `__vector`, and `__pixel` and may be undef'd
  3871. * after including the header.
  3872. *
  3873. * We use pragma push_macro/pop_macro to keep the namespace clean. */
  3874. # pragma push_macro("bool")
  3875. # pragma push_macro("vector")
  3876. # pragma push_macro("pixel")
  3877. /* silence potential macro redefined warnings */
  3878. # undef bool
  3879. # undef vector
  3880. # undef pixel
  3881. # if defined(__s390x__)
  3882. # include <s390intrin.h>
  3883. # else
  3884. # include <altivec.h>
  3885. # endif
  3886. /* Restore the original macro values, if applicable. */
  3887. # pragma pop_macro("pixel")
  3888. # pragma pop_macro("vector")
  3889. # pragma pop_macro("bool")
  3890. typedef __vector unsigned long long xxh_u64x2;
  3891. typedef __vector unsigned char xxh_u8x16;
  3892. typedef __vector unsigned xxh_u32x4;
  3893. /*
  3894. * UGLY HACK: Similar to aarch64 macOS GCC, s390x GCC has the same aliasing issue.
  3895. */
  3896. typedef xxh_u64x2 xxh_aliasing_u64x2 XXH_ALIASING;
  3897. # ifndef XXH_VSX_BE
  3898. # if defined(__BIG_ENDIAN__) \
  3899. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  3900. # define XXH_VSX_BE 1
  3901. # elif defined(__VEC_ELEMENT_REG_ORDER__) && __VEC_ELEMENT_REG_ORDER__ == __ORDER_BIG_ENDIAN__
  3902. # warning "-maltivec=be is not recommended. Please use native endianness."
  3903. # define XXH_VSX_BE 1
  3904. # else
  3905. # define XXH_VSX_BE 0
  3906. # endif
  3907. # endif /* !defined(XXH_VSX_BE) */
  3908. # if XXH_VSX_BE
  3909. # if defined(__POWER9_VECTOR__) || (defined(__clang__) && defined(__s390x__))
  3910. # define XXH_vec_revb vec_revb
  3911. # else
  3912. /*!
  3913. * A polyfill for POWER9's vec_revb().
  3914. */
  3915. XXH_FORCE_INLINE xxh_u64x2 XXH_vec_revb(xxh_u64x2 val)
  3916. {
  3917. xxh_u8x16 const vByteSwap = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
  3918. 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08 };
  3919. return vec_perm(val, val, vByteSwap);
  3920. }
  3921. # endif
  3922. # endif /* XXH_VSX_BE */
  3923. /*!
  3924. * Performs an unaligned vector load and byte swaps it on big endian.
  3925. */
  3926. XXH_FORCE_INLINE xxh_u64x2 XXH_vec_loadu(const void *ptr)
  3927. {
  3928. xxh_u64x2 ret;
  3929. XXH_memcpy(&ret, ptr, sizeof(xxh_u64x2));
  3930. # if XXH_VSX_BE
  3931. ret = XXH_vec_revb(ret);
  3932. # endif
  3933. return ret;
  3934. }
  3935. /*
  3936. * vec_mulo and vec_mule are very problematic intrinsics on PowerPC
  3937. *
  3938. * These intrinsics weren't added until GCC 8, despite existing for a while,
  3939. * and they are endian dependent. Also, their meaning swap depending on version.
  3940. * */
  3941. # if defined(__s390x__)
  3942. /* s390x is always big endian, no issue on this platform */
  3943. # define XXH_vec_mulo vec_mulo
  3944. # define XXH_vec_mule vec_mule
  3945. # elif defined(__clang__) && XXH_HAS_BUILTIN(__builtin_altivec_vmuleuw) && !defined(__ibmxl__)
  3946. /* Clang has a better way to control this, we can just use the builtin which doesn't swap. */
  3947. /* The IBM XL Compiler (which defined __clang__) only implements the vec_* operations */
  3948. # define XXH_vec_mulo __builtin_altivec_vmulouw
  3949. # define XXH_vec_mule __builtin_altivec_vmuleuw
  3950. # else
  3951. /* gcc needs inline assembly */
  3952. /* Adapted from https://github.com/google/highwayhash/blob/master/highwayhash/hh_vsx.h. */
  3953. XXH_FORCE_INLINE xxh_u64x2 XXH_vec_mulo(xxh_u32x4 a, xxh_u32x4 b)
  3954. {
  3955. xxh_u64x2 result;
  3956. __asm__("vmulouw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
  3957. return result;
  3958. }
  3959. XXH_FORCE_INLINE xxh_u64x2 XXH_vec_mule(xxh_u32x4 a, xxh_u32x4 b)
  3960. {
  3961. xxh_u64x2 result;
  3962. __asm__("vmuleuw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
  3963. return result;
  3964. }
  3965. # endif /* XXH_vec_mulo, XXH_vec_mule */
  3966. #endif /* XXH_VECTOR == XXH_VSX */
  3967. #if XXH_VECTOR == XXH_SVE
  3968. #define ACCRND(acc, offset) \
  3969. do { \
  3970. svuint64_t input_vec = svld1_u64(mask, xinput + offset); \
  3971. svuint64_t secret_vec = svld1_u64(mask, xsecret + offset); \
  3972. svuint64_t mixed = sveor_u64_x(mask, secret_vec, input_vec); \
  3973. svuint64_t swapped = svtbl_u64(input_vec, kSwap); \
  3974. svuint64_t mixed_lo = svextw_u64_x(mask, mixed); \
  3975. svuint64_t mixed_hi = svlsr_n_u64_x(mask, mixed, 32); \
  3976. svuint64_t mul = svmad_u64_x(mask, mixed_lo, mixed_hi, swapped); \
  3977. acc = svadd_u64_x(mask, acc, mul); \
  3978. } while (0)
  3979. #endif /* XXH_VECTOR == XXH_SVE */
  3980. /* prefetch
  3981. * can be disabled, by declaring XXH_NO_PREFETCH build macro */
  3982. #if defined(XXH_NO_PREFETCH)
  3983. # define XXH_PREFETCH(ptr) (void)(ptr) /* disabled */
  3984. #else
  3985. # if XXH_SIZE_OPT >= 1
  3986. # define XXH_PREFETCH(ptr) (void)(ptr)
  3987. # elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) /* _mm_prefetch() not defined outside of x86/x64 */
  3988. # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
  3989. # define XXH_PREFETCH(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
  3990. # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  3991. # define XXH_PREFETCH(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  3992. # else
  3993. # define XXH_PREFETCH(ptr) (void)(ptr) /* disabled */
  3994. # endif
  3995. #endif /* XXH_NO_PREFETCH */
  3996. /* ==========================================
  3997. * XXH3 default settings
  3998. * ========================================== */
  3999. #define XXH_SECRET_DEFAULT_SIZE 192 /* minimum XXH3_SECRET_SIZE_MIN */
  4000. #if (XXH_SECRET_DEFAULT_SIZE < XXH3_SECRET_SIZE_MIN)
  4001. # error "default keyset is not large enough"
  4002. #endif
  4003. /*! Pseudorandom secret taken directly from FARSH. */
  4004. XXH_ALIGN(64) static const xxh_u8 XXH3_kSecret[XXH_SECRET_DEFAULT_SIZE] = {
  4005. 0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c,
  4006. 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f,
  4007. 0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21,
  4008. 0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c,
  4009. 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3,
  4010. 0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8,
  4011. 0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d,
  4012. 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64,
  4013. 0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb,
  4014. 0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e,
  4015. 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce,
  4016. 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e,
  4017. };
  4018. static const xxh_u64 PRIME_MX1 = 0x165667919E3779F9ULL; /*!< 0b0001011001010110011001111001000110011110001101110111100111111001 */
  4019. static const xxh_u64 PRIME_MX2 = 0x9FB21C651E98DF25ULL; /*!< 0b1001111110110010000111000110010100011110100110001101111100100101 */
  4020. #ifdef XXH_OLD_NAMES
  4021. # define kSecret XXH3_kSecret
  4022. #endif
  4023. #ifdef XXH_DOXYGEN
  4024. /*!
  4025. * @brief Calculates a 32-bit to 64-bit long multiply.
  4026. *
  4027. * Implemented as a macro.
  4028. *
  4029. * Wraps `__emulu` on MSVC x86 because it tends to call `__allmul` when it doesn't
  4030. * need to (but it shouldn't need to anyways, it is about 7 instructions to do
  4031. * a 64x64 multiply...). Since we know that this will _always_ emit `MULL`, we
  4032. * use that instead of the normal method.
  4033. *
  4034. * If you are compiling for platforms like Thumb-1 and don't have a better option,
  4035. * you may also want to write your own long multiply routine here.
  4036. *
  4037. * @param x, y Numbers to be multiplied
  4038. * @return 64-bit product of the low 32 bits of @p x and @p y.
  4039. */
  4040. XXH_FORCE_INLINE xxh_u64
  4041. XXH_mult32to64(xxh_u64 x, xxh_u64 y)
  4042. {
  4043. return (x & 0xFFFFFFFF) * (y & 0xFFFFFFFF);
  4044. }
  4045. #elif defined(_MSC_VER) && defined(_M_IX86)
  4046. # define XXH_mult32to64(x, y) __emulu((unsigned)(x), (unsigned)(y))
  4047. #else
  4048. /*
  4049. * Downcast + upcast is usually better than masking on older compilers like
  4050. * GCC 4.2 (especially 32-bit ones), all without affecting newer compilers.
  4051. *
  4052. * The other method, (x & 0xFFFFFFFF) * (y & 0xFFFFFFFF), will AND both operands
  4053. * and perform a full 64x64 multiply -- entirely redundant on 32-bit.
  4054. */
  4055. # define XXH_mult32to64(x, y) ((xxh_u64)(xxh_u32)(x) * (xxh_u64)(xxh_u32)(y))
  4056. #endif
  4057. /*!
  4058. * @brief Calculates a 64->128-bit long multiply.
  4059. *
  4060. * Uses `__uint128_t` and `_umul128` if available, otherwise uses a scalar
  4061. * version.
  4062. *
  4063. * @param lhs , rhs The 64-bit integers to be multiplied
  4064. * @return The 128-bit result represented in an @ref XXH128_hash_t.
  4065. */
  4066. static XXH128_hash_t
  4067. XXH_mult64to128(xxh_u64 lhs, xxh_u64 rhs)
  4068. {
  4069. /*
  4070. * GCC/Clang __uint128_t method.
  4071. *
  4072. * On most 64-bit targets, GCC and Clang define a __uint128_t type.
  4073. * This is usually the best way as it usually uses a native long 64-bit
  4074. * multiply, such as MULQ on x86_64 or MUL + UMULH on aarch64.
  4075. *
  4076. * Usually.
  4077. *
  4078. * Despite being a 32-bit platform, Clang (and emscripten) define this type
  4079. * despite not having the arithmetic for it. This results in a laggy
  4080. * compiler builtin call which calculates a full 128-bit multiply.
  4081. * In that case it is best to use the portable one.
  4082. * https://github.com/Cyan4973/xxHash/issues/211#issuecomment-515575677
  4083. */
  4084. #if (defined(__GNUC__) || defined(__clang__)) && !defined(__wasm__) \
  4085. && defined(__SIZEOF_INT128__) \
  4086. || (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  4087. __uint128_t const product = (__uint128_t)lhs * (__uint128_t)rhs;
  4088. XXH128_hash_t r128;
  4089. r128.low64 = (xxh_u64)(product);
  4090. r128.high64 = (xxh_u64)(product >> 64);
  4091. return r128;
  4092. /*
  4093. * MSVC for x64's _umul128 method.
  4094. *
  4095. * xxh_u64 _umul128(xxh_u64 Multiplier, xxh_u64 Multiplicand, xxh_u64 *HighProduct);
  4096. *
  4097. * This compiles to single operand MUL on x64.
  4098. */
  4099. #elif (defined(_M_X64) || defined(_M_IA64)) && !defined(_M_ARM64EC)
  4100. #ifndef _MSC_VER
  4101. # pragma intrinsic(_umul128)
  4102. #endif
  4103. xxh_u64 product_high;
  4104. xxh_u64 const product_low = _umul128(lhs, rhs, &product_high);
  4105. XXH128_hash_t r128;
  4106. r128.low64 = product_low;
  4107. r128.high64 = product_high;
  4108. return r128;
  4109. /*
  4110. * MSVC for ARM64's __umulh method.
  4111. *
  4112. * This compiles to the same MUL + UMULH as GCC/Clang's __uint128_t method.
  4113. */
  4114. #elif defined(_M_ARM64) || defined(_M_ARM64EC)
  4115. #ifndef _MSC_VER
  4116. # pragma intrinsic(__umulh)
  4117. #endif
  4118. XXH128_hash_t r128;
  4119. r128.low64 = lhs * rhs;
  4120. r128.high64 = __umulh(lhs, rhs);
  4121. return r128;
  4122. #else
  4123. /*
  4124. * Portable scalar method. Optimized for 32-bit and 64-bit ALUs.
  4125. *
  4126. * This is a fast and simple grade school multiply, which is shown below
  4127. * with base 10 arithmetic instead of base 0x100000000.
  4128. *
  4129. * 9 3 // D2 lhs = 93
  4130. * x 7 5 // D2 rhs = 75
  4131. * ----------
  4132. * 1 5 // D2 lo_lo = (93 % 10) * (75 % 10) = 15
  4133. * 4 5 | // D2 hi_lo = (93 / 10) * (75 % 10) = 45
  4134. * 2 1 | // D2 lo_hi = (93 % 10) * (75 / 10) = 21
  4135. * + 6 3 | | // D2 hi_hi = (93 / 10) * (75 / 10) = 63
  4136. * ---------
  4137. * 2 7 | // D2 cross = (15 / 10) + (45 % 10) + 21 = 27
  4138. * + 6 7 | | // D2 upper = (27 / 10) + (45 / 10) + 63 = 67
  4139. * ---------
  4140. * 6 9 7 5 // D4 res = (27 * 10) + (15 % 10) + (67 * 100) = 6975
  4141. *
  4142. * The reasons for adding the products like this are:
  4143. * 1. It avoids manual carry tracking. Just like how
  4144. * (9 * 9) + 9 + 9 = 99, the same applies with this for UINT64_MAX.
  4145. * This avoids a lot of complexity.
  4146. *
  4147. * 2. It hints for, and on Clang, compiles to, the powerful UMAAL
  4148. * instruction available in ARM's Digital Signal Processing extension
  4149. * in 32-bit ARMv6 and later, which is shown below:
  4150. *
  4151. * void UMAAL(xxh_u32 *RdLo, xxh_u32 *RdHi, xxh_u32 Rn, xxh_u32 Rm)
  4152. * {
  4153. * xxh_u64 product = (xxh_u64)*RdLo * (xxh_u64)*RdHi + Rn + Rm;
  4154. * *RdLo = (xxh_u32)(product & 0xFFFFFFFF);
  4155. * *RdHi = (xxh_u32)(product >> 32);
  4156. * }
  4157. *
  4158. * This instruction was designed for efficient long multiplication, and
  4159. * allows this to be calculated in only 4 instructions at speeds
  4160. * comparable to some 64-bit ALUs.
  4161. *
  4162. * 3. It isn't terrible on other platforms. Usually this will be a couple
  4163. * of 32-bit ADD/ADCs.
  4164. */
  4165. /* First calculate all of the cross products. */
  4166. xxh_u64 const lo_lo = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs & 0xFFFFFFFF);
  4167. xxh_u64 const hi_lo = XXH_mult32to64(lhs >> 32, rhs & 0xFFFFFFFF);
  4168. xxh_u64 const lo_hi = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs >> 32);
  4169. xxh_u64 const hi_hi = XXH_mult32to64(lhs >> 32, rhs >> 32);
  4170. /* Now add the products together. These will never overflow. */
  4171. xxh_u64 const cross = (lo_lo >> 32) + (hi_lo & 0xFFFFFFFF) + lo_hi;
  4172. xxh_u64 const upper = (hi_lo >> 32) + (cross >> 32) + hi_hi;
  4173. xxh_u64 const lower = (cross << 32) | (lo_lo & 0xFFFFFFFF);
  4174. XXH128_hash_t r128;
  4175. r128.low64 = lower;
  4176. r128.high64 = upper;
  4177. return r128;
  4178. #endif
  4179. }
  4180. /*!
  4181. * @brief Calculates a 64-bit to 128-bit multiply, then XOR folds it.
  4182. *
  4183. * The reason for the separate function is to prevent passing too many structs
  4184. * around by value. This will hopefully inline the multiply, but we don't force it.
  4185. *
  4186. * @param lhs , rhs The 64-bit integers to multiply
  4187. * @return The low 64 bits of the product XOR'd by the high 64 bits.
  4188. * @see XXH_mult64to128()
  4189. */
  4190. static xxh_u64
  4191. XXH3_mul128_fold64(xxh_u64 lhs, xxh_u64 rhs)
  4192. {
  4193. XXH128_hash_t product = XXH_mult64to128(lhs, rhs);
  4194. return product.low64 ^ product.high64;
  4195. }
  4196. /*! Seems to produce slightly better code on GCC for some reason. */
  4197. XXH_FORCE_INLINE XXH_CONSTF xxh_u64 XXH_xorshift64(xxh_u64 v64, int shift)
  4198. {
  4199. XXH_ASSERT(0 <= shift && shift < 64);
  4200. return v64 ^ (v64 >> shift);
  4201. }
  4202. /*
  4203. * This is a fast avalanche stage,
  4204. * suitable when input bits are already partially mixed
  4205. */
  4206. static XXH64_hash_t XXH3_avalanche(xxh_u64 h64)
  4207. {
  4208. h64 = XXH_xorshift64(h64, 37);
  4209. h64 *= PRIME_MX1;
  4210. h64 = XXH_xorshift64(h64, 32);
  4211. return h64;
  4212. }
  4213. /*
  4214. * This is a stronger avalanche,
  4215. * inspired by Pelle Evensen's rrmxmx
  4216. * preferable when input has not been previously mixed
  4217. */
  4218. static XXH64_hash_t XXH3_rrmxmx(xxh_u64 h64, xxh_u64 len)
  4219. {
  4220. /* this mix is inspired by Pelle Evensen's rrmxmx */
  4221. h64 ^= XXH_rotl64(h64, 49) ^ XXH_rotl64(h64, 24);
  4222. h64 *= PRIME_MX2;
  4223. h64 ^= (h64 >> 35) + len ;
  4224. h64 *= PRIME_MX2;
  4225. return XXH_xorshift64(h64, 28);
  4226. }
  4227. /* ==========================================
  4228. * Short keys
  4229. * ==========================================
  4230. * One of the shortcomings of XXH32 and XXH64 was that their performance was
  4231. * sub-optimal on short lengths. It used an iterative algorithm which strongly
  4232. * favored lengths that were a multiple of 4 or 8.
  4233. *
  4234. * Instead of iterating over individual inputs, we use a set of single shot
  4235. * functions which piece together a range of lengths and operate in constant time.
  4236. *
  4237. * Additionally, the number of multiplies has been significantly reduced. This
  4238. * reduces latency, especially when emulating 64-bit multiplies on 32-bit.
  4239. *
  4240. * Depending on the platform, this may or may not be faster than XXH32, but it
  4241. * is almost guaranteed to be faster than XXH64.
  4242. */
  4243. /*
  4244. * At very short lengths, there isn't enough input to fully hide secrets, or use
  4245. * the entire secret.
  4246. *
  4247. * There is also only a limited amount of mixing we can do before significantly
  4248. * impacting performance.
  4249. *
  4250. * Therefore, we use different sections of the secret and always mix two secret
  4251. * samples with an XOR. This should have no effect on performance on the
  4252. * seedless or withSeed variants because everything _should_ be constant folded
  4253. * by modern compilers.
  4254. *
  4255. * The XOR mixing hides individual parts of the secret and increases entropy.
  4256. *
  4257. * This adds an extra layer of strength for custom secrets.
  4258. */
  4259. XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
  4260. XXH3_len_1to3_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  4261. {
  4262. XXH_ASSERT(input != NULL);
  4263. XXH_ASSERT(1 <= len && len <= 3);
  4264. XXH_ASSERT(secret != NULL);
  4265. /*
  4266. * len = 1: combined = { input[0], 0x01, input[0], input[0] }
  4267. * len = 2: combined = { input[1], 0x02, input[0], input[1] }
  4268. * len = 3: combined = { input[2], 0x03, input[0], input[1] }
  4269. */
  4270. { xxh_u8 const c1 = input[0];
  4271. xxh_u8 const c2 = input[len >> 1];
  4272. xxh_u8 const c3 = input[len - 1];
  4273. xxh_u32 const combined = ((xxh_u32)c1 << 16) | ((xxh_u32)c2 << 24)
  4274. | ((xxh_u32)c3 << 0) | ((xxh_u32)len << 8);
  4275. xxh_u64 const bitflip = (XXH_readLE32(secret) ^ XXH_readLE32(secret+4)) + seed;
  4276. xxh_u64 const keyed = (xxh_u64)combined ^ bitflip;
  4277. return XXH64_avalanche(keyed);
  4278. }
  4279. }
  4280. XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
  4281. XXH3_len_4to8_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  4282. {
  4283. XXH_ASSERT(input != NULL);
  4284. XXH_ASSERT(secret != NULL);
  4285. XXH_ASSERT(4 <= len && len <= 8);
  4286. seed ^= (xxh_u64)XXH_swap32((xxh_u32)seed) << 32;
  4287. { xxh_u32 const input1 = XXH_readLE32(input);
  4288. xxh_u32 const input2 = XXH_readLE32(input + len - 4);
  4289. xxh_u64 const bitflip = (XXH_readLE64(secret+8) ^ XXH_readLE64(secret+16)) - seed;
  4290. xxh_u64 const input64 = input2 + (((xxh_u64)input1) << 32);
  4291. xxh_u64 const keyed = input64 ^ bitflip;
  4292. return XXH3_rrmxmx(keyed, len);
  4293. }
  4294. }
  4295. XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
  4296. XXH3_len_9to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  4297. {
  4298. XXH_ASSERT(input != NULL);
  4299. XXH_ASSERT(secret != NULL);
  4300. XXH_ASSERT(9 <= len && len <= 16);
  4301. { xxh_u64 const bitflip1 = (XXH_readLE64(secret+24) ^ XXH_readLE64(secret+32)) + seed;
  4302. xxh_u64 const bitflip2 = (XXH_readLE64(secret+40) ^ XXH_readLE64(secret+48)) - seed;
  4303. xxh_u64 const input_lo = XXH_readLE64(input) ^ bitflip1;
  4304. xxh_u64 const input_hi = XXH_readLE64(input + len - 8) ^ bitflip2;
  4305. xxh_u64 const acc = len
  4306. + XXH_swap64(input_lo) + input_hi
  4307. + XXH3_mul128_fold64(input_lo, input_hi);
  4308. return XXH3_avalanche(acc);
  4309. }
  4310. }
  4311. XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
  4312. XXH3_len_0to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  4313. {
  4314. XXH_ASSERT(len <= 16);
  4315. { if (XXH_likely(len > 8)) return XXH3_len_9to16_64b(input, len, secret, seed);
  4316. if (XXH_likely(len >= 4)) return XXH3_len_4to8_64b(input, len, secret, seed);
  4317. if (len) return XXH3_len_1to3_64b(input, len, secret, seed);
  4318. return XXH64_avalanche(seed ^ (XXH_readLE64(secret+56) ^ XXH_readLE64(secret+64)));
  4319. }
  4320. }
  4321. /*
  4322. * DISCLAIMER: There are known *seed-dependent* multicollisions here due to
  4323. * multiplication by zero, affecting hashes of lengths 17 to 240.
  4324. *
  4325. * However, they are very unlikely.
  4326. *
  4327. * Keep this in mind when using the unseeded XXH3_64bits() variant: As with all
  4328. * unseeded non-cryptographic hashes, it does not attempt to defend itself
  4329. * against specially crafted inputs, only random inputs.
  4330. *
  4331. * Compared to classic UMAC where a 1 in 2^31 chance of 4 consecutive bytes
  4332. * cancelling out the secret is taken an arbitrary number of times (addressed
  4333. * in XXH3_accumulate_512), this collision is very unlikely with random inputs
  4334. * and/or proper seeding:
  4335. *
  4336. * This only has a 1 in 2^63 chance of 8 consecutive bytes cancelling out, in a
  4337. * function that is only called up to 16 times per hash with up to 240 bytes of
  4338. * input.
  4339. *
  4340. * This is not too bad for a non-cryptographic hash function, especially with
  4341. * only 64 bit outputs.
  4342. *
  4343. * The 128-bit variant (which trades some speed for strength) is NOT affected
  4344. * by this, although it is always a good idea to use a proper seed if you care
  4345. * about strength.
  4346. */
  4347. XXH_FORCE_INLINE xxh_u64 XXH3_mix16B(const xxh_u8* XXH_RESTRICT input,
  4348. const xxh_u8* XXH_RESTRICT secret, xxh_u64 seed64)
  4349. {
  4350. #if defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
  4351. && defined(__i386__) && defined(__SSE2__) /* x86 + SSE2 */ \
  4352. && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable like XXH32 hack */
  4353. /*
  4354. * UGLY HACK:
  4355. * GCC for x86 tends to autovectorize the 128-bit multiply, resulting in
  4356. * slower code.
  4357. *
  4358. * By forcing seed64 into a register, we disrupt the cost model and
  4359. * cause it to scalarize. See `XXH32_round()`
  4360. *
  4361. * FIXME: Clang's output is still _much_ faster -- On an AMD Ryzen 3600,
  4362. * XXH3_64bits @ len=240 runs at 4.6 GB/s with Clang 9, but 3.3 GB/s on
  4363. * GCC 9.2, despite both emitting scalar code.
  4364. *
  4365. * GCC generates much better scalar code than Clang for the rest of XXH3,
  4366. * which is why finding a more optimal codepath is an interest.
  4367. */
  4368. XXH_COMPILER_GUARD(seed64);
  4369. #endif
  4370. { xxh_u64 const input_lo = XXH_readLE64(input);
  4371. xxh_u64 const input_hi = XXH_readLE64(input+8);
  4372. return XXH3_mul128_fold64(
  4373. input_lo ^ (XXH_readLE64(secret) + seed64),
  4374. input_hi ^ (XXH_readLE64(secret+8) - seed64)
  4375. );
  4376. }
  4377. }
  4378. /* For mid range keys, XXH3 uses a Mum-hash variant. */
  4379. XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
  4380. XXH3_len_17to128_64b(const xxh_u8* XXH_RESTRICT input, size_t len,
  4381. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  4382. XXH64_hash_t seed)
  4383. {
  4384. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
  4385. XXH_ASSERT(16 < len && len <= 128);
  4386. { xxh_u64 acc = len * XXH_PRIME64_1;
  4387. #if XXH_SIZE_OPT >= 1
  4388. /* Smaller and cleaner, but slightly slower. */
  4389. unsigned int i = (unsigned int)(len - 1) / 32;
  4390. do {
  4391. acc += XXH3_mix16B(input+16 * i, secret+32*i, seed);
  4392. acc += XXH3_mix16B(input+len-16*(i+1), secret+32*i+16, seed);
  4393. } while (i-- != 0);
  4394. #else
  4395. if (len > 32) {
  4396. if (len > 64) {
  4397. if (len > 96) {
  4398. acc += XXH3_mix16B(input+48, secret+96, seed);
  4399. acc += XXH3_mix16B(input+len-64, secret+112, seed);
  4400. }
  4401. acc += XXH3_mix16B(input+32, secret+64, seed);
  4402. acc += XXH3_mix16B(input+len-48, secret+80, seed);
  4403. }
  4404. acc += XXH3_mix16B(input+16, secret+32, seed);
  4405. acc += XXH3_mix16B(input+len-32, secret+48, seed);
  4406. }
  4407. acc += XXH3_mix16B(input+0, secret+0, seed);
  4408. acc += XXH3_mix16B(input+len-16, secret+16, seed);
  4409. #endif
  4410. return XXH3_avalanche(acc);
  4411. }
  4412. }
  4413. XXH_NO_INLINE XXH_PUREF XXH64_hash_t
  4414. XXH3_len_129to240_64b(const xxh_u8* XXH_RESTRICT input, size_t len,
  4415. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  4416. XXH64_hash_t seed)
  4417. {
  4418. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
  4419. XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
  4420. #define XXH3_MIDSIZE_STARTOFFSET 3
  4421. #define XXH3_MIDSIZE_LASTOFFSET 17
  4422. { xxh_u64 acc = len * XXH_PRIME64_1;
  4423. xxh_u64 acc_end;
  4424. unsigned int const nbRounds = (unsigned int)len / 16;
  4425. unsigned int i;
  4426. XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
  4427. for (i=0; i<8; i++) {
  4428. acc += XXH3_mix16B(input+(16*i), secret+(16*i), seed);
  4429. }
  4430. /* last bytes */
  4431. acc_end = XXH3_mix16B(input + len - 16, secret + XXH3_SECRET_SIZE_MIN - XXH3_MIDSIZE_LASTOFFSET, seed);
  4432. XXH_ASSERT(nbRounds >= 8);
  4433. acc = XXH3_avalanche(acc);
  4434. #if defined(__clang__) /* Clang */ \
  4435. && (defined(__ARM_NEON) || defined(__ARM_NEON__)) /* NEON */ \
  4436. && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable */
  4437. /*
  4438. * UGLY HACK:
  4439. * Clang for ARMv7-A tries to vectorize this loop, similar to GCC x86.
  4440. * In everywhere else, it uses scalar code.
  4441. *
  4442. * For 64->128-bit multiplies, even if the NEON was 100% optimal, it
  4443. * would still be slower than UMAAL (see XXH_mult64to128).
  4444. *
  4445. * Unfortunately, Clang doesn't handle the long multiplies properly and
  4446. * converts them to the nonexistent "vmulq_u64" intrinsic, which is then
  4447. * scalarized into an ugly mess of VMOV.32 instructions.
  4448. *
  4449. * This mess is difficult to avoid without turning autovectorization
  4450. * off completely, but they are usually relatively minor and/or not
  4451. * worth it to fix.
  4452. *
  4453. * This loop is the easiest to fix, as unlike XXH32, this pragma
  4454. * _actually works_ because it is a loop vectorization instead of an
  4455. * SLP vectorization.
  4456. */
  4457. #pragma clang loop vectorize(disable)
  4458. #endif
  4459. for (i=8 ; i < nbRounds; i++) {
  4460. /*
  4461. * Prevents clang for unrolling the acc loop and interleaving with this one.
  4462. */
  4463. XXH_COMPILER_GUARD(acc);
  4464. acc_end += XXH3_mix16B(input+(16*i), secret+(16*(i-8)) + XXH3_MIDSIZE_STARTOFFSET, seed);
  4465. }
  4466. return XXH3_avalanche(acc + acc_end);
  4467. }
  4468. }
  4469. /* ======= Long Keys ======= */
  4470. #define XXH_STRIPE_LEN 64
  4471. #define XXH_SECRET_CONSUME_RATE 8 /* nb of secret bytes consumed at each accumulation */
  4472. #define XXH_ACC_NB (XXH_STRIPE_LEN / sizeof(xxh_u64))
  4473. #ifdef XXH_OLD_NAMES
  4474. # define STRIPE_LEN XXH_STRIPE_LEN
  4475. # define ACC_NB XXH_ACC_NB
  4476. #endif
  4477. #ifndef XXH_PREFETCH_DIST
  4478. # ifdef __clang__
  4479. # define XXH_PREFETCH_DIST 320
  4480. # else
  4481. # if (XXH_VECTOR == XXH_AVX512)
  4482. # define XXH_PREFETCH_DIST 512
  4483. # else
  4484. # define XXH_PREFETCH_DIST 384
  4485. # endif
  4486. # endif /* __clang__ */
  4487. #endif /* XXH_PREFETCH_DIST */
  4488. /*
  4489. * These macros are to generate an XXH3_accumulate() function.
  4490. * The two arguments select the name suffix and target attribute.
  4491. *
  4492. * The name of this symbol is XXH3_accumulate_<name>() and it calls
  4493. * XXH3_accumulate_512_<name>().
  4494. *
  4495. * It may be useful to hand implement this function if the compiler fails to
  4496. * optimize the inline function.
  4497. */
  4498. #define XXH3_ACCUMULATE_TEMPLATE(name) \
  4499. void \
  4500. XXH3_accumulate_##name(xxh_u64* XXH_RESTRICT acc, \
  4501. const xxh_u8* XXH_RESTRICT input, \
  4502. const xxh_u8* XXH_RESTRICT secret, \
  4503. size_t nbStripes) \
  4504. { \
  4505. size_t n; \
  4506. for (n = 0; n < nbStripes; n++ ) { \
  4507. const xxh_u8* const in = input + n*XXH_STRIPE_LEN; \
  4508. XXH_PREFETCH(in + XXH_PREFETCH_DIST); \
  4509. XXH3_accumulate_512_##name( \
  4510. acc, \
  4511. in, \
  4512. secret + n*XXH_SECRET_CONSUME_RATE); \
  4513. } \
  4514. }
  4515. XXH_FORCE_INLINE void XXH_writeLE64(void* dst, xxh_u64 v64)
  4516. {
  4517. if (!XXH_CPU_LITTLE_ENDIAN) v64 = XXH_swap64(v64);
  4518. XXH_memcpy(dst, &v64, sizeof(v64));
  4519. }
  4520. /* Several intrinsic functions below are supposed to accept __int64 as argument,
  4521. * as documented in https://software.intel.com/sites/landingpage/IntrinsicsGuide/ .
  4522. * However, several environments do not define __int64 type,
  4523. * requiring a workaround.
  4524. */
  4525. #if !defined (__VMS) \
  4526. && (defined (__cplusplus) \
  4527. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  4528. typedef int64_t xxh_i64;
  4529. #else
  4530. /* the following type must have a width of 64-bit */
  4531. typedef long long xxh_i64;
  4532. #endif
  4533. /*
  4534. * XXH3_accumulate_512 is the tightest loop for long inputs, and it is the most optimized.
  4535. *
  4536. * It is a hardened version of UMAC, based off of FARSH's implementation.
  4537. *
  4538. * This was chosen because it adapts quite well to 32-bit, 64-bit, and SIMD
  4539. * implementations, and it is ridiculously fast.
  4540. *
  4541. * We harden it by mixing the original input to the accumulators as well as the product.
  4542. *
  4543. * This means that in the (relatively likely) case of a multiply by zero, the
  4544. * original input is preserved.
  4545. *
  4546. * On 128-bit inputs, we swap 64-bit pairs when we add the input to improve
  4547. * cross-pollination, as otherwise the upper and lower halves would be
  4548. * essentially independent.
  4549. *
  4550. * This doesn't matter on 64-bit hashes since they all get merged together in
  4551. * the end, so we skip the extra step.
  4552. *
  4553. * Both XXH3_64bits and XXH3_128bits use this subroutine.
  4554. */
  4555. #if (XXH_VECTOR == XXH_AVX512) \
  4556. || (defined(XXH_DISPATCH_AVX512) && XXH_DISPATCH_AVX512 != 0)
  4557. #ifndef XXH_TARGET_AVX512
  4558. # define XXH_TARGET_AVX512 /* disable attribute target */
  4559. #endif
  4560. XXH_FORCE_INLINE XXH_TARGET_AVX512 void
  4561. XXH3_accumulate_512_avx512(void* XXH_RESTRICT acc,
  4562. const void* XXH_RESTRICT input,
  4563. const void* XXH_RESTRICT secret)
  4564. {
  4565. __m512i* const xacc = (__m512i *) acc;
  4566. XXH_ASSERT((((size_t)acc) & 63) == 0);
  4567. XXH_STATIC_ASSERT(XXH_STRIPE_LEN == sizeof(__m512i));
  4568. {
  4569. /* data_vec = input[0]; */
  4570. __m512i const data_vec = _mm512_loadu_si512 (input);
  4571. /* key_vec = secret[0]; */
  4572. __m512i const key_vec = _mm512_loadu_si512 (secret);
  4573. /* data_key = data_vec ^ key_vec; */
  4574. __m512i const data_key = _mm512_xor_si512 (data_vec, key_vec);
  4575. /* data_key_lo = data_key >> 32; */
  4576. __m512i const data_key_lo = _mm512_srli_epi64 (data_key, 32);
  4577. /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
  4578. __m512i const product = _mm512_mul_epu32 (data_key, data_key_lo);
  4579. /* xacc[0] += swap(data_vec); */
  4580. __m512i const data_swap = _mm512_shuffle_epi32(data_vec, (_MM_PERM_ENUM)_MM_SHUFFLE(1, 0, 3, 2));
  4581. __m512i const sum = _mm512_add_epi64(*xacc, data_swap);
  4582. /* xacc[0] += product; */
  4583. *xacc = _mm512_add_epi64(product, sum);
  4584. }
  4585. }
  4586. XXH_FORCE_INLINE XXH_TARGET_AVX512 XXH3_ACCUMULATE_TEMPLATE(avx512)
  4587. /*
  4588. * XXH3_scrambleAcc: Scrambles the accumulators to improve mixing.
  4589. *
  4590. * Multiplication isn't perfect, as explained by Google in HighwayHash:
  4591. *
  4592. * // Multiplication mixes/scrambles bytes 0-7 of the 64-bit result to
  4593. * // varying degrees. In descending order of goodness, bytes
  4594. * // 3 4 2 5 1 6 0 7 have quality 228 224 164 160 100 96 36 32.
  4595. * // As expected, the upper and lower bytes are much worse.
  4596. *
  4597. * Source: https://github.com/google/highwayhash/blob/0aaf66b/highwayhash/hh_avx2.h#L291
  4598. *
  4599. * Since our algorithm uses a pseudorandom secret to add some variance into the
  4600. * mix, we don't need to (or want to) mix as often or as much as HighwayHash does.
  4601. *
  4602. * This isn't as tight as XXH3_accumulate, but still written in SIMD to avoid
  4603. * extraction.
  4604. *
  4605. * Both XXH3_64bits and XXH3_128bits use this subroutine.
  4606. */
  4607. XXH_FORCE_INLINE XXH_TARGET_AVX512 void
  4608. XXH3_scrambleAcc_avx512(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  4609. {
  4610. XXH_ASSERT((((size_t)acc) & 63) == 0);
  4611. XXH_STATIC_ASSERT(XXH_STRIPE_LEN == sizeof(__m512i));
  4612. { __m512i* const xacc = (__m512i*) acc;
  4613. const __m512i prime32 = _mm512_set1_epi32((int)XXH_PRIME32_1);
  4614. /* xacc[0] ^= (xacc[0] >> 47) */
  4615. __m512i const acc_vec = *xacc;
  4616. __m512i const shifted = _mm512_srli_epi64 (acc_vec, 47);
  4617. /* xacc[0] ^= secret; */
  4618. __m512i const key_vec = _mm512_loadu_si512 (secret);
  4619. __m512i const data_key = _mm512_ternarylogic_epi32(key_vec, acc_vec, shifted, 0x96 /* key_vec ^ acc_vec ^ shifted */);
  4620. /* xacc[0] *= XXH_PRIME32_1; */
  4621. __m512i const data_key_hi = _mm512_srli_epi64 (data_key, 32);
  4622. __m512i const prod_lo = _mm512_mul_epu32 (data_key, prime32);
  4623. __m512i const prod_hi = _mm512_mul_epu32 (data_key_hi, prime32);
  4624. *xacc = _mm512_add_epi64(prod_lo, _mm512_slli_epi64(prod_hi, 32));
  4625. }
  4626. }
  4627. XXH_FORCE_INLINE XXH_TARGET_AVX512 void
  4628. XXH3_initCustomSecret_avx512(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
  4629. {
  4630. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 63) == 0);
  4631. XXH_STATIC_ASSERT(XXH_SEC_ALIGN == 64);
  4632. XXH_ASSERT(((size_t)customSecret & 63) == 0);
  4633. (void)(&XXH_writeLE64);
  4634. { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / sizeof(__m512i);
  4635. __m512i const seed_pos = _mm512_set1_epi64((xxh_i64)seed64);
  4636. __m512i const seed = _mm512_mask_sub_epi64(seed_pos, 0xAA, _mm512_set1_epi8(0), seed_pos);
  4637. const __m512i* const src = (const __m512i*) ((const void*) XXH3_kSecret);
  4638. __m512i* const dest = ( __m512i*) customSecret;
  4639. int i;
  4640. XXH_ASSERT(((size_t)src & 63) == 0); /* control alignment */
  4641. XXH_ASSERT(((size_t)dest & 63) == 0);
  4642. for (i=0; i < nbRounds; ++i) {
  4643. dest[i] = _mm512_add_epi64(_mm512_load_si512(src + i), seed);
  4644. } }
  4645. }
  4646. #endif
  4647. #if (XXH_VECTOR == XXH_AVX2) \
  4648. || (defined(XXH_DISPATCH_AVX2) && XXH_DISPATCH_AVX2 != 0)
  4649. #ifndef XXH_TARGET_AVX2
  4650. # define XXH_TARGET_AVX2 /* disable attribute target */
  4651. #endif
  4652. XXH_FORCE_INLINE XXH_TARGET_AVX2 void
  4653. XXH3_accumulate_512_avx2( void* XXH_RESTRICT acc,
  4654. const void* XXH_RESTRICT input,
  4655. const void* XXH_RESTRICT secret)
  4656. {
  4657. XXH_ASSERT((((size_t)acc) & 31) == 0);
  4658. { __m256i* const xacc = (__m256i *) acc;
  4659. /* Unaligned. This is mainly for pointer arithmetic, and because
  4660. * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
  4661. const __m256i* const xinput = (const __m256i *) input;
  4662. /* Unaligned. This is mainly for pointer arithmetic, and because
  4663. * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
  4664. const __m256i* const xsecret = (const __m256i *) secret;
  4665. size_t i;
  4666. for (i=0; i < XXH_STRIPE_LEN/sizeof(__m256i); i++) {
  4667. /* data_vec = xinput[i]; */
  4668. __m256i const data_vec = _mm256_loadu_si256 (xinput+i);
  4669. /* key_vec = xsecret[i]; */
  4670. __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
  4671. /* data_key = data_vec ^ key_vec; */
  4672. __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
  4673. /* data_key_lo = data_key >> 32; */
  4674. __m256i const data_key_lo = _mm256_srli_epi64 (data_key, 32);
  4675. /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
  4676. __m256i const product = _mm256_mul_epu32 (data_key, data_key_lo);
  4677. /* xacc[i] += swap(data_vec); */
  4678. __m256i const data_swap = _mm256_shuffle_epi32(data_vec, _MM_SHUFFLE(1, 0, 3, 2));
  4679. __m256i const sum = _mm256_add_epi64(xacc[i], data_swap);
  4680. /* xacc[i] += product; */
  4681. xacc[i] = _mm256_add_epi64(product, sum);
  4682. } }
  4683. }
  4684. XXH_FORCE_INLINE XXH_TARGET_AVX2 XXH3_ACCUMULATE_TEMPLATE(avx2)
  4685. XXH_FORCE_INLINE XXH_TARGET_AVX2 void
  4686. XXH3_scrambleAcc_avx2(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  4687. {
  4688. XXH_ASSERT((((size_t)acc) & 31) == 0);
  4689. { __m256i* const xacc = (__m256i*) acc;
  4690. /* Unaligned. This is mainly for pointer arithmetic, and because
  4691. * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
  4692. const __m256i* const xsecret = (const __m256i *) secret;
  4693. const __m256i prime32 = _mm256_set1_epi32((int)XXH_PRIME32_1);
  4694. size_t i;
  4695. for (i=0; i < XXH_STRIPE_LEN/sizeof(__m256i); i++) {
  4696. /* xacc[i] ^= (xacc[i] >> 47) */
  4697. __m256i const acc_vec = xacc[i];
  4698. __m256i const shifted = _mm256_srli_epi64 (acc_vec, 47);
  4699. __m256i const data_vec = _mm256_xor_si256 (acc_vec, shifted);
  4700. /* xacc[i] ^= xsecret; */
  4701. __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
  4702. __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
  4703. /* xacc[i] *= XXH_PRIME32_1; */
  4704. __m256i const data_key_hi = _mm256_srli_epi64 (data_key, 32);
  4705. __m256i const prod_lo = _mm256_mul_epu32 (data_key, prime32);
  4706. __m256i const prod_hi = _mm256_mul_epu32 (data_key_hi, prime32);
  4707. xacc[i] = _mm256_add_epi64(prod_lo, _mm256_slli_epi64(prod_hi, 32));
  4708. }
  4709. }
  4710. }
  4711. XXH_FORCE_INLINE XXH_TARGET_AVX2 void XXH3_initCustomSecret_avx2(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
  4712. {
  4713. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 31) == 0);
  4714. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE / sizeof(__m256i)) == 6);
  4715. XXH_STATIC_ASSERT(XXH_SEC_ALIGN <= 64);
  4716. (void)(&XXH_writeLE64);
  4717. XXH_PREFETCH(customSecret);
  4718. { __m256i const seed = _mm256_set_epi64x((xxh_i64)(0U - seed64), (xxh_i64)seed64, (xxh_i64)(0U - seed64), (xxh_i64)seed64);
  4719. const __m256i* const src = (const __m256i*) ((const void*) XXH3_kSecret);
  4720. __m256i* dest = ( __m256i*) customSecret;
  4721. # if defined(__GNUC__) || defined(__clang__)
  4722. /*
  4723. * On GCC & Clang, marking 'dest' as modified will cause the compiler:
  4724. * - do not extract the secret from sse registers in the internal loop
  4725. * - use less common registers, and avoid pushing these reg into stack
  4726. */
  4727. XXH_COMPILER_GUARD(dest);
  4728. # endif
  4729. XXH_ASSERT(((size_t)src & 31) == 0); /* control alignment */
  4730. XXH_ASSERT(((size_t)dest & 31) == 0);
  4731. /* GCC -O2 need unroll loop manually */
  4732. dest[0] = _mm256_add_epi64(_mm256_load_si256(src+0), seed);
  4733. dest[1] = _mm256_add_epi64(_mm256_load_si256(src+1), seed);
  4734. dest[2] = _mm256_add_epi64(_mm256_load_si256(src+2), seed);
  4735. dest[3] = _mm256_add_epi64(_mm256_load_si256(src+3), seed);
  4736. dest[4] = _mm256_add_epi64(_mm256_load_si256(src+4), seed);
  4737. dest[5] = _mm256_add_epi64(_mm256_load_si256(src+5), seed);
  4738. }
  4739. }
  4740. #endif
  4741. /* x86dispatch always generates SSE2 */
  4742. #if (XXH_VECTOR == XXH_SSE2) || defined(XXH_X86DISPATCH)
  4743. #ifndef XXH_TARGET_SSE2
  4744. # define XXH_TARGET_SSE2 /* disable attribute target */
  4745. #endif
  4746. XXH_FORCE_INLINE XXH_TARGET_SSE2 void
  4747. XXH3_accumulate_512_sse2( void* XXH_RESTRICT acc,
  4748. const void* XXH_RESTRICT input,
  4749. const void* XXH_RESTRICT secret)
  4750. {
  4751. /* SSE2 is just a half-scale version of the AVX2 version. */
  4752. XXH_ASSERT((((size_t)acc) & 15) == 0);
  4753. { __m128i* const xacc = (__m128i *) acc;
  4754. /* Unaligned. This is mainly for pointer arithmetic, and because
  4755. * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
  4756. const __m128i* const xinput = (const __m128i *) input;
  4757. /* Unaligned. This is mainly for pointer arithmetic, and because
  4758. * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
  4759. const __m128i* const xsecret = (const __m128i *) secret;
  4760. size_t i;
  4761. for (i=0; i < XXH_STRIPE_LEN/sizeof(__m128i); i++) {
  4762. /* data_vec = xinput[i]; */
  4763. __m128i const data_vec = _mm_loadu_si128 (xinput+i);
  4764. /* key_vec = xsecret[i]; */
  4765. __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
  4766. /* data_key = data_vec ^ key_vec; */
  4767. __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
  4768. /* data_key_lo = data_key >> 32; */
  4769. __m128i const data_key_lo = _mm_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
  4770. /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
  4771. __m128i const product = _mm_mul_epu32 (data_key, data_key_lo);
  4772. /* xacc[i] += swap(data_vec); */
  4773. __m128i const data_swap = _mm_shuffle_epi32(data_vec, _MM_SHUFFLE(1,0,3,2));
  4774. __m128i const sum = _mm_add_epi64(xacc[i], data_swap);
  4775. /* xacc[i] += product; */
  4776. xacc[i] = _mm_add_epi64(product, sum);
  4777. } }
  4778. }
  4779. XXH_FORCE_INLINE XXH_TARGET_SSE2 XXH3_ACCUMULATE_TEMPLATE(sse2)
  4780. XXH_FORCE_INLINE XXH_TARGET_SSE2 void
  4781. XXH3_scrambleAcc_sse2(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  4782. {
  4783. XXH_ASSERT((((size_t)acc) & 15) == 0);
  4784. { __m128i* const xacc = (__m128i*) acc;
  4785. /* Unaligned. This is mainly for pointer arithmetic, and because
  4786. * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
  4787. const __m128i* const xsecret = (const __m128i *) secret;
  4788. const __m128i prime32 = _mm_set1_epi32((int)XXH_PRIME32_1);
  4789. size_t i;
  4790. for (i=0; i < XXH_STRIPE_LEN/sizeof(__m128i); i++) {
  4791. /* xacc[i] ^= (xacc[i] >> 47) */
  4792. __m128i const acc_vec = xacc[i];
  4793. __m128i const shifted = _mm_srli_epi64 (acc_vec, 47);
  4794. __m128i const data_vec = _mm_xor_si128 (acc_vec, shifted);
  4795. /* xacc[i] ^= xsecret[i]; */
  4796. __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
  4797. __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
  4798. /* xacc[i] *= XXH_PRIME32_1; */
  4799. __m128i const data_key_hi = _mm_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
  4800. __m128i const prod_lo = _mm_mul_epu32 (data_key, prime32);
  4801. __m128i const prod_hi = _mm_mul_epu32 (data_key_hi, prime32);
  4802. xacc[i] = _mm_add_epi64(prod_lo, _mm_slli_epi64(prod_hi, 32));
  4803. }
  4804. }
  4805. }
  4806. XXH_FORCE_INLINE XXH_TARGET_SSE2 void XXH3_initCustomSecret_sse2(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
  4807. {
  4808. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 15) == 0);
  4809. (void)(&XXH_writeLE64);
  4810. { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / sizeof(__m128i);
  4811. # if defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER < 1900
  4812. /* MSVC 32bit mode does not support _mm_set_epi64x before 2015 */
  4813. XXH_ALIGN(16) const xxh_i64 seed64x2[2] = { (xxh_i64)seed64, (xxh_i64)(0U - seed64) };
  4814. __m128i const seed = _mm_load_si128((__m128i const*)seed64x2);
  4815. # else
  4816. __m128i const seed = _mm_set_epi64x((xxh_i64)(0U - seed64), (xxh_i64)seed64);
  4817. # endif
  4818. int i;
  4819. const void* const src16 = XXH3_kSecret;
  4820. __m128i* dst16 = (__m128i*) customSecret;
  4821. # if defined(__GNUC__) || defined(__clang__)
  4822. /*
  4823. * On GCC & Clang, marking 'dest' as modified will cause the compiler:
  4824. * - do not extract the secret from sse registers in the internal loop
  4825. * - use less common registers, and avoid pushing these reg into stack
  4826. */
  4827. XXH_COMPILER_GUARD(dst16);
  4828. # endif
  4829. XXH_ASSERT(((size_t)src16 & 15) == 0); /* control alignment */
  4830. XXH_ASSERT(((size_t)dst16 & 15) == 0);
  4831. for (i=0; i < nbRounds; ++i) {
  4832. dst16[i] = _mm_add_epi64(_mm_load_si128((const __m128i *)src16+i), seed);
  4833. } }
  4834. }
  4835. #endif
  4836. #if (XXH_VECTOR == XXH_NEON)
  4837. /* forward declarations for the scalar routines */
  4838. XXH_FORCE_INLINE void
  4839. XXH3_scalarRound(void* XXH_RESTRICT acc, void const* XXH_RESTRICT input,
  4840. void const* XXH_RESTRICT secret, size_t lane);
  4841. XXH_FORCE_INLINE void
  4842. XXH3_scalarScrambleRound(void* XXH_RESTRICT acc,
  4843. void const* XXH_RESTRICT secret, size_t lane);
  4844. /*!
  4845. * @internal
  4846. * @brief The bulk processing loop for NEON and WASM SIMD128.
  4847. *
  4848. * The NEON code path is actually partially scalar when running on AArch64. This
  4849. * is to optimize the pipelining and can have up to 15% speedup depending on the
  4850. * CPU, and it also mitigates some GCC codegen issues.
  4851. *
  4852. * @see XXH3_NEON_LANES for configuring this and details about this optimization.
  4853. *
  4854. * NEON's 32-bit to 64-bit long multiply takes a half vector of 32-bit
  4855. * integers instead of the other platforms which mask full 64-bit vectors,
  4856. * so the setup is more complicated than just shifting right.
  4857. *
  4858. * Additionally, there is an optimization for 4 lanes at once noted below.
  4859. *
  4860. * Since, as stated, the most optimal amount of lanes for Cortexes is 6,
  4861. * there needs to be *three* versions of the accumulate operation used
  4862. * for the remaining 2 lanes.
  4863. *
  4864. * WASM's SIMD128 uses SIMDe's arm_neon.h polyfill because the intrinsics overlap
  4865. * nearly perfectly.
  4866. */
  4867. XXH_FORCE_INLINE void
  4868. XXH3_accumulate_512_neon( void* XXH_RESTRICT acc,
  4869. const void* XXH_RESTRICT input,
  4870. const void* XXH_RESTRICT secret)
  4871. {
  4872. XXH_ASSERT((((size_t)acc) & 15) == 0);
  4873. XXH_STATIC_ASSERT(XXH3_NEON_LANES > 0 && XXH3_NEON_LANES <= XXH_ACC_NB && XXH3_NEON_LANES % 2 == 0);
  4874. { /* GCC for darwin arm64 does not like aliasing here */
  4875. xxh_aliasing_uint64x2_t* const xacc = (xxh_aliasing_uint64x2_t*) acc;
  4876. /* We don't use a uint32x4_t pointer because it causes bus errors on ARMv7. */
  4877. uint8_t const* xinput = (const uint8_t *) input;
  4878. uint8_t const* xsecret = (const uint8_t *) secret;
  4879. size_t i;
  4880. #ifdef __wasm_simd128__
  4881. /*
  4882. * On WASM SIMD128, Clang emits direct address loads when XXH3_kSecret
  4883. * is constant propagated, which results in it converting it to this
  4884. * inside the loop:
  4885. *
  4886. * a = v128.load(XXH3_kSecret + 0 + $secret_offset, offset = 0)
  4887. * b = v128.load(XXH3_kSecret + 16 + $secret_offset, offset = 0)
  4888. * ...
  4889. *
  4890. * This requires a full 32-bit address immediate (and therefore a 6 byte
  4891. * instruction) as well as an add for each offset.
  4892. *
  4893. * Putting an asm guard prevents it from folding (at the cost of losing
  4894. * the alignment hint), and uses the free offset in `v128.load` instead
  4895. * of adding secret_offset each time which overall reduces code size by
  4896. * about a kilobyte and improves performance.
  4897. */
  4898. XXH_COMPILER_GUARD(xsecret);
  4899. #endif
  4900. /* Scalar lanes use the normal scalarRound routine */
  4901. for (i = XXH3_NEON_LANES; i < XXH_ACC_NB; i++) {
  4902. XXH3_scalarRound(acc, input, secret, i);
  4903. }
  4904. i = 0;
  4905. /* 4 NEON lanes at a time. */
  4906. for (; i+1 < XXH3_NEON_LANES / 2; i+=2) {
  4907. /* data_vec = xinput[i]; */
  4908. uint64x2_t data_vec_1 = XXH_vld1q_u64(xinput + (i * 16));
  4909. uint64x2_t data_vec_2 = XXH_vld1q_u64(xinput + ((i+1) * 16));
  4910. /* key_vec = xsecret[i]; */
  4911. uint64x2_t key_vec_1 = XXH_vld1q_u64(xsecret + (i * 16));
  4912. uint64x2_t key_vec_2 = XXH_vld1q_u64(xsecret + ((i+1) * 16));
  4913. /* data_swap = swap(data_vec) */
  4914. uint64x2_t data_swap_1 = vextq_u64(data_vec_1, data_vec_1, 1);
  4915. uint64x2_t data_swap_2 = vextq_u64(data_vec_2, data_vec_2, 1);
  4916. /* data_key = data_vec ^ key_vec; */
  4917. uint64x2_t data_key_1 = veorq_u64(data_vec_1, key_vec_1);
  4918. uint64x2_t data_key_2 = veorq_u64(data_vec_2, key_vec_2);
  4919. /*
  4920. * If we reinterpret the 64x2 vectors as 32x4 vectors, we can use a
  4921. * de-interleave operation for 4 lanes in 1 step with `vuzpq_u32` to
  4922. * get one vector with the low 32 bits of each lane, and one vector
  4923. * with the high 32 bits of each lane.
  4924. *
  4925. * The intrinsic returns a double vector because the original ARMv7-a
  4926. * instruction modified both arguments in place. AArch64 and SIMD128 emit
  4927. * two instructions from this intrinsic.
  4928. *
  4929. * [ dk11L | dk11H | dk12L | dk12H ] -> [ dk11L | dk12L | dk21L | dk22L ]
  4930. * [ dk21L | dk21H | dk22L | dk22H ] -> [ dk11H | dk12H | dk21H | dk22H ]
  4931. */
  4932. uint32x4x2_t unzipped = vuzpq_u32(
  4933. vreinterpretq_u32_u64(data_key_1),
  4934. vreinterpretq_u32_u64(data_key_2)
  4935. );
  4936. /* data_key_lo = data_key & 0xFFFFFFFF */
  4937. uint32x4_t data_key_lo = unzipped.val[0];
  4938. /* data_key_hi = data_key >> 32 */
  4939. uint32x4_t data_key_hi = unzipped.val[1];
  4940. /*
  4941. * Then, we can split the vectors horizontally and multiply which, as for most
  4942. * widening intrinsics, have a variant that works on both high half vectors
  4943. * for free on AArch64. A similar instruction is available on SIMD128.
  4944. *
  4945. * sum = data_swap + (u64x2) data_key_lo * (u64x2) data_key_hi
  4946. */
  4947. uint64x2_t sum_1 = XXH_vmlal_low_u32(data_swap_1, data_key_lo, data_key_hi);
  4948. uint64x2_t sum_2 = XXH_vmlal_high_u32(data_swap_2, data_key_lo, data_key_hi);
  4949. /*
  4950. * Clang reorders
  4951. * a += b * c; // umlal swap.2d, dkl.2s, dkh.2s
  4952. * c += a; // add acc.2d, acc.2d, swap.2d
  4953. * to
  4954. * c += a; // add acc.2d, acc.2d, swap.2d
  4955. * c += b * c; // umlal acc.2d, dkl.2s, dkh.2s
  4956. *
  4957. * While it would make sense in theory since the addition is faster,
  4958. * for reasons likely related to umlal being limited to certain NEON
  4959. * pipelines, this is worse. A compiler guard fixes this.
  4960. */
  4961. XXH_COMPILER_GUARD_CLANG_NEON(sum_1);
  4962. XXH_COMPILER_GUARD_CLANG_NEON(sum_2);
  4963. /* xacc[i] = acc_vec + sum; */
  4964. xacc[i] = vaddq_u64(xacc[i], sum_1);
  4965. xacc[i+1] = vaddq_u64(xacc[i+1], sum_2);
  4966. }
  4967. /* Operate on the remaining NEON lanes 2 at a time. */
  4968. for (; i < XXH3_NEON_LANES / 2; i++) {
  4969. /* data_vec = xinput[i]; */
  4970. uint64x2_t data_vec = XXH_vld1q_u64(xinput + (i * 16));
  4971. /* key_vec = xsecret[i]; */
  4972. uint64x2_t key_vec = XXH_vld1q_u64(xsecret + (i * 16));
  4973. /* acc_vec_2 = swap(data_vec) */
  4974. uint64x2_t data_swap = vextq_u64(data_vec, data_vec, 1);
  4975. /* data_key = data_vec ^ key_vec; */
  4976. uint64x2_t data_key = veorq_u64(data_vec, key_vec);
  4977. /* For two lanes, just use VMOVN and VSHRN. */
  4978. /* data_key_lo = data_key & 0xFFFFFFFF; */
  4979. uint32x2_t data_key_lo = vmovn_u64(data_key);
  4980. /* data_key_hi = data_key >> 32; */
  4981. uint32x2_t data_key_hi = vshrn_n_u64(data_key, 32);
  4982. /* sum = data_swap + (u64x2) data_key_lo * (u64x2) data_key_hi; */
  4983. uint64x2_t sum = vmlal_u32(data_swap, data_key_lo, data_key_hi);
  4984. /* Same Clang workaround as before */
  4985. XXH_COMPILER_GUARD_CLANG_NEON(sum);
  4986. /* xacc[i] = acc_vec + sum; */
  4987. xacc[i] = vaddq_u64 (xacc[i], sum);
  4988. }
  4989. }
  4990. }
  4991. XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(neon)
  4992. XXH_FORCE_INLINE void
  4993. XXH3_scrambleAcc_neon(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  4994. {
  4995. XXH_ASSERT((((size_t)acc) & 15) == 0);
  4996. { xxh_aliasing_uint64x2_t* xacc = (xxh_aliasing_uint64x2_t*) acc;
  4997. uint8_t const* xsecret = (uint8_t const*) secret;
  4998. size_t i;
  4999. /* WASM uses operator overloads and doesn't need these. */
  5000. #ifndef __wasm_simd128__
  5001. /* { prime32_1, prime32_1 } */
  5002. uint32x2_t const kPrimeLo = vdup_n_u32(XXH_PRIME32_1);
  5003. /* { 0, prime32_1, 0, prime32_1 } */
  5004. uint32x4_t const kPrimeHi = vreinterpretq_u32_u64(vdupq_n_u64((xxh_u64)XXH_PRIME32_1 << 32));
  5005. #endif
  5006. /* AArch64 uses both scalar and neon at the same time */
  5007. for (i = XXH3_NEON_LANES; i < XXH_ACC_NB; i++) {
  5008. XXH3_scalarScrambleRound(acc, secret, i);
  5009. }
  5010. for (i=0; i < XXH3_NEON_LANES / 2; i++) {
  5011. /* xacc[i] ^= (xacc[i] >> 47); */
  5012. uint64x2_t acc_vec = xacc[i];
  5013. uint64x2_t shifted = vshrq_n_u64(acc_vec, 47);
  5014. uint64x2_t data_vec = veorq_u64(acc_vec, shifted);
  5015. /* xacc[i] ^= xsecret[i]; */
  5016. uint64x2_t key_vec = XXH_vld1q_u64(xsecret + (i * 16));
  5017. uint64x2_t data_key = veorq_u64(data_vec, key_vec);
  5018. /* xacc[i] *= XXH_PRIME32_1 */
  5019. #ifdef __wasm_simd128__
  5020. /* SIMD128 has multiply by u64x2, use it instead of expanding and scalarizing */
  5021. xacc[i] = data_key * XXH_PRIME32_1;
  5022. #else
  5023. /*
  5024. * Expanded version with portable NEON intrinsics
  5025. *
  5026. * lo(x) * lo(y) + (hi(x) * lo(y) << 32)
  5027. *
  5028. * prod_hi = hi(data_key) * lo(prime) << 32
  5029. *
  5030. * Since we only need 32 bits of this multiply a trick can be used, reinterpreting the vector
  5031. * as a uint32x4_t and multiplying by { 0, prime, 0, prime } to cancel out the unwanted bits
  5032. * and avoid the shift.
  5033. */
  5034. uint32x4_t prod_hi = vmulq_u32 (vreinterpretq_u32_u64(data_key), kPrimeHi);
  5035. /* Extract low bits for vmlal_u32 */
  5036. uint32x2_t data_key_lo = vmovn_u64(data_key);
  5037. /* xacc[i] = prod_hi + lo(data_key) * XXH_PRIME32_1; */
  5038. xacc[i] = vmlal_u32(vreinterpretq_u64_u32(prod_hi), data_key_lo, kPrimeLo);
  5039. #endif
  5040. }
  5041. }
  5042. }
  5043. #endif
  5044. #if (XXH_VECTOR == XXH_VSX)
  5045. XXH_FORCE_INLINE void
  5046. XXH3_accumulate_512_vsx( void* XXH_RESTRICT acc,
  5047. const void* XXH_RESTRICT input,
  5048. const void* XXH_RESTRICT secret)
  5049. {
  5050. /* presumed aligned */
  5051. xxh_aliasing_u64x2* const xacc = (xxh_aliasing_u64x2*) acc;
  5052. xxh_u8 const* const xinput = (xxh_u8 const*) input; /* no alignment restriction */
  5053. xxh_u8 const* const xsecret = (xxh_u8 const*) secret; /* no alignment restriction */
  5054. xxh_u64x2 const v32 = { 32, 32 };
  5055. size_t i;
  5056. for (i = 0; i < XXH_STRIPE_LEN / sizeof(xxh_u64x2); i++) {
  5057. /* data_vec = xinput[i]; */
  5058. xxh_u64x2 const data_vec = XXH_vec_loadu(xinput + 16*i);
  5059. /* key_vec = xsecret[i]; */
  5060. xxh_u64x2 const key_vec = XXH_vec_loadu(xsecret + 16*i);
  5061. xxh_u64x2 const data_key = data_vec ^ key_vec;
  5062. /* shuffled = (data_key << 32) | (data_key >> 32); */
  5063. xxh_u32x4 const shuffled = (xxh_u32x4)vec_rl(data_key, v32);
  5064. /* product = ((xxh_u64x2)data_key & 0xFFFFFFFF) * ((xxh_u64x2)shuffled & 0xFFFFFFFF); */
  5065. xxh_u64x2 const product = XXH_vec_mulo((xxh_u32x4)data_key, shuffled);
  5066. /* acc_vec = xacc[i]; */
  5067. xxh_u64x2 acc_vec = xacc[i];
  5068. acc_vec += product;
  5069. /* swap high and low halves */
  5070. #ifdef __s390x__
  5071. acc_vec += vec_permi(data_vec, data_vec, 2);
  5072. #else
  5073. acc_vec += vec_xxpermdi(data_vec, data_vec, 2);
  5074. #endif
  5075. xacc[i] = acc_vec;
  5076. }
  5077. }
  5078. XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(vsx)
  5079. XXH_FORCE_INLINE void
  5080. XXH3_scrambleAcc_vsx(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  5081. {
  5082. XXH_ASSERT((((size_t)acc) & 15) == 0);
  5083. { xxh_aliasing_u64x2* const xacc = (xxh_aliasing_u64x2*) acc;
  5084. const xxh_u8* const xsecret = (const xxh_u8*) secret;
  5085. /* constants */
  5086. xxh_u64x2 const v32 = { 32, 32 };
  5087. xxh_u64x2 const v47 = { 47, 47 };
  5088. xxh_u32x4 const prime = { XXH_PRIME32_1, XXH_PRIME32_1, XXH_PRIME32_1, XXH_PRIME32_1 };
  5089. size_t i;
  5090. for (i = 0; i < XXH_STRIPE_LEN / sizeof(xxh_u64x2); i++) {
  5091. /* xacc[i] ^= (xacc[i] >> 47); */
  5092. xxh_u64x2 const acc_vec = xacc[i];
  5093. xxh_u64x2 const data_vec = acc_vec ^ (acc_vec >> v47);
  5094. /* xacc[i] ^= xsecret[i]; */
  5095. xxh_u64x2 const key_vec = XXH_vec_loadu(xsecret + 16*i);
  5096. xxh_u64x2 const data_key = data_vec ^ key_vec;
  5097. /* xacc[i] *= XXH_PRIME32_1 */
  5098. /* prod_lo = ((xxh_u64x2)data_key & 0xFFFFFFFF) * ((xxh_u64x2)prime & 0xFFFFFFFF); */
  5099. xxh_u64x2 const prod_even = XXH_vec_mule((xxh_u32x4)data_key, prime);
  5100. /* prod_hi = ((xxh_u64x2)data_key >> 32) * ((xxh_u64x2)prime >> 32); */
  5101. xxh_u64x2 const prod_odd = XXH_vec_mulo((xxh_u32x4)data_key, prime);
  5102. xacc[i] = prod_odd + (prod_even << v32);
  5103. } }
  5104. }
  5105. #endif
  5106. #if (XXH_VECTOR == XXH_SVE)
  5107. XXH_FORCE_INLINE void
  5108. XXH3_accumulate_512_sve( void* XXH_RESTRICT acc,
  5109. const void* XXH_RESTRICT input,
  5110. const void* XXH_RESTRICT secret)
  5111. {
  5112. uint64_t *xacc = (uint64_t *)acc;
  5113. const uint64_t *xinput = (const uint64_t *)(const void *)input;
  5114. const uint64_t *xsecret = (const uint64_t *)(const void *)secret;
  5115. svuint64_t kSwap = sveor_n_u64_z(svptrue_b64(), svindex_u64(0, 1), 1);
  5116. uint64_t element_count = svcntd();
  5117. if (element_count >= 8) {
  5118. svbool_t mask = svptrue_pat_b64(SV_VL8);
  5119. svuint64_t vacc = svld1_u64(mask, xacc);
  5120. ACCRND(vacc, 0);
  5121. svst1_u64(mask, xacc, vacc);
  5122. } else if (element_count == 2) { /* sve128 */
  5123. svbool_t mask = svptrue_pat_b64(SV_VL2);
  5124. svuint64_t acc0 = svld1_u64(mask, xacc + 0);
  5125. svuint64_t acc1 = svld1_u64(mask, xacc + 2);
  5126. svuint64_t acc2 = svld1_u64(mask, xacc + 4);
  5127. svuint64_t acc3 = svld1_u64(mask, xacc + 6);
  5128. ACCRND(acc0, 0);
  5129. ACCRND(acc1, 2);
  5130. ACCRND(acc2, 4);
  5131. ACCRND(acc3, 6);
  5132. svst1_u64(mask, xacc + 0, acc0);
  5133. svst1_u64(mask, xacc + 2, acc1);
  5134. svst1_u64(mask, xacc + 4, acc2);
  5135. svst1_u64(mask, xacc + 6, acc3);
  5136. } else {
  5137. svbool_t mask = svptrue_pat_b64(SV_VL4);
  5138. svuint64_t acc0 = svld1_u64(mask, xacc + 0);
  5139. svuint64_t acc1 = svld1_u64(mask, xacc + 4);
  5140. ACCRND(acc0, 0);
  5141. ACCRND(acc1, 4);
  5142. svst1_u64(mask, xacc + 0, acc0);
  5143. svst1_u64(mask, xacc + 4, acc1);
  5144. }
  5145. }
  5146. XXH_FORCE_INLINE void
  5147. XXH3_accumulate_sve(xxh_u64* XXH_RESTRICT acc,
  5148. const xxh_u8* XXH_RESTRICT input,
  5149. const xxh_u8* XXH_RESTRICT secret,
  5150. size_t nbStripes)
  5151. {
  5152. if (nbStripes != 0) {
  5153. uint64_t *xacc = (uint64_t *)acc;
  5154. const uint64_t *xinput = (const uint64_t *)(const void *)input;
  5155. const uint64_t *xsecret = (const uint64_t *)(const void *)secret;
  5156. svuint64_t kSwap = sveor_n_u64_z(svptrue_b64(), svindex_u64(0, 1), 1);
  5157. uint64_t element_count = svcntd();
  5158. if (element_count >= 8) {
  5159. svbool_t mask = svptrue_pat_b64(SV_VL8);
  5160. svuint64_t vacc = svld1_u64(mask, xacc + 0);
  5161. do {
  5162. /* svprfd(svbool_t, void *, enum svfprop); */
  5163. svprfd(mask, xinput + 128, SV_PLDL1STRM);
  5164. ACCRND(vacc, 0);
  5165. xinput += 8;
  5166. xsecret += 1;
  5167. nbStripes--;
  5168. } while (nbStripes != 0);
  5169. svst1_u64(mask, xacc + 0, vacc);
  5170. } else if (element_count == 2) { /* sve128 */
  5171. svbool_t mask = svptrue_pat_b64(SV_VL2);
  5172. svuint64_t acc0 = svld1_u64(mask, xacc + 0);
  5173. svuint64_t acc1 = svld1_u64(mask, xacc + 2);
  5174. svuint64_t acc2 = svld1_u64(mask, xacc + 4);
  5175. svuint64_t acc3 = svld1_u64(mask, xacc + 6);
  5176. do {
  5177. svprfd(mask, xinput + 128, SV_PLDL1STRM);
  5178. ACCRND(acc0, 0);
  5179. ACCRND(acc1, 2);
  5180. ACCRND(acc2, 4);
  5181. ACCRND(acc3, 6);
  5182. xinput += 8;
  5183. xsecret += 1;
  5184. nbStripes--;
  5185. } while (nbStripes != 0);
  5186. svst1_u64(mask, xacc + 0, acc0);
  5187. svst1_u64(mask, xacc + 2, acc1);
  5188. svst1_u64(mask, xacc + 4, acc2);
  5189. svst1_u64(mask, xacc + 6, acc3);
  5190. } else {
  5191. svbool_t mask = svptrue_pat_b64(SV_VL4);
  5192. svuint64_t acc0 = svld1_u64(mask, xacc + 0);
  5193. svuint64_t acc1 = svld1_u64(mask, xacc + 4);
  5194. do {
  5195. svprfd(mask, xinput + 128, SV_PLDL1STRM);
  5196. ACCRND(acc0, 0);
  5197. ACCRND(acc1, 4);
  5198. xinput += 8;
  5199. xsecret += 1;
  5200. nbStripes--;
  5201. } while (nbStripes != 0);
  5202. svst1_u64(mask, xacc + 0, acc0);
  5203. svst1_u64(mask, xacc + 4, acc1);
  5204. }
  5205. }
  5206. }
  5207. #endif
  5208. #if (XXH_VECTOR == XXH_LSX)
  5209. #define _LSX_SHUFFLE(z, y, x, w) (((z) << 6) | ((y) << 4) | ((x) << 2) | (w))
  5210. XXH_FORCE_INLINE void
  5211. XXH3_accumulate_512_lsx( void* XXH_RESTRICT acc,
  5212. const void* XXH_RESTRICT input,
  5213. const void* XXH_RESTRICT secret)
  5214. {
  5215. XXH_ASSERT((((size_t)acc) & 15) == 0);
  5216. {
  5217. __m128i* const xacc = (__m128i *) acc;
  5218. const __m128i* const xinput = (const __m128i *) input;
  5219. const __m128i* const xsecret = (const __m128i *) secret;
  5220. for (size_t i = 0; i < XXH_STRIPE_LEN / sizeof(__m128i); i++) {
  5221. /* data_vec = xinput[i]; */
  5222. __m128i const data_vec = __lsx_vld(xinput + i, 0);
  5223. /* key_vec = xsecret[i]; */
  5224. __m128i const key_vec = __lsx_vld(xsecret + i, 0);
  5225. /* data_key = data_vec ^ key_vec; */
  5226. __m128i const data_key = __lsx_vxor_v(data_vec, key_vec);
  5227. /* data_key_lo = data_key >> 32; */
  5228. __m128i const data_key_lo = __lsx_vsrli_d(data_key, 32);
  5229. // __m128i const data_key_lo = __lsx_vsrli_d(data_key, 32);
  5230. /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
  5231. __m128i const product = __lsx_vmulwev_d_wu(data_key, data_key_lo);
  5232. /* xacc[i] += swap(data_vec); */
  5233. __m128i const data_swap = __lsx_vshuf4i_w(data_vec, _LSX_SHUFFLE(1, 0, 3, 2));
  5234. __m128i const sum = __lsx_vadd_d(xacc[i], data_swap);
  5235. /* xacc[i] += product; */
  5236. xacc[i] = __lsx_vadd_d(product, sum);
  5237. }
  5238. }
  5239. }
  5240. XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(lsx)
  5241. XXH_FORCE_INLINE void
  5242. XXH3_scrambleAcc_lsx(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  5243. {
  5244. XXH_ASSERT((((size_t)acc) & 15) == 0);
  5245. {
  5246. __m128i* const xacc = (__m128i*) acc;
  5247. const __m128i* const xsecret = (const __m128i *) secret;
  5248. const __m128i prime32 = __lsx_vreplgr2vr_w((int)XXH_PRIME32_1);
  5249. for (size_t i = 0; i < XXH_STRIPE_LEN / sizeof(__m128i); i++) {
  5250. /* xacc[i] ^= (xacc[i] >> 47) */
  5251. __m128i const acc_vec = xacc[i];
  5252. __m128i const shifted = __lsx_vsrli_d(acc_vec, 47);
  5253. __m128i const data_vec = __lsx_vxor_v(acc_vec, shifted);
  5254. /* xacc[i] ^= xsecret[i]; */
  5255. __m128i const key_vec = __lsx_vld(xsecret + i, 0);
  5256. __m128i const data_key = __lsx_vxor_v(data_vec, key_vec);
  5257. /* xacc[i] *= XXH_PRIME32_1; */
  5258. __m128i const data_key_hi = __lsx_vsrli_d(data_key, 32);
  5259. __m128i const prod_lo = __lsx_vmulwev_d_wu(data_key, prime32);
  5260. __m128i const prod_hi = __lsx_vmulwev_d_wu(data_key_hi, prime32);
  5261. xacc[i] = __lsx_vadd_d(prod_lo, __lsx_vslli_d(prod_hi, 32));
  5262. }
  5263. }
  5264. }
  5265. #endif
  5266. /* scalar variants - universal */
  5267. #if defined(__aarch64__) && (defined(__GNUC__) || defined(__clang__))
  5268. /*
  5269. * In XXH3_scalarRound(), GCC and Clang have a similar codegen issue, where they
  5270. * emit an excess mask and a full 64-bit multiply-add (MADD X-form).
  5271. *
  5272. * While this might not seem like much, as AArch64 is a 64-bit architecture, only
  5273. * big Cortex designs have a full 64-bit multiplier.
  5274. *
  5275. * On the little cores, the smaller 32-bit multiplier is used, and full 64-bit
  5276. * multiplies expand to 2-3 multiplies in microcode. This has a major penalty
  5277. * of up to 4 latency cycles and 2 stall cycles in the multiply pipeline.
  5278. *
  5279. * Thankfully, AArch64 still provides the 32-bit long multiply-add (UMADDL) which does
  5280. * not have this penalty and does the mask automatically.
  5281. */
  5282. XXH_FORCE_INLINE xxh_u64
  5283. XXH_mult32to64_add64(xxh_u64 lhs, xxh_u64 rhs, xxh_u64 acc)
  5284. {
  5285. xxh_u64 ret;
  5286. /* note: %x = 64-bit register, %w = 32-bit register */
  5287. __asm__("umaddl %x0, %w1, %w2, %x3" : "=r" (ret) : "r" (lhs), "r" (rhs), "r" (acc));
  5288. return ret;
  5289. }
  5290. #else
  5291. XXH_FORCE_INLINE xxh_u64
  5292. XXH_mult32to64_add64(xxh_u64 lhs, xxh_u64 rhs, xxh_u64 acc)
  5293. {
  5294. return XXH_mult32to64((xxh_u32)lhs, (xxh_u32)rhs) + acc;
  5295. }
  5296. #endif
  5297. /*!
  5298. * @internal
  5299. * @brief Scalar round for @ref XXH3_accumulate_512_scalar().
  5300. *
  5301. * This is extracted to its own function because the NEON path uses a combination
  5302. * of NEON and scalar.
  5303. */
  5304. XXH_FORCE_INLINE void
  5305. XXH3_scalarRound(void* XXH_RESTRICT acc,
  5306. void const* XXH_RESTRICT input,
  5307. void const* XXH_RESTRICT secret,
  5308. size_t lane)
  5309. {
  5310. xxh_u64* xacc = (xxh_u64*) acc;
  5311. xxh_u8 const* xinput = (xxh_u8 const*) input;
  5312. xxh_u8 const* xsecret = (xxh_u8 const*) secret;
  5313. XXH_ASSERT(lane < XXH_ACC_NB);
  5314. XXH_ASSERT(((size_t)acc & (XXH_ACC_ALIGN-1)) == 0);
  5315. {
  5316. xxh_u64 const data_val = XXH_readLE64(xinput + lane * 8);
  5317. xxh_u64 const data_key = data_val ^ XXH_readLE64(xsecret + lane * 8);
  5318. xacc[lane ^ 1] += data_val; /* swap adjacent lanes */
  5319. xacc[lane] = XXH_mult32to64_add64(data_key /* & 0xFFFFFFFF */, data_key >> 32, xacc[lane]);
  5320. }
  5321. }
  5322. /*!
  5323. * @internal
  5324. * @brief Processes a 64 byte block of data using the scalar path.
  5325. */
  5326. XXH_FORCE_INLINE void
  5327. XXH3_accumulate_512_scalar(void* XXH_RESTRICT acc,
  5328. const void* XXH_RESTRICT input,
  5329. const void* XXH_RESTRICT secret)
  5330. {
  5331. size_t i;
  5332. /* ARM GCC refuses to unroll this loop, resulting in a 24% slowdown on ARMv6. */
  5333. #if defined(__GNUC__) && !defined(__clang__) \
  5334. && (defined(__arm__) || defined(__thumb2__)) \
  5335. && defined(__ARM_FEATURE_UNALIGNED) /* no unaligned access just wastes bytes */ \
  5336. && XXH_SIZE_OPT <= 0
  5337. # pragma GCC unroll 8
  5338. #endif
  5339. for (i=0; i < XXH_ACC_NB; i++) {
  5340. XXH3_scalarRound(acc, input, secret, i);
  5341. }
  5342. }
  5343. XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(scalar)
  5344. /*!
  5345. * @internal
  5346. * @brief Scalar scramble step for @ref XXH3_scrambleAcc_scalar().
  5347. *
  5348. * This is extracted to its own function because the NEON path uses a combination
  5349. * of NEON and scalar.
  5350. */
  5351. XXH_FORCE_INLINE void
  5352. XXH3_scalarScrambleRound(void* XXH_RESTRICT acc,
  5353. void const* XXH_RESTRICT secret,
  5354. size_t lane)
  5355. {
  5356. xxh_u64* const xacc = (xxh_u64*) acc; /* presumed aligned */
  5357. const xxh_u8* const xsecret = (const xxh_u8*) secret; /* no alignment restriction */
  5358. XXH_ASSERT((((size_t)acc) & (XXH_ACC_ALIGN-1)) == 0);
  5359. XXH_ASSERT(lane < XXH_ACC_NB);
  5360. {
  5361. xxh_u64 const key64 = XXH_readLE64(xsecret + lane * 8);
  5362. xxh_u64 acc64 = xacc[lane];
  5363. acc64 = XXH_xorshift64(acc64, 47);
  5364. acc64 ^= key64;
  5365. acc64 *= XXH_PRIME32_1;
  5366. xacc[lane] = acc64;
  5367. }
  5368. }
  5369. /*!
  5370. * @internal
  5371. * @brief Scrambles the accumulators after a large chunk has been read
  5372. */
  5373. XXH_FORCE_INLINE void
  5374. XXH3_scrambleAcc_scalar(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
  5375. {
  5376. size_t i;
  5377. for (i=0; i < XXH_ACC_NB; i++) {
  5378. XXH3_scalarScrambleRound(acc, secret, i);
  5379. }
  5380. }
  5381. XXH_FORCE_INLINE void
  5382. XXH3_initCustomSecret_scalar(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
  5383. {
  5384. /*
  5385. * We need a separate pointer for the hack below,
  5386. * which requires a non-const pointer.
  5387. * Any decent compiler will optimize this out otherwise.
  5388. */
  5389. const xxh_u8* kSecretPtr = XXH3_kSecret;
  5390. XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 15) == 0);
  5391. #if defined(__GNUC__) && defined(__aarch64__)
  5392. /*
  5393. * UGLY HACK:
  5394. * GCC and Clang generate a bunch of MOV/MOVK pairs for aarch64, and they are
  5395. * placed sequentially, in order, at the top of the unrolled loop.
  5396. *
  5397. * While MOVK is great for generating constants (2 cycles for a 64-bit
  5398. * constant compared to 4 cycles for LDR), it fights for bandwidth with
  5399. * the arithmetic instructions.
  5400. *
  5401. * I L S
  5402. * MOVK
  5403. * MOVK
  5404. * MOVK
  5405. * MOVK
  5406. * ADD
  5407. * SUB STR
  5408. * STR
  5409. * By forcing loads from memory (as the asm line causes the compiler to assume
  5410. * that XXH3_kSecretPtr has been changed), the pipelines are used more
  5411. * efficiently:
  5412. * I L S
  5413. * LDR
  5414. * ADD LDR
  5415. * SUB STR
  5416. * STR
  5417. *
  5418. * See XXH3_NEON_LANES for details on the pipsline.
  5419. *
  5420. * XXH3_64bits_withSeed, len == 256, Snapdragon 835
  5421. * without hack: 2654.4 MB/s
  5422. * with hack: 3202.9 MB/s
  5423. */
  5424. XXH_COMPILER_GUARD(kSecretPtr);
  5425. #endif
  5426. { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / 16;
  5427. int i;
  5428. for (i=0; i < nbRounds; i++) {
  5429. /*
  5430. * The asm hack causes the compiler to assume that kSecretPtr aliases with
  5431. * customSecret, and on aarch64, this prevented LDP from merging two
  5432. * loads together for free. Putting the loads together before the stores
  5433. * properly generates LDP.
  5434. */
  5435. xxh_u64 lo = XXH_readLE64(kSecretPtr + 16*i) + seed64;
  5436. xxh_u64 hi = XXH_readLE64(kSecretPtr + 16*i + 8) - seed64;
  5437. XXH_writeLE64((xxh_u8*)customSecret + 16*i, lo);
  5438. XXH_writeLE64((xxh_u8*)customSecret + 16*i + 8, hi);
  5439. } }
  5440. }
  5441. typedef void (*XXH3_f_accumulate)(xxh_u64* XXH_RESTRICT, const xxh_u8* XXH_RESTRICT, const xxh_u8* XXH_RESTRICT, size_t);
  5442. typedef void (*XXH3_f_scrambleAcc)(void* XXH_RESTRICT, const void*);
  5443. typedef void (*XXH3_f_initCustomSecret)(void* XXH_RESTRICT, xxh_u64);
  5444. #if (XXH_VECTOR == XXH_AVX512)
  5445. #define XXH3_accumulate_512 XXH3_accumulate_512_avx512
  5446. #define XXH3_accumulate XXH3_accumulate_avx512
  5447. #define XXH3_scrambleAcc XXH3_scrambleAcc_avx512
  5448. #define XXH3_initCustomSecret XXH3_initCustomSecret_avx512
  5449. #elif (XXH_VECTOR == XXH_AVX2)
  5450. #define XXH3_accumulate_512 XXH3_accumulate_512_avx2
  5451. #define XXH3_accumulate XXH3_accumulate_avx2
  5452. #define XXH3_scrambleAcc XXH3_scrambleAcc_avx2
  5453. #define XXH3_initCustomSecret XXH3_initCustomSecret_avx2
  5454. #elif (XXH_VECTOR == XXH_SSE2)
  5455. #define XXH3_accumulate_512 XXH3_accumulate_512_sse2
  5456. #define XXH3_accumulate XXH3_accumulate_sse2
  5457. #define XXH3_scrambleAcc XXH3_scrambleAcc_sse2
  5458. #define XXH3_initCustomSecret XXH3_initCustomSecret_sse2
  5459. #elif (XXH_VECTOR == XXH_NEON)
  5460. #define XXH3_accumulate_512 XXH3_accumulate_512_neon
  5461. #define XXH3_accumulate XXH3_accumulate_neon
  5462. #define XXH3_scrambleAcc XXH3_scrambleAcc_neon
  5463. #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  5464. #elif (XXH_VECTOR == XXH_VSX)
  5465. #define XXH3_accumulate_512 XXH3_accumulate_512_vsx
  5466. #define XXH3_accumulate XXH3_accumulate_vsx
  5467. #define XXH3_scrambleAcc XXH3_scrambleAcc_vsx
  5468. #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  5469. #elif (XXH_VECTOR == XXH_SVE)
  5470. #define XXH3_accumulate_512 XXH3_accumulate_512_sve
  5471. #define XXH3_accumulate XXH3_accumulate_sve
  5472. #define XXH3_scrambleAcc XXH3_scrambleAcc_scalar
  5473. #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  5474. #elif (XXH_VECTOR == XXH_LSX)
  5475. #define XXH3_accumulate_512 XXH3_accumulate_512_lsx
  5476. #define XXH3_accumulate XXH3_accumulate_lsx
  5477. #define XXH3_scrambleAcc XXH3_scrambleAcc_lsx
  5478. #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  5479. #else /* scalar */
  5480. #define XXH3_accumulate_512 XXH3_accumulate_512_scalar
  5481. #define XXH3_accumulate XXH3_accumulate_scalar
  5482. #define XXH3_scrambleAcc XXH3_scrambleAcc_scalar
  5483. #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  5484. #endif
  5485. #if XXH_SIZE_OPT >= 1 /* don't do SIMD for initialization */
  5486. # undef XXH3_initCustomSecret
  5487. # define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
  5488. #endif
  5489. XXH_FORCE_INLINE void
  5490. XXH3_hashLong_internal_loop(xxh_u64* XXH_RESTRICT acc,
  5491. const xxh_u8* XXH_RESTRICT input, size_t len,
  5492. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  5493. XXH3_f_accumulate f_acc,
  5494. XXH3_f_scrambleAcc f_scramble)
  5495. {
  5496. size_t const nbStripesPerBlock = (secretSize - XXH_STRIPE_LEN) / XXH_SECRET_CONSUME_RATE;
  5497. size_t const block_len = XXH_STRIPE_LEN * nbStripesPerBlock;
  5498. size_t const nb_blocks = (len - 1) / block_len;
  5499. size_t n;
  5500. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
  5501. for (n = 0; n < nb_blocks; n++) {
  5502. f_acc(acc, input + n*block_len, secret, nbStripesPerBlock);
  5503. f_scramble(acc, secret + secretSize - XXH_STRIPE_LEN);
  5504. }
  5505. /* last partial block */
  5506. XXH_ASSERT(len > XXH_STRIPE_LEN);
  5507. { size_t const nbStripes = ((len - 1) - (block_len * nb_blocks)) / XXH_STRIPE_LEN;
  5508. XXH_ASSERT(nbStripes <= (secretSize / XXH_SECRET_CONSUME_RATE));
  5509. f_acc(acc, input + nb_blocks*block_len, secret, nbStripes);
  5510. /* last stripe */
  5511. { const xxh_u8* const p = input + len - XXH_STRIPE_LEN;
  5512. #define XXH_SECRET_LASTACC_START 7 /* not aligned on 8, last secret is different from acc & scrambler */
  5513. XXH3_accumulate_512(acc, p, secret + secretSize - XXH_STRIPE_LEN - XXH_SECRET_LASTACC_START);
  5514. } }
  5515. }
  5516. XXH_FORCE_INLINE xxh_u64
  5517. XXH3_mix2Accs(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret)
  5518. {
  5519. return XXH3_mul128_fold64(
  5520. acc[0] ^ XXH_readLE64(secret),
  5521. acc[1] ^ XXH_readLE64(secret+8) );
  5522. }
  5523. static XXH_PUREF XXH64_hash_t
  5524. XXH3_mergeAccs(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret, xxh_u64 start)
  5525. {
  5526. xxh_u64 result64 = start;
  5527. size_t i = 0;
  5528. for (i = 0; i < 4; i++) {
  5529. result64 += XXH3_mix2Accs(acc+2*i, secret + 16*i);
  5530. #if defined(__clang__) /* Clang */ \
  5531. && (defined(__arm__) || defined(__thumb__)) /* ARMv7 */ \
  5532. && (defined(__ARM_NEON) || defined(__ARM_NEON__)) /* NEON */ \
  5533. && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable */
  5534. /*
  5535. * UGLY HACK:
  5536. * Prevent autovectorization on Clang ARMv7-a. Exact same problem as
  5537. * the one in XXH3_len_129to240_64b. Speeds up shorter keys > 240b.
  5538. * XXH3_64bits, len == 256, Snapdragon 835:
  5539. * without hack: 2063.7 MB/s
  5540. * with hack: 2560.7 MB/s
  5541. */
  5542. XXH_COMPILER_GUARD(result64);
  5543. #endif
  5544. }
  5545. return XXH3_avalanche(result64);
  5546. }
  5547. /* do not align on 8, so that the secret is different from the accumulator */
  5548. #define XXH_SECRET_MERGEACCS_START 11
  5549. static XXH_PUREF XXH64_hash_t
  5550. XXH3_finalizeLong_64b(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret, xxh_u64 len)
  5551. {
  5552. return XXH3_mergeAccs(acc, secret + XXH_SECRET_MERGEACCS_START, len * XXH_PRIME64_1);
  5553. }
  5554. #define XXH3_INIT_ACC { XXH_PRIME32_3, XXH_PRIME64_1, XXH_PRIME64_2, XXH_PRIME64_3, \
  5555. XXH_PRIME64_4, XXH_PRIME32_2, XXH_PRIME64_5, XXH_PRIME32_1 }
  5556. XXH_FORCE_INLINE XXH64_hash_t
  5557. XXH3_hashLong_64b_internal(const void* XXH_RESTRICT input, size_t len,
  5558. const void* XXH_RESTRICT secret, size_t secretSize,
  5559. XXH3_f_accumulate f_acc,
  5560. XXH3_f_scrambleAcc f_scramble)
  5561. {
  5562. XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[XXH_ACC_NB] = XXH3_INIT_ACC;
  5563. XXH3_hashLong_internal_loop(acc, (const xxh_u8*)input, len, (const xxh_u8*)secret, secretSize, f_acc, f_scramble);
  5564. /* converge into final hash */
  5565. XXH_STATIC_ASSERT(sizeof(acc) == 64);
  5566. XXH_ASSERT(secretSize >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
  5567. return XXH3_finalizeLong_64b(acc, (const xxh_u8*)secret, (xxh_u64)len);
  5568. }
  5569. /*
  5570. * It's important for performance to transmit secret's size (when it's static)
  5571. * so that the compiler can properly optimize the vectorized loop.
  5572. * This makes a big performance difference for "medium" keys (<1 KB) when using AVX instruction set.
  5573. * When the secret size is unknown, or on GCC 12 where the mix of NO_INLINE and FORCE_INLINE
  5574. * breaks -Og, this is XXH_NO_INLINE.
  5575. */
  5576. XXH3_WITH_SECRET_INLINE XXH64_hash_t
  5577. XXH3_hashLong_64b_withSecret(const void* XXH_RESTRICT input, size_t len,
  5578. XXH64_hash_t seed64, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
  5579. {
  5580. (void)seed64;
  5581. return XXH3_hashLong_64b_internal(input, len, secret, secretLen, XXH3_accumulate, XXH3_scrambleAcc);
  5582. }
  5583. /*
  5584. * It's preferable for performance that XXH3_hashLong is not inlined,
  5585. * as it results in a smaller function for small data, easier to the instruction cache.
  5586. * Note that inside this no_inline function, we do inline the internal loop,
  5587. * and provide a statically defined secret size to allow optimization of vector loop.
  5588. */
  5589. XXH_NO_INLINE XXH_PUREF XXH64_hash_t
  5590. XXH3_hashLong_64b_default(const void* XXH_RESTRICT input, size_t len,
  5591. XXH64_hash_t seed64, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
  5592. {
  5593. (void)seed64; (void)secret; (void)secretLen;
  5594. return XXH3_hashLong_64b_internal(input, len, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_accumulate, XXH3_scrambleAcc);
  5595. }
  5596. /*
  5597. * XXH3_hashLong_64b_withSeed():
  5598. * Generate a custom key based on alteration of default XXH3_kSecret with the seed,
  5599. * and then use this key for long mode hashing.
  5600. *
  5601. * This operation is decently fast but nonetheless costs a little bit of time.
  5602. * Try to avoid it whenever possible (typically when seed==0).
  5603. *
  5604. * It's important for performance that XXH3_hashLong is not inlined. Not sure
  5605. * why (uop cache maybe?), but the difference is large and easily measurable.
  5606. */
  5607. XXH_FORCE_INLINE XXH64_hash_t
  5608. XXH3_hashLong_64b_withSeed_internal(const void* input, size_t len,
  5609. XXH64_hash_t seed,
  5610. XXH3_f_accumulate f_acc,
  5611. XXH3_f_scrambleAcc f_scramble,
  5612. XXH3_f_initCustomSecret f_initSec)
  5613. {
  5614. #if XXH_SIZE_OPT <= 0
  5615. if (seed == 0)
  5616. return XXH3_hashLong_64b_internal(input, len,
  5617. XXH3_kSecret, sizeof(XXH3_kSecret),
  5618. f_acc, f_scramble);
  5619. #endif
  5620. { XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
  5621. f_initSec(secret, seed);
  5622. return XXH3_hashLong_64b_internal(input, len, secret, sizeof(secret),
  5623. f_acc, f_scramble);
  5624. }
  5625. }
  5626. /*
  5627. * It's important for performance that XXH3_hashLong is not inlined.
  5628. */
  5629. XXH_NO_INLINE XXH64_hash_t
  5630. XXH3_hashLong_64b_withSeed(const void* XXH_RESTRICT input, size_t len,
  5631. XXH64_hash_t seed, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
  5632. {
  5633. (void)secret; (void)secretLen;
  5634. return XXH3_hashLong_64b_withSeed_internal(input, len, seed,
  5635. XXH3_accumulate, XXH3_scrambleAcc, XXH3_initCustomSecret);
  5636. }
  5637. typedef XXH64_hash_t (*XXH3_hashLong64_f)(const void* XXH_RESTRICT, size_t,
  5638. XXH64_hash_t, const xxh_u8* XXH_RESTRICT, size_t);
  5639. XXH_FORCE_INLINE XXH64_hash_t
  5640. XXH3_64bits_internal(const void* XXH_RESTRICT input, size_t len,
  5641. XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen,
  5642. XXH3_hashLong64_f f_hashLong)
  5643. {
  5644. XXH_ASSERT(secretLen >= XXH3_SECRET_SIZE_MIN);
  5645. /*
  5646. * If an action is to be taken if `secretLen` condition is not respected,
  5647. * it should be done here.
  5648. * For now, it's a contract pre-condition.
  5649. * Adding a check and a branch here would cost performance at every hash.
  5650. * Also, note that function signature doesn't offer room to return an error.
  5651. */
  5652. if (len <= 16)
  5653. return XXH3_len_0to16_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, seed64);
  5654. if (len <= 128)
  5655. return XXH3_len_17to128_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
  5656. if (len <= XXH3_MIDSIZE_MAX)
  5657. return XXH3_len_129to240_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
  5658. return f_hashLong(input, len, seed64, (const xxh_u8*)secret, secretLen);
  5659. }
  5660. /* === Public entry point === */
  5661. /*! @ingroup XXH3_family */
  5662. XXH_PUBLIC_API XXH64_hash_t XXH3_64bits(XXH_NOESCAPE const void* input, size_t length)
  5663. {
  5664. return XXH3_64bits_internal(input, length, 0, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_hashLong_64b_default);
  5665. }
  5666. /*! @ingroup XXH3_family */
  5667. XXH_PUBLIC_API XXH64_hash_t
  5668. XXH3_64bits_withSecret(XXH_NOESCAPE const void* input, size_t length, XXH_NOESCAPE const void* secret, size_t secretSize)
  5669. {
  5670. return XXH3_64bits_internal(input, length, 0, secret, secretSize, XXH3_hashLong_64b_withSecret);
  5671. }
  5672. /*! @ingroup XXH3_family */
  5673. XXH_PUBLIC_API XXH64_hash_t
  5674. XXH3_64bits_withSeed(XXH_NOESCAPE const void* input, size_t length, XXH64_hash_t seed)
  5675. {
  5676. return XXH3_64bits_internal(input, length, seed, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_hashLong_64b_withSeed);
  5677. }
  5678. XXH_PUBLIC_API XXH64_hash_t
  5679. XXH3_64bits_withSecretandSeed(XXH_NOESCAPE const void* input, size_t length, XXH_NOESCAPE const void* secret, size_t secretSize, XXH64_hash_t seed)
  5680. {
  5681. if (length <= XXH3_MIDSIZE_MAX)
  5682. return XXH3_64bits_internal(input, length, seed, XXH3_kSecret, sizeof(XXH3_kSecret), NULL);
  5683. return XXH3_hashLong_64b_withSecret(input, length, seed, (const xxh_u8*)secret, secretSize);
  5684. }
  5685. /* === XXH3 streaming === */
  5686. #ifndef XXH_NO_STREAM
  5687. /*
  5688. * Malloc's a pointer that is always aligned to @align.
  5689. *
  5690. * This must be freed with `XXH_alignedFree()`.
  5691. *
  5692. * malloc typically guarantees 16 byte alignment on 64-bit systems and 8 byte
  5693. * alignment on 32-bit. This isn't enough for the 32 byte aligned loads in AVX2
  5694. * or on 32-bit, the 16 byte aligned loads in SSE2 and NEON.
  5695. *
  5696. * This underalignment previously caused a rather obvious crash which went
  5697. * completely unnoticed due to XXH3_createState() not actually being tested.
  5698. * Credit to RedSpah for noticing this bug.
  5699. *
  5700. * The alignment is done manually: Functions like posix_memalign or _mm_malloc
  5701. * are avoided: To maintain portability, we would have to write a fallback
  5702. * like this anyways, and besides, testing for the existence of library
  5703. * functions without relying on external build tools is impossible.
  5704. *
  5705. * The method is simple: Overallocate, manually align, and store the offset
  5706. * to the original behind the returned pointer.
  5707. *
  5708. * Align must be a power of 2 and 8 <= align <= 128.
  5709. */
  5710. static XXH_MALLOCF void* XXH_alignedMalloc(size_t s, size_t align)
  5711. {
  5712. XXH_ASSERT(align <= 128 && align >= 8); /* range check */
  5713. XXH_ASSERT((align & (align-1)) == 0); /* power of 2 */
  5714. XXH_ASSERT(s != 0 && s < (s + align)); /* empty/overflow */
  5715. { /* Overallocate to make room for manual realignment and an offset byte */
  5716. xxh_u8* base = (xxh_u8*)XXH_malloc(s + align);
  5717. if (base != NULL) {
  5718. /*
  5719. * Get the offset needed to align this pointer.
  5720. *
  5721. * Even if the returned pointer is aligned, there will always be
  5722. * at least one byte to store the offset to the original pointer.
  5723. */
  5724. size_t offset = align - ((size_t)base & (align - 1)); /* base % align */
  5725. /* Add the offset for the now-aligned pointer */
  5726. xxh_u8* ptr = base + offset;
  5727. XXH_ASSERT((size_t)ptr % align == 0);
  5728. /* Store the offset immediately before the returned pointer. */
  5729. ptr[-1] = (xxh_u8)offset;
  5730. return ptr;
  5731. }
  5732. return NULL;
  5733. }
  5734. }
  5735. /*
  5736. * Frees an aligned pointer allocated by XXH_alignedMalloc(). Don't pass
  5737. * normal malloc'd pointers, XXH_alignedMalloc has a specific data layout.
  5738. */
  5739. static void XXH_alignedFree(void* p)
  5740. {
  5741. if (p != NULL) {
  5742. xxh_u8* ptr = (xxh_u8*)p;
  5743. /* Get the offset byte we added in XXH_malloc. */
  5744. xxh_u8 offset = ptr[-1];
  5745. /* Free the original malloc'd pointer */
  5746. xxh_u8* base = ptr - offset;
  5747. XXH_free(base);
  5748. }
  5749. }
  5750. /*! @ingroup XXH3_family */
  5751. /*!
  5752. * @brief Allocate an @ref XXH3_state_t.
  5753. *
  5754. * @return An allocated pointer of @ref XXH3_state_t on success.
  5755. * @return `NULL` on failure.
  5756. *
  5757. * @note Must be freed with XXH3_freeState().
  5758. *
  5759. * @see @ref streaming_example "Streaming Example"
  5760. */
  5761. XXH_PUBLIC_API XXH3_state_t* XXH3_createState(void)
  5762. {
  5763. XXH3_state_t* const state = (XXH3_state_t*)XXH_alignedMalloc(sizeof(XXH3_state_t), 64);
  5764. if (state==NULL) return NULL;
  5765. XXH3_INITSTATE(state);
  5766. return state;
  5767. }
  5768. /*! @ingroup XXH3_family */
  5769. /*!
  5770. * @brief Frees an @ref XXH3_state_t.
  5771. *
  5772. * @param statePtr A pointer to an @ref XXH3_state_t allocated with @ref XXH3_createState().
  5773. *
  5774. * @return @ref XXH_OK.
  5775. *
  5776. * @note Must be allocated with XXH3_createState().
  5777. *
  5778. * @see @ref streaming_example "Streaming Example"
  5779. */
  5780. XXH_PUBLIC_API XXH_errorcode XXH3_freeState(XXH3_state_t* statePtr)
  5781. {
  5782. XXH_alignedFree(statePtr);
  5783. return XXH_OK;
  5784. }
  5785. /*! @ingroup XXH3_family */
  5786. XXH_PUBLIC_API void
  5787. XXH3_copyState(XXH_NOESCAPE XXH3_state_t* dst_state, XXH_NOESCAPE const XXH3_state_t* src_state)
  5788. {
  5789. XXH_memcpy(dst_state, src_state, sizeof(*dst_state));
  5790. }
  5791. static void
  5792. XXH3_reset_internal(XXH3_state_t* statePtr,
  5793. XXH64_hash_t seed,
  5794. const void* secret, size_t secretSize)
  5795. {
  5796. size_t const initStart = offsetof(XXH3_state_t, bufferedSize);
  5797. size_t const initLength = offsetof(XXH3_state_t, nbStripesPerBlock) - initStart;
  5798. XXH_ASSERT(offsetof(XXH3_state_t, nbStripesPerBlock) > initStart);
  5799. XXH_ASSERT(statePtr != NULL);
  5800. /* set members from bufferedSize to nbStripesPerBlock (excluded) to 0 */
  5801. memset((char*)statePtr + initStart, 0, initLength);
  5802. statePtr->acc[0] = XXH_PRIME32_3;
  5803. statePtr->acc[1] = XXH_PRIME64_1;
  5804. statePtr->acc[2] = XXH_PRIME64_2;
  5805. statePtr->acc[3] = XXH_PRIME64_3;
  5806. statePtr->acc[4] = XXH_PRIME64_4;
  5807. statePtr->acc[5] = XXH_PRIME32_2;
  5808. statePtr->acc[6] = XXH_PRIME64_5;
  5809. statePtr->acc[7] = XXH_PRIME32_1;
  5810. statePtr->seed = seed;
  5811. statePtr->useSeed = (seed != 0);
  5812. statePtr->extSecret = (const unsigned char*)secret;
  5813. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
  5814. statePtr->secretLimit = secretSize - XXH_STRIPE_LEN;
  5815. statePtr->nbStripesPerBlock = statePtr->secretLimit / XXH_SECRET_CONSUME_RATE;
  5816. }
  5817. /*! @ingroup XXH3_family */
  5818. XXH_PUBLIC_API XXH_errorcode
  5819. XXH3_64bits_reset(XXH_NOESCAPE XXH3_state_t* statePtr)
  5820. {
  5821. if (statePtr == NULL) return XXH_ERROR;
  5822. XXH3_reset_internal(statePtr, 0, XXH3_kSecret, XXH_SECRET_DEFAULT_SIZE);
  5823. return XXH_OK;
  5824. }
  5825. /*! @ingroup XXH3_family */
  5826. XXH_PUBLIC_API XXH_errorcode
  5827. XXH3_64bits_reset_withSecret(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize)
  5828. {
  5829. if (statePtr == NULL) return XXH_ERROR;
  5830. XXH3_reset_internal(statePtr, 0, secret, secretSize);
  5831. if (secret == NULL) return XXH_ERROR;
  5832. if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
  5833. return XXH_OK;
  5834. }
  5835. /*! @ingroup XXH3_family */
  5836. XXH_PUBLIC_API XXH_errorcode
  5837. XXH3_64bits_reset_withSeed(XXH_NOESCAPE XXH3_state_t* statePtr, XXH64_hash_t seed)
  5838. {
  5839. if (statePtr == NULL) return XXH_ERROR;
  5840. if (seed==0) return XXH3_64bits_reset(statePtr);
  5841. if ((seed != statePtr->seed) || (statePtr->extSecret != NULL))
  5842. XXH3_initCustomSecret(statePtr->customSecret, seed);
  5843. XXH3_reset_internal(statePtr, seed, NULL, XXH_SECRET_DEFAULT_SIZE);
  5844. return XXH_OK;
  5845. }
  5846. /*! @ingroup XXH3_family */
  5847. XXH_PUBLIC_API XXH_errorcode
  5848. XXH3_64bits_reset_withSecretandSeed(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize, XXH64_hash_t seed64)
  5849. {
  5850. if (statePtr == NULL) return XXH_ERROR;
  5851. if (secret == NULL) return XXH_ERROR;
  5852. if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
  5853. XXH3_reset_internal(statePtr, seed64, secret, secretSize);
  5854. statePtr->useSeed = 1; /* always, even if seed64==0 */
  5855. return XXH_OK;
  5856. }
  5857. /*!
  5858. * @internal
  5859. * @brief Processes a large input for XXH3_update() and XXH3_digest_long().
  5860. *
  5861. * Unlike XXH3_hashLong_internal_loop(), this can process data that overlaps a block.
  5862. *
  5863. * @param acc Pointer to the 8 accumulator lanes
  5864. * @param nbStripesSoFarPtr In/out pointer to the number of leftover stripes in the block*
  5865. * @param nbStripesPerBlock Number of stripes in a block
  5866. * @param input Input pointer
  5867. * @param nbStripes Number of stripes to process
  5868. * @param secret Secret pointer
  5869. * @param secretLimit Offset of the last block in @p secret
  5870. * @param f_acc Pointer to an XXH3_accumulate implementation
  5871. * @param f_scramble Pointer to an XXH3_scrambleAcc implementation
  5872. * @return Pointer past the end of @p input after processing
  5873. */
  5874. XXH_FORCE_INLINE const xxh_u8 *
  5875. XXH3_consumeStripes(xxh_u64* XXH_RESTRICT acc,
  5876. size_t* XXH_RESTRICT nbStripesSoFarPtr, size_t nbStripesPerBlock,
  5877. const xxh_u8* XXH_RESTRICT input, size_t nbStripes,
  5878. const xxh_u8* XXH_RESTRICT secret, size_t secretLimit,
  5879. XXH3_f_accumulate f_acc,
  5880. XXH3_f_scrambleAcc f_scramble)
  5881. {
  5882. const xxh_u8* initialSecret = secret + *nbStripesSoFarPtr * XXH_SECRET_CONSUME_RATE;
  5883. /* Process full blocks */
  5884. if (nbStripes >= (nbStripesPerBlock - *nbStripesSoFarPtr)) {
  5885. /* Process the initial partial block... */
  5886. size_t nbStripesThisIter = nbStripesPerBlock - *nbStripesSoFarPtr;
  5887. do {
  5888. /* Accumulate and scramble */
  5889. f_acc(acc, input, initialSecret, nbStripesThisIter);
  5890. f_scramble(acc, secret + secretLimit);
  5891. input += nbStripesThisIter * XXH_STRIPE_LEN;
  5892. nbStripes -= nbStripesThisIter;
  5893. /* Then continue the loop with the full block size */
  5894. nbStripesThisIter = nbStripesPerBlock;
  5895. initialSecret = secret;
  5896. } while (nbStripes >= nbStripesPerBlock);
  5897. *nbStripesSoFarPtr = 0;
  5898. }
  5899. /* Process a partial block */
  5900. if (nbStripes > 0) {
  5901. f_acc(acc, input, initialSecret, nbStripes);
  5902. input += nbStripes * XXH_STRIPE_LEN;
  5903. *nbStripesSoFarPtr += nbStripes;
  5904. }
  5905. /* Return end pointer */
  5906. return input;
  5907. }
  5908. #ifndef XXH3_STREAM_USE_STACK
  5909. # if XXH_SIZE_OPT <= 0 && !defined(__clang__) /* clang doesn't need additional stack space */
  5910. # define XXH3_STREAM_USE_STACK 1
  5911. # endif
  5912. #endif
  5913. /*
  5914. * Both XXH3_64bits_update and XXH3_128bits_update use this routine.
  5915. */
  5916. XXH_FORCE_INLINE XXH_errorcode
  5917. XXH3_update(XXH3_state_t* XXH_RESTRICT const state,
  5918. const xxh_u8* XXH_RESTRICT input, size_t len,
  5919. XXH3_f_accumulate f_acc,
  5920. XXH3_f_scrambleAcc f_scramble)
  5921. {
  5922. if (input==NULL) {
  5923. XXH_ASSERT(len == 0);
  5924. return XXH_OK;
  5925. }
  5926. XXH_ASSERT(state != NULL);
  5927. { const xxh_u8* const bEnd = input + len;
  5928. const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
  5929. #if defined(XXH3_STREAM_USE_STACK) && XXH3_STREAM_USE_STACK >= 1
  5930. /* For some reason, gcc and MSVC seem to suffer greatly
  5931. * when operating accumulators directly into state.
  5932. * Operating into stack space seems to enable proper optimization.
  5933. * clang, on the other hand, doesn't seem to need this trick */
  5934. XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[8];
  5935. XXH_memcpy(acc, state->acc, sizeof(acc));
  5936. #else
  5937. xxh_u64* XXH_RESTRICT const acc = state->acc;
  5938. #endif
  5939. state->totalLen += len;
  5940. XXH_ASSERT(state->bufferedSize <= XXH3_INTERNALBUFFER_SIZE);
  5941. /* small input : just fill in tmp buffer */
  5942. if (len <= XXH3_INTERNALBUFFER_SIZE - state->bufferedSize) {
  5943. XXH_memcpy(state->buffer + state->bufferedSize, input, len);
  5944. state->bufferedSize += (XXH32_hash_t)len;
  5945. return XXH_OK;
  5946. }
  5947. /* total input is now > XXH3_INTERNALBUFFER_SIZE */
  5948. #define XXH3_INTERNALBUFFER_STRIPES (XXH3_INTERNALBUFFER_SIZE / XXH_STRIPE_LEN)
  5949. XXH_STATIC_ASSERT(XXH3_INTERNALBUFFER_SIZE % XXH_STRIPE_LEN == 0); /* clean multiple */
  5950. /*
  5951. * Internal buffer is partially filled (always, except at beginning)
  5952. * Complete it, then consume it.
  5953. */
  5954. if (state->bufferedSize) {
  5955. size_t const loadSize = XXH3_INTERNALBUFFER_SIZE - state->bufferedSize;
  5956. XXH_memcpy(state->buffer + state->bufferedSize, input, loadSize);
  5957. input += loadSize;
  5958. XXH3_consumeStripes(acc,
  5959. &state->nbStripesSoFar, state->nbStripesPerBlock,
  5960. state->buffer, XXH3_INTERNALBUFFER_STRIPES,
  5961. secret, state->secretLimit,
  5962. f_acc, f_scramble);
  5963. state->bufferedSize = 0;
  5964. }
  5965. XXH_ASSERT(input < bEnd);
  5966. if (bEnd - input > XXH3_INTERNALBUFFER_SIZE) {
  5967. size_t nbStripes = (size_t)(bEnd - 1 - input) / XXH_STRIPE_LEN;
  5968. input = XXH3_consumeStripes(acc,
  5969. &state->nbStripesSoFar, state->nbStripesPerBlock,
  5970. input, nbStripes,
  5971. secret, state->secretLimit,
  5972. f_acc, f_scramble);
  5973. XXH_memcpy(state->buffer + sizeof(state->buffer) - XXH_STRIPE_LEN, input - XXH_STRIPE_LEN, XXH_STRIPE_LEN);
  5974. }
  5975. /* Some remaining input (always) : buffer it */
  5976. XXH_ASSERT(input < bEnd);
  5977. XXH_ASSERT(bEnd - input <= XXH3_INTERNALBUFFER_SIZE);
  5978. XXH_ASSERT(state->bufferedSize == 0);
  5979. XXH_memcpy(state->buffer, input, (size_t)(bEnd-input));
  5980. state->bufferedSize = (XXH32_hash_t)(bEnd-input);
  5981. #if defined(XXH3_STREAM_USE_STACK) && XXH3_STREAM_USE_STACK >= 1
  5982. /* save stack accumulators into state */
  5983. XXH_memcpy(state->acc, acc, sizeof(acc));
  5984. #endif
  5985. }
  5986. return XXH_OK;
  5987. }
  5988. /*! @ingroup XXH3_family */
  5989. XXH_PUBLIC_API XXH_errorcode
  5990. XXH3_64bits_update(XXH_NOESCAPE XXH3_state_t* state, XXH_NOESCAPE const void* input, size_t len)
  5991. {
  5992. return XXH3_update(state, (const xxh_u8*)input, len,
  5993. XXH3_accumulate, XXH3_scrambleAcc);
  5994. }
  5995. XXH_FORCE_INLINE void
  5996. XXH3_digest_long (XXH64_hash_t* acc,
  5997. const XXH3_state_t* state,
  5998. const unsigned char* secret)
  5999. {
  6000. xxh_u8 lastStripe[XXH_STRIPE_LEN];
  6001. const xxh_u8* lastStripePtr;
  6002. /*
  6003. * Digest on a local copy. This way, the state remains unaltered, and it can
  6004. * continue ingesting more input afterwards.
  6005. */
  6006. XXH_memcpy(acc, state->acc, sizeof(state->acc));
  6007. if (state->bufferedSize >= XXH_STRIPE_LEN) {
  6008. /* Consume remaining stripes then point to remaining data in buffer */
  6009. size_t const nbStripes = (state->bufferedSize - 1) / XXH_STRIPE_LEN;
  6010. size_t nbStripesSoFar = state->nbStripesSoFar;
  6011. XXH3_consumeStripes(acc,
  6012. &nbStripesSoFar, state->nbStripesPerBlock,
  6013. state->buffer, nbStripes,
  6014. secret, state->secretLimit,
  6015. XXH3_accumulate, XXH3_scrambleAcc);
  6016. lastStripePtr = state->buffer + state->bufferedSize - XXH_STRIPE_LEN;
  6017. } else { /* bufferedSize < XXH_STRIPE_LEN */
  6018. /* Copy to temp buffer */
  6019. size_t const catchupSize = XXH_STRIPE_LEN - state->bufferedSize;
  6020. XXH_ASSERT(state->bufferedSize > 0); /* there is always some input buffered */
  6021. XXH_memcpy(lastStripe, state->buffer + sizeof(state->buffer) - catchupSize, catchupSize);
  6022. XXH_memcpy(lastStripe + catchupSize, state->buffer, state->bufferedSize);
  6023. lastStripePtr = lastStripe;
  6024. }
  6025. /* Last stripe */
  6026. XXH3_accumulate_512(acc,
  6027. lastStripePtr,
  6028. secret + state->secretLimit - XXH_SECRET_LASTACC_START);
  6029. }
  6030. /*! @ingroup XXH3_family */
  6031. XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_digest (XXH_NOESCAPE const XXH3_state_t* state)
  6032. {
  6033. const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
  6034. if (state->totalLen > XXH3_MIDSIZE_MAX) {
  6035. XXH_ALIGN(XXH_ACC_ALIGN) XXH64_hash_t acc[XXH_ACC_NB];
  6036. XXH3_digest_long(acc, state, secret);
  6037. return XXH3_finalizeLong_64b(acc, secret, (xxh_u64)state->totalLen);
  6038. }
  6039. /* totalLen <= XXH3_MIDSIZE_MAX: digesting a short input */
  6040. if (state->useSeed)
  6041. return XXH3_64bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
  6042. return XXH3_64bits_withSecret(state->buffer, (size_t)(state->totalLen),
  6043. secret, state->secretLimit + XXH_STRIPE_LEN);
  6044. }
  6045. #endif /* !XXH_NO_STREAM */
  6046. /* ==========================================
  6047. * XXH3 128 bits (a.k.a XXH128)
  6048. * ==========================================
  6049. * XXH3's 128-bit variant has better mixing and strength than the 64-bit variant,
  6050. * even without counting the significantly larger output size.
  6051. *
  6052. * For example, extra steps are taken to avoid the seed-dependent collisions
  6053. * in 17-240 byte inputs (See XXH3_mix16B and XXH128_mix32B).
  6054. *
  6055. * This strength naturally comes at the cost of some speed, especially on short
  6056. * lengths. Note that longer hashes are about as fast as the 64-bit version
  6057. * due to it using only a slight modification of the 64-bit loop.
  6058. *
  6059. * XXH128 is also more oriented towards 64-bit machines. It is still extremely
  6060. * fast for a _128-bit_ hash on 32-bit (it usually clears XXH64).
  6061. */
  6062. XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
  6063. XXH3_len_1to3_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  6064. {
  6065. /* A doubled version of 1to3_64b with different constants. */
  6066. XXH_ASSERT(input != NULL);
  6067. XXH_ASSERT(1 <= len && len <= 3);
  6068. XXH_ASSERT(secret != NULL);
  6069. /*
  6070. * len = 1: combinedl = { input[0], 0x01, input[0], input[0] }
  6071. * len = 2: combinedl = { input[1], 0x02, input[0], input[1] }
  6072. * len = 3: combinedl = { input[2], 0x03, input[0], input[1] }
  6073. */
  6074. { xxh_u8 const c1 = input[0];
  6075. xxh_u8 const c2 = input[len >> 1];
  6076. xxh_u8 const c3 = input[len - 1];
  6077. xxh_u32 const combinedl = ((xxh_u32)c1 <<16) | ((xxh_u32)c2 << 24)
  6078. | ((xxh_u32)c3 << 0) | ((xxh_u32)len << 8);
  6079. xxh_u32 const combinedh = XXH_rotl32(XXH_swap32(combinedl), 13);
  6080. xxh_u64 const bitflipl = (XXH_readLE32(secret) ^ XXH_readLE32(secret+4)) + seed;
  6081. xxh_u64 const bitfliph = (XXH_readLE32(secret+8) ^ XXH_readLE32(secret+12)) - seed;
  6082. xxh_u64 const keyed_lo = (xxh_u64)combinedl ^ bitflipl;
  6083. xxh_u64 const keyed_hi = (xxh_u64)combinedh ^ bitfliph;
  6084. XXH128_hash_t h128;
  6085. h128.low64 = XXH64_avalanche(keyed_lo);
  6086. h128.high64 = XXH64_avalanche(keyed_hi);
  6087. return h128;
  6088. }
  6089. }
  6090. XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
  6091. XXH3_len_4to8_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  6092. {
  6093. XXH_ASSERT(input != NULL);
  6094. XXH_ASSERT(secret != NULL);
  6095. XXH_ASSERT(4 <= len && len <= 8);
  6096. seed ^= (xxh_u64)XXH_swap32((xxh_u32)seed) << 32;
  6097. { xxh_u32 const input_lo = XXH_readLE32(input);
  6098. xxh_u32 const input_hi = XXH_readLE32(input + len - 4);
  6099. xxh_u64 const input_64 = input_lo + ((xxh_u64)input_hi << 32);
  6100. xxh_u64 const bitflip = (XXH_readLE64(secret+16) ^ XXH_readLE64(secret+24)) + seed;
  6101. xxh_u64 const keyed = input_64 ^ bitflip;
  6102. /* Shift len to the left to ensure it is even, this avoids even multiplies. */
  6103. XXH128_hash_t m128 = XXH_mult64to128(keyed, XXH_PRIME64_1 + (len << 2));
  6104. m128.high64 += (m128.low64 << 1);
  6105. m128.low64 ^= (m128.high64 >> 3);
  6106. m128.low64 = XXH_xorshift64(m128.low64, 35);
  6107. m128.low64 *= PRIME_MX2;
  6108. m128.low64 = XXH_xorshift64(m128.low64, 28);
  6109. m128.high64 = XXH3_avalanche(m128.high64);
  6110. return m128;
  6111. }
  6112. }
  6113. XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
  6114. XXH3_len_9to16_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  6115. {
  6116. XXH_ASSERT(input != NULL);
  6117. XXH_ASSERT(secret != NULL);
  6118. XXH_ASSERT(9 <= len && len <= 16);
  6119. { xxh_u64 const bitflipl = (XXH_readLE64(secret+32) ^ XXH_readLE64(secret+40)) - seed;
  6120. xxh_u64 const bitfliph = (XXH_readLE64(secret+48) ^ XXH_readLE64(secret+56)) + seed;
  6121. xxh_u64 const input_lo = XXH_readLE64(input);
  6122. xxh_u64 input_hi = XXH_readLE64(input + len - 8);
  6123. XXH128_hash_t m128 = XXH_mult64to128(input_lo ^ input_hi ^ bitflipl, XXH_PRIME64_1);
  6124. /*
  6125. * Put len in the middle of m128 to ensure that the length gets mixed to
  6126. * both the low and high bits in the 128x64 multiply below.
  6127. */
  6128. m128.low64 += (xxh_u64)(len - 1) << 54;
  6129. input_hi ^= bitfliph;
  6130. /*
  6131. * Add the high 32 bits of input_hi to the high 32 bits of m128, then
  6132. * add the long product of the low 32 bits of input_hi and XXH_PRIME32_2 to
  6133. * the high 64 bits of m128.
  6134. *
  6135. * The best approach to this operation is different on 32-bit and 64-bit.
  6136. */
  6137. if (sizeof(void *) < sizeof(xxh_u64)) { /* 32-bit */
  6138. /*
  6139. * 32-bit optimized version, which is more readable.
  6140. *
  6141. * On 32-bit, it removes an ADC and delays a dependency between the two
  6142. * halves of m128.high64, but it generates an extra mask on 64-bit.
  6143. */
  6144. m128.high64 += (input_hi & 0xFFFFFFFF00000000ULL) + XXH_mult32to64((xxh_u32)input_hi, XXH_PRIME32_2);
  6145. } else {
  6146. /*
  6147. * 64-bit optimized (albeit more confusing) version.
  6148. *
  6149. * Uses some properties of addition and multiplication to remove the mask:
  6150. *
  6151. * Let:
  6152. * a = input_hi.lo = (input_hi & 0x00000000FFFFFFFF)
  6153. * b = input_hi.hi = (input_hi & 0xFFFFFFFF00000000)
  6154. * c = XXH_PRIME32_2
  6155. *
  6156. * a + (b * c)
  6157. * Inverse Property: x + y - x == y
  6158. * a + (b * (1 + c - 1))
  6159. * Distributive Property: x * (y + z) == (x * y) + (x * z)
  6160. * a + (b * 1) + (b * (c - 1))
  6161. * Identity Property: x * 1 == x
  6162. * a + b + (b * (c - 1))
  6163. *
  6164. * Substitute a, b, and c:
  6165. * input_hi.hi + input_hi.lo + ((xxh_u64)input_hi.lo * (XXH_PRIME32_2 - 1))
  6166. *
  6167. * Since input_hi.hi + input_hi.lo == input_hi, we get this:
  6168. * input_hi + ((xxh_u64)input_hi.lo * (XXH_PRIME32_2 - 1))
  6169. */
  6170. m128.high64 += input_hi + XXH_mult32to64((xxh_u32)input_hi, XXH_PRIME32_2 - 1);
  6171. }
  6172. /* m128 ^= XXH_swap64(m128 >> 64); */
  6173. m128.low64 ^= XXH_swap64(m128.high64);
  6174. { /* 128x64 multiply: h128 = m128 * XXH_PRIME64_2; */
  6175. XXH128_hash_t h128 = XXH_mult64to128(m128.low64, XXH_PRIME64_2);
  6176. h128.high64 += m128.high64 * XXH_PRIME64_2;
  6177. h128.low64 = XXH3_avalanche(h128.low64);
  6178. h128.high64 = XXH3_avalanche(h128.high64);
  6179. return h128;
  6180. } }
  6181. }
  6182. /*
  6183. * Assumption: `secret` size is >= XXH3_SECRET_SIZE_MIN
  6184. */
  6185. XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
  6186. XXH3_len_0to16_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
  6187. {
  6188. XXH_ASSERT(len <= 16);
  6189. { if (len > 8) return XXH3_len_9to16_128b(input, len, secret, seed);
  6190. if (len >= 4) return XXH3_len_4to8_128b(input, len, secret, seed);
  6191. if (len) return XXH3_len_1to3_128b(input, len, secret, seed);
  6192. { XXH128_hash_t h128;
  6193. xxh_u64 const bitflipl = XXH_readLE64(secret+64) ^ XXH_readLE64(secret+72);
  6194. xxh_u64 const bitfliph = XXH_readLE64(secret+80) ^ XXH_readLE64(secret+88);
  6195. h128.low64 = XXH64_avalanche(seed ^ bitflipl);
  6196. h128.high64 = XXH64_avalanche( seed ^ bitfliph);
  6197. return h128;
  6198. } }
  6199. }
  6200. /*
  6201. * A bit slower than XXH3_mix16B, but handles multiply by zero better.
  6202. */
  6203. XXH_FORCE_INLINE XXH128_hash_t
  6204. XXH128_mix32B(XXH128_hash_t acc, const xxh_u8* input_1, const xxh_u8* input_2,
  6205. const xxh_u8* secret, XXH64_hash_t seed)
  6206. {
  6207. acc.low64 += XXH3_mix16B (input_1, secret+0, seed);
  6208. acc.low64 ^= XXH_readLE64(input_2) + XXH_readLE64(input_2 + 8);
  6209. acc.high64 += XXH3_mix16B (input_2, secret+16, seed);
  6210. acc.high64 ^= XXH_readLE64(input_1) + XXH_readLE64(input_1 + 8);
  6211. return acc;
  6212. }
  6213. XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
  6214. XXH3_len_17to128_128b(const xxh_u8* XXH_RESTRICT input, size_t len,
  6215. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  6216. XXH64_hash_t seed)
  6217. {
  6218. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
  6219. XXH_ASSERT(16 < len && len <= 128);
  6220. { XXH128_hash_t acc;
  6221. acc.low64 = len * XXH_PRIME64_1;
  6222. acc.high64 = 0;
  6223. #if XXH_SIZE_OPT >= 1
  6224. {
  6225. /* Smaller, but slightly slower. */
  6226. unsigned int i = (unsigned int)(len - 1) / 32;
  6227. do {
  6228. acc = XXH128_mix32B(acc, input+16*i, input+len-16*(i+1), secret+32*i, seed);
  6229. } while (i-- != 0);
  6230. }
  6231. #else
  6232. if (len > 32) {
  6233. if (len > 64) {
  6234. if (len > 96) {
  6235. acc = XXH128_mix32B(acc, input+48, input+len-64, secret+96, seed);
  6236. }
  6237. acc = XXH128_mix32B(acc, input+32, input+len-48, secret+64, seed);
  6238. }
  6239. acc = XXH128_mix32B(acc, input+16, input+len-32, secret+32, seed);
  6240. }
  6241. acc = XXH128_mix32B(acc, input, input+len-16, secret, seed);
  6242. #endif
  6243. { XXH128_hash_t h128;
  6244. h128.low64 = acc.low64 + acc.high64;
  6245. h128.high64 = (acc.low64 * XXH_PRIME64_1)
  6246. + (acc.high64 * XXH_PRIME64_4)
  6247. + ((len - seed) * XXH_PRIME64_2);
  6248. h128.low64 = XXH3_avalanche(h128.low64);
  6249. h128.high64 = (XXH64_hash_t)0 - XXH3_avalanche(h128.high64);
  6250. return h128;
  6251. }
  6252. }
  6253. }
  6254. XXH_NO_INLINE XXH_PUREF XXH128_hash_t
  6255. XXH3_len_129to240_128b(const xxh_u8* XXH_RESTRICT input, size_t len,
  6256. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  6257. XXH64_hash_t seed)
  6258. {
  6259. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
  6260. XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
  6261. { XXH128_hash_t acc;
  6262. unsigned i;
  6263. acc.low64 = len * XXH_PRIME64_1;
  6264. acc.high64 = 0;
  6265. /*
  6266. * We set as `i` as offset + 32. We do this so that unchanged
  6267. * `len` can be used as upper bound. This reaches a sweet spot
  6268. * where both x86 and aarch64 get simple agen and good codegen
  6269. * for the loop.
  6270. */
  6271. for (i = 32; i < 160; i += 32) {
  6272. acc = XXH128_mix32B(acc,
  6273. input + i - 32,
  6274. input + i - 16,
  6275. secret + i - 32,
  6276. seed);
  6277. }
  6278. acc.low64 = XXH3_avalanche(acc.low64);
  6279. acc.high64 = XXH3_avalanche(acc.high64);
  6280. /*
  6281. * NB: `i <= len` will duplicate the last 32-bytes if
  6282. * len % 32 was zero. This is an unfortunate necessity to keep
  6283. * the hash result stable.
  6284. */
  6285. for (i=160; i <= len; i += 32) {
  6286. acc = XXH128_mix32B(acc,
  6287. input + i - 32,
  6288. input + i - 16,
  6289. secret + XXH3_MIDSIZE_STARTOFFSET + i - 160,
  6290. seed);
  6291. }
  6292. /* last bytes */
  6293. acc = XXH128_mix32B(acc,
  6294. input + len - 16,
  6295. input + len - 32,
  6296. secret + XXH3_SECRET_SIZE_MIN - XXH3_MIDSIZE_LASTOFFSET - 16,
  6297. (XXH64_hash_t)0 - seed);
  6298. { XXH128_hash_t h128;
  6299. h128.low64 = acc.low64 + acc.high64;
  6300. h128.high64 = (acc.low64 * XXH_PRIME64_1)
  6301. + (acc.high64 * XXH_PRIME64_4)
  6302. + ((len - seed) * XXH_PRIME64_2);
  6303. h128.low64 = XXH3_avalanche(h128.low64);
  6304. h128.high64 = (XXH64_hash_t)0 - XXH3_avalanche(h128.high64);
  6305. return h128;
  6306. }
  6307. }
  6308. }
  6309. static XXH_PUREF XXH128_hash_t
  6310. XXH3_finalizeLong_128b(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret, size_t secretSize, xxh_u64 len)
  6311. {
  6312. XXH128_hash_t h128;
  6313. h128.low64 = XXH3_finalizeLong_64b(acc, secret, len);
  6314. h128.high64 = XXH3_mergeAccs(acc, secret + secretSize
  6315. - XXH_STRIPE_LEN - XXH_SECRET_MERGEACCS_START,
  6316. ~(len * XXH_PRIME64_2));
  6317. return h128;
  6318. }
  6319. XXH_FORCE_INLINE XXH128_hash_t
  6320. XXH3_hashLong_128b_internal(const void* XXH_RESTRICT input, size_t len,
  6321. const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
  6322. XXH3_f_accumulate f_acc,
  6323. XXH3_f_scrambleAcc f_scramble)
  6324. {
  6325. XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[XXH_ACC_NB] = XXH3_INIT_ACC;
  6326. XXH3_hashLong_internal_loop(acc, (const xxh_u8*)input, len, secret, secretSize, f_acc, f_scramble);
  6327. /* converge into final hash */
  6328. XXH_STATIC_ASSERT(sizeof(acc) == 64);
  6329. XXH_ASSERT(secretSize >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
  6330. return XXH3_finalizeLong_128b(acc, secret, secretSize, (xxh_u64)len);
  6331. }
  6332. /*
  6333. * It's important for performance that XXH3_hashLong() is not inlined.
  6334. */
  6335. XXH_NO_INLINE XXH_PUREF XXH128_hash_t
  6336. XXH3_hashLong_128b_default(const void* XXH_RESTRICT input, size_t len,
  6337. XXH64_hash_t seed64,
  6338. const void* XXH_RESTRICT secret, size_t secretLen)
  6339. {
  6340. (void)seed64; (void)secret; (void)secretLen;
  6341. return XXH3_hashLong_128b_internal(input, len, XXH3_kSecret, sizeof(XXH3_kSecret),
  6342. XXH3_accumulate, XXH3_scrambleAcc);
  6343. }
  6344. /*
  6345. * It's important for performance to pass @p secretLen (when it's static)
  6346. * to the compiler, so that it can properly optimize the vectorized loop.
  6347. *
  6348. * When the secret size is unknown, or on GCC 12 where the mix of NO_INLINE and FORCE_INLINE
  6349. * breaks -Og, this is XXH_NO_INLINE.
  6350. */
  6351. XXH3_WITH_SECRET_INLINE XXH128_hash_t
  6352. XXH3_hashLong_128b_withSecret(const void* XXH_RESTRICT input, size_t len,
  6353. XXH64_hash_t seed64,
  6354. const void* XXH_RESTRICT secret, size_t secretLen)
  6355. {
  6356. (void)seed64;
  6357. return XXH3_hashLong_128b_internal(input, len, (const xxh_u8*)secret, secretLen,
  6358. XXH3_accumulate, XXH3_scrambleAcc);
  6359. }
  6360. XXH_FORCE_INLINE XXH128_hash_t
  6361. XXH3_hashLong_128b_withSeed_internal(const void* XXH_RESTRICT input, size_t len,
  6362. XXH64_hash_t seed64,
  6363. XXH3_f_accumulate f_acc,
  6364. XXH3_f_scrambleAcc f_scramble,
  6365. XXH3_f_initCustomSecret f_initSec)
  6366. {
  6367. if (seed64 == 0)
  6368. return XXH3_hashLong_128b_internal(input, len,
  6369. XXH3_kSecret, sizeof(XXH3_kSecret),
  6370. f_acc, f_scramble);
  6371. { XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
  6372. f_initSec(secret, seed64);
  6373. return XXH3_hashLong_128b_internal(input, len, (const xxh_u8*)secret, sizeof(secret),
  6374. f_acc, f_scramble);
  6375. }
  6376. }
  6377. /*
  6378. * It's important for performance that XXH3_hashLong is not inlined.
  6379. */
  6380. XXH_NO_INLINE XXH128_hash_t
  6381. XXH3_hashLong_128b_withSeed(const void* input, size_t len,
  6382. XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen)
  6383. {
  6384. (void)secret; (void)secretLen;
  6385. return XXH3_hashLong_128b_withSeed_internal(input, len, seed64,
  6386. XXH3_accumulate, XXH3_scrambleAcc, XXH3_initCustomSecret);
  6387. }
  6388. typedef XXH128_hash_t (*XXH3_hashLong128_f)(const void* XXH_RESTRICT, size_t,
  6389. XXH64_hash_t, const void* XXH_RESTRICT, size_t);
  6390. XXH_FORCE_INLINE XXH128_hash_t
  6391. XXH3_128bits_internal(const void* input, size_t len,
  6392. XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen,
  6393. XXH3_hashLong128_f f_hl128)
  6394. {
  6395. XXH_ASSERT(secretLen >= XXH3_SECRET_SIZE_MIN);
  6396. /*
  6397. * If an action is to be taken if `secret` conditions are not respected,
  6398. * it should be done here.
  6399. * For now, it's a contract pre-condition.
  6400. * Adding a check and a branch here would cost performance at every hash.
  6401. */
  6402. if (len <= 16)
  6403. return XXH3_len_0to16_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, seed64);
  6404. if (len <= 128)
  6405. return XXH3_len_17to128_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
  6406. if (len <= XXH3_MIDSIZE_MAX)
  6407. return XXH3_len_129to240_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
  6408. return f_hl128(input, len, seed64, secret, secretLen);
  6409. }
  6410. /* === Public XXH128 API === */
  6411. /*! @ingroup XXH3_family */
  6412. XXH_PUBLIC_API XXH128_hash_t XXH3_128bits(XXH_NOESCAPE const void* input, size_t len)
  6413. {
  6414. return XXH3_128bits_internal(input, len, 0,
  6415. XXH3_kSecret, sizeof(XXH3_kSecret),
  6416. XXH3_hashLong_128b_default);
  6417. }
  6418. /*! @ingroup XXH3_family */
  6419. XXH_PUBLIC_API XXH128_hash_t
  6420. XXH3_128bits_withSecret(XXH_NOESCAPE const void* input, size_t len, XXH_NOESCAPE const void* secret, size_t secretSize)
  6421. {
  6422. return XXH3_128bits_internal(input, len, 0,
  6423. (const xxh_u8*)secret, secretSize,
  6424. XXH3_hashLong_128b_withSecret);
  6425. }
  6426. /*! @ingroup XXH3_family */
  6427. XXH_PUBLIC_API XXH128_hash_t
  6428. XXH3_128bits_withSeed(XXH_NOESCAPE const void* input, size_t len, XXH64_hash_t seed)
  6429. {
  6430. return XXH3_128bits_internal(input, len, seed,
  6431. XXH3_kSecret, sizeof(XXH3_kSecret),
  6432. XXH3_hashLong_128b_withSeed);
  6433. }
  6434. /*! @ingroup XXH3_family */
  6435. XXH_PUBLIC_API XXH128_hash_t
  6436. XXH3_128bits_withSecretandSeed(XXH_NOESCAPE const void* input, size_t len, XXH_NOESCAPE const void* secret, size_t secretSize, XXH64_hash_t seed)
  6437. {
  6438. if (len <= XXH3_MIDSIZE_MAX)
  6439. return XXH3_128bits_internal(input, len, seed, XXH3_kSecret, sizeof(XXH3_kSecret), NULL);
  6440. return XXH3_hashLong_128b_withSecret(input, len, seed, secret, secretSize);
  6441. }
  6442. /*! @ingroup XXH3_family */
  6443. XXH_PUBLIC_API XXH128_hash_t
  6444. XXH128(XXH_NOESCAPE const void* input, size_t len, XXH64_hash_t seed)
  6445. {
  6446. return XXH3_128bits_withSeed(input, len, seed);
  6447. }
  6448. /* === XXH3 128-bit streaming === */
  6449. #ifndef XXH_NO_STREAM
  6450. /*
  6451. * All initialization and update functions are identical to 64-bit streaming variant.
  6452. * The only difference is the finalization routine.
  6453. */
  6454. /*! @ingroup XXH3_family */
  6455. XXH_PUBLIC_API XXH_errorcode
  6456. XXH3_128bits_reset(XXH_NOESCAPE XXH3_state_t* statePtr)
  6457. {
  6458. return XXH3_64bits_reset(statePtr);
  6459. }
  6460. /*! @ingroup XXH3_family */
  6461. XXH_PUBLIC_API XXH_errorcode
  6462. XXH3_128bits_reset_withSecret(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize)
  6463. {
  6464. return XXH3_64bits_reset_withSecret(statePtr, secret, secretSize);
  6465. }
  6466. /*! @ingroup XXH3_family */
  6467. XXH_PUBLIC_API XXH_errorcode
  6468. XXH3_128bits_reset_withSeed(XXH_NOESCAPE XXH3_state_t* statePtr, XXH64_hash_t seed)
  6469. {
  6470. return XXH3_64bits_reset_withSeed(statePtr, seed);
  6471. }
  6472. /*! @ingroup XXH3_family */
  6473. XXH_PUBLIC_API XXH_errorcode
  6474. XXH3_128bits_reset_withSecretandSeed(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize, XXH64_hash_t seed)
  6475. {
  6476. return XXH3_64bits_reset_withSecretandSeed(statePtr, secret, secretSize, seed);
  6477. }
  6478. /*! @ingroup XXH3_family */
  6479. XXH_PUBLIC_API XXH_errorcode
  6480. XXH3_128bits_update(XXH_NOESCAPE XXH3_state_t* state, XXH_NOESCAPE const void* input, size_t len)
  6481. {
  6482. return XXH3_64bits_update(state, input, len);
  6483. }
  6484. /*! @ingroup XXH3_family */
  6485. XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_digest (XXH_NOESCAPE const XXH3_state_t* state)
  6486. {
  6487. const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
  6488. if (state->totalLen > XXH3_MIDSIZE_MAX) {
  6489. XXH_ALIGN(XXH_ACC_ALIGN) XXH64_hash_t acc[XXH_ACC_NB];
  6490. XXH3_digest_long(acc, state, secret);
  6491. XXH_ASSERT(state->secretLimit + XXH_STRIPE_LEN >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
  6492. return XXH3_finalizeLong_128b(acc, secret, state->secretLimit + XXH_STRIPE_LEN, (xxh_u64)state->totalLen);
  6493. }
  6494. /* len <= XXH3_MIDSIZE_MAX : short code */
  6495. if (state->useSeed)
  6496. return XXH3_128bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
  6497. return XXH3_128bits_withSecret(state->buffer, (size_t)(state->totalLen),
  6498. secret, state->secretLimit + XXH_STRIPE_LEN);
  6499. }
  6500. #endif /* !XXH_NO_STREAM */
  6501. /* 128-bit utility functions */
  6502. #include <string.h> /* memcmp, memcpy */
  6503. /* return : 1 is equal, 0 if different */
  6504. /*! @ingroup XXH3_family */
  6505. XXH_PUBLIC_API int XXH128_isEqual(XXH128_hash_t h1, XXH128_hash_t h2)
  6506. {
  6507. /* note : XXH128_hash_t is compact, it has no padding byte */
  6508. return !(memcmp(&h1, &h2, sizeof(h1)));
  6509. }
  6510. /* This prototype is compatible with stdlib's qsort().
  6511. * @return : >0 if *h128_1 > *h128_2
  6512. * <0 if *h128_1 < *h128_2
  6513. * =0 if *h128_1 == *h128_2 */
  6514. /*! @ingroup XXH3_family */
  6515. XXH_PUBLIC_API int XXH128_cmp(XXH_NOESCAPE const void* h128_1, XXH_NOESCAPE const void* h128_2)
  6516. {
  6517. XXH128_hash_t const h1 = *(const XXH128_hash_t*)h128_1;
  6518. XXH128_hash_t const h2 = *(const XXH128_hash_t*)h128_2;
  6519. int const hcmp = (h1.high64 > h2.high64) - (h2.high64 > h1.high64);
  6520. /* note : bets that, in most cases, hash values are different */
  6521. if (hcmp) return hcmp;
  6522. return (h1.low64 > h2.low64) - (h2.low64 > h1.low64);
  6523. }
  6524. /*====== Canonical representation ======*/
  6525. /*! @ingroup XXH3_family */
  6526. XXH_PUBLIC_API void
  6527. XXH128_canonicalFromHash(XXH_NOESCAPE XXH128_canonical_t* dst, XXH128_hash_t hash)
  6528. {
  6529. XXH_STATIC_ASSERT(sizeof(XXH128_canonical_t) == sizeof(XXH128_hash_t));
  6530. if (XXH_CPU_LITTLE_ENDIAN) {
  6531. hash.high64 = XXH_swap64(hash.high64);
  6532. hash.low64 = XXH_swap64(hash.low64);
  6533. }
  6534. XXH_memcpy(dst, &hash.high64, sizeof(hash.high64));
  6535. XXH_memcpy((char*)dst + sizeof(hash.high64), &hash.low64, sizeof(hash.low64));
  6536. }
  6537. /*! @ingroup XXH3_family */
  6538. XXH_PUBLIC_API XXH128_hash_t
  6539. XXH128_hashFromCanonical(XXH_NOESCAPE const XXH128_canonical_t* src)
  6540. {
  6541. XXH128_hash_t h;
  6542. h.high64 = XXH_readBE64(src);
  6543. h.low64 = XXH_readBE64(src->digest + 8);
  6544. return h;
  6545. }
  6546. /* ==========================================
  6547. * Secret generators
  6548. * ==========================================
  6549. */
  6550. #define XXH_MIN(x, y) (((x) > (y)) ? (y) : (x))
  6551. XXH_FORCE_INLINE void XXH3_combine16(void* dst, XXH128_hash_t h128)
  6552. {
  6553. XXH_writeLE64( dst, XXH_readLE64(dst) ^ h128.low64 );
  6554. XXH_writeLE64( (char*)dst+8, XXH_readLE64((char*)dst+8) ^ h128.high64 );
  6555. }
  6556. /*! @ingroup XXH3_family */
  6557. XXH_PUBLIC_API XXH_errorcode
  6558. XXH3_generateSecret(XXH_NOESCAPE void* secretBuffer, size_t secretSize, XXH_NOESCAPE const void* customSeed, size_t customSeedSize)
  6559. {
  6560. #if (XXH_DEBUGLEVEL >= 1)
  6561. XXH_ASSERT(secretBuffer != NULL);
  6562. XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
  6563. #else
  6564. /* production mode, assert() are disabled */
  6565. if (secretBuffer == NULL) return XXH_ERROR;
  6566. if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
  6567. #endif
  6568. if (customSeedSize == 0) {
  6569. customSeed = XXH3_kSecret;
  6570. customSeedSize = XXH_SECRET_DEFAULT_SIZE;
  6571. }
  6572. #if (XXH_DEBUGLEVEL >= 1)
  6573. XXH_ASSERT(customSeed != NULL);
  6574. #else
  6575. if (customSeed == NULL) return XXH_ERROR;
  6576. #endif
  6577. /* Fill secretBuffer with a copy of customSeed - repeat as needed */
  6578. { size_t pos = 0;
  6579. while (pos < secretSize) {
  6580. size_t const toCopy = XXH_MIN((secretSize - pos), customSeedSize);
  6581. memcpy((char*)secretBuffer + pos, customSeed, toCopy);
  6582. pos += toCopy;
  6583. } }
  6584. { size_t const nbSeg16 = secretSize / 16;
  6585. size_t n;
  6586. XXH128_canonical_t scrambler;
  6587. XXH128_canonicalFromHash(&scrambler, XXH128(customSeed, customSeedSize, 0));
  6588. for (n=0; n<nbSeg16; n++) {
  6589. XXH128_hash_t const h128 = XXH128(&scrambler, sizeof(scrambler), n);
  6590. XXH3_combine16((char*)secretBuffer + n*16, h128);
  6591. }
  6592. /* last segment */
  6593. XXH3_combine16((char*)secretBuffer + secretSize - 16, XXH128_hashFromCanonical(&scrambler));
  6594. }
  6595. return XXH_OK;
  6596. }
  6597. /*! @ingroup XXH3_family */
  6598. XXH_PUBLIC_API void
  6599. XXH3_generateSecret_fromSeed(XXH_NOESCAPE void* secretBuffer, XXH64_hash_t seed)
  6600. {
  6601. XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
  6602. XXH3_initCustomSecret(secret, seed);
  6603. XXH_ASSERT(secretBuffer != NULL);
  6604. memcpy(secretBuffer, secret, XXH_SECRET_DEFAULT_SIZE);
  6605. }
  6606. /* Pop our optimization override from above */
  6607. #if XXH_VECTOR == XXH_AVX2 /* AVX2 */ \
  6608. && defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
  6609. && defined(__OPTIMIZE__) && XXH_SIZE_OPT <= 0 /* respect -O0 and -Os */
  6610. # pragma GCC pop_options
  6611. #endif
  6612. #endif /* XXH_NO_LONG_LONG */
  6613. #endif /* XXH_NO_XXH3 */
  6614. /*!
  6615. * @}
  6616. */
  6617. #endif /* XXH_IMPLEMENTATION */
  6618. #if defined (__cplusplus)
  6619. } /* extern "C" */
  6620. #endif