xxhash.h 199 KB

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