xxhash.h 236 KB

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