InstEmitSimdArithmetic.cs 114 KB

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