xxhash.h 247 KB

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