InstEmitSimdCvt.cs 44 KB

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