InstEmitSimdCvt.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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_U4);
  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. EmitVectorExtractSx(context, op.Rn, 0, op.Size + 2);
  319. EmitFloatCast(context, op.Size);
  320. EmitScalarSetF(context, op.Rd, op.Size);
  321. }
  322. public static void Scvtf_V(ILEmitterCtx context)
  323. {
  324. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  325. int sizeF = op.Size & 1;
  326. if (Optimizations.UseSse2 && sizeF == 0)
  327. {
  328. Type[] typesCvt = new Type[] { typeof(Vector128<int>) };
  329. context.EmitLdvec(op.Rn);
  330. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
  331. context.EmitStvec(op.Rd);
  332. if (op.RegisterSize == RegisterSize.Simd64)
  333. {
  334. EmitVectorZeroUpper(context, op.Rd);
  335. }
  336. }
  337. else
  338. {
  339. EmitVectorCvtf(context, signed: true);
  340. }
  341. }
  342. public static void Ucvtf_Gp(ILEmitterCtx context)
  343. {
  344. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  345. context.EmitLdintzr(op.Rn);
  346. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  347. {
  348. context.Emit(OpCodes.Conv_U4);
  349. }
  350. context.Emit(OpCodes.Conv_R_Un);
  351. EmitFloatCast(context, op.Size);
  352. EmitScalarSetF(context, op.Rd, op.Size);
  353. }
  354. public static void Ucvtf_Gp_Fixed(ILEmitterCtx context)
  355. {
  356. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  357. context.EmitLdintzr(op.Rn);
  358. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  359. {
  360. context.Emit(OpCodes.Conv_U4);
  361. }
  362. context.Emit(OpCodes.Conv_R_Un);
  363. EmitFloatCast(context, op.Size);
  364. EmitI2fFBitsMul(context, op.Size, op.FBits);
  365. EmitScalarSetF(context, op.Rd, op.Size);
  366. }
  367. public static void Ucvtf_S(ILEmitterCtx context)
  368. {
  369. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  370. EmitVectorExtractZx(context, op.Rn, 0, op.Size + 2);
  371. context.Emit(OpCodes.Conv_R_Un);
  372. EmitFloatCast(context, op.Size);
  373. EmitScalarSetF(context, op.Rd, op.Size);
  374. }
  375. public static void Ucvtf_V(ILEmitterCtx context)
  376. {
  377. EmitVectorCvtf(context, signed: false);
  378. }
  379. private static void EmitFcvtn(ILEmitterCtx context, bool signed, bool scalar)
  380. {
  381. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  382. int sizeF = op.Size & 1;
  383. int sizeI = sizeF + 2;
  384. int bytes = op.GetBitsCount() >> 3;
  385. int elems = !scalar ? bytes >> sizeI : 1;
  386. for (int index = 0; index < elems; index++)
  387. {
  388. EmitVectorExtractF(context, op.Rn, index, sizeF);
  389. EmitRoundMathCall(context, MidpointRounding.ToEven);
  390. if (sizeF == 0)
  391. {
  392. VectorHelper.EmitCall(context, signed
  393. ? nameof(VectorHelper.SatF32ToS32)
  394. : nameof(VectorHelper.SatF32ToU32));
  395. context.Emit(OpCodes.Conv_U8);
  396. }
  397. else /* if (sizeF == 1) */
  398. {
  399. VectorHelper.EmitCall(context, signed
  400. ? nameof(VectorHelper.SatF64ToS64)
  401. : nameof(VectorHelper.SatF64ToU64));
  402. }
  403. if (scalar)
  404. {
  405. EmitVectorZeroAll(context, op.Rd);
  406. }
  407. EmitVectorInsert(context, op.Rd, index, sizeI);
  408. }
  409. if (op.RegisterSize == RegisterSize.Simd64)
  410. {
  411. EmitVectorZeroUpper(context, op.Rd);
  412. }
  413. }
  414. private static void EmitFcvtz(ILEmitterCtx context, bool signed, bool scalar)
  415. {
  416. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  417. int sizeF = op.Size & 1;
  418. int sizeI = sizeF + 2;
  419. int fBits = GetFBits(context);
  420. int bytes = op.GetBitsCount() >> 3;
  421. int elems = !scalar ? bytes >> sizeI : 1;
  422. for (int index = 0; index < elems; index++)
  423. {
  424. EmitVectorExtractF(context, op.Rn, index, sizeF);
  425. EmitF2iFBitsMul(context, sizeF, fBits);
  426. if (sizeF == 0)
  427. {
  428. VectorHelper.EmitCall(context, signed
  429. ? nameof(VectorHelper.SatF32ToS32)
  430. : nameof(VectorHelper.SatF32ToU32));
  431. context.Emit(OpCodes.Conv_U8);
  432. }
  433. else /* if (sizeF == 1) */
  434. {
  435. VectorHelper.EmitCall(context, signed
  436. ? nameof(VectorHelper.SatF64ToS64)
  437. : nameof(VectorHelper.SatF64ToU64));
  438. }
  439. if (scalar)
  440. {
  441. EmitVectorZeroAll(context, op.Rd);
  442. }
  443. EmitVectorInsert(context, op.Rd, index, sizeI);
  444. }
  445. if (op.RegisterSize == RegisterSize.Simd64)
  446. {
  447. EmitVectorZeroUpper(context, op.Rd);
  448. }
  449. }
  450. private static void EmitFcvt_s_Gp(ILEmitterCtx context, Action emit)
  451. {
  452. EmitFcvt___Gp(context, emit, true);
  453. }
  454. private static void EmitFcvt_u_Gp(ILEmitterCtx context, Action emit)
  455. {
  456. EmitFcvt___Gp(context, emit, false);
  457. }
  458. private static void EmitFcvt___Gp(ILEmitterCtx context, Action emit, bool signed)
  459. {
  460. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  461. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  462. emit();
  463. if (signed)
  464. {
  465. EmitScalarFcvts(context, op.Size, 0);
  466. }
  467. else
  468. {
  469. EmitScalarFcvtu(context, op.Size, 0);
  470. }
  471. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  472. {
  473. context.Emit(OpCodes.Conv_U8);
  474. }
  475. context.EmitStintzr(op.Rd);
  476. }
  477. private static void EmitFcvtzs_Gp_Fixed(ILEmitterCtx context)
  478. {
  479. EmitFcvtz__Gp_Fixed(context, true);
  480. }
  481. private static void EmitFcvtzu_Gp_Fixed(ILEmitterCtx context)
  482. {
  483. EmitFcvtz__Gp_Fixed(context, false);
  484. }
  485. private static void EmitFcvtz__Gp_Fixed(ILEmitterCtx context, bool signed)
  486. {
  487. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  488. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  489. if (signed)
  490. {
  491. EmitScalarFcvts(context, op.Size, op.FBits);
  492. }
  493. else
  494. {
  495. EmitScalarFcvtu(context, op.Size, op.FBits);
  496. }
  497. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  498. {
  499. context.Emit(OpCodes.Conv_U8);
  500. }
  501. context.EmitStintzr(op.Rd);
  502. }
  503. private static void EmitVectorCvtf(ILEmitterCtx context, bool signed)
  504. {
  505. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  506. int sizeF = op.Size & 1;
  507. int sizeI = sizeF + 2;
  508. int fBits = GetFBits(context);
  509. int bytes = op.GetBitsCount() >> 3;
  510. int elems = bytes >> sizeI;
  511. for (int index = 0; index < elems; index++)
  512. {
  513. EmitVectorExtract(context, op.Rn, index, sizeI, signed);
  514. if (!signed)
  515. {
  516. context.Emit(OpCodes.Conv_R_Un);
  517. }
  518. EmitFloatCast(context, sizeF);
  519. EmitI2fFBitsMul(context, sizeF, fBits);
  520. EmitVectorInsertF(context, op.Rd, index, sizeF);
  521. }
  522. if (op.RegisterSize == RegisterSize.Simd64)
  523. {
  524. EmitVectorZeroUpper(context, op.Rd);
  525. }
  526. }
  527. private static int GetFBits(ILEmitterCtx context)
  528. {
  529. if (context.CurrOp is OpCodeSimdShImm64 op)
  530. {
  531. return GetImmShr(op);
  532. }
  533. return 0;
  534. }
  535. private static void EmitFloatCast(ILEmitterCtx context, int size)
  536. {
  537. if (size == 0)
  538. {
  539. context.Emit(OpCodes.Conv_R4);
  540. }
  541. else if (size == 1)
  542. {
  543. context.Emit(OpCodes.Conv_R8);
  544. }
  545. else
  546. {
  547. throw new ArgumentOutOfRangeException(nameof(size));
  548. }
  549. }
  550. private static void EmitScalarFcvts(ILEmitterCtx context, int size, int fBits)
  551. {
  552. if (size < 0 || size > 1)
  553. {
  554. throw new ArgumentOutOfRangeException(nameof(size));
  555. }
  556. EmitF2iFBitsMul(context, size, fBits);
  557. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  558. {
  559. if (size == 0)
  560. {
  561. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS32));
  562. }
  563. else /* if (size == 1) */
  564. {
  565. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS32));
  566. }
  567. }
  568. else
  569. {
  570. if (size == 0)
  571. {
  572. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS64));
  573. }
  574. else /* if (size == 1) */
  575. {
  576. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS64));
  577. }
  578. }
  579. }
  580. private static void EmitScalarFcvtu(ILEmitterCtx context, int size, int fBits)
  581. {
  582. if (size < 0 || size > 1)
  583. {
  584. throw new ArgumentOutOfRangeException(nameof(size));
  585. }
  586. EmitF2iFBitsMul(context, size, fBits);
  587. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  588. {
  589. if (size == 0)
  590. {
  591. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU32));
  592. }
  593. else /* if (size == 1) */
  594. {
  595. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU32));
  596. }
  597. }
  598. else
  599. {
  600. if (size == 0)
  601. {
  602. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU64));
  603. }
  604. else /* if (size == 1) */
  605. {
  606. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU64));
  607. }
  608. }
  609. }
  610. private static void EmitF2iFBitsMul(ILEmitterCtx context, int size, int fBits)
  611. {
  612. if (fBits != 0)
  613. {
  614. if (size == 0)
  615. {
  616. context.EmitLdc_R4(MathF.Pow(2f, fBits));
  617. }
  618. else if (size == 1)
  619. {
  620. context.EmitLdc_R8(Math.Pow(2d, fBits));
  621. }
  622. else
  623. {
  624. throw new ArgumentOutOfRangeException(nameof(size));
  625. }
  626. context.Emit(OpCodes.Mul);
  627. }
  628. }
  629. private static void EmitI2fFBitsMul(ILEmitterCtx context, int size, int fBits)
  630. {
  631. if (fBits != 0)
  632. {
  633. if (size == 0)
  634. {
  635. context.EmitLdc_R4(1f / MathF.Pow(2f, fBits));
  636. }
  637. else if (size == 1)
  638. {
  639. context.EmitLdc_R8(1d / Math.Pow(2d, fBits));
  640. }
  641. else
  642. {
  643. throw new ArgumentOutOfRangeException(nameof(size));
  644. }
  645. context.Emit(OpCodes.Mul);
  646. }
  647. }
  648. private static void EmitSse41Fcvt_Signed(ILEmitterCtx context, RoundMode roundMode, bool scalar)
  649. {
  650. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  651. // sizeF == ((OpCodeSimdShImm64)op).Size - 2
  652. int sizeF = op.Size & 1;
  653. if (sizeF == 0)
  654. {
  655. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  656. Type[] typesRndCvt = new Type[] { typeof(Vector128<float>) };
  657. Type[] typesSav = new Type[] { typeof(int) };
  658. context.EmitLdvec(op.Rn);
  659. context.EmitLdvec(op.Rn);
  660. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), types));
  661. context.EmitLdvec(op.Rn);
  662. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
  663. if (op is OpCodeSimdShImm64 fixedOp)
  664. {
  665. int fBits = GetImmShr(fixedOp);
  666. // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
  667. int fpScaled = 0x40000000 + (fBits - 1) * 0x800000;
  668. context.EmitLdc_I4(fpScaled);
  669. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  670. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types));
  671. }
  672. context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt));
  673. context.EmitStvectmp();
  674. context.EmitLdvectmp();
  675. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
  676. context.EmitLdvectmp();
  677. context.EmitLdc_I4(0x4F000000); // 2.14748365E9f (2147483648)
  678. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  679. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThanOrEqual), types));
  680. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), types));
  681. context.EmitStvec(op.Rd);
  682. if (scalar)
  683. {
  684. EmitVectorZero32_128(context, op.Rd);
  685. }
  686. else if (op.RegisterSize == RegisterSize.Simd64)
  687. {
  688. EmitVectorZeroUpper(context, op.Rd);
  689. }
  690. }
  691. else /* if (sizeF == 1) */
  692. {
  693. Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  694. Type[] typesRndCvt = new Type[] { typeof(Vector128<double>) };
  695. Type[] typesSv = new Type[] { typeof(long), typeof(long) };
  696. Type[] typesSav = new Type[] { typeof(long) };
  697. context.EmitLdvec(op.Rn);
  698. context.EmitLdvec(op.Rn);
  699. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareOrdered), types));
  700. context.EmitLdvec(op.Rn);
  701. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
  702. if (op is OpCodeSimdShImm64 fixedOp)
  703. {
  704. int fBits = GetImmShr(fixedOp);
  705. // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
  706. long fpScaled = 0x4000000000000000L + (fBits - 1) * 0x10000000000000L;
  707. context.EmitLdc_I8(fpScaled);
  708. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  709. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types));
  710. }
  711. context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt));
  712. context.EmitStvectmp();
  713. if (!scalar)
  714. {
  715. context.EmitLdvectmp();
  716. context.EmitLdvectmp();
  717. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
  718. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  719. }
  720. else
  721. {
  722. context.EmitLdc_I8(0L);
  723. }
  724. context.EmitLdvectmp();
  725. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  726. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
  727. context.EmitLdvectmp();
  728. context.EmitLdc_I8(0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
  729. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  730. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThanOrEqual), types));
  731. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), types));
  732. context.EmitStvec(op.Rd);
  733. if (scalar)
  734. {
  735. EmitVectorZeroUpper(context, op.Rd);
  736. }
  737. }
  738. }
  739. private static void EmitSse41Fcvt_Unsigned(ILEmitterCtx context, RoundMode roundMode, bool scalar)
  740. {
  741. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  742. // sizeF == ((OpCodeSimdShImm64)op).Size - 2
  743. int sizeF = op.Size & 1;
  744. if (sizeF == 0)
  745. {
  746. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  747. Type[] typesAdd = new Type[] { typeof(Vector128<int>), typeof(Vector128<int>) };
  748. Type[] typesRndCvt = new Type[] { typeof(Vector128<float>) };
  749. Type[] typesSav = new Type[] { typeof(int) };
  750. context.EmitLdvec(op.Rn);
  751. context.EmitLdvec(op.Rn);
  752. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), types));
  753. context.EmitLdvec(op.Rn);
  754. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
  755. if (op is OpCodeSimdShImm64 fixedOp)
  756. {
  757. int fBits = GetImmShr(fixedOp);
  758. // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
  759. int fpScaled = 0x40000000 + (fBits - 1) * 0x800000;
  760. context.EmitLdc_I4(fpScaled);
  761. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  762. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types));
  763. }
  764. context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt));
  765. context.Emit(OpCodes.Dup);
  766. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  767. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThan), types));
  768. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
  769. context.EmitStvectmp();
  770. context.EmitLdvectmp();
  771. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
  772. context.EmitLdvectmp();
  773. context.EmitLdc_I4(0x4F000000); // 2.14748365E9f (2147483648)
  774. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  775. context.EmitStvectmp2();
  776. context.EmitLdvectmp2();
  777. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), types));
  778. context.Emit(OpCodes.Dup);
  779. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  780. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThan), types));
  781. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
  782. context.EmitStvectmp();
  783. context.EmitLdvectmp();
  784. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
  785. context.EmitLdvectmp();
  786. context.EmitLdvectmp2();
  787. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThanOrEqual), types));
  788. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), types));
  789. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  790. context.EmitStvec(op.Rd);
  791. if (scalar)
  792. {
  793. EmitVectorZero32_128(context, op.Rd);
  794. }
  795. else if (op.RegisterSize == RegisterSize.Simd64)
  796. {
  797. EmitVectorZeroUpper(context, op.Rd);
  798. }
  799. }
  800. else /* if (sizeF == 1) */
  801. {
  802. Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  803. Type[] typesAdd = new Type[] { typeof(Vector128<long>), typeof(Vector128<long>) };
  804. Type[] typesRndCvt = new Type[] { typeof(Vector128<double>) };
  805. Type[] typesSv = new Type[] { typeof(long), typeof(long) };
  806. Type[] typesSav = new Type[] { typeof(long) };
  807. context.EmitLdvec(op.Rn);
  808. context.EmitLdvec(op.Rn);
  809. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareOrdered), types));
  810. context.EmitLdvec(op.Rn);
  811. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
  812. if (op is OpCodeSimdShImm64 fixedOp)
  813. {
  814. int fBits = GetImmShr(fixedOp);
  815. // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
  816. long fpScaled = 0x4000000000000000L + (fBits - 1) * 0x10000000000000L;
  817. context.EmitLdc_I8(fpScaled);
  818. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  819. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types));
  820. }
  821. context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt));
  822. context.Emit(OpCodes.Dup);
  823. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  824. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), types));
  825. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
  826. context.EmitStvectmp();
  827. if (!scalar)
  828. {
  829. context.EmitLdvectmp();
  830. context.EmitLdvectmp();
  831. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
  832. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  833. }
  834. else
  835. {
  836. context.EmitLdc_I8(0L);
  837. }
  838. context.EmitLdvectmp();
  839. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  840. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
  841. context.EmitLdvectmp();
  842. context.EmitLdc_I8(0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
  843. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  844. context.EmitStvectmp2();
  845. context.EmitLdvectmp2();
  846. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), types));
  847. context.Emit(OpCodes.Dup);
  848. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  849. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), types));
  850. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
  851. context.EmitStvectmp();
  852. if (!scalar)
  853. {
  854. context.EmitLdvectmp();
  855. context.EmitLdvectmp();
  856. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
  857. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  858. }
  859. else
  860. {
  861. context.EmitLdc_I8(0L);
  862. }
  863. context.EmitLdvectmp();
  864. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
  865. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
  866. context.EmitLdvectmp();
  867. context.EmitLdvectmp2();
  868. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThanOrEqual), types));
  869. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), types));
  870. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  871. context.EmitStvec(op.Rd);
  872. if (scalar)
  873. {
  874. EmitVectorZeroUpper(context, op.Rd);
  875. }
  876. }
  877. }
  878. private static string GetSse41NameRnd(RoundMode roundMode)
  879. {
  880. switch (roundMode)
  881. {
  882. case RoundMode.ToNearest:
  883. return nameof(Sse41.RoundToNearestInteger); // even
  884. case RoundMode.TowardsMinusInfinity:
  885. return nameof(Sse41.RoundToNegativeInfinity);
  886. case RoundMode.TowardsPlusInfinity:
  887. return nameof(Sse41.RoundToPositiveInfinity);
  888. case RoundMode.TowardsZero:
  889. return nameof(Sse41.RoundToZero);
  890. default: throw new ArgumentException(nameof(roundMode));
  891. }
  892. }
  893. }
  894. }