InstEmitSimdCvt.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using System.Runtime.Intrinsics;
  7. using System.Runtime.Intrinsics.X86;
  8. using static ChocolArm64.Instructions.InstEmitSimdHelper;
  9. namespace ChocolArm64.Instructions
  10. {
  11. static partial class InstEmit
  12. {
  13. public static void Fcvt_S(ILEmitterCtx context)
  14. {
  15. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  16. if (Optimizations.UseSse2)
  17. {
  18. if (op.Size == 1 && op.Opc == 0)
  19. {
  20. //Double -> Single.
  21. Type[] typesCvt = new Type[] { typeof(Vector128<float>), typeof(Vector128<double>) };
  22. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  23. context.EmitLdvec(op.Rn);
  24. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertScalarToVector128Single), typesCvt));
  25. context.EmitStvec(op.Rd);
  26. }
  27. else if (op.Size == 0 && op.Opc == 1)
  28. {
  29. //Single -> Double.
  30. Type[] typesCvt = new Type[] { typeof(Vector128<double>), typeof(Vector128<float>) };
  31. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  32. context.EmitLdvec(op.Rn);
  33. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertScalarToVector128Double), typesCvt));
  34. context.EmitStvec(op.Rd);
  35. }
  36. else
  37. {
  38. //Invalid encoding.
  39. throw new InvalidOperationException();
  40. }
  41. }
  42. else
  43. {
  44. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  45. EmitFloatCast(context, op.Opc);
  46. EmitScalarSetF(context, op.Rd, op.Opc);
  47. }
  48. }
  49. public static void Fcvtas_Gp(ILEmitterCtx context)
  50. {
  51. EmitFcvt_s_Gp(context, () => EmitRoundMathCall(context, MidpointRounding.AwayFromZero));
  52. }
  53. public static void Fcvtau_Gp(ILEmitterCtx context)
  54. {
  55. EmitFcvt_u_Gp(context, () => EmitRoundMathCall(context, MidpointRounding.AwayFromZero));
  56. }
  57. public static void Fcvtl_V(ILEmitterCtx context)
  58. {
  59. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  60. int sizeF = op.Size & 1;
  61. if (Optimizations.UseSse2 && sizeF == 1)
  62. {
  63. Type[] typesCvt = new Type[] { typeof(Vector128<float>) };
  64. context.EmitLdvec(op.Rn);
  65. if (op.RegisterSize == RegisterSize.Simd128)
  66. {
  67. context.EmitLdvec(op.Rn);
  68. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveHighToLow)));
  69. }
  70. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Double), typesCvt));
  71. context.EmitStvec(op.Rd);
  72. }
  73. else
  74. {
  75. int elems = 4 >> sizeF;
  76. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  77. for (int index = 0; index < elems; index++)
  78. {
  79. if (sizeF == 0)
  80. {
  81. EmitVectorExtractZx(context, op.Rn, part + index, 1);
  82. context.Emit(OpCodes.Conv_U2);
  83. context.EmitLdarg(TranslatedSub.StateArgIdx);
  84. context.EmitCall(typeof(SoftFloat16_32), nameof(SoftFloat16_32.FPConvert));
  85. }
  86. else /* if (sizeF == 1) */
  87. {
  88. EmitVectorExtractF(context, op.Rn, part + index, 0);
  89. context.Emit(OpCodes.Conv_R8);
  90. }
  91. EmitVectorInsertTmpF(context, index, sizeF);
  92. }
  93. context.EmitLdvectmp();
  94. context.EmitStvec(op.Rd);
  95. }
  96. }
  97. public static void Fcvtms_Gp(ILEmitterCtx context)
  98. {
  99. EmitFcvt_s_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Floor)));
  100. }
  101. public static void Fcvtmu_Gp(ILEmitterCtx context)
  102. {
  103. EmitFcvt_u_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Floor)));
  104. }
  105. public static void Fcvtn_V(ILEmitterCtx context)
  106. {
  107. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  108. int sizeF = op.Size & 1;
  109. if (Optimizations.UseSse2 && sizeF == 1)
  110. {
  111. Type[] typesCvt = new Type[] { typeof(Vector128<double>) };
  112. string nameMov = op.RegisterSize == RegisterSize.Simd128
  113. ? nameof(Sse.MoveLowToHigh)
  114. : nameof(Sse.MoveHighToLow);
  115. context.EmitLdvec(op.Rd);
  116. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  117. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  118. context.EmitLdvec(op.Rn);
  119. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
  120. context.Emit(OpCodes.Dup);
  121. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  122. context.EmitCall(typeof(Sse).GetMethod(nameMov));
  123. context.EmitStvec(op.Rd);
  124. }
  125. else
  126. {
  127. int elems = 4 >> sizeF;
  128. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  129. if (part != 0)
  130. {
  131. context.EmitLdvec(op.Rd);
  132. context.EmitStvectmp();
  133. }
  134. for (int index = 0; index < elems; index++)
  135. {
  136. EmitVectorExtractF(context, op.Rn, index, sizeF);
  137. if (sizeF == 0)
  138. {
  139. context.EmitLdarg(TranslatedSub.StateArgIdx);
  140. context.EmitCall(typeof(SoftFloat32_16), nameof(SoftFloat32_16.FPConvert));
  141. context.Emit(OpCodes.Conv_U8);
  142. EmitVectorInsertTmp(context, part + index, 1);
  143. }
  144. else /* if (sizeF == 1) */
  145. {
  146. context.Emit(OpCodes.Conv_R4);
  147. EmitVectorInsertTmpF(context, part + index, 0);
  148. }
  149. }
  150. context.EmitLdvectmp();
  151. context.EmitStvec(op.Rd);
  152. if (part == 0)
  153. {
  154. EmitVectorZeroUpper(context, op.Rd);
  155. }
  156. }
  157. }
  158. public static void Fcvtns_S(ILEmitterCtx context)
  159. {
  160. if (Optimizations.UseSse41)
  161. {
  162. EmitSse41Fcvt_Signed(context, RoundMode.ToNearest, scalar: true);
  163. }
  164. else
  165. {
  166. EmitFcvtn(context, signed: true, scalar: true);
  167. }
  168. }
  169. public static void Fcvtns_V(ILEmitterCtx context)
  170. {
  171. if (Optimizations.UseSse41)
  172. {
  173. EmitSse41Fcvt_Signed(context, RoundMode.ToNearest, scalar: false);
  174. }
  175. else
  176. {
  177. EmitFcvtn(context, signed: true, scalar: false);
  178. }
  179. }
  180. public static void Fcvtnu_S(ILEmitterCtx context)
  181. {
  182. if (Optimizations.UseSse41)
  183. {
  184. EmitSse41Fcvt_Unsigned(context, RoundMode.ToNearest, scalar: true);
  185. }
  186. else
  187. {
  188. EmitFcvtn(context, signed: false, scalar: true);
  189. }
  190. }
  191. public static void Fcvtnu_V(ILEmitterCtx context)
  192. {
  193. if (Optimizations.UseSse41)
  194. {
  195. EmitSse41Fcvt_Unsigned(context, RoundMode.ToNearest, scalar: false);
  196. }
  197. else
  198. {
  199. EmitFcvtn(context, signed: false, scalar: false);
  200. }
  201. }
  202. public static void Fcvtps_Gp(ILEmitterCtx context)
  203. {
  204. EmitFcvt_s_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Ceiling)));
  205. }
  206. public static void Fcvtpu_Gp(ILEmitterCtx context)
  207. {
  208. EmitFcvt_u_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Ceiling)));
  209. }
  210. public static void Fcvtzs_Gp(ILEmitterCtx context)
  211. {
  212. EmitFcvt_s_Gp(context, () => { });
  213. }
  214. public static void Fcvtzs_Gp_Fixed(ILEmitterCtx context)
  215. {
  216. EmitFcvtzs_Gp_Fixed(context);
  217. }
  218. public static void Fcvtzs_S(ILEmitterCtx context)
  219. {
  220. if (Optimizations.UseSse41)
  221. {
  222. EmitSse41Fcvt_Signed(context, RoundMode.TowardsZero, scalar: true);
  223. }
  224. else
  225. {
  226. EmitFcvtz(context, signed: true, scalar: true);
  227. }
  228. }
  229. public static void Fcvtzs_V(ILEmitterCtx context)
  230. {
  231. if (Optimizations.UseSse41)
  232. {
  233. EmitSse41Fcvt_Signed(context, RoundMode.TowardsZero, scalar: false);
  234. }
  235. else
  236. {
  237. EmitFcvtz(context, signed: true, scalar: false);
  238. }
  239. }
  240. public static void Fcvtzs_V_Fixed(ILEmitterCtx context)
  241. {
  242. if (Optimizations.UseSse41)
  243. {
  244. EmitSse41Fcvt_Signed(context, RoundMode.TowardsZero, scalar: false);
  245. }
  246. else
  247. {
  248. EmitFcvtz(context, signed: true, scalar: false);
  249. }
  250. }
  251. public static void Fcvtzu_Gp(ILEmitterCtx context)
  252. {
  253. EmitFcvt_u_Gp(context, () => { });
  254. }
  255. public static void Fcvtzu_Gp_Fixed(ILEmitterCtx context)
  256. {
  257. EmitFcvtzu_Gp_Fixed(context);
  258. }
  259. public static void Fcvtzu_S(ILEmitterCtx context)
  260. {
  261. if (Optimizations.UseSse41)
  262. {
  263. EmitSse41Fcvt_Unsigned(context, RoundMode.TowardsZero, scalar: true);
  264. }
  265. else
  266. {
  267. EmitFcvtz(context, signed: false, scalar: true);
  268. }
  269. }
  270. public static void Fcvtzu_V(ILEmitterCtx context)
  271. {
  272. if (Optimizations.UseSse41)
  273. {
  274. EmitSse41Fcvt_Unsigned(context, RoundMode.TowardsZero, scalar: false);
  275. }
  276. else
  277. {
  278. EmitFcvtz(context, signed: false, scalar: false);
  279. }
  280. }
  281. public static void Fcvtzu_V_Fixed(ILEmitterCtx context)
  282. {
  283. if (Optimizations.UseSse41)
  284. {
  285. EmitSse41Fcvt_Unsigned(context, RoundMode.TowardsZero, scalar: false);
  286. }
  287. else
  288. {
  289. EmitFcvtz(context, signed: false, scalar: false);
  290. }
  291. }
  292. public static void Scvtf_Gp(ILEmitterCtx context)
  293. {
  294. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  295. context.EmitLdintzr(op.Rn);
  296. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  297. {
  298. context.Emit(OpCodes.Conv_I4);
  299. }
  300. EmitFloatCast(context, op.Size);
  301. EmitScalarSetF(context, op.Rd, op.Size);
  302. }
  303. public static void Scvtf_Gp_Fixed(ILEmitterCtx context)
  304. {
  305. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  306. context.EmitLdintzr(op.Rn);
  307. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  308. {
  309. context.Emit(OpCodes.Conv_I4);
  310. }
  311. EmitFloatCast(context, op.Size);
  312. EmitI2fFBitsMul(context, op.Size, op.FBits);
  313. EmitScalarSetF(context, op.Rd, op.Size);
  314. }
  315. public static void Scvtf_S(ILEmitterCtx context)
  316. {
  317. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  318. int sizeF = op.Size & 1;
  319. if (Optimizations.UseSse2 && sizeF == 0)
  320. {
  321. EmitSse2cvtF_Signed(context, scalar: true);
  322. }
  323. else
  324. {
  325. EmitVectorExtractSx(context, op.Rn, 0, sizeF + 2);
  326. EmitFloatCast(context, sizeF);
  327. EmitScalarSetF(context, op.Rd, sizeF);
  328. }
  329. }
  330. public static void Scvtf_V(ILEmitterCtx context)
  331. {
  332. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  333. int sizeF = op.Size & 1;
  334. if (Optimizations.UseSse2 && sizeF == 0)
  335. {
  336. EmitSse2cvtF_Signed(context, scalar: false);
  337. }
  338. else
  339. {
  340. EmitVectorCvtf(context, signed: true);
  341. }
  342. }
  343. public static void Scvtf_V_Fixed(ILEmitterCtx context)
  344. {
  345. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  346. // sizeF == ((OpCodeSimdShImm64)op).Size - 2
  347. int sizeF = op.Size & 1;
  348. if (Optimizations.UseSse2 && sizeF == 0)
  349. {
  350. EmitSse2cvtF_Signed(context, scalar: false);
  351. }
  352. else
  353. {
  354. EmitVectorCvtf(context, signed: true);
  355. }
  356. }
  357. public static void Ucvtf_Gp(ILEmitterCtx context)
  358. {
  359. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  360. context.EmitLdintzr(op.Rn);
  361. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  362. {
  363. context.Emit(OpCodes.Conv_U4);
  364. }
  365. context.Emit(OpCodes.Conv_R_Un);
  366. EmitFloatCast(context, op.Size);
  367. EmitScalarSetF(context, op.Rd, op.Size);
  368. }
  369. public static void Ucvtf_Gp_Fixed(ILEmitterCtx context)
  370. {
  371. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  372. context.EmitLdintzr(op.Rn);
  373. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  374. {
  375. context.Emit(OpCodes.Conv_U4);
  376. }
  377. context.Emit(OpCodes.Conv_R_Un);
  378. EmitFloatCast(context, op.Size);
  379. EmitI2fFBitsMul(context, op.Size, op.FBits);
  380. EmitScalarSetF(context, op.Rd, op.Size);
  381. }
  382. public static void Ucvtf_S(ILEmitterCtx context)
  383. {
  384. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  385. int sizeF = op.Size & 1;
  386. if (Optimizations.UseSse2 && sizeF == 0)
  387. {
  388. EmitSse2cvtF_Unsigned(context, scalar: true);
  389. }
  390. else
  391. {
  392. EmitVectorExtractZx(context, op.Rn, 0, sizeF + 2);
  393. context.Emit(OpCodes.Conv_R_Un);
  394. EmitFloatCast(context, sizeF);
  395. EmitScalarSetF(context, op.Rd, sizeF);
  396. }
  397. }
  398. public static void Ucvtf_V(ILEmitterCtx context)
  399. {
  400. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  401. int sizeF = op.Size & 1;
  402. if (Optimizations.UseSse2 && sizeF == 0)
  403. {
  404. EmitSse2cvtF_Unsigned(context, scalar: false);
  405. }
  406. else
  407. {
  408. EmitVectorCvtf(context, signed: false);
  409. }
  410. }
  411. public static void Ucvtf_V_Fixed(ILEmitterCtx context)
  412. {
  413. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  414. // sizeF == ((OpCodeSimdShImm64)op).Size - 2
  415. int sizeF = op.Size & 1;
  416. if (Optimizations.UseSse2 && sizeF == 0)
  417. {
  418. EmitSse2cvtF_Unsigned(context, scalar: false);
  419. }
  420. else
  421. {
  422. EmitVectorCvtf(context, signed: false);
  423. }
  424. }
  425. private static void EmitFcvtn(ILEmitterCtx context, bool signed, bool scalar)
  426. {
  427. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  428. int sizeF = op.Size & 1;
  429. int sizeI = sizeF + 2;
  430. int bytes = op.GetBitsCount() >> 3;
  431. int elems = !scalar ? bytes >> sizeI : 1;
  432. for (int index = 0; index < elems; index++)
  433. {
  434. EmitVectorExtractF(context, op.Rn, index, sizeF);
  435. EmitRoundMathCall(context, MidpointRounding.ToEven);
  436. if (sizeF == 0)
  437. {
  438. VectorHelper.EmitCall(context, signed
  439. ? nameof(VectorHelper.SatF32ToS32)
  440. : nameof(VectorHelper.SatF32ToU32));
  441. context.Emit(OpCodes.Conv_U8);
  442. }
  443. else /* if (sizeF == 1) */
  444. {
  445. VectorHelper.EmitCall(context, signed
  446. ? nameof(VectorHelper.SatF64ToS64)
  447. : nameof(VectorHelper.SatF64ToU64));
  448. }
  449. if (scalar)
  450. {
  451. EmitVectorZeroAll(context, op.Rd);
  452. }
  453. EmitVectorInsert(context, op.Rd, index, sizeI);
  454. }
  455. if (op.RegisterSize == RegisterSize.Simd64)
  456. {
  457. EmitVectorZeroUpper(context, op.Rd);
  458. }
  459. }
  460. private static void EmitFcvtz(ILEmitterCtx context, bool signed, bool scalar)
  461. {
  462. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  463. int sizeF = op.Size & 1;
  464. int sizeI = sizeF + 2;
  465. int fBits = GetFBits(context);
  466. int bytes = op.GetBitsCount() >> 3;
  467. int elems = !scalar ? bytes >> sizeI : 1;
  468. for (int index = 0; index < elems; index++)
  469. {
  470. EmitVectorExtractF(context, op.Rn, index, sizeF);
  471. EmitF2iFBitsMul(context, sizeF, fBits);
  472. if (sizeF == 0)
  473. {
  474. VectorHelper.EmitCall(context, signed
  475. ? nameof(VectorHelper.SatF32ToS32)
  476. : nameof(VectorHelper.SatF32ToU32));
  477. context.Emit(OpCodes.Conv_U8);
  478. }
  479. else /* if (sizeF == 1) */
  480. {
  481. VectorHelper.EmitCall(context, signed
  482. ? nameof(VectorHelper.SatF64ToS64)
  483. : nameof(VectorHelper.SatF64ToU64));
  484. }
  485. if (scalar)
  486. {
  487. EmitVectorZeroAll(context, op.Rd);
  488. }
  489. EmitVectorInsert(context, op.Rd, index, sizeI);
  490. }
  491. if (op.RegisterSize == RegisterSize.Simd64)
  492. {
  493. EmitVectorZeroUpper(context, op.Rd);
  494. }
  495. }
  496. private static void EmitFcvt_s_Gp(ILEmitterCtx context, Action emit)
  497. {
  498. EmitFcvt___Gp(context, emit, true);
  499. }
  500. private static void EmitFcvt_u_Gp(ILEmitterCtx context, Action emit)
  501. {
  502. EmitFcvt___Gp(context, emit, false);
  503. }
  504. private static void EmitFcvt___Gp(ILEmitterCtx context, Action emit, bool signed)
  505. {
  506. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  507. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  508. emit();
  509. if (signed)
  510. {
  511. EmitScalarFcvts(context, op.Size, 0);
  512. }
  513. else
  514. {
  515. EmitScalarFcvtu(context, op.Size, 0);
  516. }
  517. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  518. {
  519. context.Emit(OpCodes.Conv_U8);
  520. }
  521. context.EmitStintzr(op.Rd);
  522. }
  523. private static void EmitFcvtzs_Gp_Fixed(ILEmitterCtx context)
  524. {
  525. EmitFcvtz__Gp_Fixed(context, true);
  526. }
  527. private static void EmitFcvtzu_Gp_Fixed(ILEmitterCtx context)
  528. {
  529. EmitFcvtz__Gp_Fixed(context, false);
  530. }
  531. private static void EmitFcvtz__Gp_Fixed(ILEmitterCtx context, bool signed)
  532. {
  533. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  534. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  535. if (signed)
  536. {
  537. EmitScalarFcvts(context, op.Size, op.FBits);
  538. }
  539. else
  540. {
  541. EmitScalarFcvtu(context, op.Size, op.FBits);
  542. }
  543. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  544. {
  545. context.Emit(OpCodes.Conv_U8);
  546. }
  547. context.EmitStintzr(op.Rd);
  548. }
  549. private static void EmitVectorCvtf(ILEmitterCtx context, bool signed)
  550. {
  551. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  552. int sizeF = op.Size & 1;
  553. int sizeI = sizeF + 2;
  554. int fBits = GetFBits(context);
  555. int bytes = op.GetBitsCount() >> 3;
  556. int elems = bytes >> sizeI;
  557. for (int index = 0; index < elems; index++)
  558. {
  559. EmitVectorExtract(context, op.Rn, index, sizeI, signed);
  560. if (!signed)
  561. {
  562. context.Emit(OpCodes.Conv_R_Un);
  563. }
  564. EmitFloatCast(context, sizeF);
  565. EmitI2fFBitsMul(context, sizeF, fBits);
  566. EmitVectorInsertF(context, op.Rd, index, sizeF);
  567. }
  568. if (op.RegisterSize == RegisterSize.Simd64)
  569. {
  570. EmitVectorZeroUpper(context, op.Rd);
  571. }
  572. }
  573. private static int GetFBits(ILEmitterCtx context)
  574. {
  575. if (context.CurrOp is OpCodeSimdShImm64 op)
  576. {
  577. return GetImmShr(op);
  578. }
  579. return 0;
  580. }
  581. private static void EmitFloatCast(ILEmitterCtx context, int size)
  582. {
  583. if (size == 0)
  584. {
  585. context.Emit(OpCodes.Conv_R4);
  586. }
  587. else if (size == 1)
  588. {
  589. context.Emit(OpCodes.Conv_R8);
  590. }
  591. else
  592. {
  593. throw new ArgumentOutOfRangeException(nameof(size));
  594. }
  595. }
  596. private static void EmitScalarFcvts(ILEmitterCtx context, int size, int fBits)
  597. {
  598. if (size < 0 || size > 1)
  599. {
  600. throw new ArgumentOutOfRangeException(nameof(size));
  601. }
  602. EmitF2iFBitsMul(context, size, fBits);
  603. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  604. {
  605. if (size == 0)
  606. {
  607. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS32));
  608. }
  609. else /* if (size == 1) */
  610. {
  611. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS32));
  612. }
  613. }
  614. else
  615. {
  616. if (size == 0)
  617. {
  618. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS64));
  619. }
  620. else /* if (size == 1) */
  621. {
  622. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS64));
  623. }
  624. }
  625. }
  626. private static void EmitScalarFcvtu(ILEmitterCtx context, int size, int fBits)
  627. {
  628. if (size < 0 || size > 1)
  629. {
  630. throw new ArgumentOutOfRangeException(nameof(size));
  631. }
  632. EmitF2iFBitsMul(context, size, fBits);
  633. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  634. {
  635. if (size == 0)
  636. {
  637. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU32));
  638. }
  639. else /* if (size == 1) */
  640. {
  641. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU32));
  642. }
  643. }
  644. else
  645. {
  646. if (size == 0)
  647. {
  648. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU64));
  649. }
  650. else /* if (size == 1) */
  651. {
  652. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU64));
  653. }
  654. }
  655. }
  656. private static void EmitF2iFBitsMul(ILEmitterCtx context, int size, int fBits)
  657. {
  658. if (fBits != 0)
  659. {
  660. if (size == 0)
  661. {
  662. context.EmitLdc_R4(MathF.Pow(2f, fBits));
  663. }
  664. else if (size == 1)
  665. {
  666. context.EmitLdc_R8(Math.Pow(2d, fBits));
  667. }
  668. else
  669. {
  670. throw new ArgumentOutOfRangeException(nameof(size));
  671. }
  672. context.Emit(OpCodes.Mul);
  673. }
  674. }
  675. private static void EmitI2fFBitsMul(ILEmitterCtx context, int size, int fBits)
  676. {
  677. if (fBits != 0)
  678. {
  679. if (size == 0)
  680. {
  681. context.EmitLdc_R4(1f / MathF.Pow(2f, fBits));
  682. }
  683. else if (size == 1)
  684. {
  685. context.EmitLdc_R8(1d / Math.Pow(2d, fBits));
  686. }
  687. else
  688. {
  689. throw new ArgumentOutOfRangeException(nameof(size));
  690. }
  691. context.Emit(OpCodes.Mul);
  692. }
  693. }
  694. private static void EmitSse41Fcvt_Signed(ILEmitterCtx context, RoundMode roundMode, bool scalar)
  695. {
  696. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  697. // sizeF == ((OpCodeSimdShImm64)op).Size - 2
  698. int sizeF = op.Size & 1;
  699. if (sizeF == 0)
  700. {
  701. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  702. Type[] typesRndCvt = new Type[] { typeof(Vector128<float>) };
  703. Type[] typesSav = new Type[] { typeof(int) };
  704. context.EmitLdvec(op.Rn);
  705. context.EmitLdvec(op.Rn);
  706. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), types));
  707. context.EmitLdvec(op.Rn);
  708. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
  709. if (op is OpCodeSimdShImm64 fixedOp)
  710. {
  711. int fBits = GetImmShr(fixedOp);
  712. // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
  713. int fpScaled = 0x3F800000 + fBits * 0x800000;
  714. context.EmitLdc_I4(fpScaled);
  715. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  716. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types));
  717. }
  718. context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt));
  719. context.EmitStvectmp();
  720. context.EmitLdvectmp();
  721. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
  722. context.EmitLdvectmp();
  723. context.EmitLdc_I4(0x4F000000); // 2.14748365E9f (2147483648)
  724. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  725. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThanOrEqual), types));
  726. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), types));
  727. context.EmitStvec(op.Rd);
  728. if (scalar)
  729. {
  730. EmitVectorZero32_128(context, op.Rd);
  731. }
  732. else if (op.RegisterSize == RegisterSize.Simd64)
  733. {
  734. EmitVectorZeroUpper(context, op.Rd);
  735. }
  736. }
  737. else /* if (sizeF == 1) */
  738. {
  739. Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  740. Type[] typesRndCvt = new Type[] { typeof(Vector128<double>) };
  741. Type[] typesSv = new Type[] { typeof(long), typeof(long) };
  742. Type[] typesSav = new Type[] { typeof(long) };
  743. context.EmitLdvec(op.Rn);
  744. context.EmitLdvec(op.Rn);
  745. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareOrdered), types));
  746. context.EmitLdvec(op.Rn);
  747. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
  748. if (op is OpCodeSimdShImm64 fixedOp)
  749. {
  750. int fBits = GetImmShr(fixedOp);
  751. // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
  752. long fpScaled = 0x3FF0000000000000L + fBits * 0x10000000000000L;
  753. context.EmitLdc_I8(fpScaled);
  754. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  755. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types));
  756. }
  757. context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt));
  758. context.EmitStvectmp();
  759. if (!scalar)
  760. {
  761. context.EmitLdvectmp();
  762. context.EmitLdvectmp();
  763. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
  764. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  765. }
  766. else
  767. {
  768. context.EmitLdc_I8(0L);
  769. }
  770. context.EmitLdvectmp();
  771. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  772. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
  773. context.EmitLdvectmp();
  774. context.EmitLdc_I8(0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
  775. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  776. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThanOrEqual), types));
  777. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), types));
  778. context.EmitStvec(op.Rd);
  779. if (scalar)
  780. {
  781. EmitVectorZeroUpper(context, op.Rd);
  782. }
  783. }
  784. }
  785. private static void EmitSse41Fcvt_Unsigned(ILEmitterCtx context, RoundMode roundMode, bool scalar)
  786. {
  787. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  788. // sizeF == ((OpCodeSimdShImm64)op).Size - 2
  789. int sizeF = op.Size & 1;
  790. if (sizeF == 0)
  791. {
  792. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  793. Type[] typesAdd = new Type[] { typeof(Vector128<int>), typeof(Vector128<int>) };
  794. Type[] typesRndCvt = new Type[] { typeof(Vector128<float>) };
  795. Type[] typesSav = new Type[] { typeof(int) };
  796. context.EmitLdvec(op.Rn);
  797. context.EmitLdvec(op.Rn);
  798. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), types));
  799. context.EmitLdvec(op.Rn);
  800. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
  801. if (op is OpCodeSimdShImm64 fixedOp)
  802. {
  803. int fBits = GetImmShr(fixedOp);
  804. // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
  805. int fpScaled = 0x3F800000 + fBits * 0x800000;
  806. context.EmitLdc_I4(fpScaled);
  807. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  808. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types));
  809. }
  810. context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt));
  811. context.Emit(OpCodes.Dup);
  812. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  813. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThan), types));
  814. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
  815. context.EmitStvectmp();
  816. context.EmitLdvectmp();
  817. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
  818. context.EmitLdvectmp();
  819. context.EmitLdc_I4(0x4F000000); // 2.14748365E9f (2147483648)
  820. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  821. context.EmitStvectmp2();
  822. context.EmitLdvectmp2();
  823. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), types));
  824. context.Emit(OpCodes.Dup);
  825. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  826. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThan), types));
  827. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
  828. context.EmitStvectmp();
  829. context.EmitLdvectmp();
  830. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
  831. context.EmitLdvectmp();
  832. context.EmitLdvectmp2();
  833. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThanOrEqual), types));
  834. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), types));
  835. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  836. context.EmitStvec(op.Rd);
  837. if (scalar)
  838. {
  839. EmitVectorZero32_128(context, op.Rd);
  840. }
  841. else if (op.RegisterSize == RegisterSize.Simd64)
  842. {
  843. EmitVectorZeroUpper(context, op.Rd);
  844. }
  845. }
  846. else /* if (sizeF == 1) */
  847. {
  848. Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  849. Type[] typesAdd = new Type[] { typeof(Vector128<long>), typeof(Vector128<long>) };
  850. Type[] typesRndCvt = new Type[] { typeof(Vector128<double>) };
  851. Type[] typesSv = new Type[] { typeof(long), typeof(long) };
  852. Type[] typesSav = new Type[] { typeof(long) };
  853. context.EmitLdvec(op.Rn);
  854. context.EmitLdvec(op.Rn);
  855. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareOrdered), types));
  856. context.EmitLdvec(op.Rn);
  857. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
  858. if (op is OpCodeSimdShImm64 fixedOp)
  859. {
  860. int fBits = GetImmShr(fixedOp);
  861. // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
  862. long fpScaled = 0x3FF0000000000000L + fBits * 0x10000000000000L;
  863. context.EmitLdc_I8(fpScaled);
  864. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  865. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types));
  866. }
  867. context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt));
  868. context.Emit(OpCodes.Dup);
  869. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  870. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), types));
  871. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
  872. context.EmitStvectmp();
  873. if (!scalar)
  874. {
  875. context.EmitLdvectmp();
  876. context.EmitLdvectmp();
  877. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
  878. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  879. }
  880. else
  881. {
  882. context.EmitLdc_I8(0L);
  883. }
  884. context.EmitLdvectmp();
  885. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  886. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
  887. context.EmitLdvectmp();
  888. context.EmitLdc_I8(0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
  889. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  890. context.EmitStvectmp2();
  891. context.EmitLdvectmp2();
  892. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), types));
  893. context.Emit(OpCodes.Dup);
  894. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  895. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), types));
  896. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
  897. context.EmitStvectmp();
  898. if (!scalar)
  899. {
  900. context.EmitLdvectmp();
  901. context.EmitLdvectmp();
  902. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
  903. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  904. }
  905. else
  906. {
  907. context.EmitLdc_I8(0L);
  908. }
  909. context.EmitLdvectmp();
  910. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  911. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
  912. context.EmitLdvectmp();
  913. context.EmitLdvectmp2();
  914. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThanOrEqual), types));
  915. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), types));
  916. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  917. context.EmitStvec(op.Rd);
  918. if (scalar)
  919. {
  920. EmitVectorZeroUpper(context, op.Rd);
  921. }
  922. }
  923. }
  924. private static void EmitSse2cvtF_Signed(ILEmitterCtx context, bool scalar)
  925. {
  926. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  927. Type[] typesMul = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  928. Type[] typesCvt = new Type[] { typeof(Vector128<int>) };
  929. Type[] typesSav = new Type[] { typeof(int) };
  930. context.EmitLdvec(op.Rn);
  931. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
  932. if (op is OpCodeSimdShImm64 fixedOp)
  933. {
  934. int fBits = GetImmShr(fixedOp);
  935. // BitConverter.Int32BitsToSingle(fpScaled) == 1f / MathF.Pow(2f, fBits)
  936. int fpScaled = 0x3F800000 - fBits * 0x800000;
  937. context.EmitLdc_I4(fpScaled);
  938. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  939. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMul));
  940. }
  941. context.EmitStvec(op.Rd);
  942. if (scalar)
  943. {
  944. EmitVectorZero32_128(context, op.Rd);
  945. }
  946. else if (op.RegisterSize == RegisterSize.Simd64)
  947. {
  948. EmitVectorZeroUpper(context, op.Rd);
  949. }
  950. }
  951. private static void EmitSse2cvtF_Unsigned(ILEmitterCtx context, bool scalar)
  952. {
  953. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  954. Type[] typesMulAdd = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  955. Type[] typesSrlSll = new Type[] { typeof(Vector128<int>), typeof(byte) };
  956. Type[] typesCvt = new Type[] { typeof(Vector128<int>) };
  957. Type[] typesSav = new Type[] { typeof(int) };
  958. context.EmitLdvec(op.Rn);
  959. context.EmitLdc_I4(16);
  960. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical), typesSrlSll));
  961. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
  962. context.EmitLdc_I4(0x47800000); // 65536.0f (1 << 16)
  963. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  964. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulAdd));
  965. context.EmitLdvec(op.Rn);
  966. context.EmitLdc_I4(16);
  967. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical), typesSrlSll));
  968. context.EmitLdc_I4(16);
  969. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical), typesSrlSll));
  970. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
  971. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Add), typesMulAdd));
  972. if (op is OpCodeSimdShImm64 fixedOp)
  973. {
  974. int fBits = GetImmShr(fixedOp);
  975. // BitConverter.Int32BitsToSingle(fpScaled) == 1f / MathF.Pow(2f, fBits)
  976. int fpScaled = 0x3F800000 - fBits * 0x800000;
  977. context.EmitLdc_I4(fpScaled);
  978. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  979. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulAdd));
  980. }
  981. context.EmitStvec(op.Rd);
  982. if (scalar)
  983. {
  984. EmitVectorZero32_128(context, op.Rd);
  985. }
  986. else if (op.RegisterSize == RegisterSize.Simd64)
  987. {
  988. EmitVectorZeroUpper(context, op.Rd);
  989. }
  990. }
  991. private static string GetSse41NameRnd(RoundMode roundMode)
  992. {
  993. switch (roundMode)
  994. {
  995. case RoundMode.ToNearest:
  996. return nameof(Sse41.RoundToNearestInteger); // even
  997. case RoundMode.TowardsMinusInfinity:
  998. return nameof(Sse41.RoundToNegativeInfinity);
  999. case RoundMode.TowardsPlusInfinity:
  1000. return nameof(Sse41.RoundToPositiveInfinity);
  1001. case RoundMode.TowardsZero:
  1002. return nameof(Sse41.RoundToZero);
  1003. default: throw new ArgumentException(nameof(roundMode));
  1004. }
  1005. }
  1006. }
  1007. }