sse2neon.h 331 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743
  1. #ifndef SSE2NEON_H
  2. #define SSE2NEON_H
  3. // This header file provides a simple API translation layer
  4. // between SSE intrinsics to their corresponding Arm/Aarch64 NEON versions
  5. //
  6. // This header file does not yet translate all of the SSE intrinsics.
  7. //
  8. // Contributors to this work are:
  9. // John W. Ratcliff <jratcliffscarab@gmail.com>
  10. // Brandon Rowlett <browlett@nvidia.com>
  11. // Ken Fast <kfast@gdeb.com>
  12. // Eric van Beurden <evanbeurden@nvidia.com>
  13. // Alexander Potylitsin <apotylitsin@nvidia.com>
  14. // Hasindu Gamaarachchi <hasindu2008@gmail.com>
  15. // Jim Huang <jserv@biilabs.io>
  16. // Mark Cheng <marktwtn@biilabs.io>
  17. // Malcolm James MacLeod <malcolm@gulden.com>
  18. // Devin Hussey (easyaspi314) <husseydevin@gmail.com>
  19. // Sebastian Pop <spop@amazon.com>
  20. // Developer Ecosystem Engineering <DeveloperEcosystemEngineering@apple.com>
  21. // Danila Kutenin <danilak@google.com>
  22. // François Turban (JishinMaster) <francois.turban@gmail.com>
  23. // Pei-Hsuan Hung <afcidk@gmail.com>
  24. // Yang-Hao Yuan <yanghau@biilabs.io>
  25. // Syoyo Fujita <syoyo@lighttransport.com>
  26. // Brecht Van Lommel <brecht@blender.org>
  27. /*
  28. * sse2neon is freely redistributable under the MIT License.
  29. *
  30. * Permission is hereby granted, free of charge, to any person obtaining a copy
  31. * of this software and associated documentation files (the "Software"), to deal
  32. * in the Software without restriction, including without limitation the rights
  33. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  34. * copies of the Software, and to permit persons to whom the Software is
  35. * furnished to do so, subject to the following conditions:
  36. *
  37. * The above copyright notice and this permission notice shall be included in
  38. * all copies or substantial portions of the Software.
  39. *
  40. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  41. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  42. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  43. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  44. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  45. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  46. * SOFTWARE.
  47. */
  48. /* Tunable configurations */
  49. /* Enable precise implementation of math operations
  50. * This would slow down the computation a bit, but gives consistent result with
  51. * x86 SSE. (e.g. would solve a hole or NaN pixel in the rendering result)
  52. */
  53. /* _mm_min_ps and _mm_max_ps */
  54. #ifndef SSE2NEON_PRECISE_MINMAX
  55. #define SSE2NEON_PRECISE_MINMAX (0)
  56. #endif
  57. /* _mm_rcp_ps and _mm_div_ps */
  58. #ifndef SSE2NEON_PRECISE_DIV
  59. #define SSE2NEON_PRECISE_DIV (0)
  60. #endif
  61. /* _mm_sqrt_ps and _mm_rsqrt_ps */
  62. #ifndef SSE2NEON_PRECISE_SQRT
  63. #define SSE2NEON_PRECISE_SQRT (0)
  64. #endif
  65. /* _mm_dp_pd */
  66. #ifndef SSE2NEON_PRECISE_DP
  67. #define SSE2NEON_PRECISE_DP (0)
  68. #endif
  69. /* compiler specific definitions */
  70. #if defined(__GNUC__) || defined(__clang__)
  71. #pragma push_macro("FORCE_INLINE")
  72. #pragma push_macro("ALIGN_STRUCT")
  73. #define FORCE_INLINE static inline __attribute__((always_inline))
  74. #define ALIGN_STRUCT(x) __attribute__((aligned(x)))
  75. #define _sse2neon_likely(x) __builtin_expect(!!(x), 1)
  76. #define _sse2neon_unlikely(x) __builtin_expect(!!(x), 0)
  77. #else /* non-GNU / non-clang compilers */
  78. #warning "Macro name collisions may happen with unsupported compiler."
  79. #ifndef FORCE_INLINE
  80. #define FORCE_INLINE static inline
  81. #endif
  82. #ifndef ALIGN_STRUCT
  83. #define ALIGN_STRUCT(x) __declspec(align(x))
  84. #endif
  85. #define _sse2neon_likely(x) (x)
  86. #define _sse2neon_unlikely(x) (x)
  87. #endif
  88. #include <stdint.h>
  89. #include <stdlib.h>
  90. /* Architecture-specific build options */
  91. /* FIXME: #pragma GCC push_options is only available on GCC */
  92. #if defined(__GNUC__)
  93. #if defined(__arm__) && __ARM_ARCH == 7
  94. /* According to ARM C Language Extensions Architecture specification,
  95. * __ARM_NEON is defined to a value indicating the Advanced SIMD (NEON)
  96. * architecture supported.
  97. */
  98. #if !defined(__ARM_NEON) || !defined(__ARM_NEON__)
  99. #error "You must enable NEON instructions (e.g. -mfpu=neon) to use SSE2NEON."
  100. #endif
  101. #if !defined(__clang__)
  102. #pragma GCC push_options
  103. #pragma GCC target("fpu=neon")
  104. #endif
  105. #elif defined(__aarch64__)
  106. #if !defined(__clang__)
  107. #pragma GCC push_options
  108. #pragma GCC target("+simd")
  109. #endif
  110. #else
  111. #error "Unsupported target. Must be either ARMv7-A+NEON or ARMv8-A."
  112. #endif
  113. #endif
  114. #include <arm_neon.h>
  115. /* Rounding functions require either Aarch64 instructions or libm failback */
  116. #if !defined(__aarch64__)
  117. #include <math.h>
  118. #endif
  119. /* "__has_builtin" can be used to query support for built-in functions
  120. * provided by gcc/clang and other compilers that support it.
  121. */
  122. #ifndef __has_builtin /* GCC prior to 10 or non-clang compilers */
  123. /* Compatibility with gcc <= 9 */
  124. #if __GNUC__ <= 9
  125. #define __has_builtin(x) HAS##x
  126. #define HAS__builtin_popcount 1
  127. #define HAS__builtin_popcountll 1
  128. #else
  129. #define __has_builtin(x) 0
  130. #endif
  131. #endif
  132. /**
  133. * MACRO for shuffle parameter for _mm_shuffle_ps().
  134. * Argument fp3 is a digit[0123] that represents the fp from argument "b"
  135. * of mm_shuffle_ps that will be placed in fp3 of result. fp2 is the same
  136. * for fp2 in result. fp1 is a digit[0123] that represents the fp from
  137. * argument "a" of mm_shuffle_ps that will be places in fp1 of result.
  138. * fp0 is the same for fp0 of result.
  139. */
  140. #if defined(__aarch64__)
  141. #define _MN_SHUFFLE(fp3,fp2,fp1,fp0) ( (uint8x16_t){ (((fp3)*4)+0), (((fp3)*4)+1), (((fp3)*4)+2), (((fp3)*4)+3), (((fp2)*4)+0), (((fp2)*4)+1), (((fp2)*4)+\
  142. 2), (((fp2)*4)+3), (((fp1)*4)+0), (((fp1)*4)+1), (((fp1)*4)+2), (((fp1)*4)+3), (((fp0)*4)+0), (((fp0)*4)+1), (((fp0)*4)+2), (((fp0)*4)+3) } )
  143. #define _MF_SHUFFLE(fp3,fp2,fp1,fp0) ( (uint8x16_t){ (((fp3)*4)+0), (((fp3)*4)+1), (((fp3)*4)+2), (((fp3)*4)+3), (((fp2)*4)+0), (((fp2)*4)+1), (((fp2)*4)+\
  144. 2), (((fp2)*4)+3), (((fp1)*4)+16+0), (((fp1)*4)+16+1), (((fp1)*4)+16+2), (((fp1)*4)+16+3), (((fp0)*4)+16+0), (((fp0)*4)+16+1), (((fp0)*4)+16+2), (((fp0)*\
  145. 4)+16+3) } )
  146. #endif
  147. #define _MM_SHUFFLE(fp3, fp2, fp1, fp0) \
  148. (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0)))
  149. /* Rounding mode macros. */
  150. #define _MM_FROUND_TO_NEAREST_INT 0x00
  151. #define _MM_FROUND_TO_NEG_INF 0x01
  152. #define _MM_FROUND_TO_POS_INF 0x02
  153. #define _MM_FROUND_TO_ZERO 0x03
  154. #define _MM_FROUND_CUR_DIRECTION 0x04
  155. #define _MM_FROUND_NO_EXC 0x08
  156. #define _MM_ROUND_NEAREST 0x0000
  157. #define _MM_ROUND_DOWN 0x2000
  158. #define _MM_ROUND_UP 0x4000
  159. #define _MM_ROUND_TOWARD_ZERO 0x6000
  160. /* Flush zero mode macros. */
  161. #define _MM_FLUSH_ZERO_MASK 0x8000
  162. #define _MM_FLUSH_ZERO_ON 0x8000
  163. #define _MM_FLUSH_ZERO_OFF 0x0000
  164. /* Denormals are zeros mode macros. */
  165. #define _MM_DENORMALS_ZERO_MASK 0x0040
  166. #define _MM_DENORMALS_ZERO_ON 0x0040
  167. #define _MM_DENORMALS_ZERO_OFF 0x0000
  168. /* indicate immediate constant argument in a given range */
  169. #define __constrange(a, b) const
  170. /* A few intrinsics accept traditional data types like ints or floats, but
  171. * most operate on data types that are specific to SSE.
  172. * If a vector type ends in d, it contains doubles, and if it does not have
  173. * a suffix, it contains floats. An integer vector type can contain any type
  174. * of integer, from chars to shorts to unsigned long longs.
  175. */
  176. typedef int64x1_t __m64;
  177. typedef float32x4_t __m128; /* 128-bit vector containing 4 floats */
  178. // On ARM 32-bit architecture, the float64x2_t is not supported.
  179. // The data type __m128d should be represented in a different way for related
  180. // intrinsic conversion.
  181. #if defined(__aarch64__)
  182. typedef float64x2_t __m128d; /* 128-bit vector containing 2 doubles */
  183. #else
  184. typedef float32x4_t __m128d;
  185. #endif
  186. // Note: upstream sse2neon declares __m128i as int64x2_t. However, there's
  187. // many places within embree that assume __m128i can be indexed as a
  188. // 4 element u32.
  189. typedef int32x4_t __m128i; /* 128-bit vector containing integers */
  190. /* type-safe casting between types */
  191. #define vreinterpretq_m128_f16(x) vreinterpretq_f32_f16(x)
  192. #define vreinterpretq_m128_f32(x) (x)
  193. #define vreinterpretq_m128_f64(x) vreinterpretq_f32_f64(x)
  194. #define vreinterpretq_m128_u8(x) vreinterpretq_f32_u8(x)
  195. #define vreinterpretq_m128_u16(x) vreinterpretq_f32_u16(x)
  196. #define vreinterpretq_m128_u32(x) vreinterpretq_f32_u32(x)
  197. #define vreinterpretq_m128_u64(x) vreinterpretq_f32_u64(x)
  198. #define vreinterpretq_m128_s8(x) vreinterpretq_f32_s8(x)
  199. #define vreinterpretq_m128_s16(x) vreinterpretq_f32_s16(x)
  200. #define vreinterpretq_m128_s32(x) vreinterpretq_f32_s32(x)
  201. #define vreinterpretq_m128_s64(x) vreinterpretq_f32_s64(x)
  202. #define vreinterpretq_f16_m128(x) vreinterpretq_f16_f32(x)
  203. #define vreinterpretq_f32_m128(x) (x)
  204. #define vreinterpretq_f64_m128(x) vreinterpretq_f64_f32(x)
  205. #define vreinterpretq_u8_m128(x) vreinterpretq_u8_f32(x)
  206. #define vreinterpretq_u16_m128(x) vreinterpretq_u16_f32(x)
  207. #define vreinterpretq_u32_m128(x) vreinterpretq_u32_f32(x)
  208. #define vreinterpretq_u64_m128(x) vreinterpretq_u64_f32(x)
  209. #define vreinterpretq_s8_m128(x) vreinterpretq_s8_f32(x)
  210. #define vreinterpretq_s16_m128(x) vreinterpretq_s16_f32(x)
  211. #define vreinterpretq_s32_m128(x) vreinterpretq_s32_f32(x)
  212. #define vreinterpretq_s64_m128(x) vreinterpretq_s64_f32(x)
  213. #define vreinterpretq_m128i_s8(x) vreinterpretq_s32_s8(x)
  214. #define vreinterpretq_m128i_s16(x) vreinterpretq_s32_s16(x)
  215. #define vreinterpretq_m128i_s32(x) (x)
  216. #define vreinterpretq_m128i_s64(x) vreinterpretq_s32_s64(x)
  217. #define vreinterpretq_m128i_u8(x) vreinterpretq_s32_u8(x)
  218. #define vreinterpretq_m128i_u16(x) vreinterpretq_s32_u16(x)
  219. #define vreinterpretq_m128i_u32(x) vreinterpretq_s32_u32(x)
  220. #define vreinterpretq_m128i_u64(x) vreinterpretq_s32_u64(x)
  221. #define vreinterpretq_f32_m128i(x) vreinterpretq_f32_s32(x)
  222. #define vreinterpretq_f64_m128i(x) vreinterpretq_f64_s32(x)
  223. #define vreinterpretq_s8_m128i(x) vreinterpretq_s8_s32(x)
  224. #define vreinterpretq_s16_m128i(x) vreinterpretq_s16_s32(x)
  225. #define vreinterpretq_s32_m128i(x) (x)
  226. #define vreinterpretq_s64_m128i(x) vreinterpretq_s64_s32(x)
  227. #define vreinterpretq_u8_m128i(x) vreinterpretq_u8_s32(x)
  228. #define vreinterpretq_u16_m128i(x) vreinterpretq_u16_s32(x)
  229. #define vreinterpretq_u32_m128i(x) vreinterpretq_u32_s32(x)
  230. #define vreinterpretq_u64_m128i(x) vreinterpretq_u64_s32(x)
  231. #define vreinterpret_m64_s8(x) vreinterpret_s64_s8(x)
  232. #define vreinterpret_m64_s16(x) vreinterpret_s64_s16(x)
  233. #define vreinterpret_m64_s32(x) vreinterpret_s64_s32(x)
  234. #define vreinterpret_m64_s64(x) (x)
  235. #define vreinterpret_m64_u8(x) vreinterpret_s64_u8(x)
  236. #define vreinterpret_m64_u16(x) vreinterpret_s64_u16(x)
  237. #define vreinterpret_m64_u32(x) vreinterpret_s64_u32(x)
  238. #define vreinterpret_m64_u64(x) vreinterpret_s64_u64(x)
  239. #define vreinterpret_m64_f16(x) vreinterpret_s64_f16(x)
  240. #define vreinterpret_m64_f32(x) vreinterpret_s64_f32(x)
  241. #define vreinterpret_m64_f64(x) vreinterpret_s64_f64(x)
  242. #define vreinterpret_u8_m64(x) vreinterpret_u8_s64(x)
  243. #define vreinterpret_u16_m64(x) vreinterpret_u16_s64(x)
  244. #define vreinterpret_u32_m64(x) vreinterpret_u32_s64(x)
  245. #define vreinterpret_u64_m64(x) vreinterpret_u64_s64(x)
  246. #define vreinterpret_s8_m64(x) vreinterpret_s8_s64(x)
  247. #define vreinterpret_s16_m64(x) vreinterpret_s16_s64(x)
  248. #define vreinterpret_s32_m64(x) vreinterpret_s32_s64(x)
  249. #define vreinterpret_s64_m64(x) (x)
  250. #define vreinterpret_f32_m64(x) vreinterpret_f32_s64(x)
  251. #if defined(__aarch64__)
  252. #define vreinterpretq_m128d_s32(x) vreinterpretq_f64_s32(x)
  253. #define vreinterpretq_m128d_s64(x) vreinterpretq_f64_s64(x)
  254. #define vreinterpretq_m128d_u64(x) vreinterpretq_f64_u64(x)
  255. #define vreinterpretq_m128d_f32(x) vreinterpretq_f64_f32(x)
  256. #define vreinterpretq_m128d_f64(x) (x)
  257. #define vreinterpretq_s64_m128d(x) vreinterpretq_s64_f64(x)
  258. #define vreinterpretq_u32_m128d(x) vreinterpretq_u32_f64(x)
  259. #define vreinterpretq_u64_m128d(x) vreinterpretq_u64_f64(x)
  260. #define vreinterpretq_f64_m128d(x) (x)
  261. #define vreinterpretq_f32_m128d(x) vreinterpretq_f32_f64(x)
  262. #else
  263. #define vreinterpretq_m128d_s32(x) vreinterpretq_f32_s32(x)
  264. #define vreinterpretq_m128d_s64(x) vreinterpretq_f32_s64(x)
  265. #define vreinterpretq_m128d_u32(x) vreinterpretq_f32_u32(x)
  266. #define vreinterpretq_m128d_u64(x) vreinterpretq_f32_u64(x)
  267. #define vreinterpretq_m128d_f32(x) (x)
  268. #define vreinterpretq_s64_m128d(x) vreinterpretq_s64_f32(x)
  269. #define vreinterpretq_u32_m128d(x) vreinterpretq_u32_f32(x)
  270. #define vreinterpretq_u64_m128d(x) vreinterpretq_u64_f32(x)
  271. #define vreinterpretq_f32_m128d(x) (x)
  272. #endif
  273. // A struct is defined in this header file called 'SIMDVec' which can be used
  274. // by applications which attempt to access the contents of an __m128 struct
  275. // directly. It is important to note that accessing the __m128 struct directly
  276. // is bad coding practice by Microsoft: @see:
  277. // https://docs.microsoft.com/en-us/cpp/cpp/m128
  278. //
  279. // However, some legacy source code may try to access the contents of an __m128
  280. // struct directly so the developer can use the SIMDVec as an alias for it. Any
  281. // casting must be done manually by the developer, as you cannot cast or
  282. // otherwise alias the base NEON data type for intrinsic operations.
  283. //
  284. // union intended to allow direct access to an __m128 variable using the names
  285. // that the MSVC compiler provides. This union should really only be used when
  286. // trying to access the members of the vector as integer values. GCC/clang
  287. // allow native access to the float members through a simple array access
  288. // operator (in C since 4.6, in C++ since 4.8).
  289. //
  290. // Ideally direct accesses to SIMD vectors should not be used since it can cause
  291. // a performance hit. If it really is needed however, the original __m128
  292. // variable can be aliased with a pointer to this union and used to access
  293. // individual components. The use of this union should be hidden behind a macro
  294. // that is used throughout the codebase to access the members instead of always
  295. // declaring this type of variable.
  296. typedef union ALIGN_STRUCT(16) SIMDVec {
  297. float m128_f32[4]; // as floats - DON'T USE. Added for convenience.
  298. int8_t m128_i8[16]; // as signed 8-bit integers.
  299. int16_t m128_i16[8]; // as signed 16-bit integers.
  300. int32_t m128_i32[4]; // as signed 32-bit integers.
  301. int64_t m128_i64[2]; // as signed 64-bit integers.
  302. uint8_t m128_u8[16]; // as unsigned 8-bit integers.
  303. uint16_t m128_u16[8]; // as unsigned 16-bit integers.
  304. uint32_t m128_u32[4]; // as unsigned 32-bit integers.
  305. uint64_t m128_u64[2]; // as unsigned 64-bit integers.
  306. } SIMDVec;
  307. // casting using SIMDVec
  308. #define vreinterpretq_nth_u64_m128i(x, n) (((SIMDVec *) &x)->m128_u64[n])
  309. #define vreinterpretq_nth_u32_m128i(x, n) (((SIMDVec *) &x)->m128_u32[n])
  310. #define vreinterpretq_nth_u8_m128i(x, n) (((SIMDVec *) &x)->m128_u8[n])
  311. /* SSE macros */
  312. #define _MM_GET_FLUSH_ZERO_MODE _sse2neon_mm_get_flush_zero_mode
  313. #define _MM_SET_FLUSH_ZERO_MODE _sse2neon_mm_set_flush_zero_mode
  314. #define _MM_GET_DENORMALS_ZERO_MODE _sse2neon_mm_get_denormals_zero_mode
  315. #define _MM_SET_DENORMALS_ZERO_MODE _sse2neon_mm_set_denormals_zero_mode
  316. // Function declaration
  317. // SSE
  318. FORCE_INLINE unsigned int _MM_GET_ROUNDING_MODE();
  319. FORCE_INLINE __m128 _mm_move_ss(__m128, __m128);
  320. FORCE_INLINE __m128 _mm_or_ps(__m128, __m128);
  321. FORCE_INLINE __m128 _mm_set_ps1(float);
  322. FORCE_INLINE __m128 _mm_setzero_ps(void);
  323. // SSE2
  324. FORCE_INLINE __m128i _mm_and_si128(__m128i, __m128i);
  325. FORCE_INLINE __m128i _mm_castps_si128(__m128);
  326. FORCE_INLINE __m128i _mm_cmpeq_epi32(__m128i, __m128i);
  327. FORCE_INLINE __m128i _mm_cvtps_epi32(__m128);
  328. FORCE_INLINE __m128d _mm_move_sd(__m128d, __m128d);
  329. FORCE_INLINE __m128i _mm_or_si128(__m128i, __m128i);
  330. FORCE_INLINE __m128i _mm_set_epi32(int, int, int, int);
  331. FORCE_INLINE __m128i _mm_set_epi64x(int64_t, int64_t);
  332. FORCE_INLINE __m128d _mm_set_pd(double, double);
  333. FORCE_INLINE __m128i _mm_set1_epi32(int);
  334. FORCE_INLINE __m128i _mm_setzero_si128();
  335. // SSE4.1
  336. FORCE_INLINE __m128d _mm_ceil_pd(__m128d);
  337. FORCE_INLINE __m128 _mm_ceil_ps(__m128);
  338. FORCE_INLINE __m128d _mm_floor_pd(__m128d);
  339. FORCE_INLINE __m128 _mm_floor_ps(__m128);
  340. FORCE_INLINE __m128d _mm_round_pd(__m128d, int);
  341. FORCE_INLINE __m128 _mm_round_ps(__m128, int);
  342. // SSE4.2
  343. FORCE_INLINE uint32_t _mm_crc32_u8(uint32_t, uint8_t);
  344. /* Backwards compatibility for compilers with lack of specific type support */
  345. // Older gcc does not define vld1q_u8_x4 type
  346. #if defined(__GNUC__) && !defined(__clang__) && \
  347. ((__GNUC__ <= 10 && defined(__arm__)) || \
  348. (__GNUC__ == 10 && __GNUC_MINOR__ < 3 && defined(__aarch64__)) || \
  349. (__GNUC__ <= 9 && defined(__aarch64__)))
  350. FORCE_INLINE uint8x16x4_t _sse2neon_vld1q_u8_x4(const uint8_t *p)
  351. {
  352. uint8x16x4_t ret;
  353. ret.val[0] = vld1q_u8(p + 0);
  354. ret.val[1] = vld1q_u8(p + 16);
  355. ret.val[2] = vld1q_u8(p + 32);
  356. ret.val[3] = vld1q_u8(p + 48);
  357. return ret;
  358. }
  359. #else
  360. // Wraps vld1q_u8_x4
  361. FORCE_INLINE uint8x16x4_t _sse2neon_vld1q_u8_x4(const uint8_t *p)
  362. {
  363. return vld1q_u8_x4(p);
  364. }
  365. #endif
  366. /* Function Naming Conventions
  367. * The naming convention of SSE intrinsics is straightforward. A generic SSE
  368. * intrinsic function is given as follows:
  369. * _mm_<name>_<data_type>
  370. *
  371. * The parts of this format are given as follows:
  372. * 1. <name> describes the operation performed by the intrinsic
  373. * 2. <data_type> identifies the data type of the function's primary arguments
  374. *
  375. * This last part, <data_type>, is a little complicated. It identifies the
  376. * content of the input values, and can be set to any of the following values:
  377. * + ps - vectors contain floats (ps stands for packed single-precision)
  378. * + pd - vectors cantain doubles (pd stands for packed double-precision)
  379. * + epi8/epi16/epi32/epi64 - vectors contain 8-bit/16-bit/32-bit/64-bit
  380. * signed integers
  381. * + epu8/epu16/epu32/epu64 - vectors contain 8-bit/16-bit/32-bit/64-bit
  382. * unsigned integers
  383. * + si128 - unspecified 128-bit vector or 256-bit vector
  384. * + m128/m128i/m128d - identifies input vector types when they are different
  385. * than the type of the returned vector
  386. *
  387. * For example, _mm_setzero_ps. The _mm implies that the function returns
  388. * a 128-bit vector. The _ps at the end implies that the argument vectors
  389. * contain floats.
  390. *
  391. * A complete example: Byte Shuffle - pshufb (_mm_shuffle_epi8)
  392. * // Set packed 16-bit integers. 128 bits, 8 short, per 16 bits
  393. * __m128i v_in = _mm_setr_epi16(1, 2, 3, 4, 5, 6, 7, 8);
  394. * // Set packed 8-bit integers
  395. * // 128 bits, 16 chars, per 8 bits
  396. * __m128i v_perm = _mm_setr_epi8(1, 0, 2, 3, 8, 9, 10, 11,
  397. * 4, 5, 12, 13, 6, 7, 14, 15);
  398. * // Shuffle packed 8-bit integers
  399. * __m128i v_out = _mm_shuffle_epi8(v_in, v_perm); // pshufb
  400. *
  401. * Data (Number, Binary, Byte Index):
  402. +------+------+-------------+------+------+-------------+
  403. | 1 | 2 | 3 | 4 | Number
  404. +------+------+------+------+------+------+------+------+
  405. | 0000 | 0001 | 0000 | 0010 | 0000 | 0011 | 0000 | 0100 | Binary
  406. +------+------+------+------+------+------+------+------+
  407. | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Index
  408. +------+------+------+------+------+------+------+------+
  409. +------+------+------+------+------+------+------+------+
  410. | 5 | 6 | 7 | 8 | Number
  411. +------+------+------+------+------+------+------+------+
  412. | 0000 | 0101 | 0000 | 0110 | 0000 | 0111 | 0000 | 1000 | Binary
  413. +------+------+------+------+------+------+------+------+
  414. | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Index
  415. +------+------+------+------+------+------+------+------+
  416. * Index (Byte Index):
  417. +------+------+------+------+------+------+------+------+
  418. | 1 | 0 | 2 | 3 | 8 | 9 | 10 | 11 |
  419. +------+------+------+------+------+------+------+------+
  420. +------+------+------+------+------+------+------+------+
  421. | 4 | 5 | 12 | 13 | 6 | 7 | 14 | 15 |
  422. +------+------+------+------+------+------+------+------+
  423. * Result:
  424. +------+------+------+------+------+------+------+------+
  425. | 1 | 0 | 2 | 3 | 8 | 9 | 10 | 11 | Index
  426. +------+------+------+------+------+------+------+------+
  427. | 0001 | 0000 | 0000 | 0010 | 0000 | 0101 | 0000 | 0110 | Binary
  428. +------+------+------+------+------+------+------+------+
  429. | 256 | 2 | 5 | 6 | Number
  430. +------+------+------+------+------+------+------+------+
  431. +------+------+------+------+------+------+------+------+
  432. | 4 | 5 | 12 | 13 | 6 | 7 | 14 | 15 | Index
  433. +------+------+------+------+------+------+------+------+
  434. | 0000 | 0011 | 0000 | 0111 | 0000 | 0100 | 0000 | 1000 | Binary
  435. +------+------+------+------+------+------+------+------+
  436. | 3 | 7 | 4 | 8 | Number
  437. +------+------+------+------+------+------+-------------+
  438. */
  439. /* Constants for use with _mm_prefetch. */
  440. enum _mm_hint {
  441. _MM_HINT_NTA = 0, /* load data to L1 and L2 cache, mark it as NTA */
  442. _MM_HINT_T0 = 1, /* load data to L1 and L2 cache */
  443. _MM_HINT_T1 = 2, /* load data to L2 cache only */
  444. _MM_HINT_T2 = 3, /* load data to L2 cache only, mark it as NTA */
  445. _MM_HINT_ENTA = 4, /* exclusive version of _MM_HINT_NTA */
  446. _MM_HINT_ET0 = 5, /* exclusive version of _MM_HINT_T0 */
  447. _MM_HINT_ET1 = 6, /* exclusive version of _MM_HINT_T1 */
  448. _MM_HINT_ET2 = 7 /* exclusive version of _MM_HINT_T2 */
  449. };
  450. // The bit field mapping to the FPCR(floating-point control register)
  451. typedef struct {
  452. uint16_t res0;
  453. uint8_t res1 : 6;
  454. uint8_t bit22 : 1;
  455. uint8_t bit23 : 1;
  456. uint8_t bit24 : 1;
  457. uint8_t res2 : 7;
  458. #if defined(__aarch64__)
  459. uint32_t res3;
  460. #endif
  461. } fpcr_bitfield;
  462. // Takes the upper 64 bits of a and places it in the low end of the result
  463. // Takes the lower 64 bits of b and places it into the high end of the result.
  464. FORCE_INLINE __m128 _mm_shuffle_ps_1032(__m128 a, __m128 b)
  465. {
  466. float32x2_t a32 = vget_high_f32(vreinterpretq_f32_m128(a));
  467. float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(b));
  468. return vreinterpretq_m128_f32(vcombine_f32(a32, b10));
  469. }
  470. // takes the lower two 32-bit values from a and swaps them and places in high
  471. // end of result takes the higher two 32 bit values from b and swaps them and
  472. // places in low end of result.
  473. FORCE_INLINE __m128 _mm_shuffle_ps_2301(__m128 a, __m128 b)
  474. {
  475. float32x2_t a01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(a)));
  476. float32x2_t b23 = vrev64_f32(vget_high_f32(vreinterpretq_f32_m128(b)));
  477. return vreinterpretq_m128_f32(vcombine_f32(a01, b23));
  478. }
  479. FORCE_INLINE __m128 _mm_shuffle_ps_0321(__m128 a, __m128 b)
  480. {
  481. float32x2_t a21 = vget_high_f32(
  482. vextq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a), 3));
  483. float32x2_t b03 = vget_low_f32(
  484. vextq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b), 3));
  485. return vreinterpretq_m128_f32(vcombine_f32(a21, b03));
  486. }
  487. FORCE_INLINE __m128 _mm_shuffle_ps_2103(__m128 a, __m128 b)
  488. {
  489. float32x2_t a03 = vget_low_f32(
  490. vextq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a), 3));
  491. float32x2_t b21 = vget_high_f32(
  492. vextq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b), 3));
  493. return vreinterpretq_m128_f32(vcombine_f32(a03, b21));
  494. }
  495. FORCE_INLINE __m128 _mm_shuffle_ps_1010(__m128 a, __m128 b)
  496. {
  497. float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(a));
  498. float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(b));
  499. return vreinterpretq_m128_f32(vcombine_f32(a10, b10));
  500. }
  501. FORCE_INLINE __m128 _mm_shuffle_ps_1001(__m128 a, __m128 b)
  502. {
  503. float32x2_t a01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(a)));
  504. float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(b));
  505. return vreinterpretq_m128_f32(vcombine_f32(a01, b10));
  506. }
  507. FORCE_INLINE __m128 _mm_shuffle_ps_0101(__m128 a, __m128 b)
  508. {
  509. float32x2_t a01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(a)));
  510. float32x2_t b01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(b)));
  511. return vreinterpretq_m128_f32(vcombine_f32(a01, b01));
  512. }
  513. // keeps the low 64 bits of b in the low and puts the high 64 bits of a in the
  514. // high
  515. FORCE_INLINE __m128 _mm_shuffle_ps_3210(__m128 a, __m128 b)
  516. {
  517. float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(a));
  518. float32x2_t b32 = vget_high_f32(vreinterpretq_f32_m128(b));
  519. return vreinterpretq_m128_f32(vcombine_f32(a10, b32));
  520. }
  521. FORCE_INLINE __m128 _mm_shuffle_ps_0011(__m128 a, __m128 b)
  522. {
  523. float32x2_t a11 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(a)), 1);
  524. float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0);
  525. return vreinterpretq_m128_f32(vcombine_f32(a11, b00));
  526. }
  527. FORCE_INLINE __m128 _mm_shuffle_ps_0022(__m128 a, __m128 b)
  528. {
  529. float32x2_t a22 =
  530. vdup_lane_f32(vget_high_f32(vreinterpretq_f32_m128(a)), 0);
  531. float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0);
  532. return vreinterpretq_m128_f32(vcombine_f32(a22, b00));
  533. }
  534. FORCE_INLINE __m128 _mm_shuffle_ps_2200(__m128 a, __m128 b)
  535. {
  536. float32x2_t a00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(a)), 0);
  537. float32x2_t b22 =
  538. vdup_lane_f32(vget_high_f32(vreinterpretq_f32_m128(b)), 0);
  539. return vreinterpretq_m128_f32(vcombine_f32(a00, b22));
  540. }
  541. FORCE_INLINE __m128 _mm_shuffle_ps_3202(__m128 a, __m128 b)
  542. {
  543. float32_t a0 = vgetq_lane_f32(vreinterpretq_f32_m128(a), 0);
  544. float32x2_t a22 =
  545. vdup_lane_f32(vget_high_f32(vreinterpretq_f32_m128(a)), 0);
  546. float32x2_t a02 = vset_lane_f32(a0, a22, 1); /* TODO: use vzip ?*/
  547. float32x2_t b32 = vget_high_f32(vreinterpretq_f32_m128(b));
  548. return vreinterpretq_m128_f32(vcombine_f32(a02, b32));
  549. }
  550. FORCE_INLINE __m128 _mm_shuffle_ps_1133(__m128 a, __m128 b)
  551. {
  552. float32x2_t a33 =
  553. vdup_lane_f32(vget_high_f32(vreinterpretq_f32_m128(a)), 1);
  554. float32x2_t b11 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 1);
  555. return vreinterpretq_m128_f32(vcombine_f32(a33, b11));
  556. }
  557. FORCE_INLINE __m128 _mm_shuffle_ps_2010(__m128 a, __m128 b)
  558. {
  559. float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(a));
  560. float32_t b2 = vgetq_lane_f32(vreinterpretq_f32_m128(b), 2);
  561. float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0);
  562. float32x2_t b20 = vset_lane_f32(b2, b00, 1);
  563. return vreinterpretq_m128_f32(vcombine_f32(a10, b20));
  564. }
  565. FORCE_INLINE __m128 _mm_shuffle_ps_2001(__m128 a, __m128 b)
  566. {
  567. float32x2_t a01 = vrev64_f32(vget_low_f32(vreinterpretq_f32_m128(a)));
  568. float32_t b2 = vgetq_lane_f32(b, 2);
  569. float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0);
  570. float32x2_t b20 = vset_lane_f32(b2, b00, 1);
  571. return vreinterpretq_m128_f32(vcombine_f32(a01, b20));
  572. }
  573. FORCE_INLINE __m128 _mm_shuffle_ps_2032(__m128 a, __m128 b)
  574. {
  575. float32x2_t a32 = vget_high_f32(vreinterpretq_f32_m128(a));
  576. float32_t b2 = vgetq_lane_f32(b, 2);
  577. float32x2_t b00 = vdup_lane_f32(vget_low_f32(vreinterpretq_f32_m128(b)), 0);
  578. float32x2_t b20 = vset_lane_f32(b2, b00, 1);
  579. return vreinterpretq_m128_f32(vcombine_f32(a32, b20));
  580. }
  581. // Kahan summation for accurate summation of floating-point numbers.
  582. // http://blog.zachbjornson.com/2019/08/11/fast-float-summation.html
  583. FORCE_INLINE void _sse2neon_kadd_f32(float *sum, float *c, float y)
  584. {
  585. y -= *c;
  586. float t = *sum + y;
  587. *c = (t - *sum) - y;
  588. *sum = t;
  589. }
  590. #if defined(__ARM_FEATURE_CRYPTO)
  591. // Wraps vmull_p64
  592. FORCE_INLINE uint64x2_t _sse2neon_vmull_p64(uint64x1_t _a, uint64x1_t _b)
  593. {
  594. poly64_t a = vget_lane_p64(vreinterpret_p64_u64(_a), 0);
  595. poly64_t b = vget_lane_p64(vreinterpret_p64_u64(_b), 0);
  596. return vreinterpretq_u64_p128(vmull_p64(a, b));
  597. }
  598. #else // ARMv7 polyfill
  599. // ARMv7/some A64 lacks vmull_p64, but it has vmull_p8.
  600. //
  601. // vmull_p8 calculates 8 8-bit->16-bit polynomial multiplies, but we need a
  602. // 64-bit->128-bit polynomial multiply.
  603. //
  604. // It needs some work and is somewhat slow, but it is still faster than all
  605. // known scalar methods.
  606. //
  607. // Algorithm adapted to C from
  608. // https://www.workofard.com/2017/07/ghash-for-low-end-cores/, which is adapted
  609. // from "Fast Software Polynomial Multiplication on ARM Processors Using the
  610. // NEON Engine" by Danilo Camara, Conrado Gouvea, Julio Lopez and Ricardo Dahab
  611. // (https://hal.inria.fr/hal-01506572)
  612. static uint64x2_t _sse2neon_vmull_p64(uint64x1_t _a, uint64x1_t _b)
  613. {
  614. poly8x8_t a = vreinterpret_p8_u64(_a);
  615. poly8x8_t b = vreinterpret_p8_u64(_b);
  616. // Masks
  617. uint8x16_t k48_32 = vcombine_u8(vcreate_u8(0x0000ffffffffffff),
  618. vcreate_u8(0x00000000ffffffff));
  619. uint8x16_t k16_00 = vcombine_u8(vcreate_u8(0x000000000000ffff),
  620. vcreate_u8(0x0000000000000000));
  621. // Do the multiplies, rotating with vext to get all combinations
  622. uint8x16_t d = vreinterpretq_u8_p16(vmull_p8(a, b)); // D = A0 * B0
  623. uint8x16_t e =
  624. vreinterpretq_u8_p16(vmull_p8(a, vext_p8(b, b, 1))); // E = A0 * B1
  625. uint8x16_t f =
  626. vreinterpretq_u8_p16(vmull_p8(vext_p8(a, a, 1), b)); // F = A1 * B0
  627. uint8x16_t g =
  628. vreinterpretq_u8_p16(vmull_p8(a, vext_p8(b, b, 2))); // G = A0 * B2
  629. uint8x16_t h =
  630. vreinterpretq_u8_p16(vmull_p8(vext_p8(a, a, 2), b)); // H = A2 * B0
  631. uint8x16_t i =
  632. vreinterpretq_u8_p16(vmull_p8(a, vext_p8(b, b, 3))); // I = A0 * B3
  633. uint8x16_t j =
  634. vreinterpretq_u8_p16(vmull_p8(vext_p8(a, a, 3), b)); // J = A3 * B0
  635. uint8x16_t k =
  636. vreinterpretq_u8_p16(vmull_p8(a, vext_p8(b, b, 4))); // L = A0 * B4
  637. // Add cross products
  638. uint8x16_t l = veorq_u8(e, f); // L = E + F
  639. uint8x16_t m = veorq_u8(g, h); // M = G + H
  640. uint8x16_t n = veorq_u8(i, j); // N = I + J
  641. // Interleave. Using vzip1 and vzip2 prevents Clang from emitting TBL
  642. // instructions.
  643. #if defined(__aarch64__)
  644. uint8x16_t lm_p0 = vreinterpretq_u8_u64(
  645. vzip1q_u64(vreinterpretq_u64_u8(l), vreinterpretq_u64_u8(m)));
  646. uint8x16_t lm_p1 = vreinterpretq_u8_u64(
  647. vzip2q_u64(vreinterpretq_u64_u8(l), vreinterpretq_u64_u8(m)));
  648. uint8x16_t nk_p0 = vreinterpretq_u8_u64(
  649. vzip1q_u64(vreinterpretq_u64_u8(n), vreinterpretq_u64_u8(k)));
  650. uint8x16_t nk_p1 = vreinterpretq_u8_u64(
  651. vzip2q_u64(vreinterpretq_u64_u8(n), vreinterpretq_u64_u8(k)));
  652. #else
  653. uint8x16_t lm_p0 = vcombine_u8(vget_low_u8(l), vget_low_u8(m));
  654. uint8x16_t lm_p1 = vcombine_u8(vget_high_u8(l), vget_high_u8(m));
  655. uint8x16_t nk_p0 = vcombine_u8(vget_low_u8(n), vget_low_u8(k));
  656. uint8x16_t nk_p1 = vcombine_u8(vget_high_u8(n), vget_high_u8(k));
  657. #endif
  658. // t0 = (L) (P0 + P1) << 8
  659. // t1 = (M) (P2 + P3) << 16
  660. uint8x16_t t0t1_tmp = veorq_u8(lm_p0, lm_p1);
  661. uint8x16_t t0t1_h = vandq_u8(lm_p1, k48_32);
  662. uint8x16_t t0t1_l = veorq_u8(t0t1_tmp, t0t1_h);
  663. // t2 = (N) (P4 + P5) << 24
  664. // t3 = (K) (P6 + P7) << 32
  665. uint8x16_t t2t3_tmp = veorq_u8(nk_p0, nk_p1);
  666. uint8x16_t t2t3_h = vandq_u8(nk_p1, k16_00);
  667. uint8x16_t t2t3_l = veorq_u8(t2t3_tmp, t2t3_h);
  668. // De-interleave
  669. #if defined(__aarch64__)
  670. uint8x16_t t0 = vreinterpretq_u8_u64(
  671. vuzp1q_u64(vreinterpretq_u64_u8(t0t1_l), vreinterpretq_u64_u8(t0t1_h)));
  672. uint8x16_t t1 = vreinterpretq_u8_u64(
  673. vuzp2q_u64(vreinterpretq_u64_u8(t0t1_l), vreinterpretq_u64_u8(t0t1_h)));
  674. uint8x16_t t2 = vreinterpretq_u8_u64(
  675. vuzp1q_u64(vreinterpretq_u64_u8(t2t3_l), vreinterpretq_u64_u8(t2t3_h)));
  676. uint8x16_t t3 = vreinterpretq_u8_u64(
  677. vuzp2q_u64(vreinterpretq_u64_u8(t2t3_l), vreinterpretq_u64_u8(t2t3_h)));
  678. #else
  679. uint8x16_t t1 = vcombine_u8(vget_high_u8(t0t1_l), vget_high_u8(t0t1_h));
  680. uint8x16_t t0 = vcombine_u8(vget_low_u8(t0t1_l), vget_low_u8(t0t1_h));
  681. uint8x16_t t3 = vcombine_u8(vget_high_u8(t2t3_l), vget_high_u8(t2t3_h));
  682. uint8x16_t t2 = vcombine_u8(vget_low_u8(t2t3_l), vget_low_u8(t2t3_h));
  683. #endif
  684. // Shift the cross products
  685. uint8x16_t t0_shift = vextq_u8(t0, t0, 15); // t0 << 8
  686. uint8x16_t t1_shift = vextq_u8(t1, t1, 14); // t1 << 16
  687. uint8x16_t t2_shift = vextq_u8(t2, t2, 13); // t2 << 24
  688. uint8x16_t t3_shift = vextq_u8(t3, t3, 12); // t3 << 32
  689. // Accumulate the products
  690. uint8x16_t cross1 = veorq_u8(t0_shift, t1_shift);
  691. uint8x16_t cross2 = veorq_u8(t2_shift, t3_shift);
  692. uint8x16_t mix = veorq_u8(d, cross1);
  693. uint8x16_t r = veorq_u8(mix, cross2);
  694. return vreinterpretq_u64_u8(r);
  695. }
  696. #endif // ARMv7 polyfill
  697. // C equivalent:
  698. // __m128i _mm_shuffle_epi32_default(__m128i a,
  699. // __constrange(0, 255) int imm) {
  700. // __m128i ret;
  701. // ret[0] = a[imm & 0x3]; ret[1] = a[(imm >> 2) & 0x3];
  702. // ret[2] = a[(imm >> 4) & 0x03]; ret[3] = a[(imm >> 6) & 0x03];
  703. // return ret;
  704. // }
  705. #define _mm_shuffle_epi32_default(a, imm) \
  706. __extension__({ \
  707. int32x4_t ret; \
  708. ret = vmovq_n_s32( \
  709. vgetq_lane_s32(vreinterpretq_s32_m128i(a), (imm) & (0x3))); \
  710. ret = vsetq_lane_s32( \
  711. vgetq_lane_s32(vreinterpretq_s32_m128i(a), ((imm) >> 2) & 0x3), \
  712. ret, 1); \
  713. ret = vsetq_lane_s32( \
  714. vgetq_lane_s32(vreinterpretq_s32_m128i(a), ((imm) >> 4) & 0x3), \
  715. ret, 2); \
  716. ret = vsetq_lane_s32( \
  717. vgetq_lane_s32(vreinterpretq_s32_m128i(a), ((imm) >> 6) & 0x3), \
  718. ret, 3); \
  719. vreinterpretq_m128i_s32(ret); \
  720. })
  721. // Takes the upper 64 bits of a and places it in the low end of the result
  722. // Takes the lower 64 bits of a and places it into the high end of the result.
  723. FORCE_INLINE __m128i _mm_shuffle_epi_1032(__m128i a)
  724. {
  725. int32x2_t a32 = vget_high_s32(vreinterpretq_s32_m128i(a));
  726. int32x2_t a10 = vget_low_s32(vreinterpretq_s32_m128i(a));
  727. return vreinterpretq_m128i_s32(vcombine_s32(a32, a10));
  728. }
  729. // takes the lower two 32-bit values from a and swaps them and places in low end
  730. // of result takes the higher two 32 bit values from a and swaps them and places
  731. // in high end of result.
  732. FORCE_INLINE __m128i _mm_shuffle_epi_2301(__m128i a)
  733. {
  734. int32x2_t a01 = vrev64_s32(vget_low_s32(vreinterpretq_s32_m128i(a)));
  735. int32x2_t a23 = vrev64_s32(vget_high_s32(vreinterpretq_s32_m128i(a)));
  736. return vreinterpretq_m128i_s32(vcombine_s32(a01, a23));
  737. }
  738. // rotates the least significant 32 bits into the most significant 32 bits, and
  739. // shifts the rest down
  740. FORCE_INLINE __m128i _mm_shuffle_epi_0321(__m128i a)
  741. {
  742. return vreinterpretq_m128i_s32(
  743. vextq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(a), 1));
  744. }
  745. // rotates the most significant 32 bits into the least significant 32 bits, and
  746. // shifts the rest up
  747. FORCE_INLINE __m128i _mm_shuffle_epi_2103(__m128i a)
  748. {
  749. return vreinterpretq_m128i_s32(
  750. vextq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(a), 3));
  751. }
  752. // gets the lower 64 bits of a, and places it in the upper 64 bits
  753. // gets the lower 64 bits of a and places it in the lower 64 bits
  754. FORCE_INLINE __m128i _mm_shuffle_epi_1010(__m128i a)
  755. {
  756. int32x2_t a10 = vget_low_s32(vreinterpretq_s32_m128i(a));
  757. return vreinterpretq_m128i_s32(vcombine_s32(a10, a10));
  758. }
  759. // gets the lower 64 bits of a, swaps the 0 and 1 elements, and places it in the
  760. // lower 64 bits gets the lower 64 bits of a, and places it in the upper 64 bits
  761. FORCE_INLINE __m128i _mm_shuffle_epi_1001(__m128i a)
  762. {
  763. int32x2_t a01 = vrev64_s32(vget_low_s32(vreinterpretq_s32_m128i(a)));
  764. int32x2_t a10 = vget_low_s32(vreinterpretq_s32_m128i(a));
  765. return vreinterpretq_m128i_s32(vcombine_s32(a01, a10));
  766. }
  767. // gets the lower 64 bits of a, swaps the 0 and 1 elements and places it in the
  768. // upper 64 bits gets the lower 64 bits of a, swaps the 0 and 1 elements, and
  769. // places it in the lower 64 bits
  770. FORCE_INLINE __m128i _mm_shuffle_epi_0101(__m128i a)
  771. {
  772. int32x2_t a01 = vrev64_s32(vget_low_s32(vreinterpretq_s32_m128i(a)));
  773. return vreinterpretq_m128i_s32(vcombine_s32(a01, a01));
  774. }
  775. FORCE_INLINE __m128i _mm_shuffle_epi_2211(__m128i a)
  776. {
  777. int32x2_t a11 = vdup_lane_s32(vget_low_s32(vreinterpretq_s32_m128i(a)), 1);
  778. int32x2_t a22 = vdup_lane_s32(vget_high_s32(vreinterpretq_s32_m128i(a)), 0);
  779. return vreinterpretq_m128i_s32(vcombine_s32(a11, a22));
  780. }
  781. FORCE_INLINE __m128i _mm_shuffle_epi_0122(__m128i a)
  782. {
  783. int32x2_t a22 = vdup_lane_s32(vget_high_s32(vreinterpretq_s32_m128i(a)), 0);
  784. int32x2_t a01 = vrev64_s32(vget_low_s32(vreinterpretq_s32_m128i(a)));
  785. return vreinterpretq_m128i_s32(vcombine_s32(a22, a01));
  786. }
  787. FORCE_INLINE __m128i _mm_shuffle_epi_3332(__m128i a)
  788. {
  789. int32x2_t a32 = vget_high_s32(vreinterpretq_s32_m128i(a));
  790. int32x2_t a33 = vdup_lane_s32(vget_high_s32(vreinterpretq_s32_m128i(a)), 1);
  791. return vreinterpretq_m128i_s32(vcombine_s32(a32, a33));
  792. }
  793. // FORCE_INLINE __m128i _mm_shuffle_epi32_splat(__m128i a, __constrange(0,255)
  794. // int imm)
  795. #if defined(__aarch64__)
  796. #define _mm_shuffle_epi32_splat(a, imm) \
  797. __extension__({ \
  798. vreinterpretq_m128i_s32( \
  799. vdupq_laneq_s32(vreinterpretq_s32_m128i(a), (imm))); \
  800. })
  801. #else
  802. #define _mm_shuffle_epi32_splat(a, imm) \
  803. __extension__({ \
  804. vreinterpretq_m128i_s32( \
  805. vdupq_n_s32(vgetq_lane_s32(vreinterpretq_s32_m128i(a), (imm)))); \
  806. })
  807. #endif
  808. // NEON does not support a general purpose permute intrinsic
  809. // Selects four specific single-precision, floating-point values from a and b,
  810. // based on the mask i.
  811. //
  812. // C equivalent:
  813. // __m128 _mm_shuffle_ps_default(__m128 a, __m128 b,
  814. // __constrange(0, 255) int imm) {
  815. // __m128 ret;
  816. // ret[0] = a[imm & 0x3]; ret[1] = a[(imm >> 2) & 0x3];
  817. // ret[2] = b[(imm >> 4) & 0x03]; ret[3] = b[(imm >> 6) & 0x03];
  818. // return ret;
  819. // }
  820. //
  821. // https://msdn.microsoft.com/en-us/library/vstudio/5f0858x0(v=vs.100).aspx
  822. #define _mm_shuffle_ps_default(a, b, imm) \
  823. __extension__({ \
  824. float32x4_t ret; \
  825. ret = vmovq_n_f32( \
  826. vgetq_lane_f32(vreinterpretq_f32_m128(a), (imm) & (0x3))); \
  827. ret = vsetq_lane_f32( \
  828. vgetq_lane_f32(vreinterpretq_f32_m128(a), ((imm) >> 2) & 0x3), \
  829. ret, 1); \
  830. ret = vsetq_lane_f32( \
  831. vgetq_lane_f32(vreinterpretq_f32_m128(b), ((imm) >> 4) & 0x3), \
  832. ret, 2); \
  833. ret = vsetq_lane_f32( \
  834. vgetq_lane_f32(vreinterpretq_f32_m128(b), ((imm) >> 6) & 0x3), \
  835. ret, 3); \
  836. vreinterpretq_m128_f32(ret); \
  837. })
  838. // Shuffles the lower 4 signed or unsigned 16-bit integers in a as specified
  839. // by imm.
  840. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/y41dkk37(v=vs.100)
  841. // FORCE_INLINE __m128i _mm_shufflelo_epi16_function(__m128i a,
  842. // __constrange(0,255) int
  843. // imm)
  844. #define _mm_shufflelo_epi16_function(a, imm) \
  845. __extension__({ \
  846. int16x8_t ret = vreinterpretq_s16_m128i(a); \
  847. int16x4_t lowBits = vget_low_s16(ret); \
  848. ret = vsetq_lane_s16(vget_lane_s16(lowBits, (imm) & (0x3)), ret, 0); \
  849. ret = vsetq_lane_s16(vget_lane_s16(lowBits, ((imm) >> 2) & 0x3), ret, \
  850. 1); \
  851. ret = vsetq_lane_s16(vget_lane_s16(lowBits, ((imm) >> 4) & 0x3), ret, \
  852. 2); \
  853. ret = vsetq_lane_s16(vget_lane_s16(lowBits, ((imm) >> 6) & 0x3), ret, \
  854. 3); \
  855. vreinterpretq_m128i_s16(ret); \
  856. })
  857. // Shuffles the upper 4 signed or unsigned 16-bit integers in a as specified
  858. // by imm.
  859. // https://msdn.microsoft.com/en-us/library/13ywktbs(v=vs.100).aspx
  860. // FORCE_INLINE __m128i _mm_shufflehi_epi16_function(__m128i a,
  861. // __constrange(0,255) int
  862. // imm)
  863. #define _mm_shufflehi_epi16_function(a, imm) \
  864. __extension__({ \
  865. int16x8_t ret = vreinterpretq_s16_m128i(a); \
  866. int16x4_t highBits = vget_high_s16(ret); \
  867. ret = vsetq_lane_s16(vget_lane_s16(highBits, (imm) & (0x3)), ret, 4); \
  868. ret = vsetq_lane_s16(vget_lane_s16(highBits, ((imm) >> 2) & 0x3), ret, \
  869. 5); \
  870. ret = vsetq_lane_s16(vget_lane_s16(highBits, ((imm) >> 4) & 0x3), ret, \
  871. 6); \
  872. ret = vsetq_lane_s16(vget_lane_s16(highBits, ((imm) >> 6) & 0x3), ret, \
  873. 7); \
  874. vreinterpretq_m128i_s16(ret); \
  875. })
  876. /* SSE */
  877. // Adds the four single-precision, floating-point values of a and b.
  878. //
  879. // r0 := a0 + b0
  880. // r1 := a1 + b1
  881. // r2 := a2 + b2
  882. // r3 := a3 + b3
  883. //
  884. // https://msdn.microsoft.com/en-us/library/vstudio/c9848chc(v=vs.100).aspx
  885. FORCE_INLINE __m128 _mm_add_ps(__m128 a, __m128 b)
  886. {
  887. return vreinterpretq_m128_f32(
  888. vaddq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  889. }
  890. // adds the scalar single-precision floating point values of a and b.
  891. // https://msdn.microsoft.com/en-us/library/be94x2y6(v=vs.100).aspx
  892. FORCE_INLINE __m128 _mm_add_ss(__m128 a, __m128 b)
  893. {
  894. float32_t b0 = vgetq_lane_f32(vreinterpretq_f32_m128(b), 0);
  895. float32x4_t value = vsetq_lane_f32(b0, vdupq_n_f32(0), 0);
  896. // the upper values in the result must be the remnants of <a>.
  897. return vreinterpretq_m128_f32(vaddq_f32(a, value));
  898. }
  899. // Computes the bitwise AND of the four single-precision, floating-point values
  900. // of a and b.
  901. //
  902. // r0 := a0 & b0
  903. // r1 := a1 & b1
  904. // r2 := a2 & b2
  905. // r3 := a3 & b3
  906. //
  907. // https://msdn.microsoft.com/en-us/library/vstudio/73ck1xc5(v=vs.100).aspx
  908. FORCE_INLINE __m128 _mm_and_ps(__m128 a, __m128 b)
  909. {
  910. return vreinterpretq_m128_s32(
  911. vandq_s32(vreinterpretq_s32_m128(a), vreinterpretq_s32_m128(b)));
  912. }
  913. // Computes the bitwise AND-NOT of the four single-precision, floating-point
  914. // values of a and b.
  915. //
  916. // r0 := ~a0 & b0
  917. // r1 := ~a1 & b1
  918. // r2 := ~a2 & b2
  919. // r3 := ~a3 & b3
  920. //
  921. // https://msdn.microsoft.com/en-us/library/vstudio/68h7wd02(v=vs.100).aspx
  922. FORCE_INLINE __m128 _mm_andnot_ps(__m128 a, __m128 b)
  923. {
  924. return vreinterpretq_m128_s32(
  925. vbicq_s32(vreinterpretq_s32_m128(b),
  926. vreinterpretq_s32_m128(a))); // *NOTE* argument swap
  927. }
  928. // Average packed unsigned 16-bit integers in a and b, and store the results in
  929. // dst.
  930. //
  931. // FOR j := 0 to 3
  932. // i := j*16
  933. // dst[i+15:i] := (a[i+15:i] + b[i+15:i] + 1) >> 1
  934. // ENDFOR
  935. //
  936. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_avg_pu16
  937. FORCE_INLINE __m64 _mm_avg_pu16(__m64 a, __m64 b)
  938. {
  939. return vreinterpret_m64_u16(
  940. vrhadd_u16(vreinterpret_u16_m64(a), vreinterpret_u16_m64(b)));
  941. }
  942. // Average packed unsigned 8-bit integers in a and b, and store the results in
  943. // dst.
  944. //
  945. // FOR j := 0 to 7
  946. // i := j*8
  947. // dst[i+7:i] := (a[i+7:i] + b[i+7:i] + 1) >> 1
  948. // ENDFOR
  949. //
  950. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_avg_pu8
  951. FORCE_INLINE __m64 _mm_avg_pu8(__m64 a, __m64 b)
  952. {
  953. return vreinterpret_m64_u8(
  954. vrhadd_u8(vreinterpret_u8_m64(a), vreinterpret_u8_m64(b)));
  955. }
  956. // Compares for equality.
  957. // https://msdn.microsoft.com/en-us/library/vstudio/36aectz5(v=vs.100).aspx
  958. FORCE_INLINE __m128 _mm_cmpeq_ps(__m128 a, __m128 b)
  959. {
  960. return vreinterpretq_m128_u32(
  961. vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  962. }
  963. // Compares for equality.
  964. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/k423z28e(v=vs.100)
  965. FORCE_INLINE __m128 _mm_cmpeq_ss(__m128 a, __m128 b)
  966. {
  967. return _mm_move_ss(a, _mm_cmpeq_ps(a, b));
  968. }
  969. // Compares for greater than or equal.
  970. // https://msdn.microsoft.com/en-us/library/vstudio/fs813y2t(v=vs.100).aspx
  971. FORCE_INLINE __m128 _mm_cmpge_ps(__m128 a, __m128 b)
  972. {
  973. return vreinterpretq_m128_u32(
  974. vcgeq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  975. }
  976. // Compares for greater than or equal.
  977. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/kesh3ddc(v=vs.100)
  978. FORCE_INLINE __m128 _mm_cmpge_ss(__m128 a, __m128 b)
  979. {
  980. return _mm_move_ss(a, _mm_cmpge_ps(a, b));
  981. }
  982. // Compares for greater than.
  983. //
  984. // r0 := (a0 > b0) ? 0xffffffff : 0x0
  985. // r1 := (a1 > b1) ? 0xffffffff : 0x0
  986. // r2 := (a2 > b2) ? 0xffffffff : 0x0
  987. // r3 := (a3 > b3) ? 0xffffffff : 0x0
  988. //
  989. // https://msdn.microsoft.com/en-us/library/vstudio/11dy102s(v=vs.100).aspx
  990. FORCE_INLINE __m128 _mm_cmpgt_ps(__m128 a, __m128 b)
  991. {
  992. return vreinterpretq_m128_u32(
  993. vcgtq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  994. }
  995. // Compares for greater than.
  996. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/1xyyyy9e(v=vs.100)
  997. FORCE_INLINE __m128 _mm_cmpgt_ss(__m128 a, __m128 b)
  998. {
  999. return _mm_move_ss(a, _mm_cmpgt_ps(a, b));
  1000. }
  1001. // Compares for less than or equal.
  1002. //
  1003. // r0 := (a0 <= b0) ? 0xffffffff : 0x0
  1004. // r1 := (a1 <= b1) ? 0xffffffff : 0x0
  1005. // r2 := (a2 <= b2) ? 0xffffffff : 0x0
  1006. // r3 := (a3 <= b3) ? 0xffffffff : 0x0
  1007. //
  1008. // https://msdn.microsoft.com/en-us/library/vstudio/1s75w83z(v=vs.100).aspx
  1009. FORCE_INLINE __m128 _mm_cmple_ps(__m128 a, __m128 b)
  1010. {
  1011. return vreinterpretq_m128_u32(
  1012. vcleq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  1013. }
  1014. // Compares for less than or equal.
  1015. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/a7x0hbhw(v=vs.100)
  1016. FORCE_INLINE __m128 _mm_cmple_ss(__m128 a, __m128 b)
  1017. {
  1018. return _mm_move_ss(a, _mm_cmple_ps(a, b));
  1019. }
  1020. // Compares for less than
  1021. // https://msdn.microsoft.com/en-us/library/vstudio/f330yhc8(v=vs.100).aspx
  1022. FORCE_INLINE __m128 _mm_cmplt_ps(__m128 a, __m128 b)
  1023. {
  1024. return vreinterpretq_m128_u32(
  1025. vcltq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  1026. }
  1027. // Compares for less than
  1028. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/fy94wye7(v=vs.100)
  1029. FORCE_INLINE __m128 _mm_cmplt_ss(__m128 a, __m128 b)
  1030. {
  1031. return _mm_move_ss(a, _mm_cmplt_ps(a, b));
  1032. }
  1033. // Compares for inequality.
  1034. // https://msdn.microsoft.com/en-us/library/sf44thbx(v=vs.100).aspx
  1035. FORCE_INLINE __m128 _mm_cmpneq_ps(__m128 a, __m128 b)
  1036. {
  1037. return vreinterpretq_m128_u32(vmvnq_u32(
  1038. vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))));
  1039. }
  1040. // Compares for inequality.
  1041. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ekya8fh4(v=vs.100)
  1042. FORCE_INLINE __m128 _mm_cmpneq_ss(__m128 a, __m128 b)
  1043. {
  1044. return _mm_move_ss(a, _mm_cmpneq_ps(a, b));
  1045. }
  1046. // Compares for not greater than or equal.
  1047. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/wsexys62(v=vs.100)
  1048. FORCE_INLINE __m128 _mm_cmpnge_ps(__m128 a, __m128 b)
  1049. {
  1050. return vreinterpretq_m128_u32(vmvnq_u32(
  1051. vcgeq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))));
  1052. }
  1053. // Compares for not greater than or equal.
  1054. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/fk2y80s8(v=vs.100)
  1055. FORCE_INLINE __m128 _mm_cmpnge_ss(__m128 a, __m128 b)
  1056. {
  1057. return _mm_move_ss(a, _mm_cmpnge_ps(a, b));
  1058. }
  1059. // Compares for not greater than.
  1060. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/d0xh7w0s(v=vs.100)
  1061. FORCE_INLINE __m128 _mm_cmpngt_ps(__m128 a, __m128 b)
  1062. {
  1063. return vreinterpretq_m128_u32(vmvnq_u32(
  1064. vcgtq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))));
  1065. }
  1066. // Compares for not greater than.
  1067. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/z7x9ydwh(v=vs.100)
  1068. FORCE_INLINE __m128 _mm_cmpngt_ss(__m128 a, __m128 b)
  1069. {
  1070. return _mm_move_ss(a, _mm_cmpngt_ps(a, b));
  1071. }
  1072. // Compares for not less than or equal.
  1073. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/6a330kxw(v=vs.100)
  1074. FORCE_INLINE __m128 _mm_cmpnle_ps(__m128 a, __m128 b)
  1075. {
  1076. return vreinterpretq_m128_u32(vmvnq_u32(
  1077. vcleq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))));
  1078. }
  1079. // Compares for not less than or equal.
  1080. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/z7x9ydwh(v=vs.100)
  1081. FORCE_INLINE __m128 _mm_cmpnle_ss(__m128 a, __m128 b)
  1082. {
  1083. return _mm_move_ss(a, _mm_cmpnle_ps(a, b));
  1084. }
  1085. // Compares for not less than.
  1086. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/4686bbdw(v=vs.100)
  1087. FORCE_INLINE __m128 _mm_cmpnlt_ps(__m128 a, __m128 b)
  1088. {
  1089. return vreinterpretq_m128_u32(vmvnq_u32(
  1090. vcltq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))));
  1091. }
  1092. // Compares for not less than.
  1093. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/56b9z2wf(v=vs.100)
  1094. FORCE_INLINE __m128 _mm_cmpnlt_ss(__m128 a, __m128 b)
  1095. {
  1096. return _mm_move_ss(a, _mm_cmpnlt_ps(a, b));
  1097. }
  1098. // Compares the four 32-bit floats in a and b to check if any values are NaN.
  1099. // Ordered compare between each value returns true for "orderable" and false for
  1100. // "not orderable" (NaN).
  1101. // https://msdn.microsoft.com/en-us/library/vstudio/0h9w00fx(v=vs.100).aspx see
  1102. // also:
  1103. // http://stackoverflow.com/questions/8627331/what-does-ordered-unordered-comparison-mean
  1104. // http://stackoverflow.com/questions/29349621/neon-isnanval-intrinsics
  1105. FORCE_INLINE __m128 _mm_cmpord_ps(__m128 a, __m128 b)
  1106. {
  1107. // Note: NEON does not have ordered compare builtin
  1108. // Need to compare a eq a and b eq b to check for NaN
  1109. // Do AND of results to get final
  1110. uint32x4_t ceqaa =
  1111. vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a));
  1112. uint32x4_t ceqbb =
  1113. vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b));
  1114. return vreinterpretq_m128_u32(vandq_u32(ceqaa, ceqbb));
  1115. }
  1116. // Compares for ordered.
  1117. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/343t62da(v=vs.100)
  1118. FORCE_INLINE __m128 _mm_cmpord_ss(__m128 a, __m128 b)
  1119. {
  1120. return _mm_move_ss(a, _mm_cmpord_ps(a, b));
  1121. }
  1122. // Compares for unordered.
  1123. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/khy6fk1t(v=vs.100)
  1124. FORCE_INLINE __m128 _mm_cmpunord_ps(__m128 a, __m128 b)
  1125. {
  1126. uint32x4_t f32a =
  1127. vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a));
  1128. uint32x4_t f32b =
  1129. vceqq_f32(vreinterpretq_f32_m128(b), vreinterpretq_f32_m128(b));
  1130. return vreinterpretq_m128_u32(vmvnq_u32(vandq_u32(f32a, f32b)));
  1131. }
  1132. // Compares for unordered.
  1133. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/2as2387b(v=vs.100)
  1134. FORCE_INLINE __m128 _mm_cmpunord_ss(__m128 a, __m128 b)
  1135. {
  1136. return _mm_move_ss(a, _mm_cmpunord_ps(a, b));
  1137. }
  1138. // Compares the lower single-precision floating point scalar values of a and b
  1139. // using an equality operation. :
  1140. // https://msdn.microsoft.com/en-us/library/93yx2h2b(v=vs.100).aspx
  1141. FORCE_INLINE int _mm_comieq_ss(__m128 a, __m128 b)
  1142. {
  1143. uint32x4_t a_eq_b =
  1144. vceqq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b));
  1145. return vgetq_lane_u32(a_eq_b, 0) & 0x1;
  1146. }
  1147. // Compares the lower single-precision floating point scalar values of a and b
  1148. // using a greater than or equal operation. :
  1149. // https://msdn.microsoft.com/en-us/library/8t80des6(v=vs.100).aspx
  1150. FORCE_INLINE int _mm_comige_ss(__m128 a, __m128 b)
  1151. {
  1152. uint32x4_t a_ge_b =
  1153. vcgeq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b));
  1154. return vgetq_lane_u32(a_ge_b, 0) & 0x1;
  1155. }
  1156. // Compares the lower single-precision floating point scalar values of a and b
  1157. // using a greater than operation. :
  1158. // https://msdn.microsoft.com/en-us/library/b0738e0t(v=vs.100).aspx
  1159. FORCE_INLINE int _mm_comigt_ss(__m128 a, __m128 b)
  1160. {
  1161. uint32x4_t a_gt_b =
  1162. vcgtq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b));
  1163. return vgetq_lane_u32(a_gt_b, 0) & 0x1;
  1164. }
  1165. // Compares the lower single-precision floating point scalar values of a and b
  1166. // using a less than or equal operation. :
  1167. // https://msdn.microsoft.com/en-us/library/1w4t7c57(v=vs.90).aspx
  1168. FORCE_INLINE int _mm_comile_ss(__m128 a, __m128 b)
  1169. {
  1170. uint32x4_t a_le_b =
  1171. vcleq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b));
  1172. return vgetq_lane_u32(a_le_b, 0) & 0x1;
  1173. }
  1174. // Compares the lower single-precision floating point scalar values of a and b
  1175. // using a less than operation. :
  1176. // https://msdn.microsoft.com/en-us/library/2kwe606b(v=vs.90).aspx Important
  1177. // note!! The documentation on MSDN is incorrect! If either of the values is a
  1178. // NAN the docs say you will get a one, but in fact, it will return a zero!!
  1179. FORCE_INLINE int _mm_comilt_ss(__m128 a, __m128 b)
  1180. {
  1181. uint32x4_t a_lt_b =
  1182. vcltq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b));
  1183. return vgetq_lane_u32(a_lt_b, 0) & 0x1;
  1184. }
  1185. // Compares the lower single-precision floating point scalar values of a and b
  1186. // using an inequality operation. :
  1187. // https://msdn.microsoft.com/en-us/library/bafh5e0a(v=vs.90).aspx
  1188. FORCE_INLINE int _mm_comineq_ss(__m128 a, __m128 b)
  1189. {
  1190. return !_mm_comieq_ss(a, b);
  1191. }
  1192. // Convert packed signed 32-bit integers in b to packed single-precision
  1193. // (32-bit) floating-point elements, store the results in the lower 2 elements
  1194. // of dst, and copy the upper 2 packed elements from a to the upper elements of
  1195. // dst.
  1196. //
  1197. // dst[31:0] := Convert_Int32_To_FP32(b[31:0])
  1198. // dst[63:32] := Convert_Int32_To_FP32(b[63:32])
  1199. // dst[95:64] := a[95:64]
  1200. // dst[127:96] := a[127:96]
  1201. //
  1202. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvt_pi2ps
  1203. FORCE_INLINE __m128 _mm_cvt_pi2ps(__m128 a, __m64 b)
  1204. {
  1205. return vreinterpretq_m128_f32(
  1206. vcombine_f32(vcvt_f32_s32(vreinterpret_s32_m64(b)),
  1207. vget_high_f32(vreinterpretq_f32_m128(a))));
  1208. }
  1209. // Convert packed single-precision (32-bit) floating-point elements in a to
  1210. // packed 32-bit integers, and store the results in dst.
  1211. //
  1212. // FOR j := 0 to 1
  1213. // i := 32*j
  1214. // dst[i+31:i] := Convert_FP32_To_Int32(a[i+31:i])
  1215. // ENDFOR
  1216. //
  1217. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvt_ps2pi
  1218. FORCE_INLINE __m64 _mm_cvt_ps2pi(__m128 a)
  1219. {
  1220. #if defined(__aarch64__)
  1221. return vreinterpret_m64_s32(
  1222. vget_low_s32(vcvtnq_s32_f32(vrndiq_f32(vreinterpretq_f32_m128(a)))));
  1223. #else
  1224. return vreinterpret_m64_s32(vcvt_s32_f32(vget_low_f32(
  1225. vreinterpretq_f32_m128(_mm_round_ps(a, _MM_FROUND_CUR_DIRECTION)))));
  1226. #endif
  1227. }
  1228. // Convert the signed 32-bit integer b to a single-precision (32-bit)
  1229. // floating-point element, store the result in the lower element of dst, and
  1230. // copy the upper 3 packed elements from a to the upper elements of dst.
  1231. //
  1232. // dst[31:0] := Convert_Int32_To_FP32(b[31:0])
  1233. // dst[127:32] := a[127:32]
  1234. //
  1235. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvt_si2ss
  1236. FORCE_INLINE __m128 _mm_cvt_si2ss(__m128 a, int b)
  1237. {
  1238. return vreinterpretq_m128_f32(
  1239. vsetq_lane_f32((float) b, vreinterpretq_f32_m128(a), 0));
  1240. }
  1241. // Convert the lower single-precision (32-bit) floating-point element in a to a
  1242. // 32-bit integer, and store the result in dst.
  1243. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvt_ss2si
  1244. FORCE_INLINE int _mm_cvt_ss2si(__m128 a)
  1245. {
  1246. #if defined(__aarch64__)
  1247. return vgetq_lane_s32(vcvtnq_s32_f32(vrndiq_f32(vreinterpretq_f32_m128(a))),
  1248. 0);
  1249. #else
  1250. float32_t data = vgetq_lane_f32(
  1251. vreinterpretq_f32_m128(_mm_round_ps(a, _MM_FROUND_CUR_DIRECTION)), 0);
  1252. return (int32_t) data;
  1253. #endif
  1254. }
  1255. // Convert packed 16-bit integers in a to packed single-precision (32-bit)
  1256. // floating-point elements, and store the results in dst.
  1257. //
  1258. // FOR j := 0 to 3
  1259. // i := j*16
  1260. // m := j*32
  1261. // dst[m+31:m] := Convert_Int16_To_FP32(a[i+15:i])
  1262. // ENDFOR
  1263. //
  1264. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpi16_ps
  1265. FORCE_INLINE __m128 _mm_cvtpi16_ps(__m64 a)
  1266. {
  1267. return vreinterpretq_m128_f32(
  1268. vcvtq_f32_s32(vmovl_s16(vreinterpret_s16_m64(a))));
  1269. }
  1270. // Convert packed 32-bit integers in b to packed single-precision (32-bit)
  1271. // floating-point elements, store the results in the lower 2 elements of dst,
  1272. // and copy the upper 2 packed elements from a to the upper elements of dst.
  1273. //
  1274. // dst[31:0] := Convert_Int32_To_FP32(b[31:0])
  1275. // dst[63:32] := Convert_Int32_To_FP32(b[63:32])
  1276. // dst[95:64] := a[95:64]
  1277. // dst[127:96] := a[127:96]
  1278. //
  1279. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpi32_ps
  1280. FORCE_INLINE __m128 _mm_cvtpi32_ps(__m128 a, __m64 b)
  1281. {
  1282. return vreinterpretq_m128_f32(
  1283. vcombine_f32(vcvt_f32_s32(vreinterpret_s32_m64(b)),
  1284. vget_high_f32(vreinterpretq_f32_m128(a))));
  1285. }
  1286. // Convert packed signed 32-bit integers in a to packed single-precision
  1287. // (32-bit) floating-point elements, store the results in the lower 2 elements
  1288. // of dst, then covert the packed signed 32-bit integers in b to
  1289. // single-precision (32-bit) floating-point element, and store the results in
  1290. // the upper 2 elements of dst.
  1291. //
  1292. // dst[31:0] := Convert_Int32_To_FP32(a[31:0])
  1293. // dst[63:32] := Convert_Int32_To_FP32(a[63:32])
  1294. // dst[95:64] := Convert_Int32_To_FP32(b[31:0])
  1295. // dst[127:96] := Convert_Int32_To_FP32(b[63:32])
  1296. //
  1297. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpi32x2_ps
  1298. FORCE_INLINE __m128 _mm_cvtpi32x2_ps(__m64 a, __m64 b)
  1299. {
  1300. return vreinterpretq_m128_f32(vcvtq_f32_s32(
  1301. vcombine_s32(vreinterpret_s32_m64(a), vreinterpret_s32_m64(b))));
  1302. }
  1303. // Convert the lower packed 8-bit integers in a to packed single-precision
  1304. // (32-bit) floating-point elements, and store the results in dst.
  1305. //
  1306. // FOR j := 0 to 3
  1307. // i := j*8
  1308. // m := j*32
  1309. // dst[m+31:m] := Convert_Int8_To_FP32(a[i+7:i])
  1310. // ENDFOR
  1311. //
  1312. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpi8_ps
  1313. FORCE_INLINE __m128 _mm_cvtpi8_ps(__m64 a)
  1314. {
  1315. return vreinterpretq_m128_f32(vcvtq_f32_s32(
  1316. vmovl_s16(vget_low_s16(vmovl_s8(vreinterpret_s8_m64(a))))));
  1317. }
  1318. // Convert packed single-precision (32-bit) floating-point elements in a to
  1319. // packed 16-bit integers, and store the results in dst. Note: this intrinsic
  1320. // will generate 0x7FFF, rather than 0x8000, for input values between 0x7FFF and
  1321. // 0x7FFFFFFF.
  1322. //
  1323. // FOR j := 0 to 3
  1324. // i := 16*j
  1325. // k := 32*j
  1326. // IF a[k+31:k] >= FP32(0x7FFF) && a[k+31:k] <= FP32(0x7FFFFFFF)
  1327. // dst[i+15:i] := 0x7FFF
  1328. // ELSE
  1329. // dst[i+15:i] := Convert_FP32_To_Int16(a[k+31:k])
  1330. // FI
  1331. // ENDFOR
  1332. //
  1333. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtps_pi16
  1334. FORCE_INLINE __m64 _mm_cvtps_pi16(__m128 a)
  1335. {
  1336. const __m128 i16Min = _mm_set_ps1((float) INT16_MIN);
  1337. const __m128 i16Max = _mm_set_ps1((float) INT16_MAX);
  1338. const __m128 i32Max = _mm_set_ps1((float) INT32_MAX);
  1339. const __m128i maxMask = _mm_castps_si128(
  1340. _mm_and_ps(_mm_cmpge_ps(a, i16Max), _mm_cmple_ps(a, i32Max)));
  1341. const __m128i betweenMask = _mm_castps_si128(
  1342. _mm_and_ps(_mm_cmpgt_ps(a, i16Min), _mm_cmplt_ps(a, i16Max)));
  1343. const __m128i minMask = _mm_cmpeq_epi32(_mm_or_si128(maxMask, betweenMask),
  1344. _mm_setzero_si128());
  1345. __m128i max = _mm_and_si128(maxMask, _mm_set1_epi32(INT16_MAX));
  1346. __m128i min = _mm_and_si128(minMask, _mm_set1_epi32(INT16_MIN));
  1347. __m128i cvt = _mm_and_si128(betweenMask, _mm_cvtps_epi32(a));
  1348. __m128i res32 = _mm_or_si128(_mm_or_si128(max, min), cvt);
  1349. return vreinterpret_m64_s16(vmovn_s32(vreinterpretq_s32_m128i(res32)));
  1350. }
  1351. // Convert packed single-precision (32-bit) floating-point elements in a to
  1352. // packed 32-bit integers, and store the results in dst.
  1353. //
  1354. // FOR j := 0 to 1
  1355. // i := 32*j
  1356. // dst[i+31:i] := Convert_FP32_To_Int32(a[i+31:i])
  1357. // ENDFOR
  1358. //
  1359. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtps_pi32
  1360. #define _mm_cvtps_pi32(a) _mm_cvt_ps2pi(a)
  1361. // Convert packed single-precision (32-bit) floating-point elements in a to
  1362. // packed 8-bit integers, and store the results in lower 4 elements of dst.
  1363. // Note: this intrinsic will generate 0x7F, rather than 0x80, for input values
  1364. // between 0x7F and 0x7FFFFFFF.
  1365. //
  1366. // FOR j := 0 to 3
  1367. // i := 8*j
  1368. // k := 32*j
  1369. // IF a[k+31:k] >= FP32(0x7F) && a[k+31:k] <= FP32(0x7FFFFFFF)
  1370. // dst[i+7:i] := 0x7F
  1371. // ELSE
  1372. // dst[i+7:i] := Convert_FP32_To_Int8(a[k+31:k])
  1373. // FI
  1374. // ENDFOR
  1375. //
  1376. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtps_pi8
  1377. FORCE_INLINE __m64 _mm_cvtps_pi8(__m128 a)
  1378. {
  1379. const __m128 i8Min = _mm_set_ps1((float) INT8_MIN);
  1380. const __m128 i8Max = _mm_set_ps1((float) INT8_MAX);
  1381. const __m128 i32Max = _mm_set_ps1((float) INT32_MAX);
  1382. const __m128i maxMask = _mm_castps_si128(
  1383. _mm_and_ps(_mm_cmpge_ps(a, i8Max), _mm_cmple_ps(a, i32Max)));
  1384. const __m128i betweenMask = _mm_castps_si128(
  1385. _mm_and_ps(_mm_cmpgt_ps(a, i8Min), _mm_cmplt_ps(a, i8Max)));
  1386. const __m128i minMask = _mm_cmpeq_epi32(_mm_or_si128(maxMask, betweenMask),
  1387. _mm_setzero_si128());
  1388. __m128i max = _mm_and_si128(maxMask, _mm_set1_epi32(INT8_MAX));
  1389. __m128i min = _mm_and_si128(minMask, _mm_set1_epi32(INT8_MIN));
  1390. __m128i cvt = _mm_and_si128(betweenMask, _mm_cvtps_epi32(a));
  1391. __m128i res32 = _mm_or_si128(_mm_or_si128(max, min), cvt);
  1392. int16x4_t res16 = vmovn_s32(vreinterpretq_s32_m128i(res32));
  1393. int8x8_t res8 = vmovn_s16(vcombine_s16(res16, res16));
  1394. uint32_t bitMask[2] = {0xFFFFFFFF, 0};
  1395. int8x8_t mask = vreinterpret_s8_u32(vld1_u32(bitMask));
  1396. return vreinterpret_m64_s8(vorr_s8(vand_s8(mask, res8), vdup_n_s8(0)));
  1397. }
  1398. // Convert packed unsigned 16-bit integers in a to packed single-precision
  1399. // (32-bit) floating-point elements, and store the results in dst.
  1400. //
  1401. // FOR j := 0 to 3
  1402. // i := j*16
  1403. // m := j*32
  1404. // dst[m+31:m] := Convert_UInt16_To_FP32(a[i+15:i])
  1405. // ENDFOR
  1406. //
  1407. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpu16_ps
  1408. FORCE_INLINE __m128 _mm_cvtpu16_ps(__m64 a)
  1409. {
  1410. return vreinterpretq_m128_f32(
  1411. vcvtq_f32_u32(vmovl_u16(vreinterpret_u16_m64(a))));
  1412. }
  1413. // Convert the lower packed unsigned 8-bit integers in a to packed
  1414. // single-precision (32-bit) floating-point elements, and store the results in
  1415. // dst.
  1416. //
  1417. // FOR j := 0 to 3
  1418. // i := j*8
  1419. // m := j*32
  1420. // dst[m+31:m] := Convert_UInt8_To_FP32(a[i+7:i])
  1421. // ENDFOR
  1422. //
  1423. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpu8_ps
  1424. FORCE_INLINE __m128 _mm_cvtpu8_ps(__m64 a)
  1425. {
  1426. return vreinterpretq_m128_f32(vcvtq_f32_u32(
  1427. vmovl_u16(vget_low_u16(vmovl_u8(vreinterpret_u8_m64(a))))));
  1428. }
  1429. // Convert the signed 32-bit integer b to a single-precision (32-bit)
  1430. // floating-point element, store the result in the lower element of dst, and
  1431. // copy the upper 3 packed elements from a to the upper elements of dst.
  1432. //
  1433. // dst[31:0] := Convert_Int32_To_FP32(b[31:0])
  1434. // dst[127:32] := a[127:32]
  1435. //
  1436. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi32_ss
  1437. #define _mm_cvtsi32_ss(a, b) _mm_cvt_si2ss(a, b)
  1438. // Convert the signed 64-bit integer b to a single-precision (32-bit)
  1439. // floating-point element, store the result in the lower element of dst, and
  1440. // copy the upper 3 packed elements from a to the upper elements of dst.
  1441. //
  1442. // dst[31:0] := Convert_Int64_To_FP32(b[63:0])
  1443. // dst[127:32] := a[127:32]
  1444. //
  1445. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi64_ss
  1446. FORCE_INLINE __m128 _mm_cvtsi64_ss(__m128 a, int64_t b)
  1447. {
  1448. return vreinterpretq_m128_f32(
  1449. vsetq_lane_f32((float) b, vreinterpretq_f32_m128(a), 0));
  1450. }
  1451. // Copy the lower single-precision (32-bit) floating-point element of a to dst.
  1452. //
  1453. // dst[31:0] := a[31:0]
  1454. //
  1455. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtss_f32
  1456. FORCE_INLINE float _mm_cvtss_f32(__m128 a)
  1457. {
  1458. return vgetq_lane_f32(vreinterpretq_f32_m128(a), 0);
  1459. }
  1460. // Convert the lower single-precision (32-bit) floating-point element in a to a
  1461. // 32-bit integer, and store the result in dst.
  1462. //
  1463. // dst[31:0] := Convert_FP32_To_Int32(a[31:0])
  1464. //
  1465. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtss_si32
  1466. #define _mm_cvtss_si32(a) _mm_cvt_ss2si(a)
  1467. // Convert the lower single-precision (32-bit) floating-point element in a to a
  1468. // 64-bit integer, and store the result in dst.
  1469. //
  1470. // dst[63:0] := Convert_FP32_To_Int64(a[31:0])
  1471. //
  1472. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtss_si64
  1473. FORCE_INLINE int64_t _mm_cvtss_si64(__m128 a)
  1474. {
  1475. #if defined(__aarch64__)
  1476. return (int64_t) vgetq_lane_f32(vrndiq_f32(vreinterpretq_f32_m128(a)), 0);
  1477. #else
  1478. float32_t data = vgetq_lane_f32(
  1479. vreinterpretq_f32_m128(_mm_round_ps(a, _MM_FROUND_CUR_DIRECTION)), 0);
  1480. return (int64_t) data;
  1481. #endif
  1482. }
  1483. // Convert packed single-precision (32-bit) floating-point elements in a to
  1484. // packed 32-bit integers with truncation, and store the results in dst.
  1485. //
  1486. // FOR j := 0 to 1
  1487. // i := 32*j
  1488. // dst[i+31:i] := Convert_FP32_To_Int32_Truncate(a[i+31:i])
  1489. // ENDFOR
  1490. //
  1491. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtt_ps2pi
  1492. FORCE_INLINE __m64 _mm_cvtt_ps2pi(__m128 a)
  1493. {
  1494. return vreinterpret_m64_s32(
  1495. vget_low_s32(vcvtq_s32_f32(vreinterpretq_f32_m128(a))));
  1496. }
  1497. // Convert the lower single-precision (32-bit) floating-point element in a to a
  1498. // 32-bit integer with truncation, and store the result in dst.
  1499. //
  1500. // dst[31:0] := Convert_FP32_To_Int32_Truncate(a[31:0])
  1501. //
  1502. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtt_ss2si
  1503. FORCE_INLINE int _mm_cvtt_ss2si(__m128 a)
  1504. {
  1505. return vgetq_lane_s32(vcvtq_s32_f32(vreinterpretq_f32_m128(a)), 0);
  1506. }
  1507. // Convert packed single-precision (32-bit) floating-point elements in a to
  1508. // packed 32-bit integers with truncation, and store the results in dst.
  1509. //
  1510. // FOR j := 0 to 1
  1511. // i := 32*j
  1512. // dst[i+31:i] := Convert_FP32_To_Int32_Truncate(a[i+31:i])
  1513. // ENDFOR
  1514. //
  1515. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvttps_pi32
  1516. #define _mm_cvttps_pi32(a) _mm_cvtt_ps2pi(a)
  1517. // Convert the lower single-precision (32-bit) floating-point element in a to a
  1518. // 32-bit integer with truncation, and store the result in dst.
  1519. //
  1520. // dst[31:0] := Convert_FP32_To_Int32_Truncate(a[31:0])
  1521. //
  1522. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvttss_si32
  1523. #define _mm_cvttss_si32(a) _mm_cvtt_ss2si(a)
  1524. // Convert the lower single-precision (32-bit) floating-point element in a to a
  1525. // 64-bit integer with truncation, and store the result in dst.
  1526. //
  1527. // dst[63:0] := Convert_FP32_To_Int64_Truncate(a[31:0])
  1528. //
  1529. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvttss_si64
  1530. FORCE_INLINE int64_t _mm_cvttss_si64(__m128 a)
  1531. {
  1532. return (int64_t) vgetq_lane_f32(vreinterpretq_f32_m128(a), 0);
  1533. }
  1534. // Divides the four single-precision, floating-point values of a and b.
  1535. //
  1536. // r0 := a0 / b0
  1537. // r1 := a1 / b1
  1538. // r2 := a2 / b2
  1539. // r3 := a3 / b3
  1540. //
  1541. // https://msdn.microsoft.com/en-us/library/edaw8147(v=vs.100).aspx
  1542. FORCE_INLINE __m128 _mm_div_ps(__m128 a, __m128 b)
  1543. {
  1544. #if defined(__aarch64__) && !SSE2NEON_PRECISE_DIV
  1545. return vreinterpretq_m128_f32(
  1546. vdivq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  1547. #else
  1548. float32x4_t recip = vrecpeq_f32(vreinterpretq_f32_m128(b));
  1549. recip = vmulq_f32(recip, vrecpsq_f32(recip, vreinterpretq_f32_m128(b)));
  1550. #if SSE2NEON_PRECISE_DIV
  1551. // Additional Netwon-Raphson iteration for accuracy
  1552. recip = vmulq_f32(recip, vrecpsq_f32(recip, vreinterpretq_f32_m128(b)));
  1553. #endif
  1554. return vreinterpretq_m128_f32(vmulq_f32(vreinterpretq_f32_m128(a), recip));
  1555. #endif
  1556. }
  1557. // Divides the scalar single-precision floating point value of a by b.
  1558. // https://msdn.microsoft.com/en-us/library/4y73xa49(v=vs.100).aspx
  1559. FORCE_INLINE __m128 _mm_div_ss(__m128 a, __m128 b)
  1560. {
  1561. float32_t value =
  1562. vgetq_lane_f32(vreinterpretq_f32_m128(_mm_div_ps(a, b)), 0);
  1563. return vreinterpretq_m128_f32(
  1564. vsetq_lane_f32(value, vreinterpretq_f32_m128(a), 0));
  1565. }
  1566. // Extract a 16-bit integer from a, selected with imm8, and store the result in
  1567. // the lower element of dst.
  1568. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_extract_pi16
  1569. #define _mm_extract_pi16(a, imm) \
  1570. (int32_t) vget_lane_u16(vreinterpret_u16_m64(a), (imm))
  1571. // Free aligned memory that was allocated with _mm_malloc.
  1572. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_free
  1573. FORCE_INLINE void _mm_free(void *addr)
  1574. {
  1575. free(addr);
  1576. }
  1577. // Macro: Get the flush zero bits from the MXCSR control and status register.
  1578. // The flush zero may contain any of the following flags: _MM_FLUSH_ZERO_ON or
  1579. // _MM_FLUSH_ZERO_OFF
  1580. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_MM_GET_FLUSH_ZERO_MODE
  1581. FORCE_INLINE unsigned int _sse2neon_mm_get_flush_zero_mode()
  1582. {
  1583. union {
  1584. fpcr_bitfield field;
  1585. #if defined(__aarch64__)
  1586. uint64_t value;
  1587. #else
  1588. uint32_t value;
  1589. #endif
  1590. } r;
  1591. #if defined(__aarch64__)
  1592. asm volatile("mrs %0, FPCR" : "=r"(r.value)); /* read */
  1593. #else
  1594. asm volatile("vmrs %0, FPSCR" : "=r"(r.value)); /* read */
  1595. #endif
  1596. return r.field.bit24 ? _MM_FLUSH_ZERO_ON : _MM_FLUSH_ZERO_OFF;
  1597. }
  1598. // Macro: Get the rounding mode bits from the MXCSR control and status register.
  1599. // The rounding mode may contain any of the following flags: _MM_ROUND_NEAREST,
  1600. // _MM_ROUND_DOWN, _MM_ROUND_UP, _MM_ROUND_TOWARD_ZERO
  1601. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_MM_GET_ROUNDING_MODE
  1602. FORCE_INLINE unsigned int _MM_GET_ROUNDING_MODE()
  1603. {
  1604. union {
  1605. fpcr_bitfield field;
  1606. #if defined(__aarch64__)
  1607. uint64_t value;
  1608. #else
  1609. uint32_t value;
  1610. #endif
  1611. } r;
  1612. #if defined(__aarch64__)
  1613. asm volatile("mrs %0, FPCR" : "=r"(r.value)); /* read */
  1614. #else
  1615. asm volatile("vmrs %0, FPSCR" : "=r"(r.value)); /* read */
  1616. #endif
  1617. if (r.field.bit22) {
  1618. return r.field.bit23 ? _MM_ROUND_TOWARD_ZERO : _MM_ROUND_UP;
  1619. } else {
  1620. return r.field.bit23 ? _MM_ROUND_DOWN : _MM_ROUND_NEAREST;
  1621. }
  1622. }
  1623. // Copy a to dst, and insert the 16-bit integer i into dst at the location
  1624. // specified by imm8.
  1625. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_insert_pi16
  1626. #define _mm_insert_pi16(a, b, imm) \
  1627. __extension__({ \
  1628. vreinterpret_m64_s16( \
  1629. vset_lane_s16((b), vreinterpret_s16_m64(a), (imm))); \
  1630. })
  1631. // Loads four single-precision, floating-point values.
  1632. // https://msdn.microsoft.com/en-us/library/vstudio/zzd50xxt(v=vs.100).aspx
  1633. FORCE_INLINE __m128 _mm_load_ps(const float *p)
  1634. {
  1635. return vreinterpretq_m128_f32(vld1q_f32(p));
  1636. }
  1637. // Load a single-precision (32-bit) floating-point element from memory into all
  1638. // elements of dst.
  1639. //
  1640. // dst[31:0] := MEM[mem_addr+31:mem_addr]
  1641. // dst[63:32] := MEM[mem_addr+31:mem_addr]
  1642. // dst[95:64] := MEM[mem_addr+31:mem_addr]
  1643. // dst[127:96] := MEM[mem_addr+31:mem_addr]
  1644. //
  1645. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_load_ps1
  1646. #define _mm_load_ps1 _mm_load1_ps
  1647. // Loads an single - precision, floating - point value into the low word and
  1648. // clears the upper three words.
  1649. // https://msdn.microsoft.com/en-us/library/548bb9h4%28v=vs.90%29.aspx
  1650. FORCE_INLINE __m128 _mm_load_ss(const float *p)
  1651. {
  1652. return vreinterpretq_m128_f32(vsetq_lane_f32(*p, vdupq_n_f32(0), 0));
  1653. }
  1654. // Loads a single single-precision, floating-point value, copying it into all
  1655. // four words
  1656. // https://msdn.microsoft.com/en-us/library/vstudio/5cdkf716(v=vs.100).aspx
  1657. FORCE_INLINE __m128 _mm_load1_ps(const float *p)
  1658. {
  1659. return vreinterpretq_m128_f32(vld1q_dup_f32(p));
  1660. }
  1661. // Sets the upper two single-precision, floating-point values with 64
  1662. // bits of data loaded from the address p; the lower two values are passed
  1663. // through from a.
  1664. //
  1665. // r0 := a0
  1666. // r1 := a1
  1667. // r2 := *p0
  1668. // r3 := *p1
  1669. //
  1670. // https://msdn.microsoft.com/en-us/library/w92wta0x(v%3dvs.100).aspx
  1671. FORCE_INLINE __m128 _mm_loadh_pi(__m128 a, __m64 const *p)
  1672. {
  1673. return vreinterpretq_m128_f32(
  1674. vcombine_f32(vget_low_f32(a), vld1_f32((const float32_t *) p)));
  1675. }
  1676. // Sets the lower two single-precision, floating-point values with 64
  1677. // bits of data loaded from the address p; the upper two values are passed
  1678. // through from a.
  1679. //
  1680. // Return Value
  1681. // r0 := *p0
  1682. // r1 := *p1
  1683. // r2 := a2
  1684. // r3 := a3
  1685. //
  1686. // https://msdn.microsoft.com/en-us/library/s57cyak2(v=vs.100).aspx
  1687. FORCE_INLINE __m128 _mm_loadl_pi(__m128 a, __m64 const *p)
  1688. {
  1689. return vreinterpretq_m128_f32(
  1690. vcombine_f32(vld1_f32((const float32_t *) p), vget_high_f32(a)));
  1691. }
  1692. // Load 4 single-precision (32-bit) floating-point elements from memory into dst
  1693. // in reverse order. mem_addr must be aligned on a 16-byte boundary or a
  1694. // general-protection exception may be generated.
  1695. //
  1696. // dst[31:0] := MEM[mem_addr+127:mem_addr+96]
  1697. // dst[63:32] := MEM[mem_addr+95:mem_addr+64]
  1698. // dst[95:64] := MEM[mem_addr+63:mem_addr+32]
  1699. // dst[127:96] := MEM[mem_addr+31:mem_addr]
  1700. //
  1701. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadr_ps
  1702. FORCE_INLINE __m128 _mm_loadr_ps(const float *p)
  1703. {
  1704. float32x4_t v = vrev64q_f32(vld1q_f32(p));
  1705. return vreinterpretq_m128_f32(vextq_f32(v, v, 2));
  1706. }
  1707. // Loads four single-precision, floating-point values.
  1708. // https://msdn.microsoft.com/en-us/library/x1b16s7z%28v=vs.90%29.aspx
  1709. FORCE_INLINE __m128 _mm_loadu_ps(const float *p)
  1710. {
  1711. // for neon, alignment doesn't matter, so _mm_load_ps and _mm_loadu_ps are
  1712. // equivalent for neon
  1713. return vreinterpretq_m128_f32(vld1q_f32(p));
  1714. }
  1715. // Load unaligned 16-bit integer from memory into the first element of dst.
  1716. //
  1717. // dst[15:0] := MEM[mem_addr+15:mem_addr]
  1718. // dst[MAX:16] := 0
  1719. //
  1720. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadu_si16
  1721. FORCE_INLINE __m128i _mm_loadu_si16(const void *p)
  1722. {
  1723. return vreinterpretq_m128i_s16(
  1724. vsetq_lane_s16(*(const int16_t *) p, vdupq_n_s16(0), 0));
  1725. }
  1726. // Load unaligned 64-bit integer from memory into the first element of dst.
  1727. //
  1728. // dst[63:0] := MEM[mem_addr+63:mem_addr]
  1729. // dst[MAX:64] := 0
  1730. //
  1731. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadu_si64
  1732. FORCE_INLINE __m128i _mm_loadu_si64(const void *p)
  1733. {
  1734. return vreinterpretq_m128i_s64(
  1735. vcombine_s64(vld1_s64((const int64_t *) p), vdup_n_s64(0)));
  1736. }
  1737. // Allocate aligned blocks of memory.
  1738. // https://software.intel.com/en-us/
  1739. // cpp-compiler-developer-guide-and-reference-allocating-and-freeing-aligned-memory-blocks
  1740. FORCE_INLINE void *_mm_malloc(size_t size, size_t align)
  1741. {
  1742. void *ptr;
  1743. if (align == 1)
  1744. return malloc(size);
  1745. if (align == 2 || (sizeof(void *) == 8 && align == 4))
  1746. align = sizeof(void *);
  1747. if (!posix_memalign(&ptr, align, size))
  1748. return ptr;
  1749. return NULL;
  1750. }
  1751. // Conditionally store 8-bit integer elements from a into memory using mask
  1752. // (elements are not stored when the highest bit is not set in the corresponding
  1753. // element) and a non-temporal memory hint.
  1754. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_maskmove_si64
  1755. FORCE_INLINE void _mm_maskmove_si64(__m64 a, __m64 mask, char *mem_addr)
  1756. {
  1757. int8x8_t shr_mask = vshr_n_s8(vreinterpret_s8_m64(mask), 7);
  1758. __m128 b = _mm_load_ps((const float *) mem_addr);
  1759. int8x8_t masked =
  1760. vbsl_s8(vreinterpret_u8_s8(shr_mask), vreinterpret_s8_m64(a),
  1761. vreinterpret_s8_u64(vget_low_u64(vreinterpretq_u64_m128(b))));
  1762. vst1_s8((int8_t *) mem_addr, masked);
  1763. }
  1764. // Conditionally store 8-bit integer elements from a into memory using mask
  1765. // (elements are not stored when the highest bit is not set in the corresponding
  1766. // element) and a non-temporal memory hint.
  1767. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_maskmovq
  1768. #define _m_maskmovq(a, mask, mem_addr) _mm_maskmove_si64(a, mask, mem_addr)
  1769. // Compare packed signed 16-bit integers in a and b, and store packed maximum
  1770. // values in dst.
  1771. //
  1772. // FOR j := 0 to 3
  1773. // i := j*16
  1774. // dst[i+15:i] := MAX(a[i+15:i], b[i+15:i])
  1775. // ENDFOR
  1776. //
  1777. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_max_pi16
  1778. FORCE_INLINE __m64 _mm_max_pi16(__m64 a, __m64 b)
  1779. {
  1780. return vreinterpret_m64_s16(
  1781. vmax_s16(vreinterpret_s16_m64(a), vreinterpret_s16_m64(b)));
  1782. }
  1783. // Computes the maximums of the four single-precision, floating-point values of
  1784. // a and b.
  1785. // https://msdn.microsoft.com/en-us/library/vstudio/ff5d607a(v=vs.100).aspx
  1786. FORCE_INLINE __m128 _mm_max_ps(__m128 a, __m128 b)
  1787. {
  1788. #if SSE2NEON_PRECISE_MINMAX
  1789. float32x4_t _a = vreinterpretq_f32_m128(a);
  1790. float32x4_t _b = vreinterpretq_f32_m128(b);
  1791. return vbslq_f32(vcltq_f32(_b, _a), _a, _b);
  1792. #else
  1793. return vreinterpretq_m128_f32(
  1794. vmaxq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  1795. #endif
  1796. }
  1797. // Compare packed unsigned 8-bit integers in a and b, and store packed maximum
  1798. // values in dst.
  1799. //
  1800. // FOR j := 0 to 7
  1801. // i := j*8
  1802. // dst[i+7:i] := MAX(a[i+7:i], b[i+7:i])
  1803. // ENDFOR
  1804. //
  1805. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_max_pu8
  1806. FORCE_INLINE __m64 _mm_max_pu8(__m64 a, __m64 b)
  1807. {
  1808. return vreinterpret_m64_u8(
  1809. vmax_u8(vreinterpret_u8_m64(a), vreinterpret_u8_m64(b)));
  1810. }
  1811. // Computes the maximum of the two lower scalar single-precision floating point
  1812. // values of a and b.
  1813. // https://msdn.microsoft.com/en-us/library/s6db5esz(v=vs.100).aspx
  1814. FORCE_INLINE __m128 _mm_max_ss(__m128 a, __m128 b)
  1815. {
  1816. float32_t value = vgetq_lane_f32(_mm_max_ps(a, b), 0);
  1817. return vreinterpretq_m128_f32(
  1818. vsetq_lane_f32(value, vreinterpretq_f32_m128(a), 0));
  1819. }
  1820. // Compare packed signed 16-bit integers in a and b, and store packed minimum
  1821. // values in dst.
  1822. //
  1823. // FOR j := 0 to 3
  1824. // i := j*16
  1825. // dst[i+15:i] := MIN(a[i+15:i], b[i+15:i])
  1826. // ENDFOR
  1827. //
  1828. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_min_pi16
  1829. FORCE_INLINE __m64 _mm_min_pi16(__m64 a, __m64 b)
  1830. {
  1831. return vreinterpret_m64_s16(
  1832. vmin_s16(vreinterpret_s16_m64(a), vreinterpret_s16_m64(b)));
  1833. }
  1834. // Computes the minima of the four single-precision, floating-point values of a
  1835. // and b.
  1836. // https://msdn.microsoft.com/en-us/library/vstudio/wh13kadz(v=vs.100).aspx
  1837. FORCE_INLINE __m128 _mm_min_ps(__m128 a, __m128 b)
  1838. {
  1839. #if SSE2NEON_PRECISE_MINMAX
  1840. float32x4_t _a = vreinterpretq_f32_m128(a);
  1841. float32x4_t _b = vreinterpretq_f32_m128(b);
  1842. return vbslq_f32(vcltq_f32(_a, _b), _a, _b);
  1843. #else
  1844. return vreinterpretq_m128_f32(
  1845. vminq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  1846. #endif
  1847. }
  1848. // Compare packed unsigned 8-bit integers in a and b, and store packed minimum
  1849. // values in dst.
  1850. //
  1851. // FOR j := 0 to 7
  1852. // i := j*8
  1853. // dst[i+7:i] := MIN(a[i+7:i], b[i+7:i])
  1854. // ENDFOR
  1855. //
  1856. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_min_pu8
  1857. FORCE_INLINE __m64 _mm_min_pu8(__m64 a, __m64 b)
  1858. {
  1859. return vreinterpret_m64_u8(
  1860. vmin_u8(vreinterpret_u8_m64(a), vreinterpret_u8_m64(b)));
  1861. }
  1862. // Computes the minimum of the two lower scalar single-precision floating point
  1863. // values of a and b.
  1864. // https://msdn.microsoft.com/en-us/library/0a9y7xaa(v=vs.100).aspx
  1865. FORCE_INLINE __m128 _mm_min_ss(__m128 a, __m128 b)
  1866. {
  1867. float32_t value = vgetq_lane_f32(_mm_min_ps(a, b), 0);
  1868. return vreinterpretq_m128_f32(
  1869. vsetq_lane_f32(value, vreinterpretq_f32_m128(a), 0));
  1870. }
  1871. // Sets the low word to the single-precision, floating-point value of b
  1872. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/35hdzazd(v=vs.100)
  1873. FORCE_INLINE __m128 _mm_move_ss(__m128 a, __m128 b)
  1874. {
  1875. return vreinterpretq_m128_f32(
  1876. vsetq_lane_f32(vgetq_lane_f32(vreinterpretq_f32_m128(b), 0),
  1877. vreinterpretq_f32_m128(a), 0));
  1878. }
  1879. // Moves the upper two values of B into the lower two values of A.
  1880. //
  1881. // r3 := a3
  1882. // r2 := a2
  1883. // r1 := b3
  1884. // r0 := b2
  1885. FORCE_INLINE __m128 _mm_movehl_ps(__m128 __A, __m128 __B)
  1886. {
  1887. float32x2_t a32 = vget_high_f32(vreinterpretq_f32_m128(__A));
  1888. float32x2_t b32 = vget_high_f32(vreinterpretq_f32_m128(__B));
  1889. return vreinterpretq_m128_f32(vcombine_f32(b32, a32));
  1890. }
  1891. // Moves the lower two values of B into the upper two values of A.
  1892. //
  1893. // r3 := b1
  1894. // r2 := b0
  1895. // r1 := a1
  1896. // r0 := a0
  1897. FORCE_INLINE __m128 _mm_movelh_ps(__m128 __A, __m128 __B)
  1898. {
  1899. float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(__A));
  1900. float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(__B));
  1901. return vreinterpretq_m128_f32(vcombine_f32(a10, b10));
  1902. }
  1903. // Create mask from the most significant bit of each 8-bit element in a, and
  1904. // store the result in dst.
  1905. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_movemask_pi8
  1906. FORCE_INLINE int _mm_movemask_pi8(__m64 a)
  1907. {
  1908. uint8x8_t input = vreinterpret_u8_m64(a);
  1909. #if defined(__aarch64__)
  1910. static const int8x8_t shift = {0, 1, 2, 3, 4, 5, 6, 7};
  1911. uint8x8_t tmp = vshr_n_u8(input, 7);
  1912. return vaddv_u8(vshl_u8(tmp, shift));
  1913. #else
  1914. // Refer the implementation of `_mm_movemask_epi8`
  1915. uint16x4_t high_bits = vreinterpret_u16_u8(vshr_n_u8(input, 7));
  1916. uint32x2_t paired16 =
  1917. vreinterpret_u32_u16(vsra_n_u16(high_bits, high_bits, 7));
  1918. uint8x8_t paired32 =
  1919. vreinterpret_u8_u32(vsra_n_u32(paired16, paired16, 14));
  1920. return vget_lane_u8(paired32, 0) | ((int) vget_lane_u8(paired32, 4) << 4);
  1921. #endif
  1922. }
  1923. // NEON does not provide this method
  1924. // Creates a 4-bit mask from the most significant bits of the four
  1925. // single-precision, floating-point values.
  1926. // https://msdn.microsoft.com/en-us/library/vstudio/4490ys29(v=vs.100).aspx
  1927. FORCE_INLINE int _mm_movemask_ps(__m128 a)
  1928. {
  1929. uint32x4_t input = vreinterpretq_u32_m128(a);
  1930. #if defined(__aarch64__)
  1931. static const int32x4_t shift = {0, 1, 2, 3};
  1932. uint32x4_t tmp = vshrq_n_u32(input, 31);
  1933. return vaddvq_u32(vshlq_u32(tmp, shift));
  1934. #else
  1935. // Uses the exact same method as _mm_movemask_epi8, see that for details.
  1936. // Shift out everything but the sign bits with a 32-bit unsigned shift
  1937. // right.
  1938. uint64x2_t high_bits = vreinterpretq_u64_u32(vshrq_n_u32(input, 31));
  1939. // Merge the two pairs together with a 64-bit unsigned shift right + add.
  1940. uint8x16_t paired =
  1941. vreinterpretq_u8_u64(vsraq_n_u64(high_bits, high_bits, 31));
  1942. // Extract the result.
  1943. return vgetq_lane_u8(paired, 0) | (vgetq_lane_u8(paired, 8) << 2);
  1944. #endif
  1945. }
  1946. // Multiplies the four single-precision, floating-point values of a and b.
  1947. //
  1948. // r0 := a0 * b0
  1949. // r1 := a1 * b1
  1950. // r2 := a2 * b2
  1951. // r3 := a3 * b3
  1952. //
  1953. // https://msdn.microsoft.com/en-us/library/vstudio/22kbk6t9(v=vs.100).aspx
  1954. FORCE_INLINE __m128 _mm_mul_ps(__m128 a, __m128 b)
  1955. {
  1956. return vreinterpretq_m128_f32(
  1957. vmulq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  1958. }
  1959. // Multiply the lower single-precision (32-bit) floating-point element in a and
  1960. // b, store the result in the lower element of dst, and copy the upper 3 packed
  1961. // elements from a to the upper elements of dst.
  1962. //
  1963. // dst[31:0] := a[31:0] * b[31:0]
  1964. // dst[127:32] := a[127:32]
  1965. //
  1966. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_mul_ss
  1967. FORCE_INLINE __m128 _mm_mul_ss(__m128 a, __m128 b)
  1968. {
  1969. return _mm_move_ss(a, _mm_mul_ps(a, b));
  1970. }
  1971. // Multiply the packed unsigned 16-bit integers in a and b, producing
  1972. // intermediate 32-bit integers, and store the high 16 bits of the intermediate
  1973. // integers in dst.
  1974. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_mulhi_pu16
  1975. FORCE_INLINE __m64 _mm_mulhi_pu16(__m64 a, __m64 b)
  1976. {
  1977. return vreinterpret_m64_u16(vshrn_n_u32(
  1978. vmull_u16(vreinterpret_u16_m64(a), vreinterpret_u16_m64(b)), 16));
  1979. }
  1980. // Computes the bitwise OR of the four single-precision, floating-point values
  1981. // of a and b.
  1982. // https://msdn.microsoft.com/en-us/library/vstudio/7ctdsyy0(v=vs.100).aspx
  1983. FORCE_INLINE __m128 _mm_or_ps(__m128 a, __m128 b)
  1984. {
  1985. return vreinterpretq_m128_s32(
  1986. vorrq_s32(vreinterpretq_s32_m128(a), vreinterpretq_s32_m128(b)));
  1987. }
  1988. // Average packed unsigned 8-bit integers in a and b, and store the results in
  1989. // dst.
  1990. //
  1991. // FOR j := 0 to 7
  1992. // i := j*8
  1993. // dst[i+7:i] := (a[i+7:i] + b[i+7:i] + 1) >> 1
  1994. // ENDFOR
  1995. //
  1996. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pavgb
  1997. #define _m_pavgb(a, b) _mm_avg_pu8(a, b)
  1998. // Average packed unsigned 16-bit integers in a and b, and store the results in
  1999. // dst.
  2000. //
  2001. // FOR j := 0 to 3
  2002. // i := j*16
  2003. // dst[i+15:i] := (a[i+15:i] + b[i+15:i] + 1) >> 1
  2004. // ENDFOR
  2005. //
  2006. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pavgw
  2007. #define _m_pavgw(a, b) _mm_avg_pu16(a, b)
  2008. // Extract a 16-bit integer from a, selected with imm8, and store the result in
  2009. // the lower element of dst.
  2010. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pextrw
  2011. #define _m_pextrw(a, imm) _mm_extract_pi16(a, imm)
  2012. // Copy a to dst, and insert the 16-bit integer i into dst at the location
  2013. // specified by imm8.
  2014. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=m_pinsrw
  2015. #define _m_pinsrw(a, i, imm) _mm_insert_pi16(a, i, imm)
  2016. // Compare packed signed 16-bit integers in a and b, and store packed maximum
  2017. // values in dst.
  2018. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pmaxsw
  2019. #define _m_pmaxsw(a, b) _mm_max_pi16(a, b)
  2020. // Compare packed unsigned 8-bit integers in a and b, and store packed maximum
  2021. // values in dst.
  2022. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pmaxub
  2023. #define _m_pmaxub(a, b) _mm_max_pu8(a, b)
  2024. // Compare packed signed 16-bit integers in a and b, and store packed minimum
  2025. // values in dst.
  2026. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pminsw
  2027. #define _m_pminsw(a, b) _mm_min_pi16(a, b)
  2028. // Compare packed unsigned 8-bit integers in a and b, and store packed minimum
  2029. // values in dst.
  2030. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pminub
  2031. #define _m_pminub(a, b) _mm_min_pu8(a, b)
  2032. // Create mask from the most significant bit of each 8-bit element in a, and
  2033. // store the result in dst.
  2034. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pmovmskb
  2035. #define _m_pmovmskb(a) _mm_movemask_pi8(a)
  2036. // Multiply the packed unsigned 16-bit integers in a and b, producing
  2037. // intermediate 32-bit integers, and store the high 16 bits of the intermediate
  2038. // integers in dst.
  2039. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pmulhuw
  2040. #define _m_pmulhuw(a, b) _mm_mulhi_pu16(a, b)
  2041. // Loads one cache line of data from address p to a location closer to the
  2042. // processor. https://msdn.microsoft.com/en-us/library/84szxsww(v=vs.100).aspx
  2043. FORCE_INLINE void _mm_prefetch(const void *p, int i)
  2044. {
  2045. (void) i;
  2046. __builtin_prefetch(p);
  2047. }
  2048. // Compute the absolute differences of packed unsigned 8-bit integers in a and
  2049. // b, then horizontally sum each consecutive 8 differences to produce four
  2050. // unsigned 16-bit integers, and pack these unsigned 16-bit integers in the low
  2051. // 16 bits of dst.
  2052. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=m_psadbw
  2053. #define _m_psadbw(a, b) _mm_sad_pu8(a, b)
  2054. // Shuffle 16-bit integers in a using the control in imm8, and store the results
  2055. // in dst.
  2056. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_m_pshufw
  2057. #define _m_pshufw(a, imm) _mm_shuffle_pi16(a, imm)
  2058. // Compute the approximate reciprocal of packed single-precision (32-bit)
  2059. // floating-point elements in a, and store the results in dst. The maximum
  2060. // relative error for this approximation is less than 1.5*2^-12.
  2061. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_rcp_ps
  2062. FORCE_INLINE __m128 _mm_rcp_ps(__m128 in)
  2063. {
  2064. float32x4_t recip = vrecpeq_f32(vreinterpretq_f32_m128(in));
  2065. recip = vmulq_f32(recip, vrecpsq_f32(recip, vreinterpretq_f32_m128(in)));
  2066. #if SSE2NEON_PRECISE_DIV
  2067. // Additional Netwon-Raphson iteration for accuracy
  2068. recip = vmulq_f32(recip, vrecpsq_f32(recip, vreinterpretq_f32_m128(in)));
  2069. #endif
  2070. return vreinterpretq_m128_f32(recip);
  2071. }
  2072. // Compute the approximate reciprocal of the lower single-precision (32-bit)
  2073. // floating-point element in a, store the result in the lower element of dst,
  2074. // and copy the upper 3 packed elements from a to the upper elements of dst. The
  2075. // maximum relative error for this approximation is less than 1.5*2^-12.
  2076. //
  2077. // dst[31:0] := (1.0 / a[31:0])
  2078. // dst[127:32] := a[127:32]
  2079. //
  2080. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_rcp_ss
  2081. FORCE_INLINE __m128 _mm_rcp_ss(__m128 a)
  2082. {
  2083. return _mm_move_ss(a, _mm_rcp_ps(a));
  2084. }
  2085. // Computes the approximations of the reciprocal square roots of the four
  2086. // single-precision floating point values of in.
  2087. // The current precision is 1% error.
  2088. // https://msdn.microsoft.com/en-us/library/22hfsh53(v=vs.100).aspx
  2089. FORCE_INLINE __m128 _mm_rsqrt_ps(__m128 in)
  2090. {
  2091. float32x4_t out = vrsqrteq_f32(vreinterpretq_f32_m128(in));
  2092. #if SSE2NEON_PRECISE_SQRT
  2093. // Additional Netwon-Raphson iteration for accuracy
  2094. out = vmulq_f32(
  2095. out, vrsqrtsq_f32(vmulq_f32(vreinterpretq_f32_m128(in), out), out));
  2096. out = vmulq_f32(
  2097. out, vrsqrtsq_f32(vmulq_f32(vreinterpretq_f32_m128(in), out), out));
  2098. #endif
  2099. return vreinterpretq_m128_f32(out);
  2100. }
  2101. // Compute the approximate reciprocal square root of the lower single-precision
  2102. // (32-bit) floating-point element in a, store the result in the lower element
  2103. // of dst, and copy the upper 3 packed elements from a to the upper elements of
  2104. // dst.
  2105. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_rsqrt_ss
  2106. FORCE_INLINE __m128 _mm_rsqrt_ss(__m128 in)
  2107. {
  2108. return vsetq_lane_f32(vgetq_lane_f32(_mm_rsqrt_ps(in), 0), in, 0);
  2109. }
  2110. // Compute the absolute differences of packed unsigned 8-bit integers in a and
  2111. // b, then horizontally sum each consecutive 8 differences to produce four
  2112. // unsigned 16-bit integers, and pack these unsigned 16-bit integers in the low
  2113. // 16 bits of dst.
  2114. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sad_pu8
  2115. FORCE_INLINE __m64 _mm_sad_pu8(__m64 a, __m64 b)
  2116. {
  2117. uint64x1_t t = vpaddl_u32(vpaddl_u16(
  2118. vpaddl_u8(vabd_u8(vreinterpret_u8_m64(a), vreinterpret_u8_m64(b)))));
  2119. return vreinterpret_m64_u16(
  2120. vset_lane_u16(vget_lane_u64(t, 0), vdup_n_u16(0), 0));
  2121. }
  2122. // Macro: Set the flush zero bits of the MXCSR control and status register to
  2123. // the value in unsigned 32-bit integer a. The flush zero may contain any of the
  2124. // following flags: _MM_FLUSH_ZERO_ON or _MM_FLUSH_ZERO_OFF
  2125. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_MM_SET_FLUSH_ZERO_MODE
  2126. FORCE_INLINE void _sse2neon_mm_set_flush_zero_mode(unsigned int flag)
  2127. {
  2128. // AArch32 Advanced SIMD arithmetic always uses the Flush-to-zero setting,
  2129. // regardless of the value of the FZ bit.
  2130. union {
  2131. fpcr_bitfield field;
  2132. #if defined(__aarch64__)
  2133. uint64_t value;
  2134. #else
  2135. uint32_t value;
  2136. #endif
  2137. } r;
  2138. #if defined(__aarch64__)
  2139. asm volatile("mrs %0, FPCR" : "=r"(r.value)); /* read */
  2140. #else
  2141. asm volatile("vmrs %0, FPSCR" : "=r"(r.value)); /* read */
  2142. #endif
  2143. r.field.bit24 = (flag & _MM_FLUSH_ZERO_MASK) == _MM_FLUSH_ZERO_ON;
  2144. #if defined(__aarch64__)
  2145. asm volatile("msr FPCR, %0" ::"r"(r)); /* write */
  2146. #else
  2147. asm volatile("vmsr FPSCR, %0" ::"r"(r)); /* write */
  2148. #endif
  2149. }
  2150. // Sets the four single-precision, floating-point values to the four inputs.
  2151. // https://msdn.microsoft.com/en-us/library/vstudio/afh0zf75(v=vs.100).aspx
  2152. FORCE_INLINE __m128 _mm_set_ps(float w, float z, float y, float x)
  2153. {
  2154. float ALIGN_STRUCT(16) data[4] = {x, y, z, w};
  2155. return vreinterpretq_m128_f32(vld1q_f32(data));
  2156. }
  2157. // Sets the four single-precision, floating-point values to w.
  2158. // https://msdn.microsoft.com/en-us/library/vstudio/2x1se8ha(v=vs.100).aspx
  2159. FORCE_INLINE __m128 _mm_set_ps1(float _w)
  2160. {
  2161. return vreinterpretq_m128_f32(vdupq_n_f32(_w));
  2162. }
  2163. // Macro: Set the rounding mode bits of the MXCSR control and status register to
  2164. // the value in unsigned 32-bit integer a. The rounding mode may contain any of
  2165. // the following flags: _MM_ROUND_NEAREST, _MM_ROUND_DOWN, _MM_ROUND_UP,
  2166. // _MM_ROUND_TOWARD_ZERO
  2167. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_MM_SET_ROUNDING_MODE
  2168. FORCE_INLINE void _MM_SET_ROUNDING_MODE(int rounding)
  2169. {
  2170. union {
  2171. fpcr_bitfield field;
  2172. #if defined(__aarch64__)
  2173. uint64_t value;
  2174. #else
  2175. uint32_t value;
  2176. #endif
  2177. } r;
  2178. #if defined(__aarch64__)
  2179. asm volatile("mrs %0, FPCR" : "=r"(r.value)); /* read */
  2180. #else
  2181. asm volatile("vmrs %0, FPSCR" : "=r"(r.value)); /* read */
  2182. #endif
  2183. switch (rounding) {
  2184. case _MM_ROUND_TOWARD_ZERO:
  2185. r.field.bit22 = 1;
  2186. r.field.bit23 = 1;
  2187. break;
  2188. case _MM_ROUND_DOWN:
  2189. r.field.bit22 = 0;
  2190. r.field.bit23 = 1;
  2191. break;
  2192. case _MM_ROUND_UP:
  2193. r.field.bit22 = 1;
  2194. r.field.bit23 = 0;
  2195. break;
  2196. default: //_MM_ROUND_NEAREST
  2197. r.field.bit22 = 0;
  2198. r.field.bit23 = 0;
  2199. }
  2200. #if defined(__aarch64__)
  2201. asm volatile("msr FPCR, %0" ::"r"(r)); /* write */
  2202. #else
  2203. asm volatile("vmsr FPSCR, %0" ::"r"(r)); /* write */
  2204. #endif
  2205. }
  2206. // Copy single-precision (32-bit) floating-point element a to the lower element
  2207. // of dst, and zero the upper 3 elements.
  2208. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_set_ss
  2209. FORCE_INLINE __m128 _mm_set_ss(float a)
  2210. {
  2211. float ALIGN_STRUCT(16) data[4] = {a, 0, 0, 0};
  2212. return vreinterpretq_m128_f32(vld1q_f32(data));
  2213. }
  2214. // Sets the four single-precision, floating-point values to w.
  2215. //
  2216. // r0 := r1 := r2 := r3 := w
  2217. //
  2218. // https://msdn.microsoft.com/en-us/library/vstudio/2x1se8ha(v=vs.100).aspx
  2219. FORCE_INLINE __m128 _mm_set1_ps(float _w)
  2220. {
  2221. return vreinterpretq_m128_f32(vdupq_n_f32(_w));
  2222. }
  2223. FORCE_INLINE void _mm_setcsr(unsigned int a)
  2224. {
  2225. _MM_SET_ROUNDING_MODE(a);
  2226. }
  2227. // Sets the four single-precision, floating-point values to the four inputs in
  2228. // reverse order.
  2229. // https://msdn.microsoft.com/en-us/library/vstudio/d2172ct3(v=vs.100).aspx
  2230. FORCE_INLINE __m128 _mm_setr_ps(float w, float z, float y, float x)
  2231. {
  2232. float ALIGN_STRUCT(16) data[4] = {w, z, y, x};
  2233. return vreinterpretq_m128_f32(vld1q_f32(data));
  2234. }
  2235. // Clears the four single-precision, floating-point values.
  2236. // https://msdn.microsoft.com/en-us/library/vstudio/tk1t2tbz(v=vs.100).aspx
  2237. FORCE_INLINE __m128 _mm_setzero_ps(void)
  2238. {
  2239. return vreinterpretq_m128_f32(vdupq_n_f32(0));
  2240. }
  2241. // Shuffle 16-bit integers in a using the control in imm8, and store the results
  2242. // in dst.
  2243. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_shuffle_pi16
  2244. #if __has_builtin(__builtin_shufflevector)
  2245. #define _mm_shuffle_pi16(a, imm) \
  2246. __extension__({ \
  2247. vreinterpret_m64_s16(__builtin_shufflevector( \
  2248. vreinterpret_s16_m64(a), vreinterpret_s16_m64(a), (imm & 0x3), \
  2249. ((imm >> 2) & 0x3), ((imm >> 4) & 0x3), ((imm >> 6) & 0x3))); \
  2250. })
  2251. #else
  2252. #define _mm_shuffle_pi16(a, imm) \
  2253. __extension__({ \
  2254. int16x4_t ret; \
  2255. ret = \
  2256. vmov_n_s16(vget_lane_s16(vreinterpret_s16_m64(a), (imm) & (0x3))); \
  2257. ret = vset_lane_s16( \
  2258. vget_lane_s16(vreinterpret_s16_m64(a), ((imm) >> 2) & 0x3), ret, \
  2259. 1); \
  2260. ret = vset_lane_s16( \
  2261. vget_lane_s16(vreinterpret_s16_m64(a), ((imm) >> 4) & 0x3), ret, \
  2262. 2); \
  2263. ret = vset_lane_s16( \
  2264. vget_lane_s16(vreinterpret_s16_m64(a), ((imm) >> 6) & 0x3), ret, \
  2265. 3); \
  2266. vreinterpret_m64_s16(ret); \
  2267. })
  2268. #endif
  2269. // Guarantees that every preceding store is globally visible before any
  2270. // subsequent store.
  2271. // https://msdn.microsoft.com/en-us/library/5h2w73d1%28v=vs.90%29.aspx
  2272. FORCE_INLINE void _mm_sfence(void)
  2273. {
  2274. __sync_synchronize();
  2275. }
  2276. // FORCE_INLINE __m128 _mm_shuffle_ps(__m128 a, __m128 b, __constrange(0,255)
  2277. // int imm)
  2278. #if __has_builtin(__builtin_shufflevector)
  2279. #define _mm_shuffle_ps(a, b, imm) \
  2280. __extension__({ \
  2281. float32x4_t _input1 = vreinterpretq_f32_m128(a); \
  2282. float32x4_t _input2 = vreinterpretq_f32_m128(b); \
  2283. float32x4_t _shuf = __builtin_shufflevector( \
  2284. _input1, _input2, (imm) & (0x3), ((imm) >> 2) & 0x3, \
  2285. (((imm) >> 4) & 0x3) + 4, (((imm) >> 6) & 0x3) + 4); \
  2286. vreinterpretq_m128_f32(_shuf); \
  2287. })
  2288. #else // generic
  2289. #define _mm_shuffle_ps(a, b, imm) \
  2290. __extension__({ \
  2291. __m128 ret; \
  2292. switch (imm) { \
  2293. case _MM_SHUFFLE(1, 0, 3, 2): \
  2294. ret = _mm_shuffle_ps_1032((a), (b)); \
  2295. break; \
  2296. case _MM_SHUFFLE(2, 3, 0, 1): \
  2297. ret = _mm_shuffle_ps_2301((a), (b)); \
  2298. break; \
  2299. case _MM_SHUFFLE(0, 3, 2, 1): \
  2300. ret = _mm_shuffle_ps_0321((a), (b)); \
  2301. break; \
  2302. case _MM_SHUFFLE(2, 1, 0, 3): \
  2303. ret = _mm_shuffle_ps_2103((a), (b)); \
  2304. break; \
  2305. case _MM_SHUFFLE(1, 0, 1, 0): \
  2306. ret = _mm_movelh_ps((a), (b)); \
  2307. break; \
  2308. case _MM_SHUFFLE(1, 0, 0, 1): \
  2309. ret = _mm_shuffle_ps_1001((a), (b)); \
  2310. break; \
  2311. case _MM_SHUFFLE(0, 1, 0, 1): \
  2312. ret = _mm_shuffle_ps_0101((a), (b)); \
  2313. break; \
  2314. case _MM_SHUFFLE(3, 2, 1, 0): \
  2315. ret = _mm_shuffle_ps_3210((a), (b)); \
  2316. break; \
  2317. case _MM_SHUFFLE(0, 0, 1, 1): \
  2318. ret = _mm_shuffle_ps_0011((a), (b)); \
  2319. break; \
  2320. case _MM_SHUFFLE(0, 0, 2, 2): \
  2321. ret = _mm_shuffle_ps_0022((a), (b)); \
  2322. break; \
  2323. case _MM_SHUFFLE(2, 2, 0, 0): \
  2324. ret = _mm_shuffle_ps_2200((a), (b)); \
  2325. break; \
  2326. case _MM_SHUFFLE(3, 2, 0, 2): \
  2327. ret = _mm_shuffle_ps_3202((a), (b)); \
  2328. break; \
  2329. case _MM_SHUFFLE(3, 2, 3, 2): \
  2330. ret = _mm_movehl_ps((b), (a)); \
  2331. break; \
  2332. case _MM_SHUFFLE(1, 1, 3, 3): \
  2333. ret = _mm_shuffle_ps_1133((a), (b)); \
  2334. break; \
  2335. case _MM_SHUFFLE(2, 0, 1, 0): \
  2336. ret = _mm_shuffle_ps_2010((a), (b)); \
  2337. break; \
  2338. case _MM_SHUFFLE(2, 0, 0, 1): \
  2339. ret = _mm_shuffle_ps_2001((a), (b)); \
  2340. break; \
  2341. case _MM_SHUFFLE(2, 0, 3, 2): \
  2342. ret = _mm_shuffle_ps_2032((a), (b)); \
  2343. break; \
  2344. default: \
  2345. ret = _mm_shuffle_ps_default((a), (b), (imm)); \
  2346. break; \
  2347. } \
  2348. ret; \
  2349. })
  2350. #endif
  2351. // Computes the approximations of square roots of the four single-precision,
  2352. // floating-point values of a. First computes reciprocal square roots and then
  2353. // reciprocals of the four values.
  2354. //
  2355. // r0 := sqrt(a0)
  2356. // r1 := sqrt(a1)
  2357. // r2 := sqrt(a2)
  2358. // r3 := sqrt(a3)
  2359. //
  2360. // https://msdn.microsoft.com/en-us/library/vstudio/8z67bwwk(v=vs.100).aspx
  2361. FORCE_INLINE __m128 _mm_sqrt_ps(__m128 in)
  2362. {
  2363. #if SSE2NEON_PRECISE_SQRT
  2364. float32x4_t recip = vrsqrteq_f32(vreinterpretq_f32_m128(in));
  2365. // Test for vrsqrteq_f32(0) -> positive infinity case.
  2366. // Change to zero, so that s * 1/sqrt(s) result is zero too.
  2367. const uint32x4_t pos_inf = vdupq_n_u32(0x7F800000);
  2368. const uint32x4_t div_by_zero =
  2369. vceqq_u32(pos_inf, vreinterpretq_u32_f32(recip));
  2370. recip = vreinterpretq_f32_u32(
  2371. vandq_u32(vmvnq_u32(div_by_zero), vreinterpretq_u32_f32(recip)));
  2372. // Additional Netwon-Raphson iteration for accuracy
  2373. recip = vmulq_f32(
  2374. vrsqrtsq_f32(vmulq_f32(recip, recip), vreinterpretq_f32_m128(in)),
  2375. recip);
  2376. recip = vmulq_f32(
  2377. vrsqrtsq_f32(vmulq_f32(recip, recip), vreinterpretq_f32_m128(in)),
  2378. recip);
  2379. // sqrt(s) = s * 1/sqrt(s)
  2380. return vreinterpretq_m128_f32(vmulq_f32(vreinterpretq_f32_m128(in), recip));
  2381. #elif defined(__aarch64__)
  2382. return vreinterpretq_m128_f32(vsqrtq_f32(vreinterpretq_f32_m128(in)));
  2383. #else
  2384. float32x4_t recipsq = vrsqrteq_f32(vreinterpretq_f32_m128(in));
  2385. float32x4_t sq = vrecpeq_f32(recipsq);
  2386. return vreinterpretq_m128_f32(sq);
  2387. #endif
  2388. }
  2389. // Computes the approximation of the square root of the scalar single-precision
  2390. // floating point value of in.
  2391. // https://msdn.microsoft.com/en-us/library/ahfsc22d(v=vs.100).aspx
  2392. FORCE_INLINE __m128 _mm_sqrt_ss(__m128 in)
  2393. {
  2394. float32_t value =
  2395. vgetq_lane_f32(vreinterpretq_f32_m128(_mm_sqrt_ps(in)), 0);
  2396. return vreinterpretq_m128_f32(
  2397. vsetq_lane_f32(value, vreinterpretq_f32_m128(in), 0));
  2398. }
  2399. // Stores four single-precision, floating-point values.
  2400. // https://msdn.microsoft.com/en-us/library/vstudio/s3h4ay6y(v=vs.100).aspx
  2401. FORCE_INLINE void _mm_store_ps(float *p, __m128 a)
  2402. {
  2403. vst1q_f32(p, vreinterpretq_f32_m128(a));
  2404. }
  2405. // Store the lower single-precision (32-bit) floating-point element from a into
  2406. // 4 contiguous elements in memory. mem_addr must be aligned on a 16-byte
  2407. // boundary or a general-protection exception may be generated.
  2408. //
  2409. // MEM[mem_addr+31:mem_addr] := a[31:0]
  2410. // MEM[mem_addr+63:mem_addr+32] := a[31:0]
  2411. // MEM[mem_addr+95:mem_addr+64] := a[31:0]
  2412. // MEM[mem_addr+127:mem_addr+96] := a[31:0]
  2413. //
  2414. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_store_ps1
  2415. FORCE_INLINE void _mm_store_ps1(float *p, __m128 a)
  2416. {
  2417. float32_t a0 = vgetq_lane_f32(vreinterpretq_f32_m128(a), 0);
  2418. vst1q_f32(p, vdupq_n_f32(a0));
  2419. }
  2420. // Stores the lower single - precision, floating - point value.
  2421. // https://msdn.microsoft.com/en-us/library/tzz10fbx(v=vs.100).aspx
  2422. FORCE_INLINE void _mm_store_ss(float *p, __m128 a)
  2423. {
  2424. vst1q_lane_f32(p, vreinterpretq_f32_m128(a), 0);
  2425. }
  2426. // Store the lower single-precision (32-bit) floating-point element from a into
  2427. // 4 contiguous elements in memory. mem_addr must be aligned on a 16-byte
  2428. // boundary or a general-protection exception may be generated.
  2429. //
  2430. // MEM[mem_addr+31:mem_addr] := a[31:0]
  2431. // MEM[mem_addr+63:mem_addr+32] := a[31:0]
  2432. // MEM[mem_addr+95:mem_addr+64] := a[31:0]
  2433. // MEM[mem_addr+127:mem_addr+96] := a[31:0]
  2434. //
  2435. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_store1_ps
  2436. #define _mm_store1_ps _mm_store_ps1
  2437. // Stores the upper two single-precision, floating-point values of a to the
  2438. // address p.
  2439. //
  2440. // *p0 := a2
  2441. // *p1 := a3
  2442. //
  2443. // https://msdn.microsoft.com/en-us/library/a7525fs8(v%3dvs.90).aspx
  2444. FORCE_INLINE void _mm_storeh_pi(__m64 *p, __m128 a)
  2445. {
  2446. *p = vreinterpret_m64_f32(vget_high_f32(a));
  2447. }
  2448. // Stores the lower two single-precision floating point values of a to the
  2449. // address p.
  2450. //
  2451. // *p0 := a0
  2452. // *p1 := a1
  2453. //
  2454. // https://msdn.microsoft.com/en-us/library/h54t98ks(v=vs.90).aspx
  2455. FORCE_INLINE void _mm_storel_pi(__m64 *p, __m128 a)
  2456. {
  2457. *p = vreinterpret_m64_f32(vget_low_f32(a));
  2458. }
  2459. // Store 4 single-precision (32-bit) floating-point elements from a into memory
  2460. // in reverse order. mem_addr must be aligned on a 16-byte boundary or a
  2461. // general-protection exception may be generated.
  2462. //
  2463. // MEM[mem_addr+31:mem_addr] := a[127:96]
  2464. // MEM[mem_addr+63:mem_addr+32] := a[95:64]
  2465. // MEM[mem_addr+95:mem_addr+64] := a[63:32]
  2466. // MEM[mem_addr+127:mem_addr+96] := a[31:0]
  2467. //
  2468. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storer_ps
  2469. FORCE_INLINE void _mm_storer_ps(float *p, __m128 a)
  2470. {
  2471. float32x4_t tmp = vrev64q_f32(vreinterpretq_f32_m128(a));
  2472. float32x4_t rev = vextq_f32(tmp, tmp, 2);
  2473. vst1q_f32(p, rev);
  2474. }
  2475. // Stores four single-precision, floating-point values.
  2476. // https://msdn.microsoft.com/en-us/library/44e30x22(v=vs.100).aspx
  2477. FORCE_INLINE void _mm_storeu_ps(float *p, __m128 a)
  2478. {
  2479. vst1q_f32(p, vreinterpretq_f32_m128(a));
  2480. }
  2481. // Stores 16-bits of integer data a at the address p.
  2482. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storeu_si16
  2483. FORCE_INLINE void _mm_storeu_si16(void *p, __m128i a)
  2484. {
  2485. vst1q_lane_s16((int16_t *) p, vreinterpretq_s16_m128i(a), 0);
  2486. }
  2487. // Stores 64-bits of integer data a at the address p.
  2488. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storeu_si64
  2489. FORCE_INLINE void _mm_storeu_si64(void *p, __m128i a)
  2490. {
  2491. vst1q_lane_s64((int64_t *) p, vreinterpretq_s64_m128i(a), 0);
  2492. }
  2493. // Store 64-bits of integer data from a into memory using a non-temporal memory
  2494. // hint.
  2495. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_stream_pi
  2496. FORCE_INLINE void _mm_stream_pi(__m64 *p, __m64 a)
  2497. {
  2498. vst1_s64((int64_t *) p, vreinterpret_s64_m64(a));
  2499. }
  2500. // Store 128-bits (composed of 4 packed single-precision (32-bit) floating-
  2501. // point elements) from a into memory using a non-temporal memory hint.
  2502. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_stream_ps
  2503. FORCE_INLINE void _mm_stream_ps(float *p, __m128 a)
  2504. {
  2505. #if __has_builtin(__builtin_nontemporal_store)
  2506. __builtin_nontemporal_store(a, (float32x4_t *) p);
  2507. #else
  2508. vst1q_f32(p, vreinterpretq_f32_m128(a));
  2509. #endif
  2510. }
  2511. // Subtracts the four single-precision, floating-point values of a and b.
  2512. //
  2513. // r0 := a0 - b0
  2514. // r1 := a1 - b1
  2515. // r2 := a2 - b2
  2516. // r3 := a3 - b3
  2517. //
  2518. // https://msdn.microsoft.com/en-us/library/vstudio/1zad2k61(v=vs.100).aspx
  2519. FORCE_INLINE __m128 _mm_sub_ps(__m128 a, __m128 b)
  2520. {
  2521. return vreinterpretq_m128_f32(
  2522. vsubq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  2523. }
  2524. // Subtract the lower single-precision (32-bit) floating-point element in b from
  2525. // the lower single-precision (32-bit) floating-point element in a, store the
  2526. // result in the lower element of dst, and copy the upper 3 packed elements from
  2527. // a to the upper elements of dst.
  2528. //
  2529. // dst[31:0] := a[31:0] - b[31:0]
  2530. // dst[127:32] := a[127:32]
  2531. //
  2532. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sub_ss
  2533. FORCE_INLINE __m128 _mm_sub_ss(__m128 a, __m128 b)
  2534. {
  2535. return _mm_move_ss(a, _mm_sub_ps(a, b));
  2536. }
  2537. // Macro: Transpose the 4x4 matrix formed by the 4 rows of single-precision
  2538. // (32-bit) floating-point elements in row0, row1, row2, and row3, and store the
  2539. // transposed matrix in these vectors (row0 now contains column 0, etc.).
  2540. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=MM_TRANSPOSE4_PS
  2541. #define _MM_TRANSPOSE4_PS(row0, row1, row2, row3) \
  2542. do { \
  2543. float32x4x2_t ROW01 = vtrnq_f32(row0, row1); \
  2544. float32x4x2_t ROW23 = vtrnq_f32(row2, row3); \
  2545. row0 = vcombine_f32(vget_low_f32(ROW01.val[0]), \
  2546. vget_low_f32(ROW23.val[0])); \
  2547. row1 = vcombine_f32(vget_low_f32(ROW01.val[1]), \
  2548. vget_low_f32(ROW23.val[1])); \
  2549. row2 = vcombine_f32(vget_high_f32(ROW01.val[0]), \
  2550. vget_high_f32(ROW23.val[0])); \
  2551. row3 = vcombine_f32(vget_high_f32(ROW01.val[1]), \
  2552. vget_high_f32(ROW23.val[1])); \
  2553. } while (0)
  2554. // according to the documentation, these intrinsics behave the same as the
  2555. // non-'u' versions. We'll just alias them here.
  2556. #define _mm_ucomieq_ss _mm_comieq_ss
  2557. #define _mm_ucomige_ss _mm_comige_ss
  2558. #define _mm_ucomigt_ss _mm_comigt_ss
  2559. #define _mm_ucomile_ss _mm_comile_ss
  2560. #define _mm_ucomilt_ss _mm_comilt_ss
  2561. #define _mm_ucomineq_ss _mm_comineq_ss
  2562. // Return vector of type __m128i with undefined elements.
  2563. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=mm_undefined_si128
  2564. FORCE_INLINE __m128i _mm_undefined_si128(void)
  2565. {
  2566. #if defined(__GNUC__) || defined(__clang__)
  2567. #pragma GCC diagnostic push
  2568. #pragma GCC diagnostic ignored "-Wuninitialized"
  2569. #endif
  2570. __m128i a;
  2571. return a;
  2572. #if defined(__GNUC__) || defined(__clang__)
  2573. #pragma GCC diagnostic pop
  2574. #endif
  2575. }
  2576. // Return vector of type __m128 with undefined elements.
  2577. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_undefined_ps
  2578. FORCE_INLINE __m128 _mm_undefined_ps(void)
  2579. {
  2580. #if defined(__GNUC__) || defined(__clang__)
  2581. #pragma GCC diagnostic push
  2582. #pragma GCC diagnostic ignored "-Wuninitialized"
  2583. #endif
  2584. __m128 a;
  2585. return a;
  2586. #if defined(__GNUC__) || defined(__clang__)
  2587. #pragma GCC diagnostic pop
  2588. #endif
  2589. }
  2590. // Selects and interleaves the upper two single-precision, floating-point values
  2591. // from a and b.
  2592. //
  2593. // r0 := a2
  2594. // r1 := b2
  2595. // r2 := a3
  2596. // r3 := b3
  2597. //
  2598. // https://msdn.microsoft.com/en-us/library/skccxx7d%28v=vs.90%29.aspx
  2599. FORCE_INLINE __m128 _mm_unpackhi_ps(__m128 a, __m128 b)
  2600. {
  2601. #if defined(__aarch64__)
  2602. return vreinterpretq_m128_f32(
  2603. vzip2q_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  2604. #else
  2605. float32x2_t a1 = vget_high_f32(vreinterpretq_f32_m128(a));
  2606. float32x2_t b1 = vget_high_f32(vreinterpretq_f32_m128(b));
  2607. float32x2x2_t result = vzip_f32(a1, b1);
  2608. return vreinterpretq_m128_f32(vcombine_f32(result.val[0], result.val[1]));
  2609. #endif
  2610. }
  2611. // Selects and interleaves the lower two single-precision, floating-point values
  2612. // from a and b.
  2613. //
  2614. // r0 := a0
  2615. // r1 := b0
  2616. // r2 := a1
  2617. // r3 := b1
  2618. //
  2619. // https://msdn.microsoft.com/en-us/library/25st103b%28v=vs.90%29.aspx
  2620. FORCE_INLINE __m128 _mm_unpacklo_ps(__m128 a, __m128 b)
  2621. {
  2622. #if defined(__aarch64__)
  2623. return vreinterpretq_m128_f32(
  2624. vzip1q_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  2625. #else
  2626. float32x2_t a1 = vget_low_f32(vreinterpretq_f32_m128(a));
  2627. float32x2_t b1 = vget_low_f32(vreinterpretq_f32_m128(b));
  2628. float32x2x2_t result = vzip_f32(a1, b1);
  2629. return vreinterpretq_m128_f32(vcombine_f32(result.val[0], result.val[1]));
  2630. #endif
  2631. }
  2632. // Computes bitwise EXOR (exclusive-or) of the four single-precision,
  2633. // floating-point values of a and b.
  2634. // https://msdn.microsoft.com/en-us/library/ss6k3wk8(v=vs.100).aspx
  2635. FORCE_INLINE __m128 _mm_xor_ps(__m128 a, __m128 b)
  2636. {
  2637. return vreinterpretq_m128_s32(
  2638. veorq_s32(vreinterpretq_s32_m128(a), vreinterpretq_s32_m128(b)));
  2639. }
  2640. /* SSE2 */
  2641. // Adds the 8 signed or unsigned 16-bit integers in a to the 8 signed or
  2642. // unsigned 16-bit integers in b.
  2643. // https://msdn.microsoft.com/en-us/library/fceha5k4(v=vs.100).aspx
  2644. FORCE_INLINE __m128i _mm_add_epi16(__m128i a, __m128i b)
  2645. {
  2646. return vreinterpretq_m128i_s16(
  2647. vaddq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  2648. }
  2649. // Adds the 4 signed or unsigned 32-bit integers in a to the 4 signed or
  2650. // unsigned 32-bit integers in b.
  2651. //
  2652. // r0 := a0 + b0
  2653. // r1 := a1 + b1
  2654. // r2 := a2 + b2
  2655. // r3 := a3 + b3
  2656. //
  2657. // https://msdn.microsoft.com/en-us/library/vstudio/09xs4fkk(v=vs.100).aspx
  2658. FORCE_INLINE __m128i _mm_add_epi32(__m128i a, __m128i b)
  2659. {
  2660. return vreinterpretq_m128i_s32(
  2661. vaddq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  2662. }
  2663. // Adds the 4 signed or unsigned 64-bit integers in a to the 4 signed or
  2664. // unsigned 32-bit integers in b.
  2665. // https://msdn.microsoft.com/en-us/library/vstudio/09xs4fkk(v=vs.100).aspx
  2666. FORCE_INLINE __m128i _mm_add_epi64(__m128i a, __m128i b)
  2667. {
  2668. return vreinterpretq_m128i_s64(
  2669. vaddq_s64(vreinterpretq_s64_m128i(a), vreinterpretq_s64_m128i(b)));
  2670. }
  2671. // Adds the 16 signed or unsigned 8-bit integers in a to the 16 signed or
  2672. // unsigned 8-bit integers in b.
  2673. // https://technet.microsoft.com/en-us/subscriptions/yc7tcyzs(v=vs.90)
  2674. FORCE_INLINE __m128i _mm_add_epi8(__m128i a, __m128i b)
  2675. {
  2676. return vreinterpretq_m128i_s8(
  2677. vaddq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  2678. }
  2679. // Add packed double-precision (64-bit) floating-point elements in a and b, and
  2680. // store the results in dst.
  2681. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_add_pd
  2682. FORCE_INLINE __m128d _mm_add_pd(__m128d a, __m128d b)
  2683. {
  2684. #if defined(__aarch64__)
  2685. return vreinterpretq_m128d_f64(
  2686. vaddq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  2687. #else
  2688. double *da = (double *) &a;
  2689. double *db = (double *) &b;
  2690. double c[2];
  2691. c[0] = da[0] + db[0];
  2692. c[1] = da[1] + db[1];
  2693. return vld1q_f32((float32_t *) c);
  2694. #endif
  2695. }
  2696. // Add the lower double-precision (64-bit) floating-point element in a and b,
  2697. // store the result in the lower element of dst, and copy the upper element from
  2698. // a to the upper element of dst.
  2699. //
  2700. // dst[63:0] := a[63:0] + b[63:0]
  2701. // dst[127:64] := a[127:64]
  2702. //
  2703. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_add_sd
  2704. FORCE_INLINE __m128d _mm_add_sd(__m128d a, __m128d b)
  2705. {
  2706. #if defined(__aarch64__)
  2707. return _mm_move_sd(a, _mm_add_pd(a, b));
  2708. #else
  2709. double *da = (double *) &a;
  2710. double *db = (double *) &b;
  2711. double c[2];
  2712. c[0] = da[0] + db[0];
  2713. c[1] = da[1];
  2714. return vld1q_f32((float32_t *) c);
  2715. #endif
  2716. }
  2717. // Add 64-bit integers a and b, and store the result in dst.
  2718. //
  2719. // dst[63:0] := a[63:0] + b[63:0]
  2720. //
  2721. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_add_si64
  2722. FORCE_INLINE __m64 _mm_add_si64(__m64 a, __m64 b)
  2723. {
  2724. return vreinterpret_m64_s64(
  2725. vadd_s64(vreinterpret_s64_m64(a), vreinterpret_s64_m64(b)));
  2726. }
  2727. // Adds the 8 signed 16-bit integers in a to the 8 signed 16-bit integers in b
  2728. // and saturates.
  2729. //
  2730. // r0 := SignedSaturate(a0 + b0)
  2731. // r1 := SignedSaturate(a1 + b1)
  2732. // ...
  2733. // r7 := SignedSaturate(a7 + b7)
  2734. //
  2735. // https://msdn.microsoft.com/en-us/library/1a306ef8(v=vs.100).aspx
  2736. FORCE_INLINE __m128i _mm_adds_epi16(__m128i a, __m128i b)
  2737. {
  2738. return vreinterpretq_m128i_s16(
  2739. vqaddq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  2740. }
  2741. // Add packed signed 8-bit integers in a and b using saturation, and store the
  2742. // results in dst.
  2743. //
  2744. // FOR j := 0 to 15
  2745. // i := j*8
  2746. // dst[i+7:i] := Saturate8( a[i+7:i] + b[i+7:i] )
  2747. // ENDFOR
  2748. //
  2749. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_adds_epi8
  2750. FORCE_INLINE __m128i _mm_adds_epi8(__m128i a, __m128i b)
  2751. {
  2752. return vreinterpretq_m128i_s8(
  2753. vqaddq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  2754. }
  2755. // Add packed unsigned 16-bit integers in a and b using saturation, and store
  2756. // the results in dst.
  2757. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_adds_epu16
  2758. FORCE_INLINE __m128i _mm_adds_epu16(__m128i a, __m128i b)
  2759. {
  2760. return vreinterpretq_m128i_u16(
  2761. vqaddq_u16(vreinterpretq_u16_m128i(a), vreinterpretq_u16_m128i(b)));
  2762. }
  2763. // Adds the 16 unsigned 8-bit integers in a to the 16 unsigned 8-bit integers in
  2764. // b and saturates..
  2765. // https://msdn.microsoft.com/en-us/library/9hahyddy(v=vs.100).aspx
  2766. FORCE_INLINE __m128i _mm_adds_epu8(__m128i a, __m128i b)
  2767. {
  2768. return vreinterpretq_m128i_u8(
  2769. vqaddq_u8(vreinterpretq_u8_m128i(a), vreinterpretq_u8_m128i(b)));
  2770. }
  2771. // Compute the bitwise AND of packed double-precision (64-bit) floating-point
  2772. // elements in a and b, and store the results in dst.
  2773. //
  2774. // FOR j := 0 to 1
  2775. // i := j*64
  2776. // dst[i+63:i] := a[i+63:i] AND b[i+63:i]
  2777. // ENDFOR
  2778. //
  2779. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_and_pd
  2780. FORCE_INLINE __m128d _mm_and_pd(__m128d a, __m128d b)
  2781. {
  2782. return vreinterpretq_m128d_s64(
  2783. vandq_s64(vreinterpretq_s64_m128d(a), vreinterpretq_s64_m128d(b)));
  2784. }
  2785. // Computes the bitwise AND of the 128-bit value in a and the 128-bit value in
  2786. // b.
  2787. //
  2788. // r := a & b
  2789. //
  2790. // https://msdn.microsoft.com/en-us/library/vstudio/6d1txsa8(v=vs.100).aspx
  2791. FORCE_INLINE __m128i _mm_and_si128(__m128i a, __m128i b)
  2792. {
  2793. return vreinterpretq_m128i_s32(
  2794. vandq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  2795. }
  2796. // Compute the bitwise NOT of packed double-precision (64-bit) floating-point
  2797. // elements in a and then AND with b, and store the results in dst.
  2798. //
  2799. // FOR j := 0 to 1
  2800. // i := j*64
  2801. // dst[i+63:i] := ((NOT a[i+63:i]) AND b[i+63:i])
  2802. // ENDFOR
  2803. //
  2804. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_andnot_pd
  2805. FORCE_INLINE __m128d _mm_andnot_pd(__m128d a, __m128d b)
  2806. {
  2807. // *NOTE* argument swap
  2808. return vreinterpretq_m128d_s64(
  2809. vbicq_s64(vreinterpretq_s64_m128d(b), vreinterpretq_s64_m128d(a)));
  2810. }
  2811. // Computes the bitwise AND of the 128-bit value in b and the bitwise NOT of the
  2812. // 128-bit value in a.
  2813. //
  2814. // r := (~a) & b
  2815. //
  2816. // https://msdn.microsoft.com/en-us/library/vstudio/1beaceh8(v=vs.100).aspx
  2817. FORCE_INLINE __m128i _mm_andnot_si128(__m128i a, __m128i b)
  2818. {
  2819. return vreinterpretq_m128i_s32(
  2820. vbicq_s32(vreinterpretq_s32_m128i(b),
  2821. vreinterpretq_s32_m128i(a))); // *NOTE* argument swap
  2822. }
  2823. // Computes the average of the 8 unsigned 16-bit integers in a and the 8
  2824. // unsigned 16-bit integers in b and rounds.
  2825. //
  2826. // r0 := (a0 + b0) / 2
  2827. // r1 := (a1 + b1) / 2
  2828. // ...
  2829. // r7 := (a7 + b7) / 2
  2830. //
  2831. // https://msdn.microsoft.com/en-us/library/vstudio/y13ca3c8(v=vs.90).aspx
  2832. FORCE_INLINE __m128i _mm_avg_epu16(__m128i a, __m128i b)
  2833. {
  2834. return (__m128i) vrhaddq_u16(vreinterpretq_u16_m128i(a),
  2835. vreinterpretq_u16_m128i(b));
  2836. }
  2837. // Computes the average of the 16 unsigned 8-bit integers in a and the 16
  2838. // unsigned 8-bit integers in b and rounds.
  2839. //
  2840. // r0 := (a0 + b0) / 2
  2841. // r1 := (a1 + b1) / 2
  2842. // ...
  2843. // r15 := (a15 + b15) / 2
  2844. //
  2845. // https://msdn.microsoft.com/en-us/library/vstudio/8zwh554a(v%3dvs.90).aspx
  2846. FORCE_INLINE __m128i _mm_avg_epu8(__m128i a, __m128i b)
  2847. {
  2848. return vreinterpretq_m128i_u8(
  2849. vrhaddq_u8(vreinterpretq_u8_m128i(a), vreinterpretq_u8_m128i(b)));
  2850. }
  2851. // Shift a left by imm8 bytes while shifting in zeros, and store the results in
  2852. // dst.
  2853. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_bslli_si128
  2854. #define _mm_bslli_si128(a, imm) _mm_slli_si128(a, imm)
  2855. // Shift a right by imm8 bytes while shifting in zeros, and store the results in
  2856. // dst.
  2857. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_bsrli_si128
  2858. #define _mm_bsrli_si128(a, imm) _mm_srli_si128(a, imm)
  2859. // Cast vector of type __m128d to type __m128. This intrinsic is only used for
  2860. // compilation and does not generate any instructions, thus it has zero latency.
  2861. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_castpd_ps
  2862. FORCE_INLINE __m128 _mm_castpd_ps(__m128d a)
  2863. {
  2864. return vreinterpretq_m128_s64(vreinterpretq_s64_m128d(a));
  2865. }
  2866. // Cast vector of type __m128d to type __m128i. This intrinsic is only used for
  2867. // compilation and does not generate any instructions, thus it has zero latency.
  2868. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_castpd_si128
  2869. FORCE_INLINE __m128i _mm_castpd_si128(__m128d a)
  2870. {
  2871. return vreinterpretq_m128i_s64(vreinterpretq_s64_m128d(a));
  2872. }
  2873. // Cast vector of type __m128 to type __m128d. This intrinsic is only used for
  2874. // compilation and does not generate any instructions, thus it has zero latency.
  2875. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_castps_pd
  2876. FORCE_INLINE __m128d _mm_castps_pd(__m128 a)
  2877. {
  2878. return vreinterpretq_m128d_s32(vreinterpretq_s32_m128(a));
  2879. }
  2880. // Applies a type cast to reinterpret four 32-bit floating point values passed
  2881. // in as a 128-bit parameter as packed 32-bit integers.
  2882. // https://msdn.microsoft.com/en-us/library/bb514099.aspx
  2883. FORCE_INLINE __m128i _mm_castps_si128(__m128 a)
  2884. {
  2885. return vreinterpretq_m128i_s32(vreinterpretq_s32_m128(a));
  2886. }
  2887. // Cast vector of type __m128i to type __m128d. This intrinsic is only used for
  2888. // compilation and does not generate any instructions, thus it has zero latency.
  2889. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_castsi128_pd
  2890. FORCE_INLINE __m128d _mm_castsi128_pd(__m128i a)
  2891. {
  2892. #if defined(__aarch64__)
  2893. return vreinterpretq_m128d_f64(vreinterpretq_f64_m128i(a));
  2894. #else
  2895. return vreinterpretq_m128d_f32(vreinterpretq_f32_m128i(a));
  2896. #endif
  2897. }
  2898. // Applies a type cast to reinterpret four 32-bit integers passed in as a
  2899. // 128-bit parameter as packed 32-bit floating point values.
  2900. // https://msdn.microsoft.com/en-us/library/bb514029.aspx
  2901. FORCE_INLINE __m128 _mm_castsi128_ps(__m128i a)
  2902. {
  2903. return vreinterpretq_m128_s32(vreinterpretq_s32_m128i(a));
  2904. }
  2905. // Cache line containing p is flushed and invalidated from all caches in the
  2906. // coherency domain. :
  2907. // https://msdn.microsoft.com/en-us/library/ba08y07y(v=vs.100).aspx
  2908. FORCE_INLINE void _mm_clflush(void const *p)
  2909. {
  2910. (void) p;
  2911. // no corollary for Neon?
  2912. }
  2913. // Compares the 8 signed or unsigned 16-bit integers in a and the 8 signed or
  2914. // unsigned 16-bit integers in b for equality.
  2915. // https://msdn.microsoft.com/en-us/library/2ay060te(v=vs.100).aspx
  2916. FORCE_INLINE __m128i _mm_cmpeq_epi16(__m128i a, __m128i b)
  2917. {
  2918. return vreinterpretq_m128i_u16(
  2919. vceqq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  2920. }
  2921. // Compare packed 32-bit integers in a and b for equality, and store the results
  2922. // in dst
  2923. FORCE_INLINE __m128i _mm_cmpeq_epi32(__m128i a, __m128i b)
  2924. {
  2925. return vreinterpretq_m128i_u32(
  2926. vceqq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  2927. }
  2928. // Compares the 16 signed or unsigned 8-bit integers in a and the 16 signed or
  2929. // unsigned 8-bit integers in b for equality.
  2930. // https://msdn.microsoft.com/en-us/library/windows/desktop/bz5xk21a(v=vs.90).aspx
  2931. FORCE_INLINE __m128i _mm_cmpeq_epi8(__m128i a, __m128i b)
  2932. {
  2933. return vreinterpretq_m128i_u8(
  2934. vceqq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  2935. }
  2936. // Compare packed double-precision (64-bit) floating-point elements in a and b
  2937. // for equality, and store the results in dst.
  2938. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpeq_pd
  2939. FORCE_INLINE __m128d _mm_cmpeq_pd(__m128d a, __m128d b)
  2940. {
  2941. #if defined(__aarch64__)
  2942. return vreinterpretq_m128d_u64(
  2943. vceqq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  2944. #else
  2945. // (a == b) -> (a_lo == b_lo) && (a_hi == b_hi)
  2946. uint32x4_t cmp =
  2947. vceqq_u32(vreinterpretq_u32_m128d(a), vreinterpretq_u32_m128d(b));
  2948. uint32x4_t swapped = vrev64q_u32(cmp);
  2949. return vreinterpretq_m128d_u32(vandq_u32(cmp, swapped));
  2950. #endif
  2951. }
  2952. // Compare the lower double-precision (64-bit) floating-point elements in a and
  2953. // b for equality, store the result in the lower element of dst, and copy the
  2954. // upper element from a to the upper element of dst.
  2955. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpeq_sd
  2956. FORCE_INLINE __m128d _mm_cmpeq_sd(__m128d a, __m128d b)
  2957. {
  2958. return _mm_move_sd(a, _mm_cmpeq_pd(a, b));
  2959. }
  2960. // Compare packed double-precision (64-bit) floating-point elements in a and b
  2961. // for greater-than-or-equal, and store the results in dst.
  2962. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpge_pd
  2963. FORCE_INLINE __m128d _mm_cmpge_pd(__m128d a, __m128d b)
  2964. {
  2965. #if defined(__aarch64__)
  2966. return vreinterpretq_m128d_u64(
  2967. vcgeq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  2968. #else
  2969. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  2970. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  2971. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  2972. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  2973. uint64_t d[2];
  2974. d[0] = (*(double *) &a0) >= (*(double *) &b0) ? ~UINT64_C(0) : UINT64_C(0);
  2975. d[1] = (*(double *) &a1) >= (*(double *) &b1) ? ~UINT64_C(0) : UINT64_C(0);
  2976. return vreinterpretq_m128d_u64(vld1q_u64(d));
  2977. #endif
  2978. }
  2979. // Compare the lower double-precision (64-bit) floating-point elements in a and
  2980. // b for greater-than-or-equal, store the result in the lower element of dst,
  2981. // and copy the upper element from a to the upper element of dst.
  2982. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpge_sd
  2983. FORCE_INLINE __m128d _mm_cmpge_sd(__m128d a, __m128d b)
  2984. {
  2985. #if defined(__aarch64__)
  2986. return _mm_move_sd(a, _mm_cmpge_pd(a, b));
  2987. #else
  2988. // expand "_mm_cmpge_pd()" to reduce unnecessary operations
  2989. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  2990. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  2991. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  2992. uint64_t d[2];
  2993. d[0] = (*(double *) &a0) >= (*(double *) &b0) ? ~UINT64_C(0) : UINT64_C(0);
  2994. d[1] = a1;
  2995. return vreinterpretq_m128d_u64(vld1q_u64(d));
  2996. #endif
  2997. }
  2998. // Compares the 8 signed 16-bit integers in a and the 8 signed 16-bit integers
  2999. // in b for greater than.
  3000. //
  3001. // r0 := (a0 > b0) ? 0xffff : 0x0
  3002. // r1 := (a1 > b1) ? 0xffff : 0x0
  3003. // ...
  3004. // r7 := (a7 > b7) ? 0xffff : 0x0
  3005. //
  3006. // https://technet.microsoft.com/en-us/library/xd43yfsa(v=vs.100).aspx
  3007. FORCE_INLINE __m128i _mm_cmpgt_epi16(__m128i a, __m128i b)
  3008. {
  3009. return vreinterpretq_m128i_u16(
  3010. vcgtq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  3011. }
  3012. // Compares the 4 signed 32-bit integers in a and the 4 signed 32-bit integers
  3013. // in b for greater than.
  3014. // https://msdn.microsoft.com/en-us/library/vstudio/1s9f2z0y(v=vs.100).aspx
  3015. FORCE_INLINE __m128i _mm_cmpgt_epi32(__m128i a, __m128i b)
  3016. {
  3017. return vreinterpretq_m128i_u32(
  3018. vcgtq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  3019. }
  3020. // Compares the 16 signed 8-bit integers in a and the 16 signed 8-bit integers
  3021. // in b for greater than.
  3022. //
  3023. // r0 := (a0 > b0) ? 0xff : 0x0
  3024. // r1 := (a1 > b1) ? 0xff : 0x0
  3025. // ...
  3026. // r15 := (a15 > b15) ? 0xff : 0x0
  3027. //
  3028. // https://msdn.microsoft.com/zh-tw/library/wf45zt2b(v=vs.100).aspx
  3029. FORCE_INLINE __m128i _mm_cmpgt_epi8(__m128i a, __m128i b)
  3030. {
  3031. return vreinterpretq_m128i_u8(
  3032. vcgtq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  3033. }
  3034. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3035. // for greater-than, and store the results in dst.
  3036. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpgt_pd
  3037. FORCE_INLINE __m128d _mm_cmpgt_pd(__m128d a, __m128d b)
  3038. {
  3039. #if defined(__aarch64__)
  3040. return vreinterpretq_m128d_u64(
  3041. vcgtq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  3042. #else
  3043. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3044. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3045. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3046. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3047. uint64_t d[2];
  3048. d[0] = (*(double *) &a0) > (*(double *) &b0) ? ~UINT64_C(0) : UINT64_C(0);
  3049. d[1] = (*(double *) &a1) > (*(double *) &b1) ? ~UINT64_C(0) : UINT64_C(0);
  3050. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3051. #endif
  3052. }
  3053. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3054. // b for greater-than, store the result in the lower element of dst, and copy
  3055. // the upper element from a to the upper element of dst.
  3056. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpgt_sd
  3057. FORCE_INLINE __m128d _mm_cmpgt_sd(__m128d a, __m128d b)
  3058. {
  3059. #if defined(__aarch64__)
  3060. return _mm_move_sd(a, _mm_cmpgt_pd(a, b));
  3061. #else
  3062. // expand "_mm_cmpge_pd()" to reduce unnecessary operations
  3063. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3064. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3065. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3066. uint64_t d[2];
  3067. d[0] = (*(double *) &a0) > (*(double *) &b0) ? ~UINT64_C(0) : UINT64_C(0);
  3068. d[1] = a1;
  3069. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3070. #endif
  3071. }
  3072. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3073. // for less-than-or-equal, and store the results in dst.
  3074. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmple_pd
  3075. FORCE_INLINE __m128d _mm_cmple_pd(__m128d a, __m128d b)
  3076. {
  3077. #if defined(__aarch64__)
  3078. return vreinterpretq_m128d_u64(
  3079. vcleq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  3080. #else
  3081. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3082. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3083. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3084. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3085. uint64_t d[2];
  3086. d[0] = (*(double *) &a0) <= (*(double *) &b0) ? ~UINT64_C(0) : UINT64_C(0);
  3087. d[1] = (*(double *) &a1) <= (*(double *) &b1) ? ~UINT64_C(0) : UINT64_C(0);
  3088. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3089. #endif
  3090. }
  3091. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3092. // b for less-than-or-equal, store the result in the lower element of dst, and
  3093. // copy the upper element from a to the upper element of dst.
  3094. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmple_sd
  3095. FORCE_INLINE __m128d _mm_cmple_sd(__m128d a, __m128d b)
  3096. {
  3097. #if defined(__aarch64__)
  3098. return _mm_move_sd(a, _mm_cmple_pd(a, b));
  3099. #else
  3100. // expand "_mm_cmpge_pd()" to reduce unnecessary operations
  3101. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3102. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3103. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3104. uint64_t d[2];
  3105. d[0] = (*(double *) &a0) <= (*(double *) &b0) ? ~UINT64_C(0) : UINT64_C(0);
  3106. d[1] = a1;
  3107. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3108. #endif
  3109. }
  3110. // Compares the 8 signed 16-bit integers in a and the 8 signed 16-bit integers
  3111. // in b for less than.
  3112. //
  3113. // r0 := (a0 < b0) ? 0xffff : 0x0
  3114. // r1 := (a1 < b1) ? 0xffff : 0x0
  3115. // ...
  3116. // r7 := (a7 < b7) ? 0xffff : 0x0
  3117. //
  3118. // https://technet.microsoft.com/en-us/library/t863edb2(v=vs.100).aspx
  3119. FORCE_INLINE __m128i _mm_cmplt_epi16(__m128i a, __m128i b)
  3120. {
  3121. return vreinterpretq_m128i_u16(
  3122. vcltq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  3123. }
  3124. // Compares the 4 signed 32-bit integers in a and the 4 signed 32-bit integers
  3125. // in b for less than.
  3126. // https://msdn.microsoft.com/en-us/library/vstudio/4ak0bf5d(v=vs.100).aspx
  3127. FORCE_INLINE __m128i _mm_cmplt_epi32(__m128i a, __m128i b)
  3128. {
  3129. return vreinterpretq_m128i_u32(
  3130. vcltq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  3131. }
  3132. // Compares the 16 signed 8-bit integers in a and the 16 signed 8-bit integers
  3133. // in b for lesser than.
  3134. // https://msdn.microsoft.com/en-us/library/windows/desktop/9s46csht(v=vs.90).aspx
  3135. FORCE_INLINE __m128i _mm_cmplt_epi8(__m128i a, __m128i b)
  3136. {
  3137. return vreinterpretq_m128i_u8(
  3138. vcltq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  3139. }
  3140. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3141. // for less-than, and store the results in dst.
  3142. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmplt_pd
  3143. FORCE_INLINE __m128d _mm_cmplt_pd(__m128d a, __m128d b)
  3144. {
  3145. #if defined(__aarch64__)
  3146. return vreinterpretq_m128d_u64(
  3147. vcltq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  3148. #else
  3149. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3150. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3151. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3152. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3153. uint64_t d[2];
  3154. d[0] = (*(double *) &a0) < (*(double *) &b0) ? ~UINT64_C(0) : UINT64_C(0);
  3155. d[1] = (*(double *) &a1) < (*(double *) &b1) ? ~UINT64_C(0) : UINT64_C(0);
  3156. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3157. #endif
  3158. }
  3159. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3160. // b for less-than, store the result in the lower element of dst, and copy the
  3161. // upper element from a to the upper element of dst.
  3162. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmplt_sd
  3163. FORCE_INLINE __m128d _mm_cmplt_sd(__m128d a, __m128d b)
  3164. {
  3165. #if defined(__aarch64__)
  3166. return _mm_move_sd(a, _mm_cmplt_pd(a, b));
  3167. #else
  3168. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3169. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3170. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3171. uint64_t d[2];
  3172. d[0] = (*(double *) &a0) < (*(double *) &b0) ? ~UINT64_C(0) : UINT64_C(0);
  3173. d[1] = a1;
  3174. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3175. #endif
  3176. }
  3177. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3178. // for not-equal, and store the results in dst.
  3179. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpneq_pd
  3180. FORCE_INLINE __m128d _mm_cmpneq_pd(__m128d a, __m128d b)
  3181. {
  3182. #if defined(__aarch64__)
  3183. return vreinterpretq_m128d_s32(vmvnq_s32(vreinterpretq_s32_u64(
  3184. vceqq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)))));
  3185. #else
  3186. // (a == b) -> (a_lo == b_lo) && (a_hi == b_hi)
  3187. uint32x4_t cmp =
  3188. vceqq_u32(vreinterpretq_u32_m128d(a), vreinterpretq_u32_m128d(b));
  3189. uint32x4_t swapped = vrev64q_u32(cmp);
  3190. return vreinterpretq_m128d_u32(vmvnq_u32(vandq_u32(cmp, swapped)));
  3191. #endif
  3192. }
  3193. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3194. // b for not-equal, store the result in the lower element of dst, and copy the
  3195. // upper element from a to the upper element of dst.
  3196. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpneq_sd
  3197. FORCE_INLINE __m128d _mm_cmpneq_sd(__m128d a, __m128d b)
  3198. {
  3199. return _mm_move_sd(a, _mm_cmpneq_pd(a, b));
  3200. }
  3201. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3202. // for not-greater-than-or-equal, and store the results in dst.
  3203. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpnge_pd
  3204. FORCE_INLINE __m128d _mm_cmpnge_pd(__m128d a, __m128d b)
  3205. {
  3206. #if defined(__aarch64__)
  3207. return vreinterpretq_m128d_u64(veorq_u64(
  3208. vcgeq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)),
  3209. vdupq_n_u64(UINT64_MAX)));
  3210. #else
  3211. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3212. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3213. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3214. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3215. uint64_t d[2];
  3216. d[0] =
  3217. !((*(double *) &a0) >= (*(double *) &b0)) ? ~UINT64_C(0) : UINT64_C(0);
  3218. d[1] =
  3219. !((*(double *) &a1) >= (*(double *) &b1)) ? ~UINT64_C(0) : UINT64_C(0);
  3220. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3221. #endif
  3222. }
  3223. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3224. // b for not-greater-than-or-equal, store the result in the lower element of
  3225. // dst, and copy the upper element from a to the upper element of dst.
  3226. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpnge_sd
  3227. FORCE_INLINE __m128d _mm_cmpnge_sd(__m128d a, __m128d b)
  3228. {
  3229. return _mm_move_sd(a, _mm_cmpnge_pd(a, b));
  3230. }
  3231. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3232. // for not-greater-than, and store the results in dst.
  3233. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_cmpngt_pd
  3234. FORCE_INLINE __m128d _mm_cmpngt_pd(__m128d a, __m128d b)
  3235. {
  3236. #if defined(__aarch64__)
  3237. return vreinterpretq_m128d_u64(veorq_u64(
  3238. vcgtq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)),
  3239. vdupq_n_u64(UINT64_MAX)));
  3240. #else
  3241. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3242. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3243. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3244. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3245. uint64_t d[2];
  3246. d[0] =
  3247. !((*(double *) &a0) > (*(double *) &b0)) ? ~UINT64_C(0) : UINT64_C(0);
  3248. d[1] =
  3249. !((*(double *) &a1) > (*(double *) &b1)) ? ~UINT64_C(0) : UINT64_C(0);
  3250. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3251. #endif
  3252. }
  3253. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3254. // b for not-greater-than, store the result in the lower element of dst, and
  3255. // copy the upper element from a to the upper element of dst.
  3256. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpngt_sd
  3257. FORCE_INLINE __m128d _mm_cmpngt_sd(__m128d a, __m128d b)
  3258. {
  3259. return _mm_move_sd(a, _mm_cmpngt_pd(a, b));
  3260. }
  3261. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3262. // for not-less-than-or-equal, and store the results in dst.
  3263. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpnle_pd
  3264. FORCE_INLINE __m128d _mm_cmpnle_pd(__m128d a, __m128d b)
  3265. {
  3266. #if defined(__aarch64__)
  3267. return vreinterpretq_m128d_u64(veorq_u64(
  3268. vcleq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)),
  3269. vdupq_n_u64(UINT64_MAX)));
  3270. #else
  3271. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3272. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3273. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3274. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3275. uint64_t d[2];
  3276. d[0] =
  3277. !((*(double *) &a0) <= (*(double *) &b0)) ? ~UINT64_C(0) : UINT64_C(0);
  3278. d[1] =
  3279. !((*(double *) &a1) <= (*(double *) &b1)) ? ~UINT64_C(0) : UINT64_C(0);
  3280. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3281. #endif
  3282. }
  3283. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3284. // b for not-less-than-or-equal, store the result in the lower element of dst,
  3285. // and copy the upper element from a to the upper element of dst.
  3286. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpnle_sd
  3287. FORCE_INLINE __m128d _mm_cmpnle_sd(__m128d a, __m128d b)
  3288. {
  3289. return _mm_move_sd(a, _mm_cmpnle_pd(a, b));
  3290. }
  3291. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3292. // for not-less-than, and store the results in dst.
  3293. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpnlt_pd
  3294. FORCE_INLINE __m128d _mm_cmpnlt_pd(__m128d a, __m128d b)
  3295. {
  3296. #if defined(__aarch64__)
  3297. return vreinterpretq_m128d_u64(veorq_u64(
  3298. vcltq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)),
  3299. vdupq_n_u64(UINT64_MAX)));
  3300. #else
  3301. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3302. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3303. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3304. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3305. uint64_t d[2];
  3306. d[0] =
  3307. !((*(double *) &a0) < (*(double *) &b0)) ? ~UINT64_C(0) : UINT64_C(0);
  3308. d[1] =
  3309. !((*(double *) &a1) < (*(double *) &b1)) ? ~UINT64_C(0) : UINT64_C(0);
  3310. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3311. #endif
  3312. }
  3313. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3314. // b for not-less-than, store the result in the lower element of dst, and copy
  3315. // the upper element from a to the upper element of dst.
  3316. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpnlt_sd
  3317. FORCE_INLINE __m128d _mm_cmpnlt_sd(__m128d a, __m128d b)
  3318. {
  3319. return _mm_move_sd(a, _mm_cmpnlt_pd(a, b));
  3320. }
  3321. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3322. // to see if neither is NaN, and store the results in dst.
  3323. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpord_pd
  3324. FORCE_INLINE __m128d _mm_cmpord_pd(__m128d a, __m128d b)
  3325. {
  3326. #if defined(__aarch64__)
  3327. // Excluding NaNs, any two floating point numbers can be compared.
  3328. uint64x2_t not_nan_a =
  3329. vceqq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(a));
  3330. uint64x2_t not_nan_b =
  3331. vceqq_f64(vreinterpretq_f64_m128d(b), vreinterpretq_f64_m128d(b));
  3332. return vreinterpretq_m128d_u64(vandq_u64(not_nan_a, not_nan_b));
  3333. #else
  3334. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3335. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3336. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3337. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3338. uint64_t d[2];
  3339. d[0] = ((*(double *) &a0) == (*(double *) &a0) &&
  3340. (*(double *) &b0) == (*(double *) &b0))
  3341. ? ~UINT64_C(0)
  3342. : UINT64_C(0);
  3343. d[1] = ((*(double *) &a1) == (*(double *) &a1) &&
  3344. (*(double *) &b1) == (*(double *) &b1))
  3345. ? ~UINT64_C(0)
  3346. : UINT64_C(0);
  3347. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3348. #endif
  3349. }
  3350. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3351. // b to see if neither is NaN, store the result in the lower element of dst, and
  3352. // copy the upper element from a to the upper element of dst.
  3353. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpord_sd
  3354. FORCE_INLINE __m128d _mm_cmpord_sd(__m128d a, __m128d b)
  3355. {
  3356. #if defined(__aarch64__)
  3357. return _mm_move_sd(a, _mm_cmpord_pd(a, b));
  3358. #else
  3359. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3360. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3361. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3362. uint64_t d[2];
  3363. d[0] = ((*(double *) &a0) == (*(double *) &a0) &&
  3364. (*(double *) &b0) == (*(double *) &b0))
  3365. ? ~UINT64_C(0)
  3366. : UINT64_C(0);
  3367. d[1] = a1;
  3368. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3369. #endif
  3370. }
  3371. // Compare packed double-precision (64-bit) floating-point elements in a and b
  3372. // to see if either is NaN, and store the results in dst.
  3373. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpunord_pd
  3374. FORCE_INLINE __m128d _mm_cmpunord_pd(__m128d a, __m128d b)
  3375. {
  3376. #if defined(__aarch64__)
  3377. // Two NaNs are not equal in comparison operation.
  3378. uint64x2_t not_nan_a =
  3379. vceqq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(a));
  3380. uint64x2_t not_nan_b =
  3381. vceqq_f64(vreinterpretq_f64_m128d(b), vreinterpretq_f64_m128d(b));
  3382. return vreinterpretq_m128d_s32(
  3383. vmvnq_s32(vreinterpretq_s32_u64(vandq_u64(not_nan_a, not_nan_b))));
  3384. #else
  3385. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3386. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3387. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3388. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  3389. uint64_t d[2];
  3390. d[0] = ((*(double *) &a0) == (*(double *) &a0) &&
  3391. (*(double *) &b0) == (*(double *) &b0))
  3392. ? UINT64_C(0)
  3393. : ~UINT64_C(0);
  3394. d[1] = ((*(double *) &a1) == (*(double *) &a1) &&
  3395. (*(double *) &b1) == (*(double *) &b1))
  3396. ? UINT64_C(0)
  3397. : ~UINT64_C(0);
  3398. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3399. #endif
  3400. }
  3401. // Compare the lower double-precision (64-bit) floating-point elements in a and
  3402. // b to see if either is NaN, store the result in the lower element of dst, and
  3403. // copy the upper element from a to the upper element of dst.
  3404. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cmpunord_sd
  3405. FORCE_INLINE __m128d _mm_cmpunord_sd(__m128d a, __m128d b)
  3406. {
  3407. #if defined(__aarch64__)
  3408. return _mm_move_sd(a, _mm_cmpunord_pd(a, b));
  3409. #else
  3410. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3411. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3412. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  3413. uint64_t d[2];
  3414. d[0] = ((*(double *) &a0) == (*(double *) &a0) &&
  3415. (*(double *) &b0) == (*(double *) &b0))
  3416. ? UINT64_C(0)
  3417. : ~UINT64_C(0);
  3418. d[1] = a1;
  3419. return vreinterpretq_m128d_u64(vld1q_u64(d));
  3420. #endif
  3421. }
  3422. // Compare the lower double-precision (64-bit) floating-point element in a and b
  3423. // for greater-than-or-equal, and return the boolean result (0 or 1).
  3424. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_comige_sd
  3425. FORCE_INLINE int _mm_comige_sd(__m128d a, __m128d b)
  3426. {
  3427. #if defined(__aarch64__)
  3428. return vgetq_lane_u64(vcgeq_f64(a, b), 0) & 0x1;
  3429. #else
  3430. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3431. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3432. return (*(double *) &a0 >= *(double *) &b0);
  3433. #endif
  3434. }
  3435. // Compare the lower double-precision (64-bit) floating-point element in a and b
  3436. // for greater-than, and return the boolean result (0 or 1).
  3437. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_comigt_sd
  3438. FORCE_INLINE int _mm_comigt_sd(__m128d a, __m128d b)
  3439. {
  3440. #if defined(__aarch64__)
  3441. return vgetq_lane_u64(vcgtq_f64(a, b), 0) & 0x1;
  3442. #else
  3443. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3444. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3445. return (*(double *) &a0 > *(double *) &b0);
  3446. #endif
  3447. }
  3448. // Compare the lower double-precision (64-bit) floating-point element in a and b
  3449. // for less-than-or-equal, and return the boolean result (0 or 1).
  3450. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_comile_sd
  3451. FORCE_INLINE int _mm_comile_sd(__m128d a, __m128d b)
  3452. {
  3453. #if defined(__aarch64__)
  3454. return vgetq_lane_u64(vcleq_f64(a, b), 0) & 0x1;
  3455. #else
  3456. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3457. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3458. return (*(double *) &a0 <= *(double *) &b0);
  3459. #endif
  3460. }
  3461. // Compare the lower double-precision (64-bit) floating-point element in a and b
  3462. // for less-than, and return the boolean result (0 or 1).
  3463. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_comilt_sd
  3464. FORCE_INLINE int _mm_comilt_sd(__m128d a, __m128d b)
  3465. {
  3466. #if defined(__aarch64__)
  3467. return vgetq_lane_u64(vcltq_f64(a, b), 0) & 0x1;
  3468. #else
  3469. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  3470. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  3471. return (*(double *) &a0 < *(double *) &b0);
  3472. #endif
  3473. }
  3474. // Compare the lower double-precision (64-bit) floating-point element in a and b
  3475. // for equality, and return the boolean result (0 or 1).
  3476. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_comieq_sd
  3477. FORCE_INLINE int _mm_comieq_sd(__m128d a, __m128d b)
  3478. {
  3479. #if defined(__aarch64__)
  3480. return vgetq_lane_u64(vceqq_f64(a, b), 0) & 0x1;
  3481. #else
  3482. uint32x4_t a_not_nan =
  3483. vceqq_u32(vreinterpretq_u32_m128d(a), vreinterpretq_u32_m128d(a));
  3484. uint32x4_t b_not_nan =
  3485. vceqq_u32(vreinterpretq_u32_m128d(b), vreinterpretq_u32_m128d(b));
  3486. uint32x4_t a_and_b_not_nan = vandq_u32(a_not_nan, b_not_nan);
  3487. uint32x4_t a_eq_b =
  3488. vceqq_u32(vreinterpretq_u32_m128d(a), vreinterpretq_u32_m128d(b));
  3489. uint64x2_t and_results = vandq_u64(vreinterpretq_u64_u32(a_and_b_not_nan),
  3490. vreinterpretq_u64_u32(a_eq_b));
  3491. return vgetq_lane_u64(and_results, 0) & 0x1;
  3492. #endif
  3493. }
  3494. // Compare the lower double-precision (64-bit) floating-point element in a and b
  3495. // for not-equal, and return the boolean result (0 or 1).
  3496. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_comineq_sd
  3497. FORCE_INLINE int _mm_comineq_sd(__m128d a, __m128d b)
  3498. {
  3499. return !_mm_comieq_sd(a, b);
  3500. }
  3501. // Convert packed signed 32-bit integers in a to packed double-precision
  3502. // (64-bit) floating-point elements, and store the results in dst.
  3503. //
  3504. // FOR j := 0 to 1
  3505. // i := j*32
  3506. // m := j*64
  3507. // dst[m+63:m] := Convert_Int32_To_FP64(a[i+31:i])
  3508. // ENDFOR
  3509. //
  3510. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtepi32_pd
  3511. FORCE_INLINE __m128d _mm_cvtepi32_pd(__m128i a)
  3512. {
  3513. #if defined(__aarch64__)
  3514. return vreinterpretq_m128d_f64(
  3515. vcvtq_f64_s64(vmovl_s32(vget_low_s32(vreinterpretq_s32_m128i(a)))));
  3516. #else
  3517. double a0 = (double) vgetq_lane_s32(vreinterpretq_s32_m128i(a), 0);
  3518. double a1 = (double) vgetq_lane_s32(vreinterpretq_s32_m128i(a), 1);
  3519. return _mm_set_pd(a1, a0);
  3520. #endif
  3521. }
  3522. // Converts the four signed 32-bit integer values of a to single-precision,
  3523. // floating-point values
  3524. // https://msdn.microsoft.com/en-us/library/vstudio/36bwxcx5(v=vs.100).aspx
  3525. FORCE_INLINE __m128 _mm_cvtepi32_ps(__m128i a)
  3526. {
  3527. return vreinterpretq_m128_f32(vcvtq_f32_s32(vreinterpretq_s32_m128i(a)));
  3528. }
  3529. // Convert packed double-precision (64-bit) floating-point elements in a to
  3530. // packed 32-bit integers, and store the results in dst.
  3531. //
  3532. // FOR j := 0 to 1
  3533. // i := 32*j
  3534. // k := 64*j
  3535. // dst[i+31:i] := Convert_FP64_To_Int32(a[k+63:k])
  3536. // ENDFOR
  3537. //
  3538. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpd_epi32
  3539. FORCE_INLINE __m128i _mm_cvtpd_epi32(__m128d a)
  3540. {
  3541. __m128d rnd = _mm_round_pd(a, _MM_FROUND_CUR_DIRECTION);
  3542. double d0 = ((double *) &rnd)[0];
  3543. double d1 = ((double *) &rnd)[1];
  3544. return _mm_set_epi32(0, 0, (int32_t) d1, (int32_t) d0);
  3545. }
  3546. // Convert packed double-precision (64-bit) floating-point elements in a to
  3547. // packed 32-bit integers, and store the results in dst.
  3548. //
  3549. // FOR j := 0 to 1
  3550. // i := 32*j
  3551. // k := 64*j
  3552. // dst[i+31:i] := Convert_FP64_To_Int32(a[k+63:k])
  3553. // ENDFOR
  3554. //
  3555. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpd_pi32
  3556. FORCE_INLINE __m64 _mm_cvtpd_pi32(__m128d a)
  3557. {
  3558. __m128d rnd = _mm_round_pd(a, _MM_FROUND_CUR_DIRECTION);
  3559. double d0 = ((double *) &rnd)[0];
  3560. double d1 = ((double *) &rnd)[1];
  3561. int32_t ALIGN_STRUCT(16) data[2] = {(int32_t) d0, (int32_t) d1};
  3562. return vreinterpret_m64_s32(vld1_s32(data));
  3563. }
  3564. // Convert packed double-precision (64-bit) floating-point elements in a to
  3565. // packed single-precision (32-bit) floating-point elements, and store the
  3566. // results in dst.
  3567. //
  3568. // FOR j := 0 to 1
  3569. // i := 32*j
  3570. // k := 64*j
  3571. // dst[i+31:i] := Convert_FP64_To_FP32(a[k+64:k])
  3572. // ENDFOR
  3573. // dst[127:64] := 0
  3574. //
  3575. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpd_ps
  3576. FORCE_INLINE __m128 _mm_cvtpd_ps(__m128d a)
  3577. {
  3578. #if defined(__aarch64__)
  3579. float32x2_t tmp = vcvt_f32_f64(vreinterpretq_f64_m128d(a));
  3580. return vreinterpretq_m128_f32(vcombine_f32(tmp, vdup_n_f32(0)));
  3581. #else
  3582. float a0 = (float) ((double *) &a)[0];
  3583. float a1 = (float) ((double *) &a)[1];
  3584. return _mm_set_ps(0, 0, a1, a0);
  3585. #endif
  3586. }
  3587. // Convert packed signed 32-bit integers in a to packed double-precision
  3588. // (64-bit) floating-point elements, and store the results in dst.
  3589. //
  3590. // FOR j := 0 to 1
  3591. // i := j*32
  3592. // m := j*64
  3593. // dst[m+63:m] := Convert_Int32_To_FP64(a[i+31:i])
  3594. // ENDFOR
  3595. //
  3596. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtpi32_pd
  3597. FORCE_INLINE __m128d _mm_cvtpi32_pd(__m64 a)
  3598. {
  3599. #if defined(__aarch64__)
  3600. return vreinterpretq_m128d_f64(
  3601. vcvtq_f64_s64(vmovl_s32(vreinterpret_s32_m64(a))));
  3602. #else
  3603. double a0 = (double) vget_lane_s32(vreinterpret_s32_m64(a), 0);
  3604. double a1 = (double) vget_lane_s32(vreinterpret_s32_m64(a), 1);
  3605. return _mm_set_pd(a1, a0);
  3606. #endif
  3607. }
  3608. // Converts the four single-precision, floating-point values of a to signed
  3609. // 32-bit integer values.
  3610. //
  3611. // r0 := (int) a0
  3612. // r1 := (int) a1
  3613. // r2 := (int) a2
  3614. // r3 := (int) a3
  3615. //
  3616. // https://msdn.microsoft.com/en-us/library/vstudio/xdc42k5e(v=vs.100).aspx
  3617. // *NOTE*. The default rounding mode on SSE is 'round to even', which ARMv7-A
  3618. // does not support! It is supported on ARMv8-A however.
  3619. FORCE_INLINE __m128i _mm_cvtps_epi32(__m128 a)
  3620. {
  3621. #if defined(__aarch64__)
  3622. switch (_MM_GET_ROUNDING_MODE()) {
  3623. case _MM_ROUND_NEAREST:
  3624. return vreinterpretq_m128i_s32(vcvtnq_s32_f32(a));
  3625. case _MM_ROUND_DOWN:
  3626. return vreinterpretq_m128i_s32(vcvtmq_s32_f32(a));
  3627. case _MM_ROUND_UP:
  3628. return vreinterpretq_m128i_s32(vcvtpq_s32_f32(a));
  3629. default: // _MM_ROUND_TOWARD_ZERO
  3630. return vreinterpretq_m128i_s32(vcvtq_s32_f32(a));
  3631. }
  3632. #else
  3633. float *f = (float *) &a;
  3634. switch (_MM_GET_ROUNDING_MODE()) {
  3635. case _MM_ROUND_NEAREST: {
  3636. uint32x4_t signmask = vdupq_n_u32(0x80000000);
  3637. float32x4_t half = vbslq_f32(signmask, vreinterpretq_f32_m128(a),
  3638. vdupq_n_f32(0.5f)); /* +/- 0.5 */
  3639. int32x4_t r_normal = vcvtq_s32_f32(vaddq_f32(
  3640. vreinterpretq_f32_m128(a), half)); /* round to integer: [a + 0.5]*/
  3641. int32x4_t r_trunc = vcvtq_s32_f32(
  3642. vreinterpretq_f32_m128(a)); /* truncate to integer: [a] */
  3643. int32x4_t plusone = vreinterpretq_s32_u32(vshrq_n_u32(
  3644. vreinterpretq_u32_s32(vnegq_s32(r_trunc)), 31)); /* 1 or 0 */
  3645. int32x4_t r_even = vbicq_s32(vaddq_s32(r_trunc, plusone),
  3646. vdupq_n_s32(1)); /* ([a] + {0,1}) & ~1 */
  3647. float32x4_t delta = vsubq_f32(
  3648. vreinterpretq_f32_m128(a),
  3649. vcvtq_f32_s32(r_trunc)); /* compute delta: delta = (a - [a]) */
  3650. uint32x4_t is_delta_half =
  3651. vceqq_f32(delta, half); /* delta == +/- 0.5 */
  3652. return vreinterpretq_m128i_s32(
  3653. vbslq_s32(is_delta_half, r_even, r_normal));
  3654. }
  3655. case _MM_ROUND_DOWN:
  3656. return _mm_set_epi32(floorf(f[3]), floorf(f[2]), floorf(f[1]),
  3657. floorf(f[0]));
  3658. case _MM_ROUND_UP:
  3659. return _mm_set_epi32(ceilf(f[3]), ceilf(f[2]), ceilf(f[1]),
  3660. ceilf(f[0]));
  3661. default: // _MM_ROUND_TOWARD_ZERO
  3662. return _mm_set_epi32((int32_t) f[3], (int32_t) f[2], (int32_t) f[1],
  3663. (int32_t) f[0]);
  3664. }
  3665. #endif
  3666. }
  3667. // Convert packed single-precision (32-bit) floating-point elements in a to
  3668. // packed double-precision (64-bit) floating-point elements, and store the
  3669. // results in dst.
  3670. //
  3671. // FOR j := 0 to 1
  3672. // i := 64*j
  3673. // k := 32*j
  3674. // dst[i+63:i] := Convert_FP32_To_FP64(a[k+31:k])
  3675. // ENDFOR
  3676. //
  3677. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtps_pd
  3678. FORCE_INLINE __m128d _mm_cvtps_pd(__m128 a)
  3679. {
  3680. #if defined(__aarch64__)
  3681. return vreinterpretq_m128d_f64(
  3682. vcvt_f64_f32(vget_low_f32(vreinterpretq_f32_m128(a))));
  3683. #else
  3684. double a0 = (double) vgetq_lane_f32(vreinterpretq_f32_m128(a), 0);
  3685. double a1 = (double) vgetq_lane_f32(vreinterpretq_f32_m128(a), 1);
  3686. return _mm_set_pd(a1, a0);
  3687. #endif
  3688. }
  3689. // Copy the lower double-precision (64-bit) floating-point element of a to dst.
  3690. //
  3691. // dst[63:0] := a[63:0]
  3692. //
  3693. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsd_f64
  3694. FORCE_INLINE double _mm_cvtsd_f64(__m128d a)
  3695. {
  3696. #if defined(__aarch64__)
  3697. return (double) vgetq_lane_f64(vreinterpretq_f64_m128d(a), 0);
  3698. #else
  3699. return ((double *) &a)[0];
  3700. #endif
  3701. }
  3702. // Convert the lower double-precision (64-bit) floating-point element in a to a
  3703. // 32-bit integer, and store the result in dst.
  3704. //
  3705. // dst[31:0] := Convert_FP64_To_Int32(a[63:0])
  3706. //
  3707. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsd_si32
  3708. FORCE_INLINE int32_t _mm_cvtsd_si32(__m128d a)
  3709. {
  3710. #if defined(__aarch64__)
  3711. return (int32_t) vgetq_lane_f64(vrndiq_f64(vreinterpretq_f64_m128d(a)), 0);
  3712. #else
  3713. __m128d rnd = _mm_round_pd(a, _MM_FROUND_CUR_DIRECTION);
  3714. double ret = ((double *) &rnd)[0];
  3715. return (int32_t) ret;
  3716. #endif
  3717. }
  3718. // Convert the lower double-precision (64-bit) floating-point element in a to a
  3719. // 64-bit integer, and store the result in dst.
  3720. //
  3721. // dst[63:0] := Convert_FP64_To_Int64(a[63:0])
  3722. //
  3723. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsd_si64
  3724. FORCE_INLINE int64_t _mm_cvtsd_si64(__m128d a)
  3725. {
  3726. #if defined(__aarch64__)
  3727. return (int64_t) vgetq_lane_f64(vrndiq_f64(vreinterpretq_f64_m128d(a)), 0);
  3728. #else
  3729. __m128d rnd = _mm_round_pd(a, _MM_FROUND_CUR_DIRECTION);
  3730. double ret = ((double *) &rnd)[0];
  3731. return (int64_t) ret;
  3732. #endif
  3733. }
  3734. // Convert the lower double-precision (64-bit) floating-point element in a to a
  3735. // 64-bit integer, and store the result in dst.
  3736. //
  3737. // dst[63:0] := Convert_FP64_To_Int64(a[63:0])
  3738. //
  3739. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsd_si64x
  3740. #define _mm_cvtsd_si64x _mm_cvtsd_si64
  3741. // Convert the lower double-precision (64-bit) floating-point element in b to a
  3742. // single-precision (32-bit) floating-point element, store the result in the
  3743. // lower element of dst, and copy the upper 3 packed elements from a to the
  3744. // upper elements of dst.
  3745. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsd_ss
  3746. FORCE_INLINE __m128 _mm_cvtsd_ss(__m128 a, __m128d b)
  3747. {
  3748. #if defined(__aarch64__)
  3749. return vreinterpretq_m128_f32(vsetq_lane_f32(
  3750. vget_lane_f32(vcvt_f32_f64(vreinterpretq_f64_m128d(b)), 0),
  3751. vreinterpretq_f32_m128(a), 0));
  3752. #else
  3753. return vreinterpretq_m128_f32(vsetq_lane_f32((float) ((double *) &b)[0],
  3754. vreinterpretq_f32_m128(a), 0));
  3755. #endif
  3756. }
  3757. // Copy the lower 32-bit integer in a to dst.
  3758. //
  3759. // dst[31:0] := a[31:0]
  3760. //
  3761. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi128_si32
  3762. FORCE_INLINE int _mm_cvtsi128_si32(__m128i a)
  3763. {
  3764. return vgetq_lane_s32(vreinterpretq_s32_m128i(a), 0);
  3765. }
  3766. // Copy the lower 64-bit integer in a to dst.
  3767. //
  3768. // dst[63:0] := a[63:0]
  3769. //
  3770. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi128_si64
  3771. FORCE_INLINE int64_t _mm_cvtsi128_si64(__m128i a)
  3772. {
  3773. return vgetq_lane_s64(vreinterpretq_s64_m128i(a), 0);
  3774. }
  3775. // Copy the lower 64-bit integer in a to dst.
  3776. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi128_si64x
  3777. #define _mm_cvtsi128_si64x(a) _mm_cvtsi128_si64(a)
  3778. // Convert the signed 32-bit integer b to a double-precision (64-bit)
  3779. // floating-point element, store the result in the lower element of dst, and
  3780. // copy the upper element from a to the upper element of dst.
  3781. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi32_sd
  3782. FORCE_INLINE __m128d _mm_cvtsi32_sd(__m128d a, int32_t b)
  3783. {
  3784. #if defined(__aarch64__)
  3785. return vreinterpretq_m128d_f64(
  3786. vsetq_lane_f64((double) b, vreinterpretq_f64_m128d(a), 0));
  3787. #else
  3788. double bf = (double) b;
  3789. return vreinterpretq_m128d_s64(
  3790. vsetq_lane_s64(*(int64_t *) &bf, vreinterpretq_s64_m128d(a), 0));
  3791. #endif
  3792. }
  3793. // Copy the lower 64-bit integer in a to dst.
  3794. //
  3795. // dst[63:0] := a[63:0]
  3796. //
  3797. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi128_si64x
  3798. #define _mm_cvtsi128_si64x(a) _mm_cvtsi128_si64(a)
  3799. // Moves 32-bit integer a to the least significant 32 bits of an __m128 object,
  3800. // zero extending the upper bits.
  3801. //
  3802. // r0 := a
  3803. // r1 := 0x0
  3804. // r2 := 0x0
  3805. // r3 := 0x0
  3806. //
  3807. // https://msdn.microsoft.com/en-us/library/ct3539ha%28v=vs.90%29.aspx
  3808. FORCE_INLINE __m128i _mm_cvtsi32_si128(int a)
  3809. {
  3810. return vreinterpretq_m128i_s32(vsetq_lane_s32(a, vdupq_n_s32(0), 0));
  3811. }
  3812. // Convert the signed 64-bit integer b to a double-precision (64-bit)
  3813. // floating-point element, store the result in the lower element of dst, and
  3814. // copy the upper element from a to the upper element of dst.
  3815. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi64_sd
  3816. FORCE_INLINE __m128d _mm_cvtsi64_sd(__m128d a, int64_t b)
  3817. {
  3818. #if defined(__aarch64__)
  3819. return vreinterpretq_m128d_f64(
  3820. vsetq_lane_f64((double) b, vreinterpretq_f64_m128d(a), 0));
  3821. #else
  3822. double bf = (double) b;
  3823. return vreinterpretq_m128d_s64(
  3824. vsetq_lane_s64(*(int64_t *) &bf, vreinterpretq_s64_m128d(a), 0));
  3825. #endif
  3826. }
  3827. // Moves 64-bit integer a to the least significant 64 bits of an __m128 object,
  3828. // zero extending the upper bits.
  3829. //
  3830. // r0 := a
  3831. // r1 := 0x0
  3832. FORCE_INLINE __m128i _mm_cvtsi64_si128(int64_t a)
  3833. {
  3834. return vreinterpretq_m128i_s64(vsetq_lane_s64(a, vdupq_n_s64(0), 0));
  3835. }
  3836. // Copy 64-bit integer a to the lower element of dst, and zero the upper
  3837. // element.
  3838. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi64x_si128
  3839. #define _mm_cvtsi64x_si128(a) _mm_cvtsi64_si128(a)
  3840. // Convert the signed 64-bit integer b to a double-precision (64-bit)
  3841. // floating-point element, store the result in the lower element of dst, and
  3842. // copy the upper element from a to the upper element of dst.
  3843. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtsi64x_sd
  3844. #define _mm_cvtsi64x_sd(a, b) _mm_cvtsi64_sd(a, b)
  3845. // Convert the lower single-precision (32-bit) floating-point element in b to a
  3846. // double-precision (64-bit) floating-point element, store the result in the
  3847. // lower element of dst, and copy the upper element from a to the upper element
  3848. // of dst.
  3849. //
  3850. // dst[63:0] := Convert_FP32_To_FP64(b[31:0])
  3851. // dst[127:64] := a[127:64]
  3852. //
  3853. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtss_sd
  3854. FORCE_INLINE __m128d _mm_cvtss_sd(__m128d a, __m128 b)
  3855. {
  3856. double d = (double) vgetq_lane_f32(vreinterpretq_f32_m128(b), 0);
  3857. #if defined(__aarch64__)
  3858. return vreinterpretq_m128d_f64(
  3859. vsetq_lane_f64(d, vreinterpretq_f64_m128d(a), 0));
  3860. #else
  3861. return vreinterpretq_m128d_s64(
  3862. vsetq_lane_s64(*(int64_t *) &d, vreinterpretq_s64_m128d(a), 0));
  3863. #endif
  3864. }
  3865. // Convert packed double-precision (64-bit) floating-point elements in a to
  3866. // packed 32-bit integers with truncation, and store the results in dst.
  3867. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvttpd_epi32
  3868. FORCE_INLINE __m128i _mm_cvttpd_epi32(__m128d a)
  3869. {
  3870. double a0 = ((double *) &a)[0];
  3871. double a1 = ((double *) &a)[1];
  3872. return _mm_set_epi32(0, 0, (int32_t) a1, (int32_t) a0);
  3873. }
  3874. // Convert packed double-precision (64-bit) floating-point elements in a to
  3875. // packed 32-bit integers with truncation, and store the results in dst.
  3876. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvttpd_pi32
  3877. FORCE_INLINE __m64 _mm_cvttpd_pi32(__m128d a)
  3878. {
  3879. double a0 = ((double *) &a)[0];
  3880. double a1 = ((double *) &a)[1];
  3881. int32_t ALIGN_STRUCT(16) data[2] = {(int32_t) a0, (int32_t) a1};
  3882. return vreinterpret_m64_s32(vld1_s32(data));
  3883. }
  3884. // Converts the four single-precision, floating-point values of a to signed
  3885. // 32-bit integer values using truncate.
  3886. // https://msdn.microsoft.com/en-us/library/vstudio/1h005y6x(v=vs.100).aspx
  3887. FORCE_INLINE __m128i _mm_cvttps_epi32(__m128 a)
  3888. {
  3889. return vreinterpretq_m128i_s32(vcvtq_s32_f32(vreinterpretq_f32_m128(a)));
  3890. }
  3891. // Convert the lower double-precision (64-bit) floating-point element in a to a
  3892. // 32-bit integer with truncation, and store the result in dst.
  3893. //
  3894. // dst[63:0] := Convert_FP64_To_Int32_Truncate(a[63:0])
  3895. //
  3896. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvttsd_si32
  3897. FORCE_INLINE int32_t _mm_cvttsd_si32(__m128d a)
  3898. {
  3899. double ret = *((double *) &a);
  3900. return (int32_t) ret;
  3901. }
  3902. // Convert the lower double-precision (64-bit) floating-point element in a to a
  3903. // 64-bit integer with truncation, and store the result in dst.
  3904. //
  3905. // dst[63:0] := Convert_FP64_To_Int64_Truncate(a[63:0])
  3906. //
  3907. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvttsd_si64
  3908. FORCE_INLINE int64_t _mm_cvttsd_si64(__m128d a)
  3909. {
  3910. #if defined(__aarch64__)
  3911. return vgetq_lane_s64(vcvtq_s64_f64(vreinterpretq_f64_m128d(a)), 0);
  3912. #else
  3913. double ret = *((double *) &a);
  3914. return (int64_t) ret;
  3915. #endif
  3916. }
  3917. // Convert the lower double-precision (64-bit) floating-point element in a to a
  3918. // 64-bit integer with truncation, and store the result in dst.
  3919. //
  3920. // dst[63:0] := Convert_FP64_To_Int64_Truncate(a[63:0])
  3921. //
  3922. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvttsd_si64x
  3923. #define _mm_cvttsd_si64x(a) _mm_cvttsd_si64(a)
  3924. // Divide packed double-precision (64-bit) floating-point elements in a by
  3925. // packed elements in b, and store the results in dst.
  3926. //
  3927. // FOR j := 0 to 1
  3928. // i := 64*j
  3929. // dst[i+63:i] := a[i+63:i] / b[i+63:i]
  3930. // ENDFOR
  3931. //
  3932. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_div_pd
  3933. FORCE_INLINE __m128d _mm_div_pd(__m128d a, __m128d b)
  3934. {
  3935. #if defined(__aarch64__)
  3936. return vreinterpretq_m128d_f64(
  3937. vdivq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  3938. #else
  3939. double *da = (double *) &a;
  3940. double *db = (double *) &b;
  3941. double c[2];
  3942. c[0] = da[0] / db[0];
  3943. c[1] = da[1] / db[1];
  3944. return vld1q_f32((float32_t *) c);
  3945. #endif
  3946. }
  3947. // Divide the lower double-precision (64-bit) floating-point element in a by the
  3948. // lower double-precision (64-bit) floating-point element in b, store the result
  3949. // in the lower element of dst, and copy the upper element from a to the upper
  3950. // element of dst.
  3951. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_div_sd
  3952. FORCE_INLINE __m128d _mm_div_sd(__m128d a, __m128d b)
  3953. {
  3954. #if defined(__aarch64__)
  3955. float64x2_t tmp =
  3956. vdivq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b));
  3957. return vreinterpretq_m128d_f64(
  3958. vsetq_lane_f64(vgetq_lane_f64(vreinterpretq_f64_m128d(a), 1), tmp, 1));
  3959. #else
  3960. return _mm_move_sd(a, _mm_div_pd(a, b));
  3961. #endif
  3962. }
  3963. // Extracts the selected signed or unsigned 16-bit integer from a and zero
  3964. // extends.
  3965. // https://msdn.microsoft.com/en-us/library/6dceta0c(v=vs.100).aspx
  3966. // FORCE_INLINE int _mm_extract_epi16(__m128i a, __constrange(0,8) int imm)
  3967. #define _mm_extract_epi16(a, imm) \
  3968. vgetq_lane_u16(vreinterpretq_u16_m128i(a), (imm))
  3969. // Inserts the least significant 16 bits of b into the selected 16-bit integer
  3970. // of a.
  3971. // https://msdn.microsoft.com/en-us/library/kaze8hz1%28v=vs.100%29.aspx
  3972. // FORCE_INLINE __m128i _mm_insert_epi16(__m128i a, int b,
  3973. // __constrange(0,8) int imm)
  3974. #define _mm_insert_epi16(a, b, imm) \
  3975. __extension__({ \
  3976. vreinterpretq_m128i_s16( \
  3977. vsetq_lane_s16((b), vreinterpretq_s16_m128i(a), (imm))); \
  3978. })
  3979. // Loads two double-precision from 16-byte aligned memory, floating-point
  3980. // values.
  3981. //
  3982. // dst[127:0] := MEM[mem_addr+127:mem_addr]
  3983. //
  3984. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_load_pd
  3985. FORCE_INLINE __m128d _mm_load_pd(const double *p)
  3986. {
  3987. #if defined(__aarch64__)
  3988. return vreinterpretq_m128d_f64(vld1q_f64(p));
  3989. #else
  3990. const float *fp = (const float *) p;
  3991. float ALIGN_STRUCT(16) data[4] = {fp[0], fp[1], fp[2], fp[3]};
  3992. return vreinterpretq_m128d_f32(vld1q_f32(data));
  3993. #endif
  3994. }
  3995. // Load a double-precision (64-bit) floating-point element from memory into both
  3996. // elements of dst.
  3997. //
  3998. // dst[63:0] := MEM[mem_addr+63:mem_addr]
  3999. // dst[127:64] := MEM[mem_addr+63:mem_addr]
  4000. //
  4001. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_load_pd1
  4002. #define _mm_load_pd1 _mm_load1_pd
  4003. // Load a double-precision (64-bit) floating-point element from memory into the
  4004. // lower of dst, and zero the upper element. mem_addr does not need to be
  4005. // aligned on any particular boundary.
  4006. //
  4007. // dst[63:0] := MEM[mem_addr+63:mem_addr]
  4008. // dst[127:64] := 0
  4009. //
  4010. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_load_sd
  4011. FORCE_INLINE __m128d _mm_load_sd(const double *p)
  4012. {
  4013. #if defined(__aarch64__)
  4014. return vreinterpretq_m128d_f64(vsetq_lane_f64(*p, vdupq_n_f64(0), 0));
  4015. #else
  4016. const float *fp = (const float *) p;
  4017. float ALIGN_STRUCT(16) data[4] = {fp[0], fp[1], 0, 0};
  4018. return vreinterpretq_m128d_f32(vld1q_f32(data));
  4019. #endif
  4020. }
  4021. // Loads 128-bit value. :
  4022. // https://msdn.microsoft.com/en-us/library/atzzad1h(v=vs.80).aspx
  4023. FORCE_INLINE __m128i _mm_load_si128(const __m128i *p)
  4024. {
  4025. return vreinterpretq_m128i_s32(vld1q_s32((const int32_t *) p));
  4026. }
  4027. // Load a double-precision (64-bit) floating-point element from memory into both
  4028. // elements of dst.
  4029. //
  4030. // dst[63:0] := MEM[mem_addr+63:mem_addr]
  4031. // dst[127:64] := MEM[mem_addr+63:mem_addr]
  4032. //
  4033. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_load1_pd
  4034. FORCE_INLINE __m128d _mm_load1_pd(const double *p)
  4035. {
  4036. #if defined(__aarch64__)
  4037. return vreinterpretq_m128d_f64(vld1q_dup_f64(p));
  4038. #else
  4039. return vreinterpretq_m128d_s64(vdupq_n_s64(*(const int64_t *) p));
  4040. #endif
  4041. }
  4042. // Load a double-precision (64-bit) floating-point element from memory into the
  4043. // upper element of dst, and copy the lower element from a to dst. mem_addr does
  4044. // not need to be aligned on any particular boundary.
  4045. //
  4046. // dst[63:0] := a[63:0]
  4047. // dst[127:64] := MEM[mem_addr+63:mem_addr]
  4048. //
  4049. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadh_pd
  4050. FORCE_INLINE __m128d _mm_loadh_pd(__m128d a, const double *p)
  4051. {
  4052. #if defined(__aarch64__)
  4053. return vreinterpretq_m128d_f64(
  4054. vcombine_f64(vget_low_f64(vreinterpretq_f64_m128d(a)), vld1_f64(p)));
  4055. #else
  4056. return vreinterpretq_m128d_f32(vcombine_f32(
  4057. vget_low_f32(vreinterpretq_f32_m128d(a)), vld1_f32((const float *) p)));
  4058. #endif
  4059. }
  4060. // Load 64-bit integer from memory into the first element of dst.
  4061. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadl_epi64
  4062. FORCE_INLINE __m128i _mm_loadl_epi64(__m128i const *p)
  4063. {
  4064. /* Load the lower 64 bits of the value pointed to by p into the
  4065. * lower 64 bits of the result, zeroing the upper 64 bits of the result.
  4066. */
  4067. return vreinterpretq_m128i_s32(
  4068. vcombine_s32(vld1_s32((int32_t const *) p), vcreate_s32(0)));
  4069. }
  4070. // Load a double-precision (64-bit) floating-point element from memory into the
  4071. // lower element of dst, and copy the upper element from a to dst. mem_addr does
  4072. // not need to be aligned on any particular boundary.
  4073. //
  4074. // dst[63:0] := MEM[mem_addr+63:mem_addr]
  4075. // dst[127:64] := a[127:64]
  4076. //
  4077. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadl_pd
  4078. FORCE_INLINE __m128d _mm_loadl_pd(__m128d a, const double *p)
  4079. {
  4080. #if defined(__aarch64__)
  4081. return vreinterpretq_m128d_f64(
  4082. vcombine_f64(vld1_f64(p), vget_high_f64(vreinterpretq_f64_m128d(a))));
  4083. #else
  4084. return vreinterpretq_m128d_f32(
  4085. vcombine_f32(vld1_f32((const float *) p),
  4086. vget_high_f32(vreinterpretq_f32_m128d(a))));
  4087. #endif
  4088. }
  4089. // Load 2 double-precision (64-bit) floating-point elements from memory into dst
  4090. // in reverse order. mem_addr must be aligned on a 16-byte boundary or a
  4091. // general-protection exception may be generated.
  4092. //
  4093. // dst[63:0] := MEM[mem_addr+127:mem_addr+64]
  4094. // dst[127:64] := MEM[mem_addr+63:mem_addr]
  4095. //
  4096. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadr_pd
  4097. FORCE_INLINE __m128d _mm_loadr_pd(const double *p)
  4098. {
  4099. #if defined(__aarch64__)
  4100. float64x2_t v = vld1q_f64(p);
  4101. return vreinterpretq_m128d_f64(vextq_f64(v, v, 1));
  4102. #else
  4103. int64x2_t v = vld1q_s64((const int64_t *) p);
  4104. return vreinterpretq_m128d_s64(vextq_s64(v, v, 1));
  4105. #endif
  4106. }
  4107. // Loads two double-precision from unaligned memory, floating-point values.
  4108. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadu_pd
  4109. FORCE_INLINE __m128d _mm_loadu_pd(const double *p)
  4110. {
  4111. return _mm_load_pd(p);
  4112. }
  4113. // Loads 128-bit value. :
  4114. // https://msdn.microsoft.com/zh-cn/library/f4k12ae8(v=vs.90).aspx
  4115. FORCE_INLINE __m128i _mm_loadu_si128(const __m128i *p)
  4116. {
  4117. return vreinterpretq_m128i_s32(vld1q_s32((const int32_t *) p));
  4118. }
  4119. // Load unaligned 32-bit integer from memory into the first element of dst.
  4120. //
  4121. // dst[31:0] := MEM[mem_addr+31:mem_addr]
  4122. // dst[MAX:32] := 0
  4123. //
  4124. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loadu_si32
  4125. FORCE_INLINE __m128i _mm_loadu_si32(const void *p)
  4126. {
  4127. return vreinterpretq_m128i_s32(
  4128. vsetq_lane_s32(*(const int32_t *) p, vdupq_n_s32(0), 0));
  4129. }
  4130. // Multiplies the 8 signed 16-bit integers from a by the 8 signed 16-bit
  4131. // integers from b.
  4132. //
  4133. // r0 := (a0 * b0) + (a1 * b1)
  4134. // r1 := (a2 * b2) + (a3 * b3)
  4135. // r2 := (a4 * b4) + (a5 * b5)
  4136. // r3 := (a6 * b6) + (a7 * b7)
  4137. // https://msdn.microsoft.com/en-us/library/yht36sa6(v=vs.90).aspx
  4138. FORCE_INLINE __m128i _mm_madd_epi16(__m128i a, __m128i b)
  4139. {
  4140. int32x4_t low = vmull_s16(vget_low_s16(vreinterpretq_s16_m128i(a)),
  4141. vget_low_s16(vreinterpretq_s16_m128i(b)));
  4142. int32x4_t high = vmull_s16(vget_high_s16(vreinterpretq_s16_m128i(a)),
  4143. vget_high_s16(vreinterpretq_s16_m128i(b)));
  4144. int32x2_t low_sum = vpadd_s32(vget_low_s32(low), vget_high_s32(low));
  4145. int32x2_t high_sum = vpadd_s32(vget_low_s32(high), vget_high_s32(high));
  4146. return vreinterpretq_m128i_s32(vcombine_s32(low_sum, high_sum));
  4147. }
  4148. // Conditionally store 8-bit integer elements from a into memory using mask
  4149. // (elements are not stored when the highest bit is not set in the corresponding
  4150. // element) and a non-temporal memory hint. mem_addr does not need to be aligned
  4151. // on any particular boundary.
  4152. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_maskmoveu_si128
  4153. FORCE_INLINE void _mm_maskmoveu_si128(__m128i a, __m128i mask, char *mem_addr)
  4154. {
  4155. int8x16_t shr_mask = vshrq_n_s8(vreinterpretq_s8_m128i(mask), 7);
  4156. __m128 b = _mm_load_ps((const float *) mem_addr);
  4157. int8x16_t masked =
  4158. vbslq_s8(vreinterpretq_u8_s8(shr_mask), vreinterpretq_s8_m128i(a),
  4159. vreinterpretq_s8_m128(b));
  4160. vst1q_s8((int8_t *) mem_addr, masked);
  4161. }
  4162. // Computes the pairwise maxima of the 8 signed 16-bit integers from a and the 8
  4163. // signed 16-bit integers from b.
  4164. // https://msdn.microsoft.com/en-us/LIBRary/3x060h7c(v=vs.100).aspx
  4165. FORCE_INLINE __m128i _mm_max_epi16(__m128i a, __m128i b)
  4166. {
  4167. return vreinterpretq_m128i_s16(
  4168. vmaxq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  4169. }
  4170. // Computes the pairwise maxima of the 16 unsigned 8-bit integers from a and the
  4171. // 16 unsigned 8-bit integers from b.
  4172. // https://msdn.microsoft.com/en-us/library/st6634za(v=vs.100).aspx
  4173. FORCE_INLINE __m128i _mm_max_epu8(__m128i a, __m128i b)
  4174. {
  4175. return vreinterpretq_m128i_u8(
  4176. vmaxq_u8(vreinterpretq_u8_m128i(a), vreinterpretq_u8_m128i(b)));
  4177. }
  4178. // Compare packed double-precision (64-bit) floating-point elements in a and b,
  4179. // and store packed maximum values in dst.
  4180. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_max_pd
  4181. FORCE_INLINE __m128d _mm_max_pd(__m128d a, __m128d b)
  4182. {
  4183. #if defined(__aarch64__)
  4184. return vreinterpretq_m128d_f64(
  4185. vmaxq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  4186. #else
  4187. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  4188. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  4189. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  4190. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  4191. uint64_t d[2];
  4192. d[0] = (*(double *) &a0) > (*(double *) &b0) ? a0 : b0;
  4193. d[1] = (*(double *) &a1) > (*(double *) &b1) ? a1 : b1;
  4194. return vreinterpretq_m128d_u64(vld1q_u64(d));
  4195. #endif
  4196. }
  4197. // Compare the lower double-precision (64-bit) floating-point elements in a and
  4198. // b, store the maximum value in the lower element of dst, and copy the upper
  4199. // element from a to the upper element of dst.
  4200. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_max_sd
  4201. FORCE_INLINE __m128d _mm_max_sd(__m128d a, __m128d b)
  4202. {
  4203. #if defined(__aarch64__)
  4204. return _mm_move_sd(a, _mm_max_pd(a, b));
  4205. #else
  4206. double *da = (double *) &a;
  4207. double *db = (double *) &b;
  4208. double c[2] = {fmax(da[0], db[0]), da[1]};
  4209. return vld1q_f32((float32_t *) c);
  4210. #endif
  4211. }
  4212. // Computes the pairwise minima of the 8 signed 16-bit integers from a and the 8
  4213. // signed 16-bit integers from b.
  4214. // https://msdn.microsoft.com/en-us/library/vstudio/6te997ew(v=vs.100).aspx
  4215. FORCE_INLINE __m128i _mm_min_epi16(__m128i a, __m128i b)
  4216. {
  4217. return vreinterpretq_m128i_s16(
  4218. vminq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  4219. }
  4220. // Computes the pairwise minima of the 16 unsigned 8-bit integers from a and the
  4221. // 16 unsigned 8-bit integers from b.
  4222. // https://msdn.microsoft.com/ko-kr/library/17k8cf58(v=vs.100).aspxx
  4223. FORCE_INLINE __m128i _mm_min_epu8(__m128i a, __m128i b)
  4224. {
  4225. return vreinterpretq_m128i_u8(
  4226. vminq_u8(vreinterpretq_u8_m128i(a), vreinterpretq_u8_m128i(b)));
  4227. }
  4228. // Compare packed double-precision (64-bit) floating-point elements in a and b,
  4229. // and store packed minimum values in dst.
  4230. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_min_pd
  4231. FORCE_INLINE __m128d _mm_min_pd(__m128d a, __m128d b)
  4232. {
  4233. #if defined(__aarch64__)
  4234. return vreinterpretq_m128d_f64(
  4235. vminq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  4236. #else
  4237. uint64_t a0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(a));
  4238. uint64_t a1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(a));
  4239. uint64_t b0 = (uint64_t) vget_low_u64(vreinterpretq_u64_m128d(b));
  4240. uint64_t b1 = (uint64_t) vget_high_u64(vreinterpretq_u64_m128d(b));
  4241. uint64_t d[2];
  4242. d[0] = (*(double *) &a0) < (*(double *) &b0) ? a0 : b0;
  4243. d[1] = (*(double *) &a1) < (*(double *) &b1) ? a1 : b1;
  4244. return vreinterpretq_m128d_u64(vld1q_u64(d));
  4245. #endif
  4246. }
  4247. // Compare the lower double-precision (64-bit) floating-point elements in a and
  4248. // b, store the minimum value in the lower element of dst, and copy the upper
  4249. // element from a to the upper element of dst.
  4250. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_min_sd
  4251. FORCE_INLINE __m128d _mm_min_sd(__m128d a, __m128d b)
  4252. {
  4253. #if defined(__aarch64__)
  4254. return _mm_move_sd(a, _mm_min_pd(a, b));
  4255. #else
  4256. double *da = (double *) &a;
  4257. double *db = (double *) &b;
  4258. double c[2] = {fmin(da[0], db[0]), da[1]};
  4259. return vld1q_f32((float32_t *) c);
  4260. #endif
  4261. }
  4262. // Copy the lower 64-bit integer in a to the lower element of dst, and zero the
  4263. // upper element.
  4264. //
  4265. // dst[63:0] := a[63:0]
  4266. // dst[127:64] := 0
  4267. //
  4268. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_move_epi64
  4269. FORCE_INLINE __m128i _mm_move_epi64(__m128i a)
  4270. {
  4271. return vreinterpretq_m128i_s64(
  4272. vsetq_lane_s64(0, vreinterpretq_s64_m128i(a), 1));
  4273. }
  4274. // Move the lower double-precision (64-bit) floating-point element from b to the
  4275. // lower element of dst, and copy the upper element from a to the upper element
  4276. // of dst.
  4277. //
  4278. // dst[63:0] := b[63:0]
  4279. // dst[127:64] := a[127:64]
  4280. //
  4281. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_move_sd
  4282. FORCE_INLINE __m128d _mm_move_sd(__m128d a, __m128d b)
  4283. {
  4284. return vreinterpretq_m128d_f32(
  4285. vcombine_f32(vget_low_f32(vreinterpretq_f32_m128d(b)),
  4286. vget_high_f32(vreinterpretq_f32_m128d(a))));
  4287. }
  4288. // NEON does not provide a version of this function.
  4289. // Creates a 16-bit mask from the most significant bits of the 16 signed or
  4290. // unsigned 8-bit integers in a and zero extends the upper bits.
  4291. // https://msdn.microsoft.com/en-us/library/vstudio/s090c8fk(v=vs.100).aspx
  4292. FORCE_INLINE int _mm_movemask_epi8(__m128i a)
  4293. {
  4294. // Use increasingly wide shifts+adds to collect the sign bits
  4295. // together.
  4296. // Since the widening shifts would be rather confusing to follow in little
  4297. // endian, everything will be illustrated in big endian order instead. This
  4298. // has a different result - the bits would actually be reversed on a big
  4299. // endian machine.
  4300. // Starting input (only half the elements are shown):
  4301. // 89 ff 1d c0 00 10 99 33
  4302. uint8x16_t input = vreinterpretq_u8_m128i(a);
  4303. // Shift out everything but the sign bits with an unsigned shift right.
  4304. //
  4305. // Bytes of the vector::
  4306. // 89 ff 1d c0 00 10 99 33
  4307. // \ \ \ \ \ \ \ \ high_bits = (uint16x4_t)(input >> 7)
  4308. // | | | | | | | |
  4309. // 01 01 00 01 00 00 01 00
  4310. //
  4311. // Bits of first important lane(s):
  4312. // 10001001 (89)
  4313. // \______
  4314. // |
  4315. // 00000001 (01)
  4316. uint16x8_t high_bits = vreinterpretq_u16_u8(vshrq_n_u8(input, 7));
  4317. // Merge the even lanes together with a 16-bit unsigned shift right + add.
  4318. // 'xx' represents garbage data which will be ignored in the final result.
  4319. // In the important bytes, the add functions like a binary OR.
  4320. //
  4321. // 01 01 00 01 00 00 01 00
  4322. // \_ | \_ | \_ | \_ | paired16 = (uint32x4_t)(input + (input >> 7))
  4323. // \| \| \| \|
  4324. // xx 03 xx 01 xx 00 xx 02
  4325. //
  4326. // 00000001 00000001 (01 01)
  4327. // \_______ |
  4328. // \|
  4329. // xxxxxxxx xxxxxx11 (xx 03)
  4330. uint32x4_t paired16 =
  4331. vreinterpretq_u32_u16(vsraq_n_u16(high_bits, high_bits, 7));
  4332. // Repeat with a wider 32-bit shift + add.
  4333. // xx 03 xx 01 xx 00 xx 02
  4334. // \____ | \____ | paired32 = (uint64x1_t)(paired16 + (paired16 >>
  4335. // 14))
  4336. // \| \|
  4337. // xx xx xx 0d xx xx xx 02
  4338. //
  4339. // 00000011 00000001 (03 01)
  4340. // \\_____ ||
  4341. // '----.\||
  4342. // xxxxxxxx xxxx1101 (xx 0d)
  4343. uint64x2_t paired32 =
  4344. vreinterpretq_u64_u32(vsraq_n_u32(paired16, paired16, 14));
  4345. // Last, an even wider 64-bit shift + add to get our result in the low 8 bit
  4346. // lanes. xx xx xx 0d xx xx xx 02
  4347. // \_________ | paired64 = (uint8x8_t)(paired32 + (paired32 >>
  4348. // 28))
  4349. // \|
  4350. // xx xx xx xx xx xx xx d2
  4351. //
  4352. // 00001101 00000010 (0d 02)
  4353. // \ \___ | |
  4354. // '---. \| |
  4355. // xxxxxxxx 11010010 (xx d2)
  4356. uint8x16_t paired64 =
  4357. vreinterpretq_u8_u64(vsraq_n_u64(paired32, paired32, 28));
  4358. // Extract the low 8 bits from each 64-bit lane with 2 8-bit extracts.
  4359. // xx xx xx xx xx xx xx d2
  4360. // || return paired64[0]
  4361. // d2
  4362. // Note: Little endian would return the correct value 4b (01001011) instead.
  4363. return vgetq_lane_u8(paired64, 0) | ((int) vgetq_lane_u8(paired64, 8) << 8);
  4364. }
  4365. // Set each bit of mask dst based on the most significant bit of the
  4366. // corresponding packed double-precision (64-bit) floating-point element in a.
  4367. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_movemask_pd
  4368. FORCE_INLINE int _mm_movemask_pd(__m128d a)
  4369. {
  4370. uint64x2_t input = vreinterpretq_u64_m128d(a);
  4371. uint64x2_t high_bits = vshrq_n_u64(input, 63);
  4372. return vgetq_lane_u64(high_bits, 0) | (vgetq_lane_u64(high_bits, 1) << 1);
  4373. }
  4374. // Copy the lower 64-bit integer in a to dst.
  4375. //
  4376. // dst[63:0] := a[63:0]
  4377. //
  4378. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_movepi64_pi64
  4379. FORCE_INLINE __m64 _mm_movepi64_pi64(__m128i a)
  4380. {
  4381. return vreinterpret_m64_s64(vget_low_s64(vreinterpretq_s64_m128i(a)));
  4382. }
  4383. // Copy the 64-bit integer a to the lower element of dst, and zero the upper
  4384. // element.
  4385. //
  4386. // dst[63:0] := a[63:0]
  4387. // dst[127:64] := 0
  4388. //
  4389. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_movpi64_epi64
  4390. FORCE_INLINE __m128i _mm_movpi64_epi64(__m64 a)
  4391. {
  4392. return vreinterpretq_m128i_s64(
  4393. vcombine_s64(vreinterpret_s64_m64(a), vdup_n_s64(0)));
  4394. }
  4395. // Multiply the low unsigned 32-bit integers from each packed 64-bit element in
  4396. // a and b, and store the unsigned 64-bit results in dst.
  4397. //
  4398. // r0 := (a0 & 0xFFFFFFFF) * (b0 & 0xFFFFFFFF)
  4399. // r1 := (a2 & 0xFFFFFFFF) * (b2 & 0xFFFFFFFF)
  4400. FORCE_INLINE __m128i _mm_mul_epu32(__m128i a, __m128i b)
  4401. {
  4402. // vmull_u32 upcasts instead of masking, so we downcast.
  4403. uint32x2_t a_lo = vmovn_u64(vreinterpretq_u64_m128i(a));
  4404. uint32x2_t b_lo = vmovn_u64(vreinterpretq_u64_m128i(b));
  4405. return vreinterpretq_m128i_u64(vmull_u32(a_lo, b_lo));
  4406. }
  4407. // Multiply packed double-precision (64-bit) floating-point elements in a and b,
  4408. // and store the results in dst.
  4409. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_mul_pd
  4410. FORCE_INLINE __m128d _mm_mul_pd(__m128d a, __m128d b)
  4411. {
  4412. #if defined(__aarch64__)
  4413. return vreinterpretq_m128d_f64(
  4414. vmulq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  4415. #else
  4416. double *da = (double *) &a;
  4417. double *db = (double *) &b;
  4418. double c[2];
  4419. c[0] = da[0] * db[0];
  4420. c[1] = da[1] * db[1];
  4421. return vld1q_f32((float32_t *) c);
  4422. #endif
  4423. }
  4424. // Multiply the lower double-precision (64-bit) floating-point element in a and
  4425. // b, store the result in the lower element of dst, and copy the upper element
  4426. // from a to the upper element of dst.
  4427. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=mm_mul_sd
  4428. FORCE_INLINE __m128d _mm_mul_sd(__m128d a, __m128d b)
  4429. {
  4430. return _mm_move_sd(a, _mm_mul_pd(a, b));
  4431. }
  4432. // Multiply the low unsigned 32-bit integers from a and b, and store the
  4433. // unsigned 64-bit result in dst.
  4434. //
  4435. // dst[63:0] := a[31:0] * b[31:0]
  4436. //
  4437. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_mul_su32
  4438. FORCE_INLINE __m64 _mm_mul_su32(__m64 a, __m64 b)
  4439. {
  4440. return vreinterpret_m64_u64(vget_low_u64(
  4441. vmull_u32(vreinterpret_u32_m64(a), vreinterpret_u32_m64(b))));
  4442. }
  4443. // Multiplies the 8 signed 16-bit integers from a by the 8 signed 16-bit
  4444. // integers from b.
  4445. //
  4446. // r0 := (a0 * b0)[31:16]
  4447. // r1 := (a1 * b1)[31:16]
  4448. // ...
  4449. // r7 := (a7 * b7)[31:16]
  4450. //
  4451. // https://msdn.microsoft.com/en-us/library/vstudio/59hddw1d(v=vs.100).aspx
  4452. FORCE_INLINE __m128i _mm_mulhi_epi16(__m128i a, __m128i b)
  4453. {
  4454. /* FIXME: issue with large values because of result saturation */
  4455. // int16x8_t ret = vqdmulhq_s16(vreinterpretq_s16_m128i(a),
  4456. // vreinterpretq_s16_m128i(b)); /* =2*a*b */ return
  4457. // vreinterpretq_m128i_s16(vshrq_n_s16(ret, 1));
  4458. int16x4_t a3210 = vget_low_s16(vreinterpretq_s16_m128i(a));
  4459. int16x4_t b3210 = vget_low_s16(vreinterpretq_s16_m128i(b));
  4460. int32x4_t ab3210 = vmull_s16(a3210, b3210); /* 3333222211110000 */
  4461. int16x4_t a7654 = vget_high_s16(vreinterpretq_s16_m128i(a));
  4462. int16x4_t b7654 = vget_high_s16(vreinterpretq_s16_m128i(b));
  4463. int32x4_t ab7654 = vmull_s16(a7654, b7654); /* 7777666655554444 */
  4464. uint16x8x2_t r =
  4465. vuzpq_u16(vreinterpretq_u16_s32(ab3210), vreinterpretq_u16_s32(ab7654));
  4466. return vreinterpretq_m128i_u16(r.val[1]);
  4467. }
  4468. // Multiply the packed unsigned 16-bit integers in a and b, producing
  4469. // intermediate 32-bit integers, and store the high 16 bits of the intermediate
  4470. // integers in dst.
  4471. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_mulhi_epu16
  4472. FORCE_INLINE __m128i _mm_mulhi_epu16(__m128i a, __m128i b)
  4473. {
  4474. uint16x4_t a3210 = vget_low_u16(vreinterpretq_u16_m128i(a));
  4475. uint16x4_t b3210 = vget_low_u16(vreinterpretq_u16_m128i(b));
  4476. uint32x4_t ab3210 = vmull_u16(a3210, b3210);
  4477. #if defined(__aarch64__)
  4478. uint32x4_t ab7654 =
  4479. vmull_high_u16(vreinterpretq_u16_m128i(a), vreinterpretq_u16_m128i(b));
  4480. uint16x8_t r = vuzp2q_u16(vreinterpretq_u16_u32(ab3210),
  4481. vreinterpretq_u16_u32(ab7654));
  4482. return vreinterpretq_m128i_u16(r);
  4483. #else
  4484. uint16x4_t a7654 = vget_high_u16(vreinterpretq_u16_m128i(a));
  4485. uint16x4_t b7654 = vget_high_u16(vreinterpretq_u16_m128i(b));
  4486. uint32x4_t ab7654 = vmull_u16(a7654, b7654);
  4487. uint16x8x2_t r =
  4488. vuzpq_u16(vreinterpretq_u16_u32(ab3210), vreinterpretq_u16_u32(ab7654));
  4489. return vreinterpretq_m128i_u16(r.val[1]);
  4490. #endif
  4491. }
  4492. // Multiplies the 8 signed or unsigned 16-bit integers from a by the 8 signed or
  4493. // unsigned 16-bit integers from b.
  4494. //
  4495. // r0 := (a0 * b0)[15:0]
  4496. // r1 := (a1 * b1)[15:0]
  4497. // ...
  4498. // r7 := (a7 * b7)[15:0]
  4499. //
  4500. // https://msdn.microsoft.com/en-us/library/vstudio/9ks1472s(v=vs.100).aspx
  4501. FORCE_INLINE __m128i _mm_mullo_epi16(__m128i a, __m128i b)
  4502. {
  4503. return vreinterpretq_m128i_s16(
  4504. vmulq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  4505. }
  4506. // Compute the bitwise OR of packed double-precision (64-bit) floating-point
  4507. // elements in a and b, and store the results in dst.
  4508. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=mm_or_pd
  4509. FORCE_INLINE __m128d _mm_or_pd(__m128d a, __m128d b)
  4510. {
  4511. return vreinterpretq_m128d_s64(
  4512. vorrq_s64(vreinterpretq_s64_m128d(a), vreinterpretq_s64_m128d(b)));
  4513. }
  4514. // Computes the bitwise OR of the 128-bit value in a and the 128-bit value in b.
  4515. //
  4516. // r := a | b
  4517. //
  4518. // https://msdn.microsoft.com/en-us/library/vstudio/ew8ty0db(v=vs.100).aspx
  4519. FORCE_INLINE __m128i _mm_or_si128(__m128i a, __m128i b)
  4520. {
  4521. return vreinterpretq_m128i_s32(
  4522. vorrq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  4523. }
  4524. // Packs the 16 signed 16-bit integers from a and b into 8-bit integers and
  4525. // saturates.
  4526. // https://msdn.microsoft.com/en-us/library/k4y4f7w5%28v=vs.90%29.aspx
  4527. FORCE_INLINE __m128i _mm_packs_epi16(__m128i a, __m128i b)
  4528. {
  4529. return vreinterpretq_m128i_s8(
  4530. vcombine_s8(vqmovn_s16(vreinterpretq_s16_m128i(a)),
  4531. vqmovn_s16(vreinterpretq_s16_m128i(b))));
  4532. }
  4533. // Packs the 8 signed 32-bit integers from a and b into signed 16-bit integers
  4534. // and saturates.
  4535. //
  4536. // r0 := SignedSaturate(a0)
  4537. // r1 := SignedSaturate(a1)
  4538. // r2 := SignedSaturate(a2)
  4539. // r3 := SignedSaturate(a3)
  4540. // r4 := SignedSaturate(b0)
  4541. // r5 := SignedSaturate(b1)
  4542. // r6 := SignedSaturate(b2)
  4543. // r7 := SignedSaturate(b3)
  4544. //
  4545. // https://msdn.microsoft.com/en-us/library/393t56f9%28v=vs.90%29.aspx
  4546. FORCE_INLINE __m128i _mm_packs_epi32(__m128i a, __m128i b)
  4547. {
  4548. return vreinterpretq_m128i_s16(
  4549. vcombine_s16(vqmovn_s32(vreinterpretq_s32_m128i(a)),
  4550. vqmovn_s32(vreinterpretq_s32_m128i(b))));
  4551. }
  4552. // Packs the 16 signed 16 - bit integers from a and b into 8 - bit unsigned
  4553. // integers and saturates.
  4554. //
  4555. // r0 := UnsignedSaturate(a0)
  4556. // r1 := UnsignedSaturate(a1)
  4557. // ...
  4558. // r7 := UnsignedSaturate(a7)
  4559. // r8 := UnsignedSaturate(b0)
  4560. // r9 := UnsignedSaturate(b1)
  4561. // ...
  4562. // r15 := UnsignedSaturate(b7)
  4563. //
  4564. // https://msdn.microsoft.com/en-us/library/07ad1wx4(v=vs.100).aspx
  4565. FORCE_INLINE __m128i _mm_packus_epi16(const __m128i a, const __m128i b)
  4566. {
  4567. return vreinterpretq_m128i_u8(
  4568. vcombine_u8(vqmovun_s16(vreinterpretq_s16_m128i(a)),
  4569. vqmovun_s16(vreinterpretq_s16_m128i(b))));
  4570. }
  4571. // Pause the processor. This is typically used in spin-wait loops and depending
  4572. // on the x86 processor typical values are in the 40-100 cycle range. The
  4573. // 'yield' instruction isn't a good fit beacuse it's effectively a nop on most
  4574. // Arm cores. Experience with several databases has shown has shown an 'isb' is
  4575. // a reasonable approximation.
  4576. FORCE_INLINE void _mm_pause()
  4577. {
  4578. __asm__ __volatile__("isb\n");
  4579. }
  4580. // Compute the absolute differences of packed unsigned 8-bit integers in a and
  4581. // b, then horizontally sum each consecutive 8 differences to produce two
  4582. // unsigned 16-bit integers, and pack these unsigned 16-bit integers in the low
  4583. // 16 bits of 64-bit elements in dst.
  4584. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sad_epu8
  4585. FORCE_INLINE __m128i _mm_sad_epu8(__m128i a, __m128i b)
  4586. {
  4587. uint16x8_t t = vpaddlq_u8(vabdq_u8((uint8x16_t) a, (uint8x16_t) b));
  4588. return vreinterpretq_m128i_u64(vpaddlq_u32(vpaddlq_u16(t)));
  4589. }
  4590. // Sets the 8 signed 16-bit integer values.
  4591. // https://msdn.microsoft.com/en-au/library/3e0fek84(v=vs.90).aspx
  4592. FORCE_INLINE __m128i _mm_set_epi16(short i7,
  4593. short i6,
  4594. short i5,
  4595. short i4,
  4596. short i3,
  4597. short i2,
  4598. short i1,
  4599. short i0)
  4600. {
  4601. int16_t ALIGN_STRUCT(16) data[8] = {i0, i1, i2, i3, i4, i5, i6, i7};
  4602. return vreinterpretq_m128i_s16(vld1q_s16(data));
  4603. }
  4604. // Sets the 4 signed 32-bit integer values.
  4605. // https://msdn.microsoft.com/en-us/library/vstudio/019beekt(v=vs.100).aspx
  4606. FORCE_INLINE __m128i _mm_set_epi32(int i3, int i2, int i1, int i0)
  4607. {
  4608. int32_t ALIGN_STRUCT(16) data[4] = {i0, i1, i2, i3};
  4609. return vreinterpretq_m128i_s32(vld1q_s32(data));
  4610. }
  4611. // Returns the __m128i structure with its two 64-bit integer values
  4612. // initialized to the values of the two 64-bit integers passed in.
  4613. // https://msdn.microsoft.com/en-us/library/dk2sdw0h(v=vs.120).aspx
  4614. FORCE_INLINE __m128i _mm_set_epi64(__m64 i1, __m64 i2)
  4615. {
  4616. return _mm_set_epi64x((int64_t) i1, (int64_t) i2);
  4617. }
  4618. // Returns the __m128i structure with its two 64-bit integer values
  4619. // initialized to the values of the two 64-bit integers passed in.
  4620. // https://msdn.microsoft.com/en-us/library/dk2sdw0h(v=vs.120).aspx
  4621. FORCE_INLINE __m128i _mm_set_epi64x(int64_t i1, int64_t i2)
  4622. {
  4623. return vreinterpretq_m128i_s64(
  4624. vcombine_s64(vcreate_s64(i2), vcreate_s64(i1)));
  4625. }
  4626. // Sets the 16 signed 8-bit integer values.
  4627. // https://msdn.microsoft.com/en-us/library/x0cx8zd3(v=vs.90).aspx
  4628. FORCE_INLINE __m128i _mm_set_epi8(signed char b15,
  4629. signed char b14,
  4630. signed char b13,
  4631. signed char b12,
  4632. signed char b11,
  4633. signed char b10,
  4634. signed char b9,
  4635. signed char b8,
  4636. signed char b7,
  4637. signed char b6,
  4638. signed char b5,
  4639. signed char b4,
  4640. signed char b3,
  4641. signed char b2,
  4642. signed char b1,
  4643. signed char b0)
  4644. {
  4645. int8_t ALIGN_STRUCT(16)
  4646. data[16] = {(int8_t) b0, (int8_t) b1, (int8_t) b2, (int8_t) b3,
  4647. (int8_t) b4, (int8_t) b5, (int8_t) b6, (int8_t) b7,
  4648. (int8_t) b8, (int8_t) b9, (int8_t) b10, (int8_t) b11,
  4649. (int8_t) b12, (int8_t) b13, (int8_t) b14, (int8_t) b15};
  4650. return (__m128i) vld1q_s8(data);
  4651. }
  4652. // Set packed double-precision (64-bit) floating-point elements in dst with the
  4653. // supplied values.
  4654. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_set_pd
  4655. FORCE_INLINE __m128d _mm_set_pd(double e1, double e0)
  4656. {
  4657. double ALIGN_STRUCT(16) data[2] = {e0, e1};
  4658. #if defined(__aarch64__)
  4659. return vreinterpretq_m128d_f64(vld1q_f64((float64_t *) data));
  4660. #else
  4661. return vreinterpretq_m128d_f32(vld1q_f32((float32_t *) data));
  4662. #endif
  4663. }
  4664. // Broadcast double-precision (64-bit) floating-point value a to all elements of
  4665. // dst.
  4666. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_set_pd1
  4667. #define _mm_set_pd1 _mm_set1_pd
  4668. // Copy double-precision (64-bit) floating-point element a to the lower element
  4669. // of dst, and zero the upper element.
  4670. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_set_sd
  4671. FORCE_INLINE __m128d _mm_set_sd(double a)
  4672. {
  4673. return _mm_set_pd(0, a);
  4674. }
  4675. // Sets the 8 signed 16-bit integer values to w.
  4676. //
  4677. // r0 := w
  4678. // r1 := w
  4679. // ...
  4680. // r7 := w
  4681. //
  4682. // https://msdn.microsoft.com/en-us/library/k0ya3x0e(v=vs.90).aspx
  4683. FORCE_INLINE __m128i _mm_set1_epi16(short w)
  4684. {
  4685. return vreinterpretq_m128i_s16(vdupq_n_s16(w));
  4686. }
  4687. // Sets the 4 signed 32-bit integer values to i.
  4688. //
  4689. // r0 := i
  4690. // r1 := i
  4691. // r2 := i
  4692. // r3 := I
  4693. //
  4694. // https://msdn.microsoft.com/en-us/library/vstudio/h4xscxat(v=vs.100).aspx
  4695. FORCE_INLINE __m128i _mm_set1_epi32(int _i)
  4696. {
  4697. return vreinterpretq_m128i_s32(vdupq_n_s32(_i));
  4698. }
  4699. // Sets the 2 signed 64-bit integer values to i.
  4700. // https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/whtfzhzk(v=vs.100)
  4701. FORCE_INLINE __m128i _mm_set1_epi64(__m64 _i)
  4702. {
  4703. return vreinterpretq_m128i_s64(vdupq_n_s64((int64_t) _i));
  4704. }
  4705. // Sets the 2 signed 64-bit integer values to i.
  4706. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_set1_epi64x
  4707. FORCE_INLINE __m128i _mm_set1_epi64x(int64_t _i)
  4708. {
  4709. return vreinterpretq_m128i_s64(vdupq_n_s64(_i));
  4710. }
  4711. // Sets the 16 signed 8-bit integer values to b.
  4712. //
  4713. // r0 := b
  4714. // r1 := b
  4715. // ...
  4716. // r15 := b
  4717. //
  4718. // https://msdn.microsoft.com/en-us/library/6e14xhyf(v=vs.100).aspx
  4719. FORCE_INLINE __m128i _mm_set1_epi8(signed char w)
  4720. {
  4721. return vreinterpretq_m128i_s8(vdupq_n_s8(w));
  4722. }
  4723. // Broadcast double-precision (64-bit) floating-point value a to all elements of
  4724. // dst.
  4725. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_set1_pd
  4726. FORCE_INLINE __m128d _mm_set1_pd(double d)
  4727. {
  4728. #if defined(__aarch64__)
  4729. return vreinterpretq_m128d_f64(vdupq_n_f64(d));
  4730. #else
  4731. return vreinterpretq_m128d_s64(vdupq_n_s64(*(int64_t *) &d));
  4732. #endif
  4733. }
  4734. // Sets the 8 signed 16-bit integer values in reverse order.
  4735. //
  4736. // Return Value
  4737. // r0 := w0
  4738. // r1 := w1
  4739. // ...
  4740. // r7 := w7
  4741. FORCE_INLINE __m128i _mm_setr_epi16(short w0,
  4742. short w1,
  4743. short w2,
  4744. short w3,
  4745. short w4,
  4746. short w5,
  4747. short w6,
  4748. short w7)
  4749. {
  4750. int16_t ALIGN_STRUCT(16) data[8] = {w0, w1, w2, w3, w4, w5, w6, w7};
  4751. return vreinterpretq_m128i_s16(vld1q_s16((int16_t *) data));
  4752. }
  4753. // Sets the 4 signed 32-bit integer values in reverse order
  4754. // https://technet.microsoft.com/en-us/library/security/27yb3ee5(v=vs.90).aspx
  4755. FORCE_INLINE __m128i _mm_setr_epi32(int i3, int i2, int i1, int i0)
  4756. {
  4757. int32_t ALIGN_STRUCT(16) data[4] = {i3, i2, i1, i0};
  4758. return vreinterpretq_m128i_s32(vld1q_s32(data));
  4759. }
  4760. // Set packed 64-bit integers in dst with the supplied values in reverse order.
  4761. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_setr_epi64
  4762. FORCE_INLINE __m128i _mm_setr_epi64(__m64 e1, __m64 e0)
  4763. {
  4764. return vreinterpretq_m128i_s64(vcombine_s64(e1, e0));
  4765. }
  4766. // Sets the 16 signed 8-bit integer values in reverse order.
  4767. // https://msdn.microsoft.com/en-us/library/2khb9c7k(v=vs.90).aspx
  4768. FORCE_INLINE __m128i _mm_setr_epi8(signed char b0,
  4769. signed char b1,
  4770. signed char b2,
  4771. signed char b3,
  4772. signed char b4,
  4773. signed char b5,
  4774. signed char b6,
  4775. signed char b7,
  4776. signed char b8,
  4777. signed char b9,
  4778. signed char b10,
  4779. signed char b11,
  4780. signed char b12,
  4781. signed char b13,
  4782. signed char b14,
  4783. signed char b15)
  4784. {
  4785. int8_t ALIGN_STRUCT(16)
  4786. data[16] = {(int8_t) b0, (int8_t) b1, (int8_t) b2, (int8_t) b3,
  4787. (int8_t) b4, (int8_t) b5, (int8_t) b6, (int8_t) b7,
  4788. (int8_t) b8, (int8_t) b9, (int8_t) b10, (int8_t) b11,
  4789. (int8_t) b12, (int8_t) b13, (int8_t) b14, (int8_t) b15};
  4790. return (__m128i) vld1q_s8(data);
  4791. }
  4792. // Set packed double-precision (64-bit) floating-point elements in dst with the
  4793. // supplied values in reverse order.
  4794. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_setr_pd
  4795. FORCE_INLINE __m128d _mm_setr_pd(double e1, double e0)
  4796. {
  4797. return _mm_set_pd(e0, e1);
  4798. }
  4799. // Return vector of type __m128d with all elements set to zero.
  4800. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_setzero_pd
  4801. FORCE_INLINE __m128d _mm_setzero_pd(void)
  4802. {
  4803. #if defined(__aarch64__)
  4804. return vreinterpretq_m128d_f64(vdupq_n_f64(0));
  4805. #else
  4806. return vreinterpretq_m128d_f32(vdupq_n_f32(0));
  4807. #endif
  4808. }
  4809. // Sets the 128-bit value to zero
  4810. // https://msdn.microsoft.com/en-us/library/vstudio/ys7dw0kh(v=vs.100).aspx
  4811. FORCE_INLINE __m128i _mm_setzero_si128(void)
  4812. {
  4813. return vreinterpretq_m128i_s32(vdupq_n_s32(0));
  4814. }
  4815. // Shuffles the 4 signed or unsigned 32-bit integers in a as specified by imm.
  4816. // https://msdn.microsoft.com/en-us/library/56f67xbk%28v=vs.90%29.aspx
  4817. // FORCE_INLINE __m128i _mm_shuffle_epi32(__m128i a,
  4818. // __constrange(0,255) int imm)
  4819. #if __has_builtin(__builtin_shufflevector)
  4820. #define _mm_shuffle_epi32(a, imm) \
  4821. __extension__({ \
  4822. int32x4_t _input = vreinterpretq_s32_m128i(a); \
  4823. int32x4_t _shuf = __builtin_shufflevector( \
  4824. _input, _input, (imm) & (0x3), ((imm) >> 2) & 0x3, \
  4825. ((imm) >> 4) & 0x3, ((imm) >> 6) & 0x3); \
  4826. vreinterpretq_m128i_s32(_shuf); \
  4827. })
  4828. #else // generic
  4829. #define _mm_shuffle_epi32(a, imm) \
  4830. __extension__({ \
  4831. __m128i ret; \
  4832. switch (imm) { \
  4833. case _MM_SHUFFLE(1, 0, 3, 2): \
  4834. ret = _mm_shuffle_epi_1032((a)); \
  4835. break; \
  4836. case _MM_SHUFFLE(2, 3, 0, 1): \
  4837. ret = _mm_shuffle_epi_2301((a)); \
  4838. break; \
  4839. case _MM_SHUFFLE(0, 3, 2, 1): \
  4840. ret = _mm_shuffle_epi_0321((a)); \
  4841. break; \
  4842. case _MM_SHUFFLE(2, 1, 0, 3): \
  4843. ret = _mm_shuffle_epi_2103((a)); \
  4844. break; \
  4845. case _MM_SHUFFLE(1, 0, 1, 0): \
  4846. ret = _mm_shuffle_epi_1010((a)); \
  4847. break; \
  4848. case _MM_SHUFFLE(1, 0, 0, 1): \
  4849. ret = _mm_shuffle_epi_1001((a)); \
  4850. break; \
  4851. case _MM_SHUFFLE(0, 1, 0, 1): \
  4852. ret = _mm_shuffle_epi_0101((a)); \
  4853. break; \
  4854. case _MM_SHUFFLE(2, 2, 1, 1): \
  4855. ret = _mm_shuffle_epi_2211((a)); \
  4856. break; \
  4857. case _MM_SHUFFLE(0, 1, 2, 2): \
  4858. ret = _mm_shuffle_epi_0122((a)); \
  4859. break; \
  4860. case _MM_SHUFFLE(3, 3, 3, 2): \
  4861. ret = _mm_shuffle_epi_3332((a)); \
  4862. break; \
  4863. case _MM_SHUFFLE(0, 0, 0, 0): \
  4864. ret = _mm_shuffle_epi32_splat((a), 0); \
  4865. break; \
  4866. case _MM_SHUFFLE(1, 1, 1, 1): \
  4867. ret = _mm_shuffle_epi32_splat((a), 1); \
  4868. break; \
  4869. case _MM_SHUFFLE(2, 2, 2, 2): \
  4870. ret = _mm_shuffle_epi32_splat((a), 2); \
  4871. break; \
  4872. case _MM_SHUFFLE(3, 3, 3, 3): \
  4873. ret = _mm_shuffle_epi32_splat((a), 3); \
  4874. break; \
  4875. default: \
  4876. ret = _mm_shuffle_epi32_default((a), (imm)); \
  4877. break; \
  4878. } \
  4879. ret; \
  4880. })
  4881. #endif
  4882. // Shuffle double-precision (64-bit) floating-point elements using the control
  4883. // in imm8, and store the results in dst.
  4884. //
  4885. // dst[63:0] := (imm8[0] == 0) ? a[63:0] : a[127:64]
  4886. // dst[127:64] := (imm8[1] == 0) ? b[63:0] : b[127:64]
  4887. //
  4888. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_shuffle_pd
  4889. #if __has_builtin(__builtin_shufflevector)
  4890. #define _mm_shuffle_pd(a, b, imm8) \
  4891. vreinterpretq_m128d_s64(__builtin_shufflevector( \
  4892. vreinterpretq_s64_m128d(a), vreinterpretq_s64_m128d(b), imm8 & 0x1, \
  4893. ((imm8 & 0x2) >> 1) + 2))
  4894. #else
  4895. #define _mm_shuffle_pd(a, b, imm8) \
  4896. _mm_castsi128_pd(_mm_set_epi64x( \
  4897. vgetq_lane_s64(vreinterpretq_s64_m128d(b), (imm8 & 0x2) >> 1), \
  4898. vgetq_lane_s64(vreinterpretq_s64_m128d(a), imm8 & 0x1)))
  4899. #endif
  4900. // FORCE_INLINE __m128i _mm_shufflehi_epi16(__m128i a,
  4901. // __constrange(0,255) int imm)
  4902. #if __has_builtin(__builtin_shufflevector)
  4903. #define _mm_shufflehi_epi16(a, imm) \
  4904. __extension__({ \
  4905. int16x8_t _input = vreinterpretq_s16_m128i(a); \
  4906. int16x8_t _shuf = __builtin_shufflevector( \
  4907. _input, _input, 0, 1, 2, 3, ((imm) & (0x3)) + 4, \
  4908. (((imm) >> 2) & 0x3) + 4, (((imm) >> 4) & 0x3) + 4, \
  4909. (((imm) >> 6) & 0x3) + 4); \
  4910. vreinterpretq_m128i_s16(_shuf); \
  4911. })
  4912. #else // generic
  4913. #define _mm_shufflehi_epi16(a, imm) _mm_shufflehi_epi16_function((a), (imm))
  4914. #endif
  4915. // FORCE_INLINE __m128i _mm_shufflelo_epi16(__m128i a,
  4916. // __constrange(0,255) int imm)
  4917. #if __has_builtin(__builtin_shufflevector)
  4918. #define _mm_shufflelo_epi16(a, imm) \
  4919. __extension__({ \
  4920. int16x8_t _input = vreinterpretq_s16_m128i(a); \
  4921. int16x8_t _shuf = __builtin_shufflevector( \
  4922. _input, _input, ((imm) & (0x3)), (((imm) >> 2) & 0x3), \
  4923. (((imm) >> 4) & 0x3), (((imm) >> 6) & 0x3), 4, 5, 6, 7); \
  4924. vreinterpretq_m128i_s16(_shuf); \
  4925. })
  4926. #else // generic
  4927. #define _mm_shufflelo_epi16(a, imm) _mm_shufflelo_epi16_function((a), (imm))
  4928. #endif
  4929. // Shift packed 16-bit integers in a left by count while shifting in zeros, and
  4930. // store the results in dst.
  4931. //
  4932. // FOR j := 0 to 7
  4933. // i := j*16
  4934. // IF count[63:0] > 15
  4935. // dst[i+15:i] := 0
  4936. // ELSE
  4937. // dst[i+15:i] := ZeroExtend16(a[i+15:i] << count[63:0])
  4938. // FI
  4939. // ENDFOR
  4940. //
  4941. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sll_epi16
  4942. FORCE_INLINE __m128i _mm_sll_epi16(__m128i a, __m128i count)
  4943. {
  4944. uint64_t c = vreinterpretq_nth_u64_m128i(count, 0);
  4945. if (_sse2neon_unlikely(c & ~15))
  4946. return _mm_setzero_si128();
  4947. int16x8_t vc = vdupq_n_s16((int16_t) c);
  4948. return vreinterpretq_m128i_s16(vshlq_s16(vreinterpretq_s16_m128i(a), vc));
  4949. }
  4950. // Shift packed 32-bit integers in a left by count while shifting in zeros, and
  4951. // store the results in dst.
  4952. //
  4953. // FOR j := 0 to 3
  4954. // i := j*32
  4955. // IF count[63:0] > 31
  4956. // dst[i+31:i] := 0
  4957. // ELSE
  4958. // dst[i+31:i] := ZeroExtend32(a[i+31:i] << count[63:0])
  4959. // FI
  4960. // ENDFOR
  4961. //
  4962. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sll_epi32
  4963. FORCE_INLINE __m128i _mm_sll_epi32(__m128i a, __m128i count)
  4964. {
  4965. uint64_t c = vreinterpretq_nth_u64_m128i(count, 0);
  4966. if (_sse2neon_unlikely(c & ~31))
  4967. return _mm_setzero_si128();
  4968. int32x4_t vc = vdupq_n_s32((int32_t) c);
  4969. return vreinterpretq_m128i_s32(vshlq_s32(vreinterpretq_s32_m128i(a), vc));
  4970. }
  4971. // Shift packed 64-bit integers in a left by count while shifting in zeros, and
  4972. // store the results in dst.
  4973. //
  4974. // FOR j := 0 to 1
  4975. // i := j*64
  4976. // IF count[63:0] > 63
  4977. // dst[i+63:i] := 0
  4978. // ELSE
  4979. // dst[i+63:i] := ZeroExtend64(a[i+63:i] << count[63:0])
  4980. // FI
  4981. // ENDFOR
  4982. //
  4983. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sll_epi64
  4984. FORCE_INLINE __m128i _mm_sll_epi64(__m128i a, __m128i count)
  4985. {
  4986. uint64_t c = vreinterpretq_nth_u64_m128i(count, 0);
  4987. if (_sse2neon_unlikely(c & ~63))
  4988. return _mm_setzero_si128();
  4989. int64x2_t vc = vdupq_n_s64((int64_t) c);
  4990. return vreinterpretq_m128i_s64(vshlq_s64(vreinterpretq_s64_m128i(a), vc));
  4991. }
  4992. // Shift packed 16-bit integers in a left by imm8 while shifting in zeros, and
  4993. // store the results in dst.
  4994. //
  4995. // FOR j := 0 to 7
  4996. // i := j*16
  4997. // IF imm8[7:0] > 15
  4998. // dst[i+15:i] := 0
  4999. // ELSE
  5000. // dst[i+15:i] := ZeroExtend16(a[i+15:i] << imm8[7:0])
  5001. // FI
  5002. // ENDFOR
  5003. //
  5004. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_slli_epi16
  5005. FORCE_INLINE __m128i _mm_slli_epi16(__m128i a, int imm)
  5006. {
  5007. if (_sse2neon_unlikely(imm & ~15))
  5008. return _mm_setzero_si128();
  5009. return vreinterpretq_m128i_s16(
  5010. vshlq_s16(vreinterpretq_s16_m128i(a), vdupq_n_s16(imm)));
  5011. }
  5012. // Shift packed 32-bit integers in a left by imm8 while shifting in zeros, and
  5013. // store the results in dst.
  5014. //
  5015. // FOR j := 0 to 3
  5016. // i := j*32
  5017. // IF imm8[7:0] > 31
  5018. // dst[i+31:i] := 0
  5019. // ELSE
  5020. // dst[i+31:i] := ZeroExtend32(a[i+31:i] << imm8[7:0])
  5021. // FI
  5022. // ENDFOR
  5023. //
  5024. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_slli_epi32
  5025. FORCE_INLINE __m128i _mm_slli_epi32(__m128i a, int imm)
  5026. {
  5027. if (_sse2neon_unlikely(imm & ~31))
  5028. return _mm_setzero_si128();
  5029. return vreinterpretq_m128i_s32(
  5030. vshlq_s32(vreinterpretq_s32_m128i(a), vdupq_n_s32(imm)));
  5031. }
  5032. // Shift packed 64-bit integers in a left by imm8 while shifting in zeros, and
  5033. // store the results in dst.
  5034. //
  5035. // FOR j := 0 to 1
  5036. // i := j*64
  5037. // IF imm8[7:0] > 63
  5038. // dst[i+63:i] := 0
  5039. // ELSE
  5040. // dst[i+63:i] := ZeroExtend64(a[i+63:i] << imm8[7:0])
  5041. // FI
  5042. // ENDFOR
  5043. //
  5044. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_slli_epi64
  5045. FORCE_INLINE __m128i _mm_slli_epi64(__m128i a, int imm)
  5046. {
  5047. if (_sse2neon_unlikely(imm & ~63))
  5048. return _mm_setzero_si128();
  5049. return vreinterpretq_m128i_s64(
  5050. vshlq_s64(vreinterpretq_s64_m128i(a), vdupq_n_s64(imm)));
  5051. }
  5052. // Shift a left by imm8 bytes while shifting in zeros, and store the results in
  5053. // dst.
  5054. //
  5055. // tmp := imm8[7:0]
  5056. // IF tmp > 15
  5057. // tmp := 16
  5058. // FI
  5059. // dst[127:0] := a[127:0] << (tmp*8)
  5060. //
  5061. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_slli_si128
  5062. FORCE_INLINE __m128i _mm_slli_si128(__m128i a, int imm)
  5063. {
  5064. if (_sse2neon_unlikely(imm & ~15))
  5065. return _mm_setzero_si128();
  5066. uint8x16_t tmp[2] = {vdupq_n_u8(0), vreinterpretq_u8_m128i(a)};
  5067. return vreinterpretq_m128i_u8(
  5068. vld1q_u8(((uint8_t const *) tmp) + (16 - imm)));
  5069. }
  5070. // Compute the square root of packed double-precision (64-bit) floating-point
  5071. // elements in a, and store the results in dst.
  5072. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sqrt_pd
  5073. FORCE_INLINE __m128d _mm_sqrt_pd(__m128d a)
  5074. {
  5075. #if defined(__aarch64__)
  5076. return vreinterpretq_m128d_f64(vsqrtq_f64(vreinterpretq_f64_m128d(a)));
  5077. #else
  5078. double a0 = sqrt(((double *) &a)[0]);
  5079. double a1 = sqrt(((double *) &a)[1]);
  5080. return _mm_set_pd(a1, a0);
  5081. #endif
  5082. }
  5083. // Compute the square root of the lower double-precision (64-bit) floating-point
  5084. // element in b, store the result in the lower element of dst, and copy the
  5085. // upper element from a to the upper element of dst.
  5086. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sqrt_sd
  5087. FORCE_INLINE __m128d _mm_sqrt_sd(__m128d a, __m128d b)
  5088. {
  5089. #if defined(__aarch64__)
  5090. return _mm_move_sd(a, _mm_sqrt_pd(b));
  5091. #else
  5092. return _mm_set_pd(((double *) &a)[1], sqrt(((double *) &b)[0]));
  5093. #endif
  5094. }
  5095. // Shift packed 16-bit integers in a right by count while shifting in sign bits,
  5096. // and store the results in dst.
  5097. //
  5098. // FOR j := 0 to 7
  5099. // i := j*16
  5100. // IF count[63:0] > 15
  5101. // dst[i+15:i] := (a[i+15] ? 0xFFFF : 0x0)
  5102. // ELSE
  5103. // dst[i+15:i] := SignExtend16(a[i+15:i] >> count[63:0])
  5104. // FI
  5105. // ENDFOR
  5106. //
  5107. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sra_epi16
  5108. FORCE_INLINE __m128i _mm_sra_epi16(__m128i a, __m128i count)
  5109. {
  5110. int64_t c = (int64_t) vget_low_s64((int64x2_t) count);
  5111. if (_sse2neon_unlikely(c & ~15))
  5112. return _mm_cmplt_epi16(a, _mm_setzero_si128());
  5113. return vreinterpretq_m128i_s16(vshlq_s16((int16x8_t) a, vdupq_n_s16(-c)));
  5114. }
  5115. // Shift packed 32-bit integers in a right by count while shifting in sign bits,
  5116. // and store the results in dst.
  5117. //
  5118. // FOR j := 0 to 3
  5119. // i := j*32
  5120. // IF count[63:0] > 31
  5121. // dst[i+31:i] := (a[i+31] ? 0xFFFFFFFF : 0x0)
  5122. // ELSE
  5123. // dst[i+31:i] := SignExtend32(a[i+31:i] >> count[63:0])
  5124. // FI
  5125. // ENDFOR
  5126. //
  5127. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sra_epi32
  5128. FORCE_INLINE __m128i _mm_sra_epi32(__m128i a, __m128i count)
  5129. {
  5130. int64_t c = (int64_t) vget_low_s64((int64x2_t) count);
  5131. if (_sse2neon_unlikely(c & ~31))
  5132. return _mm_cmplt_epi32(a, _mm_setzero_si128());
  5133. return vreinterpretq_m128i_s32(vshlq_s32((int32x4_t) a, vdupq_n_s32(-c)));
  5134. }
  5135. // Shift packed 16-bit integers in a right by imm8 while shifting in sign
  5136. // bits, and store the results in dst.
  5137. //
  5138. // FOR j := 0 to 7
  5139. // i := j*16
  5140. // IF imm8[7:0] > 15
  5141. // dst[i+15:i] := (a[i+15] ? 0xFFFF : 0x0)
  5142. // ELSE
  5143. // dst[i+15:i] := SignExtend16(a[i+15:i] >> imm8[7:0])
  5144. // FI
  5145. // ENDFOR
  5146. //
  5147. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srai_epi16
  5148. FORCE_INLINE __m128i _mm_srai_epi16(__m128i a, int imm)
  5149. {
  5150. const int count = (imm & ~15) ? 15 : imm;
  5151. return (__m128i) vshlq_s16((int16x8_t) a, vdupq_n_s16(-count));
  5152. }
  5153. // Shift packed 32-bit integers in a right by imm8 while shifting in sign bits,
  5154. // and store the results in dst.
  5155. //
  5156. // FOR j := 0 to 3
  5157. // i := j*32
  5158. // IF imm8[7:0] > 31
  5159. // dst[i+31:i] := (a[i+31] ? 0xFFFFFFFF : 0x0)
  5160. // ELSE
  5161. // dst[i+31:i] := SignExtend32(a[i+31:i] >> imm8[7:0])
  5162. // FI
  5163. // ENDFOR
  5164. //
  5165. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srai_epi32
  5166. // FORCE_INLINE __m128i _mm_srai_epi32(__m128i a, __constrange(0,255) int imm)
  5167. #define _mm_srai_epi32(a, imm) \
  5168. __extension__({ \
  5169. __m128i ret; \
  5170. if (_sse2neon_unlikely((imm) == 0)) { \
  5171. ret = a; \
  5172. } else if (_sse2neon_likely(0 < (imm) && (imm) < 32)) { \
  5173. ret = vreinterpretq_m128i_s32( \
  5174. vshlq_s32(vreinterpretq_s32_m128i(a), vdupq_n_s32(-imm))); \
  5175. } else { \
  5176. ret = vreinterpretq_m128i_s32( \
  5177. vshrq_n_s32(vreinterpretq_s32_m128i(a), 31)); \
  5178. } \
  5179. ret; \
  5180. })
  5181. // Shift packed 16-bit integers in a right by count while shifting in zeros, and
  5182. // store the results in dst.
  5183. //
  5184. // FOR j := 0 to 7
  5185. // i := j*16
  5186. // IF count[63:0] > 15
  5187. // dst[i+15:i] := 0
  5188. // ELSE
  5189. // dst[i+15:i] := ZeroExtend16(a[i+15:i] >> count[63:0])
  5190. // FI
  5191. // ENDFOR
  5192. //
  5193. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srl_epi16
  5194. FORCE_INLINE __m128i _mm_srl_epi16(__m128i a, __m128i count)
  5195. {
  5196. uint64_t c = vreinterpretq_nth_u64_m128i(count, 0);
  5197. if (_sse2neon_unlikely(c & ~15))
  5198. return _mm_setzero_si128();
  5199. int16x8_t vc = vdupq_n_s16(-(int16_t) c);
  5200. return vreinterpretq_m128i_u16(vshlq_u16(vreinterpretq_u16_m128i(a), vc));
  5201. }
  5202. // Shift packed 32-bit integers in a right by count while shifting in zeros, and
  5203. // store the results in dst.
  5204. //
  5205. // FOR j := 0 to 3
  5206. // i := j*32
  5207. // IF count[63:0] > 31
  5208. // dst[i+31:i] := 0
  5209. // ELSE
  5210. // dst[i+31:i] := ZeroExtend32(a[i+31:i] >> count[63:0])
  5211. // FI
  5212. // ENDFOR
  5213. //
  5214. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srl_epi32
  5215. FORCE_INLINE __m128i _mm_srl_epi32(__m128i a, __m128i count)
  5216. {
  5217. uint64_t c = vreinterpretq_nth_u64_m128i(count, 0);
  5218. if (_sse2neon_unlikely(c & ~31))
  5219. return _mm_setzero_si128();
  5220. int32x4_t vc = vdupq_n_s32(-(int32_t) c);
  5221. return vreinterpretq_m128i_u32(vshlq_u32(vreinterpretq_u32_m128i(a), vc));
  5222. }
  5223. // Shift packed 64-bit integers in a right by count while shifting in zeros, and
  5224. // store the results in dst.
  5225. //
  5226. // FOR j := 0 to 1
  5227. // i := j*64
  5228. // IF count[63:0] > 63
  5229. // dst[i+63:i] := 0
  5230. // ELSE
  5231. // dst[i+63:i] := ZeroExtend64(a[i+63:i] >> count[63:0])
  5232. // FI
  5233. // ENDFOR
  5234. //
  5235. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srl_epi64
  5236. FORCE_INLINE __m128i _mm_srl_epi64(__m128i a, __m128i count)
  5237. {
  5238. uint64_t c = vreinterpretq_nth_u64_m128i(count, 0);
  5239. if (_sse2neon_unlikely(c & ~63))
  5240. return _mm_setzero_si128();
  5241. int64x2_t vc = vdupq_n_s64(-(int64_t) c);
  5242. return vreinterpretq_m128i_u64(vshlq_u64(vreinterpretq_u64_m128i(a), vc));
  5243. }
  5244. // Shift packed 16-bit integers in a right by imm8 while shifting in zeros, and
  5245. // store the results in dst.
  5246. //
  5247. // FOR j := 0 to 7
  5248. // i := j*16
  5249. // IF imm8[7:0] > 15
  5250. // dst[i+15:i] := 0
  5251. // ELSE
  5252. // dst[i+15:i] := ZeroExtend16(a[i+15:i] >> imm8[7:0])
  5253. // FI
  5254. // ENDFOR
  5255. //
  5256. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srli_epi16
  5257. #define _mm_srli_epi16(a, imm) \
  5258. __extension__({ \
  5259. __m128i ret; \
  5260. if (_sse2neon_unlikely(imm & ~15)) { \
  5261. ret = _mm_setzero_si128(); \
  5262. } else { \
  5263. ret = vreinterpretq_m128i_u16( \
  5264. vshlq_u16(vreinterpretq_u16_m128i(a), vdupq_n_s16(-imm))); \
  5265. } \
  5266. ret; \
  5267. })
  5268. // Shift packed 32-bit integers in a right by imm8 while shifting in zeros, and
  5269. // store the results in dst.
  5270. //
  5271. // FOR j := 0 to 3
  5272. // i := j*32
  5273. // IF imm8[7:0] > 31
  5274. // dst[i+31:i] := 0
  5275. // ELSE
  5276. // dst[i+31:i] := ZeroExtend32(a[i+31:i] >> imm8[7:0])
  5277. // FI
  5278. // ENDFOR
  5279. //
  5280. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srli_epi32
  5281. // FORCE_INLINE __m128i _mm_srli_epi32(__m128i a, __constrange(0,255) int imm)
  5282. #define _mm_srli_epi32(a, imm) \
  5283. __extension__({ \
  5284. __m128i ret; \
  5285. if (_sse2neon_unlikely(imm & ~31)) { \
  5286. ret = _mm_setzero_si128(); \
  5287. } else { \
  5288. ret = vreinterpretq_m128i_u32( \
  5289. vshlq_u32(vreinterpretq_u32_m128i(a), vdupq_n_s32(-imm))); \
  5290. } \
  5291. ret; \
  5292. })
  5293. // Shift packed 64-bit integers in a right by imm8 while shifting in zeros, and
  5294. // store the results in dst.
  5295. //
  5296. // FOR j := 0 to 1
  5297. // i := j*64
  5298. // IF imm8[7:0] > 63
  5299. // dst[i+63:i] := 0
  5300. // ELSE
  5301. // dst[i+63:i] := ZeroExtend64(a[i+63:i] >> imm8[7:0])
  5302. // FI
  5303. // ENDFOR
  5304. //
  5305. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srli_epi64
  5306. #define _mm_srli_epi64(a, imm) \
  5307. __extension__({ \
  5308. __m128i ret; \
  5309. if (_sse2neon_unlikely(imm & ~63)) { \
  5310. ret = _mm_setzero_si128(); \
  5311. } else { \
  5312. ret = vreinterpretq_m128i_u64( \
  5313. vshlq_u64(vreinterpretq_u64_m128i(a), vdupq_n_s64(-imm))); \
  5314. } \
  5315. ret; \
  5316. })
  5317. // Shift a right by imm8 bytes while shifting in zeros, and store the results in
  5318. // dst.
  5319. //
  5320. // tmp := imm8[7:0]
  5321. // IF tmp > 15
  5322. // tmp := 16
  5323. // FI
  5324. // dst[127:0] := a[127:0] >> (tmp*8)
  5325. //
  5326. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_srli_si128
  5327. FORCE_INLINE __m128i _mm_srli_si128(__m128i a, int imm)
  5328. {
  5329. if (_sse2neon_unlikely(imm & ~15))
  5330. return _mm_setzero_si128();
  5331. uint8x16_t tmp[2] = {vreinterpretq_u8_m128i(a), vdupq_n_u8(0)};
  5332. return vreinterpretq_m128i_u8(vld1q_u8(((uint8_t const *) tmp) + imm));
  5333. }
  5334. // Store 128-bits (composed of 2 packed double-precision (64-bit) floating-point
  5335. // elements) from a into memory. mem_addr must be aligned on a 16-byte boundary
  5336. // or a general-protection exception may be generated.
  5337. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_store_pd
  5338. FORCE_INLINE void _mm_store_pd(double *mem_addr, __m128d a)
  5339. {
  5340. #if defined(__aarch64__)
  5341. vst1q_f64((float64_t *) mem_addr, vreinterpretq_f64_m128d(a));
  5342. #else
  5343. vst1q_f32((float32_t *) mem_addr, vreinterpretq_f32_m128d(a));
  5344. #endif
  5345. }
  5346. // Store the lower double-precision (64-bit) floating-point element from a into
  5347. // 2 contiguous elements in memory. mem_addr must be aligned on a 16-byte
  5348. // boundary or a general-protection exception may be generated.
  5349. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_store_pd1
  5350. FORCE_INLINE void _mm_store_pd1(double *mem_addr, __m128d a)
  5351. {
  5352. #if defined(__aarch64__)
  5353. float64x1_t a_low = vget_low_f64(vreinterpretq_f64_m128d(a));
  5354. vst1q_f64((float64_t *) mem_addr,
  5355. vreinterpretq_f64_m128d(vcombine_f64(a_low, a_low)));
  5356. #else
  5357. float32x2_t a_low = vget_low_f32(vreinterpretq_f32_m128d(a));
  5358. vst1q_f32((float32_t *) mem_addr,
  5359. vreinterpretq_f32_m128d(vcombine_f32(a_low, a_low)));
  5360. #endif
  5361. }
  5362. // Store the lower double-precision (64-bit) floating-point element from a into
  5363. // memory. mem_addr does not need to be aligned on any particular boundary.
  5364. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=mm_store_sd
  5365. FORCE_INLINE void _mm_store_sd(double *mem_addr, __m128d a)
  5366. {
  5367. #if defined(__aarch64__)
  5368. vst1_f64((float64_t *) mem_addr, vget_low_f64(vreinterpretq_f64_m128d(a)));
  5369. #else
  5370. vst1_u64((uint64_t *) mem_addr, vget_low_u64(vreinterpretq_u64_m128d(a)));
  5371. #endif
  5372. }
  5373. // Stores four 32-bit integer values as (as a __m128i value) at the address p.
  5374. // https://msdn.microsoft.com/en-us/library/vstudio/edk11s13(v=vs.100).aspx
  5375. FORCE_INLINE void _mm_store_si128(__m128i *p, __m128i a)
  5376. {
  5377. vst1q_s32((int32_t *) p, vreinterpretq_s32_m128i(a));
  5378. }
  5379. // Store the lower double-precision (64-bit) floating-point element from a into
  5380. // 2 contiguous elements in memory. mem_addr must be aligned on a 16-byte
  5381. // boundary or a general-protection exception may be generated.
  5382. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#expand=9,526,5601&text=_mm_store1_pd
  5383. #define _mm_store1_pd _mm_store_pd1
  5384. // Store the upper double-precision (64-bit) floating-point element from a into
  5385. // memory.
  5386. //
  5387. // MEM[mem_addr+63:mem_addr] := a[127:64]
  5388. //
  5389. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storeh_pd
  5390. FORCE_INLINE void _mm_storeh_pd(double *mem_addr, __m128d a)
  5391. {
  5392. #if defined(__aarch64__)
  5393. vst1_f64((float64_t *) mem_addr, vget_high_f64(vreinterpretq_f64_m128d(a)));
  5394. #else
  5395. vst1_f32((float32_t *) mem_addr, vget_high_f32(vreinterpretq_f32_m128d(a)));
  5396. #endif
  5397. }
  5398. // Reads the lower 64 bits of b and stores them into the lower 64 bits of a.
  5399. // https://msdn.microsoft.com/en-us/library/hhwf428f%28v=vs.90%29.aspx
  5400. FORCE_INLINE void _mm_storel_epi64(__m128i *a, __m128i b)
  5401. {
  5402. uint64x1_t hi = vget_high_u64(vreinterpretq_u64_m128i(*a));
  5403. uint64x1_t lo = vget_low_u64(vreinterpretq_u64_m128i(b));
  5404. *a = vreinterpretq_m128i_u64(vcombine_u64(lo, hi));
  5405. }
  5406. // Store the lower double-precision (64-bit) floating-point element from a into
  5407. // memory.
  5408. //
  5409. // MEM[mem_addr+63:mem_addr] := a[63:0]
  5410. //
  5411. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storel_pd
  5412. FORCE_INLINE void _mm_storel_pd(double *mem_addr, __m128d a)
  5413. {
  5414. #if defined(__aarch64__)
  5415. vst1_f64((float64_t *) mem_addr, vget_low_f64(vreinterpretq_f64_m128d(a)));
  5416. #else
  5417. vst1_f32((float32_t *) mem_addr, vget_low_f32(vreinterpretq_f32_m128d(a)));
  5418. #endif
  5419. }
  5420. // Store 2 double-precision (64-bit) floating-point elements from a into memory
  5421. // in reverse order. mem_addr must be aligned on a 16-byte boundary or a
  5422. // general-protection exception may be generated.
  5423. //
  5424. // MEM[mem_addr+63:mem_addr] := a[127:64]
  5425. // MEM[mem_addr+127:mem_addr+64] := a[63:0]
  5426. //
  5427. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storer_pd
  5428. FORCE_INLINE void _mm_storer_pd(double *mem_addr, __m128d a)
  5429. {
  5430. float32x4_t f = vreinterpretq_f32_m128d(a);
  5431. _mm_store_pd(mem_addr, vreinterpretq_m128d_f32(vextq_f32(f, f, 2)));
  5432. }
  5433. // Store 128-bits (composed of 2 packed double-precision (64-bit) floating-point
  5434. // elements) from a into memory. mem_addr does not need to be aligned on any
  5435. // particular boundary.
  5436. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storeu_pd
  5437. FORCE_INLINE void _mm_storeu_pd(double *mem_addr, __m128d a)
  5438. {
  5439. _mm_store_pd(mem_addr, a);
  5440. }
  5441. // Stores 128-bits of integer data a at the address p.
  5442. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storeu_si128
  5443. FORCE_INLINE void _mm_storeu_si128(__m128i *p, __m128i a)
  5444. {
  5445. vst1q_s32((int32_t *) p, vreinterpretq_s32_m128i(a));
  5446. }
  5447. // Stores 32-bits of integer data a at the address p.
  5448. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_storeu_si32
  5449. FORCE_INLINE void _mm_storeu_si32(void *p, __m128i a)
  5450. {
  5451. vst1q_lane_s32((int32_t *) p, vreinterpretq_s32_m128i(a), 0);
  5452. }
  5453. // Store 128-bits (composed of 2 packed double-precision (64-bit) floating-point
  5454. // elements) from a into memory using a non-temporal memory hint. mem_addr must
  5455. // be aligned on a 16-byte boundary or a general-protection exception may be
  5456. // generated.
  5457. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_stream_pd
  5458. FORCE_INLINE void _mm_stream_pd(double *p, __m128d a)
  5459. {
  5460. #if __has_builtin(__builtin_nontemporal_store)
  5461. __builtin_nontemporal_store(a, (float32x4_t *) p);
  5462. #elif defined(__aarch64__)
  5463. vst1q_f64(p, vreinterpretq_f64_m128d(a));
  5464. #else
  5465. vst1q_s64((int64_t *) p, vreinterpretq_s64_m128d(a));
  5466. #endif
  5467. }
  5468. // Stores the data in a to the address p without polluting the caches. If the
  5469. // cache line containing address p is already in the cache, the cache will be
  5470. // updated.
  5471. // https://msdn.microsoft.com/en-us/library/ba08y07y%28v=vs.90%29.aspx
  5472. FORCE_INLINE void _mm_stream_si128(__m128i *p, __m128i a)
  5473. {
  5474. #if __has_builtin(__builtin_nontemporal_store)
  5475. __builtin_nontemporal_store(a, p);
  5476. #else
  5477. vst1q_s64((int64_t *) p, vreinterpretq_s64_m128i(a));
  5478. #endif
  5479. }
  5480. // Store 32-bit integer a into memory using a non-temporal hint to minimize
  5481. // cache pollution. If the cache line containing address mem_addr is already in
  5482. // the cache, the cache will be updated.
  5483. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_stream_si32
  5484. FORCE_INLINE void _mm_stream_si32(int *p, int a)
  5485. {
  5486. vst1q_lane_s32((int32_t *) p, vdupq_n_s32(a), 0);
  5487. }
  5488. // Subtract packed 16-bit integers in b from packed 16-bit integers in a, and
  5489. // store the results in dst.
  5490. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sub_epi16
  5491. FORCE_INLINE __m128i _mm_sub_epi16(__m128i a, __m128i b)
  5492. {
  5493. return vreinterpretq_m128i_s16(
  5494. vsubq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  5495. }
  5496. // Subtracts the 4 signed or unsigned 32-bit integers of b from the 4 signed or
  5497. // unsigned 32-bit integers of a.
  5498. //
  5499. // r0 := a0 - b0
  5500. // r1 := a1 - b1
  5501. // r2 := a2 - b2
  5502. // r3 := a3 - b3
  5503. //
  5504. // https://msdn.microsoft.com/en-us/library/vstudio/fhh866h0(v=vs.100).aspx
  5505. FORCE_INLINE __m128i _mm_sub_epi32(__m128i a, __m128i b)
  5506. {
  5507. return vreinterpretq_m128i_s32(
  5508. vsubq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  5509. }
  5510. // Subtract 2 packed 64-bit integers in b from 2 packed 64-bit integers in a,
  5511. // and store the results in dst.
  5512. // r0 := a0 - b0
  5513. // r1 := a1 - b1
  5514. FORCE_INLINE __m128i _mm_sub_epi64(__m128i a, __m128i b)
  5515. {
  5516. return vreinterpretq_m128i_s64(
  5517. vsubq_s64(vreinterpretq_s64_m128i(a), vreinterpretq_s64_m128i(b)));
  5518. }
  5519. // Subtract packed 8-bit integers in b from packed 8-bit integers in a, and
  5520. // store the results in dst.
  5521. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sub_epi8
  5522. FORCE_INLINE __m128i _mm_sub_epi8(__m128i a, __m128i b)
  5523. {
  5524. return vreinterpretq_m128i_s8(
  5525. vsubq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  5526. }
  5527. // Subtract packed double-precision (64-bit) floating-point elements in b from
  5528. // packed double-precision (64-bit) floating-point elements in a, and store the
  5529. // results in dst.
  5530. //
  5531. // FOR j := 0 to 1
  5532. // i := j*64
  5533. // dst[i+63:i] := a[i+63:i] - b[i+63:i]
  5534. // ENDFOR
  5535. //
  5536. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=mm_sub_pd
  5537. FORCE_INLINE __m128d _mm_sub_pd(__m128d a, __m128d b)
  5538. {
  5539. #if defined(__aarch64__)
  5540. return vreinterpretq_m128d_f64(
  5541. vsubq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  5542. #else
  5543. double *da = (double *) &a;
  5544. double *db = (double *) &b;
  5545. double c[2];
  5546. c[0] = da[0] - db[0];
  5547. c[1] = da[1] - db[1];
  5548. return vld1q_f32((float32_t *) c);
  5549. #endif
  5550. }
  5551. // Subtract the lower double-precision (64-bit) floating-point element in b from
  5552. // the lower double-precision (64-bit) floating-point element in a, store the
  5553. // result in the lower element of dst, and copy the upper element from a to the
  5554. // upper element of dst.
  5555. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sub_sd
  5556. FORCE_INLINE __m128d _mm_sub_sd(__m128d a, __m128d b)
  5557. {
  5558. return _mm_move_sd(a, _mm_sub_pd(a, b));
  5559. }
  5560. // Subtract 64-bit integer b from 64-bit integer a, and store the result in dst.
  5561. //
  5562. // dst[63:0] := a[63:0] - b[63:0]
  5563. //
  5564. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sub_si64
  5565. FORCE_INLINE __m64 _mm_sub_si64(__m64 a, __m64 b)
  5566. {
  5567. return vreinterpret_m64_s64(
  5568. vsub_s64(vreinterpret_s64_m64(a), vreinterpret_s64_m64(b)));
  5569. }
  5570. // Subtracts the 8 signed 16-bit integers of b from the 8 signed 16-bit integers
  5571. // of a and saturates.
  5572. //
  5573. // r0 := SignedSaturate(a0 - b0)
  5574. // r1 := SignedSaturate(a1 - b1)
  5575. // ...
  5576. // r7 := SignedSaturate(a7 - b7)
  5577. //
  5578. // https://technet.microsoft.com/en-us/subscriptions/3247z5b8(v=vs.90)
  5579. FORCE_INLINE __m128i _mm_subs_epi16(__m128i a, __m128i b)
  5580. {
  5581. return vreinterpretq_m128i_s16(
  5582. vqsubq_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  5583. }
  5584. // Subtracts the 16 signed 8-bit integers of b from the 16 signed 8-bit integers
  5585. // of a and saturates.
  5586. //
  5587. // r0 := SignedSaturate(a0 - b0)
  5588. // r1 := SignedSaturate(a1 - b1)
  5589. // ...
  5590. // r15 := SignedSaturate(a15 - b15)
  5591. //
  5592. // https://technet.microsoft.com/en-us/subscriptions/by7kzks1(v=vs.90)
  5593. FORCE_INLINE __m128i _mm_subs_epi8(__m128i a, __m128i b)
  5594. {
  5595. return vreinterpretq_m128i_s8(
  5596. vqsubq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  5597. }
  5598. // Subtracts the 8 unsigned 16-bit integers of bfrom the 8 unsigned 16-bit
  5599. // integers of a and saturates..
  5600. // https://technet.microsoft.com/en-us/subscriptions/index/f44y0s19(v=vs.90).aspx
  5601. FORCE_INLINE __m128i _mm_subs_epu16(__m128i a, __m128i b)
  5602. {
  5603. return vreinterpretq_m128i_u16(
  5604. vqsubq_u16(vreinterpretq_u16_m128i(a), vreinterpretq_u16_m128i(b)));
  5605. }
  5606. // Subtracts the 16 unsigned 8-bit integers of b from the 16 unsigned 8-bit
  5607. // integers of a and saturates.
  5608. //
  5609. // r0 := UnsignedSaturate(a0 - b0)
  5610. // r1 := UnsignedSaturate(a1 - b1)
  5611. // ...
  5612. // r15 := UnsignedSaturate(a15 - b15)
  5613. //
  5614. // https://technet.microsoft.com/en-us/subscriptions/yadkxc18(v=vs.90)
  5615. FORCE_INLINE __m128i _mm_subs_epu8(__m128i a, __m128i b)
  5616. {
  5617. return vreinterpretq_m128i_u8(
  5618. vqsubq_u8(vreinterpretq_u8_m128i(a), vreinterpretq_u8_m128i(b)));
  5619. }
  5620. #define _mm_ucomieq_sd _mm_comieq_sd
  5621. #define _mm_ucomige_sd _mm_comige_sd
  5622. #define _mm_ucomigt_sd _mm_comigt_sd
  5623. #define _mm_ucomile_sd _mm_comile_sd
  5624. #define _mm_ucomilt_sd _mm_comilt_sd
  5625. #define _mm_ucomineq_sd _mm_comineq_sd
  5626. // Return vector of type __m128d with undefined elements.
  5627. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_undefined_pd
  5628. FORCE_INLINE __m128d _mm_undefined_pd(void)
  5629. {
  5630. #if defined(__GNUC__) || defined(__clang__)
  5631. #pragma GCC diagnostic push
  5632. #pragma GCC diagnostic ignored "-Wuninitialized"
  5633. #endif
  5634. __m128d a;
  5635. return a;
  5636. #if defined(__GNUC__) || defined(__clang__)
  5637. #pragma GCC diagnostic pop
  5638. #endif
  5639. }
  5640. // Interleaves the upper 4 signed or unsigned 16-bit integers in a with the
  5641. // upper 4 signed or unsigned 16-bit integers in b.
  5642. //
  5643. // r0 := a4
  5644. // r1 := b4
  5645. // r2 := a5
  5646. // r3 := b5
  5647. // r4 := a6
  5648. // r5 := b6
  5649. // r6 := a7
  5650. // r7 := b7
  5651. //
  5652. // https://msdn.microsoft.com/en-us/library/03196cz7(v=vs.100).aspx
  5653. FORCE_INLINE __m128i _mm_unpackhi_epi16(__m128i a, __m128i b)
  5654. {
  5655. #if defined(__aarch64__)
  5656. return vreinterpretq_m128i_s16(
  5657. vzip2q_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  5658. #else
  5659. int16x4_t a1 = vget_high_s16(vreinterpretq_s16_m128i(a));
  5660. int16x4_t b1 = vget_high_s16(vreinterpretq_s16_m128i(b));
  5661. int16x4x2_t result = vzip_s16(a1, b1);
  5662. return vreinterpretq_m128i_s16(vcombine_s16(result.val[0], result.val[1]));
  5663. #endif
  5664. }
  5665. // Interleaves the upper 2 signed or unsigned 32-bit integers in a with the
  5666. // upper 2 signed or unsigned 32-bit integers in b.
  5667. // https://msdn.microsoft.com/en-us/library/65sa7cbs(v=vs.100).aspx
  5668. FORCE_INLINE __m128i _mm_unpackhi_epi32(__m128i a, __m128i b)
  5669. {
  5670. #if defined(__aarch64__)
  5671. return vreinterpretq_m128i_s32(
  5672. vzip2q_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  5673. #else
  5674. int32x2_t a1 = vget_high_s32(vreinterpretq_s32_m128i(a));
  5675. int32x2_t b1 = vget_high_s32(vreinterpretq_s32_m128i(b));
  5676. int32x2x2_t result = vzip_s32(a1, b1);
  5677. return vreinterpretq_m128i_s32(vcombine_s32(result.val[0], result.val[1]));
  5678. #endif
  5679. }
  5680. // Interleaves the upper signed or unsigned 64-bit integer in a with the
  5681. // upper signed or unsigned 64-bit integer in b.
  5682. //
  5683. // r0 := a1
  5684. // r1 := b1
  5685. FORCE_INLINE __m128i _mm_unpackhi_epi64(__m128i a, __m128i b)
  5686. {
  5687. int64x1_t a_h = vget_high_s64(vreinterpretq_s64_m128i(a));
  5688. int64x1_t b_h = vget_high_s64(vreinterpretq_s64_m128i(b));
  5689. return vreinterpretq_m128i_s64(vcombine_s64(a_h, b_h));
  5690. }
  5691. // Interleaves the upper 8 signed or unsigned 8-bit integers in a with the upper
  5692. // 8 signed or unsigned 8-bit integers in b.
  5693. //
  5694. // r0 := a8
  5695. // r1 := b8
  5696. // r2 := a9
  5697. // r3 := b9
  5698. // ...
  5699. // r14 := a15
  5700. // r15 := b15
  5701. //
  5702. // https://msdn.microsoft.com/en-us/library/t5h7783k(v=vs.100).aspx
  5703. FORCE_INLINE __m128i _mm_unpackhi_epi8(__m128i a, __m128i b)
  5704. {
  5705. #if defined(__aarch64__)
  5706. return vreinterpretq_m128i_s8(
  5707. vzip2q_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  5708. #else
  5709. int8x8_t a1 =
  5710. vreinterpret_s8_s16(vget_high_s16(vreinterpretq_s16_m128i(a)));
  5711. int8x8_t b1 =
  5712. vreinterpret_s8_s16(vget_high_s16(vreinterpretq_s16_m128i(b)));
  5713. int8x8x2_t result = vzip_s8(a1, b1);
  5714. return vreinterpretq_m128i_s8(vcombine_s8(result.val[0], result.val[1]));
  5715. #endif
  5716. }
  5717. // Unpack and interleave double-precision (64-bit) floating-point elements from
  5718. // the high half of a and b, and store the results in dst.
  5719. //
  5720. // DEFINE INTERLEAVE_HIGH_QWORDS(src1[127:0], src2[127:0]) {
  5721. // dst[63:0] := src1[127:64]
  5722. // dst[127:64] := src2[127:64]
  5723. // RETURN dst[127:0]
  5724. // }
  5725. // dst[127:0] := INTERLEAVE_HIGH_QWORDS(a[127:0], b[127:0])
  5726. //
  5727. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_unpackhi_pd
  5728. FORCE_INLINE __m128d _mm_unpackhi_pd(__m128d a, __m128d b)
  5729. {
  5730. #if defined(__aarch64__)
  5731. return vreinterpretq_m128d_f64(
  5732. vzip2q_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  5733. #else
  5734. return vreinterpretq_m128d_s64(
  5735. vcombine_s64(vget_high_s64(vreinterpretq_s64_m128d(a)),
  5736. vget_high_s64(vreinterpretq_s64_m128d(b))));
  5737. #endif
  5738. }
  5739. // Interleaves the lower 4 signed or unsigned 16-bit integers in a with the
  5740. // lower 4 signed or unsigned 16-bit integers in b.
  5741. //
  5742. // r0 := a0
  5743. // r1 := b0
  5744. // r2 := a1
  5745. // r3 := b1
  5746. // r4 := a2
  5747. // r5 := b2
  5748. // r6 := a3
  5749. // r7 := b3
  5750. //
  5751. // https://msdn.microsoft.com/en-us/library/btxb17bw%28v=vs.90%29.aspx
  5752. FORCE_INLINE __m128i _mm_unpacklo_epi16(__m128i a, __m128i b)
  5753. {
  5754. #if defined(__aarch64__)
  5755. return vreinterpretq_m128i_s16(
  5756. vzip1q_s16(vreinterpretq_s16_m128i(a), vreinterpretq_s16_m128i(b)));
  5757. #else
  5758. int16x4_t a1 = vget_low_s16(vreinterpretq_s16_m128i(a));
  5759. int16x4_t b1 = vget_low_s16(vreinterpretq_s16_m128i(b));
  5760. int16x4x2_t result = vzip_s16(a1, b1);
  5761. return vreinterpretq_m128i_s16(vcombine_s16(result.val[0], result.val[1]));
  5762. #endif
  5763. }
  5764. // Interleaves the lower 2 signed or unsigned 32 - bit integers in a with the
  5765. // lower 2 signed or unsigned 32 - bit integers in b.
  5766. //
  5767. // r0 := a0
  5768. // r1 := b0
  5769. // r2 := a1
  5770. // r3 := b1
  5771. //
  5772. // https://msdn.microsoft.com/en-us/library/x8atst9d(v=vs.100).aspx
  5773. FORCE_INLINE __m128i _mm_unpacklo_epi32(__m128i a, __m128i b)
  5774. {
  5775. #if defined(__aarch64__)
  5776. return vreinterpretq_m128i_s32(
  5777. vzip1q_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  5778. #else
  5779. int32x2_t a1 = vget_low_s32(vreinterpretq_s32_m128i(a));
  5780. int32x2_t b1 = vget_low_s32(vreinterpretq_s32_m128i(b));
  5781. int32x2x2_t result = vzip_s32(a1, b1);
  5782. return vreinterpretq_m128i_s32(vcombine_s32(result.val[0], result.val[1]));
  5783. #endif
  5784. }
  5785. FORCE_INLINE __m128i _mm_unpacklo_epi64(__m128i a, __m128i b)
  5786. {
  5787. int64x1_t a_l = vget_low_s64(vreinterpretq_s64_m128i(a));
  5788. int64x1_t b_l = vget_low_s64(vreinterpretq_s64_m128i(b));
  5789. return vreinterpretq_m128i_s64(vcombine_s64(a_l, b_l));
  5790. }
  5791. // Interleaves the lower 8 signed or unsigned 8-bit integers in a with the lower
  5792. // 8 signed or unsigned 8-bit integers in b.
  5793. //
  5794. // r0 := a0
  5795. // r1 := b0
  5796. // r2 := a1
  5797. // r3 := b1
  5798. // ...
  5799. // r14 := a7
  5800. // r15 := b7
  5801. //
  5802. // https://msdn.microsoft.com/en-us/library/xf7k860c%28v=vs.90%29.aspx
  5803. FORCE_INLINE __m128i _mm_unpacklo_epi8(__m128i a, __m128i b)
  5804. {
  5805. #if defined(__aarch64__)
  5806. return vreinterpretq_m128i_s8(
  5807. vzip1q_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  5808. #else
  5809. int8x8_t a1 = vreinterpret_s8_s16(vget_low_s16(vreinterpretq_s16_m128i(a)));
  5810. int8x8_t b1 = vreinterpret_s8_s16(vget_low_s16(vreinterpretq_s16_m128i(b)));
  5811. int8x8x2_t result = vzip_s8(a1, b1);
  5812. return vreinterpretq_m128i_s8(vcombine_s8(result.val[0], result.val[1]));
  5813. #endif
  5814. }
  5815. // Unpack and interleave double-precision (64-bit) floating-point elements from
  5816. // the low half of a and b, and store the results in dst.
  5817. //
  5818. // DEFINE INTERLEAVE_QWORDS(src1[127:0], src2[127:0]) {
  5819. // dst[63:0] := src1[63:0]
  5820. // dst[127:64] := src2[63:0]
  5821. // RETURN dst[127:0]
  5822. // }
  5823. // dst[127:0] := INTERLEAVE_QWORDS(a[127:0], b[127:0])
  5824. //
  5825. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_unpacklo_pd
  5826. FORCE_INLINE __m128d _mm_unpacklo_pd(__m128d a, __m128d b)
  5827. {
  5828. #if defined(__aarch64__)
  5829. return vreinterpretq_m128d_f64(
  5830. vzip1q_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  5831. #else
  5832. return vreinterpretq_m128d_s64(
  5833. vcombine_s64(vget_low_s64(vreinterpretq_s64_m128d(a)),
  5834. vget_low_s64(vreinterpretq_s64_m128d(b))));
  5835. #endif
  5836. }
  5837. // Compute the bitwise XOR of packed double-precision (64-bit) floating-point
  5838. // elements in a and b, and store the results in dst.
  5839. //
  5840. // FOR j := 0 to 1
  5841. // i := j*64
  5842. // dst[i+63:i] := a[i+63:i] XOR b[i+63:i]
  5843. // ENDFOR
  5844. //
  5845. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_xor_pd
  5846. FORCE_INLINE __m128d _mm_xor_pd(__m128d a, __m128d b)
  5847. {
  5848. return vreinterpretq_m128d_s64(
  5849. veorq_s64(vreinterpretq_s64_m128d(a), vreinterpretq_s64_m128d(b)));
  5850. }
  5851. // Computes the bitwise XOR of the 128-bit value in a and the 128-bit value in
  5852. // b. https://msdn.microsoft.com/en-us/library/fzt08www(v=vs.100).aspx
  5853. FORCE_INLINE __m128i _mm_xor_si128(__m128i a, __m128i b)
  5854. {
  5855. return vreinterpretq_m128i_s32(
  5856. veorq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  5857. }
  5858. /* SSE3 */
  5859. // Alternatively add and subtract packed double-precision (64-bit)
  5860. // floating-point elements in a to/from packed elements in b, and store the
  5861. // results in dst.
  5862. //
  5863. // FOR j := 0 to 1
  5864. // i := j*64
  5865. // IF ((j & 1) == 0)
  5866. // dst[i+63:i] := a[i+63:i] - b[i+63:i]
  5867. // ELSE
  5868. // dst[i+63:i] := a[i+63:i] + b[i+63:i]
  5869. // FI
  5870. // ENDFOR
  5871. //
  5872. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_addsub_pd
  5873. FORCE_INLINE __m128d _mm_addsub_pd(__m128d a, __m128d b)
  5874. {
  5875. __m128d mask = _mm_set_pd(1.0f, -1.0f);
  5876. #if defined(__aarch64__)
  5877. return vreinterpretq_m128d_f64(vfmaq_f64(vreinterpretq_f64_m128d(a),
  5878. vreinterpretq_f64_m128d(b),
  5879. vreinterpretq_f64_m128d(mask)));
  5880. #else
  5881. return _mm_add_pd(_mm_mul_pd(b, mask), a);
  5882. #endif
  5883. }
  5884. // Alternatively add and subtract packed single-precision (32-bit)
  5885. // floating-point elements in a to/from packed elements in b, and store the
  5886. // results in dst.
  5887. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=addsub_ps
  5888. FORCE_INLINE __m128 _mm_addsub_ps(__m128 a, __m128 b)
  5889. {
  5890. __m128 mask = {-1.0f, 1.0f, -1.0f, 1.0f};
  5891. #if defined(__aarch64__) || defined(__ARM_FEATURE_FMA) /* VFPv4+ */
  5892. return vreinterpretq_m128_f32(vfmaq_f32(vreinterpretq_f32_m128(a),
  5893. vreinterpretq_f32_m128(mask),
  5894. vreinterpretq_f32_m128(b)));
  5895. #else
  5896. return _mm_add_ps(_mm_mul_ps(b, mask), a);
  5897. #endif
  5898. }
  5899. // Horizontally add adjacent pairs of double-precision (64-bit) floating-point
  5900. // elements in a and b, and pack the results in dst.
  5901. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hadd_pd
  5902. FORCE_INLINE __m128d _mm_hadd_pd(__m128d a, __m128d b)
  5903. {
  5904. #if defined(__aarch64__)
  5905. return vreinterpretq_m128d_f64(
  5906. vpaddq_f64(vreinterpretq_f64_m128d(a), vreinterpretq_f64_m128d(b)));
  5907. #else
  5908. double *da = (double *) &a;
  5909. double *db = (double *) &b;
  5910. double c[] = {da[0] + da[1], db[0] + db[1]};
  5911. return vreinterpretq_m128d_u64(vld1q_u64((uint64_t *) c));
  5912. #endif
  5913. }
  5914. // Computes pairwise add of each argument as single-precision, floating-point
  5915. // values a and b.
  5916. // https://msdn.microsoft.com/en-us/library/yd9wecaa.aspx
  5917. FORCE_INLINE __m128 _mm_hadd_ps(__m128 a, __m128 b)
  5918. {
  5919. #if defined(__aarch64__)
  5920. return vreinterpretq_m128_f32(
  5921. vpaddq_f32(vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b)));
  5922. #else
  5923. float32x2_t a10 = vget_low_f32(vreinterpretq_f32_m128(a));
  5924. float32x2_t a32 = vget_high_f32(vreinterpretq_f32_m128(a));
  5925. float32x2_t b10 = vget_low_f32(vreinterpretq_f32_m128(b));
  5926. float32x2_t b32 = vget_high_f32(vreinterpretq_f32_m128(b));
  5927. return vreinterpretq_m128_f32(
  5928. vcombine_f32(vpadd_f32(a10, a32), vpadd_f32(b10, b32)));
  5929. #endif
  5930. }
  5931. // Horizontally subtract adjacent pairs of double-precision (64-bit)
  5932. // floating-point elements in a and b, and pack the results in dst.
  5933. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hsub_pd
  5934. FORCE_INLINE __m128d _mm_hsub_pd(__m128d _a, __m128d _b)
  5935. {
  5936. #if defined(__aarch64__)
  5937. return vreinterpretq_m128d_f64(vsubq_f64(
  5938. vuzp1q_f64(vreinterpretq_f64_m128d(_a), vreinterpretq_f64_m128d(_b)),
  5939. vuzp2q_f64(vreinterpretq_f64_m128d(_a), vreinterpretq_f64_m128d(_b))));
  5940. #else
  5941. double *da = (double *) &_a;
  5942. double *db = (double *) &_b;
  5943. double c[] = {da[0] - da[1], db[0] - db[1]};
  5944. return vreinterpretq_m128d_u64(vld1q_u64((uint64_t *) c));
  5945. #endif
  5946. }
  5947. // Horizontally substract adjacent pairs of single-precision (32-bit)
  5948. // floating-point elements in a and b, and pack the results in dst.
  5949. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hsub_ps
  5950. FORCE_INLINE __m128 _mm_hsub_ps(__m128 _a, __m128 _b)
  5951. {
  5952. #if defined(__aarch64__)
  5953. return vreinterpretq_m128_f32(vsubq_f32(
  5954. vuzp1q_f32(vreinterpretq_f32_m128(_a), vreinterpretq_f32_m128(_b)),
  5955. vuzp2q_f32(vreinterpretq_f32_m128(_a), vreinterpretq_f32_m128(_b))));
  5956. #else
  5957. float32x4x2_t c =
  5958. vuzpq_f32(vreinterpretq_f32_m128(_a), vreinterpretq_f32_m128(_b));
  5959. return vreinterpretq_m128_f32(vsubq_f32(c.val[0], c.val[1]));
  5960. #endif
  5961. }
  5962. // Load 128-bits of integer data from unaligned memory into dst. This intrinsic
  5963. // may perform better than _mm_loadu_si128 when the data crosses a cache line
  5964. // boundary.
  5965. //
  5966. // dst[127:0] := MEM[mem_addr+127:mem_addr]
  5967. //
  5968. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_lddqu_si128
  5969. #define _mm_lddqu_si128 _mm_loadu_si128
  5970. // Load a double-precision (64-bit) floating-point element from memory into both
  5971. // elements of dst.
  5972. //
  5973. // dst[63:0] := MEM[mem_addr+63:mem_addr]
  5974. // dst[127:64] := MEM[mem_addr+63:mem_addr]
  5975. //
  5976. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_loaddup_pd
  5977. #define _mm_loaddup_pd _mm_load1_pd
  5978. // Duplicate the low double-precision (64-bit) floating-point element from a,
  5979. // and store the results in dst.
  5980. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_movedup_pd
  5981. FORCE_INLINE __m128d _mm_movedup_pd(__m128d a)
  5982. {
  5983. #if (__aarch64__)
  5984. return vreinterpretq_m128d_f64(
  5985. vdupq_laneq_f64(vreinterpretq_f64_m128d(a), 0));
  5986. #else
  5987. return vreinterpretq_m128d_u64(
  5988. vdupq_n_u64(vgetq_lane_u64(vreinterpretq_u64_m128d(a), 0)));
  5989. #endif
  5990. }
  5991. // Duplicate odd-indexed single-precision (32-bit) floating-point elements
  5992. // from a, and store the results in dst.
  5993. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_movehdup_ps
  5994. FORCE_INLINE __m128 _mm_movehdup_ps(__m128 a)
  5995. {
  5996. #if __has_builtin(__builtin_shufflevector)
  5997. return vreinterpretq_m128_f32(__builtin_shufflevector(
  5998. vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a), 1, 1, 3, 3));
  5999. #else
  6000. float32_t a1 = vgetq_lane_f32(vreinterpretq_f32_m128(a), 1);
  6001. float32_t a3 = vgetq_lane_f32(vreinterpretq_f32_m128(a), 3);
  6002. float ALIGN_STRUCT(16) data[4] = {a1, a1, a3, a3};
  6003. return vreinterpretq_m128_f32(vld1q_f32(data));
  6004. #endif
  6005. }
  6006. // Duplicate even-indexed single-precision (32-bit) floating-point elements
  6007. // from a, and store the results in dst.
  6008. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_moveldup_ps
  6009. FORCE_INLINE __m128 _mm_moveldup_ps(__m128 a)
  6010. {
  6011. #if __has_builtin(__builtin_shufflevector)
  6012. return vreinterpretq_m128_f32(__builtin_shufflevector(
  6013. vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(a), 0, 0, 2, 2));
  6014. #else
  6015. float32_t a0 = vgetq_lane_f32(vreinterpretq_f32_m128(a), 0);
  6016. float32_t a2 = vgetq_lane_f32(vreinterpretq_f32_m128(a), 2);
  6017. float ALIGN_STRUCT(16) data[4] = {a0, a0, a2, a2};
  6018. return vreinterpretq_m128_f32(vld1q_f32(data));
  6019. #endif
  6020. }
  6021. /* SSSE3 */
  6022. // Compute the absolute value of packed signed 16-bit integers in a, and store
  6023. // the unsigned results in dst.
  6024. //
  6025. // FOR j := 0 to 7
  6026. // i := j*16
  6027. // dst[i+15:i] := ABS(a[i+15:i])
  6028. // ENDFOR
  6029. //
  6030. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_abs_epi16
  6031. FORCE_INLINE __m128i _mm_abs_epi16(__m128i a)
  6032. {
  6033. return vreinterpretq_m128i_s16(vabsq_s16(vreinterpretq_s16_m128i(a)));
  6034. }
  6035. // Compute the absolute value of packed signed 32-bit integers in a, and store
  6036. // the unsigned results in dst.
  6037. //
  6038. // FOR j := 0 to 3
  6039. // i := j*32
  6040. // dst[i+31:i] := ABS(a[i+31:i])
  6041. // ENDFOR
  6042. //
  6043. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_abs_epi32
  6044. FORCE_INLINE __m128i _mm_abs_epi32(__m128i a)
  6045. {
  6046. return vreinterpretq_m128i_s32(vabsq_s32(vreinterpretq_s32_m128i(a)));
  6047. }
  6048. // Compute the absolute value of packed signed 8-bit integers in a, and store
  6049. // the unsigned results in dst.
  6050. //
  6051. // FOR j := 0 to 15
  6052. // i := j*8
  6053. // dst[i+7:i] := ABS(a[i+7:i])
  6054. // ENDFOR
  6055. //
  6056. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_abs_epi8
  6057. FORCE_INLINE __m128i _mm_abs_epi8(__m128i a)
  6058. {
  6059. return vreinterpretq_m128i_s8(vabsq_s8(vreinterpretq_s8_m128i(a)));
  6060. }
  6061. // Compute the absolute value of packed signed 16-bit integers in a, and store
  6062. // the unsigned results in dst.
  6063. //
  6064. // FOR j := 0 to 3
  6065. // i := j*16
  6066. // dst[i+15:i] := ABS(a[i+15:i])
  6067. // ENDFOR
  6068. //
  6069. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_abs_pi16
  6070. FORCE_INLINE __m64 _mm_abs_pi16(__m64 a)
  6071. {
  6072. return vreinterpret_m64_s16(vabs_s16(vreinterpret_s16_m64(a)));
  6073. }
  6074. // Compute the absolute value of packed signed 32-bit integers in a, and store
  6075. // the unsigned results in dst.
  6076. //
  6077. // FOR j := 0 to 1
  6078. // i := j*32
  6079. // dst[i+31:i] := ABS(a[i+31:i])
  6080. // ENDFOR
  6081. //
  6082. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_abs_pi32
  6083. FORCE_INLINE __m64 _mm_abs_pi32(__m64 a)
  6084. {
  6085. return vreinterpret_m64_s32(vabs_s32(vreinterpret_s32_m64(a)));
  6086. }
  6087. // Compute the absolute value of packed signed 8-bit integers in a, and store
  6088. // the unsigned results in dst.
  6089. //
  6090. // FOR j := 0 to 7
  6091. // i := j*8
  6092. // dst[i+7:i] := ABS(a[i+7:i])
  6093. // ENDFOR
  6094. //
  6095. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_abs_pi8
  6096. FORCE_INLINE __m64 _mm_abs_pi8(__m64 a)
  6097. {
  6098. return vreinterpret_m64_s8(vabs_s8(vreinterpret_s8_m64(a)));
  6099. }
  6100. // Concatenate 16-byte blocks in a and b into a 32-byte temporary result, shift
  6101. // the result right by imm8 bytes, and store the low 16 bytes in dst.
  6102. //
  6103. // tmp[255:0] := ((a[127:0] << 128)[255:0] OR b[127:0]) >> (imm8*8)
  6104. // dst[127:0] := tmp[127:0]
  6105. //
  6106. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_alignr_epi8
  6107. FORCE_INLINE __m128i _mm_alignr_epi8(__m128i a, __m128i b, int imm)
  6108. {
  6109. if (_sse2neon_unlikely(imm & ~31))
  6110. return _mm_setzero_si128();
  6111. int idx;
  6112. uint8x16_t tmp[2];
  6113. if (imm >= 16) {
  6114. idx = imm - 16;
  6115. tmp[0] = vreinterpretq_u8_m128i(a);
  6116. tmp[1] = vdupq_n_u8(0);
  6117. } else {
  6118. idx = imm;
  6119. tmp[0] = vreinterpretq_u8_m128i(b);
  6120. tmp[1] = vreinterpretq_u8_m128i(a);
  6121. }
  6122. return vreinterpretq_m128i_u8(vld1q_u8(((uint8_t const *) tmp) + idx));
  6123. }
  6124. // Concatenate 8-byte blocks in a and b into a 16-byte temporary result, shift
  6125. // the result right by imm8 bytes, and store the low 8 bytes in dst.
  6126. //
  6127. // tmp[127:0] := ((a[63:0] << 64)[127:0] OR b[63:0]) >> (imm8*8)
  6128. // dst[63:0] := tmp[63:0]
  6129. //
  6130. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_alignr_pi8
  6131. #define _mm_alignr_pi8(a, b, imm) \
  6132. __extension__({ \
  6133. __m64 ret; \
  6134. if (_sse2neon_unlikely((imm) >= 16)) { \
  6135. ret = vreinterpret_m64_s8(vdup_n_s8(0)); \
  6136. } else { \
  6137. uint8x8_t tmp_low, tmp_high; \
  6138. if (imm >= 8) { \
  6139. const int idx = imm - 8; \
  6140. tmp_low = vreinterpret_u8_m64(a); \
  6141. tmp_high = vdup_n_u8(0); \
  6142. ret = vreinterpret_m64_u8(vext_u8(tmp_low, tmp_high, idx)); \
  6143. } else { \
  6144. const int idx = imm; \
  6145. tmp_low = vreinterpret_u8_m64(b); \
  6146. tmp_high = vreinterpret_u8_m64(a); \
  6147. ret = vreinterpret_m64_u8(vext_u8(tmp_low, tmp_high, idx)); \
  6148. } \
  6149. } \
  6150. ret; \
  6151. })
  6152. // Computes pairwise add of each argument as a 16-bit signed or unsigned integer
  6153. // values a and b.
  6154. FORCE_INLINE __m128i _mm_hadd_epi16(__m128i _a, __m128i _b)
  6155. {
  6156. int16x8_t a = vreinterpretq_s16_m128i(_a);
  6157. int16x8_t b = vreinterpretq_s16_m128i(_b);
  6158. #if defined(__aarch64__)
  6159. return vreinterpretq_m128i_s16(vpaddq_s16(a, b));
  6160. #else
  6161. return vreinterpretq_m128i_s16(
  6162. vcombine_s16(vpadd_s16(vget_low_s16(a), vget_high_s16(a)),
  6163. vpadd_s16(vget_low_s16(b), vget_high_s16(b))));
  6164. #endif
  6165. }
  6166. // Computes pairwise add of each argument as a 32-bit signed or unsigned integer
  6167. // values a and b.
  6168. FORCE_INLINE __m128i _mm_hadd_epi32(__m128i _a, __m128i _b)
  6169. {
  6170. int32x4_t a = vreinterpretq_s32_m128i(_a);
  6171. int32x4_t b = vreinterpretq_s32_m128i(_b);
  6172. return vreinterpretq_m128i_s32(
  6173. vcombine_s32(vpadd_s32(vget_low_s32(a), vget_high_s32(a)),
  6174. vpadd_s32(vget_low_s32(b), vget_high_s32(b))));
  6175. }
  6176. // Horizontally add adjacent pairs of 16-bit integers in a and b, and pack the
  6177. // signed 16-bit results in dst.
  6178. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hadd_pi16
  6179. FORCE_INLINE __m64 _mm_hadd_pi16(__m64 a, __m64 b)
  6180. {
  6181. return vreinterpret_m64_s16(
  6182. vpadd_s16(vreinterpret_s16_m64(a), vreinterpret_s16_m64(b)));
  6183. }
  6184. // Horizontally add adjacent pairs of 32-bit integers in a and b, and pack the
  6185. // signed 32-bit results in dst.
  6186. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hadd_pi32
  6187. FORCE_INLINE __m64 _mm_hadd_pi32(__m64 a, __m64 b)
  6188. {
  6189. return vreinterpret_m64_s32(
  6190. vpadd_s32(vreinterpret_s32_m64(a), vreinterpret_s32_m64(b)));
  6191. }
  6192. // Computes saturated pairwise sub of each argument as a 16-bit signed
  6193. // integer values a and b.
  6194. FORCE_INLINE __m128i _mm_hadds_epi16(__m128i _a, __m128i _b)
  6195. {
  6196. #if defined(__aarch64__)
  6197. int16x8_t a = vreinterpretq_s16_m128i(_a);
  6198. int16x8_t b = vreinterpretq_s16_m128i(_b);
  6199. return vreinterpretq_s64_s16(
  6200. vqaddq_s16(vuzp1q_s16(a, b), vuzp2q_s16(a, b)));
  6201. #else
  6202. int32x4_t a = vreinterpretq_s32_m128i(_a);
  6203. int32x4_t b = vreinterpretq_s32_m128i(_b);
  6204. // Interleave using vshrn/vmovn
  6205. // [a0|a2|a4|a6|b0|b2|b4|b6]
  6206. // [a1|a3|a5|a7|b1|b3|b5|b7]
  6207. int16x8_t ab0246 = vcombine_s16(vmovn_s32(a), vmovn_s32(b));
  6208. int16x8_t ab1357 = vcombine_s16(vshrn_n_s32(a, 16), vshrn_n_s32(b, 16));
  6209. // Saturated add
  6210. return vreinterpretq_m128i_s16(vqaddq_s16(ab0246, ab1357));
  6211. #endif
  6212. }
  6213. // Horizontally add adjacent pairs of signed 16-bit integers in a and b using
  6214. // saturation, and pack the signed 16-bit results in dst.
  6215. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hadds_pi16
  6216. FORCE_INLINE __m64 _mm_hadds_pi16(__m64 _a, __m64 _b)
  6217. {
  6218. int16x4_t a = vreinterpret_s16_m64(_a);
  6219. int16x4_t b = vreinterpret_s16_m64(_b);
  6220. #if defined(__aarch64__)
  6221. return vreinterpret_s64_s16(vqadd_s16(vuzp1_s16(a, b), vuzp2_s16(a, b)));
  6222. #else
  6223. int16x4x2_t res = vuzp_s16(a, b);
  6224. return vreinterpret_s64_s16(vqadd_s16(res.val[0], res.val[1]));
  6225. #endif
  6226. }
  6227. // Computes pairwise difference of each argument as a 16-bit signed or unsigned
  6228. // integer values a and b.
  6229. FORCE_INLINE __m128i _mm_hsub_epi16(__m128i _a, __m128i _b)
  6230. {
  6231. int32x4_t a = vreinterpretq_s32_m128i(_a);
  6232. int32x4_t b = vreinterpretq_s32_m128i(_b);
  6233. // Interleave using vshrn/vmovn
  6234. // [a0|a2|a4|a6|b0|b2|b4|b6]
  6235. // [a1|a3|a5|a7|b1|b3|b5|b7]
  6236. int16x8_t ab0246 = vcombine_s16(vmovn_s32(a), vmovn_s32(b));
  6237. int16x8_t ab1357 = vcombine_s16(vshrn_n_s32(a, 16), vshrn_n_s32(b, 16));
  6238. // Subtract
  6239. return vreinterpretq_m128i_s16(vsubq_s16(ab0246, ab1357));
  6240. }
  6241. // Computes pairwise difference of each argument as a 32-bit signed or unsigned
  6242. // integer values a and b.
  6243. FORCE_INLINE __m128i _mm_hsub_epi32(__m128i _a, __m128i _b)
  6244. {
  6245. int64x2_t a = vreinterpretq_s64_m128i(_a);
  6246. int64x2_t b = vreinterpretq_s64_m128i(_b);
  6247. // Interleave using vshrn/vmovn
  6248. // [a0|a2|b0|b2]
  6249. // [a1|a2|b1|b3]
  6250. int32x4_t ab02 = vcombine_s32(vmovn_s64(a), vmovn_s64(b));
  6251. int32x4_t ab13 = vcombine_s32(vshrn_n_s64(a, 32), vshrn_n_s64(b, 32));
  6252. // Subtract
  6253. return vreinterpretq_m128i_s32(vsubq_s32(ab02, ab13));
  6254. }
  6255. // Horizontally subtract adjacent pairs of 16-bit integers in a and b, and pack
  6256. // the signed 16-bit results in dst.
  6257. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hsub_pi16
  6258. FORCE_INLINE __m64 _mm_hsub_pi16(__m64 _a, __m64 _b)
  6259. {
  6260. int32x4_t ab =
  6261. vcombine_s32(vreinterpret_s32_m64(_a), vreinterpret_s32_m64(_b));
  6262. int16x4_t ab_low_bits = vmovn_s32(ab);
  6263. int16x4_t ab_high_bits = vshrn_n_s32(ab, 16);
  6264. return vreinterpret_m64_s16(vsub_s16(ab_low_bits, ab_high_bits));
  6265. }
  6266. // Horizontally subtract adjacent pairs of 32-bit integers in a and b, and pack
  6267. // the signed 32-bit results in dst.
  6268. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=mm_hsub_pi32
  6269. FORCE_INLINE __m64 _mm_hsub_pi32(__m64 _a, __m64 _b)
  6270. {
  6271. #if defined(__aarch64__)
  6272. int32x2_t a = vreinterpret_s32_m64(_a);
  6273. int32x2_t b = vreinterpret_s32_m64(_b);
  6274. return vreinterpret_m64_s32(vsub_s32(vtrn1_s32(a, b), vtrn2_s32(a, b)));
  6275. #else
  6276. int32x2x2_t trn_ab =
  6277. vtrn_s32(vreinterpret_s32_m64(_a), vreinterpret_s32_m64(_b));
  6278. return vreinterpret_m64_s32(vsub_s32(trn_ab.val[0], trn_ab.val[1]));
  6279. #endif
  6280. }
  6281. // Computes saturated pairwise difference of each argument as a 16-bit signed
  6282. // integer values a and b.
  6283. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hsubs_epi16
  6284. FORCE_INLINE __m128i _mm_hsubs_epi16(__m128i _a, __m128i _b)
  6285. {
  6286. #if defined(__aarch64__)
  6287. int16x8_t a = vreinterpretq_s16_m128i(_a);
  6288. int16x8_t b = vreinterpretq_s16_m128i(_b);
  6289. return vreinterpretq_s64_s16(
  6290. vqsubq_s16(vuzp1q_s16(a, b), vuzp2q_s16(a, b)));
  6291. #else
  6292. int32x4_t a = vreinterpretq_s32_m128i(_a);
  6293. int32x4_t b = vreinterpretq_s32_m128i(_b);
  6294. // Interleave using vshrn/vmovn
  6295. // [a0|a2|a4|a6|b0|b2|b4|b6]
  6296. // [a1|a3|a5|a7|b1|b3|b5|b7]
  6297. int16x8_t ab0246 = vcombine_s16(vmovn_s32(a), vmovn_s32(b));
  6298. int16x8_t ab1357 = vcombine_s16(vshrn_n_s32(a, 16), vshrn_n_s32(b, 16));
  6299. // Saturated subtract
  6300. return vreinterpretq_m128i_s16(vqsubq_s16(ab0246, ab1357));
  6301. #endif
  6302. }
  6303. // Horizontally subtract adjacent pairs of signed 16-bit integers in a and b
  6304. // using saturation, and pack the signed 16-bit results in dst.
  6305. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_hsubs_pi16
  6306. FORCE_INLINE __m64 _mm_hsubs_pi16(__m64 _a, __m64 _b)
  6307. {
  6308. int16x4_t a = vreinterpret_s16_m64(_a);
  6309. int16x4_t b = vreinterpret_s16_m64(_b);
  6310. #if defined(__aarch64__)
  6311. return vreinterpret_s64_s16(vqsub_s16(vuzp1_s16(a, b), vuzp2_s16(a, b)));
  6312. #else
  6313. int16x4x2_t res = vuzp_s16(a, b);
  6314. return vreinterpret_s64_s16(vqsub_s16(res.val[0], res.val[1]));
  6315. #endif
  6316. }
  6317. // Vertically multiply each unsigned 8-bit integer from a with the corresponding
  6318. // signed 8-bit integer from b, producing intermediate signed 16-bit integers.
  6319. // Horizontally add adjacent pairs of intermediate signed 16-bit integers,
  6320. // and pack the saturated results in dst.
  6321. //
  6322. // FOR j := 0 to 7
  6323. // i := j*16
  6324. // dst[i+15:i] := Saturate_To_Int16( a[i+15:i+8]*b[i+15:i+8] +
  6325. // a[i+7:i]*b[i+7:i] )
  6326. // ENDFOR
  6327. FORCE_INLINE __m128i _mm_maddubs_epi16(__m128i _a, __m128i _b)
  6328. {
  6329. #if defined(__aarch64__)
  6330. uint8x16_t a = vreinterpretq_u8_m128i(_a);
  6331. int8x16_t b = vreinterpretq_s8_m128i(_b);
  6332. int16x8_t tl = vmulq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(a))),
  6333. vmovl_s8(vget_low_s8(b)));
  6334. int16x8_t th = vmulq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(a))),
  6335. vmovl_s8(vget_high_s8(b)));
  6336. return vreinterpretq_m128i_s16(
  6337. vqaddq_s16(vuzp1q_s16(tl, th), vuzp2q_s16(tl, th)));
  6338. #else
  6339. // This would be much simpler if x86 would choose to zero extend OR sign
  6340. // extend, not both. This could probably be optimized better.
  6341. uint16x8_t a = vreinterpretq_u16_m128i(_a);
  6342. int16x8_t b = vreinterpretq_s16_m128i(_b);
  6343. // Zero extend a
  6344. int16x8_t a_odd = vreinterpretq_s16_u16(vshrq_n_u16(a, 8));
  6345. int16x8_t a_even = vreinterpretq_s16_u16(vbicq_u16(a, vdupq_n_u16(0xff00)));
  6346. // Sign extend by shifting left then shifting right.
  6347. int16x8_t b_even = vshrq_n_s16(vshlq_n_s16(b, 8), 8);
  6348. int16x8_t b_odd = vshrq_n_s16(b, 8);
  6349. // multiply
  6350. int16x8_t prod1 = vmulq_s16(a_even, b_even);
  6351. int16x8_t prod2 = vmulq_s16(a_odd, b_odd);
  6352. // saturated add
  6353. return vreinterpretq_m128i_s16(vqaddq_s16(prod1, prod2));
  6354. #endif
  6355. }
  6356. // Vertically multiply each unsigned 8-bit integer from a with the corresponding
  6357. // signed 8-bit integer from b, producing intermediate signed 16-bit integers.
  6358. // Horizontally add adjacent pairs of intermediate signed 16-bit integers, and
  6359. // pack the saturated results in dst.
  6360. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_maddubs_pi16
  6361. FORCE_INLINE __m64 _mm_maddubs_pi16(__m64 _a, __m64 _b)
  6362. {
  6363. uint16x4_t a = vreinterpret_u16_m64(_a);
  6364. int16x4_t b = vreinterpret_s16_m64(_b);
  6365. // Zero extend a
  6366. int16x4_t a_odd = vreinterpret_s16_u16(vshr_n_u16(a, 8));
  6367. int16x4_t a_even = vreinterpret_s16_u16(vand_u16(a, vdup_n_u16(0xff)));
  6368. // Sign extend by shifting left then shifting right.
  6369. int16x4_t b_even = vshr_n_s16(vshl_n_s16(b, 8), 8);
  6370. int16x4_t b_odd = vshr_n_s16(b, 8);
  6371. // multiply
  6372. int16x4_t prod1 = vmul_s16(a_even, b_even);
  6373. int16x4_t prod2 = vmul_s16(a_odd, b_odd);
  6374. // saturated add
  6375. return vreinterpret_m64_s16(vqadd_s16(prod1, prod2));
  6376. }
  6377. // Multiply packed signed 16-bit integers in a and b, producing intermediate
  6378. // signed 32-bit integers. Shift right by 15 bits while rounding up, and store
  6379. // the packed 16-bit integers in dst.
  6380. //
  6381. // r0 := Round(((int32_t)a0 * (int32_t)b0) >> 15)
  6382. // r1 := Round(((int32_t)a1 * (int32_t)b1) >> 15)
  6383. // r2 := Round(((int32_t)a2 * (int32_t)b2) >> 15)
  6384. // ...
  6385. // r7 := Round(((int32_t)a7 * (int32_t)b7) >> 15)
  6386. FORCE_INLINE __m128i _mm_mulhrs_epi16(__m128i a, __m128i b)
  6387. {
  6388. // Has issues due to saturation
  6389. // return vreinterpretq_m128i_s16(vqrdmulhq_s16(a, b));
  6390. // Multiply
  6391. int32x4_t mul_lo = vmull_s16(vget_low_s16(vreinterpretq_s16_m128i(a)),
  6392. vget_low_s16(vreinterpretq_s16_m128i(b)));
  6393. int32x4_t mul_hi = vmull_s16(vget_high_s16(vreinterpretq_s16_m128i(a)),
  6394. vget_high_s16(vreinterpretq_s16_m128i(b)));
  6395. // Rounding narrowing shift right
  6396. // narrow = (int16_t)((mul + 16384) >> 15);
  6397. int16x4_t narrow_lo = vrshrn_n_s32(mul_lo, 15);
  6398. int16x4_t narrow_hi = vrshrn_n_s32(mul_hi, 15);
  6399. // Join together
  6400. return vreinterpretq_m128i_s16(vcombine_s16(narrow_lo, narrow_hi));
  6401. }
  6402. // Multiply packed signed 16-bit integers in a and b, producing intermediate
  6403. // signed 32-bit integers. Truncate each intermediate integer to the 18 most
  6404. // significant bits, round by adding 1, and store bits [16:1] to dst.
  6405. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_mulhrs_pi16
  6406. FORCE_INLINE __m64 _mm_mulhrs_pi16(__m64 a, __m64 b)
  6407. {
  6408. int32x4_t mul_extend =
  6409. vmull_s16((vreinterpret_s16_m64(a)), (vreinterpret_s16_m64(b)));
  6410. // Rounding narrowing shift right
  6411. return vreinterpret_m64_s16(vrshrn_n_s32(mul_extend, 15));
  6412. }
  6413. // Shuffle packed 8-bit integers in a according to shuffle control mask in the
  6414. // corresponding 8-bit element of b, and store the results in dst.
  6415. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_shuffle_epi8
  6416. FORCE_INLINE __m128i _mm_shuffle_epi8(__m128i a, __m128i b)
  6417. {
  6418. int8x16_t tbl = vreinterpretq_s8_m128i(a); // input a
  6419. uint8x16_t idx = vreinterpretq_u8_m128i(b); // input b
  6420. uint8x16_t idx_masked =
  6421. vandq_u8(idx, vdupq_n_u8(0x8F)); // avoid using meaningless bits
  6422. #if defined(__aarch64__)
  6423. return vreinterpretq_m128i_s8(vqtbl1q_s8(tbl, idx_masked));
  6424. #elif defined(__GNUC__)
  6425. int8x16_t ret;
  6426. // %e and %f represent the even and odd D registers
  6427. // respectively.
  6428. __asm__ __volatile__(
  6429. "vtbl.8 %e[ret], {%e[tbl], %f[tbl]}, %e[idx]\n"
  6430. "vtbl.8 %f[ret], {%e[tbl], %f[tbl]}, %f[idx]\n"
  6431. : [ret] "=&w"(ret)
  6432. : [tbl] "w"(tbl), [idx] "w"(idx_masked));
  6433. return vreinterpretq_m128i_s8(ret);
  6434. #else
  6435. // use this line if testing on aarch64
  6436. int8x8x2_t a_split = {vget_low_s8(tbl), vget_high_s8(tbl)};
  6437. return vreinterpretq_m128i_s8(
  6438. vcombine_s8(vtbl2_s8(a_split, vget_low_u8(idx_masked)),
  6439. vtbl2_s8(a_split, vget_high_u8(idx_masked))));
  6440. #endif
  6441. }
  6442. // Shuffle packed 8-bit integers in a according to shuffle control mask in the
  6443. // corresponding 8-bit element of b, and store the results in dst.
  6444. //
  6445. // FOR j := 0 to 7
  6446. // i := j*8
  6447. // IF b[i+7] == 1
  6448. // dst[i+7:i] := 0
  6449. // ELSE
  6450. // index[2:0] := b[i+2:i]
  6451. // dst[i+7:i] := a[index*8+7:index*8]
  6452. // FI
  6453. // ENDFOR
  6454. //
  6455. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_shuffle_pi8
  6456. FORCE_INLINE __m64 _mm_shuffle_pi8(__m64 a, __m64 b)
  6457. {
  6458. const int8x8_t controlMask =
  6459. vand_s8(vreinterpret_s8_m64(b), vdup_n_s8((int8_t)(0x1 << 7 | 0x07)));
  6460. int8x8_t res = vtbl1_s8(vreinterpret_s8_m64(a), controlMask);
  6461. return vreinterpret_m64_s8(res);
  6462. }
  6463. // Negate packed 16-bit integers in a when the corresponding signed
  6464. // 16-bit integer in b is negative, and store the results in dst.
  6465. // Element in dst are zeroed out when the corresponding element
  6466. // in b is zero.
  6467. //
  6468. // for i in 0..7
  6469. // if b[i] < 0
  6470. // r[i] := -a[i]
  6471. // else if b[i] == 0
  6472. // r[i] := 0
  6473. // else
  6474. // r[i] := a[i]
  6475. // fi
  6476. // done
  6477. FORCE_INLINE __m128i _mm_sign_epi16(__m128i _a, __m128i _b)
  6478. {
  6479. int16x8_t a = vreinterpretq_s16_m128i(_a);
  6480. int16x8_t b = vreinterpretq_s16_m128i(_b);
  6481. // signed shift right: faster than vclt
  6482. // (b < 0) ? 0xFFFF : 0
  6483. uint16x8_t ltMask = vreinterpretq_u16_s16(vshrq_n_s16(b, 15));
  6484. // (b == 0) ? 0xFFFF : 0
  6485. #if defined(__aarch64__)
  6486. int16x8_t zeroMask = vreinterpretq_s16_u16(vceqzq_s16(b));
  6487. #else
  6488. int16x8_t zeroMask = vreinterpretq_s16_u16(vceqq_s16(b, vdupq_n_s16(0)));
  6489. #endif
  6490. // bitwise select either a or negative 'a' (vnegq_s16(a) equals to negative
  6491. // 'a') based on ltMask
  6492. int16x8_t masked = vbslq_s16(ltMask, vnegq_s16(a), a);
  6493. // res = masked & (~zeroMask)
  6494. int16x8_t res = vbicq_s16(masked, zeroMask);
  6495. return vreinterpretq_m128i_s16(res);
  6496. }
  6497. // Negate packed 32-bit integers in a when the corresponding signed
  6498. // 32-bit integer in b is negative, and store the results in dst.
  6499. // Element in dst are zeroed out when the corresponding element
  6500. // in b is zero.
  6501. //
  6502. // for i in 0..3
  6503. // if b[i] < 0
  6504. // r[i] := -a[i]
  6505. // else if b[i] == 0
  6506. // r[i] := 0
  6507. // else
  6508. // r[i] := a[i]
  6509. // fi
  6510. // done
  6511. FORCE_INLINE __m128i _mm_sign_epi32(__m128i _a, __m128i _b)
  6512. {
  6513. int32x4_t a = vreinterpretq_s32_m128i(_a);
  6514. int32x4_t b = vreinterpretq_s32_m128i(_b);
  6515. // signed shift right: faster than vclt
  6516. // (b < 0) ? 0xFFFFFFFF : 0
  6517. uint32x4_t ltMask = vreinterpretq_u32_s32(vshrq_n_s32(b, 31));
  6518. // (b == 0) ? 0xFFFFFFFF : 0
  6519. #if defined(__aarch64__)
  6520. int32x4_t zeroMask = vreinterpretq_s32_u32(vceqzq_s32(b));
  6521. #else
  6522. int32x4_t zeroMask = vreinterpretq_s32_u32(vceqq_s32(b, vdupq_n_s32(0)));
  6523. #endif
  6524. // bitwise select either a or negative 'a' (vnegq_s32(a) equals to negative
  6525. // 'a') based on ltMask
  6526. int32x4_t masked = vbslq_s32(ltMask, vnegq_s32(a), a);
  6527. // res = masked & (~zeroMask)
  6528. int32x4_t res = vbicq_s32(masked, zeroMask);
  6529. return vreinterpretq_m128i_s32(res);
  6530. }
  6531. // Negate packed 8-bit integers in a when the corresponding signed
  6532. // 8-bit integer in b is negative, and store the results in dst.
  6533. // Element in dst are zeroed out when the corresponding element
  6534. // in b is zero.
  6535. //
  6536. // for i in 0..15
  6537. // if b[i] < 0
  6538. // r[i] := -a[i]
  6539. // else if b[i] == 0
  6540. // r[i] := 0
  6541. // else
  6542. // r[i] := a[i]
  6543. // fi
  6544. // done
  6545. FORCE_INLINE __m128i _mm_sign_epi8(__m128i _a, __m128i _b)
  6546. {
  6547. int8x16_t a = vreinterpretq_s8_m128i(_a);
  6548. int8x16_t b = vreinterpretq_s8_m128i(_b);
  6549. // signed shift right: faster than vclt
  6550. // (b < 0) ? 0xFF : 0
  6551. uint8x16_t ltMask = vreinterpretq_u8_s8(vshrq_n_s8(b, 7));
  6552. // (b == 0) ? 0xFF : 0
  6553. #if defined(__aarch64__)
  6554. int8x16_t zeroMask = vreinterpretq_s8_u8(vceqzq_s8(b));
  6555. #else
  6556. int8x16_t zeroMask = vreinterpretq_s8_u8(vceqq_s8(b, vdupq_n_s8(0)));
  6557. #endif
  6558. // bitwise select either a or nagative 'a' (vnegq_s8(a) return nagative 'a')
  6559. // based on ltMask
  6560. int8x16_t masked = vbslq_s8(ltMask, vnegq_s8(a), a);
  6561. // res = masked & (~zeroMask)
  6562. int8x16_t res = vbicq_s8(masked, zeroMask);
  6563. return vreinterpretq_m128i_s8(res);
  6564. }
  6565. // Negate packed 16-bit integers in a when the corresponding signed 16-bit
  6566. // integer in b is negative, and store the results in dst. Element in dst are
  6567. // zeroed out when the corresponding element in b is zero.
  6568. //
  6569. // FOR j := 0 to 3
  6570. // i := j*16
  6571. // IF b[i+15:i] < 0
  6572. // dst[i+15:i] := -(a[i+15:i])
  6573. // ELSE IF b[i+15:i] == 0
  6574. // dst[i+15:i] := 0
  6575. // ELSE
  6576. // dst[i+15:i] := a[i+15:i]
  6577. // FI
  6578. // ENDFOR
  6579. //
  6580. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sign_pi16
  6581. FORCE_INLINE __m64 _mm_sign_pi16(__m64 _a, __m64 _b)
  6582. {
  6583. int16x4_t a = vreinterpret_s16_m64(_a);
  6584. int16x4_t b = vreinterpret_s16_m64(_b);
  6585. // signed shift right: faster than vclt
  6586. // (b < 0) ? 0xFFFF : 0
  6587. uint16x4_t ltMask = vreinterpret_u16_s16(vshr_n_s16(b, 15));
  6588. // (b == 0) ? 0xFFFF : 0
  6589. #if defined(__aarch64__)
  6590. int16x4_t zeroMask = vreinterpret_s16_u16(vceqz_s16(b));
  6591. #else
  6592. int16x4_t zeroMask = vreinterpret_s16_u16(vceq_s16(b, vdup_n_s16(0)));
  6593. #endif
  6594. // bitwise select either a or nagative 'a' (vneg_s16(a) return nagative 'a')
  6595. // based on ltMask
  6596. int16x4_t masked = vbsl_s16(ltMask, vneg_s16(a), a);
  6597. // res = masked & (~zeroMask)
  6598. int16x4_t res = vbic_s16(masked, zeroMask);
  6599. return vreinterpret_m64_s16(res);
  6600. }
  6601. // Negate packed 32-bit integers in a when the corresponding signed 32-bit
  6602. // integer in b is negative, and store the results in dst. Element in dst are
  6603. // zeroed out when the corresponding element in b is zero.
  6604. //
  6605. // FOR j := 0 to 1
  6606. // i := j*32
  6607. // IF b[i+31:i] < 0
  6608. // dst[i+31:i] := -(a[i+31:i])
  6609. // ELSE IF b[i+31:i] == 0
  6610. // dst[i+31:i] := 0
  6611. // ELSE
  6612. // dst[i+31:i] := a[i+31:i]
  6613. // FI
  6614. // ENDFOR
  6615. //
  6616. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sign_pi32
  6617. FORCE_INLINE __m64 _mm_sign_pi32(__m64 _a, __m64 _b)
  6618. {
  6619. int32x2_t a = vreinterpret_s32_m64(_a);
  6620. int32x2_t b = vreinterpret_s32_m64(_b);
  6621. // signed shift right: faster than vclt
  6622. // (b < 0) ? 0xFFFFFFFF : 0
  6623. uint32x2_t ltMask = vreinterpret_u32_s32(vshr_n_s32(b, 31));
  6624. // (b == 0) ? 0xFFFFFFFF : 0
  6625. #if defined(__aarch64__)
  6626. int32x2_t zeroMask = vreinterpret_s32_u32(vceqz_s32(b));
  6627. #else
  6628. int32x2_t zeroMask = vreinterpret_s32_u32(vceq_s32(b, vdup_n_s32(0)));
  6629. #endif
  6630. // bitwise select either a or nagative 'a' (vneg_s32(a) return nagative 'a')
  6631. // based on ltMask
  6632. int32x2_t masked = vbsl_s32(ltMask, vneg_s32(a), a);
  6633. // res = masked & (~zeroMask)
  6634. int32x2_t res = vbic_s32(masked, zeroMask);
  6635. return vreinterpret_m64_s32(res);
  6636. }
  6637. // Negate packed 8-bit integers in a when the corresponding signed 8-bit integer
  6638. // in b is negative, and store the results in dst. Element in dst are zeroed out
  6639. // when the corresponding element in b is zero.
  6640. //
  6641. // FOR j := 0 to 7
  6642. // i := j*8
  6643. // IF b[i+7:i] < 0
  6644. // dst[i+7:i] := -(a[i+7:i])
  6645. // ELSE IF b[i+7:i] == 0
  6646. // dst[i+7:i] := 0
  6647. // ELSE
  6648. // dst[i+7:i] := a[i+7:i]
  6649. // FI
  6650. // ENDFOR
  6651. //
  6652. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_sign_pi8
  6653. FORCE_INLINE __m64 _mm_sign_pi8(__m64 _a, __m64 _b)
  6654. {
  6655. int8x8_t a = vreinterpret_s8_m64(_a);
  6656. int8x8_t b = vreinterpret_s8_m64(_b);
  6657. // signed shift right: faster than vclt
  6658. // (b < 0) ? 0xFF : 0
  6659. uint8x8_t ltMask = vreinterpret_u8_s8(vshr_n_s8(b, 7));
  6660. // (b == 0) ? 0xFF : 0
  6661. #if defined(__aarch64__)
  6662. int8x8_t zeroMask = vreinterpret_s8_u8(vceqz_s8(b));
  6663. #else
  6664. int8x8_t zeroMask = vreinterpret_s8_u8(vceq_s8(b, vdup_n_s8(0)));
  6665. #endif
  6666. // bitwise select either a or nagative 'a' (vneg_s8(a) return nagative 'a')
  6667. // based on ltMask
  6668. int8x8_t masked = vbsl_s8(ltMask, vneg_s8(a), a);
  6669. // res = masked & (~zeroMask)
  6670. int8x8_t res = vbic_s8(masked, zeroMask);
  6671. return vreinterpret_m64_s8(res);
  6672. }
  6673. /* SSE4.1 */
  6674. // Blend packed 16-bit integers from a and b using control mask imm8, and store
  6675. // the results in dst.
  6676. //
  6677. // FOR j := 0 to 7
  6678. // i := j*16
  6679. // IF imm8[j]
  6680. // dst[i+15:i] := b[i+15:i]
  6681. // ELSE
  6682. // dst[i+15:i] := a[i+15:i]
  6683. // FI
  6684. // ENDFOR
  6685. // FORCE_INLINE __m128i _mm_blend_epi16(__m128i a, __m128i b,
  6686. // __constrange(0,255) int imm)
  6687. #define _mm_blend_epi16(a, b, imm) \
  6688. __extension__({ \
  6689. const uint16_t ones = 0xffff; \
  6690. const uint16_t zeros = 0x0000; \
  6691. const uint16_t _mask[8] = {((imm) & (1 << 0)) ? ones : zeros, \
  6692. ((imm) & (1 << 1)) ? ones : zeros, \
  6693. ((imm) & (1 << 2)) ? ones : zeros, \
  6694. ((imm) & (1 << 3)) ? ones : zeros, \
  6695. ((imm) & (1 << 4)) ? ones : zeros, \
  6696. ((imm) & (1 << 5)) ? ones : zeros, \
  6697. ((imm) & (1 << 6)) ? ones : zeros, \
  6698. ((imm) & (1 << 7)) ? ones : zeros}; \
  6699. uint16x8_t _mask_vec = vld1q_u16(_mask); \
  6700. uint16x8_t _a = vreinterpretq_u16_m128i(a); \
  6701. uint16x8_t _b = vreinterpretq_u16_m128i(b); \
  6702. vreinterpretq_m128i_u16(vbslq_u16(_mask_vec, _b, _a)); \
  6703. })
  6704. // Blend packed double-precision (64-bit) floating-point elements from a and b
  6705. // using control mask imm8, and store the results in dst.
  6706. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_blend_pd
  6707. #define _mm_blend_pd(a, b, imm) \
  6708. __extension__({ \
  6709. const uint64_t _mask[2] = { \
  6710. ((imm) & (1 << 0)) ? ~UINT64_C(0) : UINT64_C(0), \
  6711. ((imm) & (1 << 1)) ? ~UINT64_C(0) : UINT64_C(0)}; \
  6712. uint64x2_t _mask_vec = vld1q_u64(_mask); \
  6713. uint64x2_t _a = vreinterpretq_u64_m128d(a); \
  6714. uint64x2_t _b = vreinterpretq_u64_m128d(b); \
  6715. vreinterpretq_m128d_u64(vbslq_u64(_mask_vec, _b, _a)); \
  6716. })
  6717. // Blend packed single-precision (32-bit) floating-point elements from a and b
  6718. // using mask, and store the results in dst.
  6719. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_blend_ps
  6720. FORCE_INLINE __m128 _mm_blend_ps(__m128 _a, __m128 _b, const char imm8)
  6721. {
  6722. const uint32_t ALIGN_STRUCT(16)
  6723. data[4] = {((imm8) & (1 << 0)) ? UINT32_MAX : 0,
  6724. ((imm8) & (1 << 1)) ? UINT32_MAX : 0,
  6725. ((imm8) & (1 << 2)) ? UINT32_MAX : 0,
  6726. ((imm8) & (1 << 3)) ? UINT32_MAX : 0};
  6727. uint32x4_t mask = vld1q_u32(data);
  6728. float32x4_t a = vreinterpretq_f32_m128(_a);
  6729. float32x4_t b = vreinterpretq_f32_m128(_b);
  6730. return vreinterpretq_m128_f32(vbslq_f32(mask, b, a));
  6731. }
  6732. // Blend packed 8-bit integers from a and b using mask, and store the results in
  6733. // dst.
  6734. //
  6735. // FOR j := 0 to 15
  6736. // i := j*8
  6737. // IF mask[i+7]
  6738. // dst[i+7:i] := b[i+7:i]
  6739. // ELSE
  6740. // dst[i+7:i] := a[i+7:i]
  6741. // FI
  6742. // ENDFOR
  6743. FORCE_INLINE __m128i _mm_blendv_epi8(__m128i _a, __m128i _b, __m128i _mask)
  6744. {
  6745. // Use a signed shift right to create a mask with the sign bit
  6746. uint8x16_t mask =
  6747. vreinterpretq_u8_s8(vshrq_n_s8(vreinterpretq_s8_m128i(_mask), 7));
  6748. uint8x16_t a = vreinterpretq_u8_m128i(_a);
  6749. uint8x16_t b = vreinterpretq_u8_m128i(_b);
  6750. return vreinterpretq_m128i_u8(vbslq_u8(mask, b, a));
  6751. }
  6752. // Blend packed double-precision (64-bit) floating-point elements from a and b
  6753. // using mask, and store the results in dst.
  6754. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_blendv_pd
  6755. FORCE_INLINE __m128d _mm_blendv_pd(__m128d _a, __m128d _b, __m128d _mask)
  6756. {
  6757. uint64x2_t mask =
  6758. vreinterpretq_u64_s64(vshrq_n_s64(vreinterpretq_s64_m128d(_mask), 63));
  6759. #if defined(__aarch64__)
  6760. float64x2_t a = vreinterpretq_f64_m128d(_a);
  6761. float64x2_t b = vreinterpretq_f64_m128d(_b);
  6762. return vreinterpretq_m128d_f64(vbslq_f64(mask, b, a));
  6763. #else
  6764. uint64x2_t a = vreinterpretq_u64_m128d(_a);
  6765. uint64x2_t b = vreinterpretq_u64_m128d(_b);
  6766. return vreinterpretq_m128d_u64(vbslq_u64(mask, b, a));
  6767. #endif
  6768. }
  6769. // Blend packed single-precision (32-bit) floating-point elements from a and b
  6770. // using mask, and store the results in dst.
  6771. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_blendv_ps
  6772. FORCE_INLINE __m128 _mm_blendv_ps(__m128 _a, __m128 _b, __m128 _mask)
  6773. {
  6774. // Use a signed shift right to create a mask with the sign bit
  6775. uint32x4_t mask =
  6776. vreinterpretq_u32_s32(vshrq_n_s32(vreinterpretq_s32_m128(_mask), 31));
  6777. float32x4_t a = vreinterpretq_f32_m128(_a);
  6778. float32x4_t b = vreinterpretq_f32_m128(_b);
  6779. return vreinterpretq_m128_f32(vbslq_f32(mask, b, a));
  6780. }
  6781. // Round the packed double-precision (64-bit) floating-point elements in a up
  6782. // to an integer value, and store the results as packed double-precision
  6783. // floating-point elements in dst.
  6784. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_ceil_pd
  6785. FORCE_INLINE __m128d _mm_ceil_pd(__m128d a)
  6786. {
  6787. #if defined(__aarch64__)
  6788. return vreinterpretq_m128d_f64(vrndpq_f64(vreinterpretq_f64_m128d(a)));
  6789. #else
  6790. double *f = (double *) &a;
  6791. return _mm_set_pd(ceil(f[1]), ceil(f[0]));
  6792. #endif
  6793. }
  6794. // Round the packed single-precision (32-bit) floating-point elements in a up to
  6795. // an integer value, and store the results as packed single-precision
  6796. // floating-point elements in dst.
  6797. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_ceil_ps
  6798. FORCE_INLINE __m128 _mm_ceil_ps(__m128 a)
  6799. {
  6800. #if defined(__aarch64__)
  6801. return vreinterpretq_m128_f32(vrndpq_f32(vreinterpretq_f32_m128(a)));
  6802. #else
  6803. float *f = (float *) &a;
  6804. return _mm_set_ps(ceilf(f[3]), ceilf(f[2]), ceilf(f[1]), ceilf(f[0]));
  6805. #endif
  6806. }
  6807. // Round the lower double-precision (64-bit) floating-point element in b up to
  6808. // an integer value, store the result as a double-precision floating-point
  6809. // element in the lower element of dst, and copy the upper element from a to the
  6810. // upper element of dst.
  6811. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_ceil_sd
  6812. FORCE_INLINE __m128d _mm_ceil_sd(__m128d a, __m128d b)
  6813. {
  6814. return _mm_move_sd(a, _mm_ceil_pd(b));
  6815. }
  6816. // Round the lower single-precision (32-bit) floating-point element in b up to
  6817. // an integer value, store the result as a single-precision floating-point
  6818. // element in the lower element of dst, and copy the upper 3 packed elements
  6819. // from a to the upper elements of dst.
  6820. //
  6821. // dst[31:0] := CEIL(b[31:0])
  6822. // dst[127:32] := a[127:32]
  6823. //
  6824. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_ceil_ss
  6825. FORCE_INLINE __m128 _mm_ceil_ss(__m128 a, __m128 b)
  6826. {
  6827. return _mm_move_ss(a, _mm_ceil_ps(b));
  6828. }
  6829. // Compare packed 64-bit integers in a and b for equality, and store the results
  6830. // in dst
  6831. FORCE_INLINE __m128i _mm_cmpeq_epi64(__m128i a, __m128i b)
  6832. {
  6833. #if defined(__aarch64__)
  6834. return vreinterpretq_m128i_u64(
  6835. vceqq_u64(vreinterpretq_u64_m128i(a), vreinterpretq_u64_m128i(b)));
  6836. #else
  6837. // ARMv7 lacks vceqq_u64
  6838. // (a == b) -> (a_lo == b_lo) && (a_hi == b_hi)
  6839. uint32x4_t cmp =
  6840. vceqq_u32(vreinterpretq_u32_m128i(a), vreinterpretq_u32_m128i(b));
  6841. uint32x4_t swapped = vrev64q_u32(cmp);
  6842. return vreinterpretq_m128i_u32(vandq_u32(cmp, swapped));
  6843. #endif
  6844. }
  6845. // Converts the four signed 16-bit integers in the lower 64 bits to four signed
  6846. // 32-bit integers.
  6847. FORCE_INLINE __m128i _mm_cvtepi16_epi32(__m128i a)
  6848. {
  6849. return vreinterpretq_m128i_s32(
  6850. vmovl_s16(vget_low_s16(vreinterpretq_s16_m128i(a))));
  6851. }
  6852. // Converts the two signed 16-bit integers in the lower 32 bits two signed
  6853. // 32-bit integers.
  6854. FORCE_INLINE __m128i _mm_cvtepi16_epi64(__m128i a)
  6855. {
  6856. int16x8_t s16x8 = vreinterpretq_s16_m128i(a); /* xxxx xxxx xxxx 0B0A */
  6857. int32x4_t s32x4 = vmovl_s16(vget_low_s16(s16x8)); /* 000x 000x 000B 000A */
  6858. int64x2_t s64x2 = vmovl_s32(vget_low_s32(s32x4)); /* 0000 000B 0000 000A */
  6859. return vreinterpretq_m128i_s64(s64x2);
  6860. }
  6861. // Converts the two signed 32-bit integers in the lower 64 bits to two signed
  6862. // 64-bit integers.
  6863. FORCE_INLINE __m128i _mm_cvtepi32_epi64(__m128i a)
  6864. {
  6865. return vreinterpretq_m128i_s64(
  6866. vmovl_s32(vget_low_s32(vreinterpretq_s32_m128i(a))));
  6867. }
  6868. // Converts the four unsigned 8-bit integers in the lower 16 bits to four
  6869. // unsigned 32-bit integers.
  6870. FORCE_INLINE __m128i _mm_cvtepi8_epi16(__m128i a)
  6871. {
  6872. int8x16_t s8x16 = vreinterpretq_s8_m128i(a); /* xxxx xxxx xxxx DCBA */
  6873. int16x8_t s16x8 = vmovl_s8(vget_low_s8(s8x16)); /* 0x0x 0x0x 0D0C 0B0A */
  6874. return vreinterpretq_m128i_s16(s16x8);
  6875. }
  6876. // Converts the four unsigned 8-bit integers in the lower 32 bits to four
  6877. // unsigned 32-bit integers.
  6878. FORCE_INLINE __m128i _mm_cvtepi8_epi32(__m128i a)
  6879. {
  6880. int8x16_t s8x16 = vreinterpretq_s8_m128i(a); /* xxxx xxxx xxxx DCBA */
  6881. int16x8_t s16x8 = vmovl_s8(vget_low_s8(s8x16)); /* 0x0x 0x0x 0D0C 0B0A */
  6882. int32x4_t s32x4 = vmovl_s16(vget_low_s16(s16x8)); /* 000D 000C 000B 000A */
  6883. return vreinterpretq_m128i_s32(s32x4);
  6884. }
  6885. // Converts the two signed 8-bit integers in the lower 32 bits to four
  6886. // signed 64-bit integers.
  6887. FORCE_INLINE __m128i _mm_cvtepi8_epi64(__m128i a)
  6888. {
  6889. int8x16_t s8x16 = vreinterpretq_s8_m128i(a); /* xxxx xxxx xxxx xxBA */
  6890. int16x8_t s16x8 = vmovl_s8(vget_low_s8(s8x16)); /* 0x0x 0x0x 0x0x 0B0A */
  6891. int32x4_t s32x4 = vmovl_s16(vget_low_s16(s16x8)); /* 000x 000x 000B 000A */
  6892. int64x2_t s64x2 = vmovl_s32(vget_low_s32(s32x4)); /* 0000 000B 0000 000A */
  6893. return vreinterpretq_m128i_s64(s64x2);
  6894. }
  6895. // Converts the four unsigned 16-bit integers in the lower 64 bits to four
  6896. // unsigned 32-bit integers.
  6897. FORCE_INLINE __m128i _mm_cvtepu16_epi32(__m128i a)
  6898. {
  6899. return vreinterpretq_m128i_u32(
  6900. vmovl_u16(vget_low_u16(vreinterpretq_u16_m128i(a))));
  6901. }
  6902. // Converts the two unsigned 16-bit integers in the lower 32 bits to two
  6903. // unsigned 64-bit integers.
  6904. FORCE_INLINE __m128i _mm_cvtepu16_epi64(__m128i a)
  6905. {
  6906. uint16x8_t u16x8 = vreinterpretq_u16_m128i(a); /* xxxx xxxx xxxx 0B0A */
  6907. uint32x4_t u32x4 = vmovl_u16(vget_low_u16(u16x8)); /* 000x 000x 000B 000A */
  6908. uint64x2_t u64x2 = vmovl_u32(vget_low_u32(u32x4)); /* 0000 000B 0000 000A */
  6909. return vreinterpretq_m128i_u64(u64x2);
  6910. }
  6911. // Converts the two unsigned 32-bit integers in the lower 64 bits to two
  6912. // unsigned 64-bit integers.
  6913. FORCE_INLINE __m128i _mm_cvtepu32_epi64(__m128i a)
  6914. {
  6915. return vreinterpretq_m128i_u64(
  6916. vmovl_u32(vget_low_u32(vreinterpretq_u32_m128i(a))));
  6917. }
  6918. // Zero extend packed unsigned 8-bit integers in a to packed 16-bit integers,
  6919. // and store the results in dst.
  6920. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_cvtepu8_epi16
  6921. FORCE_INLINE __m128i _mm_cvtepu8_epi16(__m128i a)
  6922. {
  6923. uint8x16_t u8x16 = vreinterpretq_u8_m128i(a); /* xxxx xxxx HGFE DCBA */
  6924. uint16x8_t u16x8 = vmovl_u8(vget_low_u8(u8x16)); /* 0H0G 0F0E 0D0C 0B0A */
  6925. return vreinterpretq_m128i_u16(u16x8);
  6926. }
  6927. // Converts the four unsigned 8-bit integers in the lower 32 bits to four
  6928. // unsigned 32-bit integers.
  6929. // https://msdn.microsoft.com/en-us/library/bb531467%28v=vs.100%29.aspx
  6930. FORCE_INLINE __m128i _mm_cvtepu8_epi32(__m128i a)
  6931. {
  6932. uint8x16_t u8x16 = vreinterpretq_u8_m128i(a); /* xxxx xxxx xxxx DCBA */
  6933. uint16x8_t u16x8 = vmovl_u8(vget_low_u8(u8x16)); /* 0x0x 0x0x 0D0C 0B0A */
  6934. uint32x4_t u32x4 = vmovl_u16(vget_low_u16(u16x8)); /* 000D 000C 000B 000A */
  6935. return vreinterpretq_m128i_u32(u32x4);
  6936. }
  6937. // Converts the two unsigned 8-bit integers in the lower 16 bits to two
  6938. // unsigned 64-bit integers.
  6939. FORCE_INLINE __m128i _mm_cvtepu8_epi64(__m128i a)
  6940. {
  6941. uint8x16_t u8x16 = vreinterpretq_u8_m128i(a); /* xxxx xxxx xxxx xxBA */
  6942. uint16x8_t u16x8 = vmovl_u8(vget_low_u8(u8x16)); /* 0x0x 0x0x 0x0x 0B0A */
  6943. uint32x4_t u32x4 = vmovl_u16(vget_low_u16(u16x8)); /* 000x 000x 000B 000A */
  6944. uint64x2_t u64x2 = vmovl_u32(vget_low_u32(u32x4)); /* 0000 000B 0000 000A */
  6945. return vreinterpretq_m128i_u64(u64x2);
  6946. }
  6947. // Conditionally multiply the packed double-precision (64-bit) floating-point
  6948. // elements in a and b using the high 4 bits in imm8, sum the four products, and
  6949. // conditionally store the sum in dst using the low 4 bits of imm8.
  6950. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_dp_pd
  6951. FORCE_INLINE __m128d _mm_dp_pd(__m128d a, __m128d b, const int imm)
  6952. {
  6953. // Generate mask value from constant immediate bit value
  6954. const int64_t bit0Mask = imm & 0x01 ? UINT64_MAX : 0;
  6955. const int64_t bit1Mask = imm & 0x02 ? UINT64_MAX : 0;
  6956. #if !SSE2NEON_PRECISE_DP
  6957. const int64_t bit4Mask = imm & 0x10 ? UINT64_MAX : 0;
  6958. const int64_t bit5Mask = imm & 0x20 ? UINT64_MAX : 0;
  6959. #endif
  6960. // Conditional multiplication
  6961. #if !SSE2NEON_PRECISE_DP
  6962. __m128d mul = _mm_mul_pd(a, b);
  6963. const __m128d mulMask =
  6964. _mm_castsi128_pd(_mm_set_epi64x(bit5Mask, bit4Mask));
  6965. __m128d tmp = _mm_and_pd(mul, mulMask);
  6966. #else
  6967. #if defined(__aarch64__)
  6968. double d0 = (imm & 0x10) ? vgetq_lane_f64(vreinterpretq_f64_m128d(a), 0) *
  6969. vgetq_lane_f64(vreinterpretq_f64_m128d(b), 0)
  6970. : 0;
  6971. double d1 = (imm & 0x20) ? vgetq_lane_f64(vreinterpretq_f64_m128d(a), 1) *
  6972. vgetq_lane_f64(vreinterpretq_f64_m128d(b), 1)
  6973. : 0;
  6974. #else
  6975. double d0 = (imm & 0x10) ? ((double *) &a)[0] * ((double *) &b)[0] : 0;
  6976. double d1 = (imm & 0x20) ? ((double *) &a)[1] * ((double *) &b)[1] : 0;
  6977. #endif
  6978. __m128d tmp = _mm_set_pd(d1, d0);
  6979. #endif
  6980. // Sum the products
  6981. #if defined(__aarch64__)
  6982. double sum = vpaddd_f64(vreinterpretq_f64_m128d(tmp));
  6983. #else
  6984. double sum = *((double *) &tmp) + *(((double *) &tmp) + 1);
  6985. #endif
  6986. // Conditionally store the sum
  6987. const __m128d sumMask =
  6988. _mm_castsi128_pd(_mm_set_epi64x(bit1Mask, bit0Mask));
  6989. __m128d res = _mm_and_pd(_mm_set_pd1(sum), sumMask);
  6990. return res;
  6991. }
  6992. // Conditionally multiply the packed single-precision (32-bit) floating-point
  6993. // elements in a and b using the high 4 bits in imm8, sum the four products,
  6994. // and conditionally store the sum in dst using the low 4 bits of imm.
  6995. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_dp_ps
  6996. FORCE_INLINE __m128 _mm_dp_ps(__m128 a, __m128 b, const int imm)
  6997. {
  6998. #if defined(__aarch64__)
  6999. /* shortcuts */
  7000. if (imm == 0xFF) {
  7001. return _mm_set1_ps(vaddvq_f32(_mm_mul_ps(a, b)));
  7002. }
  7003. if (imm == 0x7F) {
  7004. float32x4_t m = _mm_mul_ps(a, b);
  7005. m[3] = 0;
  7006. return _mm_set1_ps(vaddvq_f32(m));
  7007. }
  7008. #endif
  7009. float s = 0, c = 0;
  7010. float32x4_t f32a = vreinterpretq_f32_m128(a);
  7011. float32x4_t f32b = vreinterpretq_f32_m128(b);
  7012. /* To improve the accuracy of floating-point summation, Kahan algorithm
  7013. * is used for each operation.
  7014. */
  7015. if (imm & (1 << 4))
  7016. _sse2neon_kadd_f32(&s, &c, f32a[0] * f32b[0]);
  7017. if (imm & (1 << 5))
  7018. _sse2neon_kadd_f32(&s, &c, f32a[1] * f32b[1]);
  7019. if (imm & (1 << 6))
  7020. _sse2neon_kadd_f32(&s, &c, f32a[2] * f32b[2]);
  7021. if (imm & (1 << 7))
  7022. _sse2neon_kadd_f32(&s, &c, f32a[3] * f32b[3]);
  7023. s += c;
  7024. float32x4_t res = {
  7025. (imm & 0x1) ? s : 0,
  7026. (imm & 0x2) ? s : 0,
  7027. (imm & 0x4) ? s : 0,
  7028. (imm & 0x8) ? s : 0,
  7029. };
  7030. return vreinterpretq_m128_f32(res);
  7031. }
  7032. // Extracts the selected signed or unsigned 32-bit integer from a and zero
  7033. // extends.
  7034. // FORCE_INLINE int _mm_extract_epi32(__m128i a, __constrange(0,4) int imm)
  7035. #define _mm_extract_epi32(a, imm) \
  7036. vgetq_lane_s32(vreinterpretq_s32_m128i(a), (imm))
  7037. // Extracts the selected signed or unsigned 64-bit integer from a and zero
  7038. // extends.
  7039. // FORCE_INLINE __int64 _mm_extract_epi64(__m128i a, __constrange(0,2) int imm)
  7040. #define _mm_extract_epi64(a, imm) \
  7041. vgetq_lane_s64(vreinterpretq_s64_m128i(a), (imm))
  7042. // Extracts the selected signed or unsigned 8-bit integer from a and zero
  7043. // extends.
  7044. // FORCE_INLINE int _mm_extract_epi8(__m128i a, __constrange(0,16) int imm)
  7045. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_extract_epi8
  7046. #define _mm_extract_epi8(a, imm) vgetq_lane_u8(vreinterpretq_u8_m128i(a), (imm))
  7047. // Extracts the selected single-precision (32-bit) floating-point from a.
  7048. // FORCE_INLINE int _mm_extract_ps(__m128 a, __constrange(0,4) int imm)
  7049. #define _mm_extract_ps(a, imm) vgetq_lane_s32(vreinterpretq_s32_m128(a), (imm))
  7050. // Round the packed double-precision (64-bit) floating-point elements in a down
  7051. // to an integer value, and store the results as packed double-precision
  7052. // floating-point elements in dst.
  7053. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_floor_pd
  7054. FORCE_INLINE __m128d _mm_floor_pd(__m128d a)
  7055. {
  7056. #if defined(__aarch64__)
  7057. return vreinterpretq_m128d_f64(vrndmq_f64(vreinterpretq_f64_m128d(a)));
  7058. #else
  7059. double *f = (double *) &a;
  7060. return _mm_set_pd(floor(f[1]), floor(f[0]));
  7061. #endif
  7062. }
  7063. // Round the packed single-precision (32-bit) floating-point elements in a down
  7064. // to an integer value, and store the results as packed single-precision
  7065. // floating-point elements in dst.
  7066. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_floor_ps
  7067. FORCE_INLINE __m128 _mm_floor_ps(__m128 a)
  7068. {
  7069. #if defined(__aarch64__)
  7070. return vreinterpretq_m128_f32(vrndmq_f32(vreinterpretq_f32_m128(a)));
  7071. #else
  7072. float *f = (float *) &a;
  7073. return _mm_set_ps(floorf(f[3]), floorf(f[2]), floorf(f[1]), floorf(f[0]));
  7074. #endif
  7075. }
  7076. // Round the lower double-precision (64-bit) floating-point element in b down to
  7077. // an integer value, store the result as a double-precision floating-point
  7078. // element in the lower element of dst, and copy the upper element from a to the
  7079. // upper element of dst.
  7080. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_floor_sd
  7081. FORCE_INLINE __m128d _mm_floor_sd(__m128d a, __m128d b)
  7082. {
  7083. return _mm_move_sd(a, _mm_floor_pd(b));
  7084. }
  7085. // Round the lower single-precision (32-bit) floating-point element in b down to
  7086. // an integer value, store the result as a single-precision floating-point
  7087. // element in the lower element of dst, and copy the upper 3 packed elements
  7088. // from a to the upper elements of dst.
  7089. //
  7090. // dst[31:0] := FLOOR(b[31:0])
  7091. // dst[127:32] := a[127:32]
  7092. //
  7093. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_floor_ss
  7094. FORCE_INLINE __m128 _mm_floor_ss(__m128 a, __m128 b)
  7095. {
  7096. return _mm_move_ss(a, _mm_floor_ps(b));
  7097. }
  7098. // Inserts the least significant 32 bits of b into the selected 32-bit integer
  7099. // of a.
  7100. // FORCE_INLINE __m128i _mm_insert_epi32(__m128i a, int b,
  7101. // __constrange(0,4) int imm)
  7102. #define _mm_insert_epi32(a, b, imm) \
  7103. __extension__({ \
  7104. vreinterpretq_m128i_s32( \
  7105. vsetq_lane_s32((b), vreinterpretq_s32_m128i(a), (imm))); \
  7106. })
  7107. // Inserts the least significant 64 bits of b into the selected 64-bit integer
  7108. // of a.
  7109. // FORCE_INLINE __m128i _mm_insert_epi64(__m128i a, __int64 b,
  7110. // __constrange(0,2) int imm)
  7111. #define _mm_insert_epi64(a, b, imm) \
  7112. __extension__({ \
  7113. vreinterpretq_m128i_s64( \
  7114. vsetq_lane_s64((b), vreinterpretq_s64_m128i(a), (imm))); \
  7115. })
  7116. // Inserts the least significant 8 bits of b into the selected 8-bit integer
  7117. // of a.
  7118. // FORCE_INLINE __m128i _mm_insert_epi8(__m128i a, int b,
  7119. // __constrange(0,16) int imm)
  7120. #define _mm_insert_epi8(a, b, imm) \
  7121. __extension__({ \
  7122. vreinterpretq_m128i_s8( \
  7123. vsetq_lane_s8((b), vreinterpretq_s8_m128i(a), (imm))); \
  7124. })
  7125. // Copy a to tmp, then insert a single-precision (32-bit) floating-point
  7126. // element from b into tmp using the control in imm8. Store tmp to dst using
  7127. // the mask in imm8 (elements are zeroed out when the corresponding bit is set).
  7128. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=insert_ps
  7129. #define _mm_insert_ps(a, b, imm8) \
  7130. __extension__({ \
  7131. float32x4_t tmp1 = \
  7132. vsetq_lane_f32(vgetq_lane_f32(b, (imm8 >> 6) & 0x3), \
  7133. vreinterpretq_f32_m128(a), 0); \
  7134. float32x4_t tmp2 = \
  7135. vsetq_lane_f32(vgetq_lane_f32(tmp1, 0), vreinterpretq_f32_m128(a), \
  7136. ((imm8 >> 4) & 0x3)); \
  7137. const uint32_t data[4] = {((imm8) & (1 << 0)) ? UINT32_MAX : 0, \
  7138. ((imm8) & (1 << 1)) ? UINT32_MAX : 0, \
  7139. ((imm8) & (1 << 2)) ? UINT32_MAX : 0, \
  7140. ((imm8) & (1 << 3)) ? UINT32_MAX : 0}; \
  7141. uint32x4_t mask = vld1q_u32(data); \
  7142. float32x4_t all_zeros = vdupq_n_f32(0); \
  7143. \
  7144. vreinterpretq_m128_f32( \
  7145. vbslq_f32(mask, all_zeros, vreinterpretq_f32_m128(tmp2))); \
  7146. })
  7147. // epi versions of min/max
  7148. // Computes the pariwise maximums of the four signed 32-bit integer values of a
  7149. // and b.
  7150. //
  7151. // A 128-bit parameter that can be defined with the following equations:
  7152. // r0 := (a0 > b0) ? a0 : b0
  7153. // r1 := (a1 > b1) ? a1 : b1
  7154. // r2 := (a2 > b2) ? a2 : b2
  7155. // r3 := (a3 > b3) ? a3 : b3
  7156. //
  7157. // https://msdn.microsoft.com/en-us/library/vstudio/bb514055(v=vs.100).aspx
  7158. FORCE_INLINE __m128i _mm_max_epi32(__m128i a, __m128i b)
  7159. {
  7160. return vreinterpretq_m128i_s32(
  7161. vmaxq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  7162. }
  7163. // Compare packed signed 8-bit integers in a and b, and store packed maximum
  7164. // values in dst.
  7165. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_max_epi8
  7166. FORCE_INLINE __m128i _mm_max_epi8(__m128i a, __m128i b)
  7167. {
  7168. return vreinterpretq_m128i_s8(
  7169. vmaxq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  7170. }
  7171. // Compare packed unsigned 16-bit integers in a and b, and store packed maximum
  7172. // values in dst.
  7173. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_max_epu16
  7174. FORCE_INLINE __m128i _mm_max_epu16(__m128i a, __m128i b)
  7175. {
  7176. return vreinterpretq_m128i_u16(
  7177. vmaxq_u16(vreinterpretq_u16_m128i(a), vreinterpretq_u16_m128i(b)));
  7178. }
  7179. // Compare packed unsigned 32-bit integers in a and b, and store packed maximum
  7180. // values in dst.
  7181. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_max_epu32
  7182. FORCE_INLINE __m128i _mm_max_epu32(__m128i a, __m128i b)
  7183. {
  7184. return vreinterpretq_m128i_u32(
  7185. vmaxq_u32(vreinterpretq_u32_m128i(a), vreinterpretq_u32_m128i(b)));
  7186. }
  7187. // Computes the pariwise minima of the four signed 32-bit integer values of a
  7188. // and b.
  7189. //
  7190. // A 128-bit parameter that can be defined with the following equations:
  7191. // r0 := (a0 < b0) ? a0 : b0
  7192. // r1 := (a1 < b1) ? a1 : b1
  7193. // r2 := (a2 < b2) ? a2 : b2
  7194. // r3 := (a3 < b3) ? a3 : b3
  7195. //
  7196. // https://msdn.microsoft.com/en-us/library/vstudio/bb531476(v=vs.100).aspx
  7197. FORCE_INLINE __m128i _mm_min_epi32(__m128i a, __m128i b)
  7198. {
  7199. return vreinterpretq_m128i_s32(
  7200. vminq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  7201. }
  7202. // Compare packed signed 8-bit integers in a and b, and store packed minimum
  7203. // values in dst.
  7204. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_min_epi8
  7205. FORCE_INLINE __m128i _mm_min_epi8(__m128i a, __m128i b)
  7206. {
  7207. return vreinterpretq_m128i_s8(
  7208. vminq_s8(vreinterpretq_s8_m128i(a), vreinterpretq_s8_m128i(b)));
  7209. }
  7210. // Compare packed unsigned 16-bit integers in a and b, and store packed minimum
  7211. // values in dst.
  7212. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_min_epu16
  7213. FORCE_INLINE __m128i _mm_min_epu16(__m128i a, __m128i b)
  7214. {
  7215. return vreinterpretq_m128i_u16(
  7216. vminq_u16(vreinterpretq_u16_m128i(a), vreinterpretq_u16_m128i(b)));
  7217. }
  7218. // Compare packed unsigned 32-bit integers in a and b, and store packed minimum
  7219. // values in dst.
  7220. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_max_epu32
  7221. FORCE_INLINE __m128i _mm_min_epu32(__m128i a, __m128i b)
  7222. {
  7223. return vreinterpretq_m128i_u32(
  7224. vminq_u32(vreinterpretq_u32_m128i(a), vreinterpretq_u32_m128i(b)));
  7225. }
  7226. // Horizontally compute the minimum amongst the packed unsigned 16-bit integers
  7227. // in a, store the minimum and index in dst, and zero the remaining bits in dst.
  7228. //
  7229. // index[2:0] := 0
  7230. // min[15:0] := a[15:0]
  7231. // FOR j := 0 to 7
  7232. // i := j*16
  7233. // IF a[i+15:i] < min[15:0]
  7234. // index[2:0] := j
  7235. // min[15:0] := a[i+15:i]
  7236. // FI
  7237. // ENDFOR
  7238. // dst[15:0] := min[15:0]
  7239. // dst[18:16] := index[2:0]
  7240. // dst[127:19] := 0
  7241. //
  7242. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_minpos_epu16
  7243. FORCE_INLINE __m128i _mm_minpos_epu16(__m128i a)
  7244. {
  7245. __m128i dst;
  7246. uint16_t min, idx = 0;
  7247. // Find the minimum value
  7248. #if defined(__aarch64__)
  7249. min = vminvq_u16(vreinterpretq_u16_m128i(a));
  7250. #else
  7251. __m64 tmp;
  7252. tmp = vreinterpret_m64_u16(
  7253. vmin_u16(vget_low_u16(vreinterpretq_u16_m128i(a)),
  7254. vget_high_u16(vreinterpretq_u16_m128i(a))));
  7255. tmp = vreinterpret_m64_u16(
  7256. vpmin_u16(vreinterpret_u16_m64(tmp), vreinterpret_u16_m64(tmp)));
  7257. tmp = vreinterpret_m64_u16(
  7258. vpmin_u16(vreinterpret_u16_m64(tmp), vreinterpret_u16_m64(tmp)));
  7259. min = vget_lane_u16(vreinterpret_u16_m64(tmp), 0);
  7260. #endif
  7261. // Get the index of the minimum value
  7262. int i;
  7263. for (i = 0; i < 8; i++) {
  7264. if (min == vgetq_lane_u16(vreinterpretq_u16_m128i(a), 0)) {
  7265. idx = (uint16_t) i;
  7266. break;
  7267. }
  7268. a = _mm_srli_si128(a, 2);
  7269. }
  7270. // Generate result
  7271. dst = _mm_setzero_si128();
  7272. dst = vreinterpretq_m128i_u16(
  7273. vsetq_lane_u16(min, vreinterpretq_u16_m128i(dst), 0));
  7274. dst = vreinterpretq_m128i_u16(
  7275. vsetq_lane_u16(idx, vreinterpretq_u16_m128i(dst), 1));
  7276. return dst;
  7277. }
  7278. // Compute the sum of absolute differences (SADs) of quadruplets of unsigned
  7279. // 8-bit integers in a compared to those in b, and store the 16-bit results in
  7280. // dst. Eight SADs are performed using one quadruplet from b and eight
  7281. // quadruplets from a. One quadruplet is selected from b starting at on the
  7282. // offset specified in imm8. Eight quadruplets are formed from sequential 8-bit
  7283. // integers selected from a starting at the offset specified in imm8.
  7284. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_mpsadbw_epu8
  7285. FORCE_INLINE __m128i _mm_mpsadbw_epu8(__m128i a, __m128i b, const int imm)
  7286. {
  7287. uint8x16_t _a, _b;
  7288. switch (imm & 0x4) {
  7289. case 0:
  7290. // do nothing
  7291. _a = vreinterpretq_u8_m128i(a);
  7292. break;
  7293. case 4:
  7294. _a = vreinterpretq_u8_u32(vextq_u32(vreinterpretq_u32_m128i(a),
  7295. vreinterpretq_u32_m128i(a), 1));
  7296. break;
  7297. default:
  7298. #if defined(__GNUC__) || defined(__clang__)
  7299. __builtin_unreachable();
  7300. #endif
  7301. break;
  7302. }
  7303. switch (imm & 0x3) {
  7304. case 0:
  7305. _b = vreinterpretq_u8_u32(
  7306. vdupq_n_u32(vgetq_lane_u32(vreinterpretq_u32_m128i(b), 0)));
  7307. break;
  7308. case 1:
  7309. _b = vreinterpretq_u8_u32(
  7310. vdupq_n_u32(vgetq_lane_u32(vreinterpretq_u32_m128i(b), 1)));
  7311. break;
  7312. case 2:
  7313. _b = vreinterpretq_u8_u32(
  7314. vdupq_n_u32(vgetq_lane_u32(vreinterpretq_u32_m128i(b), 2)));
  7315. break;
  7316. case 3:
  7317. _b = vreinterpretq_u8_u32(
  7318. vdupq_n_u32(vgetq_lane_u32(vreinterpretq_u32_m128i(b), 3)));
  7319. break;
  7320. default:
  7321. #if defined(__GNUC__) || defined(__clang__)
  7322. __builtin_unreachable();
  7323. #endif
  7324. break;
  7325. }
  7326. int16x8_t c04, c15, c26, c37;
  7327. uint8x8_t low_b = vget_low_u8(_b);
  7328. c04 = vabsq_s16(vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(_a), low_b)));
  7329. _a = vextq_u8(_a, _a, 1);
  7330. c15 = vabsq_s16(vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(_a), low_b)));
  7331. _a = vextq_u8(_a, _a, 1);
  7332. c26 = vabsq_s16(vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(_a), low_b)));
  7333. _a = vextq_u8(_a, _a, 1);
  7334. c37 = vabsq_s16(vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(_a), low_b)));
  7335. #if defined(__aarch64__)
  7336. // |0|4|2|6|
  7337. c04 = vpaddq_s16(c04, c26);
  7338. // |1|5|3|7|
  7339. c15 = vpaddq_s16(c15, c37);
  7340. int32x4_t trn1_c =
  7341. vtrn1q_s32(vreinterpretq_s32_s16(c04), vreinterpretq_s32_s16(c15));
  7342. int32x4_t trn2_c =
  7343. vtrn2q_s32(vreinterpretq_s32_s16(c04), vreinterpretq_s32_s16(c15));
  7344. return vreinterpretq_m128i_s16(vpaddq_s16(vreinterpretq_s16_s32(trn1_c),
  7345. vreinterpretq_s16_s32(trn2_c)));
  7346. #else
  7347. int16x4_t c01, c23, c45, c67;
  7348. c01 = vpadd_s16(vget_low_s16(c04), vget_low_s16(c15));
  7349. c23 = vpadd_s16(vget_low_s16(c26), vget_low_s16(c37));
  7350. c45 = vpadd_s16(vget_high_s16(c04), vget_high_s16(c15));
  7351. c67 = vpadd_s16(vget_high_s16(c26), vget_high_s16(c37));
  7352. return vreinterpretq_m128i_s16(
  7353. vcombine_s16(vpadd_s16(c01, c23), vpadd_s16(c45, c67)));
  7354. #endif
  7355. }
  7356. // Multiply the low signed 32-bit integers from each packed 64-bit element in
  7357. // a and b, and store the signed 64-bit results in dst.
  7358. //
  7359. // r0 := (int64_t)(int32_t)a0 * (int64_t)(int32_t)b0
  7360. // r1 := (int64_t)(int32_t)a2 * (int64_t)(int32_t)b2
  7361. FORCE_INLINE __m128i _mm_mul_epi32(__m128i a, __m128i b)
  7362. {
  7363. // vmull_s32 upcasts instead of masking, so we downcast.
  7364. int32x2_t a_lo = vmovn_s64(vreinterpretq_s64_m128i(a));
  7365. int32x2_t b_lo = vmovn_s64(vreinterpretq_s64_m128i(b));
  7366. return vreinterpretq_m128i_s64(vmull_s32(a_lo, b_lo));
  7367. }
  7368. // Multiplies the 4 signed or unsigned 32-bit integers from a by the 4 signed or
  7369. // unsigned 32-bit integers from b.
  7370. // https://msdn.microsoft.com/en-us/library/vstudio/bb531409(v=vs.100).aspx
  7371. FORCE_INLINE __m128i _mm_mullo_epi32(__m128i a, __m128i b)
  7372. {
  7373. return vreinterpretq_m128i_s32(
  7374. vmulq_s32(vreinterpretq_s32_m128i(a), vreinterpretq_s32_m128i(b)));
  7375. }
  7376. // Packs the 8 unsigned 32-bit integers from a and b into unsigned 16-bit
  7377. // integers and saturates.
  7378. //
  7379. // r0 := UnsignedSaturate(a0)
  7380. // r1 := UnsignedSaturate(a1)
  7381. // r2 := UnsignedSaturate(a2)
  7382. // r3 := UnsignedSaturate(a3)
  7383. // r4 := UnsignedSaturate(b0)
  7384. // r5 := UnsignedSaturate(b1)
  7385. // r6 := UnsignedSaturate(b2)
  7386. // r7 := UnsignedSaturate(b3)
  7387. FORCE_INLINE __m128i _mm_packus_epi32(__m128i a, __m128i b)
  7388. {
  7389. return vreinterpretq_m128i_u16(
  7390. vcombine_u16(vqmovun_s32(vreinterpretq_s32_m128i(a)),
  7391. vqmovun_s32(vreinterpretq_s32_m128i(b))));
  7392. }
  7393. // Round the packed double-precision (64-bit) floating-point elements in a using
  7394. // the rounding parameter, and store the results as packed double-precision
  7395. // floating-point elements in dst.
  7396. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_round_pd
  7397. FORCE_INLINE __m128d _mm_round_pd(__m128d a, int rounding)
  7398. {
  7399. #if defined(__aarch64__)
  7400. switch (rounding) {
  7401. case (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC):
  7402. return vreinterpretq_m128d_f64(vrndnq_f64(vreinterpretq_f64_m128d(a)));
  7403. case (_MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC):
  7404. return _mm_floor_pd(a);
  7405. case (_MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC):
  7406. return _mm_ceil_pd(a);
  7407. case (_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC):
  7408. return vreinterpretq_m128d_f64(vrndq_f64(vreinterpretq_f64_m128d(a)));
  7409. default: //_MM_FROUND_CUR_DIRECTION
  7410. return vreinterpretq_m128d_f64(vrndiq_f64(vreinterpretq_f64_m128d(a)));
  7411. }
  7412. #else
  7413. double *v_double = (double *) &a;
  7414. if (rounding == (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC) ||
  7415. (rounding == _MM_FROUND_CUR_DIRECTION &&
  7416. _MM_GET_ROUNDING_MODE() == _MM_ROUND_NEAREST)) {
  7417. double res[2], tmp;
  7418. for (int i = 0; i < 2; i++) {
  7419. tmp = (v_double[i] < 0) ? -v_double[i] : v_double[i];
  7420. double roundDown = floor(tmp); // Round down value
  7421. double roundUp = ceil(tmp); // Round up value
  7422. double diffDown = tmp - roundDown;
  7423. double diffUp = roundUp - tmp;
  7424. if (diffDown < diffUp) {
  7425. /* If it's closer to the round down value, then use it */
  7426. res[i] = roundDown;
  7427. } else if (diffDown > diffUp) {
  7428. /* If it's closer to the round up value, then use it */
  7429. res[i] = roundUp;
  7430. } else {
  7431. /* If it's equidistant between round up and round down value,
  7432. * pick the one which is an even number */
  7433. double half = roundDown / 2;
  7434. if (half != floor(half)) {
  7435. /* If the round down value is odd, return the round up value
  7436. */
  7437. res[i] = roundUp;
  7438. } else {
  7439. /* If the round up value is odd, return the round down value
  7440. */
  7441. res[i] = roundDown;
  7442. }
  7443. }
  7444. res[i] = (v_double[i] < 0) ? -res[i] : res[i];
  7445. }
  7446. return _mm_set_pd(res[1], res[0]);
  7447. } else if (rounding == (_MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC) ||
  7448. (rounding == _MM_FROUND_CUR_DIRECTION &&
  7449. _MM_GET_ROUNDING_MODE() == _MM_ROUND_DOWN)) {
  7450. return _mm_floor_pd(a);
  7451. } else if (rounding == (_MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC) ||
  7452. (rounding == _MM_FROUND_CUR_DIRECTION &&
  7453. _MM_GET_ROUNDING_MODE() == _MM_ROUND_UP)) {
  7454. return _mm_ceil_pd(a);
  7455. }
  7456. return _mm_set_pd(v_double[1] > 0 ? floor(v_double[1]) : ceil(v_double[1]),
  7457. v_double[0] > 0 ? floor(v_double[0]) : ceil(v_double[0]));
  7458. #endif
  7459. }
  7460. // Round the packed single-precision (32-bit) floating-point elements in a using
  7461. // the rounding parameter, and store the results as packed single-precision
  7462. // floating-point elements in dst.
  7463. // software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_round_ps
  7464. FORCE_INLINE __m128 _mm_round_ps(__m128 a, int rounding)
  7465. {
  7466. #if defined(__aarch64__)
  7467. switch (rounding) {
  7468. case (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC):
  7469. return vreinterpretq_m128_f32(vrndnq_f32(vreinterpretq_f32_m128(a)));
  7470. case (_MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC):
  7471. return _mm_floor_ps(a);
  7472. case (_MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC):
  7473. return _mm_ceil_ps(a);
  7474. case (_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC):
  7475. return vreinterpretq_m128_f32(vrndq_f32(vreinterpretq_f32_m128(a)));
  7476. default: //_MM_FROUND_CUR_DIRECTION
  7477. return vreinterpretq_m128_f32(vrndiq_f32(vreinterpretq_f32_m128(a)));
  7478. }
  7479. #else
  7480. float *v_float = (float *) &a;
  7481. if (rounding == (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC) ||
  7482. (rounding == _MM_FROUND_CUR_DIRECTION &&
  7483. _MM_GET_ROUNDING_MODE() == _MM_ROUND_NEAREST)) {
  7484. uint32x4_t signmask = vdupq_n_u32(0x80000000);
  7485. float32x4_t half = vbslq_f32(signmask, vreinterpretq_f32_m128(a),
  7486. vdupq_n_f32(0.5f)); /* +/- 0.5 */
  7487. int32x4_t r_normal = vcvtq_s32_f32(vaddq_f32(
  7488. vreinterpretq_f32_m128(a), half)); /* round to integer: [a + 0.5]*/
  7489. int32x4_t r_trunc = vcvtq_s32_f32(
  7490. vreinterpretq_f32_m128(a)); /* truncate to integer: [a] */
  7491. int32x4_t plusone = vreinterpretq_s32_u32(vshrq_n_u32(
  7492. vreinterpretq_u32_s32(vnegq_s32(r_trunc)), 31)); /* 1 or 0 */
  7493. int32x4_t r_even = vbicq_s32(vaddq_s32(r_trunc, plusone),
  7494. vdupq_n_s32(1)); /* ([a] + {0,1}) & ~1 */
  7495. float32x4_t delta = vsubq_f32(
  7496. vreinterpretq_f32_m128(a),
  7497. vcvtq_f32_s32(r_trunc)); /* compute delta: delta = (a - [a]) */
  7498. uint32x4_t is_delta_half =
  7499. vceqq_f32(delta, half); /* delta == +/- 0.5 */
  7500. return vreinterpretq_m128_f32(
  7501. vcvtq_f32_s32(vbslq_s32(is_delta_half, r_even, r_normal)));
  7502. } else if (rounding == (_MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC) ||
  7503. (rounding == _MM_FROUND_CUR_DIRECTION &&
  7504. _MM_GET_ROUNDING_MODE() == _MM_ROUND_DOWN)) {
  7505. return _mm_floor_ps(a);
  7506. } else if (rounding == (_MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC) ||
  7507. (rounding == _MM_FROUND_CUR_DIRECTION &&
  7508. _MM_GET_ROUNDING_MODE() == _MM_ROUND_UP)) {
  7509. return _mm_ceil_ps(a);
  7510. }
  7511. return _mm_set_ps(v_float[3] > 0 ? floorf(v_float[3]) : ceilf(v_float[3]),
  7512. v_float[2] > 0 ? floorf(v_float[2]) : ceilf(v_float[2]),
  7513. v_float[1] > 0 ? floorf(v_float[1]) : ceilf(v_float[1]),
  7514. v_float[0] > 0 ? floorf(v_float[0]) : ceilf(v_float[0]));
  7515. #endif
  7516. }
  7517. // Round the lower double-precision (64-bit) floating-point element in b using
  7518. // the rounding parameter, store the result as a double-precision floating-point
  7519. // element in the lower element of dst, and copy the upper element from a to the
  7520. // upper element of dst.
  7521. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_round_sd
  7522. FORCE_INLINE __m128d _mm_round_sd(__m128d a, __m128d b, int rounding)
  7523. {
  7524. return _mm_move_sd(a, _mm_round_pd(b, rounding));
  7525. }
  7526. // Round the lower single-precision (32-bit) floating-point element in b using
  7527. // the rounding parameter, store the result as a single-precision floating-point
  7528. // element in the lower element of dst, and copy the upper 3 packed elements
  7529. // from a to the upper elements of dst. Rounding is done according to the
  7530. // rounding[3:0] parameter, which can be one of:
  7531. // (_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC) // round to nearest, and
  7532. // suppress exceptions
  7533. // (_MM_FROUND_TO_NEG_INF |_MM_FROUND_NO_EXC) // round down, and
  7534. // suppress exceptions
  7535. // (_MM_FROUND_TO_POS_INF |_MM_FROUND_NO_EXC) // round up, and suppress
  7536. // exceptions
  7537. // (_MM_FROUND_TO_ZERO |_MM_FROUND_NO_EXC) // truncate, and suppress
  7538. // exceptions _MM_FROUND_CUR_DIRECTION // use MXCSR.RC; see
  7539. // _MM_SET_ROUNDING_MODE
  7540. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_round_ss
  7541. FORCE_INLINE __m128 _mm_round_ss(__m128 a, __m128 b, int rounding)
  7542. {
  7543. return _mm_move_ss(a, _mm_round_ps(b, rounding));
  7544. }
  7545. // Load 128-bits of integer data from memory into dst using a non-temporal
  7546. // memory hint. mem_addr must be aligned on a 16-byte boundary or a
  7547. // general-protection exception may be generated.
  7548. //
  7549. // dst[127:0] := MEM[mem_addr+127:mem_addr]
  7550. //
  7551. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_stream_load_si128
  7552. FORCE_INLINE __m128i _mm_stream_load_si128(__m128i *p)
  7553. {
  7554. #if __has_builtin(__builtin_nontemporal_store)
  7555. return __builtin_nontemporal_load(p);
  7556. #else
  7557. return vreinterpretq_m128i_s64(vld1q_s64((int64_t *) p));
  7558. #endif
  7559. }
  7560. // Compute the bitwise NOT of a and then AND with a 128-bit vector containing
  7561. // all 1's, and return 1 if the result is zero, otherwise return 0.
  7562. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_test_all_ones
  7563. FORCE_INLINE int _mm_test_all_ones(__m128i a)
  7564. {
  7565. return (uint64_t)(vgetq_lane_s64(a, 0) & vgetq_lane_s64(a, 1)) ==
  7566. ~(uint64_t) 0;
  7567. }
  7568. // Compute the bitwise AND of 128 bits (representing integer data) in a and
  7569. // mask, and return 1 if the result is zero, otherwise return 0.
  7570. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_test_all_zeros
  7571. FORCE_INLINE int _mm_test_all_zeros(__m128i a, __m128i mask)
  7572. {
  7573. int64x2_t a_and_mask =
  7574. vandq_s64(vreinterpretq_s64_m128i(a), vreinterpretq_s64_m128i(mask));
  7575. return !(vgetq_lane_s64(a_and_mask, 0) | vgetq_lane_s64(a_and_mask, 1));
  7576. }
  7577. // Compute the bitwise AND of 128 bits (representing integer data) in a and
  7578. // mask, and set ZF to 1 if the result is zero, otherwise set ZF to 0. Compute
  7579. // the bitwise NOT of a and then AND with mask, and set CF to 1 if the result is
  7580. // zero, otherwise set CF to 0. Return 1 if both the ZF and CF values are zero,
  7581. // otherwise return 0.
  7582. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=mm_test_mix_ones_zero
  7583. FORCE_INLINE int _mm_test_mix_ones_zeros(__m128i a, __m128i mask)
  7584. {
  7585. uint64x2_t zf =
  7586. vandq_u64(vreinterpretq_u64_m128i(mask), vreinterpretq_u64_m128i(a));
  7587. uint64x2_t cf =
  7588. vbicq_u64(vreinterpretq_u64_m128i(mask), vreinterpretq_u64_m128i(a));
  7589. uint64x2_t result = vandq_u64(zf, cf);
  7590. return !(vgetq_lane_u64(result, 0) | vgetq_lane_u64(result, 1));
  7591. }
  7592. // Compute the bitwise AND of 128 bits (representing integer data) in a and b,
  7593. // and set ZF to 1 if the result is zero, otherwise set ZF to 0. Compute the
  7594. // bitwise NOT of a and then AND with b, and set CF to 1 if the result is zero,
  7595. // otherwise set CF to 0. Return the CF value.
  7596. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_testc_si128
  7597. FORCE_INLINE int _mm_testc_si128(__m128i a, __m128i b)
  7598. {
  7599. int64x2_t s64 =
  7600. vandq_s64(vreinterpretq_s64_s32(vmvnq_s32(vreinterpretq_s32_m128i(a))),
  7601. vreinterpretq_s64_m128i(b));
  7602. return !(vgetq_lane_s64(s64, 0) | vgetq_lane_s64(s64, 1));
  7603. }
  7604. // Compute the bitwise AND of 128 bits (representing integer data) in a and b,
  7605. // and set ZF to 1 if the result is zero, otherwise set ZF to 0. Compute the
  7606. // bitwise NOT of a and then AND with b, and set CF to 1 if the result is zero,
  7607. // otherwise set CF to 0. Return 1 if both the ZF and CF values are zero,
  7608. // otherwise return 0.
  7609. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_testnzc_si128
  7610. #define _mm_testnzc_si128(a, b) _mm_test_mix_ones_zeros(a, b)
  7611. // Compute the bitwise AND of 128 bits (representing integer data) in a and b,
  7612. // and set ZF to 1 if the result is zero, otherwise set ZF to 0. Compute the
  7613. // bitwise NOT of a and then AND with b, and set CF to 1 if the result is zero,
  7614. // otherwise set CF to 0. Return the ZF value.
  7615. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_testz_si128
  7616. FORCE_INLINE int _mm_testz_si128(__m128i a, __m128i b)
  7617. {
  7618. int64x2_t s64 =
  7619. vandq_s64(vreinterpretq_s64_m128i(a), vreinterpretq_s64_m128i(b));
  7620. return !(vgetq_lane_s64(s64, 0) | vgetq_lane_s64(s64, 1));
  7621. }
  7622. /* SSE4.2 */
  7623. // Compares the 2 signed 64-bit integers in a and the 2 signed 64-bit integers
  7624. // in b for greater than.
  7625. FORCE_INLINE __m128i _mm_cmpgt_epi64(__m128i a, __m128i b)
  7626. {
  7627. #if defined(__aarch64__)
  7628. return vreinterpretq_m128i_u64(
  7629. vcgtq_s64(vreinterpretq_s64_m128i(a), vreinterpretq_s64_m128i(b)));
  7630. #else
  7631. return vreinterpretq_m128i_s64(vshrq_n_s64(
  7632. vqsubq_s64(vreinterpretq_s64_m128i(b), vreinterpretq_s64_m128i(a)),
  7633. 63));
  7634. #endif
  7635. }
  7636. // Starting with the initial value in crc, accumulates a CRC32 value for
  7637. // unsigned 16-bit integer v.
  7638. // https://msdn.microsoft.com/en-us/library/bb531411(v=vs.100)
  7639. FORCE_INLINE uint32_t _mm_crc32_u16(uint32_t crc, uint16_t v)
  7640. {
  7641. #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32)
  7642. __asm__ __volatile__("crc32ch %w[c], %w[c], %w[v]\n\t"
  7643. : [c] "+r"(crc)
  7644. : [v] "r"(v));
  7645. #else
  7646. crc = _mm_crc32_u8(crc, v & 0xff);
  7647. crc = _mm_crc32_u8(crc, (v >> 8) & 0xff);
  7648. #endif
  7649. return crc;
  7650. }
  7651. // Starting with the initial value in crc, accumulates a CRC32 value for
  7652. // unsigned 32-bit integer v.
  7653. // https://msdn.microsoft.com/en-us/library/bb531394(v=vs.100)
  7654. FORCE_INLINE uint32_t _mm_crc32_u32(uint32_t crc, uint32_t v)
  7655. {
  7656. #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32)
  7657. __asm__ __volatile__("crc32cw %w[c], %w[c], %w[v]\n\t"
  7658. : [c] "+r"(crc)
  7659. : [v] "r"(v));
  7660. #else
  7661. crc = _mm_crc32_u16(crc, v & 0xffff);
  7662. crc = _mm_crc32_u16(crc, (v >> 16) & 0xffff);
  7663. #endif
  7664. return crc;
  7665. }
  7666. // Starting with the initial value in crc, accumulates a CRC32 value for
  7667. // unsigned 64-bit integer v.
  7668. // https://msdn.microsoft.com/en-us/library/bb514033(v=vs.100)
  7669. FORCE_INLINE uint64_t _mm_crc32_u64(uint64_t crc, uint64_t v)
  7670. {
  7671. #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32)
  7672. __asm__ __volatile__("crc32cx %w[c], %w[c], %x[v]\n\t"
  7673. : [c] "+r"(crc)
  7674. : [v] "r"(v));
  7675. #else
  7676. crc = _mm_crc32_u32((uint32_t)(crc), v & 0xffffffff);
  7677. crc = _mm_crc32_u32((uint32_t)(crc), (v >> 32) & 0xffffffff);
  7678. #endif
  7679. return crc;
  7680. }
  7681. // Starting with the initial value in crc, accumulates a CRC32 value for
  7682. // unsigned 8-bit integer v.
  7683. // https://msdn.microsoft.com/en-us/library/bb514036(v=vs.100)
  7684. FORCE_INLINE uint32_t _mm_crc32_u8(uint32_t crc, uint8_t v)
  7685. {
  7686. #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32)
  7687. __asm__ __volatile__("crc32cb %w[c], %w[c], %w[v]\n\t"
  7688. : [c] "+r"(crc)
  7689. : [v] "r"(v));
  7690. #else
  7691. crc ^= v;
  7692. for (int bit = 0; bit < 8; bit++) {
  7693. if (crc & 1)
  7694. crc = (crc >> 1) ^ UINT32_C(0x82f63b78);
  7695. else
  7696. crc = (crc >> 1);
  7697. }
  7698. #endif
  7699. return crc;
  7700. }
  7701. /* AES */
  7702. #if !defined(__ARM_FEATURE_CRYPTO)
  7703. /* clang-format off */
  7704. #define SSE2NEON_AES_DATA(w) \
  7705. { \
  7706. w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), \
  7707. w(0xc5), w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), \
  7708. w(0xab), w(0x76), w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), \
  7709. w(0x59), w(0x47), w(0xf0), w(0xad), w(0xd4), w(0xa2), w(0xaf), \
  7710. w(0x9c), w(0xa4), w(0x72), w(0xc0), w(0xb7), w(0xfd), w(0x93), \
  7711. w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc), w(0x34), w(0xa5), \
  7712. w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15), w(0x04), \
  7713. w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a), \
  7714. w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), \
  7715. w(0x75), w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), \
  7716. w(0x5a), w(0xa0), w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), \
  7717. w(0xe3), w(0x2f), w(0x84), w(0x53), w(0xd1), w(0x00), w(0xed), \
  7718. w(0x20), w(0xfc), w(0xb1), w(0x5b), w(0x6a), w(0xcb), w(0xbe), \
  7719. w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf), w(0xd0), w(0xef), \
  7720. w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85), w(0x45), \
  7721. w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8), \
  7722. w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), \
  7723. w(0xf5), w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), \
  7724. w(0xf3), w(0xd2), w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), \
  7725. w(0x97), w(0x44), w(0x17), w(0xc4), w(0xa7), w(0x7e), w(0x3d), \
  7726. w(0x64), w(0x5d), w(0x19), w(0x73), w(0x60), w(0x81), w(0x4f), \
  7727. w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88), w(0x46), w(0xee), \
  7728. w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb), w(0xe0), \
  7729. w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c), \
  7730. w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), \
  7731. w(0x79), w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), \
  7732. w(0x4e), w(0xa9), w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), \
  7733. w(0x7a), w(0xae), w(0x08), w(0xba), w(0x78), w(0x25), w(0x2e), \
  7734. w(0x1c), w(0xa6), w(0xb4), w(0xc6), w(0xe8), w(0xdd), w(0x74), \
  7735. w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a), w(0x70), w(0x3e), \
  7736. w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e), w(0x61), \
  7737. w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e), \
  7738. w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), \
  7739. w(0x94), w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), \
  7740. w(0x28), w(0xdf), w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), \
  7741. w(0xe6), w(0x42), w(0x68), w(0x41), w(0x99), w(0x2d), w(0x0f), \
  7742. w(0xb0), w(0x54), w(0xbb), w(0x16) \
  7743. }
  7744. /* clang-format on */
  7745. /* X Macro trick. See https://en.wikipedia.org/wiki/X_Macro */
  7746. #define SSE2NEON_AES_H0(x) (x)
  7747. static const uint8_t SSE2NEON_sbox[256] = SSE2NEON_AES_DATA(SSE2NEON_AES_H0);
  7748. #undef SSE2NEON_AES_H0
  7749. // In the absence of crypto extensions, implement aesenc using regular neon
  7750. // intrinsics instead. See:
  7751. // https://www.workofard.com/2017/01/accelerated-aes-for-the-arm64-linux-kernel/
  7752. // https://www.workofard.com/2017/07/ghash-for-low-end-cores/ and
  7753. // https://github.com/ColinIanKing/linux-next-mirror/blob/b5f466091e130caaf0735976648f72bd5e09aa84/crypto/aegis128-neon-inner.c#L52
  7754. // for more information Reproduced with permission of the author.
  7755. FORCE_INLINE __m128i _mm_aesenc_si128(__m128i EncBlock, __m128i RoundKey)
  7756. {
  7757. #if defined(__aarch64__)
  7758. static const uint8_t shift_rows[] = {0x0, 0x5, 0xa, 0xf, 0x4, 0x9,
  7759. 0xe, 0x3, 0x8, 0xd, 0x2, 0x7,
  7760. 0xc, 0x1, 0x6, 0xb};
  7761. static const uint8_t ror32by8[] = {0x1, 0x2, 0x3, 0x0, 0x5, 0x6, 0x7, 0x4,
  7762. 0x9, 0xa, 0xb, 0x8, 0xd, 0xe, 0xf, 0xc};
  7763. uint8x16_t v;
  7764. uint8x16_t w = vreinterpretq_u8_m128i(EncBlock);
  7765. // shift rows
  7766. w = vqtbl1q_u8(w, vld1q_u8(shift_rows));
  7767. // sub bytes
  7768. v = vqtbl4q_u8(_sse2neon_vld1q_u8_x4(SSE2NEON_sbox), w);
  7769. v = vqtbx4q_u8(v, _sse2neon_vld1q_u8_x4(SSE2NEON_sbox + 0x40), w - 0x40);
  7770. v = vqtbx4q_u8(v, _sse2neon_vld1q_u8_x4(SSE2NEON_sbox + 0x80), w - 0x80);
  7771. v = vqtbx4q_u8(v, _sse2neon_vld1q_u8_x4(SSE2NEON_sbox + 0xc0), w - 0xc0);
  7772. // mix columns
  7773. w = (v << 1) ^ (uint8x16_t)(((int8x16_t) v >> 7) & 0x1b);
  7774. w ^= (uint8x16_t) vrev32q_u16((uint16x8_t) v);
  7775. w ^= vqtbl1q_u8(v ^ w, vld1q_u8(ror32by8));
  7776. // add round key
  7777. return vreinterpretq_m128i_u8(w) ^ RoundKey;
  7778. #else /* ARMv7-A NEON implementation */
  7779. #define SSE2NEON_AES_B2W(b0, b1, b2, b3) \
  7780. (((uint32_t)(b3) << 24) | ((uint32_t)(b2) << 16) | ((uint32_t)(b1) << 8) | \
  7781. (b0))
  7782. #define SSE2NEON_AES_F2(x) ((x << 1) ^ (((x >> 7) & 1) * 0x011b /* WPOLY */))
  7783. #define SSE2NEON_AES_F3(x) (SSE2NEON_AES_F2(x) ^ x)
  7784. #define SSE2NEON_AES_U0(p) \
  7785. SSE2NEON_AES_B2W(SSE2NEON_AES_F2(p), p, p, SSE2NEON_AES_F3(p))
  7786. #define SSE2NEON_AES_U1(p) \
  7787. SSE2NEON_AES_B2W(SSE2NEON_AES_F3(p), SSE2NEON_AES_F2(p), p, p)
  7788. #define SSE2NEON_AES_U2(p) \
  7789. SSE2NEON_AES_B2W(p, SSE2NEON_AES_F3(p), SSE2NEON_AES_F2(p), p)
  7790. #define SSE2NEON_AES_U3(p) \
  7791. SSE2NEON_AES_B2W(p, p, SSE2NEON_AES_F3(p), SSE2NEON_AES_F2(p))
  7792. static const uint32_t ALIGN_STRUCT(16) aes_table[4][256] = {
  7793. SSE2NEON_AES_DATA(SSE2NEON_AES_U0),
  7794. SSE2NEON_AES_DATA(SSE2NEON_AES_U1),
  7795. SSE2NEON_AES_DATA(SSE2NEON_AES_U2),
  7796. SSE2NEON_AES_DATA(SSE2NEON_AES_U3),
  7797. };
  7798. #undef SSE2NEON_AES_B2W
  7799. #undef SSE2NEON_AES_F2
  7800. #undef SSE2NEON_AES_F3
  7801. #undef SSE2NEON_AES_U0
  7802. #undef SSE2NEON_AES_U1
  7803. #undef SSE2NEON_AES_U2
  7804. #undef SSE2NEON_AES_U3
  7805. uint32_t x0 = _mm_cvtsi128_si32(EncBlock);
  7806. uint32_t x1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(EncBlock, 0x55));
  7807. uint32_t x2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(EncBlock, 0xAA));
  7808. uint32_t x3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(EncBlock, 0xFF));
  7809. __m128i out = _mm_set_epi32(
  7810. (aes_table[0][x3 & 0xff] ^ aes_table[1][(x0 >> 8) & 0xff] ^
  7811. aes_table[2][(x1 >> 16) & 0xff] ^ aes_table[3][x2 >> 24]),
  7812. (aes_table[0][x2 & 0xff] ^ aes_table[1][(x3 >> 8) & 0xff] ^
  7813. aes_table[2][(x0 >> 16) & 0xff] ^ aes_table[3][x1 >> 24]),
  7814. (aes_table[0][x1 & 0xff] ^ aes_table[1][(x2 >> 8) & 0xff] ^
  7815. aes_table[2][(x3 >> 16) & 0xff] ^ aes_table[3][x0 >> 24]),
  7816. (aes_table[0][x0 & 0xff] ^ aes_table[1][(x1 >> 8) & 0xff] ^
  7817. aes_table[2][(x2 >> 16) & 0xff] ^ aes_table[3][x3 >> 24]));
  7818. return _mm_xor_si128(out, RoundKey);
  7819. #endif
  7820. }
  7821. // Perform the last round of an AES encryption flow on data (state) in a using
  7822. // the round key in RoundKey, and store the result in dst.
  7823. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_aesenclast_si128
  7824. FORCE_INLINE __m128i _mm_aesenclast_si128(__m128i a, __m128i RoundKey)
  7825. {
  7826. /* FIXME: optimized for NEON */
  7827. uint8_t v[4][4] = {
  7828. {SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 0)],
  7829. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 5)],
  7830. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 10)],
  7831. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 15)]},
  7832. {SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 4)],
  7833. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 9)],
  7834. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 14)],
  7835. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 3)]},
  7836. {SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 8)],
  7837. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 13)],
  7838. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 2)],
  7839. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 7)]},
  7840. {SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 12)],
  7841. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 1)],
  7842. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 6)],
  7843. SSE2NEON_sbox[vreinterpretq_nth_u8_m128i(a, 11)]},
  7844. };
  7845. for (int i = 0; i < 16; i++)
  7846. vreinterpretq_nth_u8_m128i(a, i) =
  7847. v[i / 4][i % 4] ^ vreinterpretq_nth_u8_m128i(RoundKey, i);
  7848. return a;
  7849. }
  7850. // Emits the Advanced Encryption Standard (AES) instruction aeskeygenassist.
  7851. // This instruction generates a round key for AES encryption. See
  7852. // https://kazakov.life/2017/11/01/cryptocurrency-mining-on-ios-devices/
  7853. // for details.
  7854. //
  7855. // https://msdn.microsoft.com/en-us/library/cc714138(v=vs.120).aspx
  7856. FORCE_INLINE __m128i _mm_aeskeygenassist_si128(__m128i key, const int rcon)
  7857. {
  7858. uint32_t X1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(key, 0x55));
  7859. uint32_t X3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(key, 0xFF));
  7860. for (int i = 0; i < 4; ++i) {
  7861. ((uint8_t *) &X1)[i] = SSE2NEON_sbox[((uint8_t *) &X1)[i]];
  7862. ((uint8_t *) &X3)[i] = SSE2NEON_sbox[((uint8_t *) &X3)[i]];
  7863. }
  7864. return _mm_set_epi32(((X3 >> 8) | (X3 << 24)) ^ rcon, X3,
  7865. ((X1 >> 8) | (X1 << 24)) ^ rcon, X1);
  7866. }
  7867. #undef SSE2NEON_AES_DATA
  7868. #else /* __ARM_FEATURE_CRYPTO */
  7869. // Implements equivalent of 'aesenc' by combining AESE (with an empty key) and
  7870. // AESMC and then manually applying the real key as an xor operation. This
  7871. // unfortunately means an additional xor op; the compiler should be able to
  7872. // optimize this away for repeated calls however. See
  7873. // https://blog.michaelbrase.com/2018/05/08/emulating-x86-aes-intrinsics-on-armv8-a
  7874. // for more details.
  7875. FORCE_INLINE __m128i _mm_aesenc_si128(__m128i a, __m128i b)
  7876. {
  7877. return vreinterpretq_m128i_u8(
  7878. vaesmcq_u8(vaeseq_u8(vreinterpretq_u8_m128i(a), vdupq_n_u8(0))) ^
  7879. vreinterpretq_u8_m128i(b));
  7880. }
  7881. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_aesenclast_si128
  7882. FORCE_INLINE __m128i _mm_aesenclast_si128(__m128i a, __m128i RoundKey)
  7883. {
  7884. return _mm_xor_si128(vreinterpretq_m128i_u8(vaeseq_u8(
  7885. vreinterpretq_u8_m128i(a), vdupq_n_u8(0))),
  7886. RoundKey);
  7887. }
  7888. FORCE_INLINE __m128i _mm_aeskeygenassist_si128(__m128i a, const int rcon)
  7889. {
  7890. // AESE does ShiftRows and SubBytes on A
  7891. uint8x16_t u8 = vaeseq_u8(vreinterpretq_u8_m128i(a), vdupq_n_u8(0));
  7892. uint8x16_t dest = {
  7893. // Undo ShiftRows step from AESE and extract X1 and X3
  7894. u8[0x4], u8[0x1], u8[0xE], u8[0xB], // SubBytes(X1)
  7895. u8[0x1], u8[0xE], u8[0xB], u8[0x4], // ROT(SubBytes(X1))
  7896. u8[0xC], u8[0x9], u8[0x6], u8[0x3], // SubBytes(X3)
  7897. u8[0x9], u8[0x6], u8[0x3], u8[0xC], // ROT(SubBytes(X3))
  7898. };
  7899. uint32x4_t r = {0, (unsigned) rcon, 0, (unsigned) rcon};
  7900. return vreinterpretq_m128i_u8(dest) ^ vreinterpretq_m128i_u32(r);
  7901. }
  7902. #endif
  7903. /* Others */
  7904. // Perform a carry-less multiplication of two 64-bit integers, selected from a
  7905. // and b according to imm8, and store the results in dst.
  7906. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_clmulepi64_si128
  7907. FORCE_INLINE __m128i _mm_clmulepi64_si128(__m128i _a, __m128i _b, const int imm)
  7908. {
  7909. uint64x2_t a = vreinterpretq_u64_m128i(_a);
  7910. uint64x2_t b = vreinterpretq_u64_m128i(_b);
  7911. switch (imm & 0x11) {
  7912. case 0x00:
  7913. return vreinterpretq_m128i_u64(
  7914. _sse2neon_vmull_p64(vget_low_u64(a), vget_low_u64(b)));
  7915. case 0x01:
  7916. return vreinterpretq_m128i_u64(
  7917. _sse2neon_vmull_p64(vget_high_u64(a), vget_low_u64(b)));
  7918. case 0x10:
  7919. return vreinterpretq_m128i_u64(
  7920. _sse2neon_vmull_p64(vget_low_u64(a), vget_high_u64(b)));
  7921. case 0x11:
  7922. return vreinterpretq_m128i_u64(
  7923. _sse2neon_vmull_p64(vget_high_u64(a), vget_high_u64(b)));
  7924. default:
  7925. abort();
  7926. }
  7927. }
  7928. FORCE_INLINE unsigned int _sse2neon_mm_get_denormals_zero_mode()
  7929. {
  7930. union {
  7931. fpcr_bitfield field;
  7932. #if defined(__aarch64__)
  7933. uint64_t value;
  7934. #else
  7935. uint32_t value;
  7936. #endif
  7937. } r;
  7938. #if defined(__aarch64__)
  7939. asm volatile("mrs %0, FPCR" : "=r"(r.value)); /* read */
  7940. #else
  7941. asm volatile("vmrs %0, FPSCR" : "=r"(r.value)); /* read */
  7942. #endif
  7943. return r.field.bit24 ? _MM_DENORMALS_ZERO_ON : _MM_DENORMALS_ZERO_OFF;
  7944. }
  7945. // Count the number of bits set to 1 in unsigned 32-bit integer a, and
  7946. // return that count in dst.
  7947. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_popcnt_u32
  7948. FORCE_INLINE int _mm_popcnt_u32(unsigned int a)
  7949. {
  7950. #if defined(__aarch64__)
  7951. #if __has_builtin(__builtin_popcount)
  7952. return __builtin_popcount(a);
  7953. #else
  7954. return (int) vaddlv_u8(vcnt_u8(vcreate_u8((uint64_t) a)));
  7955. #endif
  7956. #else
  7957. uint32_t count = 0;
  7958. uint8x8_t input_val, count8x8_val;
  7959. uint16x4_t count16x4_val;
  7960. uint32x2_t count32x2_val;
  7961. input_val = vld1_u8((uint8_t *) &a);
  7962. count8x8_val = vcnt_u8(input_val);
  7963. count16x4_val = vpaddl_u8(count8x8_val);
  7964. count32x2_val = vpaddl_u16(count16x4_val);
  7965. vst1_u32(&count, count32x2_val);
  7966. return count;
  7967. #endif
  7968. }
  7969. // Count the number of bits set to 1 in unsigned 64-bit integer a, and
  7970. // return that count in dst.
  7971. // https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm_popcnt_u64
  7972. FORCE_INLINE int64_t _mm_popcnt_u64(uint64_t a)
  7973. {
  7974. #if defined(__aarch64__)
  7975. #if __has_builtin(__builtin_popcountll)
  7976. return __builtin_popcountll(a);
  7977. #else
  7978. return (int64_t) vaddlv_u8(vcnt_u8(vcreate_u8(a)));
  7979. #endif
  7980. #else
  7981. uint64_t count = 0;
  7982. uint8x8_t input_val, count8x8_val;
  7983. uint16x4_t count16x4_val;
  7984. uint32x2_t count32x2_val;
  7985. uint64x1_t count64x1_val;
  7986. input_val = vld1_u8((uint8_t *) &a);
  7987. count8x8_val = vcnt_u8(input_val);
  7988. count16x4_val = vpaddl_u8(count8x8_val);
  7989. count32x2_val = vpaddl_u16(count16x4_val);
  7990. count64x1_val = vpaddl_u32(count32x2_val);
  7991. vst1_u64(&count, count64x1_val);
  7992. return count;
  7993. #endif
  7994. }
  7995. FORCE_INLINE void _sse2neon_mm_set_denormals_zero_mode(unsigned int flag)
  7996. {
  7997. // AArch32 Advanced SIMD arithmetic always uses the Flush-to-zero setting,
  7998. // regardless of the value of the FZ bit.
  7999. union {
  8000. fpcr_bitfield field;
  8001. #if defined(__aarch64__)
  8002. uint64_t value;
  8003. #else
  8004. uint32_t value;
  8005. #endif
  8006. } r;
  8007. #if defined(__aarch64__)
  8008. asm volatile("mrs %0, FPCR" : "=r"(r.value)); /* read */
  8009. #else
  8010. asm volatile("vmrs %0, FPSCR" : "=r"(r.value)); /* read */
  8011. #endif
  8012. r.field.bit24 = (flag & _MM_DENORMALS_ZERO_MASK) == _MM_DENORMALS_ZERO_ON;
  8013. #if defined(__aarch64__)
  8014. asm volatile("msr FPCR, %0" ::"r"(r)); /* write */
  8015. #else
  8016. asm volatile("vmsr FPSCR, %0" ::"r"(r)); /* write */
  8017. #endif
  8018. }
  8019. #if defined(__GNUC__) || defined(__clang__)
  8020. #pragma pop_macro("ALIGN_STRUCT")
  8021. #pragma pop_macro("FORCE_INLINE")
  8022. #endif
  8023. #if defined(__GNUC__) && !defined(__clang__)
  8024. #pragma GCC pop_options
  8025. #endif
  8026. #endif