xxhash.h 259 KB

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