simd.h 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199
  1. // Definition of the public simd interfaces -*- C++ -*-
  2. // Copyright (C) 2020-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. #ifndef _GLIBCXX_EXPERIMENTAL_SIMD_H
  21. #define _GLIBCXX_EXPERIMENTAL_SIMD_H
  22. #if __cplusplus >= 201703L
  23. #include "simd_detail.h"
  24. #include "numeric_traits.h"
  25. #include <bit>
  26. #include <bitset>
  27. #ifdef _GLIBCXX_DEBUG_UB
  28. #include <cstdio> // for stderr
  29. #endif
  30. #include <cstring>
  31. #include <functional>
  32. #include <iosfwd>
  33. #include <utility>
  34. #if _GLIBCXX_SIMD_X86INTRIN
  35. #include <x86intrin.h>
  36. #elif _GLIBCXX_SIMD_HAVE_NEON
  37. #include <arm_neon.h>
  38. #endif
  39. /* There are several closely related types, with the following naming
  40. * convention:
  41. * _Tp: vectorizable (arithmetic) type (or any type)
  42. * _TV: __vector_type_t<_Tp, _Np>
  43. * _TW: _SimdWrapper<_Tp, _Np>
  44. * _TI: __intrinsic_type_t<_Tp, _Np>
  45. * _TVT: _VectorTraits<_TV> or _VectorTraits<_TW>
  46. * If one additional type is needed use _U instead of _T.
  47. * Otherwise use _T\d, _TV\d, _TW\d, TI\d, _TVT\d.
  48. *
  49. * More naming conventions:
  50. * _Ap or _Abi: An ABI tag from the simd_abi namespace
  51. * _Ip: often used for integer types with sizeof(_Ip) == sizeof(_Tp),
  52. * _IV, _IW as for _TV, _TW
  53. * _Np: number of elements (not bytes)
  54. * _Bytes: number of bytes
  55. *
  56. * Variable names:
  57. * __k: mask object (vector- or bitmask)
  58. */
  59. _GLIBCXX_SIMD_BEGIN_NAMESPACE
  60. #if !_GLIBCXX_SIMD_X86INTRIN
  61. using __m128 [[__gnu__::__vector_size__(16)]] = float;
  62. using __m128d [[__gnu__::__vector_size__(16)]] = double;
  63. using __m128i [[__gnu__::__vector_size__(16)]] = long long;
  64. using __m256 [[__gnu__::__vector_size__(32)]] = float;
  65. using __m256d [[__gnu__::__vector_size__(32)]] = double;
  66. using __m256i [[__gnu__::__vector_size__(32)]] = long long;
  67. using __m512 [[__gnu__::__vector_size__(64)]] = float;
  68. using __m512d [[__gnu__::__vector_size__(64)]] = double;
  69. using __m512i [[__gnu__::__vector_size__(64)]] = long long;
  70. #endif
  71. namespace simd_abi {
  72. // simd_abi forward declarations {{{
  73. // implementation details:
  74. struct _Scalar;
  75. template <int _Np>
  76. struct _Fixed;
  77. // There are two major ABIs that appear on different architectures.
  78. // Both have non-boolean values packed into an N Byte register
  79. // -> #elements = N / sizeof(T)
  80. // Masks differ:
  81. // 1. Use value vector registers for masks (all 0 or all 1)
  82. // 2. Use bitmasks (mask registers) with one bit per value in the corresponding
  83. // value vector
  84. //
  85. // Both can be partially used, masking off the rest when doing horizontal
  86. // operations or operations that can trap (e.g. FP_INVALID or integer division
  87. // by 0). This is encoded as the number of used bytes.
  88. template <int _UsedBytes>
  89. struct _VecBuiltin;
  90. template <int _UsedBytes>
  91. struct _VecBltnBtmsk;
  92. template <typename _Tp, int _Np>
  93. using _VecN = _VecBuiltin<sizeof(_Tp) * _Np>;
  94. template <int _UsedBytes = 16>
  95. using _Sse = _VecBuiltin<_UsedBytes>;
  96. template <int _UsedBytes = 32>
  97. using _Avx = _VecBuiltin<_UsedBytes>;
  98. template <int _UsedBytes = 64>
  99. using _Avx512 = _VecBltnBtmsk<_UsedBytes>;
  100. template <int _UsedBytes = 16>
  101. using _Neon = _VecBuiltin<_UsedBytes>;
  102. // implementation-defined:
  103. using __sse = _Sse<>;
  104. using __avx = _Avx<>;
  105. using __avx512 = _Avx512<>;
  106. using __neon = _Neon<>;
  107. using __neon128 = _Neon<16>;
  108. using __neon64 = _Neon<8>;
  109. // standard:
  110. template <typename _Tp, size_t _Np, typename...>
  111. struct deduce;
  112. template <int _Np>
  113. using fixed_size = _Fixed<_Np>;
  114. using scalar = _Scalar;
  115. // }}}
  116. } // namespace simd_abi
  117. // forward declarations is_simd(_mask), simd(_mask), simd_size {{{
  118. template <typename _Tp>
  119. struct is_simd;
  120. template <typename _Tp>
  121. struct is_simd_mask;
  122. template <typename _Tp, typename _Abi>
  123. class simd;
  124. template <typename _Tp, typename _Abi>
  125. class simd_mask;
  126. template <typename _Tp, typename _Abi>
  127. struct simd_size;
  128. // }}}
  129. // load/store flags {{{
  130. struct element_aligned_tag
  131. {
  132. template <typename _Tp, typename _Up = typename _Tp::value_type>
  133. static constexpr size_t _S_alignment = alignof(_Up);
  134. template <typename _Tp, typename _Up>
  135. _GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
  136. _S_apply(_Up* __ptr)
  137. { return __ptr; }
  138. };
  139. struct vector_aligned_tag
  140. {
  141. template <typename _Tp, typename _Up = typename _Tp::value_type>
  142. static constexpr size_t _S_alignment
  143. = std::__bit_ceil(sizeof(_Up) * _Tp::size());
  144. template <typename _Tp, typename _Up>
  145. _GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
  146. _S_apply(_Up* __ptr)
  147. {
  148. return static_cast<_Up*>(
  149. __builtin_assume_aligned(__ptr, _S_alignment<_Tp, _Up>));
  150. }
  151. };
  152. template <size_t _Np> struct overaligned_tag
  153. {
  154. template <typename _Tp, typename _Up = typename _Tp::value_type>
  155. static constexpr size_t _S_alignment = _Np;
  156. template <typename _Tp, typename _Up>
  157. _GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
  158. _S_apply(_Up* __ptr)
  159. { return static_cast<_Up*>(__builtin_assume_aligned(__ptr, _Np)); }
  160. };
  161. inline constexpr element_aligned_tag element_aligned = {};
  162. inline constexpr vector_aligned_tag vector_aligned = {};
  163. template <size_t _Np>
  164. inline constexpr overaligned_tag<_Np> overaligned = {};
  165. // }}}
  166. template <size_t _Xp>
  167. using _SizeConstant = integral_constant<size_t, _Xp>;
  168. namespace __detail
  169. {
  170. struct _Minimum
  171. {
  172. template <typename _Tp>
  173. _GLIBCXX_SIMD_INTRINSIC constexpr
  174. _Tp
  175. operator()(_Tp __a, _Tp __b) const
  176. {
  177. using std::min;
  178. return min(__a, __b);
  179. }
  180. };
  181. struct _Maximum
  182. {
  183. template <typename _Tp>
  184. _GLIBCXX_SIMD_INTRINSIC constexpr
  185. _Tp
  186. operator()(_Tp __a, _Tp __b) const
  187. {
  188. using std::max;
  189. return max(__a, __b);
  190. }
  191. };
  192. } // namespace __detail
  193. // unrolled/pack execution helpers
  194. // __execute_n_times{{{
  195. template <typename _Fp, size_t... _I>
  196. _GLIBCXX_SIMD_INTRINSIC constexpr void
  197. __execute_on_index_sequence(_Fp&& __f, index_sequence<_I...>)
  198. { ((void)__f(_SizeConstant<_I>()), ...); }
  199. template <typename _Fp>
  200. _GLIBCXX_SIMD_INTRINSIC constexpr void
  201. __execute_on_index_sequence(_Fp&&, index_sequence<>)
  202. { }
  203. template <size_t _Np, typename _Fp>
  204. _GLIBCXX_SIMD_INTRINSIC constexpr void
  205. __execute_n_times(_Fp&& __f)
  206. {
  207. __execute_on_index_sequence(static_cast<_Fp&&>(__f),
  208. make_index_sequence<_Np>{});
  209. }
  210. // }}}
  211. // __generate_from_n_evaluations{{{
  212. template <typename _R, typename _Fp, size_t... _I>
  213. _GLIBCXX_SIMD_INTRINSIC constexpr _R
  214. __execute_on_index_sequence_with_return(_Fp&& __f, index_sequence<_I...>)
  215. { return _R{__f(_SizeConstant<_I>())...}; }
  216. template <size_t _Np, typename _R, typename _Fp>
  217. _GLIBCXX_SIMD_INTRINSIC constexpr _R
  218. __generate_from_n_evaluations(_Fp&& __f)
  219. {
  220. return __execute_on_index_sequence_with_return<_R>(
  221. static_cast<_Fp&&>(__f), make_index_sequence<_Np>{});
  222. }
  223. // }}}
  224. // __call_with_n_evaluations{{{
  225. template <size_t... _I, typename _F0, typename _FArgs>
  226. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  227. __call_with_n_evaluations(index_sequence<_I...>, _F0&& __f0, _FArgs&& __fargs)
  228. { return __f0(__fargs(_SizeConstant<_I>())...); }
  229. template <size_t _Np, typename _F0, typename _FArgs>
  230. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  231. __call_with_n_evaluations(_F0&& __f0, _FArgs&& __fargs)
  232. {
  233. return __call_with_n_evaluations(make_index_sequence<_Np>{},
  234. static_cast<_F0&&>(__f0),
  235. static_cast<_FArgs&&>(__fargs));
  236. }
  237. // }}}
  238. // __call_with_subscripts{{{
  239. template <size_t _First = 0, size_t... _It, typename _Tp, typename _Fp>
  240. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  241. __call_with_subscripts(_Tp&& __x, index_sequence<_It...>, _Fp&& __fun)
  242. { return __fun(__x[_First + _It]...); }
  243. template <size_t _Np, size_t _First = 0, typename _Tp, typename _Fp>
  244. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  245. __call_with_subscripts(_Tp&& __x, _Fp&& __fun)
  246. {
  247. return __call_with_subscripts<_First>(static_cast<_Tp&&>(__x),
  248. make_index_sequence<_Np>(),
  249. static_cast<_Fp&&>(__fun));
  250. }
  251. // }}}
  252. // vvv ---- type traits ---- vvv
  253. // integer type aliases{{{
  254. using _UChar = unsigned char;
  255. using _SChar = signed char;
  256. using _UShort = unsigned short;
  257. using _UInt = unsigned int;
  258. using _ULong = unsigned long;
  259. using _ULLong = unsigned long long;
  260. using _LLong = long long;
  261. //}}}
  262. // __first_of_pack{{{
  263. template <typename _T0, typename...>
  264. struct __first_of_pack
  265. { using type = _T0; };
  266. template <typename... _Ts>
  267. using __first_of_pack_t = typename __first_of_pack<_Ts...>::type;
  268. //}}}
  269. // __value_type_or_identity_t {{{
  270. template <typename _Tp>
  271. typename _Tp::value_type
  272. __value_type_or_identity_impl(int);
  273. template <typename _Tp>
  274. _Tp
  275. __value_type_or_identity_impl(float);
  276. template <typename _Tp>
  277. using __value_type_or_identity_t
  278. = decltype(__value_type_or_identity_impl<_Tp>(int()));
  279. // }}}
  280. // __is_vectorizable {{{
  281. template <typename _Tp>
  282. struct __is_vectorizable : public is_arithmetic<_Tp> {};
  283. template <>
  284. struct __is_vectorizable<bool> : public false_type {};
  285. template <typename _Tp>
  286. inline constexpr bool __is_vectorizable_v = __is_vectorizable<_Tp>::value;
  287. // Deduces to a vectorizable type
  288. template <typename _Tp, typename = enable_if_t<__is_vectorizable_v<_Tp>>>
  289. using _Vectorizable = _Tp;
  290. // }}}
  291. // _LoadStorePtr / __is_possible_loadstore_conversion {{{
  292. template <typename _Ptr, typename _ValueType>
  293. struct __is_possible_loadstore_conversion
  294. : conjunction<__is_vectorizable<_Ptr>, __is_vectorizable<_ValueType>> {};
  295. template <>
  296. struct __is_possible_loadstore_conversion<bool, bool> : true_type {};
  297. // Deduces to a type allowed for load/store with the given value type.
  298. template <typename _Ptr, typename _ValueType,
  299. typename = enable_if_t<
  300. __is_possible_loadstore_conversion<_Ptr, _ValueType>::value>>
  301. using _LoadStorePtr = _Ptr;
  302. // }}}
  303. // __is_bitmask{{{
  304. template <typename _Tp, typename = void_t<>>
  305. struct __is_bitmask : false_type {};
  306. template <typename _Tp>
  307. inline constexpr bool __is_bitmask_v = __is_bitmask<_Tp>::value;
  308. // the __mmaskXX case:
  309. template <typename _Tp>
  310. struct __is_bitmask<_Tp,
  311. void_t<decltype(declval<unsigned&>() = declval<_Tp>() & 1u)>>
  312. : true_type {};
  313. // }}}
  314. // __int_for_sizeof{{{
  315. #pragma GCC diagnostic push
  316. #pragma GCC diagnostic ignored "-Wpedantic"
  317. template <size_t _Bytes>
  318. constexpr auto
  319. __int_for_sizeof()
  320. {
  321. if constexpr (_Bytes == sizeof(int))
  322. return int();
  323. #ifdef __clang__
  324. else if constexpr (_Bytes == sizeof(char))
  325. return char();
  326. #else
  327. else if constexpr (_Bytes == sizeof(_SChar))
  328. return _SChar();
  329. #endif
  330. else if constexpr (_Bytes == sizeof(short))
  331. return short();
  332. #ifndef __clang__
  333. else if constexpr (_Bytes == sizeof(long))
  334. return long();
  335. #endif
  336. else if constexpr (_Bytes == sizeof(_LLong))
  337. return _LLong();
  338. #ifdef __SIZEOF_INT128__
  339. else if constexpr (_Bytes == sizeof(__int128))
  340. return __int128();
  341. #endif // __SIZEOF_INT128__
  342. else if constexpr (_Bytes % sizeof(int) == 0)
  343. {
  344. constexpr size_t _Np = _Bytes / sizeof(int);
  345. struct _Ip
  346. {
  347. int _M_data[_Np];
  348. _GLIBCXX_SIMD_INTRINSIC constexpr _Ip
  349. operator&(_Ip __rhs) const
  350. {
  351. return __generate_from_n_evaluations<_Np, _Ip>(
  352. [&](auto __i) { return __rhs._M_data[__i] & _M_data[__i]; });
  353. }
  354. _GLIBCXX_SIMD_INTRINSIC constexpr _Ip
  355. operator|(_Ip __rhs) const
  356. {
  357. return __generate_from_n_evaluations<_Np, _Ip>(
  358. [&](auto __i) { return __rhs._M_data[__i] | _M_data[__i]; });
  359. }
  360. _GLIBCXX_SIMD_INTRINSIC constexpr _Ip
  361. operator^(_Ip __rhs) const
  362. {
  363. return __generate_from_n_evaluations<_Np, _Ip>(
  364. [&](auto __i) { return __rhs._M_data[__i] ^ _M_data[__i]; });
  365. }
  366. _GLIBCXX_SIMD_INTRINSIC constexpr _Ip
  367. operator~() const
  368. {
  369. return __generate_from_n_evaluations<_Np, _Ip>(
  370. [&](auto __i) { return ~_M_data[__i]; });
  371. }
  372. };
  373. return _Ip{};
  374. }
  375. else
  376. static_assert(_Bytes != _Bytes, "this should be unreachable");
  377. }
  378. #pragma GCC diagnostic pop
  379. template <typename _Tp>
  380. using __int_for_sizeof_t = decltype(__int_for_sizeof<sizeof(_Tp)>());
  381. template <size_t _Np>
  382. using __int_with_sizeof_t = decltype(__int_for_sizeof<_Np>());
  383. // }}}
  384. // __is_fixed_size_abi{{{
  385. template <typename _Tp>
  386. struct __is_fixed_size_abi : false_type {};
  387. template <int _Np>
  388. struct __is_fixed_size_abi<simd_abi::fixed_size<_Np>> : true_type {};
  389. template <typename _Tp>
  390. inline constexpr bool __is_fixed_size_abi_v = __is_fixed_size_abi<_Tp>::value;
  391. // }}}
  392. // constexpr feature detection{{{
  393. constexpr inline bool __have_mmx = _GLIBCXX_SIMD_HAVE_MMX;
  394. constexpr inline bool __have_sse = _GLIBCXX_SIMD_HAVE_SSE;
  395. constexpr inline bool __have_sse2 = _GLIBCXX_SIMD_HAVE_SSE2;
  396. constexpr inline bool __have_sse3 = _GLIBCXX_SIMD_HAVE_SSE3;
  397. constexpr inline bool __have_ssse3 = _GLIBCXX_SIMD_HAVE_SSSE3;
  398. constexpr inline bool __have_sse4_1 = _GLIBCXX_SIMD_HAVE_SSE4_1;
  399. constexpr inline bool __have_sse4_2 = _GLIBCXX_SIMD_HAVE_SSE4_2;
  400. constexpr inline bool __have_xop = _GLIBCXX_SIMD_HAVE_XOP;
  401. constexpr inline bool __have_avx = _GLIBCXX_SIMD_HAVE_AVX;
  402. constexpr inline bool __have_avx2 = _GLIBCXX_SIMD_HAVE_AVX2;
  403. constexpr inline bool __have_bmi = _GLIBCXX_SIMD_HAVE_BMI1;
  404. constexpr inline bool __have_bmi2 = _GLIBCXX_SIMD_HAVE_BMI2;
  405. constexpr inline bool __have_lzcnt = _GLIBCXX_SIMD_HAVE_LZCNT;
  406. constexpr inline bool __have_sse4a = _GLIBCXX_SIMD_HAVE_SSE4A;
  407. constexpr inline bool __have_fma = _GLIBCXX_SIMD_HAVE_FMA;
  408. constexpr inline bool __have_fma4 = _GLIBCXX_SIMD_HAVE_FMA4;
  409. constexpr inline bool __have_f16c = _GLIBCXX_SIMD_HAVE_F16C;
  410. constexpr inline bool __have_popcnt = _GLIBCXX_SIMD_HAVE_POPCNT;
  411. constexpr inline bool __have_avx512f = _GLIBCXX_SIMD_HAVE_AVX512F;
  412. constexpr inline bool __have_avx512dq = _GLIBCXX_SIMD_HAVE_AVX512DQ;
  413. constexpr inline bool __have_avx512vl = _GLIBCXX_SIMD_HAVE_AVX512VL;
  414. constexpr inline bool __have_avx512bw = _GLIBCXX_SIMD_HAVE_AVX512BW;
  415. constexpr inline bool __have_avx512dq_vl = __have_avx512dq && __have_avx512vl;
  416. constexpr inline bool __have_avx512bw_vl = __have_avx512bw && __have_avx512vl;
  417. constexpr inline bool __have_neon = _GLIBCXX_SIMD_HAVE_NEON;
  418. constexpr inline bool __have_neon_a32 = _GLIBCXX_SIMD_HAVE_NEON_A32;
  419. constexpr inline bool __have_neon_a64 = _GLIBCXX_SIMD_HAVE_NEON_A64;
  420. constexpr inline bool __support_neon_float =
  421. #if defined __GCC_IEC_559
  422. __GCC_IEC_559 == 0;
  423. #elif defined __FAST_MATH__
  424. true;
  425. #else
  426. false;
  427. #endif
  428. #ifdef _ARCH_PWR10
  429. constexpr inline bool __have_power10vec = true;
  430. #else
  431. constexpr inline bool __have_power10vec = false;
  432. #endif
  433. #ifdef __POWER9_VECTOR__
  434. constexpr inline bool __have_power9vec = true;
  435. #else
  436. constexpr inline bool __have_power9vec = false;
  437. #endif
  438. #if defined __POWER8_VECTOR__
  439. constexpr inline bool __have_power8vec = true;
  440. #else
  441. constexpr inline bool __have_power8vec = __have_power9vec;
  442. #endif
  443. #if defined __VSX__
  444. constexpr inline bool __have_power_vsx = true;
  445. #else
  446. constexpr inline bool __have_power_vsx = __have_power8vec;
  447. #endif
  448. #if defined __ALTIVEC__
  449. constexpr inline bool __have_power_vmx = true;
  450. #else
  451. constexpr inline bool __have_power_vmx = __have_power_vsx;
  452. #endif
  453. // }}}
  454. // __is_scalar_abi {{{
  455. template <typename _Abi>
  456. constexpr bool
  457. __is_scalar_abi()
  458. { return is_same_v<simd_abi::scalar, _Abi>; }
  459. // }}}
  460. // __abi_bytes_v {{{
  461. template <template <int> class _Abi, int _Bytes>
  462. constexpr int
  463. __abi_bytes_impl(_Abi<_Bytes>*)
  464. { return _Bytes; }
  465. template <typename _Tp>
  466. constexpr int
  467. __abi_bytes_impl(_Tp*)
  468. { return -1; }
  469. template <typename _Abi>
  470. inline constexpr int __abi_bytes_v
  471. = __abi_bytes_impl(static_cast<_Abi*>(nullptr));
  472. // }}}
  473. // __is_builtin_bitmask_abi {{{
  474. template <typename _Abi>
  475. constexpr bool
  476. __is_builtin_bitmask_abi()
  477. { return is_same_v<simd_abi::_VecBltnBtmsk<__abi_bytes_v<_Abi>>, _Abi>; }
  478. // }}}
  479. // __is_sse_abi {{{
  480. template <typename _Abi>
  481. constexpr bool
  482. __is_sse_abi()
  483. {
  484. constexpr auto _Bytes = __abi_bytes_v<_Abi>;
  485. return _Bytes <= 16 && is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
  486. }
  487. // }}}
  488. // __is_avx_abi {{{
  489. template <typename _Abi>
  490. constexpr bool
  491. __is_avx_abi()
  492. {
  493. constexpr auto _Bytes = __abi_bytes_v<_Abi>;
  494. return _Bytes > 16 && _Bytes <= 32
  495. && is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
  496. }
  497. // }}}
  498. // __is_avx512_abi {{{
  499. template <typename _Abi>
  500. constexpr bool
  501. __is_avx512_abi()
  502. {
  503. constexpr auto _Bytes = __abi_bytes_v<_Abi>;
  504. return _Bytes <= 64 && is_same_v<simd_abi::_Avx512<_Bytes>, _Abi>;
  505. }
  506. // }}}
  507. // __is_neon_abi {{{
  508. template <typename _Abi>
  509. constexpr bool
  510. __is_neon_abi()
  511. {
  512. constexpr auto _Bytes = __abi_bytes_v<_Abi>;
  513. return _Bytes <= 16 && is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
  514. }
  515. // }}}
  516. // __make_dependent_t {{{
  517. template <typename, typename _Up>
  518. struct __make_dependent
  519. { using type = _Up; };
  520. template <typename _Tp, typename _Up>
  521. using __make_dependent_t = typename __make_dependent<_Tp, _Up>::type;
  522. // }}}
  523. // ^^^ ---- type traits ---- ^^^
  524. // __invoke_ub{{{
  525. template <typename... _Args>
  526. [[noreturn]] _GLIBCXX_SIMD_ALWAYS_INLINE void
  527. __invoke_ub([[maybe_unused]] const char* __msg,
  528. [[maybe_unused]] const _Args&... __args)
  529. {
  530. #ifdef _GLIBCXX_DEBUG_UB
  531. __builtin_fprintf(stderr, __msg, __args...);
  532. __builtin_trap();
  533. #else
  534. __builtin_unreachable();
  535. #endif
  536. }
  537. // }}}
  538. // __assert_unreachable{{{
  539. template <typename _Tp>
  540. struct __assert_unreachable
  541. { static_assert(!is_same_v<_Tp, _Tp>, "this should be unreachable"); };
  542. // }}}
  543. // __size_or_zero_v {{{
  544. template <typename _Tp, typename _Ap, size_t _Np = simd_size<_Tp, _Ap>::value>
  545. constexpr size_t
  546. __size_or_zero_dispatch(int)
  547. { return _Np; }
  548. template <typename _Tp, typename _Ap>
  549. constexpr size_t
  550. __size_or_zero_dispatch(float)
  551. { return 0; }
  552. template <typename _Tp, typename _Ap>
  553. inline constexpr size_t __size_or_zero_v
  554. = __size_or_zero_dispatch<_Tp, _Ap>(0);
  555. // }}}
  556. // __div_roundup {{{
  557. inline constexpr size_t
  558. __div_roundup(size_t __a, size_t __b)
  559. { return (__a + __b - 1) / __b; }
  560. // }}}
  561. // _ExactBool{{{
  562. class _ExactBool
  563. {
  564. const bool _M_data;
  565. public:
  566. _GLIBCXX_SIMD_INTRINSIC constexpr _ExactBool(bool __b) : _M_data(__b) {}
  567. _ExactBool(int) = delete;
  568. _GLIBCXX_SIMD_INTRINSIC constexpr operator bool() const { return _M_data; }
  569. };
  570. // }}}
  571. // __may_alias{{{
  572. /**@internal
  573. * Helper __may_alias<_Tp> that turns _Tp into the type to be used for an
  574. * aliasing pointer. This adds the __may_alias attribute to _Tp (with compilers
  575. * that support it).
  576. */
  577. template <typename _Tp>
  578. using __may_alias [[__gnu__::__may_alias__]] = _Tp;
  579. // }}}
  580. // _UnsupportedBase {{{
  581. // simd and simd_mask base for unsupported <_Tp, _Abi>
  582. struct _UnsupportedBase
  583. {
  584. _UnsupportedBase() = delete;
  585. _UnsupportedBase(const _UnsupportedBase&) = delete;
  586. _UnsupportedBase& operator=(const _UnsupportedBase&) = delete;
  587. ~_UnsupportedBase() = delete;
  588. };
  589. // }}}
  590. // _InvalidTraits {{{
  591. /**
  592. * @internal
  593. * Defines the implementation of __a given <_Tp, _Abi>.
  594. *
  595. * Implementations must ensure that only valid <_Tp, _Abi> instantiations are
  596. * possible. Static assertions in the type definition do not suffice. It is
  597. * important that SFINAE works.
  598. */
  599. struct _InvalidTraits
  600. {
  601. using _IsValid = false_type;
  602. using _SimdBase = _UnsupportedBase;
  603. using _MaskBase = _UnsupportedBase;
  604. static constexpr size_t _S_full_size = 0;
  605. static constexpr bool _S_is_partial = false;
  606. static constexpr size_t _S_simd_align = 1;
  607. struct _SimdImpl;
  608. struct _SimdMember {};
  609. struct _SimdCastType;
  610. static constexpr size_t _S_mask_align = 1;
  611. struct _MaskImpl;
  612. struct _MaskMember {};
  613. struct _MaskCastType;
  614. };
  615. // }}}
  616. // _SimdTraits {{{
  617. template <typename _Tp, typename _Abi, typename = void_t<>>
  618. struct _SimdTraits : _InvalidTraits {};
  619. // }}}
  620. // __private_init, __bitset_init{{{
  621. /**
  622. * @internal
  623. * Tag used for private init constructor of simd and simd_mask
  624. */
  625. inline constexpr struct _PrivateInit {} __private_init = {};
  626. inline constexpr struct _BitsetInit {} __bitset_init = {};
  627. // }}}
  628. // __is_narrowing_conversion<_From, _To>{{{
  629. template <typename _From, typename _To, bool = is_arithmetic_v<_From>,
  630. bool = is_arithmetic_v<_To>>
  631. struct __is_narrowing_conversion;
  632. // ignore "signed/unsigned mismatch" in the following trait.
  633. // The implicit conversions will do the right thing here.
  634. template <typename _From, typename _To>
  635. struct __is_narrowing_conversion<_From, _To, true, true>
  636. : public __bool_constant<(
  637. __digits_v<_From> > __digits_v<_To>
  638. || __finite_max_v<_From> > __finite_max_v<_To>
  639. || __finite_min_v<_From> < __finite_min_v<_To>
  640. || (is_signed_v<_From> && is_unsigned_v<_To>))> {};
  641. template <typename _Tp>
  642. struct __is_narrowing_conversion<_Tp, bool, true, true>
  643. : public true_type {};
  644. template <>
  645. struct __is_narrowing_conversion<bool, bool, true, true>
  646. : public false_type {};
  647. template <typename _Tp>
  648. struct __is_narrowing_conversion<_Tp, _Tp, true, true>
  649. : public false_type {};
  650. template <typename _From, typename _To>
  651. struct __is_narrowing_conversion<_From, _To, false, true>
  652. : public negation<is_convertible<_From, _To>> {};
  653. // }}}
  654. // __converts_to_higher_integer_rank{{{
  655. template <typename _From, typename _To, bool = (sizeof(_From) < sizeof(_To))>
  656. struct __converts_to_higher_integer_rank : public true_type {};
  657. // this may fail for char -> short if sizeof(char) == sizeof(short)
  658. template <typename _From, typename _To>
  659. struct __converts_to_higher_integer_rank<_From, _To, false>
  660. : public is_same<decltype(declval<_From>() + declval<_To>()), _To> {};
  661. // }}}
  662. // __data(simd/simd_mask) {{{
  663. template <typename _Tp, typename _Ap>
  664. _GLIBCXX_SIMD_INTRINSIC constexpr const auto&
  665. __data(const simd<_Tp, _Ap>& __x);
  666. template <typename _Tp, typename _Ap>
  667. _GLIBCXX_SIMD_INTRINSIC constexpr auto&
  668. __data(simd<_Tp, _Ap>& __x);
  669. template <typename _Tp, typename _Ap>
  670. _GLIBCXX_SIMD_INTRINSIC constexpr const auto&
  671. __data(const simd_mask<_Tp, _Ap>& __x);
  672. template <typename _Tp, typename _Ap>
  673. _GLIBCXX_SIMD_INTRINSIC constexpr auto&
  674. __data(simd_mask<_Tp, _Ap>& __x);
  675. // }}}
  676. // _SimdConverter {{{
  677. template <typename _FromT, typename _FromA, typename _ToT, typename _ToA,
  678. typename = void>
  679. struct _SimdConverter;
  680. template <typename _Tp, typename _Ap>
  681. struct _SimdConverter<_Tp, _Ap, _Tp, _Ap, void>
  682. {
  683. template <typename _Up>
  684. _GLIBCXX_SIMD_INTRINSIC const _Up&
  685. operator()(const _Up& __x)
  686. { return __x; }
  687. };
  688. // }}}
  689. // __to_value_type_or_member_type {{{
  690. template <typename _V>
  691. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  692. __to_value_type_or_member_type(const _V& __x) -> decltype(__data(__x))
  693. { return __data(__x); }
  694. template <typename _V>
  695. _GLIBCXX_SIMD_INTRINSIC constexpr const typename _V::value_type&
  696. __to_value_type_or_member_type(const typename _V::value_type& __x)
  697. { return __x; }
  698. // }}}
  699. // __bool_storage_member_type{{{
  700. template <size_t _Size>
  701. struct __bool_storage_member_type;
  702. template <size_t _Size>
  703. using __bool_storage_member_type_t =
  704. typename __bool_storage_member_type<_Size>::type;
  705. // }}}
  706. // _SimdTuple {{{
  707. // why not tuple?
  708. // 1. tuple gives no guarantee about the storage order, but I require
  709. // storage
  710. // equivalent to array<_Tp, _Np>
  711. // 2. direct access to the element type (first template argument)
  712. // 3. enforces equal element type, only different _Abi types are allowed
  713. template <typename _Tp, typename... _Abis>
  714. struct _SimdTuple;
  715. //}}}
  716. // __fixed_size_storage_t {{{
  717. template <typename _Tp, int _Np>
  718. struct __fixed_size_storage;
  719. template <typename _Tp, int _Np>
  720. using __fixed_size_storage_t = typename __fixed_size_storage<_Tp, _Np>::type;
  721. // }}}
  722. // _SimdWrapper fwd decl{{{
  723. template <typename _Tp, size_t _Size, typename = void_t<>>
  724. struct _SimdWrapper;
  725. template <typename _Tp>
  726. using _SimdWrapper8 = _SimdWrapper<_Tp, 8 / sizeof(_Tp)>;
  727. template <typename _Tp>
  728. using _SimdWrapper16 = _SimdWrapper<_Tp, 16 / sizeof(_Tp)>;
  729. template <typename _Tp>
  730. using _SimdWrapper32 = _SimdWrapper<_Tp, 32 / sizeof(_Tp)>;
  731. template <typename _Tp>
  732. using _SimdWrapper64 = _SimdWrapper<_Tp, 64 / sizeof(_Tp)>;
  733. // }}}
  734. // __is_simd_wrapper {{{
  735. template <typename _Tp>
  736. struct __is_simd_wrapper : false_type {};
  737. template <typename _Tp, size_t _Np>
  738. struct __is_simd_wrapper<_SimdWrapper<_Tp, _Np>> : true_type {};
  739. template <typename _Tp>
  740. inline constexpr bool __is_simd_wrapper_v = __is_simd_wrapper<_Tp>::value;
  741. // }}}
  742. // _BitOps {{{
  743. struct _BitOps
  744. {
  745. // _S_bit_iteration {{{
  746. template <typename _Tp, typename _Fp>
  747. static void
  748. _S_bit_iteration(_Tp __mask, _Fp&& __f)
  749. {
  750. static_assert(sizeof(_ULLong) >= sizeof(_Tp));
  751. conditional_t<sizeof(_Tp) <= sizeof(_UInt), _UInt, _ULLong> __k;
  752. if constexpr (is_convertible_v<_Tp, decltype(__k)>)
  753. __k = __mask;
  754. else
  755. __k = __mask.to_ullong();
  756. while(__k)
  757. {
  758. __f(std::__countr_zero(__k));
  759. __k &= (__k - 1);
  760. }
  761. }
  762. //}}}
  763. };
  764. //}}}
  765. // __increment, __decrement {{{
  766. template <typename _Tp = void>
  767. struct __increment
  768. { constexpr _Tp operator()(_Tp __a) const { return ++__a; } };
  769. template <>
  770. struct __increment<void>
  771. {
  772. template <typename _Tp>
  773. constexpr _Tp
  774. operator()(_Tp __a) const
  775. { return ++__a; }
  776. };
  777. template <typename _Tp = void>
  778. struct __decrement
  779. { constexpr _Tp operator()(_Tp __a) const { return --__a; } };
  780. template <>
  781. struct __decrement<void>
  782. {
  783. template <typename _Tp>
  784. constexpr _Tp
  785. operator()(_Tp __a) const
  786. { return --__a; }
  787. };
  788. // }}}
  789. // _ValuePreserving(OrInt) {{{
  790. template <typename _From, typename _To,
  791. typename = enable_if_t<negation<
  792. __is_narrowing_conversion<__remove_cvref_t<_From>, _To>>::value>>
  793. using _ValuePreserving = _From;
  794. template <typename _From, typename _To,
  795. typename _DecayedFrom = __remove_cvref_t<_From>,
  796. typename = enable_if_t<conjunction<
  797. is_convertible<_From, _To>,
  798. disjunction<
  799. is_same<_DecayedFrom, _To>, is_same<_DecayedFrom, int>,
  800. conjunction<is_same<_DecayedFrom, _UInt>, is_unsigned<_To>>,
  801. negation<__is_narrowing_conversion<_DecayedFrom, _To>>>>::value>>
  802. using _ValuePreservingOrInt = _From;
  803. // }}}
  804. // __intrinsic_type {{{
  805. template <typename _Tp, size_t _Bytes, typename = void_t<>>
  806. struct __intrinsic_type;
  807. template <typename _Tp, size_t _Size>
  808. using __intrinsic_type_t =
  809. typename __intrinsic_type<_Tp, _Size * sizeof(_Tp)>::type;
  810. template <typename _Tp>
  811. using __intrinsic_type2_t = typename __intrinsic_type<_Tp, 2>::type;
  812. template <typename _Tp>
  813. using __intrinsic_type4_t = typename __intrinsic_type<_Tp, 4>::type;
  814. template <typename _Tp>
  815. using __intrinsic_type8_t = typename __intrinsic_type<_Tp, 8>::type;
  816. template <typename _Tp>
  817. using __intrinsic_type16_t = typename __intrinsic_type<_Tp, 16>::type;
  818. template <typename _Tp>
  819. using __intrinsic_type32_t = typename __intrinsic_type<_Tp, 32>::type;
  820. template <typename _Tp>
  821. using __intrinsic_type64_t = typename __intrinsic_type<_Tp, 64>::type;
  822. // }}}
  823. // _BitMask {{{
  824. template <size_t _Np, bool _Sanitized = false>
  825. struct _BitMask;
  826. template <size_t _Np, bool _Sanitized>
  827. struct __is_bitmask<_BitMask<_Np, _Sanitized>, void> : true_type {};
  828. template <size_t _Np>
  829. using _SanitizedBitMask = _BitMask<_Np, true>;
  830. template <size_t _Np, bool _Sanitized>
  831. struct _BitMask
  832. {
  833. static_assert(_Np > 0);
  834. static constexpr size_t _NBytes = __div_roundup(_Np, __CHAR_BIT__);
  835. using _Tp = conditional_t<_Np == 1, bool,
  836. make_unsigned_t<__int_with_sizeof_t<std::min(
  837. sizeof(_ULLong), std::__bit_ceil(_NBytes))>>>;
  838. static constexpr int _S_array_size = __div_roundup(_NBytes, sizeof(_Tp));
  839. _Tp _M_bits[_S_array_size];
  840. static constexpr int _S_unused_bits
  841. = _Np == 1 ? 0 : _S_array_size * sizeof(_Tp) * __CHAR_BIT__ - _Np;
  842. static constexpr _Tp _S_bitmask = +_Tp(~_Tp()) >> _S_unused_bits;
  843. constexpr _BitMask() noexcept = default;
  844. constexpr _BitMask(unsigned long long __x) noexcept
  845. : _M_bits{static_cast<_Tp>(__x)} {}
  846. _BitMask(bitset<_Np> __x) noexcept : _BitMask(__x.to_ullong()) {}
  847. constexpr _BitMask(const _BitMask&) noexcept = default;
  848. template <bool _RhsSanitized, typename = enable_if_t<_RhsSanitized == false
  849. && _Sanitized == true>>
  850. constexpr _BitMask(const _BitMask<_Np, _RhsSanitized>& __rhs) noexcept
  851. : _BitMask(__rhs._M_sanitized()) {}
  852. constexpr operator _SimdWrapper<bool, _Np>() const noexcept
  853. {
  854. static_assert(_S_array_size == 1);
  855. return _M_bits[0];
  856. }
  857. // precondition: is sanitized
  858. constexpr _Tp
  859. _M_to_bits() const noexcept
  860. {
  861. static_assert(_S_array_size == 1);
  862. return _M_bits[0];
  863. }
  864. // precondition: is sanitized
  865. constexpr unsigned long long
  866. to_ullong() const noexcept
  867. {
  868. static_assert(_S_array_size == 1);
  869. return _M_bits[0];
  870. }
  871. // precondition: is sanitized
  872. constexpr unsigned long
  873. to_ulong() const noexcept
  874. {
  875. static_assert(_S_array_size == 1);
  876. return _M_bits[0];
  877. }
  878. constexpr bitset<_Np>
  879. _M_to_bitset() const noexcept
  880. {
  881. static_assert(_S_array_size == 1);
  882. return _M_bits[0];
  883. }
  884. constexpr decltype(auto)
  885. _M_sanitized() const noexcept
  886. {
  887. if constexpr (_Sanitized)
  888. return *this;
  889. else if constexpr (_Np == 1)
  890. return _SanitizedBitMask<_Np>(_M_bits[0]);
  891. else
  892. {
  893. _SanitizedBitMask<_Np> __r = {};
  894. for (int __i = 0; __i < _S_array_size; ++__i)
  895. __r._M_bits[__i] = _M_bits[__i];
  896. if constexpr (_S_unused_bits > 0)
  897. __r._M_bits[_S_array_size - 1] &= _S_bitmask;
  898. return __r;
  899. }
  900. }
  901. template <size_t _Mp, bool _LSanitized>
  902. constexpr _BitMask<_Np + _Mp, _Sanitized>
  903. _M_prepend(_BitMask<_Mp, _LSanitized> __lsb) const noexcept
  904. {
  905. constexpr size_t _RN = _Np + _Mp;
  906. using _Rp = _BitMask<_RN, _Sanitized>;
  907. if constexpr (_Rp::_S_array_size == 1)
  908. {
  909. _Rp __r{{_M_bits[0]}};
  910. __r._M_bits[0] <<= _Mp;
  911. __r._M_bits[0] |= __lsb._M_sanitized()._M_bits[0];
  912. return __r;
  913. }
  914. else
  915. __assert_unreachable<_Rp>();
  916. }
  917. // Return a new _BitMask with size _NewSize while dropping _DropLsb least
  918. // significant bits. If the operation implicitly produces a sanitized bitmask,
  919. // the result type will have _Sanitized set.
  920. template <size_t _DropLsb, size_t _NewSize = _Np - _DropLsb>
  921. constexpr auto
  922. _M_extract() const noexcept
  923. {
  924. static_assert(_Np > _DropLsb);
  925. static_assert(_DropLsb + _NewSize <= sizeof(_ULLong) * __CHAR_BIT__,
  926. "not implemented for bitmasks larger than one ullong");
  927. if constexpr (_NewSize == 1)
  928. // must sanitize because the return _Tp is bool
  929. return _SanitizedBitMask<1>(_M_bits[0] & (_Tp(1) << _DropLsb));
  930. else
  931. return _BitMask<_NewSize,
  932. ((_NewSize + _DropLsb == sizeof(_Tp) * __CHAR_BIT__
  933. && _NewSize + _DropLsb <= _Np)
  934. || ((_Sanitized || _Np == sizeof(_Tp) * __CHAR_BIT__)
  935. && _NewSize + _DropLsb >= _Np))>(_M_bits[0]
  936. >> _DropLsb);
  937. }
  938. // True if all bits are set. Implicitly sanitizes if _Sanitized == false.
  939. constexpr bool
  940. all() const noexcept
  941. {
  942. if constexpr (_Np == 1)
  943. return _M_bits[0];
  944. else if constexpr (!_Sanitized)
  945. return _M_sanitized().all();
  946. else
  947. {
  948. constexpr _Tp __allbits = ~_Tp();
  949. for (int __i = 0; __i < _S_array_size - 1; ++__i)
  950. if (_M_bits[__i] != __allbits)
  951. return false;
  952. return _M_bits[_S_array_size - 1] == _S_bitmask;
  953. }
  954. }
  955. // True if at least one bit is set. Implicitly sanitizes if _Sanitized ==
  956. // false.
  957. constexpr bool
  958. any() const noexcept
  959. {
  960. if constexpr (_Np == 1)
  961. return _M_bits[0];
  962. else if constexpr (!_Sanitized)
  963. return _M_sanitized().any();
  964. else
  965. {
  966. for (int __i = 0; __i < _S_array_size - 1; ++__i)
  967. if (_M_bits[__i] != 0)
  968. return true;
  969. return _M_bits[_S_array_size - 1] != 0;
  970. }
  971. }
  972. // True if no bit is set. Implicitly sanitizes if _Sanitized == false.
  973. constexpr bool
  974. none() const noexcept
  975. {
  976. if constexpr (_Np == 1)
  977. return !_M_bits[0];
  978. else if constexpr (!_Sanitized)
  979. return _M_sanitized().none();
  980. else
  981. {
  982. for (int __i = 0; __i < _S_array_size - 1; ++__i)
  983. if (_M_bits[__i] != 0)
  984. return false;
  985. return _M_bits[_S_array_size - 1] == 0;
  986. }
  987. }
  988. // Returns the number of set bits. Implicitly sanitizes if _Sanitized ==
  989. // false.
  990. constexpr int
  991. count() const noexcept
  992. {
  993. if constexpr (_Np == 1)
  994. return _M_bits[0];
  995. else if constexpr (!_Sanitized)
  996. return _M_sanitized().none();
  997. else
  998. {
  999. int __result = __builtin_popcountll(_M_bits[0]);
  1000. for (int __i = 1; __i < _S_array_size; ++__i)
  1001. __result += __builtin_popcountll(_M_bits[__i]);
  1002. return __result;
  1003. }
  1004. }
  1005. // Returns the bit at offset __i as bool.
  1006. constexpr bool
  1007. operator[](size_t __i) const noexcept
  1008. {
  1009. if constexpr (_Np == 1)
  1010. return _M_bits[0];
  1011. else if constexpr (_S_array_size == 1)
  1012. return (_M_bits[0] >> __i) & 1;
  1013. else
  1014. {
  1015. const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
  1016. const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
  1017. return (_M_bits[__j] >> __shift) & 1;
  1018. }
  1019. }
  1020. template <size_t __i>
  1021. constexpr bool
  1022. operator[](_SizeConstant<__i>) const noexcept
  1023. {
  1024. static_assert(__i < _Np);
  1025. constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
  1026. constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
  1027. return static_cast<bool>(_M_bits[__j] & (_Tp(1) << __shift));
  1028. }
  1029. // Set the bit at offset __i to __x.
  1030. constexpr void
  1031. set(size_t __i, bool __x) noexcept
  1032. {
  1033. if constexpr (_Np == 1)
  1034. _M_bits[0] = __x;
  1035. else if constexpr (_S_array_size == 1)
  1036. {
  1037. _M_bits[0] &= ~_Tp(_Tp(1) << __i);
  1038. _M_bits[0] |= _Tp(_Tp(__x) << __i);
  1039. }
  1040. else
  1041. {
  1042. const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
  1043. const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
  1044. _M_bits[__j] &= ~_Tp(_Tp(1) << __shift);
  1045. _M_bits[__j] |= _Tp(_Tp(__x) << __shift);
  1046. }
  1047. }
  1048. template <size_t __i>
  1049. constexpr void
  1050. set(_SizeConstant<__i>, bool __x) noexcept
  1051. {
  1052. static_assert(__i < _Np);
  1053. if constexpr (_Np == 1)
  1054. _M_bits[0] = __x;
  1055. else
  1056. {
  1057. constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
  1058. constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
  1059. constexpr _Tp __mask = ~_Tp(_Tp(1) << __shift);
  1060. _M_bits[__j] &= __mask;
  1061. _M_bits[__j] |= _Tp(_Tp(__x) << __shift);
  1062. }
  1063. }
  1064. // Inverts all bits. Sanitized input leads to sanitized output.
  1065. constexpr _BitMask
  1066. operator~() const noexcept
  1067. {
  1068. if constexpr (_Np == 1)
  1069. return !_M_bits[0];
  1070. else
  1071. {
  1072. _BitMask __result{};
  1073. for (int __i = 0; __i < _S_array_size - 1; ++__i)
  1074. __result._M_bits[__i] = ~_M_bits[__i];
  1075. if constexpr (_Sanitized)
  1076. __result._M_bits[_S_array_size - 1]
  1077. = _M_bits[_S_array_size - 1] ^ _S_bitmask;
  1078. else
  1079. __result._M_bits[_S_array_size - 1] = ~_M_bits[_S_array_size - 1];
  1080. return __result;
  1081. }
  1082. }
  1083. constexpr _BitMask&
  1084. operator^=(const _BitMask& __b) & noexcept
  1085. {
  1086. __execute_n_times<_S_array_size>(
  1087. [&](auto __i) { _M_bits[__i] ^= __b._M_bits[__i]; });
  1088. return *this;
  1089. }
  1090. constexpr _BitMask&
  1091. operator|=(const _BitMask& __b) & noexcept
  1092. {
  1093. __execute_n_times<_S_array_size>(
  1094. [&](auto __i) { _M_bits[__i] |= __b._M_bits[__i]; });
  1095. return *this;
  1096. }
  1097. constexpr _BitMask&
  1098. operator&=(const _BitMask& __b) & noexcept
  1099. {
  1100. __execute_n_times<_S_array_size>(
  1101. [&](auto __i) { _M_bits[__i] &= __b._M_bits[__i]; });
  1102. return *this;
  1103. }
  1104. friend constexpr _BitMask
  1105. operator^(const _BitMask& __a, const _BitMask& __b) noexcept
  1106. {
  1107. _BitMask __r = __a;
  1108. __r ^= __b;
  1109. return __r;
  1110. }
  1111. friend constexpr _BitMask
  1112. operator|(const _BitMask& __a, const _BitMask& __b) noexcept
  1113. {
  1114. _BitMask __r = __a;
  1115. __r |= __b;
  1116. return __r;
  1117. }
  1118. friend constexpr _BitMask
  1119. operator&(const _BitMask& __a, const _BitMask& __b) noexcept
  1120. {
  1121. _BitMask __r = __a;
  1122. __r &= __b;
  1123. return __r;
  1124. }
  1125. _GLIBCXX_SIMD_INTRINSIC
  1126. constexpr bool
  1127. _M_is_constprop() const
  1128. {
  1129. if constexpr (_S_array_size == 0)
  1130. return __builtin_constant_p(_M_bits[0]);
  1131. else
  1132. {
  1133. for (int __i = 0; __i < _S_array_size; ++__i)
  1134. if (!__builtin_constant_p(_M_bits[__i]))
  1135. return false;
  1136. return true;
  1137. }
  1138. }
  1139. };
  1140. // }}}
  1141. // vvv ---- builtin vector types [[gnu::vector_size(N)]] and operations ---- vvv
  1142. // __min_vector_size {{{
  1143. template <typename _Tp = void>
  1144. static inline constexpr int __min_vector_size = 2 * sizeof(_Tp);
  1145. #if _GLIBCXX_SIMD_HAVE_NEON
  1146. template <>
  1147. inline constexpr int __min_vector_size<void> = 8;
  1148. #else
  1149. template <>
  1150. inline constexpr int __min_vector_size<void> = 16;
  1151. #endif
  1152. // }}}
  1153. // __vector_type {{{
  1154. template <typename _Tp, size_t _Np, typename = void>
  1155. struct __vector_type_n {};
  1156. // substition failure for 0-element case
  1157. template <typename _Tp>
  1158. struct __vector_type_n<_Tp, 0, void> {};
  1159. // special case 1-element to be _Tp itself
  1160. template <typename _Tp>
  1161. struct __vector_type_n<_Tp, 1, enable_if_t<__is_vectorizable_v<_Tp>>>
  1162. { using type = _Tp; };
  1163. // else, use GNU-style builtin vector types
  1164. template <typename _Tp, size_t _Np>
  1165. struct __vector_type_n<_Tp, _Np,
  1166. enable_if_t<__is_vectorizable_v<_Tp> && _Np >= 2>>
  1167. {
  1168. static constexpr size_t _S_Np2 = std::__bit_ceil(_Np * sizeof(_Tp));
  1169. static constexpr size_t _S_Bytes =
  1170. #ifdef __i386__
  1171. // Using [[gnu::vector_size(8)]] would wreak havoc on the FPU because
  1172. // those objects are passed via MMX registers and nothing ever calls EMMS.
  1173. _S_Np2 == 8 ? 16 :
  1174. #endif
  1175. _S_Np2 < __min_vector_size<_Tp> ? __min_vector_size<_Tp>
  1176. : _S_Np2;
  1177. using type [[__gnu__::__vector_size__(_S_Bytes)]] = _Tp;
  1178. };
  1179. template <typename _Tp, size_t _Bytes, size_t = _Bytes % sizeof(_Tp)>
  1180. struct __vector_type;
  1181. template <typename _Tp, size_t _Bytes>
  1182. struct __vector_type<_Tp, _Bytes, 0>
  1183. : __vector_type_n<_Tp, _Bytes / sizeof(_Tp)> {};
  1184. template <typename _Tp, size_t _Size>
  1185. using __vector_type_t = typename __vector_type_n<_Tp, _Size>::type;
  1186. template <typename _Tp>
  1187. using __vector_type2_t = typename __vector_type<_Tp, 2>::type;
  1188. template <typename _Tp>
  1189. using __vector_type4_t = typename __vector_type<_Tp, 4>::type;
  1190. template <typename _Tp>
  1191. using __vector_type8_t = typename __vector_type<_Tp, 8>::type;
  1192. template <typename _Tp>
  1193. using __vector_type16_t = typename __vector_type<_Tp, 16>::type;
  1194. template <typename _Tp>
  1195. using __vector_type32_t = typename __vector_type<_Tp, 32>::type;
  1196. template <typename _Tp>
  1197. using __vector_type64_t = typename __vector_type<_Tp, 64>::type;
  1198. // }}}
  1199. // __is_vector_type {{{
  1200. template <typename _Tp, typename = void_t<>>
  1201. struct __is_vector_type : false_type {};
  1202. template <typename _Tp>
  1203. struct __is_vector_type<
  1204. _Tp, void_t<typename __vector_type<
  1205. remove_reference_t<decltype(declval<_Tp>()[0])>, sizeof(_Tp)>::type>>
  1206. : is_same<_Tp, typename __vector_type<
  1207. remove_reference_t<decltype(declval<_Tp>()[0])>,
  1208. sizeof(_Tp)>::type> {};
  1209. template <typename _Tp>
  1210. inline constexpr bool __is_vector_type_v = __is_vector_type<_Tp>::value;
  1211. // }}}
  1212. // __is_intrinsic_type {{{
  1213. #if _GLIBCXX_SIMD_HAVE_SSE_ABI
  1214. template <typename _Tp>
  1215. using __is_intrinsic_type = __is_vector_type<_Tp>;
  1216. #else // not SSE (x86)
  1217. template <typename _Tp, typename = void_t<>>
  1218. struct __is_intrinsic_type : false_type {};
  1219. template <typename _Tp>
  1220. struct __is_intrinsic_type<
  1221. _Tp, void_t<typename __intrinsic_type<
  1222. remove_reference_t<decltype(declval<_Tp>()[0])>, sizeof(_Tp)>::type>>
  1223. : is_same<_Tp, typename __intrinsic_type<
  1224. remove_reference_t<decltype(declval<_Tp>()[0])>,
  1225. sizeof(_Tp)>::type> {};
  1226. #endif
  1227. template <typename _Tp>
  1228. inline constexpr bool __is_intrinsic_type_v = __is_intrinsic_type<_Tp>::value;
  1229. // }}}
  1230. // _VectorTraits{{{
  1231. template <typename _Tp, typename = void_t<>>
  1232. struct _VectorTraitsImpl;
  1233. template <typename _Tp>
  1234. struct _VectorTraitsImpl<_Tp, enable_if_t<__is_vector_type_v<_Tp>
  1235. || __is_intrinsic_type_v<_Tp>>>
  1236. {
  1237. using type = _Tp;
  1238. using value_type = remove_reference_t<decltype(declval<_Tp>()[0])>;
  1239. static constexpr int _S_full_size = sizeof(_Tp) / sizeof(value_type);
  1240. using _Wrapper = _SimdWrapper<value_type, _S_full_size>;
  1241. template <typename _Up, int _W = _S_full_size>
  1242. static constexpr bool _S_is
  1243. = is_same_v<value_type, _Up> && _W == _S_full_size;
  1244. };
  1245. template <typename _Tp, size_t _Np>
  1246. struct _VectorTraitsImpl<_SimdWrapper<_Tp, _Np>,
  1247. void_t<__vector_type_t<_Tp, _Np>>>
  1248. {
  1249. using type = __vector_type_t<_Tp, _Np>;
  1250. using value_type = _Tp;
  1251. static constexpr int _S_full_size = sizeof(type) / sizeof(value_type);
  1252. using _Wrapper = _SimdWrapper<_Tp, _Np>;
  1253. static constexpr bool _S_is_partial = (_Np == _S_full_size);
  1254. static constexpr int _S_partial_width = _Np;
  1255. template <typename _Up, int _W = _S_full_size>
  1256. static constexpr bool _S_is
  1257. = is_same_v<value_type, _Up>&& _W == _S_full_size;
  1258. };
  1259. template <typename _Tp, typename = typename _VectorTraitsImpl<_Tp>::type>
  1260. using _VectorTraits = _VectorTraitsImpl<_Tp>;
  1261. // }}}
  1262. // __as_vector{{{
  1263. template <typename _V>
  1264. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  1265. __as_vector(_V __x)
  1266. {
  1267. if constexpr (__is_vector_type_v<_V>)
  1268. return __x;
  1269. else if constexpr (is_simd<_V>::value || is_simd_mask<_V>::value)
  1270. return __data(__x)._M_data;
  1271. else if constexpr (__is_vectorizable_v<_V>)
  1272. return __vector_type_t<_V, 2>{__x};
  1273. else
  1274. return __x._M_data;
  1275. }
  1276. // }}}
  1277. // __as_wrapper{{{
  1278. template <size_t _Np = 0, typename _V>
  1279. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  1280. __as_wrapper(_V __x)
  1281. {
  1282. if constexpr (__is_vector_type_v<_V>)
  1283. return _SimdWrapper<typename _VectorTraits<_V>::value_type,
  1284. (_Np > 0 ? _Np : _VectorTraits<_V>::_S_full_size)>(__x);
  1285. else if constexpr (is_simd<_V>::value || is_simd_mask<_V>::value)
  1286. {
  1287. static_assert(_V::size() == _Np);
  1288. return __data(__x);
  1289. }
  1290. else
  1291. {
  1292. static_assert(_V::_S_size == _Np);
  1293. return __x;
  1294. }
  1295. }
  1296. // }}}
  1297. // __intrin_bitcast{{{
  1298. template <typename _To, typename _From>
  1299. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  1300. __intrin_bitcast(_From __v)
  1301. {
  1302. static_assert((__is_vector_type_v<_From> || __is_intrinsic_type_v<_From>)
  1303. && (__is_vector_type_v<_To> || __is_intrinsic_type_v<_To>));
  1304. if constexpr (sizeof(_To) == sizeof(_From))
  1305. return reinterpret_cast<_To>(__v);
  1306. else if constexpr (sizeof(_From) > sizeof(_To))
  1307. if constexpr (sizeof(_To) >= 16)
  1308. return reinterpret_cast<const __may_alias<_To>&>(__v);
  1309. else
  1310. {
  1311. _To __r;
  1312. __builtin_memcpy(&__r, &__v, sizeof(_To));
  1313. return __r;
  1314. }
  1315. #if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
  1316. else if constexpr (__have_avx && sizeof(_From) == 16 && sizeof(_To) == 32)
  1317. return reinterpret_cast<_To>(__builtin_ia32_ps256_ps(
  1318. reinterpret_cast<__vector_type_t<float, 4>>(__v)));
  1319. else if constexpr (__have_avx512f && sizeof(_From) == 16
  1320. && sizeof(_To) == 64)
  1321. return reinterpret_cast<_To>(__builtin_ia32_ps512_ps(
  1322. reinterpret_cast<__vector_type_t<float, 4>>(__v)));
  1323. else if constexpr (__have_avx512f && sizeof(_From) == 32
  1324. && sizeof(_To) == 64)
  1325. return reinterpret_cast<_To>(__builtin_ia32_ps512_256ps(
  1326. reinterpret_cast<__vector_type_t<float, 8>>(__v)));
  1327. #endif // _GLIBCXX_SIMD_X86INTRIN
  1328. else if constexpr (sizeof(__v) <= 8)
  1329. return reinterpret_cast<_To>(
  1330. __vector_type_t<__int_for_sizeof_t<_From>, sizeof(_To) / sizeof(_From)>{
  1331. reinterpret_cast<__int_for_sizeof_t<_From>>(__v)});
  1332. else
  1333. {
  1334. static_assert(sizeof(_To) > sizeof(_From));
  1335. _To __r = {};
  1336. __builtin_memcpy(&__r, &__v, sizeof(_From));
  1337. return __r;
  1338. }
  1339. }
  1340. // }}}
  1341. // __vector_bitcast{{{
  1342. template <typename _To, size_t _NN = 0, typename _From,
  1343. typename _FromVT = _VectorTraits<_From>,
  1344. size_t _Np = _NN == 0 ? sizeof(_From) / sizeof(_To) : _NN>
  1345. _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_To, _Np>
  1346. __vector_bitcast(_From __x)
  1347. {
  1348. using _R = __vector_type_t<_To, _Np>;
  1349. return __intrin_bitcast<_R>(__x);
  1350. }
  1351. template <typename _To, size_t _NN = 0, typename _Tp, size_t _Nx,
  1352. size_t _Np
  1353. = _NN == 0 ? sizeof(_SimdWrapper<_Tp, _Nx>) / sizeof(_To) : _NN>
  1354. _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_To, _Np>
  1355. __vector_bitcast(const _SimdWrapper<_Tp, _Nx>& __x)
  1356. {
  1357. static_assert(_Np > 1);
  1358. return __intrin_bitcast<__vector_type_t<_To, _Np>>(__x._M_data);
  1359. }
  1360. // }}}
  1361. // __convert_x86 declarations {{{
  1362. #ifdef _GLIBCXX_SIMD_WORKAROUND_PR85048
  1363. template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1364. _To __convert_x86(_Tp);
  1365. template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1366. _To __convert_x86(_Tp, _Tp);
  1367. template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1368. _To __convert_x86(_Tp, _Tp, _Tp, _Tp);
  1369. template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1370. _To __convert_x86(_Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp);
  1371. template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1372. _To __convert_x86(_Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp,
  1373. _Tp, _Tp, _Tp, _Tp);
  1374. #endif // _GLIBCXX_SIMD_WORKAROUND_PR85048
  1375. //}}}
  1376. // __bit_cast {{{
  1377. template <typename _To, typename _From>
  1378. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  1379. __bit_cast(const _From __x)
  1380. {
  1381. // TODO: implement with / replace by __builtin_bit_cast ASAP
  1382. static_assert(sizeof(_To) == sizeof(_From));
  1383. constexpr bool __to_is_vectorizable
  1384. = is_arithmetic_v<_To> || is_enum_v<_To>;
  1385. constexpr bool __from_is_vectorizable
  1386. = is_arithmetic_v<_From> || is_enum_v<_From>;
  1387. if constexpr (__is_vector_type_v<_To> && __is_vector_type_v<_From>)
  1388. return reinterpret_cast<_To>(__x);
  1389. else if constexpr (__is_vector_type_v<_To> && __from_is_vectorizable)
  1390. {
  1391. using _FV [[gnu::vector_size(sizeof(_From))]] = _From;
  1392. return reinterpret_cast<_To>(_FV{__x});
  1393. }
  1394. else if constexpr (__to_is_vectorizable && __from_is_vectorizable)
  1395. {
  1396. using _TV [[gnu::vector_size(sizeof(_To))]] = _To;
  1397. using _FV [[gnu::vector_size(sizeof(_From))]] = _From;
  1398. return reinterpret_cast<_TV>(_FV{__x})[0];
  1399. }
  1400. else if constexpr (__to_is_vectorizable && __is_vector_type_v<_From>)
  1401. {
  1402. using _TV [[gnu::vector_size(sizeof(_To))]] = _To;
  1403. return reinterpret_cast<_TV>(__x)[0];
  1404. }
  1405. else
  1406. {
  1407. _To __r;
  1408. __builtin_memcpy(reinterpret_cast<char*>(&__r),
  1409. reinterpret_cast<const char*>(&__x), sizeof(_To));
  1410. return __r;
  1411. }
  1412. }
  1413. // }}}
  1414. // __to_intrin {{{
  1415. template <typename _Tp, typename _TVT = _VectorTraits<_Tp>,
  1416. typename _R
  1417. = __intrinsic_type_t<typename _TVT::value_type, _TVT::_S_full_size>>
  1418. _GLIBCXX_SIMD_INTRINSIC constexpr _R
  1419. __to_intrin(_Tp __x)
  1420. {
  1421. static_assert(sizeof(__x) <= sizeof(_R),
  1422. "__to_intrin may never drop values off the end");
  1423. if constexpr (sizeof(__x) == sizeof(_R))
  1424. return reinterpret_cast<_R>(__as_vector(__x));
  1425. else
  1426. {
  1427. using _Up = __int_for_sizeof_t<_Tp>;
  1428. return reinterpret_cast<_R>(
  1429. __vector_type_t<_Up, sizeof(_R) / sizeof(_Up)>{__bit_cast<_Up>(__x)});
  1430. }
  1431. }
  1432. // }}}
  1433. // __make_vector{{{
  1434. template <typename _Tp, typename... _Args>
  1435. _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, sizeof...(_Args)>
  1436. __make_vector(const _Args&... __args)
  1437. {
  1438. return __vector_type_t<_Tp, sizeof...(_Args)>{static_cast<_Tp>(__args)...};
  1439. }
  1440. // }}}
  1441. // __vector_broadcast{{{
  1442. template <size_t _Np, typename _Tp>
  1443. _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
  1444. __vector_broadcast(_Tp __x)
  1445. {
  1446. return __call_with_n_evaluations<_Np>(
  1447. [](auto... __xx) { return __vector_type_t<_Tp, _Np>{__xx...}; },
  1448. [&__x](int) { return __x; });
  1449. }
  1450. // }}}
  1451. // __generate_vector{{{
  1452. template <typename _Tp, size_t _Np, typename _Gp, size_t... _I>
  1453. _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
  1454. __generate_vector_impl(_Gp&& __gen, index_sequence<_I...>)
  1455. {
  1456. return __vector_type_t<_Tp, _Np>{
  1457. static_cast<_Tp>(__gen(_SizeConstant<_I>()))...};
  1458. }
  1459. template <typename _V, typename _VVT = _VectorTraits<_V>, typename _Gp>
  1460. _GLIBCXX_SIMD_INTRINSIC constexpr _V
  1461. __generate_vector(_Gp&& __gen)
  1462. {
  1463. if constexpr (__is_vector_type_v<_V>)
  1464. return __generate_vector_impl<typename _VVT::value_type,
  1465. _VVT::_S_full_size>(
  1466. static_cast<_Gp&&>(__gen), make_index_sequence<_VVT::_S_full_size>());
  1467. else
  1468. return __generate_vector_impl<typename _VVT::value_type,
  1469. _VVT::_S_partial_width>(
  1470. static_cast<_Gp&&>(__gen),
  1471. make_index_sequence<_VVT::_S_partial_width>());
  1472. }
  1473. template <typename _Tp, size_t _Np, typename _Gp>
  1474. _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
  1475. __generate_vector(_Gp&& __gen)
  1476. {
  1477. return __generate_vector_impl<_Tp, _Np>(static_cast<_Gp&&>(__gen),
  1478. make_index_sequence<_Np>());
  1479. }
  1480. // }}}
  1481. // __xor{{{
  1482. template <typename _TW>
  1483. _GLIBCXX_SIMD_INTRINSIC constexpr _TW
  1484. __xor(_TW __a, _TW __b) noexcept
  1485. {
  1486. if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
  1487. {
  1488. using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
  1489. _VectorTraitsImpl<_TW>>::value_type;
  1490. if constexpr (is_floating_point_v<_Tp>)
  1491. {
  1492. using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
  1493. return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
  1494. ^ __vector_bitcast<_Ip>(__b));
  1495. }
  1496. else if constexpr (__is_vector_type_v<_TW>)
  1497. return __a ^ __b;
  1498. else
  1499. return __a._M_data ^ __b._M_data;
  1500. }
  1501. else
  1502. return __a ^ __b;
  1503. }
  1504. // }}}
  1505. // __or{{{
  1506. template <typename _TW>
  1507. _GLIBCXX_SIMD_INTRINSIC constexpr _TW
  1508. __or(_TW __a, _TW __b) noexcept
  1509. {
  1510. if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
  1511. {
  1512. using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
  1513. _VectorTraitsImpl<_TW>>::value_type;
  1514. if constexpr (is_floating_point_v<_Tp>)
  1515. {
  1516. using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
  1517. return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
  1518. | __vector_bitcast<_Ip>(__b));
  1519. }
  1520. else if constexpr (__is_vector_type_v<_TW>)
  1521. return __a | __b;
  1522. else
  1523. return __a._M_data | __b._M_data;
  1524. }
  1525. else
  1526. return __a | __b;
  1527. }
  1528. // }}}
  1529. // __and{{{
  1530. template <typename _TW>
  1531. _GLIBCXX_SIMD_INTRINSIC constexpr _TW
  1532. __and(_TW __a, _TW __b) noexcept
  1533. {
  1534. if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
  1535. {
  1536. using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
  1537. _VectorTraitsImpl<_TW>>::value_type;
  1538. if constexpr (is_floating_point_v<_Tp>)
  1539. {
  1540. using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
  1541. return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
  1542. & __vector_bitcast<_Ip>(__b));
  1543. }
  1544. else if constexpr (__is_vector_type_v<_TW>)
  1545. return __a & __b;
  1546. else
  1547. return __a._M_data & __b._M_data;
  1548. }
  1549. else
  1550. return __a & __b;
  1551. }
  1552. // }}}
  1553. // __andnot{{{
  1554. #if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
  1555. static constexpr struct
  1556. {
  1557. _GLIBCXX_SIMD_INTRINSIC __v4sf
  1558. operator()(__v4sf __a, __v4sf __b) const noexcept
  1559. { return __builtin_ia32_andnps(__a, __b); }
  1560. _GLIBCXX_SIMD_INTRINSIC __v2df
  1561. operator()(__v2df __a, __v2df __b) const noexcept
  1562. { return __builtin_ia32_andnpd(__a, __b); }
  1563. _GLIBCXX_SIMD_INTRINSIC __v2di
  1564. operator()(__v2di __a, __v2di __b) const noexcept
  1565. { return __builtin_ia32_pandn128(__a, __b); }
  1566. _GLIBCXX_SIMD_INTRINSIC __v8sf
  1567. operator()(__v8sf __a, __v8sf __b) const noexcept
  1568. { return __builtin_ia32_andnps256(__a, __b); }
  1569. _GLIBCXX_SIMD_INTRINSIC __v4df
  1570. operator()(__v4df __a, __v4df __b) const noexcept
  1571. { return __builtin_ia32_andnpd256(__a, __b); }
  1572. _GLIBCXX_SIMD_INTRINSIC __v4di
  1573. operator()(__v4di __a, __v4di __b) const noexcept
  1574. {
  1575. if constexpr (__have_avx2)
  1576. return __builtin_ia32_andnotsi256(__a, __b);
  1577. else
  1578. return reinterpret_cast<__v4di>(
  1579. __builtin_ia32_andnpd256(reinterpret_cast<__v4df>(__a),
  1580. reinterpret_cast<__v4df>(__b)));
  1581. }
  1582. _GLIBCXX_SIMD_INTRINSIC __v16sf
  1583. operator()(__v16sf __a, __v16sf __b) const noexcept
  1584. {
  1585. if constexpr (__have_avx512dq)
  1586. return _mm512_andnot_ps(__a, __b);
  1587. else
  1588. return reinterpret_cast<__v16sf>(
  1589. _mm512_andnot_si512(reinterpret_cast<__v8di>(__a),
  1590. reinterpret_cast<__v8di>(__b)));
  1591. }
  1592. _GLIBCXX_SIMD_INTRINSIC __v8df
  1593. operator()(__v8df __a, __v8df __b) const noexcept
  1594. {
  1595. if constexpr (__have_avx512dq)
  1596. return _mm512_andnot_pd(__a, __b);
  1597. else
  1598. return reinterpret_cast<__v8df>(
  1599. _mm512_andnot_si512(reinterpret_cast<__v8di>(__a),
  1600. reinterpret_cast<__v8di>(__b)));
  1601. }
  1602. _GLIBCXX_SIMD_INTRINSIC __v8di
  1603. operator()(__v8di __a, __v8di __b) const noexcept
  1604. { return _mm512_andnot_si512(__a, __b); }
  1605. } _S_x86_andnot;
  1606. #endif // _GLIBCXX_SIMD_X86INTRIN && !__clang__
  1607. template <typename _TW>
  1608. _GLIBCXX_SIMD_INTRINSIC constexpr _TW
  1609. __andnot(_TW __a, _TW __b) noexcept
  1610. {
  1611. if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
  1612. {
  1613. using _TVT = conditional_t<__is_simd_wrapper_v<_TW>, _TW,
  1614. _VectorTraitsImpl<_TW>>;
  1615. using _Tp = typename _TVT::value_type;
  1616. #if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
  1617. if constexpr (sizeof(_TW) >= 16)
  1618. {
  1619. const auto __ai = __to_intrin(__a);
  1620. const auto __bi = __to_intrin(__b);
  1621. if (!__builtin_is_constant_evaluated()
  1622. && !(__builtin_constant_p(__ai) && __builtin_constant_p(__bi)))
  1623. {
  1624. const auto __r = _S_x86_andnot(__ai, __bi);
  1625. if constexpr (is_convertible_v<decltype(__r), _TW>)
  1626. return __r;
  1627. else
  1628. return reinterpret_cast<typename _TVT::type>(__r);
  1629. }
  1630. }
  1631. #endif // _GLIBCXX_SIMD_X86INTRIN
  1632. using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
  1633. return __vector_bitcast<_Tp>(~__vector_bitcast<_Ip>(__a)
  1634. & __vector_bitcast<_Ip>(__b));
  1635. }
  1636. else
  1637. return ~__a & __b;
  1638. }
  1639. // }}}
  1640. // __not{{{
  1641. template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1642. _GLIBCXX_SIMD_INTRINSIC constexpr _Tp
  1643. __not(_Tp __a) noexcept
  1644. {
  1645. if constexpr (is_floating_point_v<typename _TVT::value_type>)
  1646. return reinterpret_cast<typename _TVT::type>(
  1647. ~__vector_bitcast<unsigned>(__a));
  1648. else
  1649. return ~__a;
  1650. }
  1651. // }}}
  1652. // __concat{{{
  1653. template <typename _Tp, typename _TVT = _VectorTraits<_Tp>,
  1654. typename _R = __vector_type_t<typename _TVT::value_type,
  1655. _TVT::_S_full_size * 2>>
  1656. constexpr _R
  1657. __concat(_Tp a_, _Tp b_)
  1658. {
  1659. #ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_1
  1660. using _W
  1661. = conditional_t<is_floating_point_v<typename _TVT::value_type>, double,
  1662. conditional_t<(sizeof(_Tp) >= 2 * sizeof(long long)),
  1663. long long, typename _TVT::value_type>>;
  1664. constexpr int input_width = sizeof(_Tp) / sizeof(_W);
  1665. const auto __a = __vector_bitcast<_W>(a_);
  1666. const auto __b = __vector_bitcast<_W>(b_);
  1667. using _Up = __vector_type_t<_W, sizeof(_R) / sizeof(_W)>;
  1668. #else
  1669. constexpr int input_width = _TVT::_S_full_size;
  1670. const _Tp& __a = a_;
  1671. const _Tp& __b = b_;
  1672. using _Up = _R;
  1673. #endif
  1674. if constexpr (input_width == 2)
  1675. return reinterpret_cast<_R>(_Up{__a[0], __a[1], __b[0], __b[1]});
  1676. else if constexpr (input_width == 4)
  1677. return reinterpret_cast<_R>(
  1678. _Up{__a[0], __a[1], __a[2], __a[3], __b[0], __b[1], __b[2], __b[3]});
  1679. else if constexpr (input_width == 8)
  1680. return reinterpret_cast<_R>(
  1681. _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6], __a[7],
  1682. __b[0], __b[1], __b[2], __b[3], __b[4], __b[5], __b[6], __b[7]});
  1683. else if constexpr (input_width == 16)
  1684. return reinterpret_cast<_R>(
  1685. _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6],
  1686. __a[7], __a[8], __a[9], __a[10], __a[11], __a[12], __a[13],
  1687. __a[14], __a[15], __b[0], __b[1], __b[2], __b[3], __b[4],
  1688. __b[5], __b[6], __b[7], __b[8], __b[9], __b[10], __b[11],
  1689. __b[12], __b[13], __b[14], __b[15]});
  1690. else if constexpr (input_width == 32)
  1691. return reinterpret_cast<_R>(
  1692. _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6],
  1693. __a[7], __a[8], __a[9], __a[10], __a[11], __a[12], __a[13],
  1694. __a[14], __a[15], __a[16], __a[17], __a[18], __a[19], __a[20],
  1695. __a[21], __a[22], __a[23], __a[24], __a[25], __a[26], __a[27],
  1696. __a[28], __a[29], __a[30], __a[31], __b[0], __b[1], __b[2],
  1697. __b[3], __b[4], __b[5], __b[6], __b[7], __b[8], __b[9],
  1698. __b[10], __b[11], __b[12], __b[13], __b[14], __b[15], __b[16],
  1699. __b[17], __b[18], __b[19], __b[20], __b[21], __b[22], __b[23],
  1700. __b[24], __b[25], __b[26], __b[27], __b[28], __b[29], __b[30],
  1701. __b[31]});
  1702. }
  1703. // }}}
  1704. // __zero_extend {{{
  1705. template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1706. struct _ZeroExtendProxy
  1707. {
  1708. using value_type = typename _TVT::value_type;
  1709. static constexpr size_t _Np = _TVT::_S_full_size;
  1710. const _Tp __x;
  1711. template <typename _To, typename _ToVT = _VectorTraits<_To>,
  1712. typename
  1713. = enable_if_t<is_same_v<typename _ToVT::value_type, value_type>>>
  1714. _GLIBCXX_SIMD_INTRINSIC operator _To() const
  1715. {
  1716. constexpr size_t _ToN = _ToVT::_S_full_size;
  1717. if constexpr (_ToN == _Np)
  1718. return __x;
  1719. else if constexpr (_ToN == 2 * _Np)
  1720. {
  1721. #ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_3
  1722. if constexpr (__have_avx && _TVT::template _S_is<float, 4>)
  1723. return __vector_bitcast<value_type>(
  1724. _mm256_insertf128_ps(__m256(), __x, 0));
  1725. else if constexpr (__have_avx && _TVT::template _S_is<double, 2>)
  1726. return __vector_bitcast<value_type>(
  1727. _mm256_insertf128_pd(__m256d(), __x, 0));
  1728. else if constexpr (__have_avx2 && _Np * sizeof(value_type) == 16)
  1729. return __vector_bitcast<value_type>(
  1730. _mm256_insertf128_si256(__m256i(), __to_intrin(__x), 0));
  1731. else if constexpr (__have_avx512f && _TVT::template _S_is<float, 8>)
  1732. {
  1733. if constexpr (__have_avx512dq)
  1734. return __vector_bitcast<value_type>(
  1735. _mm512_insertf32x8(__m512(), __x, 0));
  1736. else
  1737. return reinterpret_cast<__m512>(
  1738. _mm512_insertf64x4(__m512d(),
  1739. reinterpret_cast<__m256d>(__x), 0));
  1740. }
  1741. else if constexpr (__have_avx512f
  1742. && _TVT::template _S_is<double, 4>)
  1743. return __vector_bitcast<value_type>(
  1744. _mm512_insertf64x4(__m512d(), __x, 0));
  1745. else if constexpr (__have_avx512f && _Np * sizeof(value_type) == 32)
  1746. return __vector_bitcast<value_type>(
  1747. _mm512_inserti64x4(__m512i(), __to_intrin(__x), 0));
  1748. #endif
  1749. return __concat(__x, _Tp());
  1750. }
  1751. else if constexpr (_ToN == 4 * _Np)
  1752. {
  1753. #ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_3
  1754. if constexpr (__have_avx512dq && _TVT::template _S_is<double, 2>)
  1755. {
  1756. return __vector_bitcast<value_type>(
  1757. _mm512_insertf64x2(__m512d(), __x, 0));
  1758. }
  1759. else if constexpr (__have_avx512f
  1760. && is_floating_point_v<value_type>)
  1761. {
  1762. return __vector_bitcast<value_type>(
  1763. _mm512_insertf32x4(__m512(), reinterpret_cast<__m128>(__x),
  1764. 0));
  1765. }
  1766. else if constexpr (__have_avx512f && _Np * sizeof(value_type) == 16)
  1767. {
  1768. return __vector_bitcast<value_type>(
  1769. _mm512_inserti32x4(__m512i(), __to_intrin(__x), 0));
  1770. }
  1771. #endif
  1772. return __concat(__concat(__x, _Tp()),
  1773. __vector_type_t<value_type, _Np * 2>());
  1774. }
  1775. else if constexpr (_ToN == 8 * _Np)
  1776. return __concat(operator __vector_type_t<value_type, _Np * 4>(),
  1777. __vector_type_t<value_type, _Np * 4>());
  1778. else if constexpr (_ToN == 16 * _Np)
  1779. return __concat(operator __vector_type_t<value_type, _Np * 8>(),
  1780. __vector_type_t<value_type, _Np * 8>());
  1781. else
  1782. __assert_unreachable<_Tp>();
  1783. }
  1784. };
  1785. template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1786. _GLIBCXX_SIMD_INTRINSIC _ZeroExtendProxy<_Tp, _TVT>
  1787. __zero_extend(_Tp __x)
  1788. { return {__x}; }
  1789. // }}}
  1790. // __extract<_Np, By>{{{
  1791. template <int _Offset,
  1792. int _SplitBy,
  1793. typename _Tp,
  1794. typename _TVT = _VectorTraits<_Tp>,
  1795. typename _R = __vector_type_t<typename _TVT::value_type,
  1796. _TVT::_S_full_size / _SplitBy>>
  1797. _GLIBCXX_SIMD_INTRINSIC constexpr _R
  1798. __extract(_Tp __in)
  1799. {
  1800. using value_type = typename _TVT::value_type;
  1801. #if _GLIBCXX_SIMD_X86INTRIN // {{{
  1802. if constexpr (sizeof(_Tp) == 64 && _SplitBy == 4 && _Offset > 0)
  1803. {
  1804. if constexpr (__have_avx512dq && is_same_v<double, value_type>)
  1805. return _mm512_extractf64x2_pd(__to_intrin(__in), _Offset);
  1806. else if constexpr (is_floating_point_v<value_type>)
  1807. return __vector_bitcast<value_type>(
  1808. _mm512_extractf32x4_ps(__intrin_bitcast<__m512>(__in), _Offset));
  1809. else
  1810. return reinterpret_cast<_R>(
  1811. _mm512_extracti32x4_epi32(__intrin_bitcast<__m512i>(__in),
  1812. _Offset));
  1813. }
  1814. else
  1815. #endif // _GLIBCXX_SIMD_X86INTRIN }}}
  1816. {
  1817. #ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_1
  1818. using _W = conditional_t<
  1819. is_floating_point_v<value_type>, double,
  1820. conditional_t<(sizeof(_R) >= 16), long long, value_type>>;
  1821. static_assert(sizeof(_R) % sizeof(_W) == 0);
  1822. constexpr int __return_width = sizeof(_R) / sizeof(_W);
  1823. using _Up = __vector_type_t<_W, __return_width>;
  1824. const auto __x = __vector_bitcast<_W>(__in);
  1825. #else
  1826. constexpr int __return_width = _TVT::_S_full_size / _SplitBy;
  1827. using _Up = _R;
  1828. const __vector_type_t<value_type, _TVT::_S_full_size>& __x
  1829. = __in; // only needed for _Tp = _SimdWrapper<value_type, _Np>
  1830. #endif
  1831. constexpr int _O = _Offset * __return_width;
  1832. return __call_with_subscripts<__return_width, _O>(
  1833. __x, [](auto... __entries) {
  1834. return reinterpret_cast<_R>(_Up{__entries...});
  1835. });
  1836. }
  1837. }
  1838. // }}}
  1839. // __lo/__hi64[z]{{{
  1840. template <typename _Tp,
  1841. typename _R
  1842. = __vector_type8_t<typename _VectorTraits<_Tp>::value_type>>
  1843. _GLIBCXX_SIMD_INTRINSIC constexpr _R
  1844. __lo64(_Tp __x)
  1845. {
  1846. _R __r{};
  1847. __builtin_memcpy(&__r, &__x, 8);
  1848. return __r;
  1849. }
  1850. template <typename _Tp,
  1851. typename _R
  1852. = __vector_type8_t<typename _VectorTraits<_Tp>::value_type>>
  1853. _GLIBCXX_SIMD_INTRINSIC constexpr _R
  1854. __hi64(_Tp __x)
  1855. {
  1856. static_assert(sizeof(_Tp) == 16, "use __hi64z if you meant it");
  1857. _R __r{};
  1858. __builtin_memcpy(&__r, reinterpret_cast<const char*>(&__x) + 8, 8);
  1859. return __r;
  1860. }
  1861. template <typename _Tp,
  1862. typename _R
  1863. = __vector_type8_t<typename _VectorTraits<_Tp>::value_type>>
  1864. _GLIBCXX_SIMD_INTRINSIC constexpr _R
  1865. __hi64z([[maybe_unused]] _Tp __x)
  1866. {
  1867. _R __r{};
  1868. if constexpr (sizeof(_Tp) == 16)
  1869. __builtin_memcpy(&__r, reinterpret_cast<const char*>(&__x) + 8, 8);
  1870. return __r;
  1871. }
  1872. // }}}
  1873. // __lo/__hi128{{{
  1874. template <typename _Tp>
  1875. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  1876. __lo128(_Tp __x)
  1877. { return __extract<0, sizeof(_Tp) / 16>(__x); }
  1878. template <typename _Tp>
  1879. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  1880. __hi128(_Tp __x)
  1881. {
  1882. static_assert(sizeof(__x) == 32);
  1883. return __extract<1, 2>(__x);
  1884. }
  1885. // }}}
  1886. // __lo/__hi256{{{
  1887. template <typename _Tp>
  1888. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  1889. __lo256(_Tp __x)
  1890. {
  1891. static_assert(sizeof(__x) == 64);
  1892. return __extract<0, 2>(__x);
  1893. }
  1894. template <typename _Tp>
  1895. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  1896. __hi256(_Tp __x)
  1897. {
  1898. static_assert(sizeof(__x) == 64);
  1899. return __extract<1, 2>(__x);
  1900. }
  1901. // }}}
  1902. // __auto_bitcast{{{
  1903. template <typename _Tp>
  1904. struct _AutoCast
  1905. {
  1906. static_assert(__is_vector_type_v<_Tp>);
  1907. const _Tp __x;
  1908. template <typename _Up, typename _UVT = _VectorTraits<_Up>>
  1909. _GLIBCXX_SIMD_INTRINSIC constexpr operator _Up() const
  1910. { return __intrin_bitcast<typename _UVT::type>(__x); }
  1911. };
  1912. template <typename _Tp>
  1913. _GLIBCXX_SIMD_INTRINSIC constexpr _AutoCast<_Tp>
  1914. __auto_bitcast(const _Tp& __x)
  1915. { return {__x}; }
  1916. template <typename _Tp, size_t _Np>
  1917. _GLIBCXX_SIMD_INTRINSIC constexpr
  1918. _AutoCast<typename _SimdWrapper<_Tp, _Np>::_BuiltinType>
  1919. __auto_bitcast(const _SimdWrapper<_Tp, _Np>& __x)
  1920. { return {__x._M_data}; }
  1921. // }}}
  1922. // ^^^ ---- builtin vector types [[gnu::vector_size(N)]] and operations ---- ^^^
  1923. #if _GLIBCXX_SIMD_HAVE_SSE_ABI
  1924. // __bool_storage_member_type{{{
  1925. #if _GLIBCXX_SIMD_HAVE_AVX512F && _GLIBCXX_SIMD_X86INTRIN
  1926. template <size_t _Size>
  1927. struct __bool_storage_member_type
  1928. {
  1929. static_assert((_Size & (_Size - 1)) != 0,
  1930. "This trait may only be used for non-power-of-2 sizes. "
  1931. "Power-of-2 sizes must be specialized.");
  1932. using type =
  1933. typename __bool_storage_member_type<std::__bit_ceil(_Size)>::type;
  1934. };
  1935. template <>
  1936. struct __bool_storage_member_type<1> { using type = bool; };
  1937. template <>
  1938. struct __bool_storage_member_type<2> { using type = __mmask8; };
  1939. template <>
  1940. struct __bool_storage_member_type<4> { using type = __mmask8; };
  1941. template <>
  1942. struct __bool_storage_member_type<8> { using type = __mmask8; };
  1943. template <>
  1944. struct __bool_storage_member_type<16> { using type = __mmask16; };
  1945. template <>
  1946. struct __bool_storage_member_type<32> { using type = __mmask32; };
  1947. template <>
  1948. struct __bool_storage_member_type<64> { using type = __mmask64; };
  1949. #endif // _GLIBCXX_SIMD_HAVE_AVX512F
  1950. // }}}
  1951. // __intrinsic_type (x86){{{
  1952. // the following excludes bool via __is_vectorizable
  1953. #if _GLIBCXX_SIMD_HAVE_SSE
  1954. template <typename _Tp, size_t _Bytes>
  1955. struct __intrinsic_type<_Tp, _Bytes,
  1956. enable_if_t<__is_vectorizable_v<_Tp> && _Bytes <= 64>>
  1957. {
  1958. static_assert(!is_same_v<_Tp, long double>,
  1959. "no __intrinsic_type support for long double on x86");
  1960. static constexpr size_t _S_VBytes = _Bytes <= 16 ? 16
  1961. : _Bytes <= 32 ? 32
  1962. : 64;
  1963. using type [[__gnu__::__vector_size__(_S_VBytes)]]
  1964. = conditional_t<is_integral_v<_Tp>, long long int, _Tp>;
  1965. };
  1966. #endif // _GLIBCXX_SIMD_HAVE_SSE
  1967. // }}}
  1968. #endif // _GLIBCXX_SIMD_HAVE_SSE_ABI
  1969. // __intrinsic_type (ARM){{{
  1970. #if _GLIBCXX_SIMD_HAVE_NEON
  1971. template <>
  1972. struct __intrinsic_type<float, 8, void>
  1973. { using type = float32x2_t; };
  1974. template <>
  1975. struct __intrinsic_type<float, 16, void>
  1976. { using type = float32x4_t; };
  1977. #if _GLIBCXX_SIMD_HAVE_NEON_A64
  1978. template <>
  1979. struct __intrinsic_type<double, 8, void>
  1980. { using type = float64x1_t; };
  1981. template <>
  1982. struct __intrinsic_type<double, 16, void>
  1983. { using type = float64x2_t; };
  1984. #endif
  1985. #define _GLIBCXX_SIMD_ARM_INTRIN(_Bits, _Np) \
  1986. template <> \
  1987. struct __intrinsic_type<__int_with_sizeof_t<_Bits / 8>, \
  1988. _Np * _Bits / 8, void> \
  1989. { using type = int##_Bits##x##_Np##_t; }; \
  1990. template <> \
  1991. struct __intrinsic_type<make_unsigned_t<__int_with_sizeof_t<_Bits / 8>>, \
  1992. _Np * _Bits / 8, void> \
  1993. { using type = uint##_Bits##x##_Np##_t; }
  1994. _GLIBCXX_SIMD_ARM_INTRIN(8, 8);
  1995. _GLIBCXX_SIMD_ARM_INTRIN(8, 16);
  1996. _GLIBCXX_SIMD_ARM_INTRIN(16, 4);
  1997. _GLIBCXX_SIMD_ARM_INTRIN(16, 8);
  1998. _GLIBCXX_SIMD_ARM_INTRIN(32, 2);
  1999. _GLIBCXX_SIMD_ARM_INTRIN(32, 4);
  2000. _GLIBCXX_SIMD_ARM_INTRIN(64, 1);
  2001. _GLIBCXX_SIMD_ARM_INTRIN(64, 2);
  2002. #undef _GLIBCXX_SIMD_ARM_INTRIN
  2003. template <typename _Tp, size_t _Bytes>
  2004. struct __intrinsic_type<_Tp, _Bytes,
  2005. enable_if_t<__is_vectorizable_v<_Tp> && _Bytes <= 16>>
  2006. {
  2007. static constexpr int _SVecBytes = _Bytes <= 8 ? 8 : 16;
  2008. using _Ip = __int_for_sizeof_t<_Tp>;
  2009. using _Up = conditional_t<
  2010. is_floating_point_v<_Tp>, _Tp,
  2011. conditional_t<is_unsigned_v<_Tp>, make_unsigned_t<_Ip>, _Ip>>;
  2012. static_assert(!is_same_v<_Tp, _Up> || _SVecBytes != _Bytes,
  2013. "should use explicit specialization above");
  2014. using type = typename __intrinsic_type<_Up, _SVecBytes>::type;
  2015. };
  2016. #endif // _GLIBCXX_SIMD_HAVE_NEON
  2017. // }}}
  2018. // __intrinsic_type (PPC){{{
  2019. #ifdef __ALTIVEC__
  2020. template <typename _Tp>
  2021. struct __intrinsic_type_impl;
  2022. #define _GLIBCXX_SIMD_PPC_INTRIN(_Tp) \
  2023. template <> \
  2024. struct __intrinsic_type_impl<_Tp> { using type = __vector _Tp; }
  2025. _GLIBCXX_SIMD_PPC_INTRIN(float);
  2026. _GLIBCXX_SIMD_PPC_INTRIN(double);
  2027. _GLIBCXX_SIMD_PPC_INTRIN(signed char);
  2028. _GLIBCXX_SIMD_PPC_INTRIN(unsigned char);
  2029. _GLIBCXX_SIMD_PPC_INTRIN(signed short);
  2030. _GLIBCXX_SIMD_PPC_INTRIN(unsigned short);
  2031. _GLIBCXX_SIMD_PPC_INTRIN(signed int);
  2032. _GLIBCXX_SIMD_PPC_INTRIN(unsigned int);
  2033. _GLIBCXX_SIMD_PPC_INTRIN(signed long);
  2034. _GLIBCXX_SIMD_PPC_INTRIN(unsigned long);
  2035. _GLIBCXX_SIMD_PPC_INTRIN(signed long long);
  2036. _GLIBCXX_SIMD_PPC_INTRIN(unsigned long long);
  2037. #undef _GLIBCXX_SIMD_PPC_INTRIN
  2038. template <typename _Tp, size_t _Bytes>
  2039. struct __intrinsic_type<_Tp, _Bytes,
  2040. enable_if_t<__is_vectorizable_v<_Tp> && _Bytes <= 16>>
  2041. {
  2042. static constexpr bool _S_is_ldouble = is_same_v<_Tp, long double>;
  2043. // allow _Tp == long double with -mlong-double-64
  2044. static_assert(!(_S_is_ldouble && sizeof(long double) > sizeof(double)),
  2045. "no __intrinsic_type support for long double on PPC");
  2046. #ifndef __VSX__
  2047. static_assert(!is_same_v<_Tp, double>,
  2048. "no __intrinsic_type support for double on PPC w/o VSX");
  2049. #endif
  2050. using type =
  2051. typename __intrinsic_type_impl<
  2052. conditional_t<is_floating_point_v<_Tp>,
  2053. conditional_t<_S_is_ldouble, double, _Tp>,
  2054. __int_for_sizeof_t<_Tp>>>::type;
  2055. };
  2056. #endif // __ALTIVEC__
  2057. // }}}
  2058. // _SimdWrapper<bool>{{{1
  2059. template <size_t _Width>
  2060. struct _SimdWrapper<bool, _Width,
  2061. void_t<typename __bool_storage_member_type<_Width>::type>>
  2062. {
  2063. using _BuiltinType = typename __bool_storage_member_type<_Width>::type;
  2064. using value_type = bool;
  2065. static constexpr size_t _S_full_size = sizeof(_BuiltinType) * __CHAR_BIT__;
  2066. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper<bool, _S_full_size>
  2067. __as_full_vector() const { return _M_data; }
  2068. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper() = default;
  2069. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper(_BuiltinType __k)
  2070. : _M_data(__k) {};
  2071. _GLIBCXX_SIMD_INTRINSIC operator const _BuiltinType&() const
  2072. { return _M_data; }
  2073. _GLIBCXX_SIMD_INTRINSIC operator _BuiltinType&()
  2074. { return _M_data; }
  2075. _GLIBCXX_SIMD_INTRINSIC _BuiltinType __intrin() const
  2076. { return _M_data; }
  2077. _GLIBCXX_SIMD_INTRINSIC constexpr value_type operator[](size_t __i) const
  2078. { return _M_data & (_BuiltinType(1) << __i); }
  2079. template <size_t __i>
  2080. _GLIBCXX_SIMD_INTRINSIC constexpr value_type
  2081. operator[](_SizeConstant<__i>) const
  2082. { return _M_data & (_BuiltinType(1) << __i); }
  2083. _GLIBCXX_SIMD_INTRINSIC constexpr void _M_set(size_t __i, value_type __x)
  2084. {
  2085. if (__x)
  2086. _M_data |= (_BuiltinType(1) << __i);
  2087. else
  2088. _M_data &= ~(_BuiltinType(1) << __i);
  2089. }
  2090. _GLIBCXX_SIMD_INTRINSIC
  2091. constexpr bool _M_is_constprop() const
  2092. { return __builtin_constant_p(_M_data); }
  2093. _GLIBCXX_SIMD_INTRINSIC constexpr bool _M_is_constprop_none_of() const
  2094. {
  2095. if (__builtin_constant_p(_M_data))
  2096. {
  2097. constexpr int __nbits = sizeof(_BuiltinType) * __CHAR_BIT__;
  2098. constexpr _BuiltinType __active_mask
  2099. = ~_BuiltinType() >> (__nbits - _Width);
  2100. return (_M_data & __active_mask) == 0;
  2101. }
  2102. return false;
  2103. }
  2104. _GLIBCXX_SIMD_INTRINSIC constexpr bool _M_is_constprop_all_of() const
  2105. {
  2106. if (__builtin_constant_p(_M_data))
  2107. {
  2108. constexpr int __nbits = sizeof(_BuiltinType) * __CHAR_BIT__;
  2109. constexpr _BuiltinType __active_mask
  2110. = ~_BuiltinType() >> (__nbits - _Width);
  2111. return (_M_data & __active_mask) == __active_mask;
  2112. }
  2113. return false;
  2114. }
  2115. _BuiltinType _M_data;
  2116. };
  2117. // _SimdWrapperBase{{{1
  2118. template <bool _MustZeroInitPadding, typename _BuiltinType>
  2119. struct _SimdWrapperBase;
  2120. template <typename _BuiltinType>
  2121. struct _SimdWrapperBase<false, _BuiltinType> // no padding or no SNaNs
  2122. {
  2123. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapperBase() = default;
  2124. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapperBase(_BuiltinType __init)
  2125. : _M_data(__init)
  2126. {}
  2127. _BuiltinType _M_data;
  2128. };
  2129. template <typename _BuiltinType>
  2130. struct _SimdWrapperBase<true, _BuiltinType> // with padding that needs to
  2131. // never become SNaN
  2132. {
  2133. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapperBase() : _M_data() {}
  2134. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapperBase(_BuiltinType __init)
  2135. : _M_data(__init)
  2136. {}
  2137. _BuiltinType _M_data;
  2138. };
  2139. // }}}
  2140. // _SimdWrapper{{{
  2141. template <typename _Tp, size_t _Width>
  2142. struct _SimdWrapper<
  2143. _Tp, _Width,
  2144. void_t<__vector_type_t<_Tp, _Width>, __intrinsic_type_t<_Tp, _Width>>>
  2145. : _SimdWrapperBase<__has_iec559_behavior<__signaling_NaN, _Tp>::value
  2146. && sizeof(_Tp) * _Width
  2147. == sizeof(__vector_type_t<_Tp, _Width>),
  2148. __vector_type_t<_Tp, _Width>>
  2149. {
  2150. using _Base
  2151. = _SimdWrapperBase<__has_iec559_behavior<__signaling_NaN, _Tp>::value
  2152. && sizeof(_Tp) * _Width
  2153. == sizeof(__vector_type_t<_Tp, _Width>),
  2154. __vector_type_t<_Tp, _Width>>;
  2155. static_assert(__is_vectorizable_v<_Tp>);
  2156. static_assert(_Width >= 2); // 1 doesn't make sense, use _Tp directly then
  2157. using _BuiltinType = __vector_type_t<_Tp, _Width>;
  2158. using value_type = _Tp;
  2159. static inline constexpr size_t _S_full_size
  2160. = sizeof(_BuiltinType) / sizeof(value_type);
  2161. static inline constexpr int _S_size = _Width;
  2162. static inline constexpr bool _S_is_partial = _S_full_size != _S_size;
  2163. using _Base::_M_data;
  2164. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper<_Tp, _S_full_size>
  2165. __as_full_vector() const
  2166. { return _M_data; }
  2167. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper(initializer_list<_Tp> __init)
  2168. : _Base(__generate_from_n_evaluations<_Width, _BuiltinType>(
  2169. [&](auto __i) { return __init.begin()[__i.value]; })) {}
  2170. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper() = default;
  2171. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper(const _SimdWrapper&)
  2172. = default;
  2173. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper(_SimdWrapper&&) = default;
  2174. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper&
  2175. operator=(const _SimdWrapper&) = default;
  2176. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper&
  2177. operator=(_SimdWrapper&&) = default;
  2178. template <typename _V, typename = enable_if_t<disjunction_v<
  2179. is_same<_V, __vector_type_t<_Tp, _Width>>,
  2180. is_same<_V, __intrinsic_type_t<_Tp, _Width>>>>>
  2181. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper(_V __x)
  2182. // __vector_bitcast can convert e.g. __m128 to __vector(2) float
  2183. : _Base(__vector_bitcast<_Tp, _Width>(__x)) {}
  2184. template <typename... _As,
  2185. typename = enable_if_t<((is_same_v<simd_abi::scalar, _As> && ...)
  2186. && sizeof...(_As) <= _Width)>>
  2187. _GLIBCXX_SIMD_INTRINSIC constexpr
  2188. operator _SimdTuple<_Tp, _As...>() const
  2189. {
  2190. const auto& dd = _M_data; // workaround for GCC7 ICE
  2191. return __generate_from_n_evaluations<sizeof...(_As),
  2192. _SimdTuple<_Tp, _As...>>([&](
  2193. auto __i) constexpr { return dd[int(__i)]; });
  2194. }
  2195. _GLIBCXX_SIMD_INTRINSIC constexpr operator const _BuiltinType&() const
  2196. { return _M_data; }
  2197. _GLIBCXX_SIMD_INTRINSIC constexpr operator _BuiltinType&()
  2198. { return _M_data; }
  2199. _GLIBCXX_SIMD_INTRINSIC constexpr _Tp operator[](size_t __i) const
  2200. { return _M_data[__i]; }
  2201. template <size_t __i>
  2202. _GLIBCXX_SIMD_INTRINSIC constexpr _Tp operator[](_SizeConstant<__i>) const
  2203. { return _M_data[__i]; }
  2204. _GLIBCXX_SIMD_INTRINSIC constexpr void _M_set(size_t __i, _Tp __x)
  2205. { _M_data[__i] = __x; }
  2206. _GLIBCXX_SIMD_INTRINSIC
  2207. constexpr bool _M_is_constprop() const
  2208. { return __builtin_constant_p(_M_data); }
  2209. _GLIBCXX_SIMD_INTRINSIC constexpr bool _M_is_constprop_none_of() const
  2210. {
  2211. if (__builtin_constant_p(_M_data))
  2212. {
  2213. bool __r = true;
  2214. if constexpr (is_floating_point_v<_Tp>)
  2215. {
  2216. using _Ip = __int_for_sizeof_t<_Tp>;
  2217. const auto __intdata = __vector_bitcast<_Ip>(_M_data);
  2218. __execute_n_times<_Width>(
  2219. [&](auto __i) { __r &= __intdata[__i.value] == _Ip(); });
  2220. }
  2221. else
  2222. __execute_n_times<_Width>(
  2223. [&](auto __i) { __r &= _M_data[__i.value] == _Tp(); });
  2224. return __r;
  2225. }
  2226. return false;
  2227. }
  2228. _GLIBCXX_SIMD_INTRINSIC constexpr bool _M_is_constprop_all_of() const
  2229. {
  2230. if (__builtin_constant_p(_M_data))
  2231. {
  2232. bool __r = true;
  2233. if constexpr (is_floating_point_v<_Tp>)
  2234. {
  2235. using _Ip = __int_for_sizeof_t<_Tp>;
  2236. const auto __intdata = __vector_bitcast<_Ip>(_M_data);
  2237. __execute_n_times<_Width>(
  2238. [&](auto __i) { __r &= __intdata[__i.value] == ~_Ip(); });
  2239. }
  2240. else
  2241. __execute_n_times<_Width>(
  2242. [&](auto __i) { __r &= _M_data[__i.value] == ~_Tp(); });
  2243. return __r;
  2244. }
  2245. return false;
  2246. }
  2247. };
  2248. // }}}
  2249. // __vectorized_sizeof {{{
  2250. template <typename _Tp>
  2251. constexpr size_t
  2252. __vectorized_sizeof()
  2253. {
  2254. if constexpr (!__is_vectorizable_v<_Tp>)
  2255. return 0;
  2256. if constexpr (sizeof(_Tp) <= 8)
  2257. {
  2258. // X86:
  2259. if constexpr (__have_avx512bw)
  2260. return 64;
  2261. if constexpr (__have_avx512f && sizeof(_Tp) >= 4)
  2262. return 64;
  2263. if constexpr (__have_avx2)
  2264. return 32;
  2265. if constexpr (__have_avx && is_floating_point_v<_Tp>)
  2266. return 32;
  2267. if constexpr (__have_sse2)
  2268. return 16;
  2269. if constexpr (__have_sse && is_same_v<_Tp, float>)
  2270. return 16;
  2271. /* The following is too much trouble because of mixed MMX and x87 code.
  2272. * While nothing here explicitly calls MMX instructions of registers,
  2273. * they are still emitted but no EMMS cleanup is done.
  2274. if constexpr (__have_mmx && sizeof(_Tp) <= 4 && is_integral_v<_Tp>)
  2275. return 8;
  2276. */
  2277. // PowerPC:
  2278. if constexpr (__have_power8vec
  2279. || (__have_power_vmx && (sizeof(_Tp) < 8))
  2280. || (__have_power_vsx && is_floating_point_v<_Tp>) )
  2281. return 16;
  2282. // ARM:
  2283. if constexpr (__have_neon_a64
  2284. || (__have_neon_a32 && !is_same_v<_Tp, double>) )
  2285. return 16;
  2286. if constexpr (__have_neon
  2287. && sizeof(_Tp) < 8
  2288. // Only allow fp if the user allows non-ICE559 fp (e.g.
  2289. // via -ffast-math). ARMv7 NEON fp is not conforming to
  2290. // IEC559.
  2291. && (__support_neon_float || !is_floating_point_v<_Tp>))
  2292. return 16;
  2293. }
  2294. return sizeof(_Tp);
  2295. }
  2296. // }}}
  2297. namespace simd_abi {
  2298. // most of simd_abi is defined in simd_detail.h
  2299. template <typename _Tp>
  2300. inline constexpr int max_fixed_size
  2301. = (__have_avx512bw && sizeof(_Tp) == 1) ? 64 : 32;
  2302. // compatible {{{
  2303. #if defined __x86_64__ || defined __aarch64__
  2304. template <typename _Tp>
  2305. using compatible = conditional_t<(sizeof(_Tp) <= 8), _VecBuiltin<16>, scalar>;
  2306. #elif defined __ARM_NEON
  2307. // FIXME: not sure, probably needs to be scalar (or dependent on the hard-float
  2308. // ABI?)
  2309. template <typename _Tp>
  2310. using compatible
  2311. = conditional_t<(sizeof(_Tp) < 8
  2312. && (__support_neon_float || !is_floating_point_v<_Tp>)),
  2313. _VecBuiltin<16>, scalar>;
  2314. #else
  2315. template <typename>
  2316. using compatible = scalar;
  2317. #endif
  2318. // }}}
  2319. // native {{{
  2320. template <typename _Tp>
  2321. constexpr auto
  2322. __determine_native_abi()
  2323. {
  2324. constexpr size_t __bytes = __vectorized_sizeof<_Tp>();
  2325. if constexpr (__bytes == sizeof(_Tp))
  2326. return static_cast<scalar*>(nullptr);
  2327. else if constexpr (__have_avx512vl || (__have_avx512f && __bytes == 64))
  2328. return static_cast<_VecBltnBtmsk<__bytes>*>(nullptr);
  2329. else
  2330. return static_cast<_VecBuiltin<__bytes>*>(nullptr);
  2331. }
  2332. template <typename _Tp, typename = enable_if_t<__is_vectorizable_v<_Tp>>>
  2333. using native = remove_pointer_t<decltype(__determine_native_abi<_Tp>())>;
  2334. // }}}
  2335. // __default_abi {{{
  2336. #if defined _GLIBCXX_SIMD_DEFAULT_ABI
  2337. template <typename _Tp>
  2338. using __default_abi = _GLIBCXX_SIMD_DEFAULT_ABI<_Tp>;
  2339. #else
  2340. template <typename _Tp>
  2341. using __default_abi = compatible<_Tp>;
  2342. #endif
  2343. // }}}
  2344. } // namespace simd_abi
  2345. // traits {{{1
  2346. // is_abi_tag {{{2
  2347. template <typename _Tp, typename = void_t<>>
  2348. struct is_abi_tag : false_type {};
  2349. template <typename _Tp>
  2350. struct is_abi_tag<_Tp, void_t<typename _Tp::_IsValidAbiTag>>
  2351. : public _Tp::_IsValidAbiTag {};
  2352. template <typename _Tp>
  2353. inline constexpr bool is_abi_tag_v = is_abi_tag<_Tp>::value;
  2354. // is_simd(_mask) {{{2
  2355. template <typename _Tp>
  2356. struct is_simd : public false_type {};
  2357. template <typename _Tp>
  2358. inline constexpr bool is_simd_v = is_simd<_Tp>::value;
  2359. template <typename _Tp>
  2360. struct is_simd_mask : public false_type {};
  2361. template <typename _Tp>
  2362. inline constexpr bool is_simd_mask_v = is_simd_mask<_Tp>::value;
  2363. // simd_size {{{2
  2364. template <typename _Tp, typename _Abi, typename = void>
  2365. struct __simd_size_impl {};
  2366. template <typename _Tp, typename _Abi>
  2367. struct __simd_size_impl<
  2368. _Tp, _Abi,
  2369. enable_if_t<conjunction_v<__is_vectorizable<_Tp>, is_abi_tag<_Abi>>>>
  2370. : _SizeConstant<_Abi::template _S_size<_Tp>> {};
  2371. template <typename _Tp, typename _Abi = simd_abi::__default_abi<_Tp>>
  2372. struct simd_size : __simd_size_impl<_Tp, _Abi> {};
  2373. template <typename _Tp, typename _Abi = simd_abi::__default_abi<_Tp>>
  2374. inline constexpr size_t simd_size_v = simd_size<_Tp, _Abi>::value;
  2375. // simd_abi::deduce {{{2
  2376. template <typename _Tp, size_t _Np, typename = void>
  2377. struct __deduce_impl;
  2378. namespace simd_abi {
  2379. /**
  2380. * @tparam _Tp The requested `value_type` for the elements.
  2381. * @tparam _Np The requested number of elements.
  2382. * @tparam _Abis This parameter is ignored, since this implementation cannot
  2383. * make any use of it. Either __a good native ABI is matched and used as `type`
  2384. * alias, or the `fixed_size<_Np>` ABI is used, which internally is built from
  2385. * the best matching native ABIs.
  2386. */
  2387. template <typename _Tp, size_t _Np, typename...>
  2388. struct deduce : __deduce_impl<_Tp, _Np> {};
  2389. template <typename _Tp, size_t _Np, typename... _Abis>
  2390. using deduce_t = typename deduce<_Tp, _Np, _Abis...>::type;
  2391. } // namespace simd_abi
  2392. // }}}2
  2393. // rebind_simd {{{2
  2394. template <typename _Tp, typename _V, typename = void>
  2395. struct rebind_simd;
  2396. template <typename _Tp, typename _Up, typename _Abi>
  2397. struct rebind_simd<
  2398. _Tp, simd<_Up, _Abi>,
  2399. void_t<simd_abi::deduce_t<_Tp, simd_size_v<_Up, _Abi>, _Abi>>>
  2400. {
  2401. using type
  2402. = simd<_Tp, simd_abi::deduce_t<_Tp, simd_size_v<_Up, _Abi>, _Abi>>;
  2403. };
  2404. template <typename _Tp, typename _Up, typename _Abi>
  2405. struct rebind_simd<
  2406. _Tp, simd_mask<_Up, _Abi>,
  2407. void_t<simd_abi::deduce_t<_Tp, simd_size_v<_Up, _Abi>, _Abi>>>
  2408. {
  2409. using type
  2410. = simd_mask<_Tp, simd_abi::deduce_t<_Tp, simd_size_v<_Up, _Abi>, _Abi>>;
  2411. };
  2412. template <typename _Tp, typename _V>
  2413. using rebind_simd_t = typename rebind_simd<_Tp, _V>::type;
  2414. // resize_simd {{{2
  2415. template <int _Np, typename _V, typename = void>
  2416. struct resize_simd;
  2417. template <int _Np, typename _Tp, typename _Abi>
  2418. struct resize_simd<_Np, simd<_Tp, _Abi>,
  2419. void_t<simd_abi::deduce_t<_Tp, _Np, _Abi>>>
  2420. { using type = simd<_Tp, simd_abi::deduce_t<_Tp, _Np, _Abi>>; };
  2421. template <int _Np, typename _Tp, typename _Abi>
  2422. struct resize_simd<_Np, simd_mask<_Tp, _Abi>,
  2423. void_t<simd_abi::deduce_t<_Tp, _Np, _Abi>>>
  2424. { using type = simd_mask<_Tp, simd_abi::deduce_t<_Tp, _Np, _Abi>>; };
  2425. template <int _Np, typename _V>
  2426. using resize_simd_t = typename resize_simd<_Np, _V>::type;
  2427. // }}}2
  2428. // memory_alignment {{{2
  2429. template <typename _Tp, typename _Up = typename _Tp::value_type>
  2430. struct memory_alignment
  2431. : public _SizeConstant<vector_aligned_tag::_S_alignment<_Tp, _Up>> {};
  2432. template <typename _Tp, typename _Up = typename _Tp::value_type>
  2433. inline constexpr size_t memory_alignment_v = memory_alignment<_Tp, _Up>::value;
  2434. // class template simd [simd] {{{1
  2435. template <typename _Tp, typename _Abi = simd_abi::__default_abi<_Tp>>
  2436. class simd;
  2437. template <typename _Tp, typename _Abi>
  2438. struct is_simd<simd<_Tp, _Abi>> : public true_type {};
  2439. template <typename _Tp>
  2440. using native_simd = simd<_Tp, simd_abi::native<_Tp>>;
  2441. template <typename _Tp, int _Np>
  2442. using fixed_size_simd = simd<_Tp, simd_abi::fixed_size<_Np>>;
  2443. template <typename _Tp, size_t _Np>
  2444. using __deduced_simd = simd<_Tp, simd_abi::deduce_t<_Tp, _Np>>;
  2445. // class template simd_mask [simd_mask] {{{1
  2446. template <typename _Tp, typename _Abi = simd_abi::__default_abi<_Tp>>
  2447. class simd_mask;
  2448. template <typename _Tp, typename _Abi>
  2449. struct is_simd_mask<simd_mask<_Tp, _Abi>> : public true_type {};
  2450. template <typename _Tp>
  2451. using native_simd_mask = simd_mask<_Tp, simd_abi::native<_Tp>>;
  2452. template <typename _Tp, int _Np>
  2453. using fixed_size_simd_mask = simd_mask<_Tp, simd_abi::fixed_size<_Np>>;
  2454. template <typename _Tp, size_t _Np>
  2455. using __deduced_simd_mask = simd_mask<_Tp, simd_abi::deduce_t<_Tp, _Np>>;
  2456. // casts [simd.casts] {{{1
  2457. // static_simd_cast {{{2
  2458. template <typename _Tp, typename _Up, typename _Ap, bool = is_simd_v<_Tp>,
  2459. typename = void>
  2460. struct __static_simd_cast_return_type;
  2461. template <typename _Tp, typename _A0, typename _Up, typename _Ap>
  2462. struct __static_simd_cast_return_type<simd_mask<_Tp, _A0>, _Up, _Ap, false,
  2463. void>
  2464. : __static_simd_cast_return_type<simd<_Tp, _A0>, _Up, _Ap> {};
  2465. template <typename _Tp, typename _Up, typename _Ap>
  2466. struct __static_simd_cast_return_type<
  2467. _Tp, _Up, _Ap, true, enable_if_t<_Tp::size() == simd_size_v<_Up, _Ap>>>
  2468. { using type = _Tp; };
  2469. template <typename _Tp, typename _Ap>
  2470. struct __static_simd_cast_return_type<_Tp, _Tp, _Ap, false,
  2471. #ifdef _GLIBCXX_SIMD_FIX_P2TS_ISSUE66
  2472. enable_if_t<__is_vectorizable_v<_Tp>>
  2473. #else
  2474. void
  2475. #endif
  2476. >
  2477. { using type = simd<_Tp, _Ap>; };
  2478. template <typename _Tp, typename = void>
  2479. struct __safe_make_signed { using type = _Tp;};
  2480. template <typename _Tp>
  2481. struct __safe_make_signed<_Tp, enable_if_t<is_integral_v<_Tp>>>
  2482. {
  2483. // the extra make_unsigned_t is because of PR85951
  2484. using type = make_signed_t<make_unsigned_t<_Tp>>;
  2485. };
  2486. template <typename _Tp>
  2487. using safe_make_signed_t = typename __safe_make_signed<_Tp>::type;
  2488. template <typename _Tp, typename _Up, typename _Ap>
  2489. struct __static_simd_cast_return_type<_Tp, _Up, _Ap, false,
  2490. #ifdef _GLIBCXX_SIMD_FIX_P2TS_ISSUE66
  2491. enable_if_t<__is_vectorizable_v<_Tp>>
  2492. #else
  2493. void
  2494. #endif
  2495. >
  2496. {
  2497. using type = conditional_t<
  2498. (is_integral_v<_Up> && is_integral_v<_Tp> &&
  2499. #ifndef _GLIBCXX_SIMD_FIX_P2TS_ISSUE65
  2500. is_signed_v<_Up> != is_signed_v<_Tp> &&
  2501. #endif
  2502. is_same_v<safe_make_signed_t<_Up>, safe_make_signed_t<_Tp>>),
  2503. simd<_Tp, _Ap>, fixed_size_simd<_Tp, simd_size_v<_Up, _Ap>>>;
  2504. };
  2505. template <typename _Tp, typename _Up, typename _Ap,
  2506. typename _R
  2507. = typename __static_simd_cast_return_type<_Tp, _Up, _Ap>::type>
  2508. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _R
  2509. static_simd_cast(const simd<_Up, _Ap>& __x)
  2510. {
  2511. if constexpr (is_same<_R, simd<_Up, _Ap>>::value)
  2512. return __x;
  2513. else
  2514. {
  2515. _SimdConverter<_Up, _Ap, typename _R::value_type, typename _R::abi_type>
  2516. __c;
  2517. return _R(__private_init, __c(__data(__x)));
  2518. }
  2519. }
  2520. namespace __proposed {
  2521. template <typename _Tp, typename _Up, typename _Ap,
  2522. typename _R
  2523. = typename __static_simd_cast_return_type<_Tp, _Up, _Ap>::type>
  2524. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR typename _R::mask_type
  2525. static_simd_cast(const simd_mask<_Up, _Ap>& __x)
  2526. {
  2527. using _RM = typename _R::mask_type;
  2528. return {__private_init, _RM::abi_type::_MaskImpl::template _S_convert<
  2529. typename _RM::simd_type::value_type>(__x)};
  2530. }
  2531. } // namespace __proposed
  2532. // simd_cast {{{2
  2533. template <typename _Tp, typename _Up, typename _Ap,
  2534. typename _To = __value_type_or_identity_t<_Tp>>
  2535. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR auto
  2536. simd_cast(const simd<_ValuePreserving<_Up, _To>, _Ap>& __x)
  2537. -> decltype(static_simd_cast<_Tp>(__x))
  2538. { return static_simd_cast<_Tp>(__x); }
  2539. namespace __proposed {
  2540. template <typename _Tp, typename _Up, typename _Ap,
  2541. typename _To = __value_type_or_identity_t<_Tp>>
  2542. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR auto
  2543. simd_cast(const simd_mask<_ValuePreserving<_Up, _To>, _Ap>& __x)
  2544. -> decltype(static_simd_cast<_Tp>(__x))
  2545. { return static_simd_cast<_Tp>(__x); }
  2546. } // namespace __proposed
  2547. // }}}2
  2548. // resizing_simd_cast {{{
  2549. namespace __proposed {
  2550. /* Proposed spec:
  2551. template <class T, class U, class Abi>
  2552. T resizing_simd_cast(const simd<U, Abi>& x)
  2553. p1 Constraints:
  2554. - is_simd_v<T> is true and
  2555. - T::value_type is the same type as U
  2556. p2 Returns:
  2557. A simd object with the i^th element initialized to x[i] for all i in the
  2558. range of [0, min(T::size(), simd_size_v<U, Abi>)). If T::size() is larger
  2559. than simd_size_v<U, Abi>, the remaining elements are value-initialized.
  2560. template <class T, class U, class Abi>
  2561. T resizing_simd_cast(const simd_mask<U, Abi>& x)
  2562. p1 Constraints: is_simd_mask_v<T> is true
  2563. p2 Returns:
  2564. A simd_mask object with the i^th element initialized to x[i] for all i in
  2565. the range of [0, min(T::size(), simd_size_v<U, Abi>)). If T::size() is larger
  2566. than simd_size_v<U, Abi>, the remaining elements are initialized to false.
  2567. */
  2568. template <typename _Tp, typename _Up, typename _Ap>
  2569. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR enable_if_t<
  2570. conjunction_v<is_simd<_Tp>, is_same<typename _Tp::value_type, _Up>>, _Tp>
  2571. resizing_simd_cast(const simd<_Up, _Ap>& __x)
  2572. {
  2573. if constexpr (is_same_v<typename _Tp::abi_type, _Ap>)
  2574. return __x;
  2575. else if constexpr (simd_size_v<_Up, _Ap> == 1)
  2576. {
  2577. _Tp __r{};
  2578. __r[0] = __x[0];
  2579. return __r;
  2580. }
  2581. else if constexpr (_Tp::size() == 1)
  2582. return __x[0];
  2583. else if constexpr (sizeof(_Tp) == sizeof(__x)
  2584. && !__is_fixed_size_abi_v<_Ap>)
  2585. return {__private_init,
  2586. __vector_bitcast<typename _Tp::value_type, _Tp::size()>(
  2587. _Ap::_S_masked(__data(__x))._M_data)};
  2588. else
  2589. {
  2590. _Tp __r{};
  2591. __builtin_memcpy(&__data(__r), &__data(__x),
  2592. sizeof(_Up)
  2593. * std::min(_Tp::size(), simd_size_v<_Up, _Ap>));
  2594. return __r;
  2595. }
  2596. }
  2597. template <typename _Tp, typename _Up, typename _Ap>
  2598. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
  2599. enable_if_t<is_simd_mask_v<_Tp>, _Tp>
  2600. resizing_simd_cast(const simd_mask<_Up, _Ap>& __x)
  2601. {
  2602. return {__private_init, _Tp::abi_type::_MaskImpl::template _S_convert<
  2603. typename _Tp::simd_type::value_type>(__x)};
  2604. }
  2605. } // namespace __proposed
  2606. // }}}
  2607. // to_fixed_size {{{2
  2608. template <typename _Tp, int _Np>
  2609. _GLIBCXX_SIMD_INTRINSIC fixed_size_simd<_Tp, _Np>
  2610. to_fixed_size(const fixed_size_simd<_Tp, _Np>& __x)
  2611. { return __x; }
  2612. template <typename _Tp, int _Np>
  2613. _GLIBCXX_SIMD_INTRINSIC fixed_size_simd_mask<_Tp, _Np>
  2614. to_fixed_size(const fixed_size_simd_mask<_Tp, _Np>& __x)
  2615. { return __x; }
  2616. template <typename _Tp, typename _Ap>
  2617. _GLIBCXX_SIMD_INTRINSIC auto
  2618. to_fixed_size(const simd<_Tp, _Ap>& __x)
  2619. {
  2620. return simd<_Tp, simd_abi::fixed_size<simd_size_v<_Tp, _Ap>>>([&__x](
  2621. auto __i) constexpr { return __x[__i]; });
  2622. }
  2623. template <typename _Tp, typename _Ap>
  2624. _GLIBCXX_SIMD_INTRINSIC auto
  2625. to_fixed_size(const simd_mask<_Tp, _Ap>& __x)
  2626. {
  2627. constexpr int _Np = simd_mask<_Tp, _Ap>::size();
  2628. fixed_size_simd_mask<_Tp, _Np> __r;
  2629. __execute_n_times<_Np>([&](auto __i) constexpr { __r[__i] = __x[__i]; });
  2630. return __r;
  2631. }
  2632. // to_native {{{2
  2633. template <typename _Tp, int _Np>
  2634. _GLIBCXX_SIMD_INTRINSIC
  2635. enable_if_t<(_Np == native_simd<_Tp>::size()), native_simd<_Tp>>
  2636. to_native(const fixed_size_simd<_Tp, _Np>& __x)
  2637. {
  2638. alignas(memory_alignment_v<native_simd<_Tp>>) _Tp __mem[_Np];
  2639. __x.copy_to(__mem, vector_aligned);
  2640. return {__mem, vector_aligned};
  2641. }
  2642. template <typename _Tp, size_t _Np>
  2643. _GLIBCXX_SIMD_INTRINSIC
  2644. enable_if_t<(_Np == native_simd_mask<_Tp>::size()), native_simd_mask<_Tp>>
  2645. to_native(const fixed_size_simd_mask<_Tp, _Np>& __x)
  2646. {
  2647. return native_simd_mask<_Tp>([&](auto __i) constexpr { return __x[__i]; });
  2648. }
  2649. // to_compatible {{{2
  2650. template <typename _Tp, size_t _Np>
  2651. _GLIBCXX_SIMD_INTRINSIC enable_if_t<(_Np == simd<_Tp>::size()), simd<_Tp>>
  2652. to_compatible(const simd<_Tp, simd_abi::fixed_size<_Np>>& __x)
  2653. {
  2654. alignas(memory_alignment_v<simd<_Tp>>) _Tp __mem[_Np];
  2655. __x.copy_to(__mem, vector_aligned);
  2656. return {__mem, vector_aligned};
  2657. }
  2658. template <typename _Tp, size_t _Np>
  2659. _GLIBCXX_SIMD_INTRINSIC
  2660. enable_if_t<(_Np == simd_mask<_Tp>::size()), simd_mask<_Tp>>
  2661. to_compatible(const simd_mask<_Tp, simd_abi::fixed_size<_Np>>& __x)
  2662. { return simd_mask<_Tp>([&](auto __i) constexpr { return __x[__i]; }); }
  2663. // masked assignment [simd_mask.where] {{{1
  2664. // where_expression {{{1
  2665. // const_where_expression<M, T> {{{2
  2666. template <typename _M, typename _Tp>
  2667. class const_where_expression
  2668. {
  2669. using _V = _Tp;
  2670. static_assert(is_same_v<_V, __remove_cvref_t<_Tp>>);
  2671. struct _Wrapper { using value_type = _V; };
  2672. protected:
  2673. using _Impl = typename _V::_Impl;
  2674. using value_type =
  2675. typename conditional_t<is_arithmetic_v<_V>, _Wrapper, _V>::value_type;
  2676. _GLIBCXX_SIMD_INTRINSIC friend const _M&
  2677. __get_mask(const const_where_expression& __x)
  2678. { return __x._M_k; }
  2679. _GLIBCXX_SIMD_INTRINSIC friend const _Tp&
  2680. __get_lvalue(const const_where_expression& __x)
  2681. { return __x._M_value; }
  2682. const _M& _M_k;
  2683. _Tp& _M_value;
  2684. public:
  2685. const_where_expression(const const_where_expression&) = delete;
  2686. const_where_expression& operator=(const const_where_expression&) = delete;
  2687. _GLIBCXX_SIMD_INTRINSIC const_where_expression(const _M& __kk, const _Tp& dd)
  2688. : _M_k(__kk), _M_value(const_cast<_Tp&>(dd)) {}
  2689. _GLIBCXX_SIMD_INTRINSIC _V
  2690. operator-() const&&
  2691. {
  2692. return {__private_init,
  2693. _Impl::template _S_masked_unary<negate>(__data(_M_k),
  2694. __data(_M_value))};
  2695. }
  2696. template <typename _Up, typename _Flags>
  2697. [[nodiscard]] _GLIBCXX_SIMD_INTRINSIC _V
  2698. copy_from(const _LoadStorePtr<_Up, value_type>* __mem, _Flags) const&&
  2699. {
  2700. return {__private_init,
  2701. _Impl::_S_masked_load(__data(_M_value), __data(_M_k),
  2702. _Flags::template _S_apply<_V>(__mem))};
  2703. }
  2704. template <typename _Up, typename _Flags>
  2705. _GLIBCXX_SIMD_INTRINSIC void
  2706. copy_to(_LoadStorePtr<_Up, value_type>* __mem, _Flags) const&&
  2707. {
  2708. _Impl::_S_masked_store(__data(_M_value),
  2709. _Flags::template _S_apply<_V>(__mem),
  2710. __data(_M_k));
  2711. }
  2712. };
  2713. // const_where_expression<bool, T> {{{2
  2714. template <typename _Tp>
  2715. class const_where_expression<bool, _Tp>
  2716. {
  2717. using _M = bool;
  2718. using _V = _Tp;
  2719. static_assert(is_same_v<_V, __remove_cvref_t<_Tp>>);
  2720. struct _Wrapper { using value_type = _V; };
  2721. protected:
  2722. using value_type =
  2723. typename conditional_t<is_arithmetic_v<_V>, _Wrapper, _V>::value_type;
  2724. _GLIBCXX_SIMD_INTRINSIC friend const _M&
  2725. __get_mask(const const_where_expression& __x)
  2726. { return __x._M_k; }
  2727. _GLIBCXX_SIMD_INTRINSIC friend const _Tp&
  2728. __get_lvalue(const const_where_expression& __x)
  2729. { return __x._M_value; }
  2730. const bool _M_k;
  2731. _Tp& _M_value;
  2732. public:
  2733. const_where_expression(const const_where_expression&) = delete;
  2734. const_where_expression& operator=(const const_where_expression&) = delete;
  2735. _GLIBCXX_SIMD_INTRINSIC const_where_expression(const bool __kk, const _Tp& dd)
  2736. : _M_k(__kk), _M_value(const_cast<_Tp&>(dd)) {}
  2737. _GLIBCXX_SIMD_INTRINSIC _V operator-() const&&
  2738. { return _M_k ? -_M_value : _M_value; }
  2739. template <typename _Up, typename _Flags>
  2740. [[nodiscard]] _GLIBCXX_SIMD_INTRINSIC _V
  2741. copy_from(const _LoadStorePtr<_Up, value_type>* __mem, _Flags) const&&
  2742. { return _M_k ? static_cast<_V>(__mem[0]) : _M_value; }
  2743. template <typename _Up, typename _Flags>
  2744. _GLIBCXX_SIMD_INTRINSIC void
  2745. copy_to(_LoadStorePtr<_Up, value_type>* __mem, _Flags) const&&
  2746. {
  2747. if (_M_k)
  2748. __mem[0] = _M_value;
  2749. }
  2750. };
  2751. // where_expression<M, T> {{{2
  2752. template <typename _M, typename _Tp>
  2753. class where_expression : public const_where_expression<_M, _Tp>
  2754. {
  2755. using _Impl = typename const_where_expression<_M, _Tp>::_Impl;
  2756. static_assert(!is_const<_Tp>::value,
  2757. "where_expression may only be instantiated with __a non-const "
  2758. "_Tp parameter");
  2759. using typename const_where_expression<_M, _Tp>::value_type;
  2760. using const_where_expression<_M, _Tp>::_M_k;
  2761. using const_where_expression<_M, _Tp>::_M_value;
  2762. static_assert(
  2763. is_same<typename _M::abi_type, typename _Tp::abi_type>::value, "");
  2764. static_assert(_M::size() == _Tp::size(), "");
  2765. _GLIBCXX_SIMD_INTRINSIC friend _Tp& __get_lvalue(where_expression& __x)
  2766. { return __x._M_value; }
  2767. public:
  2768. where_expression(const where_expression&) = delete;
  2769. where_expression& operator=(const where_expression&) = delete;
  2770. _GLIBCXX_SIMD_INTRINSIC where_expression(const _M& __kk, _Tp& dd)
  2771. : const_where_expression<_M, _Tp>(__kk, dd) {}
  2772. template <typename _Up>
  2773. _GLIBCXX_SIMD_INTRINSIC void operator=(_Up&& __x) &&
  2774. {
  2775. _Impl::_S_masked_assign(__data(_M_k), __data(_M_value),
  2776. __to_value_type_or_member_type<_Tp>(
  2777. static_cast<_Up&&>(__x)));
  2778. }
  2779. #define _GLIBCXX_SIMD_OP_(__op, __name) \
  2780. template <typename _Up> \
  2781. _GLIBCXX_SIMD_INTRINSIC void operator __op##=(_Up&& __x)&& \
  2782. { \
  2783. _Impl::template _S_masked_cassign( \
  2784. __data(_M_k), __data(_M_value), \
  2785. __to_value_type_or_member_type<_Tp>(static_cast<_Up&&>(__x)), \
  2786. [](auto __impl, auto __lhs, auto __rhs) constexpr { \
  2787. return __impl.__name(__lhs, __rhs); \
  2788. }); \
  2789. } \
  2790. static_assert(true)
  2791. _GLIBCXX_SIMD_OP_(+, _S_plus);
  2792. _GLIBCXX_SIMD_OP_(-, _S_minus);
  2793. _GLIBCXX_SIMD_OP_(*, _S_multiplies);
  2794. _GLIBCXX_SIMD_OP_(/, _S_divides);
  2795. _GLIBCXX_SIMD_OP_(%, _S_modulus);
  2796. _GLIBCXX_SIMD_OP_(&, _S_bit_and);
  2797. _GLIBCXX_SIMD_OP_(|, _S_bit_or);
  2798. _GLIBCXX_SIMD_OP_(^, _S_bit_xor);
  2799. _GLIBCXX_SIMD_OP_(<<, _S_shift_left);
  2800. _GLIBCXX_SIMD_OP_(>>, _S_shift_right);
  2801. #undef _GLIBCXX_SIMD_OP_
  2802. _GLIBCXX_SIMD_INTRINSIC void operator++() &&
  2803. {
  2804. __data(_M_value)
  2805. = _Impl::template _S_masked_unary<__increment>(__data(_M_k),
  2806. __data(_M_value));
  2807. }
  2808. _GLIBCXX_SIMD_INTRINSIC void operator++(int) &&
  2809. {
  2810. __data(_M_value)
  2811. = _Impl::template _S_masked_unary<__increment>(__data(_M_k),
  2812. __data(_M_value));
  2813. }
  2814. _GLIBCXX_SIMD_INTRINSIC void operator--() &&
  2815. {
  2816. __data(_M_value)
  2817. = _Impl::template _S_masked_unary<__decrement>(__data(_M_k),
  2818. __data(_M_value));
  2819. }
  2820. _GLIBCXX_SIMD_INTRINSIC void operator--(int) &&
  2821. {
  2822. __data(_M_value)
  2823. = _Impl::template _S_masked_unary<__decrement>(__data(_M_k),
  2824. __data(_M_value));
  2825. }
  2826. // intentionally hides const_where_expression::copy_from
  2827. template <typename _Up, typename _Flags>
  2828. _GLIBCXX_SIMD_INTRINSIC void
  2829. copy_from(const _LoadStorePtr<_Up, value_type>* __mem, _Flags) &&
  2830. {
  2831. __data(_M_value)
  2832. = _Impl::_S_masked_load(__data(_M_value), __data(_M_k),
  2833. _Flags::template _S_apply<_Tp>(__mem));
  2834. }
  2835. };
  2836. // where_expression<bool, T> {{{2
  2837. template <typename _Tp>
  2838. class where_expression<bool, _Tp> : public const_where_expression<bool, _Tp>
  2839. {
  2840. using _M = bool;
  2841. using typename const_where_expression<_M, _Tp>::value_type;
  2842. using const_where_expression<_M, _Tp>::_M_k;
  2843. using const_where_expression<_M, _Tp>::_M_value;
  2844. public:
  2845. where_expression(const where_expression&) = delete;
  2846. where_expression& operator=(const where_expression&) = delete;
  2847. _GLIBCXX_SIMD_INTRINSIC where_expression(const _M& __kk, _Tp& dd)
  2848. : const_where_expression<_M, _Tp>(__kk, dd) {}
  2849. #define _GLIBCXX_SIMD_OP_(__op) \
  2850. template <typename _Up> \
  2851. _GLIBCXX_SIMD_INTRINSIC void operator __op(_Up&& __x)&& \
  2852. { if (_M_k) _M_value __op static_cast<_Up&&>(__x); }
  2853. _GLIBCXX_SIMD_OP_(=)
  2854. _GLIBCXX_SIMD_OP_(+=)
  2855. _GLIBCXX_SIMD_OP_(-=)
  2856. _GLIBCXX_SIMD_OP_(*=)
  2857. _GLIBCXX_SIMD_OP_(/=)
  2858. _GLIBCXX_SIMD_OP_(%=)
  2859. _GLIBCXX_SIMD_OP_(&=)
  2860. _GLIBCXX_SIMD_OP_(|=)
  2861. _GLIBCXX_SIMD_OP_(^=)
  2862. _GLIBCXX_SIMD_OP_(<<=)
  2863. _GLIBCXX_SIMD_OP_(>>=)
  2864. #undef _GLIBCXX_SIMD_OP_
  2865. _GLIBCXX_SIMD_INTRINSIC void operator++() &&
  2866. { if (_M_k) ++_M_value; }
  2867. _GLIBCXX_SIMD_INTRINSIC void operator++(int) &&
  2868. { if (_M_k) ++_M_value; }
  2869. _GLIBCXX_SIMD_INTRINSIC void operator--() &&
  2870. { if (_M_k) --_M_value; }
  2871. _GLIBCXX_SIMD_INTRINSIC void operator--(int) &&
  2872. { if (_M_k) --_M_value; }
  2873. // intentionally hides const_where_expression::copy_from
  2874. template <typename _Up, typename _Flags>
  2875. _GLIBCXX_SIMD_INTRINSIC void
  2876. copy_from(const _LoadStorePtr<_Up, value_type>* __mem, _Flags) &&
  2877. { if (_M_k) _M_value = __mem[0]; }
  2878. };
  2879. // where {{{1
  2880. template <typename _Tp, typename _Ap>
  2881. _GLIBCXX_SIMD_INTRINSIC where_expression<simd_mask<_Tp, _Ap>, simd<_Tp, _Ap>>
  2882. where(const typename simd<_Tp, _Ap>::mask_type& __k, simd<_Tp, _Ap>& __value)
  2883. { return {__k, __value}; }
  2884. template <typename _Tp, typename _Ap>
  2885. _GLIBCXX_SIMD_INTRINSIC
  2886. const_where_expression<simd_mask<_Tp, _Ap>, simd<_Tp, _Ap>>
  2887. where(const typename simd<_Tp, _Ap>::mask_type& __k,
  2888. const simd<_Tp, _Ap>& __value)
  2889. { return {__k, __value}; }
  2890. template <typename _Tp, typename _Ap>
  2891. _GLIBCXX_SIMD_INTRINSIC
  2892. where_expression<simd_mask<_Tp, _Ap>, simd_mask<_Tp, _Ap>>
  2893. where(const remove_const_t<simd_mask<_Tp, _Ap>>& __k,
  2894. simd_mask<_Tp, _Ap>& __value)
  2895. { return {__k, __value}; }
  2896. template <typename _Tp, typename _Ap>
  2897. _GLIBCXX_SIMD_INTRINSIC
  2898. const_where_expression<simd_mask<_Tp, _Ap>, simd_mask<_Tp, _Ap>>
  2899. where(const remove_const_t<simd_mask<_Tp, _Ap>>& __k,
  2900. const simd_mask<_Tp, _Ap>& __value)
  2901. { return {__k, __value}; }
  2902. template <typename _Tp>
  2903. _GLIBCXX_SIMD_INTRINSIC where_expression<bool, _Tp>
  2904. where(_ExactBool __k, _Tp& __value)
  2905. { return {__k, __value}; }
  2906. template <typename _Tp>
  2907. _GLIBCXX_SIMD_INTRINSIC const_where_expression<bool, _Tp>
  2908. where(_ExactBool __k, const _Tp& __value)
  2909. { return {__k, __value}; }
  2910. template <typename _Tp, typename _Ap>
  2911. void where(bool __k, simd<_Tp, _Ap>& __value) = delete;
  2912. template <typename _Tp, typename _Ap>
  2913. void where(bool __k, const simd<_Tp, _Ap>& __value) = delete;
  2914. // proposed mask iterations {{{1
  2915. namespace __proposed {
  2916. template <size_t _Np>
  2917. class where_range
  2918. {
  2919. const bitset<_Np> __bits;
  2920. public:
  2921. where_range(bitset<_Np> __b) : __bits(__b) {}
  2922. class iterator
  2923. {
  2924. size_t __mask;
  2925. size_t __bit;
  2926. _GLIBCXX_SIMD_INTRINSIC void __next_bit()
  2927. { __bit = __builtin_ctzl(__mask); }
  2928. _GLIBCXX_SIMD_INTRINSIC void __reset_lsb()
  2929. {
  2930. // 01100100 - 1 = 01100011
  2931. __mask &= (__mask - 1);
  2932. // __asm__("btr %1,%0" : "+r"(__mask) : "r"(__bit));
  2933. }
  2934. public:
  2935. iterator(decltype(__mask) __m) : __mask(__m) { __next_bit(); }
  2936. iterator(const iterator&) = default;
  2937. iterator(iterator&&) = default;
  2938. _GLIBCXX_SIMD_ALWAYS_INLINE size_t operator->() const
  2939. { return __bit; }
  2940. _GLIBCXX_SIMD_ALWAYS_INLINE size_t operator*() const
  2941. { return __bit; }
  2942. _GLIBCXX_SIMD_ALWAYS_INLINE iterator& operator++()
  2943. {
  2944. __reset_lsb();
  2945. __next_bit();
  2946. return *this;
  2947. }
  2948. _GLIBCXX_SIMD_ALWAYS_INLINE iterator operator++(int)
  2949. {
  2950. iterator __tmp = *this;
  2951. __reset_lsb();
  2952. __next_bit();
  2953. return __tmp;
  2954. }
  2955. _GLIBCXX_SIMD_ALWAYS_INLINE bool operator==(const iterator& __rhs) const
  2956. { return __mask == __rhs.__mask; }
  2957. _GLIBCXX_SIMD_ALWAYS_INLINE bool operator!=(const iterator& __rhs) const
  2958. { return __mask != __rhs.__mask; }
  2959. };
  2960. iterator begin() const
  2961. { return __bits.to_ullong(); }
  2962. iterator end() const
  2963. { return 0; }
  2964. };
  2965. template <typename _Tp, typename _Ap>
  2966. where_range<simd_size_v<_Tp, _Ap>>
  2967. where(const simd_mask<_Tp, _Ap>& __k)
  2968. { return __k.__to_bitset(); }
  2969. } // namespace __proposed
  2970. // }}}1
  2971. // reductions [simd.reductions] {{{1
  2972. template <typename _Tp, typename _Abi, typename _BinaryOperation = plus<>>
  2973. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
  2974. reduce(const simd<_Tp, _Abi>& __v,
  2975. _BinaryOperation __binary_op = _BinaryOperation())
  2976. { return _Abi::_SimdImpl::_S_reduce(__v, __binary_op); }
  2977. template <typename _M, typename _V, typename _BinaryOperation = plus<>>
  2978. _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
  2979. reduce(const const_where_expression<_M, _V>& __x,
  2980. typename _V::value_type __identity_element,
  2981. _BinaryOperation __binary_op)
  2982. {
  2983. if (__builtin_expect(none_of(__get_mask(__x)), false))
  2984. return __identity_element;
  2985. _V __tmp = __identity_element;
  2986. _V::_Impl::_S_masked_assign(__data(__get_mask(__x)), __data(__tmp),
  2987. __data(__get_lvalue(__x)));
  2988. return reduce(__tmp, __binary_op);
  2989. }
  2990. template <typename _M, typename _V>
  2991. _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
  2992. reduce(const const_where_expression<_M, _V>& __x, plus<> __binary_op = {})
  2993. { return reduce(__x, 0, __binary_op); }
  2994. template <typename _M, typename _V>
  2995. _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
  2996. reduce(const const_where_expression<_M, _V>& __x, multiplies<> __binary_op)
  2997. { return reduce(__x, 1, __binary_op); }
  2998. template <typename _M, typename _V>
  2999. _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
  3000. reduce(const const_where_expression<_M, _V>& __x, bit_and<> __binary_op)
  3001. { return reduce(__x, ~typename _V::value_type(), __binary_op); }
  3002. template <typename _M, typename _V>
  3003. _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
  3004. reduce(const const_where_expression<_M, _V>& __x, bit_or<> __binary_op)
  3005. { return reduce(__x, 0, __binary_op); }
  3006. template <typename _M, typename _V>
  3007. _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
  3008. reduce(const const_where_expression<_M, _V>& __x, bit_xor<> __binary_op)
  3009. { return reduce(__x, 0, __binary_op); }
  3010. template <typename _Tp, typename _Abi>
  3011. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
  3012. hmin(const simd<_Tp, _Abi>& __v) noexcept
  3013. {
  3014. return _Abi::_SimdImpl::_S_reduce(__v, __detail::_Minimum());
  3015. }
  3016. template <typename _Tp, typename _Abi>
  3017. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
  3018. hmax(const simd<_Tp, _Abi>& __v) noexcept
  3019. {
  3020. return _Abi::_SimdImpl::_S_reduce(__v, __detail::_Maximum());
  3021. }
  3022. template <typename _M, typename _V>
  3023. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
  3024. typename _V::value_type
  3025. hmin(const const_where_expression<_M, _V>& __x) noexcept
  3026. {
  3027. using _Tp = typename _V::value_type;
  3028. constexpr _Tp __id_elem =
  3029. #ifdef __FINITE_MATH_ONLY__
  3030. __finite_max_v<_Tp>;
  3031. #else
  3032. __value_or<__infinity, _Tp>(__finite_max_v<_Tp>);
  3033. #endif
  3034. _V __tmp = __id_elem;
  3035. _V::_Impl::_S_masked_assign(__data(__get_mask(__x)), __data(__tmp),
  3036. __data(__get_lvalue(__x)));
  3037. return _V::abi_type::_SimdImpl::_S_reduce(__tmp, __detail::_Minimum());
  3038. }
  3039. template <typename _M, typename _V>
  3040. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
  3041. typename _V::value_type
  3042. hmax(const const_where_expression<_M, _V>& __x) noexcept
  3043. {
  3044. using _Tp = typename _V::value_type;
  3045. constexpr _Tp __id_elem =
  3046. #ifdef __FINITE_MATH_ONLY__
  3047. __finite_min_v<_Tp>;
  3048. #else
  3049. [] {
  3050. if constexpr (__value_exists_v<__infinity, _Tp>)
  3051. return -__infinity_v<_Tp>;
  3052. else
  3053. return __finite_min_v<_Tp>;
  3054. }();
  3055. #endif
  3056. _V __tmp = __id_elem;
  3057. _V::_Impl::_S_masked_assign(__data(__get_mask(__x)), __data(__tmp),
  3058. __data(__get_lvalue(__x)));
  3059. return _V::abi_type::_SimdImpl::_S_reduce(__tmp, __detail::_Maximum());
  3060. }
  3061. // }}}1
  3062. // algorithms [simd.alg] {{{
  3063. template <typename _Tp, typename _Ap>
  3064. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
  3065. min(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
  3066. { return {__private_init, _Ap::_SimdImpl::_S_min(__data(__a), __data(__b))}; }
  3067. template <typename _Tp, typename _Ap>
  3068. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
  3069. max(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
  3070. { return {__private_init, _Ap::_SimdImpl::_S_max(__data(__a), __data(__b))}; }
  3071. template <typename _Tp, typename _Ap>
  3072. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
  3073. pair<simd<_Tp, _Ap>, simd<_Tp, _Ap>>
  3074. minmax(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
  3075. {
  3076. const auto pair_of_members
  3077. = _Ap::_SimdImpl::_S_minmax(__data(__a), __data(__b));
  3078. return {simd<_Tp, _Ap>(__private_init, pair_of_members.first),
  3079. simd<_Tp, _Ap>(__private_init, pair_of_members.second)};
  3080. }
  3081. template <typename _Tp, typename _Ap>
  3082. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
  3083. clamp(const simd<_Tp, _Ap>& __v, const simd<_Tp, _Ap>& __lo,
  3084. const simd<_Tp, _Ap>& __hi)
  3085. {
  3086. using _Impl = typename _Ap::_SimdImpl;
  3087. return {__private_init,
  3088. _Impl::_S_min(__data(__hi),
  3089. _Impl::_S_max(__data(__lo), __data(__v)))};
  3090. }
  3091. // }}}
  3092. template <size_t... _Sizes, typename _Tp, typename _Ap,
  3093. typename = enable_if_t<((_Sizes + ...) == simd<_Tp, _Ap>::size())>>
  3094. inline tuple<simd<_Tp, simd_abi::deduce_t<_Tp, _Sizes>>...>
  3095. split(const simd<_Tp, _Ap>&);
  3096. // __extract_part {{{
  3097. template <int _Index, int _Total, int _Combine = 1, typename _Tp, size_t _Np>
  3098. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_CONST
  3099. _SimdWrapper<_Tp, _Np / _Total * _Combine>
  3100. __extract_part(const _SimdWrapper<_Tp, _Np> __x);
  3101. template <int Index, int Parts, int _Combine = 1, typename _Tp, typename _A0,
  3102. typename... _As>
  3103. _GLIBCXX_SIMD_INTRINSIC auto
  3104. __extract_part(const _SimdTuple<_Tp, _A0, _As...>& __x);
  3105. // }}}
  3106. // _SizeList {{{
  3107. template <size_t _V0, size_t... _Values>
  3108. struct _SizeList
  3109. {
  3110. template <size_t _I>
  3111. static constexpr size_t _S_at(_SizeConstant<_I> = {})
  3112. {
  3113. if constexpr (_I == 0)
  3114. return _V0;
  3115. else
  3116. return _SizeList<_Values...>::template _S_at<_I - 1>();
  3117. }
  3118. template <size_t _I>
  3119. static constexpr auto _S_before(_SizeConstant<_I> = {})
  3120. {
  3121. if constexpr (_I == 0)
  3122. return _SizeConstant<0>();
  3123. else
  3124. return _SizeConstant<
  3125. _V0 + _SizeList<_Values...>::template _S_before<_I - 1>()>();
  3126. }
  3127. template <size_t _Np>
  3128. static constexpr auto _S_pop_front(_SizeConstant<_Np> = {})
  3129. {
  3130. if constexpr (_Np == 0)
  3131. return _SizeList();
  3132. else
  3133. return _SizeList<_Values...>::template _S_pop_front<_Np - 1>();
  3134. }
  3135. };
  3136. // }}}
  3137. // __extract_center {{{
  3138. template <typename _Tp, size_t _Np>
  3139. _GLIBCXX_SIMD_INTRINSIC _SimdWrapper<_Tp, _Np / 2>
  3140. __extract_center(_SimdWrapper<_Tp, _Np> __x)
  3141. {
  3142. static_assert(_Np >= 4);
  3143. static_assert(_Np % 4 == 0); // x0 - x1 - x2 - x3 -> return {x1, x2}
  3144. #if _GLIBCXX_SIMD_X86INTRIN // {{{
  3145. if constexpr (__have_avx512f && sizeof(_Tp) * _Np == 64)
  3146. {
  3147. const auto __intrin = __to_intrin(__x);
  3148. if constexpr (is_integral_v<_Tp>)
  3149. return __vector_bitcast<_Tp>(_mm512_castsi512_si256(
  3150. _mm512_shuffle_i32x4(__intrin, __intrin,
  3151. 1 + 2 * 0x4 + 2 * 0x10 + 3 * 0x40)));
  3152. else if constexpr (sizeof(_Tp) == 4)
  3153. return __vector_bitcast<_Tp>(_mm512_castps512_ps256(
  3154. _mm512_shuffle_f32x4(__intrin, __intrin,
  3155. 1 + 2 * 0x4 + 2 * 0x10 + 3 * 0x40)));
  3156. else if constexpr (sizeof(_Tp) == 8)
  3157. return __vector_bitcast<_Tp>(_mm512_castpd512_pd256(
  3158. _mm512_shuffle_f64x2(__intrin, __intrin,
  3159. 1 + 2 * 0x4 + 2 * 0x10 + 3 * 0x40)));
  3160. else
  3161. __assert_unreachable<_Tp>();
  3162. }
  3163. else if constexpr (sizeof(_Tp) * _Np == 32 && is_floating_point_v<_Tp>)
  3164. return __vector_bitcast<_Tp>(
  3165. _mm_shuffle_pd(__lo128(__vector_bitcast<double>(__x)),
  3166. __hi128(__vector_bitcast<double>(__x)), 1));
  3167. else if constexpr (sizeof(__x) == 32 && sizeof(_Tp) * _Np <= 32)
  3168. return __vector_bitcast<_Tp>(
  3169. _mm_alignr_epi8(__hi128(__vector_bitcast<_LLong>(__x)),
  3170. __lo128(__vector_bitcast<_LLong>(__x)),
  3171. sizeof(_Tp) * _Np / 4));
  3172. else
  3173. #endif // _GLIBCXX_SIMD_X86INTRIN }}}
  3174. {
  3175. __vector_type_t<_Tp, _Np / 2> __r;
  3176. __builtin_memcpy(&__r,
  3177. reinterpret_cast<const char*>(&__x)
  3178. + sizeof(_Tp) * _Np / 4,
  3179. sizeof(_Tp) * _Np / 2);
  3180. return __r;
  3181. }
  3182. }
  3183. template <typename _Tp, typename _A0, typename... _As>
  3184. _GLIBCXX_SIMD_INTRINSIC
  3185. _SimdWrapper<_Tp, _SimdTuple<_Tp, _A0, _As...>::_S_size() / 2>
  3186. __extract_center(const _SimdTuple<_Tp, _A0, _As...>& __x)
  3187. {
  3188. if constexpr (sizeof...(_As) == 0)
  3189. return __extract_center(__x.first);
  3190. else
  3191. return __extract_part<1, 4, 2>(__x);
  3192. }
  3193. // }}}
  3194. // __split_wrapper {{{
  3195. template <size_t... _Sizes, typename _Tp, typename... _As>
  3196. auto
  3197. __split_wrapper(_SizeList<_Sizes...>, const _SimdTuple<_Tp, _As...>& __x)
  3198. {
  3199. return split<_Sizes...>(
  3200. fixed_size_simd<_Tp, _SimdTuple<_Tp, _As...>::_S_size()>(__private_init,
  3201. __x));
  3202. }
  3203. // }}}
  3204. // split<simd>(simd) {{{
  3205. template <typename _V, typename _Ap,
  3206. size_t Parts = simd_size_v<typename _V::value_type, _Ap> / _V::size()>
  3207. enable_if_t<simd_size_v<typename _V::value_type, _Ap> == Parts * _V::size()
  3208. && is_simd_v<_V>, array<_V, Parts>>
  3209. split(const simd<typename _V::value_type, _Ap>& __x)
  3210. {
  3211. using _Tp = typename _V::value_type;
  3212. if constexpr (Parts == 1)
  3213. {
  3214. return {simd_cast<_V>(__x)};
  3215. }
  3216. else if (__x._M_is_constprop())
  3217. {
  3218. return __generate_from_n_evaluations<Parts, array<_V, Parts>>([&](
  3219. auto __i) constexpr {
  3220. return _V([&](auto __j) constexpr {
  3221. return __x[__i * _V::size() + __j];
  3222. });
  3223. });
  3224. }
  3225. else if constexpr (
  3226. __is_fixed_size_abi_v<_Ap>
  3227. && (is_same_v<typename _V::abi_type, simd_abi::scalar>
  3228. || (__is_fixed_size_abi_v<typename _V::abi_type>
  3229. && sizeof(_V) == sizeof(_Tp) * _V::size() // _V doesn't have padding
  3230. )))
  3231. {
  3232. // fixed_size -> fixed_size (w/o padding) or scalar
  3233. #ifdef _GLIBCXX_SIMD_USE_ALIASING_LOADS
  3234. const __may_alias<_Tp>* const __element_ptr
  3235. = reinterpret_cast<const __may_alias<_Tp>*>(&__data(__x));
  3236. return __generate_from_n_evaluations<Parts, array<_V, Parts>>([&](
  3237. auto __i) constexpr {
  3238. return _V(__element_ptr + __i * _V::size(), vector_aligned);
  3239. });
  3240. #else
  3241. const auto& __xx = __data(__x);
  3242. return __generate_from_n_evaluations<Parts, array<_V, Parts>>([&](
  3243. auto __i) constexpr {
  3244. [[maybe_unused]] constexpr size_t __offset
  3245. = decltype(__i)::value * _V::size();
  3246. return _V([&](auto __j) constexpr {
  3247. constexpr _SizeConstant<__j + __offset> __k;
  3248. return __xx[__k];
  3249. });
  3250. });
  3251. #endif
  3252. }
  3253. else if constexpr (is_same_v<typename _V::abi_type, simd_abi::scalar>)
  3254. {
  3255. // normally memcpy should work here as well
  3256. return __generate_from_n_evaluations<Parts, array<_V, Parts>>([&](
  3257. auto __i) constexpr { return __x[__i]; });
  3258. }
  3259. else
  3260. {
  3261. return __generate_from_n_evaluations<Parts, array<_V, Parts>>([&](
  3262. auto __i) constexpr {
  3263. if constexpr (__is_fixed_size_abi_v<typename _V::abi_type>)
  3264. return _V([&](auto __j) constexpr {
  3265. return __x[__i * _V::size() + __j];
  3266. });
  3267. else
  3268. return _V(__private_init,
  3269. __extract_part<decltype(__i)::value, Parts>(__data(__x)));
  3270. });
  3271. }
  3272. }
  3273. // }}}
  3274. // split<simd_mask>(simd_mask) {{{
  3275. template <typename _V, typename _Ap,
  3276. size_t _Parts
  3277. = simd_size_v<typename _V::simd_type::value_type, _Ap> / _V::size()>
  3278. enable_if_t<is_simd_mask_v<_V> && simd_size_v<typename
  3279. _V::simd_type::value_type, _Ap> == _Parts * _V::size(), array<_V, _Parts>>
  3280. split(const simd_mask<typename _V::simd_type::value_type, _Ap>& __x)
  3281. {
  3282. if constexpr (is_same_v<_Ap, typename _V::abi_type>)
  3283. return {__x};
  3284. else if constexpr (_Parts == 1)
  3285. return {__proposed::static_simd_cast<_V>(__x)};
  3286. else if constexpr (_Parts == 2 && __is_sse_abi<typename _V::abi_type>()
  3287. && __is_avx_abi<_Ap>())
  3288. return {_V(__private_init, __lo128(__data(__x))),
  3289. _V(__private_init, __hi128(__data(__x)))};
  3290. else if constexpr (_V::size() <= __CHAR_BIT__ * sizeof(_ULLong))
  3291. {
  3292. const bitset __bits = __x.__to_bitset();
  3293. return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>([&](
  3294. auto __i) constexpr {
  3295. constexpr size_t __offset = __i * _V::size();
  3296. return _V(__bitset_init, (__bits >> __offset).to_ullong());
  3297. });
  3298. }
  3299. else
  3300. {
  3301. return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>([&](
  3302. auto __i) constexpr {
  3303. constexpr size_t __offset = __i * _V::size();
  3304. return _V(
  3305. __private_init, [&](auto __j) constexpr {
  3306. return __x[__j + __offset];
  3307. });
  3308. });
  3309. }
  3310. }
  3311. // }}}
  3312. // split<_Sizes...>(simd) {{{
  3313. template <size_t... _Sizes, typename _Tp, typename _Ap, typename>
  3314. _GLIBCXX_SIMD_ALWAYS_INLINE
  3315. tuple<simd<_Tp, simd_abi::deduce_t<_Tp, _Sizes>>...>
  3316. split(const simd<_Tp, _Ap>& __x)
  3317. {
  3318. using _SL = _SizeList<_Sizes...>;
  3319. using _Tuple = tuple<__deduced_simd<_Tp, _Sizes>...>;
  3320. constexpr size_t _Np = simd_size_v<_Tp, _Ap>;
  3321. constexpr size_t _N0 = _SL::template _S_at<0>();
  3322. using _V = __deduced_simd<_Tp, _N0>;
  3323. if (__x._M_is_constprop())
  3324. return __generate_from_n_evaluations<sizeof...(_Sizes), _Tuple>([&](
  3325. auto __i) constexpr {
  3326. using _Vi = __deduced_simd<_Tp, _SL::_S_at(__i)>;
  3327. constexpr size_t __offset = _SL::_S_before(__i);
  3328. return _Vi([&](auto __j) constexpr { return __x[__offset + __j]; });
  3329. });
  3330. else if constexpr (_Np == _N0)
  3331. {
  3332. static_assert(sizeof...(_Sizes) == 1);
  3333. return {simd_cast<_V>(__x)};
  3334. }
  3335. else if constexpr // split from fixed_size, such that __x::first.size == _N0
  3336. (__is_fixed_size_abi_v<
  3337. _Ap> && __fixed_size_storage_t<_Tp, _Np>::_S_first_size == _N0)
  3338. {
  3339. static_assert(
  3340. !__is_fixed_size_abi_v<typename _V::abi_type>,
  3341. "How can <_Tp, _Np> be __a single _SimdTuple entry but __a "
  3342. "fixed_size_simd "
  3343. "when deduced?");
  3344. // extract first and recurse (__split_wrapper is needed to deduce a new
  3345. // _Sizes pack)
  3346. return tuple_cat(make_tuple(_V(__private_init, __data(__x).first)),
  3347. __split_wrapper(_SL::template _S_pop_front<1>(),
  3348. __data(__x).second));
  3349. }
  3350. else if constexpr ((!is_same_v<simd_abi::scalar,
  3351. simd_abi::deduce_t<_Tp, _Sizes>> && ...)
  3352. && (!__is_fixed_size_abi_v<
  3353. simd_abi::deduce_t<_Tp, _Sizes>> && ...))
  3354. {
  3355. if constexpr (((_Sizes * 2 == _Np) && ...))
  3356. return {{__private_init, __extract_part<0, 2>(__data(__x))},
  3357. {__private_init, __extract_part<1, 2>(__data(__x))}};
  3358. else if constexpr (is_same_v<_SizeList<_Sizes...>,
  3359. _SizeList<_Np / 3, _Np / 3, _Np / 3>>)
  3360. return {{__private_init, __extract_part<0, 3>(__data(__x))},
  3361. {__private_init, __extract_part<1, 3>(__data(__x))},
  3362. {__private_init, __extract_part<2, 3>(__data(__x))}};
  3363. else if constexpr (is_same_v<_SizeList<_Sizes...>,
  3364. _SizeList<2 * _Np / 3, _Np / 3>>)
  3365. return {{__private_init, __extract_part<0, 3, 2>(__data(__x))},
  3366. {__private_init, __extract_part<2, 3>(__data(__x))}};
  3367. else if constexpr (is_same_v<_SizeList<_Sizes...>,
  3368. _SizeList<_Np / 3, 2 * _Np / 3>>)
  3369. return {{__private_init, __extract_part<0, 3>(__data(__x))},
  3370. {__private_init, __extract_part<1, 3, 2>(__data(__x))}};
  3371. else if constexpr (is_same_v<_SizeList<_Sizes...>,
  3372. _SizeList<_Np / 2, _Np / 4, _Np / 4>>)
  3373. return {{__private_init, __extract_part<0, 2>(__data(__x))},
  3374. {__private_init, __extract_part<2, 4>(__data(__x))},
  3375. {__private_init, __extract_part<3, 4>(__data(__x))}};
  3376. else if constexpr (is_same_v<_SizeList<_Sizes...>,
  3377. _SizeList<_Np / 4, _Np / 4, _Np / 2>>)
  3378. return {{__private_init, __extract_part<0, 4>(__data(__x))},
  3379. {__private_init, __extract_part<1, 4>(__data(__x))},
  3380. {__private_init, __extract_part<1, 2>(__data(__x))}};
  3381. else if constexpr (is_same_v<_SizeList<_Sizes...>,
  3382. _SizeList<_Np / 4, _Np / 2, _Np / 4>>)
  3383. return {{__private_init, __extract_part<0, 4>(__data(__x))},
  3384. {__private_init, __extract_center(__data(__x))},
  3385. {__private_init, __extract_part<3, 4>(__data(__x))}};
  3386. else if constexpr (((_Sizes * 4 == _Np) && ...))
  3387. return {{__private_init, __extract_part<0, 4>(__data(__x))},
  3388. {__private_init, __extract_part<1, 4>(__data(__x))},
  3389. {__private_init, __extract_part<2, 4>(__data(__x))},
  3390. {__private_init, __extract_part<3, 4>(__data(__x))}};
  3391. // else fall through
  3392. }
  3393. #ifdef _GLIBCXX_SIMD_USE_ALIASING_LOADS
  3394. const __may_alias<_Tp>* const __element_ptr
  3395. = reinterpret_cast<const __may_alias<_Tp>*>(&__x);
  3396. return __generate_from_n_evaluations<sizeof...(_Sizes), _Tuple>([&](
  3397. auto __i) constexpr {
  3398. using _Vi = __deduced_simd<_Tp, _SL::_S_at(__i)>;
  3399. constexpr size_t __offset = _SL::_S_before(__i);
  3400. constexpr size_t __base_align = alignof(simd<_Tp, _Ap>);
  3401. constexpr size_t __a
  3402. = __base_align - ((__offset * sizeof(_Tp)) % __base_align);
  3403. constexpr size_t __b = ((__a - 1) & __a) ^ __a;
  3404. constexpr size_t __alignment = __b == 0 ? __a : __b;
  3405. return _Vi(__element_ptr + __offset, overaligned<__alignment>);
  3406. });
  3407. #else
  3408. return __generate_from_n_evaluations<sizeof...(_Sizes), _Tuple>([&](
  3409. auto __i) constexpr {
  3410. using _Vi = __deduced_simd<_Tp, _SL::_S_at(__i)>;
  3411. const auto& __xx = __data(__x);
  3412. using _Offset = decltype(_SL::_S_before(__i));
  3413. return _Vi([&](auto __j) constexpr {
  3414. constexpr _SizeConstant<_Offset::value + __j> __k;
  3415. return __xx[__k];
  3416. });
  3417. });
  3418. #endif
  3419. }
  3420. // }}}
  3421. // __subscript_in_pack {{{
  3422. template <size_t _I, typename _Tp, typename _Ap, typename... _As>
  3423. _GLIBCXX_SIMD_INTRINSIC constexpr _Tp
  3424. __subscript_in_pack(const simd<_Tp, _Ap>& __x, const simd<_Tp, _As>&... __xs)
  3425. {
  3426. if constexpr (_I < simd_size_v<_Tp, _Ap>)
  3427. return __x[_I];
  3428. else
  3429. return __subscript_in_pack<_I - simd_size_v<_Tp, _Ap>>(__xs...);
  3430. }
  3431. // }}}
  3432. // __store_pack_of_simd {{{
  3433. template <typename _Tp, typename _A0, typename... _As>
  3434. _GLIBCXX_SIMD_INTRINSIC void
  3435. __store_pack_of_simd(char* __mem, const simd<_Tp, _A0>& __x0,
  3436. const simd<_Tp, _As>&... __xs)
  3437. {
  3438. constexpr size_t __n_bytes = sizeof(_Tp) * simd_size_v<_Tp, _A0>;
  3439. __builtin_memcpy(__mem, &__data(__x0), __n_bytes);
  3440. if constexpr (sizeof...(__xs) > 0)
  3441. __store_pack_of_simd(__mem + __n_bytes, __xs...);
  3442. }
  3443. // }}}
  3444. // concat(simd...) {{{
  3445. template <typename _Tp, typename... _As>
  3446. inline _GLIBCXX_SIMD_CONSTEXPR
  3447. simd<_Tp, simd_abi::deduce_t<_Tp, (simd_size_v<_Tp, _As> + ...)>>
  3448. concat(const simd<_Tp, _As>&... __xs)
  3449. {
  3450. using _Rp = __deduced_simd<_Tp, (simd_size_v<_Tp, _As> + ...)>;
  3451. if constexpr (sizeof...(__xs) == 1)
  3452. return simd_cast<_Rp>(__xs...);
  3453. else if ((... && __xs._M_is_constprop()))
  3454. return simd<_Tp,
  3455. simd_abi::deduce_t<_Tp, (simd_size_v<_Tp, _As> + ...)>>([&](
  3456. auto __i) constexpr { return __subscript_in_pack<__i>(__xs...); });
  3457. else
  3458. {
  3459. _Rp __r{};
  3460. __store_pack_of_simd(reinterpret_cast<char*>(&__data(__r)), __xs...);
  3461. return __r;
  3462. }
  3463. }
  3464. // }}}
  3465. // concat(array<simd>) {{{
  3466. template <typename _Tp, typename _Abi, size_t _Np>
  3467. _GLIBCXX_SIMD_ALWAYS_INLINE
  3468. _GLIBCXX_SIMD_CONSTEXPR __deduced_simd<_Tp, simd_size_v<_Tp, _Abi> * _Np>
  3469. concat(const array<simd<_Tp, _Abi>, _Np>& __x)
  3470. {
  3471. return __call_with_subscripts<_Np>(__x, [](const auto&... __xs) {
  3472. return concat(__xs...);
  3473. });
  3474. }
  3475. // }}}
  3476. // _SmartReference {{{
  3477. template <typename _Up, typename _Accessor = _Up,
  3478. typename _ValueType = typename _Up::value_type>
  3479. class _SmartReference
  3480. {
  3481. friend _Accessor;
  3482. int _M_index;
  3483. _Up& _M_obj;
  3484. _GLIBCXX_SIMD_INTRINSIC constexpr _ValueType _M_read() const noexcept
  3485. {
  3486. if constexpr (is_arithmetic_v<_Up>)
  3487. return _M_obj;
  3488. else
  3489. return _M_obj[_M_index];
  3490. }
  3491. template <typename _Tp>
  3492. _GLIBCXX_SIMD_INTRINSIC constexpr void _M_write(_Tp&& __x) const
  3493. { _Accessor::_S_set(_M_obj, _M_index, static_cast<_Tp&&>(__x)); }
  3494. public:
  3495. _GLIBCXX_SIMD_INTRINSIC constexpr
  3496. _SmartReference(_Up& __o, int __i) noexcept
  3497. : _M_index(__i), _M_obj(__o) {}
  3498. using value_type = _ValueType;
  3499. _GLIBCXX_SIMD_INTRINSIC _SmartReference(const _SmartReference&) = delete;
  3500. _GLIBCXX_SIMD_INTRINSIC constexpr operator value_type() const noexcept
  3501. { return _M_read(); }
  3502. template <typename _Tp,
  3503. typename
  3504. = _ValuePreservingOrInt<__remove_cvref_t<_Tp>, value_type>>
  3505. _GLIBCXX_SIMD_INTRINSIC constexpr _SmartReference operator=(_Tp&& __x) &&
  3506. {
  3507. _M_write(static_cast<_Tp&&>(__x));
  3508. return {_M_obj, _M_index};
  3509. }
  3510. #define _GLIBCXX_SIMD_OP_(__op) \
  3511. template <typename _Tp, \
  3512. typename _TT \
  3513. = decltype(declval<value_type>() __op declval<_Tp>()), \
  3514. typename = _ValuePreservingOrInt<__remove_cvref_t<_Tp>, _TT>, \
  3515. typename = _ValuePreservingOrInt<_TT, value_type>> \
  3516. _GLIBCXX_SIMD_INTRINSIC constexpr _SmartReference \
  3517. operator __op##=(_Tp&& __x) && \
  3518. { \
  3519. const value_type& __lhs = _M_read(); \
  3520. _M_write(__lhs __op __x); \
  3521. return {_M_obj, _M_index}; \
  3522. }
  3523. _GLIBCXX_SIMD_ALL_ARITHMETICS(_GLIBCXX_SIMD_OP_);
  3524. _GLIBCXX_SIMD_ALL_SHIFTS(_GLIBCXX_SIMD_OP_);
  3525. _GLIBCXX_SIMD_ALL_BINARY(_GLIBCXX_SIMD_OP_);
  3526. #undef _GLIBCXX_SIMD_OP_
  3527. template <typename _Tp = void,
  3528. typename
  3529. = decltype(++declval<conditional_t<true, value_type, _Tp>&>())>
  3530. _GLIBCXX_SIMD_INTRINSIC constexpr _SmartReference operator++() &&
  3531. {
  3532. value_type __x = _M_read();
  3533. _M_write(++__x);
  3534. return {_M_obj, _M_index};
  3535. }
  3536. template <typename _Tp = void,
  3537. typename
  3538. = decltype(declval<conditional_t<true, value_type, _Tp>&>()++)>
  3539. _GLIBCXX_SIMD_INTRINSIC constexpr value_type operator++(int) &&
  3540. {
  3541. const value_type __r = _M_read();
  3542. value_type __x = __r;
  3543. _M_write(++__x);
  3544. return __r;
  3545. }
  3546. template <typename _Tp = void,
  3547. typename
  3548. = decltype(--declval<conditional_t<true, value_type, _Tp>&>())>
  3549. _GLIBCXX_SIMD_INTRINSIC constexpr _SmartReference operator--() &&
  3550. {
  3551. value_type __x = _M_read();
  3552. _M_write(--__x);
  3553. return {_M_obj, _M_index};
  3554. }
  3555. template <typename _Tp = void,
  3556. typename
  3557. = decltype(declval<conditional_t<true, value_type, _Tp>&>()--)>
  3558. _GLIBCXX_SIMD_INTRINSIC constexpr value_type operator--(int) &&
  3559. {
  3560. const value_type __r = _M_read();
  3561. value_type __x = __r;
  3562. _M_write(--__x);
  3563. return __r;
  3564. }
  3565. _GLIBCXX_SIMD_INTRINSIC friend void
  3566. swap(_SmartReference&& __a, _SmartReference&& __b) noexcept(
  3567. conjunction<
  3568. is_nothrow_constructible<value_type, _SmartReference&&>,
  3569. is_nothrow_assignable<_SmartReference&&, value_type&&>>::value)
  3570. {
  3571. value_type __tmp = static_cast<_SmartReference&&>(__a);
  3572. static_cast<_SmartReference&&>(__a) = static_cast<value_type>(__b);
  3573. static_cast<_SmartReference&&>(__b) = std::move(__tmp);
  3574. }
  3575. _GLIBCXX_SIMD_INTRINSIC friend void
  3576. swap(value_type& __a, _SmartReference&& __b) noexcept(
  3577. conjunction<
  3578. is_nothrow_constructible<value_type, value_type&&>,
  3579. is_nothrow_assignable<value_type&, value_type&&>,
  3580. is_nothrow_assignable<_SmartReference&&, value_type&&>>::value)
  3581. {
  3582. value_type __tmp(std::move(__a));
  3583. __a = static_cast<value_type>(__b);
  3584. static_cast<_SmartReference&&>(__b) = std::move(__tmp);
  3585. }
  3586. _GLIBCXX_SIMD_INTRINSIC friend void
  3587. swap(_SmartReference&& __a, value_type& __b) noexcept(
  3588. conjunction<
  3589. is_nothrow_constructible<value_type, _SmartReference&&>,
  3590. is_nothrow_assignable<value_type&, value_type&&>,
  3591. is_nothrow_assignable<_SmartReference&&, value_type&&>>::value)
  3592. {
  3593. value_type __tmp(__a);
  3594. static_cast<_SmartReference&&>(__a) = std::move(__b);
  3595. __b = std::move(__tmp);
  3596. }
  3597. };
  3598. // }}}
  3599. // __scalar_abi_wrapper {{{
  3600. template <int _Bytes>
  3601. struct __scalar_abi_wrapper
  3602. {
  3603. template <typename _Tp> static constexpr size_t _S_full_size = 1;
  3604. template <typename _Tp> static constexpr size_t _S_size = 1;
  3605. template <typename _Tp> static constexpr size_t _S_is_partial = false;
  3606. template <typename _Tp, typename _Abi = simd_abi::scalar>
  3607. static constexpr bool _S_is_valid_v
  3608. = _Abi::template _IsValid<_Tp>::value && sizeof(_Tp) == _Bytes;
  3609. };
  3610. // }}}
  3611. // __decay_abi metafunction {{{
  3612. template <typename _Tp>
  3613. struct __decay_abi { using type = _Tp; };
  3614. template <int _Bytes>
  3615. struct __decay_abi<__scalar_abi_wrapper<_Bytes>>
  3616. { using type = simd_abi::scalar; };
  3617. // }}}
  3618. // __find_next_valid_abi metafunction {{{1
  3619. // Given an ABI tag A<N>, find an N2 < N such that A<N2>::_S_is_valid_v<_Tp> ==
  3620. // true, N2 is a power-of-2, and A<N2>::_S_is_partial<_Tp> is false. Break
  3621. // recursion at 2 elements in the resulting ABI tag. In this case
  3622. // type::_S_is_valid_v<_Tp> may be false.
  3623. template <template <int> class _Abi, int _Bytes, typename _Tp>
  3624. struct __find_next_valid_abi
  3625. {
  3626. static constexpr auto _S_choose()
  3627. {
  3628. constexpr int _NextBytes = std::__bit_ceil(_Bytes) / 2;
  3629. using _NextAbi = _Abi<_NextBytes>;
  3630. if constexpr (_NextBytes < sizeof(_Tp) * 2) // break recursion
  3631. return _Abi<_Bytes>();
  3632. else if constexpr (_NextAbi::template _S_is_partial<_Tp> == false
  3633. && _NextAbi::template _S_is_valid_v<_Tp>)
  3634. return _NextAbi();
  3635. else
  3636. return __find_next_valid_abi<_Abi, _NextBytes, _Tp>::_S_choose();
  3637. }
  3638. using type = decltype(_S_choose());
  3639. };
  3640. template <int _Bytes, typename _Tp>
  3641. struct __find_next_valid_abi<__scalar_abi_wrapper, _Bytes, _Tp>
  3642. { using type = simd_abi::scalar; };
  3643. // _AbiList {{{1
  3644. template <template <int> class...>
  3645. struct _AbiList
  3646. {
  3647. template <typename, int> static constexpr bool _S_has_valid_abi = false;
  3648. template <typename, int> using _FirstValidAbi = void;
  3649. template <typename, int> using _BestAbi = void;
  3650. };
  3651. template <template <int> class _A0, template <int> class... _Rest>
  3652. struct _AbiList<_A0, _Rest...>
  3653. {
  3654. template <typename _Tp, int _Np>
  3655. static constexpr bool _S_has_valid_abi
  3656. = _A0<sizeof(_Tp) * _Np>::template _S_is_valid_v<
  3657. _Tp> || _AbiList<_Rest...>::template _S_has_valid_abi<_Tp, _Np>;
  3658. template <typename _Tp, int _Np>
  3659. using _FirstValidAbi = conditional_t<
  3660. _A0<sizeof(_Tp) * _Np>::template _S_is_valid_v<_Tp>,
  3661. typename __decay_abi<_A0<sizeof(_Tp) * _Np>>::type,
  3662. typename _AbiList<_Rest...>::template _FirstValidAbi<_Tp, _Np>>;
  3663. template <typename _Tp, int _Np>
  3664. static constexpr auto _S_determine_best_abi()
  3665. {
  3666. static_assert(_Np >= 1);
  3667. constexpr int _Bytes = sizeof(_Tp) * _Np;
  3668. if constexpr (_Np == 1)
  3669. return __make_dependent_t<_Tp, simd_abi::scalar>{};
  3670. else
  3671. {
  3672. constexpr int __fullsize = _A0<_Bytes>::template _S_full_size<_Tp>;
  3673. // _A0<_Bytes> is good if:
  3674. // 1. The ABI tag is valid for _Tp
  3675. // 2. The storage overhead is no more than padding to fill the next
  3676. // power-of-2 number of bytes
  3677. if constexpr (_A0<_Bytes>::template _S_is_valid_v<
  3678. _Tp> && __fullsize / 2 < _Np)
  3679. return typename __decay_abi<_A0<_Bytes>>::type{};
  3680. else
  3681. {
  3682. using _Bp =
  3683. typename __find_next_valid_abi<_A0, _Bytes, _Tp>::type;
  3684. if constexpr (_Bp::template _S_is_valid_v<
  3685. _Tp> && _Bp::template _S_size<_Tp> <= _Np)
  3686. return _Bp{};
  3687. else
  3688. return
  3689. typename _AbiList<_Rest...>::template _BestAbi<_Tp, _Np>{};
  3690. }
  3691. }
  3692. }
  3693. template <typename _Tp, int _Np>
  3694. using _BestAbi = decltype(_S_determine_best_abi<_Tp, _Np>());
  3695. };
  3696. // }}}1
  3697. // the following lists all native ABIs, which makes them accessible to
  3698. // simd_abi::deduce and select_best_vector_type_t (for fixed_size). Order
  3699. // matters: Whatever comes first has higher priority.
  3700. using _AllNativeAbis = _AbiList<simd_abi::_VecBltnBtmsk, simd_abi::_VecBuiltin,
  3701. __scalar_abi_wrapper>;
  3702. // valid _SimdTraits specialization {{{1
  3703. template <typename _Tp, typename _Abi>
  3704. struct _SimdTraits<_Tp, _Abi, void_t<typename _Abi::template _IsValid<_Tp>>>
  3705. : _Abi::template __traits<_Tp> {};
  3706. // __deduce_impl specializations {{{1
  3707. // try all native ABIs (including scalar) first
  3708. template <typename _Tp, size_t _Np>
  3709. struct __deduce_impl<
  3710. _Tp, _Np, enable_if_t<_AllNativeAbis::template _S_has_valid_abi<_Tp, _Np>>>
  3711. { using type = _AllNativeAbis::_FirstValidAbi<_Tp, _Np>; };
  3712. // fall back to fixed_size only if scalar and native ABIs don't match
  3713. template <typename _Tp, size_t _Np, typename = void>
  3714. struct __deduce_fixed_size_fallback {};
  3715. template <typename _Tp, size_t _Np>
  3716. struct __deduce_fixed_size_fallback<_Tp, _Np,
  3717. enable_if_t<simd_abi::fixed_size<_Np>::template _S_is_valid_v<_Tp>>>
  3718. { using type = simd_abi::fixed_size<_Np>; };
  3719. template <typename _Tp, size_t _Np, typename>
  3720. struct __deduce_impl : public __deduce_fixed_size_fallback<_Tp, _Np> {};
  3721. //}}}1
  3722. // simd_mask {{{
  3723. template <typename _Tp, typename _Abi>
  3724. class simd_mask : public _SimdTraits<_Tp, _Abi>::_MaskBase
  3725. {
  3726. // types, tags, and friends {{{
  3727. using _Traits = _SimdTraits<_Tp, _Abi>;
  3728. using _MemberType = typename _Traits::_MaskMember;
  3729. // We map all masks with equal element sizeof to a single integer type, the
  3730. // one given by __int_for_sizeof_t<_Tp>. This is the approach
  3731. // [[gnu::vector_size(N)]] types take as well and it reduces the number of
  3732. // template specializations in the implementation classes.
  3733. using _Ip = __int_for_sizeof_t<_Tp>;
  3734. static constexpr _Ip* _S_type_tag = nullptr;
  3735. friend typename _Traits::_MaskBase;
  3736. friend class simd<_Tp, _Abi>; // to construct masks on return
  3737. friend typename _Traits::_SimdImpl; // to construct masks on return and
  3738. // inspect data on masked operations
  3739. public:
  3740. using _Impl = typename _Traits::_MaskImpl;
  3741. friend _Impl;
  3742. // }}}
  3743. // member types {{{
  3744. using value_type = bool;
  3745. using reference = _SmartReference<_MemberType, _Impl, value_type>;
  3746. using simd_type = simd<_Tp, _Abi>;
  3747. using abi_type = _Abi;
  3748. // }}}
  3749. static constexpr size_t size() // {{{
  3750. { return __size_or_zero_v<_Tp, _Abi>; }
  3751. // }}}
  3752. // constructors & assignment {{{
  3753. simd_mask() = default;
  3754. simd_mask(const simd_mask&) = default;
  3755. simd_mask(simd_mask&&) = default;
  3756. simd_mask& operator=(const simd_mask&) = default;
  3757. simd_mask& operator=(simd_mask&&) = default;
  3758. // }}}
  3759. // access to internal representation (optional feature) {{{
  3760. _GLIBCXX_SIMD_ALWAYS_INLINE explicit
  3761. simd_mask(typename _Traits::_MaskCastType __init)
  3762. : _M_data{__init} {}
  3763. // conversions to internal type is done in _MaskBase
  3764. // }}}
  3765. // bitset interface (extension to be proposed) {{{
  3766. // TS_FEEDBACK:
  3767. // Conversion of simd_mask to and from bitset makes it much easier to
  3768. // interface with other facilities. I suggest adding `static
  3769. // simd_mask::from_bitset` and `simd_mask::to_bitset`.
  3770. _GLIBCXX_SIMD_ALWAYS_INLINE static simd_mask
  3771. __from_bitset(bitset<size()> bs)
  3772. { return {__bitset_init, bs}; }
  3773. _GLIBCXX_SIMD_ALWAYS_INLINE bitset<size()>
  3774. __to_bitset() const
  3775. { return _Impl::_S_to_bits(_M_data)._M_to_bitset(); }
  3776. // }}}
  3777. // explicit broadcast constructor {{{
  3778. _GLIBCXX_SIMD_ALWAYS_INLINE explicit _GLIBCXX_SIMD_CONSTEXPR
  3779. simd_mask(value_type __x)
  3780. : _M_data(_Impl::template _S_broadcast<_Ip>(__x)) {}
  3781. // }}}
  3782. // implicit type conversion constructor {{{
  3783. #ifdef _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
  3784. // proposed improvement
  3785. template <typename _Up, typename _A2,
  3786. typename = enable_if_t<simd_size_v<_Up, _A2> == size()>>
  3787. _GLIBCXX_SIMD_ALWAYS_INLINE explicit(sizeof(_MemberType)
  3788. != sizeof(typename _SimdTraits<_Up, _A2>::_MaskMember))
  3789. simd_mask(const simd_mask<_Up, _A2>& __x)
  3790. : simd_mask(__proposed::static_simd_cast<simd_mask>(__x)) {}
  3791. #else
  3792. // conforming to ISO/IEC 19570:2018
  3793. template <typename _Up, typename = enable_if_t<conjunction<
  3794. is_same<abi_type, simd_abi::fixed_size<size()>>,
  3795. is_same<_Up, _Up>>::value>>
  3796. _GLIBCXX_SIMD_ALWAYS_INLINE
  3797. simd_mask(const simd_mask<_Up, simd_abi::fixed_size<size()>>& __x)
  3798. : _M_data(_Impl::_S_from_bitmask(__data(__x), _S_type_tag)) {}
  3799. #endif
  3800. // }}}
  3801. // load constructor {{{
  3802. template <typename _Flags>
  3803. _GLIBCXX_SIMD_ALWAYS_INLINE
  3804. simd_mask(const value_type* __mem, _Flags)
  3805. : _M_data(_Impl::template _S_load<_Ip>(
  3806. _Flags::template _S_apply<simd_mask>(__mem))) {}
  3807. template <typename _Flags>
  3808. _GLIBCXX_SIMD_ALWAYS_INLINE
  3809. simd_mask(const value_type* __mem, simd_mask __k, _Flags)
  3810. : _M_data{}
  3811. {
  3812. _M_data
  3813. = _Impl::_S_masked_load(_M_data, __k._M_data,
  3814. _Flags::template _S_apply<simd_mask>(__mem));
  3815. }
  3816. // }}}
  3817. // loads [simd_mask.load] {{{
  3818. template <typename _Flags>
  3819. _GLIBCXX_SIMD_ALWAYS_INLINE void
  3820. copy_from(const value_type* __mem, _Flags)
  3821. {
  3822. _M_data = _Impl::template _S_load<_Ip>(
  3823. _Flags::template _S_apply<simd_mask>(__mem));
  3824. }
  3825. // }}}
  3826. // stores [simd_mask.store] {{{
  3827. template <typename _Flags>
  3828. _GLIBCXX_SIMD_ALWAYS_INLINE void
  3829. copy_to(value_type* __mem, _Flags) const
  3830. { _Impl::_S_store(_M_data, _Flags::template _S_apply<simd_mask>(__mem)); }
  3831. // }}}
  3832. // scalar access {{{
  3833. _GLIBCXX_SIMD_ALWAYS_INLINE reference
  3834. operator[](size_t __i)
  3835. {
  3836. if (__i >= size())
  3837. __invoke_ub("Subscript %d is out of range [0, %d]", __i, size() - 1);
  3838. return {_M_data, int(__i)};
  3839. }
  3840. _GLIBCXX_SIMD_ALWAYS_INLINE value_type
  3841. operator[](size_t __i) const
  3842. {
  3843. if (__i >= size())
  3844. __invoke_ub("Subscript %d is out of range [0, %d]", __i, size() - 1);
  3845. if constexpr (__is_scalar_abi<_Abi>())
  3846. return _M_data;
  3847. else
  3848. return static_cast<bool>(_M_data[__i]);
  3849. }
  3850. // }}}
  3851. // negation {{{
  3852. _GLIBCXX_SIMD_ALWAYS_INLINE simd_mask
  3853. operator!() const
  3854. { return {__private_init, _Impl::_S_bit_not(_M_data)}; }
  3855. // }}}
  3856. // simd_mask binary operators [simd_mask.binary] {{{
  3857. #ifdef _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
  3858. // simd_mask<int> && simd_mask<uint> needs disambiguation
  3859. template <typename _Up, typename _A2,
  3860. typename
  3861. = enable_if_t<is_convertible_v<simd_mask<_Up, _A2>, simd_mask>>>
  3862. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
  3863. operator&&(const simd_mask& __x, const simd_mask<_Up, _A2>& __y)
  3864. {
  3865. return {__private_init,
  3866. _Impl::_S_logical_and(__x._M_data, simd_mask(__y)._M_data)};
  3867. }
  3868. template <typename _Up, typename _A2,
  3869. typename
  3870. = enable_if_t<is_convertible_v<simd_mask<_Up, _A2>, simd_mask>>>
  3871. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
  3872. operator||(const simd_mask& __x, const simd_mask<_Up, _A2>& __y)
  3873. {
  3874. return {__private_init,
  3875. _Impl::_S_logical_or(__x._M_data, simd_mask(__y)._M_data)};
  3876. }
  3877. #endif // _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
  3878. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
  3879. operator&&(const simd_mask& __x, const simd_mask& __y)
  3880. {
  3881. return {__private_init, _Impl::_S_logical_and(__x._M_data, __y._M_data)};
  3882. }
  3883. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
  3884. operator||(const simd_mask& __x, const simd_mask& __y)
  3885. {
  3886. return {__private_init, _Impl::_S_logical_or(__x._M_data, __y._M_data)};
  3887. }
  3888. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
  3889. operator&(const simd_mask& __x, const simd_mask& __y)
  3890. { return {__private_init, _Impl::_S_bit_and(__x._M_data, __y._M_data)}; }
  3891. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
  3892. operator|(const simd_mask& __x, const simd_mask& __y)
  3893. { return {__private_init, _Impl::_S_bit_or(__x._M_data, __y._M_data)}; }
  3894. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
  3895. operator^(const simd_mask& __x, const simd_mask& __y)
  3896. { return {__private_init, _Impl::_S_bit_xor(__x._M_data, __y._M_data)}; }
  3897. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask&
  3898. operator&=(simd_mask& __x, const simd_mask& __y)
  3899. {
  3900. __x._M_data = _Impl::_S_bit_and(__x._M_data, __y._M_data);
  3901. return __x;
  3902. }
  3903. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask&
  3904. operator|=(simd_mask& __x, const simd_mask& __y)
  3905. {
  3906. __x._M_data = _Impl::_S_bit_or(__x._M_data, __y._M_data);
  3907. return __x;
  3908. }
  3909. _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask&
  3910. operator^=(simd_mask& __x, const simd_mask& __y)
  3911. {
  3912. __x._M_data = _Impl::_S_bit_xor(__x._M_data, __y._M_data);
  3913. return __x;
  3914. }
  3915. // }}}
  3916. // simd_mask compares [simd_mask.comparison] {{{
  3917. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd_mask
  3918. operator==(const simd_mask& __x, const simd_mask& __y)
  3919. { return !operator!=(__x, __y); }
  3920. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd_mask
  3921. operator!=(const simd_mask& __x, const simd_mask& __y)
  3922. { return {__private_init, _Impl::_S_bit_xor(__x._M_data, __y._M_data)}; }
  3923. // }}}
  3924. // private_init ctor {{{
  3925. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
  3926. simd_mask(_PrivateInit, typename _Traits::_MaskMember __init)
  3927. : _M_data(__init) {}
  3928. // }}}
  3929. // private_init generator ctor {{{
  3930. template <typename _Fp, typename = decltype(bool(declval<_Fp>()(size_t())))>
  3931. _GLIBCXX_SIMD_INTRINSIC constexpr
  3932. simd_mask(_PrivateInit, _Fp&& __gen)
  3933. : _M_data()
  3934. {
  3935. __execute_n_times<size()>([&](auto __i) constexpr {
  3936. _Impl::_S_set(_M_data, __i, __gen(__i));
  3937. });
  3938. }
  3939. // }}}
  3940. // bitset_init ctor {{{
  3941. _GLIBCXX_SIMD_INTRINSIC simd_mask(_BitsetInit, bitset<size()> __init)
  3942. : _M_data(
  3943. _Impl::_S_from_bitmask(_SanitizedBitMask<size()>(__init), _S_type_tag))
  3944. {}
  3945. // }}}
  3946. // __cvt {{{
  3947. // TS_FEEDBACK:
  3948. // The conversion operator this implements should be a ctor on simd_mask.
  3949. // Once you call .__cvt() on a simd_mask it converts conveniently.
  3950. // A useful variation: add `explicit(sizeof(_Tp) != sizeof(_Up))`
  3951. struct _CvtProxy
  3952. {
  3953. template <typename _Up, typename _A2,
  3954. typename
  3955. = enable_if_t<simd_size_v<_Up, _A2> == simd_size_v<_Tp, _Abi>>>
  3956. operator simd_mask<_Up, _A2>() &&
  3957. {
  3958. using namespace std::experimental::__proposed;
  3959. return static_simd_cast<simd_mask<_Up, _A2>>(_M_data);
  3960. }
  3961. const simd_mask<_Tp, _Abi>& _M_data;
  3962. };
  3963. _GLIBCXX_SIMD_INTRINSIC _CvtProxy
  3964. __cvt() const
  3965. { return {*this}; }
  3966. // }}}
  3967. // operator?: overloads (suggested extension) {{{
  3968. #ifdef __GXX_CONDITIONAL_IS_OVERLOADABLE__
  3969. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd_mask
  3970. operator?:(const simd_mask& __k, const simd_mask& __where_true,
  3971. const simd_mask& __where_false)
  3972. {
  3973. auto __ret = __where_false;
  3974. _Impl::_S_masked_assign(__k._M_data, __ret._M_data, __where_true._M_data);
  3975. return __ret;
  3976. }
  3977. template <typename _U1, typename _U2,
  3978. typename _Rp = simd<common_type_t<_U1, _U2>, _Abi>,
  3979. typename = enable_if_t<conjunction_v<
  3980. is_convertible<_U1, _Rp>, is_convertible<_U2, _Rp>,
  3981. is_convertible<simd_mask, typename _Rp::mask_type>>>>
  3982. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend _Rp
  3983. operator?:(const simd_mask& __k, const _U1& __where_true,
  3984. const _U2& __where_false)
  3985. {
  3986. _Rp __ret = __where_false;
  3987. _Rp::_Impl::_S_masked_assign(
  3988. __data(static_cast<typename _Rp::mask_type>(__k)), __data(__ret),
  3989. __data(static_cast<_Rp>(__where_true)));
  3990. return __ret;
  3991. }
  3992. #ifdef _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
  3993. template <typename _Kp, typename _Ak, typename _Up, typename _Au,
  3994. typename = enable_if_t<
  3995. conjunction_v<is_convertible<simd_mask<_Kp, _Ak>, simd_mask>,
  3996. is_convertible<simd_mask<_Up, _Au>, simd_mask>>>>
  3997. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd_mask
  3998. operator?:(const simd_mask<_Kp, _Ak>& __k, const simd_mask& __where_true,
  3999. const simd_mask<_Up, _Au>& __where_false)
  4000. {
  4001. simd_mask __ret = __where_false;
  4002. _Impl::_S_masked_assign(simd_mask(__k)._M_data, __ret._M_data,
  4003. __where_true._M_data);
  4004. return __ret;
  4005. }
  4006. #endif // _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
  4007. #endif // __GXX_CONDITIONAL_IS_OVERLOADABLE__
  4008. // }}}
  4009. // _M_is_constprop {{{
  4010. _GLIBCXX_SIMD_INTRINSIC constexpr bool
  4011. _M_is_constprop() const
  4012. {
  4013. if constexpr (__is_scalar_abi<_Abi>())
  4014. return __builtin_constant_p(_M_data);
  4015. else
  4016. return _M_data._M_is_constprop();
  4017. }
  4018. // }}}
  4019. private:
  4020. friend const auto& __data<_Tp, abi_type>(const simd_mask&);
  4021. friend auto& __data<_Tp, abi_type>(simd_mask&);
  4022. alignas(_Traits::_S_mask_align) _MemberType _M_data;
  4023. };
  4024. // }}}
  4025. // __data(simd_mask) {{{
  4026. template <typename _Tp, typename _Ap>
  4027. _GLIBCXX_SIMD_INTRINSIC constexpr const auto&
  4028. __data(const simd_mask<_Tp, _Ap>& __x)
  4029. { return __x._M_data; }
  4030. template <typename _Tp, typename _Ap>
  4031. _GLIBCXX_SIMD_INTRINSIC constexpr auto&
  4032. __data(simd_mask<_Tp, _Ap>& __x)
  4033. { return __x._M_data; }
  4034. // }}}
  4035. // simd_mask reductions [simd_mask.reductions] {{{
  4036. template <typename _Tp, typename _Abi>
  4037. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
  4038. all_of(const simd_mask<_Tp, _Abi>& __k) noexcept
  4039. {
  4040. if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
  4041. {
  4042. for (size_t __i = 0; __i < simd_size_v<_Tp, _Abi>; ++__i)
  4043. if (!__k[__i])
  4044. return false;
  4045. return true;
  4046. }
  4047. else
  4048. return _Abi::_MaskImpl::_S_all_of(__k);
  4049. }
  4050. template <typename _Tp, typename _Abi>
  4051. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
  4052. any_of(const simd_mask<_Tp, _Abi>& __k) noexcept
  4053. {
  4054. if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
  4055. {
  4056. for (size_t __i = 0; __i < simd_size_v<_Tp, _Abi>; ++__i)
  4057. if (__k[__i])
  4058. return true;
  4059. return false;
  4060. }
  4061. else
  4062. return _Abi::_MaskImpl::_S_any_of(__k);
  4063. }
  4064. template <typename _Tp, typename _Abi>
  4065. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
  4066. none_of(const simd_mask<_Tp, _Abi>& __k) noexcept
  4067. {
  4068. if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
  4069. {
  4070. for (size_t __i = 0; __i < simd_size_v<_Tp, _Abi>; ++__i)
  4071. if (__k[__i])
  4072. return false;
  4073. return true;
  4074. }
  4075. else
  4076. return _Abi::_MaskImpl::_S_none_of(__k);
  4077. }
  4078. template <typename _Tp, typename _Abi>
  4079. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
  4080. some_of(const simd_mask<_Tp, _Abi>& __k) noexcept
  4081. {
  4082. if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
  4083. {
  4084. for (size_t __i = 1; __i < simd_size_v<_Tp, _Abi>; ++__i)
  4085. if (__k[__i] != __k[__i - 1])
  4086. return true;
  4087. return false;
  4088. }
  4089. else
  4090. return _Abi::_MaskImpl::_S_some_of(__k);
  4091. }
  4092. template <typename _Tp, typename _Abi>
  4093. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
  4094. popcount(const simd_mask<_Tp, _Abi>& __k) noexcept
  4095. {
  4096. if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
  4097. {
  4098. const int __r = __call_with_subscripts<simd_size_v<_Tp, _Abi>>(
  4099. __k, [](auto... __elements) { return ((__elements != 0) + ...); });
  4100. if (__builtin_is_constant_evaluated() || __builtin_constant_p(__r))
  4101. return __r;
  4102. }
  4103. return _Abi::_MaskImpl::_S_popcount(__k);
  4104. }
  4105. template <typename _Tp, typename _Abi>
  4106. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
  4107. find_first_set(const simd_mask<_Tp, _Abi>& __k)
  4108. {
  4109. if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
  4110. {
  4111. constexpr size_t _Np = simd_size_v<_Tp, _Abi>;
  4112. const size_t _Idx = __call_with_n_evaluations<_Np>(
  4113. [](auto... __indexes) { return std::min({__indexes...}); },
  4114. [&](auto __i) { return __k[__i] ? +__i : _Np; });
  4115. if (_Idx >= _Np)
  4116. __invoke_ub("find_first_set(empty mask) is UB");
  4117. if (__builtin_constant_p(_Idx))
  4118. return _Idx;
  4119. }
  4120. return _Abi::_MaskImpl::_S_find_first_set(__k);
  4121. }
  4122. template <typename _Tp, typename _Abi>
  4123. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
  4124. find_last_set(const simd_mask<_Tp, _Abi>& __k)
  4125. {
  4126. if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
  4127. {
  4128. constexpr size_t _Np = simd_size_v<_Tp, _Abi>;
  4129. const int _Idx = __call_with_n_evaluations<_Np>(
  4130. [](auto... __indexes) { return std::max({__indexes...}); },
  4131. [&](auto __i) { return __k[__i] ? int(__i) : -1; });
  4132. if (_Idx < 0)
  4133. __invoke_ub("find_first_set(empty mask) is UB");
  4134. if (__builtin_constant_p(_Idx))
  4135. return _Idx;
  4136. }
  4137. return _Abi::_MaskImpl::_S_find_last_set(__k);
  4138. }
  4139. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
  4140. all_of(_ExactBool __x) noexcept
  4141. { return __x; }
  4142. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
  4143. any_of(_ExactBool __x) noexcept
  4144. { return __x; }
  4145. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
  4146. none_of(_ExactBool __x) noexcept
  4147. { return !__x; }
  4148. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
  4149. some_of(_ExactBool) noexcept
  4150. { return false; }
  4151. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
  4152. popcount(_ExactBool __x) noexcept
  4153. { return __x; }
  4154. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
  4155. find_first_set(_ExactBool)
  4156. { return 0; }
  4157. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
  4158. find_last_set(_ExactBool)
  4159. { return 0; }
  4160. // }}}
  4161. // _SimdIntOperators{{{1
  4162. template <typename _V, typename _Impl, bool>
  4163. class _SimdIntOperators {};
  4164. template <typename _V, typename _Impl>
  4165. class _SimdIntOperators<_V, _Impl, true>
  4166. {
  4167. _GLIBCXX_SIMD_INTRINSIC const _V& __derived() const
  4168. { return *static_cast<const _V*>(this); }
  4169. template <typename _Tp>
  4170. _GLIBCXX_SIMD_INTRINSIC static _GLIBCXX_SIMD_CONSTEXPR _V
  4171. _S_make_derived(_Tp&& __d)
  4172. { return {__private_init, static_cast<_Tp&&>(__d)}; }
  4173. public:
  4174. _GLIBCXX_SIMD_CONSTEXPR friend _V& operator%=(_V& __lhs, const _V& __x)
  4175. { return __lhs = __lhs % __x; }
  4176. _GLIBCXX_SIMD_CONSTEXPR friend _V& operator&=(_V& __lhs, const _V& __x)
  4177. { return __lhs = __lhs & __x; }
  4178. _GLIBCXX_SIMD_CONSTEXPR friend _V& operator|=(_V& __lhs, const _V& __x)
  4179. { return __lhs = __lhs | __x; }
  4180. _GLIBCXX_SIMD_CONSTEXPR friend _V& operator^=(_V& __lhs, const _V& __x)
  4181. { return __lhs = __lhs ^ __x; }
  4182. _GLIBCXX_SIMD_CONSTEXPR friend _V& operator<<=(_V& __lhs, const _V& __x)
  4183. { return __lhs = __lhs << __x; }
  4184. _GLIBCXX_SIMD_CONSTEXPR friend _V& operator>>=(_V& __lhs, const _V& __x)
  4185. { return __lhs = __lhs >> __x; }
  4186. _GLIBCXX_SIMD_CONSTEXPR friend _V& operator<<=(_V& __lhs, int __x)
  4187. { return __lhs = __lhs << __x; }
  4188. _GLIBCXX_SIMD_CONSTEXPR friend _V& operator>>=(_V& __lhs, int __x)
  4189. { return __lhs = __lhs >> __x; }
  4190. _GLIBCXX_SIMD_CONSTEXPR friend _V operator%(const _V& __x, const _V& __y)
  4191. {
  4192. return _SimdIntOperators::_S_make_derived(
  4193. _Impl::_S_modulus(__data(__x), __data(__y)));
  4194. }
  4195. _GLIBCXX_SIMD_CONSTEXPR friend _V operator&(const _V& __x, const _V& __y)
  4196. {
  4197. return _SimdIntOperators::_S_make_derived(
  4198. _Impl::_S_bit_and(__data(__x), __data(__y)));
  4199. }
  4200. _GLIBCXX_SIMD_CONSTEXPR friend _V operator|(const _V& __x, const _V& __y)
  4201. {
  4202. return _SimdIntOperators::_S_make_derived(
  4203. _Impl::_S_bit_or(__data(__x), __data(__y)));
  4204. }
  4205. _GLIBCXX_SIMD_CONSTEXPR friend _V operator^(const _V& __x, const _V& __y)
  4206. {
  4207. return _SimdIntOperators::_S_make_derived(
  4208. _Impl::_S_bit_xor(__data(__x), __data(__y)));
  4209. }
  4210. _GLIBCXX_SIMD_CONSTEXPR friend _V operator<<(const _V& __x, const _V& __y)
  4211. {
  4212. return _SimdIntOperators::_S_make_derived(
  4213. _Impl::_S_bit_shift_left(__data(__x), __data(__y)));
  4214. }
  4215. _GLIBCXX_SIMD_CONSTEXPR friend _V operator>>(const _V& __x, const _V& __y)
  4216. {
  4217. return _SimdIntOperators::_S_make_derived(
  4218. _Impl::_S_bit_shift_right(__data(__x), __data(__y)));
  4219. }
  4220. template <typename _VV = _V>
  4221. _GLIBCXX_SIMD_CONSTEXPR friend _V operator<<(const _V& __x, int __y)
  4222. {
  4223. using _Tp = typename _VV::value_type;
  4224. if (__y < 0)
  4225. __invoke_ub("The behavior is undefined if the right operand of a "
  4226. "shift operation is negative. [expr.shift]\nA shift by "
  4227. "%d was requested",
  4228. __y);
  4229. if (size_t(__y) >= sizeof(declval<_Tp>() << __y) * __CHAR_BIT__)
  4230. __invoke_ub(
  4231. "The behavior is undefined if the right operand of a "
  4232. "shift operation is greater than or equal to the width of the "
  4233. "promoted left operand. [expr.shift]\nA shift by %d was requested",
  4234. __y);
  4235. return _SimdIntOperators::_S_make_derived(
  4236. _Impl::_S_bit_shift_left(__data(__x), __y));
  4237. }
  4238. template <typename _VV = _V>
  4239. _GLIBCXX_SIMD_CONSTEXPR friend _V operator>>(const _V& __x, int __y)
  4240. {
  4241. using _Tp = typename _VV::value_type;
  4242. if (__y < 0)
  4243. __invoke_ub(
  4244. "The behavior is undefined if the right operand of a shift "
  4245. "operation is negative. [expr.shift]\nA shift by %d was requested",
  4246. __y);
  4247. if (size_t(__y) >= sizeof(declval<_Tp>() << __y) * __CHAR_BIT__)
  4248. __invoke_ub(
  4249. "The behavior is undefined if the right operand of a shift "
  4250. "operation is greater than or equal to the width of the promoted "
  4251. "left operand. [expr.shift]\nA shift by %d was requested",
  4252. __y);
  4253. return _SimdIntOperators::_S_make_derived(
  4254. _Impl::_S_bit_shift_right(__data(__x), __y));
  4255. }
  4256. // unary operators (for integral _Tp)
  4257. _GLIBCXX_SIMD_CONSTEXPR _V operator~() const
  4258. { return {__private_init, _Impl::_S_complement(__derived()._M_data)}; }
  4259. };
  4260. //}}}1
  4261. // simd {{{
  4262. template <typename _Tp, typename _Abi>
  4263. class simd : public _SimdIntOperators<
  4264. simd<_Tp, _Abi>, typename _SimdTraits<_Tp, _Abi>::_SimdImpl,
  4265. conjunction<is_integral<_Tp>,
  4266. typename _SimdTraits<_Tp, _Abi>::_IsValid>::value>,
  4267. public _SimdTraits<_Tp, _Abi>::_SimdBase
  4268. {
  4269. using _Traits = _SimdTraits<_Tp, _Abi>;
  4270. using _MemberType = typename _Traits::_SimdMember;
  4271. using _CastType = typename _Traits::_SimdCastType;
  4272. static constexpr _Tp* _S_type_tag = nullptr;
  4273. friend typename _Traits::_SimdBase;
  4274. public:
  4275. using _Impl = typename _Traits::_SimdImpl;
  4276. friend _Impl;
  4277. friend _SimdIntOperators<simd, _Impl, true>;
  4278. using value_type = _Tp;
  4279. using reference = _SmartReference<_MemberType, _Impl, value_type>;
  4280. using mask_type = simd_mask<_Tp, _Abi>;
  4281. using abi_type = _Abi;
  4282. static constexpr size_t size()
  4283. { return __size_or_zero_v<_Tp, _Abi>; }
  4284. _GLIBCXX_SIMD_CONSTEXPR simd() = default;
  4285. _GLIBCXX_SIMD_CONSTEXPR simd(const simd&) = default;
  4286. _GLIBCXX_SIMD_CONSTEXPR simd(simd&&) noexcept = default;
  4287. _GLIBCXX_SIMD_CONSTEXPR simd& operator=(const simd&) = default;
  4288. _GLIBCXX_SIMD_CONSTEXPR simd& operator=(simd&&) noexcept = default;
  4289. // implicit broadcast constructor
  4290. template <typename _Up,
  4291. typename = enable_if_t<!is_same_v<__remove_cvref_t<_Up>, bool>>>
  4292. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR
  4293. simd(_ValuePreservingOrInt<_Up, value_type>&& __x)
  4294. : _M_data(
  4295. _Impl::_S_broadcast(static_cast<value_type>(static_cast<_Up&&>(__x))))
  4296. {}
  4297. // implicit type conversion constructor (convert from fixed_size to
  4298. // fixed_size)
  4299. template <typename _Up>
  4300. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR
  4301. simd(const simd<_Up, simd_abi::fixed_size<size()>>& __x,
  4302. enable_if_t<
  4303. conjunction<
  4304. is_same<simd_abi::fixed_size<size()>, abi_type>,
  4305. negation<__is_narrowing_conversion<_Up, value_type>>,
  4306. __converts_to_higher_integer_rank<_Up, value_type>>::value,
  4307. void*> = nullptr)
  4308. : simd{static_cast<array<_Up, size()>>(__x).data(), vector_aligned} {}
  4309. // explicit type conversion constructor
  4310. #ifdef _GLIBCXX_SIMD_ENABLE_STATIC_CAST
  4311. template <typename _Up, typename _A2,
  4312. typename = decltype(static_simd_cast<simd>(
  4313. declval<const simd<_Up, _A2>&>()))>
  4314. _GLIBCXX_SIMD_ALWAYS_INLINE explicit _GLIBCXX_SIMD_CONSTEXPR
  4315. simd(const simd<_Up, _A2>& __x)
  4316. : simd(static_simd_cast<simd>(__x)) {}
  4317. #endif // _GLIBCXX_SIMD_ENABLE_STATIC_CAST
  4318. // generator constructor
  4319. template <typename _Fp>
  4320. _GLIBCXX_SIMD_ALWAYS_INLINE explicit _GLIBCXX_SIMD_CONSTEXPR
  4321. simd(_Fp&& __gen, _ValuePreservingOrInt<decltype(declval<_Fp>()(
  4322. declval<_SizeConstant<0>&>())),
  4323. value_type>* = nullptr)
  4324. : _M_data(_Impl::_S_generator(static_cast<_Fp&&>(__gen), _S_type_tag)) {}
  4325. // load constructor
  4326. template <typename _Up, typename _Flags>
  4327. _GLIBCXX_SIMD_ALWAYS_INLINE
  4328. simd(const _Up* __mem, _Flags)
  4329. : _M_data(
  4330. _Impl::_S_load(_Flags::template _S_apply<simd>(__mem), _S_type_tag))
  4331. {}
  4332. // loads [simd.load]
  4333. template <typename _Up, typename _Flags>
  4334. _GLIBCXX_SIMD_ALWAYS_INLINE void
  4335. copy_from(const _Vectorizable<_Up>* __mem, _Flags)
  4336. {
  4337. _M_data = static_cast<decltype(_M_data)>(
  4338. _Impl::_S_load(_Flags::template _S_apply<simd>(__mem), _S_type_tag));
  4339. }
  4340. // stores [simd.store]
  4341. template <typename _Up, typename _Flags>
  4342. _GLIBCXX_SIMD_ALWAYS_INLINE void
  4343. copy_to(_Vectorizable<_Up>* __mem, _Flags) const
  4344. {
  4345. _Impl::_S_store(_M_data, _Flags::template _S_apply<simd>(__mem),
  4346. _S_type_tag);
  4347. }
  4348. // scalar access
  4349. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR reference
  4350. operator[](size_t __i)
  4351. { return {_M_data, int(__i)}; }
  4352. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR value_type
  4353. operator[]([[maybe_unused]] size_t __i) const
  4354. {
  4355. if constexpr (__is_scalar_abi<_Abi>())
  4356. {
  4357. _GLIBCXX_DEBUG_ASSERT(__i == 0);
  4358. return _M_data;
  4359. }
  4360. else
  4361. return _M_data[__i];
  4362. }
  4363. // increment and decrement:
  4364. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd&
  4365. operator++()
  4366. {
  4367. _Impl::_S_increment(_M_data);
  4368. return *this;
  4369. }
  4370. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd
  4371. operator++(int)
  4372. {
  4373. simd __r = *this;
  4374. _Impl::_S_increment(_M_data);
  4375. return __r;
  4376. }
  4377. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd&
  4378. operator--()
  4379. {
  4380. _Impl::_S_decrement(_M_data);
  4381. return *this;
  4382. }
  4383. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd
  4384. operator--(int)
  4385. {
  4386. simd __r = *this;
  4387. _Impl::_S_decrement(_M_data);
  4388. return __r;
  4389. }
  4390. // unary operators (for any _Tp)
  4391. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR mask_type
  4392. operator!() const
  4393. { return {__private_init, _Impl::_S_negate(_M_data)}; }
  4394. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd
  4395. operator+() const
  4396. { return *this; }
  4397. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd
  4398. operator-() const
  4399. { return {__private_init, _Impl::_S_unary_minus(_M_data)}; }
  4400. // access to internal representation (suggested extension)
  4401. _GLIBCXX_SIMD_ALWAYS_INLINE explicit _GLIBCXX_SIMD_CONSTEXPR
  4402. simd(_CastType __init) : _M_data(__init) {}
  4403. // compound assignment [simd.cassign]
  4404. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd&
  4405. operator+=(simd& __lhs, const simd& __x)
  4406. { return __lhs = __lhs + __x; }
  4407. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd&
  4408. operator-=(simd& __lhs, const simd& __x)
  4409. { return __lhs = __lhs - __x; }
  4410. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd&
  4411. operator*=(simd& __lhs, const simd& __x)
  4412. { return __lhs = __lhs * __x; }
  4413. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd&
  4414. operator/=(simd& __lhs, const simd& __x)
  4415. { return __lhs = __lhs / __x; }
  4416. // binary operators [simd.binary]
  4417. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
  4418. operator+(const simd& __x, const simd& __y)
  4419. { return {__private_init, _Impl::_S_plus(__x._M_data, __y._M_data)}; }
  4420. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
  4421. operator-(const simd& __x, const simd& __y)
  4422. { return {__private_init, _Impl::_S_minus(__x._M_data, __y._M_data)}; }
  4423. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
  4424. operator*(const simd& __x, const simd& __y)
  4425. { return {__private_init, _Impl::_S_multiplies(__x._M_data, __y._M_data)}; }
  4426. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
  4427. operator/(const simd& __x, const simd& __y)
  4428. { return {__private_init, _Impl::_S_divides(__x._M_data, __y._M_data)}; }
  4429. // compares [simd.comparison]
  4430. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
  4431. operator==(const simd& __x, const simd& __y)
  4432. { return simd::_S_make_mask(_Impl::_S_equal_to(__x._M_data, __y._M_data)); }
  4433. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
  4434. operator!=(const simd& __x, const simd& __y)
  4435. {
  4436. return simd::_S_make_mask(
  4437. _Impl::_S_not_equal_to(__x._M_data, __y._M_data));
  4438. }
  4439. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
  4440. operator<(const simd& __x, const simd& __y)
  4441. { return simd::_S_make_mask(_Impl::_S_less(__x._M_data, __y._M_data)); }
  4442. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
  4443. operator<=(const simd& __x, const simd& __y)
  4444. {
  4445. return simd::_S_make_mask(_Impl::_S_less_equal(__x._M_data, __y._M_data));
  4446. }
  4447. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
  4448. operator>(const simd& __x, const simd& __y)
  4449. { return simd::_S_make_mask(_Impl::_S_less(__y._M_data, __x._M_data)); }
  4450. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
  4451. operator>=(const simd& __x, const simd& __y)
  4452. {
  4453. return simd::_S_make_mask(_Impl::_S_less_equal(__y._M_data, __x._M_data));
  4454. }
  4455. // operator?: overloads (suggested extension) {{{
  4456. #ifdef __GXX_CONDITIONAL_IS_OVERLOADABLE__
  4457. _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
  4458. operator?:(const mask_type& __k, const simd& __where_true,
  4459. const simd& __where_false)
  4460. {
  4461. auto __ret = __where_false;
  4462. _Impl::_S_masked_assign(__data(__k), __data(__ret), __data(__where_true));
  4463. return __ret;
  4464. }
  4465. #endif // __GXX_CONDITIONAL_IS_OVERLOADABLE__
  4466. // }}}
  4467. // "private" because of the first arguments's namespace
  4468. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
  4469. simd(_PrivateInit, const _MemberType& __init)
  4470. : _M_data(__init) {}
  4471. // "private" because of the first arguments's namespace
  4472. _GLIBCXX_SIMD_INTRINSIC
  4473. simd(_BitsetInit, bitset<size()> __init) : _M_data()
  4474. { where(mask_type(__bitset_init, __init), *this) = ~*this; }
  4475. _GLIBCXX_SIMD_INTRINSIC constexpr bool
  4476. _M_is_constprop() const
  4477. {
  4478. if constexpr (__is_scalar_abi<_Abi>())
  4479. return __builtin_constant_p(_M_data);
  4480. else
  4481. return _M_data._M_is_constprop();
  4482. }
  4483. private:
  4484. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR static mask_type
  4485. _S_make_mask(typename mask_type::_MemberType __k)
  4486. { return {__private_init, __k}; }
  4487. friend const auto& __data<value_type, abi_type>(const simd&);
  4488. friend auto& __data<value_type, abi_type>(simd&);
  4489. alignas(_Traits::_S_simd_align) _MemberType _M_data;
  4490. };
  4491. // }}}
  4492. // __data {{{
  4493. template <typename _Tp, typename _Ap>
  4494. _GLIBCXX_SIMD_INTRINSIC constexpr const auto&
  4495. __data(const simd<_Tp, _Ap>& __x)
  4496. { return __x._M_data; }
  4497. template <typename _Tp, typename _Ap>
  4498. _GLIBCXX_SIMD_INTRINSIC constexpr auto&
  4499. __data(simd<_Tp, _Ap>& __x)
  4500. { return __x._M_data; }
  4501. // }}}
  4502. namespace __float_bitwise_operators { //{{{
  4503. template <typename _Tp, typename _Ap>
  4504. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
  4505. operator^(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
  4506. {
  4507. return {__private_init,
  4508. _Ap::_SimdImpl::_S_bit_xor(__data(__a), __data(__b))};
  4509. }
  4510. template <typename _Tp, typename _Ap>
  4511. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
  4512. operator|(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
  4513. {
  4514. return {__private_init,
  4515. _Ap::_SimdImpl::_S_bit_or(__data(__a), __data(__b))};
  4516. }
  4517. template <typename _Tp, typename _Ap>
  4518. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
  4519. operator&(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
  4520. {
  4521. return {__private_init,
  4522. _Ap::_SimdImpl::_S_bit_and(__data(__a), __data(__b))};
  4523. }
  4524. } // namespace __float_bitwise_operators }}}
  4525. _GLIBCXX_SIMD_END_NAMESPACE
  4526. #endif // __cplusplus >= 201703L
  4527. #endif // _GLIBCXX_EXPERIMENTAL_SIMD_H
  4528. // vim: foldmethod=marker foldmarker={{{,}}}