InstEmitSimdArithmetic.cs 105 KB

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