InstEmitSimdArithmetic.cs 80 KB

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