InstEmitSimdArithmetic.cs 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784
  1. // https://github.com/intel/ARM_NEON_2_x86_SSE/blob/master/NEON_2_SSE.h
  2. // https://www.agner.org/optimize/#vectorclass @ vectori128.h
  3. using ChocolArm64.Decoders;
  4. using ChocolArm64.IntermediateRepresentation;
  5. using ChocolArm64.State;
  6. using ChocolArm64.Translation;
  7. using System;
  8. using System.Reflection;
  9. using System.Reflection.Emit;
  10. using System.Runtime.Intrinsics;
  11. using System.Runtime.Intrinsics.X86;
  12. using static ChocolArm64.Instructions.InstEmitSimdHelper;
  13. namespace ChocolArm64.Instructions
  14. {
  15. static partial class InstEmit
  16. {
  17. public static void Abs_S(ILEmitterCtx context)
  18. {
  19. EmitScalarUnaryOpSx(context, () => EmitAbs(context));
  20. }
  21. public static void Abs_V(ILEmitterCtx context)
  22. {
  23. EmitVectorUnaryOpSx(context, () => EmitAbs(context));
  24. }
  25. public static void Add_S(ILEmitterCtx context)
  26. {
  27. EmitScalarBinaryOpZx(context, () => context.Emit(OpCodes.Add));
  28. }
  29. public static void Add_V(ILEmitterCtx context)
  30. {
  31. if (Optimizations.UseSse2)
  32. {
  33. EmitSse2Op(context, nameof(Sse2.Add));
  34. }
  35. else
  36. {
  37. EmitVectorBinaryOpZx(context, () => context.Emit(OpCodes.Add));
  38. }
  39. }
  40. public static void Addhn_V(ILEmitterCtx context)
  41. {
  42. EmitHighNarrow(context, () => context.Emit(OpCodes.Add), round: false);
  43. }
  44. public static void Addp_S(ILEmitterCtx context)
  45. {
  46. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  47. EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  48. EmitVectorExtractZx(context, op.Rn, 1, op.Size);
  49. context.Emit(OpCodes.Add);
  50. EmitScalarSet(context, op.Rd, op.Size);
  51. }
  52. public static void Addp_V(ILEmitterCtx context)
  53. {
  54. EmitVectorPairwiseOpZx(context, () => context.Emit(OpCodes.Add));
  55. }
  56. public static void Addv_V(ILEmitterCtx context)
  57. {
  58. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  59. int bytes = op.GetBitsCount() >> 3;
  60. int elems = bytes >> op.Size;
  61. EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  62. for (int index = 1; index < elems; index++)
  63. {
  64. EmitVectorExtractZx(context, op.Rn, index, op.Size);
  65. context.Emit(OpCodes.Add);
  66. }
  67. EmitScalarSet(context, op.Rd, op.Size);
  68. }
  69. public static void Cls_V(ILEmitterCtx context)
  70. {
  71. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  72. int bytes = op.GetBitsCount() >> 3;
  73. int elems = bytes >> op.Size;
  74. int eSize = 8 << op.Size;
  75. for (int index = 0; index < elems; index++)
  76. {
  77. EmitVectorExtractZx(context, op.Rn, index, op.Size);
  78. context.EmitLdc_I4(eSize);
  79. SoftFallback.EmitCall(context, nameof(SoftFallback.CountLeadingSigns));
  80. EmitVectorInsert(context, op.Rd, index, op.Size);
  81. }
  82. if (op.RegisterSize == RegisterSize.Simd64)
  83. {
  84. EmitVectorZeroUpper(context, op.Rd);
  85. }
  86. }
  87. public static void Clz_V(ILEmitterCtx context)
  88. {
  89. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  90. int bytes = op.GetBitsCount() >> 3;
  91. int elems = bytes >> op.Size;
  92. int eSize = 8 << op.Size;
  93. for (int index = 0; index < elems; index++)
  94. {
  95. EmitVectorExtractZx(context, op.Rn, index, op.Size);
  96. if (Lzcnt.IsSupported && eSize == 32)
  97. {
  98. context.Emit(OpCodes.Conv_U4);
  99. context.EmitCall(typeof(Lzcnt).GetMethod(nameof(Lzcnt.LeadingZeroCount), new Type[] { typeof(uint) }));
  100. context.Emit(OpCodes.Conv_U8);
  101. }
  102. else
  103. {
  104. context.EmitLdc_I4(eSize);
  105. SoftFallback.EmitCall(context, nameof(SoftFallback.CountLeadingZeros));
  106. }
  107. EmitVectorInsert(context, op.Rd, index, op.Size);
  108. }
  109. if (op.RegisterSize == RegisterSize.Simd64)
  110. {
  111. EmitVectorZeroUpper(context, op.Rd);
  112. }
  113. }
  114. public static void Cnt_V(ILEmitterCtx context)
  115. {
  116. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  117. int elems = op.RegisterSize == RegisterSize.Simd128 ? 16 : 8;
  118. for (int index = 0; index < elems; index++)
  119. {
  120. EmitVectorExtractZx(context, op.Rn, index, 0);
  121. if (Popcnt.IsSupported)
  122. {
  123. context.EmitCall(typeof(Popcnt).GetMethod(nameof(Popcnt.PopCount), new Type[] { typeof(ulong) }));
  124. }
  125. else
  126. {
  127. SoftFallback.EmitCall(context, nameof(SoftFallback.CountSetBits8));
  128. }
  129. EmitVectorInsert(context, op.Rd, index, 0);
  130. }
  131. if (op.RegisterSize == RegisterSize.Simd64)
  132. {
  133. EmitVectorZeroUpper(context, op.Rd);
  134. }
  135. }
  136. public static void Fabd_S(ILEmitterCtx context)
  137. {
  138. if (Optimizations.FastFP && Optimizations.UseSse2)
  139. {
  140. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  141. int sizeF = op.Size & 1;
  142. if (sizeF == 0)
  143. {
  144. Type[] typesSsv = new Type[] { typeof(float) };
  145. Type[] typesSubAnt = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  146. context.EmitLdc_R4(-0f);
  147. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  148. context.EmitLdvec(op.Rn);
  149. context.EmitLdvec(op.Rm);
  150. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SubtractScalar), typesSubAnt));
  151. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AndNot), typesSubAnt));
  152. context.EmitStvec(op.Rd);
  153. EmitVectorZero32_128(context, op.Rd);
  154. }
  155. else /* if (sizeF == 1) */
  156. {
  157. Type[] typesSsv = new Type[] { typeof(double) };
  158. Type[] typesSubAnt = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  159. context.EmitLdc_R8(-0d);
  160. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  161. context.EmitLdvec(op.Rn);
  162. context.EmitLdvec(op.Rm);
  163. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SubtractScalar), typesSubAnt));
  164. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesSubAnt));
  165. context.EmitStvec(op.Rd);
  166. EmitVectorZeroUpper(context, op.Rd);
  167. }
  168. }
  169. else
  170. {
  171. EmitScalarBinaryOpF(context, () =>
  172. {
  173. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub));
  174. EmitUnaryMathCall(context, nameof(Math.Abs));
  175. });
  176. }
  177. }
  178. public static void Fabd_V(ILEmitterCtx context)
  179. {
  180. if (Optimizations.FastFP && Optimizations.UseSse2)
  181. {
  182. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  183. int sizeF = op.Size & 1;
  184. if (sizeF == 0)
  185. {
  186. Type[] typesSav = new Type[] { typeof(float) };
  187. Type[] typesSubAnt = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  188. context.EmitLdc_R4(-0f);
  189. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  190. context.EmitLdvec(op.Rn);
  191. context.EmitLdvec(op.Rm);
  192. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesSubAnt));
  193. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AndNot), typesSubAnt));
  194. context.EmitStvec(op.Rd);
  195. if (op.RegisterSize == RegisterSize.Simd64)
  196. {
  197. EmitVectorZeroUpper(context, op.Rd);
  198. }
  199. }
  200. else /* if (sizeF == 1) */
  201. {
  202. Type[] typesSav = new Type[] { typeof(double) };
  203. Type[] typesSubAnt = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  204. context.EmitLdc_R8(-0d);
  205. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  206. context.EmitLdvec(op.Rn);
  207. context.EmitLdvec(op.Rm);
  208. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSubAnt));
  209. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesSubAnt));
  210. context.EmitStvec(op.Rd);
  211. }
  212. }
  213. else
  214. {
  215. EmitVectorBinaryOpF(context, () =>
  216. {
  217. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub));
  218. EmitUnaryMathCall(context, nameof(Math.Abs));
  219. });
  220. }
  221. }
  222. public static void Fabs_S(ILEmitterCtx context)
  223. {
  224. if (Optimizations.UseSse2)
  225. {
  226. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  227. if (op.Size == 0)
  228. {
  229. Type[] typesSsv = new Type[] { typeof(float) };
  230. Type[] typesAnt = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  231. context.EmitLdc_R4(-0f);
  232. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  233. context.EmitLdvec(op.Rn);
  234. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AndNot), typesAnt));
  235. context.EmitStvec(op.Rd);
  236. EmitVectorZero32_128(context, op.Rd);
  237. }
  238. else /* if (op.Size == 1) */
  239. {
  240. Type[] typesSsv = new Type[] { typeof(double) };
  241. Type[] typesAnt = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  242. context.EmitLdc_R8(-0d);
  243. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  244. context.EmitLdvec(op.Rn);
  245. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAnt));
  246. context.EmitStvec(op.Rd);
  247. EmitVectorZeroUpper(context, op.Rd);
  248. }
  249. }
  250. else
  251. {
  252. EmitScalarUnaryOpF(context, () =>
  253. {
  254. EmitUnaryMathCall(context, nameof(Math.Abs));
  255. });
  256. }
  257. }
  258. public static void Fabs_V(ILEmitterCtx context)
  259. {
  260. if (Optimizations.UseSse2)
  261. {
  262. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  263. int sizeF = op.Size & 1;
  264. if (sizeF == 0)
  265. {
  266. Type[] typesSav = new Type[] { typeof(float) };
  267. Type[] typesAnt = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  268. context.EmitLdc_R4(-0f);
  269. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  270. context.EmitLdvec(op.Rn);
  271. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AndNot), typesAnt));
  272. context.EmitStvec(op.Rd);
  273. if (op.RegisterSize == RegisterSize.Simd64)
  274. {
  275. EmitVectorZeroUpper(context, op.Rd);
  276. }
  277. }
  278. else /* if (sizeF == 1) */
  279. {
  280. Type[] typesSav = new Type[] { typeof(double) };
  281. Type[] typesAnt = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  282. context.EmitLdc_R8(-0d);
  283. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  284. context.EmitLdvec(op.Rn);
  285. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAnt));
  286. context.EmitStvec(op.Rd);
  287. }
  288. }
  289. else
  290. {
  291. EmitVectorUnaryOpF(context, () =>
  292. {
  293. EmitUnaryMathCall(context, nameof(Math.Abs));
  294. });
  295. }
  296. }
  297. public static void Fadd_S(ILEmitterCtx context)
  298. {
  299. if (Optimizations.FastFP && Optimizations.UseSse2)
  300. {
  301. EmitScalarSseOrSse2OpF(context, nameof(Sse.AddScalar));
  302. }
  303. else
  304. {
  305. EmitScalarBinaryOpF(context, () =>
  306. {
  307. EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd));
  308. });
  309. }
  310. }
  311. public static void Fadd_V(ILEmitterCtx context)
  312. {
  313. if (Optimizations.FastFP && Optimizations.UseSse2)
  314. {
  315. EmitVectorSseOrSse2OpF(context, nameof(Sse.Add));
  316. }
  317. else
  318. {
  319. EmitVectorBinaryOpF(context, () =>
  320. {
  321. EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd));
  322. });
  323. }
  324. }
  325. public static void Faddp_S(ILEmitterCtx context)
  326. {
  327. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  328. int sizeF = op.Size & 1;
  329. if (Optimizations.FastFP && Optimizations.UseSse3)
  330. {
  331. if (sizeF == 0)
  332. {
  333. Type[] typesAddH = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  334. context.EmitLdvec(op.Rn);
  335. context.EmitLdvec(op.Rn);
  336. context.EmitCall(typeof(Sse3).GetMethod(nameof(Sse3.HorizontalAdd), typesAddH));
  337. context.EmitStvec(op.Rd);
  338. EmitVectorZero32_128(context, op.Rd);
  339. }
  340. else /* if (sizeF == 1) */
  341. {
  342. Type[] typesAddH = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  343. context.EmitLdvec(op.Rn);
  344. context.EmitLdvec(op.Rn);
  345. context.EmitCall(typeof(Sse3).GetMethod(nameof(Sse3.HorizontalAdd), typesAddH));
  346. context.EmitStvec(op.Rd);
  347. EmitVectorZeroUpper(context, op.Rd);
  348. }
  349. }
  350. else
  351. {
  352. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  353. EmitVectorExtractF(context, op.Rn, 1, sizeF);
  354. EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd));
  355. EmitScalarSetF(context, op.Rd, sizeF);
  356. }
  357. }
  358. public static void Faddp_V(ILEmitterCtx context)
  359. {
  360. if (Optimizations.FastFP && Optimizations.UseSse2)
  361. {
  362. EmitVectorPairwiseSseOrSse2OpF(context, nameof(Sse.Add));
  363. }
  364. else
  365. {
  366. EmitVectorPairwiseOpF(context, () =>
  367. {
  368. EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd));
  369. });
  370. }
  371. }
  372. public static void Fdiv_S(ILEmitterCtx context)
  373. {
  374. if (Optimizations.FastFP && Optimizations.UseSse2)
  375. {
  376. EmitScalarSseOrSse2OpF(context, nameof(Sse.DivideScalar));
  377. }
  378. else
  379. {
  380. EmitScalarBinaryOpF(context, () =>
  381. {
  382. EmitSoftFloatCall(context, nameof(SoftFloat32.FPDiv));
  383. });
  384. }
  385. }
  386. public static void Fdiv_V(ILEmitterCtx context)
  387. {
  388. if (Optimizations.FastFP && Optimizations.UseSse2)
  389. {
  390. EmitVectorSseOrSse2OpF(context, nameof(Sse.Divide));
  391. }
  392. else
  393. {
  394. EmitVectorBinaryOpF(context, () =>
  395. {
  396. EmitSoftFloatCall(context, nameof(SoftFloat32.FPDiv));
  397. });
  398. }
  399. }
  400. public static void Fmadd_S(ILEmitterCtx context) // Fused.
  401. {
  402. if (Optimizations.FastFP && Optimizations.UseSse2)
  403. {
  404. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  405. if (op.Size == 0)
  406. {
  407. Type[] typesMulAdd = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  408. context.EmitLdvec(op.Ra);
  409. context.EmitLdvec(op.Rn);
  410. context.EmitLdvec(op.Rm);
  411. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulAdd));
  412. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AddScalar), typesMulAdd));
  413. context.EmitStvec(op.Rd);
  414. EmitVectorZero32_128(context, op.Rd);
  415. }
  416. else /* if (op.Size == 1) */
  417. {
  418. Type[] typesMulAdd = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  419. context.EmitLdvec(op.Ra);
  420. context.EmitLdvec(op.Rn);
  421. context.EmitLdvec(op.Rm);
  422. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulAdd));
  423. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AddScalar), typesMulAdd));
  424. context.EmitStvec(op.Rd);
  425. EmitVectorZeroUpper(context, op.Rd);
  426. }
  427. }
  428. else
  429. {
  430. EmitScalarTernaryRaOpF(context, () =>
  431. {
  432. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulAdd));
  433. });
  434. }
  435. }
  436. public static void Fmax_S(ILEmitterCtx context)
  437. {
  438. if (Optimizations.FastFP && Optimizations.UseSse2)
  439. {
  440. EmitScalarSseOrSse2OpF(context, nameof(Sse.MaxScalar));
  441. }
  442. else
  443. {
  444. EmitScalarBinaryOpF(context, () =>
  445. {
  446. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMax));
  447. });
  448. }
  449. }
  450. public static void Fmax_V(ILEmitterCtx context)
  451. {
  452. if (Optimizations.FastFP && Optimizations.UseSse2)
  453. {
  454. EmitVectorSseOrSse2OpF(context, nameof(Sse.Max));
  455. }
  456. else
  457. {
  458. EmitVectorBinaryOpF(context, () =>
  459. {
  460. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMax));
  461. });
  462. }
  463. }
  464. public static void Fmaxnm_S(ILEmitterCtx context)
  465. {
  466. EmitScalarBinaryOpF(context, () =>
  467. {
  468. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMaxNum));
  469. });
  470. }
  471. public static void Fmaxnm_V(ILEmitterCtx context)
  472. {
  473. EmitVectorBinaryOpF(context, () =>
  474. {
  475. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMaxNum));
  476. });
  477. }
  478. public static void Fmaxp_V(ILEmitterCtx context)
  479. {
  480. if (Optimizations.FastFP && Optimizations.UseSse2)
  481. {
  482. EmitVectorPairwiseSseOrSse2OpF(context, nameof(Sse.Max));
  483. }
  484. else
  485. {
  486. EmitVectorPairwiseOpF(context, () =>
  487. {
  488. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMax));
  489. });
  490. }
  491. }
  492. public static void Fmin_S(ILEmitterCtx context)
  493. {
  494. if (Optimizations.FastFP && Optimizations.UseSse2)
  495. {
  496. EmitScalarSseOrSse2OpF(context, nameof(Sse.MinScalar));
  497. }
  498. else
  499. {
  500. EmitScalarBinaryOpF(context, () =>
  501. {
  502. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMin));
  503. });
  504. }
  505. }
  506. public static void Fmin_V(ILEmitterCtx context)
  507. {
  508. if (Optimizations.FastFP && Optimizations.UseSse2)
  509. {
  510. EmitVectorSseOrSse2OpF(context, nameof(Sse.Min));
  511. }
  512. else
  513. {
  514. EmitVectorBinaryOpF(context, () =>
  515. {
  516. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMin));
  517. });
  518. }
  519. }
  520. public static void Fminnm_S(ILEmitterCtx context)
  521. {
  522. EmitScalarBinaryOpF(context, () =>
  523. {
  524. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMinNum));
  525. });
  526. }
  527. public static void Fminnm_V(ILEmitterCtx context)
  528. {
  529. EmitVectorBinaryOpF(context, () =>
  530. {
  531. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMinNum));
  532. });
  533. }
  534. public static void Fminp_V(ILEmitterCtx context)
  535. {
  536. if (Optimizations.FastFP && Optimizations.UseSse2)
  537. {
  538. EmitVectorPairwiseSseOrSse2OpF(context, nameof(Sse.Min));
  539. }
  540. else
  541. {
  542. EmitVectorPairwiseOpF(context, () =>
  543. {
  544. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMin));
  545. });
  546. }
  547. }
  548. public static void Fmla_Se(ILEmitterCtx context)
  549. {
  550. EmitScalarTernaryOpByElemF(context, () =>
  551. {
  552. context.Emit(OpCodes.Mul);
  553. context.Emit(OpCodes.Add);
  554. });
  555. }
  556. public static void Fmla_V(ILEmitterCtx context) // Fused.
  557. {
  558. if (Optimizations.FastFP && Optimizations.UseSse2)
  559. {
  560. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  561. int sizeF = op.Size & 1;
  562. if (sizeF == 0)
  563. {
  564. Type[] typesMulAdd = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  565. context.EmitLdvec(op.Rd);
  566. context.EmitLdvec(op.Rn);
  567. context.EmitLdvec(op.Rm);
  568. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulAdd));
  569. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Add), typesMulAdd));
  570. context.EmitStvec(op.Rd);
  571. if (op.RegisterSize == RegisterSize.Simd64)
  572. {
  573. EmitVectorZeroUpper(context, op.Rd);
  574. }
  575. }
  576. else /* if (sizeF == 1) */
  577. {
  578. Type[] typesMulAdd = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  579. context.EmitLdvec(op.Rd);
  580. context.EmitLdvec(op.Rn);
  581. context.EmitLdvec(op.Rm);
  582. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulAdd));
  583. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesMulAdd));
  584. context.EmitStvec(op.Rd);
  585. }
  586. }
  587. else
  588. {
  589. EmitVectorTernaryOpF(context, () =>
  590. {
  591. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulAdd));
  592. });
  593. }
  594. }
  595. public static void Fmla_Ve(ILEmitterCtx context) // Fused.
  596. {
  597. if (Optimizations.FastFP && Optimizations.UseSse2)
  598. {
  599. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  600. int sizeF = op.Size & 1;
  601. if (sizeF == 0)
  602. {
  603. Type[] typesSfl = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>), typeof(byte) };
  604. Type[] typesMulAdd = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  605. context.EmitLdvec(op.Rd);
  606. context.EmitLdvec(op.Rn);
  607. context.EmitLdvec(op.Rm);
  608. context.EmitLdvec(op.Rm);
  609. context.EmitLdc_I4(op.Index | op.Index << 2 | op.Index << 4 | op.Index << 6);
  610. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  611. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulAdd));
  612. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Add), typesMulAdd));
  613. context.EmitStvec(op.Rd);
  614. if (op.RegisterSize == RegisterSize.Simd64)
  615. {
  616. EmitVectorZeroUpper(context, op.Rd);
  617. }
  618. }
  619. else /* if (sizeF == 1) */
  620. {
  621. Type[] typesSfl = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>), typeof(byte) };
  622. Type[] typesMulAdd = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  623. context.EmitLdvec(op.Rd);
  624. context.EmitLdvec(op.Rn);
  625. context.EmitLdvec(op.Rm);
  626. context.EmitLdvec(op.Rm);
  627. context.EmitLdc_I4(op.Index | op.Index << 1);
  628. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Shuffle), typesSfl));
  629. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulAdd));
  630. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesMulAdd));
  631. context.EmitStvec(op.Rd);
  632. }
  633. }
  634. else
  635. {
  636. EmitVectorTernaryOpByElemF(context, () =>
  637. {
  638. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulAdd));
  639. });
  640. }
  641. }
  642. public static void Fmls_Se(ILEmitterCtx context)
  643. {
  644. EmitScalarTernaryOpByElemF(context, () =>
  645. {
  646. context.Emit(OpCodes.Mul);
  647. context.Emit(OpCodes.Sub);
  648. });
  649. }
  650. public static void Fmls_V(ILEmitterCtx context) // Fused.
  651. {
  652. if (Optimizations.FastFP && Optimizations.UseSse2)
  653. {
  654. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  655. int sizeF = op.Size & 1;
  656. if (sizeF == 0)
  657. {
  658. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  659. context.EmitLdvec(op.Rd);
  660. context.EmitLdvec(op.Rn);
  661. context.EmitLdvec(op.Rm);
  662. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  663. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesMulSub));
  664. context.EmitStvec(op.Rd);
  665. if (op.RegisterSize == RegisterSize.Simd64)
  666. {
  667. EmitVectorZeroUpper(context, op.Rd);
  668. }
  669. }
  670. else /* if (sizeF == 1) */
  671. {
  672. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  673. context.EmitLdvec(op.Rd);
  674. context.EmitLdvec(op.Rn);
  675. context.EmitLdvec(op.Rm);
  676. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  677. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  678. context.EmitStvec(op.Rd);
  679. }
  680. }
  681. else
  682. {
  683. EmitVectorTernaryOpF(context, () =>
  684. {
  685. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulSub));
  686. });
  687. }
  688. }
  689. public static void Fmls_Ve(ILEmitterCtx context) // Fused.
  690. {
  691. if (Optimizations.FastFP && Optimizations.UseSse2)
  692. {
  693. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  694. int sizeF = op.Size & 1;
  695. if (sizeF == 0)
  696. {
  697. Type[] typesSfl = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>), typeof(byte) };
  698. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  699. context.EmitLdvec(op.Rd);
  700. context.EmitLdvec(op.Rn);
  701. context.EmitLdvec(op.Rm);
  702. context.EmitLdvec(op.Rm);
  703. context.EmitLdc_I4(op.Index | op.Index << 2 | op.Index << 4 | op.Index << 6);
  704. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  705. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  706. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesMulSub));
  707. context.EmitStvec(op.Rd);
  708. if (op.RegisterSize == RegisterSize.Simd64)
  709. {
  710. EmitVectorZeroUpper(context, op.Rd);
  711. }
  712. }
  713. else /* if (sizeF == 1) */
  714. {
  715. Type[] typesSfl = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>), typeof(byte) };
  716. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  717. context.EmitLdvec(op.Rd);
  718. context.EmitLdvec(op.Rn);
  719. context.EmitLdvec(op.Rm);
  720. context.EmitLdvec(op.Rm);
  721. context.EmitLdc_I4(op.Index | op.Index << 1);
  722. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Shuffle), typesSfl));
  723. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  724. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  725. context.EmitStvec(op.Rd);
  726. }
  727. }
  728. else
  729. {
  730. EmitVectorTernaryOpByElemF(context, () =>
  731. {
  732. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulSub));
  733. });
  734. }
  735. }
  736. public static void Fmsub_S(ILEmitterCtx context) // Fused.
  737. {
  738. if (Optimizations.FastFP && Optimizations.UseSse2)
  739. {
  740. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  741. if (op.Size == 0)
  742. {
  743. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  744. context.EmitLdvec(op.Ra);
  745. context.EmitLdvec(op.Rn);
  746. context.EmitLdvec(op.Rm);
  747. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulSub));
  748. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SubtractScalar), typesMulSub));
  749. context.EmitStvec(op.Rd);
  750. EmitVectorZero32_128(context, op.Rd);
  751. }
  752. else /* if (op.Size == 1) */
  753. {
  754. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  755. context.EmitLdvec(op.Ra);
  756. context.EmitLdvec(op.Rn);
  757. context.EmitLdvec(op.Rm);
  758. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulSub));
  759. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SubtractScalar), typesMulSub));
  760. context.EmitStvec(op.Rd);
  761. EmitVectorZeroUpper(context, op.Rd);
  762. }
  763. }
  764. else
  765. {
  766. EmitScalarTernaryRaOpF(context, () =>
  767. {
  768. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulSub));
  769. });
  770. }
  771. }
  772. public static void Fmul_S(ILEmitterCtx context)
  773. {
  774. if (Optimizations.FastFP && Optimizations.UseSse2)
  775. {
  776. EmitScalarSseOrSse2OpF(context, nameof(Sse.MultiplyScalar));
  777. }
  778. else
  779. {
  780. EmitScalarBinaryOpF(context, () =>
  781. {
  782. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul));
  783. });
  784. }
  785. }
  786. public static void Fmul_Se(ILEmitterCtx context)
  787. {
  788. EmitScalarBinaryOpByElemF(context, () => context.Emit(OpCodes.Mul));
  789. }
  790. public static void Fmul_V(ILEmitterCtx context)
  791. {
  792. if (Optimizations.FastFP && Optimizations.UseSse2)
  793. {
  794. EmitVectorSseOrSse2OpF(context, nameof(Sse.Multiply));
  795. }
  796. else
  797. {
  798. EmitVectorBinaryOpF(context, () =>
  799. {
  800. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul));
  801. });
  802. }
  803. }
  804. public static void Fmul_Ve(ILEmitterCtx context)
  805. {
  806. if (Optimizations.FastFP && Optimizations.UseSse2)
  807. {
  808. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  809. int sizeF = op.Size & 1;
  810. if (sizeF == 0)
  811. {
  812. Type[] typesSfl = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>), typeof(byte) };
  813. Type[] typesMul = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  814. context.EmitLdvec(op.Rn);
  815. context.EmitLdvec(op.Rm);
  816. context.EmitLdvec(op.Rm);
  817. context.EmitLdc_I4(op.Index | op.Index << 2 | op.Index << 4 | op.Index << 6);
  818. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  819. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMul));
  820. context.EmitStvec(op.Rd);
  821. if (op.RegisterSize == RegisterSize.Simd64)
  822. {
  823. EmitVectorZeroUpper(context, op.Rd);
  824. }
  825. }
  826. else /* if (sizeF == 1) */
  827. {
  828. Type[] typesSfl = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>), typeof(byte) };
  829. Type[] typesMul = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  830. context.EmitLdvec(op.Rn);
  831. context.EmitLdvec(op.Rm);
  832. context.EmitLdvec(op.Rm);
  833. context.EmitLdc_I4(op.Index | op.Index << 1);
  834. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Shuffle), typesSfl));
  835. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMul));
  836. context.EmitStvec(op.Rd);
  837. }
  838. }
  839. else
  840. {
  841. EmitVectorBinaryOpByElemF(context, () =>
  842. {
  843. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul));
  844. });
  845. }
  846. }
  847. public static void Fmulx_S(ILEmitterCtx context)
  848. {
  849. EmitScalarBinaryOpF(context, () =>
  850. {
  851. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulX));
  852. });
  853. }
  854. public static void Fmulx_Se(ILEmitterCtx context)
  855. {
  856. EmitScalarBinaryOpByElemF(context, () =>
  857. {
  858. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulX));
  859. });
  860. }
  861. public static void Fmulx_V(ILEmitterCtx context)
  862. {
  863. EmitVectorBinaryOpF(context, () =>
  864. {
  865. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulX));
  866. });
  867. }
  868. public static void Fmulx_Ve(ILEmitterCtx context)
  869. {
  870. EmitVectorBinaryOpByElemF(context, () =>
  871. {
  872. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulX));
  873. });
  874. }
  875. public static void Fneg_S(ILEmitterCtx context)
  876. {
  877. if (Optimizations.UseSse2)
  878. {
  879. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  880. if (op.Size == 0)
  881. {
  882. Type[] typesSsv = new Type[] { typeof(float) };
  883. Type[] typesXor = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  884. context.EmitLdc_R4(-0f);
  885. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  886. context.EmitLdvec(op.Rn);
  887. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), typesXor));
  888. context.EmitStvec(op.Rd);
  889. EmitVectorZero32_128(context, op.Rd);
  890. }
  891. else /* if (op.Size == 1) */
  892. {
  893. Type[] typesSsv = new Type[] { typeof(double) };
  894. Type[] typesXor = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  895. context.EmitLdc_R8(-0d);
  896. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  897. context.EmitLdvec(op.Rn);
  898. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesXor));
  899. context.EmitStvec(op.Rd);
  900. EmitVectorZeroUpper(context, op.Rd);
  901. }
  902. }
  903. else
  904. {
  905. EmitScalarUnaryOpF(context, () => context.Emit(OpCodes.Neg));
  906. }
  907. }
  908. public static void Fneg_V(ILEmitterCtx context)
  909. {
  910. if (Optimizations.UseSse2)
  911. {
  912. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  913. int sizeF = op.Size & 1;
  914. if (sizeF == 0)
  915. {
  916. Type[] typesSav = new Type[] { typeof(float) };
  917. Type[] typesXor = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  918. context.EmitLdc_R4(-0f);
  919. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  920. context.EmitLdvec(op.Rn);
  921. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), typesXor));
  922. context.EmitStvec(op.Rd);
  923. if (op.RegisterSize == RegisterSize.Simd64)
  924. {
  925. EmitVectorZeroUpper(context, op.Rd);
  926. }
  927. }
  928. else /* if (sizeF == 1) */
  929. {
  930. Type[] typesSav = new Type[] { typeof(double) };
  931. Type[] typesXor = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  932. context.EmitLdc_R8(-0d);
  933. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  934. context.EmitLdvec(op.Rn);
  935. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesXor));
  936. context.EmitStvec(op.Rd);
  937. }
  938. }
  939. else
  940. {
  941. EmitVectorUnaryOpF(context, () => context.Emit(OpCodes.Neg));
  942. }
  943. }
  944. public static void Fnmadd_S(ILEmitterCtx context)
  945. {
  946. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  947. int sizeF = op.Size & 1;
  948. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  949. context.Emit(OpCodes.Neg);
  950. EmitVectorExtractF(context, op.Rm, 0, sizeF);
  951. context.Emit(OpCodes.Mul);
  952. EmitVectorExtractF(context, op.Ra, 0, sizeF);
  953. context.Emit(OpCodes.Sub);
  954. EmitScalarSetF(context, op.Rd, sizeF);
  955. }
  956. public static void Fnmsub_S(ILEmitterCtx context)
  957. {
  958. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  959. int sizeF = op.Size & 1;
  960. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  961. EmitVectorExtractF(context, op.Rm, 0, sizeF);
  962. context.Emit(OpCodes.Mul);
  963. EmitVectorExtractF(context, op.Ra, 0, sizeF);
  964. context.Emit(OpCodes.Sub);
  965. EmitScalarSetF(context, op.Rd, sizeF);
  966. }
  967. public static void Fnmul_S(ILEmitterCtx context)
  968. {
  969. EmitScalarBinaryOpF(context, () =>
  970. {
  971. context.Emit(OpCodes.Mul);
  972. context.Emit(OpCodes.Neg);
  973. });
  974. }
  975. public static void Frecpe_S(ILEmitterCtx context)
  976. {
  977. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  978. int sizeF = op.Size & 1;
  979. if (Optimizations.FastFP && Optimizations.UseSse && sizeF == 0)
  980. {
  981. EmitScalarSseOrSse2OpF(context, nameof(Sse.ReciprocalScalar));
  982. }
  983. else
  984. {
  985. EmitScalarUnaryOpF(context, () =>
  986. {
  987. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipEstimate));
  988. });
  989. }
  990. }
  991. public static void Frecpe_V(ILEmitterCtx context)
  992. {
  993. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  994. int sizeF = op.Size & 1;
  995. if (Optimizations.FastFP && Optimizations.UseSse && sizeF == 0)
  996. {
  997. EmitVectorSseOrSse2OpF(context, nameof(Sse.Reciprocal));
  998. }
  999. else
  1000. {
  1001. EmitVectorUnaryOpF(context, () =>
  1002. {
  1003. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipEstimate));
  1004. });
  1005. }
  1006. }
  1007. public static void Frecps_S(ILEmitterCtx context) // Fused.
  1008. {
  1009. if (Optimizations.FastFP && Optimizations.UseSse2)
  1010. {
  1011. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1012. int sizeF = op.Size & 1;
  1013. if (sizeF == 0)
  1014. {
  1015. Type[] typesSsv = new Type[] { typeof(float) };
  1016. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  1017. context.EmitLdc_R4(2f);
  1018. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  1019. context.EmitLdvec(op.Rn);
  1020. context.EmitLdvec(op.Rm);
  1021. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulSub));
  1022. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SubtractScalar), typesMulSub));
  1023. context.EmitStvec(op.Rd);
  1024. EmitVectorZero32_128(context, op.Rd);
  1025. }
  1026. else /* if (sizeF == 1) */
  1027. {
  1028. Type[] typesSsv = new Type[] { typeof(double) };
  1029. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  1030. context.EmitLdc_R8(2d);
  1031. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  1032. context.EmitLdvec(op.Rn);
  1033. context.EmitLdvec(op.Rm);
  1034. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulSub));
  1035. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SubtractScalar), typesMulSub));
  1036. context.EmitStvec(op.Rd);
  1037. EmitVectorZeroUpper(context, op.Rd);
  1038. }
  1039. }
  1040. else
  1041. {
  1042. EmitScalarBinaryOpF(context, () =>
  1043. {
  1044. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipStepFused));
  1045. });
  1046. }
  1047. }
  1048. public static void Frecps_V(ILEmitterCtx context) // Fused.
  1049. {
  1050. if (Optimizations.FastFP && Optimizations.UseSse2)
  1051. {
  1052. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1053. int sizeF = op.Size & 1;
  1054. if (sizeF == 0)
  1055. {
  1056. Type[] typesSav = new Type[] { typeof(float) };
  1057. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  1058. context.EmitLdc_R4(2f);
  1059. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  1060. context.EmitLdvec(op.Rn);
  1061. context.EmitLdvec(op.Rm);
  1062. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  1063. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesMulSub));
  1064. context.EmitStvec(op.Rd);
  1065. if (op.RegisterSize == RegisterSize.Simd64)
  1066. {
  1067. EmitVectorZeroUpper(context, op.Rd);
  1068. }
  1069. }
  1070. else /* if (sizeF == 1) */
  1071. {
  1072. Type[] typesSav = new Type[] { typeof(double) };
  1073. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  1074. context.EmitLdc_R8(2d);
  1075. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1076. context.EmitLdvec(op.Rn);
  1077. context.EmitLdvec(op.Rm);
  1078. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  1079. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  1080. context.EmitStvec(op.Rd);
  1081. }
  1082. }
  1083. else
  1084. {
  1085. EmitVectorBinaryOpF(context, () =>
  1086. {
  1087. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipStepFused));
  1088. });
  1089. }
  1090. }
  1091. public static void Frecpx_S(ILEmitterCtx context)
  1092. {
  1093. EmitScalarUnaryOpF(context, () =>
  1094. {
  1095. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecpX));
  1096. });
  1097. }
  1098. public static void Frinta_S(ILEmitterCtx context)
  1099. {
  1100. EmitScalarUnaryOpF(context, () =>
  1101. {
  1102. EmitRoundMathCall(context, MidpointRounding.AwayFromZero);
  1103. });
  1104. }
  1105. public static void Frinta_V(ILEmitterCtx context)
  1106. {
  1107. EmitVectorUnaryOpF(context, () =>
  1108. {
  1109. EmitRoundMathCall(context, MidpointRounding.AwayFromZero);
  1110. });
  1111. }
  1112. public static void Frinti_S(ILEmitterCtx context)
  1113. {
  1114. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1115. if (Optimizations.UseSse41)
  1116. {
  1117. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1118. context.EmitLdvec(op.Rn);
  1119. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1120. if (op.Size == 0)
  1121. {
  1122. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41ScalarRoundF));
  1123. }
  1124. else /* if (op.Size == 1) */
  1125. {
  1126. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41ScalarRound));
  1127. }
  1128. context.EmitStvec(op.Rd);
  1129. }
  1130. else
  1131. {
  1132. EmitScalarUnaryOpF(context, () =>
  1133. {
  1134. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1135. if (op.Size == 0)
  1136. {
  1137. VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
  1138. }
  1139. else /* if (op.Size == 1) */
  1140. {
  1141. VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
  1142. }
  1143. });
  1144. }
  1145. }
  1146. public static void Frinti_V(ILEmitterCtx context)
  1147. {
  1148. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1149. int sizeF = op.Size & 1;
  1150. if (Optimizations.UseSse41)
  1151. {
  1152. context.EmitLdvec(op.Rn);
  1153. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1154. if (sizeF == 0)
  1155. {
  1156. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorRoundF));
  1157. }
  1158. else /* if (sizeF == 1) */
  1159. {
  1160. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorRound));
  1161. }
  1162. context.EmitStvec(op.Rd);
  1163. if (sizeF == 0 && op.RegisterSize == RegisterSize.Simd64)
  1164. {
  1165. EmitVectorZeroUpper(context, op.Rd);
  1166. }
  1167. }
  1168. else
  1169. {
  1170. EmitVectorUnaryOpF(context, () =>
  1171. {
  1172. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1173. if (sizeF == 0)
  1174. {
  1175. VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
  1176. }
  1177. else /* if (sizeF == 1) */
  1178. {
  1179. VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
  1180. }
  1181. });
  1182. }
  1183. }
  1184. public static void Frintm_S(ILEmitterCtx context)
  1185. {
  1186. if (Optimizations.UseSse41)
  1187. {
  1188. EmitSse41Frint(context, RoundMode.TowardsMinusInfinity, scalar: true);
  1189. }
  1190. else
  1191. {
  1192. EmitScalarUnaryOpF(context, () =>
  1193. {
  1194. EmitUnaryMathCall(context, nameof(Math.Floor));
  1195. });
  1196. }
  1197. }
  1198. public static void Frintm_V(ILEmitterCtx context)
  1199. {
  1200. if (Optimizations.UseSse41)
  1201. {
  1202. EmitSse41Frint(context, RoundMode.TowardsMinusInfinity, scalar: false);
  1203. }
  1204. else
  1205. {
  1206. EmitVectorUnaryOpF(context, () =>
  1207. {
  1208. EmitUnaryMathCall(context, nameof(Math.Floor));
  1209. });
  1210. }
  1211. }
  1212. public static void Frintn_S(ILEmitterCtx context)
  1213. {
  1214. if (Optimizations.UseSse41)
  1215. {
  1216. EmitSse41Frint(context, RoundMode.ToNearest, scalar: true);
  1217. }
  1218. else
  1219. {
  1220. EmitScalarUnaryOpF(context, () =>
  1221. {
  1222. EmitRoundMathCall(context, MidpointRounding.ToEven);
  1223. });
  1224. }
  1225. }
  1226. public static void Frintn_V(ILEmitterCtx context)
  1227. {
  1228. if (Optimizations.UseSse41)
  1229. {
  1230. EmitSse41Frint(context, RoundMode.ToNearest, scalar: false);
  1231. }
  1232. else
  1233. {
  1234. EmitVectorUnaryOpF(context, () =>
  1235. {
  1236. EmitRoundMathCall(context, MidpointRounding.ToEven);
  1237. });
  1238. }
  1239. }
  1240. public static void Frintp_S(ILEmitterCtx context)
  1241. {
  1242. if (Optimizations.UseSse41)
  1243. {
  1244. EmitSse41Frint(context, RoundMode.TowardsPlusInfinity, scalar: true);
  1245. }
  1246. else
  1247. {
  1248. EmitScalarUnaryOpF(context, () =>
  1249. {
  1250. EmitUnaryMathCall(context, nameof(Math.Ceiling));
  1251. });
  1252. }
  1253. }
  1254. public static void Frintp_V(ILEmitterCtx context)
  1255. {
  1256. if (Optimizations.UseSse41)
  1257. {
  1258. EmitSse41Frint(context, RoundMode.TowardsPlusInfinity, scalar: false);
  1259. }
  1260. else
  1261. {
  1262. EmitVectorUnaryOpF(context, () =>
  1263. {
  1264. EmitUnaryMathCall(context, nameof(Math.Ceiling));
  1265. });
  1266. }
  1267. }
  1268. public static void Frintx_S(ILEmitterCtx context)
  1269. {
  1270. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1271. if (Optimizations.UseSse41)
  1272. {
  1273. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1274. context.EmitLdvec(op.Rn);
  1275. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1276. if (op.Size == 0)
  1277. {
  1278. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41ScalarRoundF));
  1279. }
  1280. else /* if (op.Size == 1) */
  1281. {
  1282. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41ScalarRound));
  1283. }
  1284. context.EmitStvec(op.Rd);
  1285. }
  1286. else
  1287. {
  1288. EmitScalarUnaryOpF(context, () =>
  1289. {
  1290. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1291. if (op.Size == 0)
  1292. {
  1293. VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
  1294. }
  1295. else /* if (op.Size == 1) */
  1296. {
  1297. VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
  1298. }
  1299. });
  1300. }
  1301. }
  1302. public static void Frintx_V(ILEmitterCtx context)
  1303. {
  1304. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1305. int sizeF = op.Size & 1;
  1306. if (Optimizations.UseSse41)
  1307. {
  1308. context.EmitLdvec(op.Rn);
  1309. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1310. if (sizeF == 0)
  1311. {
  1312. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorRoundF));
  1313. }
  1314. else /* if (sizeF == 1) */
  1315. {
  1316. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorRound));
  1317. }
  1318. context.EmitStvec(op.Rd);
  1319. if (sizeF == 0 && op.RegisterSize == RegisterSize.Simd64)
  1320. {
  1321. EmitVectorZeroUpper(context, op.Rd);
  1322. }
  1323. }
  1324. else
  1325. {
  1326. EmitVectorUnaryOpF(context, () =>
  1327. {
  1328. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1329. if (sizeF == 0)
  1330. {
  1331. VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
  1332. }
  1333. else /* if (sizeF == 1) */
  1334. {
  1335. VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
  1336. }
  1337. });
  1338. }
  1339. }
  1340. public static void Frintz_S(ILEmitterCtx context)
  1341. {
  1342. if (Optimizations.UseSse41)
  1343. {
  1344. EmitSse41Frint(context, RoundMode.TowardsZero, scalar: true);
  1345. }
  1346. else
  1347. {
  1348. EmitScalarUnaryOpF(context, () =>
  1349. {
  1350. EmitUnaryMathCall(context, nameof(Math.Truncate));
  1351. });
  1352. }
  1353. }
  1354. public static void Frintz_V(ILEmitterCtx context)
  1355. {
  1356. if (Optimizations.UseSse41)
  1357. {
  1358. EmitSse41Frint(context, RoundMode.TowardsZero, scalar: false);
  1359. }
  1360. else
  1361. {
  1362. EmitVectorUnaryOpF(context, () =>
  1363. {
  1364. EmitUnaryMathCall(context, nameof(Math.Truncate));
  1365. });
  1366. }
  1367. }
  1368. public static void Frsqrte_S(ILEmitterCtx context)
  1369. {
  1370. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1371. int sizeF = op.Size & 1;
  1372. if (Optimizations.FastFP && Optimizations.UseSse && sizeF == 0)
  1373. {
  1374. EmitScalarSseOrSse2OpF(context, nameof(Sse.ReciprocalSqrtScalar));
  1375. }
  1376. else
  1377. {
  1378. EmitScalarUnaryOpF(context, () =>
  1379. {
  1380. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtEstimate));
  1381. });
  1382. }
  1383. }
  1384. public static void Frsqrte_V(ILEmitterCtx context)
  1385. {
  1386. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1387. int sizeF = op.Size & 1;
  1388. if (Optimizations.FastFP && Optimizations.UseSse && sizeF == 0)
  1389. {
  1390. EmitVectorSseOrSse2OpF(context, nameof(Sse.ReciprocalSqrt));
  1391. }
  1392. else
  1393. {
  1394. EmitVectorUnaryOpF(context, () =>
  1395. {
  1396. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtEstimate));
  1397. });
  1398. }
  1399. }
  1400. public static void Frsqrts_S(ILEmitterCtx context) // Fused.
  1401. {
  1402. if (Optimizations.FastFP && Optimizations.UseSse2)
  1403. {
  1404. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1405. int sizeF = op.Size & 1;
  1406. if (sizeF == 0)
  1407. {
  1408. Type[] typesSsv = new Type[] { typeof(float) };
  1409. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  1410. context.EmitLdc_R4(0.5f);
  1411. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  1412. context.EmitLdc_R4(3f);
  1413. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  1414. context.EmitLdvec(op.Rn);
  1415. context.EmitLdvec(op.Rm);
  1416. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulSub));
  1417. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SubtractScalar), typesMulSub));
  1418. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulSub));
  1419. context.EmitStvec(op.Rd);
  1420. EmitVectorZero32_128(context, op.Rd);
  1421. }
  1422. else /* if (sizeF == 1) */
  1423. {
  1424. Type[] typesSsv = new Type[] { typeof(double) };
  1425. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  1426. context.EmitLdc_R8(0.5d);
  1427. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  1428. context.EmitLdc_R8(3d);
  1429. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  1430. context.EmitLdvec(op.Rn);
  1431. context.EmitLdvec(op.Rm);
  1432. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulSub));
  1433. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SubtractScalar), typesMulSub));
  1434. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulSub));
  1435. context.EmitStvec(op.Rd);
  1436. EmitVectorZeroUpper(context, op.Rd);
  1437. }
  1438. }
  1439. else
  1440. {
  1441. EmitScalarBinaryOpF(context, () =>
  1442. {
  1443. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtStepFused));
  1444. });
  1445. }
  1446. }
  1447. public static void Frsqrts_V(ILEmitterCtx context) // Fused.
  1448. {
  1449. if (Optimizations.FastFP && Optimizations.UseSse2)
  1450. {
  1451. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1452. int sizeF = op.Size & 1;
  1453. if (sizeF == 0)
  1454. {
  1455. Type[] typesSav = new Type[] { typeof(float) };
  1456. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  1457. context.EmitLdc_R4(0.5f);
  1458. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  1459. context.EmitLdc_R4(3f);
  1460. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  1461. context.EmitLdvec(op.Rn);
  1462. context.EmitLdvec(op.Rm);
  1463. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  1464. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesMulSub));
  1465. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  1466. context.EmitStvec(op.Rd);
  1467. if (op.RegisterSize == RegisterSize.Simd64)
  1468. {
  1469. EmitVectorZeroUpper(context, op.Rd);
  1470. }
  1471. }
  1472. else /* if (sizeF == 1) */
  1473. {
  1474. Type[] typesSav = new Type[] { typeof(double) };
  1475. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  1476. context.EmitLdc_R8(0.5d);
  1477. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1478. context.EmitLdc_R8(3d);
  1479. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1480. context.EmitLdvec(op.Rn);
  1481. context.EmitLdvec(op.Rm);
  1482. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  1483. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  1484. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  1485. context.EmitStvec(op.Rd);
  1486. }
  1487. }
  1488. else
  1489. {
  1490. EmitVectorBinaryOpF(context, () =>
  1491. {
  1492. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtStepFused));
  1493. });
  1494. }
  1495. }
  1496. public static void Fsqrt_S(ILEmitterCtx context)
  1497. {
  1498. if (Optimizations.FastFP && Optimizations.UseSse2)
  1499. {
  1500. EmitScalarSseOrSse2OpF(context, nameof(Sse.SqrtScalar));
  1501. }
  1502. else
  1503. {
  1504. EmitScalarUnaryOpF(context, () =>
  1505. {
  1506. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSqrt));
  1507. });
  1508. }
  1509. }
  1510. public static void Fsqrt_V(ILEmitterCtx context)
  1511. {
  1512. if (Optimizations.FastFP && Optimizations.UseSse2)
  1513. {
  1514. EmitVectorSseOrSse2OpF(context, nameof(Sse.Sqrt));
  1515. }
  1516. else
  1517. {
  1518. EmitVectorUnaryOpF(context, () =>
  1519. {
  1520. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSqrt));
  1521. });
  1522. }
  1523. }
  1524. public static void Fsub_S(ILEmitterCtx context)
  1525. {
  1526. if (Optimizations.FastFP && Optimizations.UseSse2)
  1527. {
  1528. EmitScalarSseOrSse2OpF(context, nameof(Sse.SubtractScalar));
  1529. }
  1530. else
  1531. {
  1532. EmitScalarBinaryOpF(context, () =>
  1533. {
  1534. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub));
  1535. });
  1536. }
  1537. }
  1538. public static void Fsub_V(ILEmitterCtx context)
  1539. {
  1540. if (Optimizations.FastFP && Optimizations.UseSse2)
  1541. {
  1542. EmitVectorSseOrSse2OpF(context, nameof(Sse.Subtract));
  1543. }
  1544. else
  1545. {
  1546. EmitVectorBinaryOpF(context, () =>
  1547. {
  1548. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub));
  1549. });
  1550. }
  1551. }
  1552. public static void Mla_V(ILEmitterCtx context)
  1553. {
  1554. if (Optimizations.UseSse41)
  1555. {
  1556. EmitSse41Mul_AddSub(context, nameof(Sse2.Add));
  1557. }
  1558. else
  1559. {
  1560. EmitVectorTernaryOpZx(context, () =>
  1561. {
  1562. context.Emit(OpCodes.Mul);
  1563. context.Emit(OpCodes.Add);
  1564. });
  1565. }
  1566. }
  1567. public static void Mla_Ve(ILEmitterCtx context)
  1568. {
  1569. EmitVectorTernaryOpByElemZx(context, () =>
  1570. {
  1571. context.Emit(OpCodes.Mul);
  1572. context.Emit(OpCodes.Add);
  1573. });
  1574. }
  1575. public static void Mls_V(ILEmitterCtx context)
  1576. {
  1577. if (Optimizations.UseSse41)
  1578. {
  1579. EmitSse41Mul_AddSub(context, nameof(Sse2.Subtract));
  1580. }
  1581. else
  1582. {
  1583. EmitVectorTernaryOpZx(context, () =>
  1584. {
  1585. context.Emit(OpCodes.Mul);
  1586. context.Emit(OpCodes.Sub);
  1587. });
  1588. }
  1589. }
  1590. public static void Mls_Ve(ILEmitterCtx context)
  1591. {
  1592. EmitVectorTernaryOpByElemZx(context, () =>
  1593. {
  1594. context.Emit(OpCodes.Mul);
  1595. context.Emit(OpCodes.Sub);
  1596. });
  1597. }
  1598. public static void Mul_V(ILEmitterCtx context)
  1599. {
  1600. if (Optimizations.UseSse41)
  1601. {
  1602. EmitSse41Mul_AddSub(context);
  1603. }
  1604. else
  1605. {
  1606. EmitVectorBinaryOpZx(context, () => context.Emit(OpCodes.Mul));
  1607. }
  1608. }
  1609. public static void Mul_Ve(ILEmitterCtx context)
  1610. {
  1611. EmitVectorBinaryOpByElemZx(context, () => context.Emit(OpCodes.Mul));
  1612. }
  1613. public static void Neg_S(ILEmitterCtx context)
  1614. {
  1615. EmitScalarUnaryOpSx(context, () => context.Emit(OpCodes.Neg));
  1616. }
  1617. public static void Neg_V(ILEmitterCtx context)
  1618. {
  1619. if (Optimizations.UseSse2)
  1620. {
  1621. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1622. Type[] typesSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1623. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1624. context.EmitLdvec(op.Rn);
  1625. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  1626. context.EmitStvec(op.Rd);
  1627. if (op.RegisterSize == RegisterSize.Simd64)
  1628. {
  1629. EmitVectorZeroUpper(context, op.Rd);
  1630. }
  1631. }
  1632. else
  1633. {
  1634. EmitVectorUnaryOpSx(context, () => context.Emit(OpCodes.Neg));
  1635. }
  1636. }
  1637. public static void Raddhn_V(ILEmitterCtx context)
  1638. {
  1639. EmitHighNarrow(context, () => context.Emit(OpCodes.Add), round: true);
  1640. }
  1641. public static void Rsubhn_V(ILEmitterCtx context)
  1642. {
  1643. EmitHighNarrow(context, () => context.Emit(OpCodes.Sub), round: true);
  1644. }
  1645. public static void Saba_V(ILEmitterCtx context)
  1646. {
  1647. EmitVectorTernaryOpSx(context, () =>
  1648. {
  1649. context.Emit(OpCodes.Sub);
  1650. EmitAbs(context);
  1651. context.Emit(OpCodes.Add);
  1652. });
  1653. }
  1654. public static void Sabal_V(ILEmitterCtx context)
  1655. {
  1656. EmitVectorWidenRnRmTernaryOpSx(context, () =>
  1657. {
  1658. context.Emit(OpCodes.Sub);
  1659. EmitAbs(context);
  1660. context.Emit(OpCodes.Add);
  1661. });
  1662. }
  1663. public static void Sabd_V(ILEmitterCtx context)
  1664. {
  1665. if (Optimizations.UseSse2)
  1666. {
  1667. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1668. Type[] typesCmpSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1669. Type[] typesAndOr = new Type[] { typeof(Vector128<long>), typeof(Vector128<long>) };
  1670. context.EmitLdvec(op.Rn);
  1671. context.EmitLdvec(op.Rm);
  1672. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), typesCmpSub));
  1673. context.EmitStvectmp(); // Cmp mask
  1674. context.EmitLdvectmp(); // Cmp mask
  1675. context.EmitLdvec(op.Rn);
  1676. context.EmitLdvec(op.Rm);
  1677. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSub));
  1678. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndOr));
  1679. context.EmitLdvectmp(); // Cmp mask
  1680. context.EmitLdvec(op.Rm);
  1681. context.EmitLdvec(op.Rn);
  1682. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSub));
  1683. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAndOr));
  1684. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesAndOr));
  1685. context.EmitStvec(op.Rd);
  1686. if (op.RegisterSize == RegisterSize.Simd64)
  1687. {
  1688. EmitVectorZeroUpper(context, op.Rd);
  1689. }
  1690. }
  1691. else
  1692. {
  1693. EmitVectorBinaryOpSx(context, () =>
  1694. {
  1695. context.Emit(OpCodes.Sub);
  1696. EmitAbs(context);
  1697. });
  1698. }
  1699. }
  1700. public static void Sabdl_V(ILEmitterCtx context)
  1701. {
  1702. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1703. if (Optimizations.UseSse41 && op.Size < 2)
  1704. {
  1705. Type[] typesCmpSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1706. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1707. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1708. Type[] typesAndOr = new Type[] { typeof(Vector128<long>), typeof(Vector128<long>) };
  1709. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1710. string nameCvt = op.Size == 0
  1711. ? nameof(Sse41.ConvertToVector128Int16)
  1712. : nameof(Sse41.ConvertToVector128Int32);
  1713. context.EmitLdvec(op.Rn);
  1714. if (op.RegisterSize == RegisterSize.Simd128)
  1715. {
  1716. context.Emit(OpCodes.Ldc_I4_8);
  1717. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1718. }
  1719. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  1720. context.EmitLdvec(op.Rm);
  1721. if (op.RegisterSize == RegisterSize.Simd128)
  1722. {
  1723. context.Emit(OpCodes.Ldc_I4_8);
  1724. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1725. }
  1726. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  1727. context.EmitStvectmp2(); // Long Rm
  1728. context.EmitStvectmp(); // Long Rn
  1729. context.EmitLdvectmp(); // Long Rn
  1730. context.EmitLdvectmp2(); // Long Rm
  1731. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), typesCmpSub));
  1732. context.EmitStvectmp3(); // Cmp mask
  1733. context.EmitLdvectmp3(); // Cmp mask
  1734. context.EmitLdvectmp(); // Long Rn
  1735. context.EmitLdvectmp2(); // Long Rm
  1736. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSub));
  1737. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndOr));
  1738. context.EmitLdvectmp3(); // Cmp mask
  1739. context.EmitLdvectmp2(); // Long Rm
  1740. context.EmitLdvectmp(); // Long Rn
  1741. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSub));
  1742. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAndOr));
  1743. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesAndOr));
  1744. context.EmitStvec(op.Rd);
  1745. }
  1746. else
  1747. {
  1748. EmitVectorWidenRnRmBinaryOpSx(context, () =>
  1749. {
  1750. context.Emit(OpCodes.Sub);
  1751. EmitAbs(context);
  1752. });
  1753. }
  1754. }
  1755. public static void Sadalp_V(ILEmitterCtx context)
  1756. {
  1757. EmitAddLongPairwise(context, signed: true, accumulate: true);
  1758. }
  1759. public static void Saddl_V(ILEmitterCtx context)
  1760. {
  1761. if (Optimizations.UseSse41)
  1762. {
  1763. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1764. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1765. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1766. Type[] typesAdd = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1767. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1768. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  1769. nameof(Sse41.ConvertToVector128Int32),
  1770. nameof(Sse41.ConvertToVector128Int64) };
  1771. context.EmitLdvec(op.Rn);
  1772. if (op.RegisterSize == RegisterSize.Simd128)
  1773. {
  1774. context.Emit(OpCodes.Ldc_I4_8);
  1775. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1776. }
  1777. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1778. context.EmitLdvec(op.Rm);
  1779. if (op.RegisterSize == RegisterSize.Simd128)
  1780. {
  1781. context.Emit(OpCodes.Ldc_I4_8);
  1782. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1783. }
  1784. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1785. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  1786. context.EmitStvec(op.Rd);
  1787. }
  1788. else
  1789. {
  1790. EmitVectorWidenRnRmBinaryOpSx(context, () => context.Emit(OpCodes.Add));
  1791. }
  1792. }
  1793. public static void Saddlp_V(ILEmitterCtx context)
  1794. {
  1795. EmitAddLongPairwise(context, signed: true, accumulate: false);
  1796. }
  1797. public static void Saddw_V(ILEmitterCtx context)
  1798. {
  1799. if (Optimizations.UseSse41)
  1800. {
  1801. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1802. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1803. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1804. Type[] typesAdd = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1805. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1806. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  1807. nameof(Sse41.ConvertToVector128Int32),
  1808. nameof(Sse41.ConvertToVector128Int64) };
  1809. context.EmitLdvec(op.Rn);
  1810. context.EmitLdvec(op.Rm);
  1811. if (op.RegisterSize == RegisterSize.Simd128)
  1812. {
  1813. context.Emit(OpCodes.Ldc_I4_8);
  1814. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1815. }
  1816. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1817. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  1818. context.EmitStvec(op.Rd);
  1819. }
  1820. else
  1821. {
  1822. EmitVectorWidenRmBinaryOpSx(context, () => context.Emit(OpCodes.Add));
  1823. }
  1824. }
  1825. public static void Shadd_V(ILEmitterCtx context)
  1826. {
  1827. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1828. if (Optimizations.UseSse2 && op.Size > 0)
  1829. {
  1830. Type[] typesSra = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1831. Type[] typesAndXorAdd = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1832. context.EmitLdvec(op.Rn);
  1833. context.EmitLdvec(op.Rm);
  1834. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndXorAdd));
  1835. context.EmitLdvec(op.Rn);
  1836. context.EmitLdvec(op.Rm);
  1837. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesAndXorAdd));
  1838. context.Emit(OpCodes.Ldc_I4_1);
  1839. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightArithmetic), typesSra));
  1840. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAndXorAdd));
  1841. context.EmitStvec(op.Rd);
  1842. if (op.RegisterSize == RegisterSize.Simd64)
  1843. {
  1844. EmitVectorZeroUpper(context, op.Rd);
  1845. }
  1846. }
  1847. else
  1848. {
  1849. EmitVectorBinaryOpSx(context, () =>
  1850. {
  1851. context.Emit(OpCodes.Add);
  1852. context.Emit(OpCodes.Ldc_I4_1);
  1853. context.Emit(OpCodes.Shr);
  1854. });
  1855. }
  1856. }
  1857. public static void Shsub_V(ILEmitterCtx context)
  1858. {
  1859. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1860. if (Optimizations.UseSse2 && op.Size < 2)
  1861. {
  1862. Type[] typesSav = new Type[] { IntTypesPerSizeLog2[op.Size] };
  1863. Type[] typesAddSub = new Type[] { VectorIntTypesPerSizeLog2 [op.Size], VectorIntTypesPerSizeLog2 [op.Size] };
  1864. Type[] typesAvg = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  1865. context.EmitLdc_I4(op.Size == 0 ? sbyte.MinValue : short.MinValue);
  1866. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1867. context.EmitStvectmp();
  1868. context.EmitLdvec(op.Rn);
  1869. context.EmitLdvectmp();
  1870. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAddSub));
  1871. context.Emit(OpCodes.Dup);
  1872. context.EmitLdvec(op.Rm);
  1873. context.EmitLdvectmp();
  1874. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAddSub));
  1875. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Average), typesAvg));
  1876. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesAddSub));
  1877. context.EmitStvec(op.Rd);
  1878. if (op.RegisterSize == RegisterSize.Simd64)
  1879. {
  1880. EmitVectorZeroUpper(context, op.Rd);
  1881. }
  1882. }
  1883. else
  1884. {
  1885. EmitVectorBinaryOpSx(context, () =>
  1886. {
  1887. context.Emit(OpCodes.Sub);
  1888. context.Emit(OpCodes.Ldc_I4_1);
  1889. context.Emit(OpCodes.Shr);
  1890. });
  1891. }
  1892. }
  1893. public static void Smax_V(ILEmitterCtx context)
  1894. {
  1895. if (Optimizations.UseSse41)
  1896. {
  1897. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1898. Type[] typesMax = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1899. Type typeSse = op.Size == 1 ? typeof(Sse2) : typeof(Sse41);
  1900. context.EmitLdvec(op.Rn);
  1901. context.EmitLdvec(op.Rm);
  1902. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Max), typesMax));
  1903. context.EmitStvec(op.Rd);
  1904. if (op.RegisterSize == RegisterSize.Simd64)
  1905. {
  1906. EmitVectorZeroUpper(context, op.Rd);
  1907. }
  1908. }
  1909. else
  1910. {
  1911. Type[] types = new Type[] { typeof(long), typeof(long) };
  1912. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Max), types);
  1913. EmitVectorBinaryOpSx(context, () => context.EmitCall(mthdInfo));
  1914. }
  1915. }
  1916. public static void Smaxp_V(ILEmitterCtx context)
  1917. {
  1918. Type[] types = new Type[] { typeof(long), typeof(long) };
  1919. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Max), types);
  1920. EmitVectorPairwiseOpSx(context, () => context.EmitCall(mthdInfo));
  1921. }
  1922. public static void Smin_V(ILEmitterCtx context)
  1923. {
  1924. if (Optimizations.UseSse41)
  1925. {
  1926. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1927. Type[] typesMin = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1928. Type typeSse = op.Size == 1 ? typeof(Sse2) : typeof(Sse41);
  1929. context.EmitLdvec(op.Rn);
  1930. context.EmitLdvec(op.Rm);
  1931. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Min), typesMin));
  1932. context.EmitStvec(op.Rd);
  1933. if (op.RegisterSize == RegisterSize.Simd64)
  1934. {
  1935. EmitVectorZeroUpper(context, op.Rd);
  1936. }
  1937. }
  1938. else
  1939. {
  1940. Type[] types = new Type[] { typeof(long), typeof(long) };
  1941. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Min), types);
  1942. EmitVectorBinaryOpSx(context, () => context.EmitCall(mthdInfo));
  1943. }
  1944. }
  1945. public static void Sminp_V(ILEmitterCtx context)
  1946. {
  1947. Type[] types = new Type[] { typeof(long), typeof(long) };
  1948. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Min), types);
  1949. EmitVectorPairwiseOpSx(context, () => context.EmitCall(mthdInfo));
  1950. }
  1951. public static void Smlal_V(ILEmitterCtx context)
  1952. {
  1953. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1954. if (Optimizations.UseSse41 && op.Size < 2)
  1955. {
  1956. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1957. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1958. Type[] typesMulAdd = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1959. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1960. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  1961. string nameCvt = op.Size == 0
  1962. ? nameof(Sse41.ConvertToVector128Int16)
  1963. : nameof(Sse41.ConvertToVector128Int32);
  1964. context.EmitLdvec(op.Rd);
  1965. context.EmitLdvec(op.Rn);
  1966. if (op.RegisterSize == RegisterSize.Simd128)
  1967. {
  1968. context.Emit(OpCodes.Ldc_I4_8);
  1969. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1970. }
  1971. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  1972. context.EmitLdvec(op.Rm);
  1973. if (op.RegisterSize == RegisterSize.Simd128)
  1974. {
  1975. context.Emit(OpCodes.Ldc_I4_8);
  1976. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1977. }
  1978. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  1979. context.EmitCall(typeSse.GetMethod(nameof(Sse2.MultiplyLow), typesMulAdd));
  1980. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesMulAdd));
  1981. context.EmitStvec(op.Rd);
  1982. }
  1983. else
  1984. {
  1985. EmitVectorWidenRnRmTernaryOpSx(context, () =>
  1986. {
  1987. context.Emit(OpCodes.Mul);
  1988. context.Emit(OpCodes.Add);
  1989. });
  1990. }
  1991. }
  1992. public static void Smlal_Ve(ILEmitterCtx context)
  1993. {
  1994. EmitVectorWidenTernaryOpByElemSx(context, () =>
  1995. {
  1996. context.Emit(OpCodes.Mul);
  1997. context.Emit(OpCodes.Add);
  1998. });
  1999. }
  2000. public static void Smlsl_V(ILEmitterCtx context)
  2001. {
  2002. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2003. if (Optimizations.UseSse41 && op.Size < 2)
  2004. {
  2005. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2006. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  2007. Type[] typesMulSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  2008. VectorIntTypesPerSizeLog2[op.Size + 1] };
  2009. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2010. string nameCvt = op.Size == 0
  2011. ? nameof(Sse41.ConvertToVector128Int16)
  2012. : nameof(Sse41.ConvertToVector128Int32);
  2013. context.EmitLdvec(op.Rd);
  2014. context.EmitLdvec(op.Rn);
  2015. if (op.RegisterSize == RegisterSize.Simd128)
  2016. {
  2017. context.Emit(OpCodes.Ldc_I4_8);
  2018. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2019. }
  2020. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2021. context.EmitLdvec(op.Rm);
  2022. if (op.RegisterSize == RegisterSize.Simd128)
  2023. {
  2024. context.Emit(OpCodes.Ldc_I4_8);
  2025. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2026. }
  2027. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2028. context.EmitCall(typeSse.GetMethod(nameof(Sse2.MultiplyLow), typesMulSub));
  2029. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  2030. context.EmitStvec(op.Rd);
  2031. }
  2032. else
  2033. {
  2034. EmitVectorWidenRnRmTernaryOpSx(context, () =>
  2035. {
  2036. context.Emit(OpCodes.Mul);
  2037. context.Emit(OpCodes.Sub);
  2038. });
  2039. }
  2040. }
  2041. public static void Smlsl_Ve(ILEmitterCtx context)
  2042. {
  2043. EmitVectorWidenTernaryOpByElemSx(context, () =>
  2044. {
  2045. context.Emit(OpCodes.Mul);
  2046. context.Emit(OpCodes.Sub);
  2047. });
  2048. }
  2049. public static void Smull_V(ILEmitterCtx context)
  2050. {
  2051. EmitVectorWidenRnRmBinaryOpSx(context, () => context.Emit(OpCodes.Mul));
  2052. }
  2053. public static void Smull_Ve(ILEmitterCtx context)
  2054. {
  2055. EmitVectorWidenBinaryOpByElemSx(context, () => context.Emit(OpCodes.Mul));
  2056. }
  2057. public static void Sqabs_S(ILEmitterCtx context)
  2058. {
  2059. EmitScalarSaturatingUnaryOpSx(context, () => EmitAbs(context));
  2060. }
  2061. public static void Sqabs_V(ILEmitterCtx context)
  2062. {
  2063. EmitVectorSaturatingUnaryOpSx(context, () => EmitAbs(context));
  2064. }
  2065. public static void Sqadd_S(ILEmitterCtx context)
  2066. {
  2067. EmitScalarSaturatingBinaryOpSx(context, SaturatingFlags.Add);
  2068. }
  2069. public static void Sqadd_V(ILEmitterCtx context)
  2070. {
  2071. EmitVectorSaturatingBinaryOpSx(context, SaturatingFlags.Add);
  2072. }
  2073. public static void Sqdmulh_S(ILEmitterCtx context)
  2074. {
  2075. EmitSaturatingBinaryOp(context, () => EmitDoublingMultiplyHighHalf(context, round: false), SaturatingFlags.ScalarSx);
  2076. }
  2077. public static void Sqdmulh_V(ILEmitterCtx context)
  2078. {
  2079. EmitSaturatingBinaryOp(context, () => EmitDoublingMultiplyHighHalf(context, round: false), SaturatingFlags.VectorSx);
  2080. }
  2081. public static void Sqneg_S(ILEmitterCtx context)
  2082. {
  2083. EmitScalarSaturatingUnaryOpSx(context, () => context.Emit(OpCodes.Neg));
  2084. }
  2085. public static void Sqneg_V(ILEmitterCtx context)
  2086. {
  2087. EmitVectorSaturatingUnaryOpSx(context, () => context.Emit(OpCodes.Neg));
  2088. }
  2089. public static void Sqrdmulh_S(ILEmitterCtx context)
  2090. {
  2091. EmitSaturatingBinaryOp(context, () => EmitDoublingMultiplyHighHalf(context, round: true), SaturatingFlags.ScalarSx);
  2092. }
  2093. public static void Sqrdmulh_V(ILEmitterCtx context)
  2094. {
  2095. EmitSaturatingBinaryOp(context, () => EmitDoublingMultiplyHighHalf(context, round: true), SaturatingFlags.VectorSx);
  2096. }
  2097. public static void Sqsub_S(ILEmitterCtx context)
  2098. {
  2099. EmitScalarSaturatingBinaryOpSx(context, SaturatingFlags.Sub);
  2100. }
  2101. public static void Sqsub_V(ILEmitterCtx context)
  2102. {
  2103. EmitVectorSaturatingBinaryOpSx(context, SaturatingFlags.Sub);
  2104. }
  2105. public static void Sqxtn_S(ILEmitterCtx context)
  2106. {
  2107. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.ScalarSxSx);
  2108. }
  2109. public static void Sqxtn_V(ILEmitterCtx context)
  2110. {
  2111. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.VectorSxSx);
  2112. }
  2113. public static void Sqxtun_S(ILEmitterCtx context)
  2114. {
  2115. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.ScalarSxZx);
  2116. }
  2117. public static void Sqxtun_V(ILEmitterCtx context)
  2118. {
  2119. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.VectorSxZx);
  2120. }
  2121. public static void Srhadd_V(ILEmitterCtx context)
  2122. {
  2123. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2124. if (Optimizations.UseSse2 && op.Size < 2)
  2125. {
  2126. Type[] typesSav = new Type[] { IntTypesPerSizeLog2[op.Size] };
  2127. Type[] typesSubAdd = new Type[] { VectorIntTypesPerSizeLog2 [op.Size], VectorIntTypesPerSizeLog2 [op.Size] };
  2128. Type[] typesAvg = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2129. context.EmitLdc_I4(op.Size == 0 ? sbyte.MinValue : short.MinValue);
  2130. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  2131. context.EmitStvectmp();
  2132. context.EmitLdvectmp();
  2133. context.EmitLdvec(op.Rn);
  2134. context.EmitLdvectmp();
  2135. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSubAdd));
  2136. context.EmitLdvec(op.Rm);
  2137. context.EmitLdvectmp();
  2138. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSubAdd));
  2139. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Average), typesAvg));
  2140. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesSubAdd));
  2141. context.EmitStvec(op.Rd);
  2142. if (op.RegisterSize == RegisterSize.Simd64)
  2143. {
  2144. EmitVectorZeroUpper(context, op.Rd);
  2145. }
  2146. }
  2147. else
  2148. {
  2149. EmitVectorBinaryOpSx(context, () =>
  2150. {
  2151. context.Emit(OpCodes.Add);
  2152. context.Emit(OpCodes.Ldc_I4_1);
  2153. context.Emit(OpCodes.Add);
  2154. context.Emit(OpCodes.Ldc_I4_1);
  2155. context.Emit(OpCodes.Shr);
  2156. });
  2157. }
  2158. }
  2159. public static void Ssubl_V(ILEmitterCtx context)
  2160. {
  2161. if (Optimizations.UseSse41)
  2162. {
  2163. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2164. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2165. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  2166. Type[] typesSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  2167. VectorIntTypesPerSizeLog2[op.Size + 1] };
  2168. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2169. nameof(Sse41.ConvertToVector128Int32),
  2170. nameof(Sse41.ConvertToVector128Int64) };
  2171. context.EmitLdvec(op.Rn);
  2172. if (op.RegisterSize == RegisterSize.Simd128)
  2173. {
  2174. context.Emit(OpCodes.Ldc_I4_8);
  2175. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2176. }
  2177. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2178. context.EmitLdvec(op.Rm);
  2179. if (op.RegisterSize == RegisterSize.Simd128)
  2180. {
  2181. context.Emit(OpCodes.Ldc_I4_8);
  2182. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2183. }
  2184. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2185. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  2186. context.EmitStvec(op.Rd);
  2187. }
  2188. else
  2189. {
  2190. EmitVectorWidenRnRmBinaryOpSx(context, () => context.Emit(OpCodes.Sub));
  2191. }
  2192. }
  2193. public static void Ssubw_V(ILEmitterCtx context)
  2194. {
  2195. if (Optimizations.UseSse41)
  2196. {
  2197. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2198. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2199. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  2200. Type[] typesSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  2201. VectorIntTypesPerSizeLog2[op.Size + 1] };
  2202. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2203. nameof(Sse41.ConvertToVector128Int32),
  2204. nameof(Sse41.ConvertToVector128Int64) };
  2205. context.EmitLdvec(op.Rn);
  2206. context.EmitLdvec(op.Rm);
  2207. if (op.RegisterSize == RegisterSize.Simd128)
  2208. {
  2209. context.Emit(OpCodes.Ldc_I4_8);
  2210. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2211. }
  2212. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2213. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  2214. context.EmitStvec(op.Rd);
  2215. }
  2216. else
  2217. {
  2218. EmitVectorWidenRmBinaryOpSx(context, () => context.Emit(OpCodes.Sub));
  2219. }
  2220. }
  2221. public static void Sub_S(ILEmitterCtx context)
  2222. {
  2223. EmitScalarBinaryOpZx(context, () => context.Emit(OpCodes.Sub));
  2224. }
  2225. public static void Sub_V(ILEmitterCtx context)
  2226. {
  2227. if (Optimizations.UseSse2)
  2228. {
  2229. EmitSse2Op(context, nameof(Sse2.Subtract));
  2230. }
  2231. else
  2232. {
  2233. EmitVectorBinaryOpZx(context, () => context.Emit(OpCodes.Sub));
  2234. }
  2235. }
  2236. public static void Subhn_V(ILEmitterCtx context)
  2237. {
  2238. EmitHighNarrow(context, () => context.Emit(OpCodes.Sub), round: false);
  2239. }
  2240. public static void Suqadd_S(ILEmitterCtx context)
  2241. {
  2242. EmitScalarSaturatingBinaryOpSx(context, SaturatingFlags.Accumulate);
  2243. }
  2244. public static void Suqadd_V(ILEmitterCtx context)
  2245. {
  2246. EmitVectorSaturatingBinaryOpSx(context, SaturatingFlags.Accumulate);
  2247. }
  2248. public static void Uaba_V(ILEmitterCtx context)
  2249. {
  2250. EmitVectorTernaryOpZx(context, () =>
  2251. {
  2252. context.Emit(OpCodes.Sub);
  2253. EmitAbs(context);
  2254. context.Emit(OpCodes.Add);
  2255. });
  2256. }
  2257. public static void Uabal_V(ILEmitterCtx context)
  2258. {
  2259. EmitVectorWidenRnRmTernaryOpZx(context, () =>
  2260. {
  2261. context.Emit(OpCodes.Sub);
  2262. EmitAbs(context);
  2263. context.Emit(OpCodes.Add);
  2264. });
  2265. }
  2266. public static void Uabd_V(ILEmitterCtx context)
  2267. {
  2268. if (Optimizations.UseSse41)
  2269. {
  2270. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2271. Type[] typesMax = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2272. Type[] typesCmpSub = new Type[] { VectorIntTypesPerSizeLog2 [op.Size], VectorIntTypesPerSizeLog2 [op.Size] };
  2273. Type[] typesAndOr = new Type[] { typeof(Vector128<long>), typeof(Vector128<long>) };
  2274. Type[] typesSav = new Type[] { typeof(long) };
  2275. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2276. context.EmitLdvec(op.Rm);
  2277. context.EmitLdvec(op.Rn);
  2278. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Max), typesMax));
  2279. context.EmitLdvec(op.Rm);
  2280. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareEqual), typesCmpSub));
  2281. context.EmitLdc_I8(-1L);
  2282. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  2283. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAndOr));
  2284. context.EmitStvectmp(); // Cmp mask
  2285. context.EmitLdvectmp(); // Cmp mask
  2286. context.EmitLdvec(op.Rn);
  2287. context.EmitLdvec(op.Rm);
  2288. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSub));
  2289. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndOr));
  2290. context.EmitLdvectmp(); // Cmp mask
  2291. context.EmitLdvec(op.Rm);
  2292. context.EmitLdvec(op.Rn);
  2293. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSub));
  2294. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAndOr));
  2295. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesAndOr));
  2296. context.EmitStvec(op.Rd);
  2297. if (op.RegisterSize == RegisterSize.Simd64)
  2298. {
  2299. EmitVectorZeroUpper(context, op.Rd);
  2300. }
  2301. }
  2302. else
  2303. {
  2304. EmitVectorBinaryOpZx(context, () =>
  2305. {
  2306. context.Emit(OpCodes.Sub);
  2307. EmitAbs(context);
  2308. });
  2309. }
  2310. }
  2311. public static void Uabdl_V(ILEmitterCtx context)
  2312. {
  2313. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2314. if (Optimizations.UseSse41 && op.Size < 2)
  2315. {
  2316. Type[] typesMax = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2317. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2318. Type[] typesCmpSub = new Type[] { VectorIntTypesPerSizeLog2 [op.Size + 1],
  2319. VectorIntTypesPerSizeLog2 [op.Size + 1] };
  2320. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2321. Type[] typesAndOr = new Type[] { typeof(Vector128<long>), typeof(Vector128<long>) };
  2322. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2323. Type[] typesSav = new Type[] { typeof(long) };
  2324. string nameCvt = op.Size == 0
  2325. ? nameof(Sse41.ConvertToVector128Int16)
  2326. : nameof(Sse41.ConvertToVector128Int32);
  2327. context.EmitLdvec(op.Rn);
  2328. if (op.RegisterSize == RegisterSize.Simd128)
  2329. {
  2330. context.Emit(OpCodes.Ldc_I4_8);
  2331. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2332. }
  2333. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2334. context.EmitLdvec(op.Rm);
  2335. if (op.RegisterSize == RegisterSize.Simd128)
  2336. {
  2337. context.Emit(OpCodes.Ldc_I4_8);
  2338. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2339. }
  2340. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2341. context.EmitStvectmp2(); // Long Rm
  2342. context.EmitStvectmp(); // Long Rn
  2343. context.EmitLdvectmp2(); // Long Rm
  2344. context.EmitLdvectmp(); // Long Rn
  2345. context.EmitCall(typeof(Sse41).GetMethod(nameof(Sse41.Max), typesMax));
  2346. context.EmitLdvectmp2(); // Long Rm
  2347. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareEqual), typesCmpSub));
  2348. context.EmitLdc_I8(-1L);
  2349. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  2350. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAndOr));
  2351. context.EmitStvectmp3(); // Cmp mask
  2352. context.EmitLdvectmp3(); // Cmp mask
  2353. context.EmitLdvectmp(); // Long Rn
  2354. context.EmitLdvectmp2(); // Long Rm
  2355. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSub));
  2356. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndOr));
  2357. context.EmitLdvectmp3(); // Cmp mask
  2358. context.EmitLdvectmp2(); // Long Rm
  2359. context.EmitLdvectmp(); // Long Rn
  2360. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSub));
  2361. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAndOr));
  2362. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesAndOr));
  2363. context.EmitStvec(op.Rd);
  2364. }
  2365. else
  2366. {
  2367. EmitVectorWidenRnRmBinaryOpZx(context, () =>
  2368. {
  2369. context.Emit(OpCodes.Sub);
  2370. EmitAbs(context);
  2371. });
  2372. }
  2373. }
  2374. public static void Uadalp_V(ILEmitterCtx context)
  2375. {
  2376. EmitAddLongPairwise(context, signed: false, accumulate: true);
  2377. }
  2378. public static void Uaddl_V(ILEmitterCtx context)
  2379. {
  2380. if (Optimizations.UseSse41)
  2381. {
  2382. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2383. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2384. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2385. Type[] typesAdd = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2386. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2387. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2388. nameof(Sse41.ConvertToVector128Int32),
  2389. nameof(Sse41.ConvertToVector128Int64) };
  2390. context.EmitLdvec(op.Rn);
  2391. if (op.RegisterSize == RegisterSize.Simd128)
  2392. {
  2393. context.Emit(OpCodes.Ldc_I4_8);
  2394. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2395. }
  2396. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2397. context.EmitLdvec(op.Rm);
  2398. if (op.RegisterSize == RegisterSize.Simd128)
  2399. {
  2400. context.Emit(OpCodes.Ldc_I4_8);
  2401. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2402. }
  2403. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2404. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  2405. context.EmitStvec(op.Rd);
  2406. }
  2407. else
  2408. {
  2409. EmitVectorWidenRnRmBinaryOpZx(context, () => context.Emit(OpCodes.Add));
  2410. }
  2411. }
  2412. public static void Uaddlp_V(ILEmitterCtx context)
  2413. {
  2414. EmitAddLongPairwise(context, signed: false, accumulate: false);
  2415. }
  2416. public static void Uaddlv_V(ILEmitterCtx context)
  2417. {
  2418. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  2419. int bytes = op.GetBitsCount() >> 3;
  2420. int elems = bytes >> op.Size;
  2421. EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  2422. for (int index = 1; index < elems; index++)
  2423. {
  2424. EmitVectorExtractZx(context, op.Rn, index, op.Size);
  2425. context.Emit(OpCodes.Add);
  2426. }
  2427. EmitScalarSet(context, op.Rd, op.Size + 1);
  2428. }
  2429. public static void Uaddw_V(ILEmitterCtx context)
  2430. {
  2431. if (Optimizations.UseSse41)
  2432. {
  2433. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2434. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2435. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2436. Type[] typesAdd = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2437. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2438. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2439. nameof(Sse41.ConvertToVector128Int32),
  2440. nameof(Sse41.ConvertToVector128Int64) };
  2441. context.EmitLdvec(op.Rn);
  2442. context.EmitLdvec(op.Rm);
  2443. if (op.RegisterSize == RegisterSize.Simd128)
  2444. {
  2445. context.Emit(OpCodes.Ldc_I4_8);
  2446. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2447. }
  2448. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2449. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  2450. context.EmitStvec(op.Rd);
  2451. }
  2452. else
  2453. {
  2454. EmitVectorWidenRmBinaryOpZx(context, () => context.Emit(OpCodes.Add));
  2455. }
  2456. }
  2457. public static void Uhadd_V(ILEmitterCtx context)
  2458. {
  2459. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2460. if (Optimizations.UseSse2 && op.Size > 0)
  2461. {
  2462. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2463. Type[] typesAndXorAdd = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2464. context.EmitLdvec(op.Rn);
  2465. context.EmitLdvec(op.Rm);
  2466. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndXorAdd));
  2467. context.EmitLdvec(op.Rn);
  2468. context.EmitLdvec(op.Rm);
  2469. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesAndXorAdd));
  2470. context.Emit(OpCodes.Ldc_I4_1);
  2471. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical), typesSrl));
  2472. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAndXorAdd));
  2473. context.EmitStvec(op.Rd);
  2474. if (op.RegisterSize == RegisterSize.Simd64)
  2475. {
  2476. EmitVectorZeroUpper(context, op.Rd);
  2477. }
  2478. }
  2479. else
  2480. {
  2481. EmitVectorBinaryOpZx(context, () =>
  2482. {
  2483. context.Emit(OpCodes.Add);
  2484. context.Emit(OpCodes.Ldc_I4_1);
  2485. context.Emit(OpCodes.Shr_Un);
  2486. });
  2487. }
  2488. }
  2489. public static void Uhsub_V(ILEmitterCtx context)
  2490. {
  2491. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2492. if (Optimizations.UseSse2 && op.Size < 2)
  2493. {
  2494. Type[] typesAvgSub = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2495. context.EmitLdvec(op.Rn);
  2496. context.EmitLdvec(op.Rn);
  2497. context.EmitLdvec(op.Rm);
  2498. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Average), typesAvgSub));
  2499. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesAvgSub));
  2500. context.EmitStvec(op.Rd);
  2501. if (op.RegisterSize == RegisterSize.Simd64)
  2502. {
  2503. EmitVectorZeroUpper(context, op.Rd);
  2504. }
  2505. }
  2506. else
  2507. {
  2508. EmitVectorBinaryOpZx(context, () =>
  2509. {
  2510. context.Emit(OpCodes.Sub);
  2511. context.Emit(OpCodes.Ldc_I4_1);
  2512. context.Emit(OpCodes.Shr_Un);
  2513. });
  2514. }
  2515. }
  2516. public static void Umax_V(ILEmitterCtx context)
  2517. {
  2518. if (Optimizations.UseSse41)
  2519. {
  2520. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2521. Type[] typesMax = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2522. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2523. context.EmitLdvec(op.Rn);
  2524. context.EmitLdvec(op.Rm);
  2525. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Max), typesMax));
  2526. context.EmitStvec(op.Rd);
  2527. if (op.RegisterSize == RegisterSize.Simd64)
  2528. {
  2529. EmitVectorZeroUpper(context, op.Rd);
  2530. }
  2531. }
  2532. else
  2533. {
  2534. Type[] types = new Type[] { typeof(ulong), typeof(ulong) };
  2535. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Max), types);
  2536. EmitVectorBinaryOpZx(context, () => context.EmitCall(mthdInfo));
  2537. }
  2538. }
  2539. public static void Umaxp_V(ILEmitterCtx context)
  2540. {
  2541. Type[] types = new Type[] { typeof(ulong), typeof(ulong) };
  2542. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Max), types);
  2543. EmitVectorPairwiseOpZx(context, () => context.EmitCall(mthdInfo));
  2544. }
  2545. public static void Umin_V(ILEmitterCtx context)
  2546. {
  2547. if (Optimizations.UseSse41)
  2548. {
  2549. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2550. Type[] typesMin = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2551. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2552. context.EmitLdvec(op.Rn);
  2553. context.EmitLdvec(op.Rm);
  2554. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Min), typesMin));
  2555. context.EmitStvec(op.Rd);
  2556. if (op.RegisterSize == RegisterSize.Simd64)
  2557. {
  2558. EmitVectorZeroUpper(context, op.Rd);
  2559. }
  2560. }
  2561. else
  2562. {
  2563. Type[] types = new Type[] { typeof(ulong), typeof(ulong) };
  2564. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Min), types);
  2565. EmitVectorBinaryOpZx(context, () => context.EmitCall(mthdInfo));
  2566. }
  2567. }
  2568. public static void Uminp_V(ILEmitterCtx context)
  2569. {
  2570. Type[] types = new Type[] { typeof(ulong), typeof(ulong) };
  2571. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Min), types);
  2572. EmitVectorPairwiseOpZx(context, () => context.EmitCall(mthdInfo));
  2573. }
  2574. public static void Umlal_V(ILEmitterCtx context)
  2575. {
  2576. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2577. if (Optimizations.UseSse41 && op.Size < 2)
  2578. {
  2579. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2580. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2581. Type[] typesMulAdd = new Type[] { VectorIntTypesPerSizeLog2 [op.Size + 1],
  2582. VectorIntTypesPerSizeLog2 [op.Size + 1] };
  2583. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2584. string nameCvt = op.Size == 0
  2585. ? nameof(Sse41.ConvertToVector128Int16)
  2586. : nameof(Sse41.ConvertToVector128Int32);
  2587. context.EmitLdvec(op.Rd);
  2588. context.EmitLdvec(op.Rn);
  2589. if (op.RegisterSize == RegisterSize.Simd128)
  2590. {
  2591. context.Emit(OpCodes.Ldc_I4_8);
  2592. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2593. }
  2594. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2595. context.EmitLdvec(op.Rm);
  2596. if (op.RegisterSize == RegisterSize.Simd128)
  2597. {
  2598. context.Emit(OpCodes.Ldc_I4_8);
  2599. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2600. }
  2601. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2602. context.EmitCall(typeSse.GetMethod(nameof(Sse2.MultiplyLow), typesMulAdd));
  2603. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesMulAdd));
  2604. context.EmitStvec(op.Rd);
  2605. }
  2606. else
  2607. {
  2608. EmitVectorWidenRnRmTernaryOpZx(context, () =>
  2609. {
  2610. context.Emit(OpCodes.Mul);
  2611. context.Emit(OpCodes.Add);
  2612. });
  2613. }
  2614. }
  2615. public static void Umlal_Ve(ILEmitterCtx context)
  2616. {
  2617. EmitVectorWidenTernaryOpByElemZx(context, () =>
  2618. {
  2619. context.Emit(OpCodes.Mul);
  2620. context.Emit(OpCodes.Add);
  2621. });
  2622. }
  2623. public static void Umlsl_V(ILEmitterCtx context)
  2624. {
  2625. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2626. if (Optimizations.UseSse41 && op.Size < 2)
  2627. {
  2628. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2629. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2630. Type[] typesMulSub = new Type[] { VectorIntTypesPerSizeLog2 [op.Size + 1],
  2631. VectorIntTypesPerSizeLog2 [op.Size + 1] };
  2632. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2633. string nameCvt = op.Size == 0
  2634. ? nameof(Sse41.ConvertToVector128Int16)
  2635. : nameof(Sse41.ConvertToVector128Int32);
  2636. context.EmitLdvec(op.Rd);
  2637. context.EmitLdvec(op.Rn);
  2638. if (op.RegisterSize == RegisterSize.Simd128)
  2639. {
  2640. context.Emit(OpCodes.Ldc_I4_8);
  2641. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2642. }
  2643. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2644. context.EmitLdvec(op.Rm);
  2645. if (op.RegisterSize == RegisterSize.Simd128)
  2646. {
  2647. context.Emit(OpCodes.Ldc_I4_8);
  2648. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2649. }
  2650. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2651. context.EmitCall(typeSse.GetMethod(nameof(Sse2.MultiplyLow), typesMulSub));
  2652. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  2653. context.EmitStvec(op.Rd);
  2654. }
  2655. else
  2656. {
  2657. EmitVectorWidenRnRmTernaryOpZx(context, () =>
  2658. {
  2659. context.Emit(OpCodes.Mul);
  2660. context.Emit(OpCodes.Sub);
  2661. });
  2662. }
  2663. }
  2664. public static void Umlsl_Ve(ILEmitterCtx context)
  2665. {
  2666. EmitVectorWidenTernaryOpByElemZx(context, () =>
  2667. {
  2668. context.Emit(OpCodes.Mul);
  2669. context.Emit(OpCodes.Sub);
  2670. });
  2671. }
  2672. public static void Umull_V(ILEmitterCtx context)
  2673. {
  2674. EmitVectorWidenRnRmBinaryOpZx(context, () => context.Emit(OpCodes.Mul));
  2675. }
  2676. public static void Umull_Ve(ILEmitterCtx context)
  2677. {
  2678. EmitVectorWidenBinaryOpByElemZx(context, () => context.Emit(OpCodes.Mul));
  2679. }
  2680. public static void Uqadd_S(ILEmitterCtx context)
  2681. {
  2682. EmitScalarSaturatingBinaryOpZx(context, SaturatingFlags.Add);
  2683. }
  2684. public static void Uqadd_V(ILEmitterCtx context)
  2685. {
  2686. EmitVectorSaturatingBinaryOpZx(context, SaturatingFlags.Add);
  2687. }
  2688. public static void Uqsub_S(ILEmitterCtx context)
  2689. {
  2690. EmitScalarSaturatingBinaryOpZx(context, SaturatingFlags.Sub);
  2691. }
  2692. public static void Uqsub_V(ILEmitterCtx context)
  2693. {
  2694. EmitVectorSaturatingBinaryOpZx(context, SaturatingFlags.Sub);
  2695. }
  2696. public static void Uqxtn_S(ILEmitterCtx context)
  2697. {
  2698. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.ScalarZxZx);
  2699. }
  2700. public static void Uqxtn_V(ILEmitterCtx context)
  2701. {
  2702. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.VectorZxZx);
  2703. }
  2704. public static void Urhadd_V(ILEmitterCtx context)
  2705. {
  2706. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2707. if (Optimizations.UseSse2 && op.Size < 2)
  2708. {
  2709. Type[] typesAvg = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2710. context.EmitLdvec(op.Rn);
  2711. context.EmitLdvec(op.Rm);
  2712. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Average), typesAvg));
  2713. context.EmitStvec(op.Rd);
  2714. if (op.RegisterSize == RegisterSize.Simd64)
  2715. {
  2716. EmitVectorZeroUpper(context, op.Rd);
  2717. }
  2718. }
  2719. else
  2720. {
  2721. EmitVectorBinaryOpZx(context, () =>
  2722. {
  2723. context.Emit(OpCodes.Add);
  2724. context.Emit(OpCodes.Ldc_I4_1);
  2725. context.Emit(OpCodes.Add);
  2726. context.Emit(OpCodes.Ldc_I4_1);
  2727. context.Emit(OpCodes.Shr_Un);
  2728. });
  2729. }
  2730. }
  2731. public static void Usqadd_S(ILEmitterCtx context)
  2732. {
  2733. EmitScalarSaturatingBinaryOpZx(context, SaturatingFlags.Accumulate);
  2734. }
  2735. public static void Usqadd_V(ILEmitterCtx context)
  2736. {
  2737. EmitVectorSaturatingBinaryOpZx(context, SaturatingFlags.Accumulate);
  2738. }
  2739. public static void Usubl_V(ILEmitterCtx context)
  2740. {
  2741. if (Optimizations.UseSse41)
  2742. {
  2743. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2744. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2745. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2746. Type[] typesSub = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2747. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2748. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2749. nameof(Sse41.ConvertToVector128Int32),
  2750. nameof(Sse41.ConvertToVector128Int64) };
  2751. context.EmitLdvec(op.Rn);
  2752. if (op.RegisterSize == RegisterSize.Simd128)
  2753. {
  2754. context.Emit(OpCodes.Ldc_I4_8);
  2755. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2756. }
  2757. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2758. context.EmitLdvec(op.Rm);
  2759. if (op.RegisterSize == RegisterSize.Simd128)
  2760. {
  2761. context.Emit(OpCodes.Ldc_I4_8);
  2762. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2763. }
  2764. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2765. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  2766. context.EmitStvec(op.Rd);
  2767. }
  2768. else
  2769. {
  2770. EmitVectorWidenRnRmBinaryOpZx(context, () => context.Emit(OpCodes.Sub));
  2771. }
  2772. }
  2773. public static void Usubw_V(ILEmitterCtx context)
  2774. {
  2775. if (Optimizations.UseSse41)
  2776. {
  2777. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2778. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2779. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2780. Type[] typesSub = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2781. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2782. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2783. nameof(Sse41.ConvertToVector128Int32),
  2784. nameof(Sse41.ConvertToVector128Int64) };
  2785. context.EmitLdvec(op.Rn);
  2786. context.EmitLdvec(op.Rm);
  2787. if (op.RegisterSize == RegisterSize.Simd128)
  2788. {
  2789. context.Emit(OpCodes.Ldc_I4_8);
  2790. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2791. }
  2792. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2793. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  2794. context.EmitStvec(op.Rd);
  2795. }
  2796. else
  2797. {
  2798. EmitVectorWidenRmBinaryOpZx(context, () => context.Emit(OpCodes.Sub));
  2799. }
  2800. }
  2801. private static void EmitAbs(ILEmitterCtx context)
  2802. {
  2803. ILLabel lblTrue = new ILLabel();
  2804. context.Emit(OpCodes.Dup);
  2805. context.Emit(OpCodes.Ldc_I4_0);
  2806. context.Emit(OpCodes.Bge_S, lblTrue);
  2807. context.Emit(OpCodes.Neg);
  2808. context.MarkLabel(lblTrue);
  2809. }
  2810. private static void EmitAddLongPairwise(ILEmitterCtx context, bool signed, bool accumulate)
  2811. {
  2812. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  2813. int words = op.GetBitsCount() >> 4;
  2814. int pairs = words >> op.Size;
  2815. for (int index = 0; index < pairs; index++)
  2816. {
  2817. int idx = index << 1;
  2818. EmitVectorExtract(context, op.Rn, idx, op.Size, signed);
  2819. EmitVectorExtract(context, op.Rn, idx + 1, op.Size, signed);
  2820. context.Emit(OpCodes.Add);
  2821. if (accumulate)
  2822. {
  2823. EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  2824. context.Emit(OpCodes.Add);
  2825. }
  2826. EmitVectorInsertTmp(context, index, op.Size + 1);
  2827. }
  2828. context.EmitLdvectmp();
  2829. context.EmitStvec(op.Rd);
  2830. if (op.RegisterSize == RegisterSize.Simd64)
  2831. {
  2832. EmitVectorZeroUpper(context, op.Rd);
  2833. }
  2834. }
  2835. private static void EmitDoublingMultiplyHighHalf(ILEmitterCtx context, bool round)
  2836. {
  2837. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2838. int eSize = 8 << op.Size;
  2839. context.Emit(OpCodes.Mul);
  2840. if (!round)
  2841. {
  2842. context.EmitAsr(eSize - 1);
  2843. }
  2844. else
  2845. {
  2846. long roundConst = 1L << (eSize - 1);
  2847. ILLabel lblTrue = new ILLabel();
  2848. context.EmitLsl(1);
  2849. context.EmitLdc_I8(roundConst);
  2850. context.Emit(OpCodes.Add);
  2851. context.EmitAsr(eSize);
  2852. context.Emit(OpCodes.Dup);
  2853. context.EmitLdc_I8((long)int.MinValue);
  2854. context.Emit(OpCodes.Bne_Un_S, lblTrue);
  2855. context.Emit(OpCodes.Neg);
  2856. context.MarkLabel(lblTrue);
  2857. }
  2858. }
  2859. private static void EmitHighNarrow(ILEmitterCtx context, Action emit, bool round)
  2860. {
  2861. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2862. int elems = 8 >> op.Size;
  2863. int eSize = 8 << op.Size;
  2864. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  2865. long roundConst = 1L << (eSize - 1);
  2866. if (part != 0)
  2867. {
  2868. context.EmitLdvec(op.Rd);
  2869. context.EmitStvectmp();
  2870. }
  2871. for (int index = 0; index < elems; index++)
  2872. {
  2873. EmitVectorExtractZx(context, op.Rn, index, op.Size + 1);
  2874. EmitVectorExtractZx(context, op.Rm, index, op.Size + 1);
  2875. emit();
  2876. if (round)
  2877. {
  2878. context.EmitLdc_I8(roundConst);
  2879. context.Emit(OpCodes.Add);
  2880. }
  2881. context.EmitLsr(eSize);
  2882. EmitVectorInsertTmp(context, part + index, op.Size);
  2883. }
  2884. context.EmitLdvectmp();
  2885. context.EmitStvec(op.Rd);
  2886. if (part == 0)
  2887. {
  2888. EmitVectorZeroUpper(context, op.Rd);
  2889. }
  2890. }
  2891. private static void EmitSse41Frint(ILEmitterCtx context, RoundMode roundMode, bool scalar)
  2892. {
  2893. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  2894. if (scalar)
  2895. {
  2896. Type[] typesRnd = op.Size == 0
  2897. ? new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) }
  2898. : new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  2899. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  2900. context.EmitLdvec(op.Rn);
  2901. context.EmitCall(typeof(Sse41).GetMethod(GetScalarSse41NameRnd(roundMode), typesRnd));
  2902. context.EmitStvec(op.Rd);
  2903. }
  2904. else
  2905. {
  2906. int sizeF = op.Size & 1;
  2907. Type[] typesRnd = sizeF == 0
  2908. ? new Type[] { typeof(Vector128<float>) }
  2909. : new Type[] { typeof(Vector128<double>) };
  2910. context.EmitLdvec(op.Rn);
  2911. context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRnd));
  2912. context.EmitStvec(op.Rd);
  2913. if (sizeF == 0 && op.RegisterSize == RegisterSize.Simd64)
  2914. {
  2915. EmitVectorZeroUpper(context, op.Rd);
  2916. }
  2917. }
  2918. }
  2919. private static void EmitSse41Mul_AddSub(ILEmitterCtx context, string nameAddSub = null)
  2920. {
  2921. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2922. if (nameAddSub != null)
  2923. {
  2924. context.EmitLdvec(op.Rd);
  2925. }
  2926. if (op.Size == 0)
  2927. {
  2928. Type[] typesBle = new Type[] { typeof(Vector128<sbyte>), typeof(Vector128<sbyte>), typeof(Vector128<sbyte>) };
  2929. Type[] typesMul = new Type[] { typeof(Vector128<short>), typeof(Vector128<short>) };
  2930. Type[] typesShs = new Type[] { typeof(Vector128<short>), typeof(byte) };
  2931. Type[] typesSav = new Type[] { typeof(int) };
  2932. context.EmitLdvec(op.Rn);
  2933. context.Emit(OpCodes.Ldc_I4_8);
  2934. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical), typesShs));
  2935. context.EmitLdvec(op.Rm);
  2936. context.Emit(OpCodes.Ldc_I4_8);
  2937. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical), typesShs));
  2938. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyLow), typesMul));
  2939. context.Emit(OpCodes.Ldc_I4_8);
  2940. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical), typesShs));
  2941. context.EmitLdvec(op.Rn);
  2942. context.EmitLdvec(op.Rm);
  2943. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyLow), typesMul));
  2944. context.EmitLdc_I4(0x00FF00FF);
  2945. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  2946. context.EmitCall(typeof(Sse41).GetMethod(nameof(Sse41.BlendVariable), typesBle));
  2947. }
  2948. else if (op.Size == 1)
  2949. {
  2950. Type[] typesMul = new Type[] { typeof(Vector128<short>), typeof(Vector128<short>) };
  2951. context.EmitLdvec(op.Rn);
  2952. context.EmitLdvec(op.Rm);
  2953. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyLow), typesMul));
  2954. }
  2955. else /* if (op.Size == 2) */
  2956. {
  2957. Type[] typesMul = new Type[] { typeof(Vector128<int>), typeof(Vector128<int>) };
  2958. context.EmitLdvec(op.Rn);
  2959. context.EmitLdvec(op.Rm);
  2960. context.EmitCall(typeof(Sse41).GetMethod(nameof(Sse41.MultiplyLow), typesMul));
  2961. }
  2962. if (nameAddSub != null)
  2963. {
  2964. Type[] typesAddSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  2965. context.EmitCall(typeof(Sse2).GetMethod(nameAddSub, typesAddSub));
  2966. }
  2967. context.EmitStvec(op.Rd);
  2968. if (op.RegisterSize == RegisterSize.Simd64)
  2969. {
  2970. EmitVectorZeroUpper(context, op.Rd);
  2971. }
  2972. }
  2973. }
  2974. }