AInstEmitSimdCvt.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using ChocolArm64.Decoder;
  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.Instruction.AInstEmitSimdHelper;
  9. namespace ChocolArm64.Instruction
  10. {
  11. static partial class AInstEmit
  12. {
  13. public static void Fcvt_S(AILEmitterCtx Context)
  14. {
  15. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  16. if (AOptimizations.UseSse2)
  17. {
  18. if (Op.Size == 1 && Op.Opc == 0)
  19. {
  20. //Double -> Single.
  21. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleZero));
  22. EmitLdvecWithCastToDouble(Context, Op.Rn);
  23. Type[] Types = new Type[] { typeof(Vector128<float>), typeof(Vector128<double>) };
  24. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertScalarToVector128Single), Types));
  25. Context.EmitStvec(Op.Rd);
  26. }
  27. else if (Op.Size == 0 && Op.Opc == 1)
  28. {
  29. //Single -> Double.
  30. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorDoubleZero));
  31. Context.EmitLdvec(Op.Rn);
  32. Type[] Types = new Type[] { typeof(Vector128<double>), typeof(Vector128<float>) };
  33. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertScalarToVector128Double), Types));
  34. EmitStvecWithCastFromDouble(Context, 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(AILEmitterCtx Context)
  50. {
  51. EmitFcvt_s_Gp(Context, () => EmitRoundMathCall(Context, MidpointRounding.AwayFromZero));
  52. }
  53. public static void Fcvtau_Gp(AILEmitterCtx Context)
  54. {
  55. EmitFcvt_u_Gp(Context, () => EmitRoundMathCall(Context, MidpointRounding.AwayFromZero));
  56. }
  57. public static void Fcvtl_V(AILEmitterCtx Context)
  58. {
  59. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  60. int SizeF = Op.Size & 1;
  61. int Elems = 4 >> SizeF;
  62. int Part = Context.CurrOp.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  63. for (int Index = 0; Index < Elems; Index++)
  64. {
  65. if (SizeF == 0)
  66. {
  67. EmitVectorExtractZx(Context, Op.Rn, Part + Index, 1);
  68. Context.Emit(OpCodes.Conv_U2);
  69. Context.EmitCall(typeof(ASoftFloat), nameof(ASoftFloat.ConvertHalfToSingle));
  70. }
  71. else /* if (SizeF == 1) */
  72. {
  73. EmitVectorExtractF(Context, Op.Rn, Part + Index, 0);
  74. Context.Emit(OpCodes.Conv_R8);
  75. }
  76. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  77. }
  78. }
  79. public static void Fcvtms_Gp(AILEmitterCtx Context)
  80. {
  81. EmitFcvt_s_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Floor)));
  82. }
  83. public static void Fcvtmu_Gp(AILEmitterCtx Context)
  84. {
  85. EmitFcvt_u_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Floor)));
  86. }
  87. public static void Fcvtn_V(AILEmitterCtx Context)
  88. {
  89. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  90. int SizeF = Op.Size & 1;
  91. int Elems = 4 >> SizeF;
  92. int Part = Context.CurrOp.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  93. for (int Index = 0; Index < Elems; Index++)
  94. {
  95. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  96. if (SizeF == 0)
  97. {
  98. //TODO: This need the half precision floating point type,
  99. //that is not yet supported on .NET. We should probably
  100. //do our own implementation on the meantime.
  101. throw new NotImplementedException();
  102. }
  103. else /* if (SizeF == 1) */
  104. {
  105. Context.Emit(OpCodes.Conv_R4);
  106. EmitVectorInsertF(Context, Op.Rd, Part + Index, 0);
  107. }
  108. }
  109. if (Op.RegisterSize == ARegisterSize.SIMD64)
  110. {
  111. EmitVectorZeroUpper(Context, Op.Rd);
  112. }
  113. }
  114. public static void Fcvtns_S(AILEmitterCtx Context)
  115. {
  116. EmitFcvtn(Context, Signed: true, Scalar: true);
  117. }
  118. public static void Fcvtns_V(AILEmitterCtx Context)
  119. {
  120. EmitFcvtn(Context, Signed: true, Scalar: false);
  121. }
  122. public static void Fcvtnu_S(AILEmitterCtx Context)
  123. {
  124. EmitFcvtn(Context, Signed: false, Scalar: true);
  125. }
  126. public static void Fcvtnu_V(AILEmitterCtx Context)
  127. {
  128. EmitFcvtn(Context, Signed: false, Scalar: false);
  129. }
  130. public static void Fcvtps_Gp(AILEmitterCtx Context)
  131. {
  132. EmitFcvt_s_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Ceiling)));
  133. }
  134. public static void Fcvtpu_Gp(AILEmitterCtx Context)
  135. {
  136. EmitFcvt_u_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Ceiling)));
  137. }
  138. public static void Fcvtzs_Gp(AILEmitterCtx Context)
  139. {
  140. EmitFcvt_s_Gp(Context, () => { });
  141. }
  142. public static void Fcvtzs_Gp_Fix(AILEmitterCtx Context)
  143. {
  144. EmitFcvtzs_Gp_Fix(Context);
  145. }
  146. public static void Fcvtzs_S(AILEmitterCtx Context)
  147. {
  148. EmitScalarFcvtzs(Context);
  149. }
  150. public static void Fcvtzs_V(AILEmitterCtx Context)
  151. {
  152. EmitVectorFcvtzs(Context);
  153. }
  154. public static void Fcvtzu_Gp(AILEmitterCtx Context)
  155. {
  156. EmitFcvt_u_Gp(Context, () => { });
  157. }
  158. public static void Fcvtzu_Gp_Fix(AILEmitterCtx Context)
  159. {
  160. EmitFcvtzu_Gp_Fix(Context);
  161. }
  162. public static void Fcvtzu_S(AILEmitterCtx Context)
  163. {
  164. EmitScalarFcvtzu(Context);
  165. }
  166. public static void Fcvtzu_V(AILEmitterCtx Context)
  167. {
  168. EmitVectorFcvtzu(Context);
  169. }
  170. public static void Scvtf_Gp(AILEmitterCtx Context)
  171. {
  172. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  173. Context.EmitLdintzr(Op.Rn);
  174. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  175. {
  176. Context.Emit(OpCodes.Conv_U4);
  177. }
  178. EmitFloatCast(Context, Op.Size);
  179. EmitScalarSetF(Context, Op.Rd, Op.Size);
  180. }
  181. public static void Scvtf_S(AILEmitterCtx Context)
  182. {
  183. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  184. EmitVectorExtractSx(Context, Op.Rn, 0, Op.Size + 2);
  185. EmitFloatCast(Context, Op.Size);
  186. EmitScalarSetF(Context, Op.Rd, Op.Size);
  187. }
  188. public static void Scvtf_V(AILEmitterCtx Context)
  189. {
  190. EmitVectorCvtf(Context, Signed: true);
  191. }
  192. public static void Ucvtf_Gp(AILEmitterCtx Context)
  193. {
  194. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  195. Context.EmitLdintzr(Op.Rn);
  196. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  197. {
  198. Context.Emit(OpCodes.Conv_U4);
  199. }
  200. Context.Emit(OpCodes.Conv_R_Un);
  201. EmitFloatCast(Context, Op.Size);
  202. EmitScalarSetF(Context, Op.Rd, Op.Size);
  203. }
  204. public static void Ucvtf_S(AILEmitterCtx Context)
  205. {
  206. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  207. EmitVectorExtractZx(Context, Op.Rn, 0, Op.Size + 2);
  208. Context.Emit(OpCodes.Conv_R_Un);
  209. EmitFloatCast(Context, Op.Size);
  210. EmitScalarSetF(Context, Op.Rd, Op.Size);
  211. }
  212. public static void Ucvtf_V(AILEmitterCtx Context)
  213. {
  214. EmitVectorCvtf(Context, Signed: false);
  215. }
  216. private static int GetFBits(AILEmitterCtx Context)
  217. {
  218. if (Context.CurrOp is AOpCodeSimdShImm Op)
  219. {
  220. return GetImmShr(Op);
  221. }
  222. return 0;
  223. }
  224. private static void EmitFloatCast(AILEmitterCtx Context, int Size)
  225. {
  226. if (Size == 0)
  227. {
  228. Context.Emit(OpCodes.Conv_R4);
  229. }
  230. else if (Size == 1)
  231. {
  232. Context.Emit(OpCodes.Conv_R8);
  233. }
  234. else
  235. {
  236. throw new ArgumentOutOfRangeException(nameof(Size));
  237. }
  238. }
  239. private static void EmitFcvtn(AILEmitterCtx Context, bool Signed, bool Scalar)
  240. {
  241. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  242. int SizeF = Op.Size & 1;
  243. int SizeI = SizeF + 2;
  244. int Bytes = Op.GetBitsCount() >> 3;
  245. int Elems = !Scalar ? Bytes >> SizeI : 1;
  246. if (Scalar && (SizeF == 0))
  247. {
  248. EmitVectorZeroLowerTmp(Context);
  249. }
  250. for (int Index = 0; Index < Elems; Index++)
  251. {
  252. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  253. EmitRoundMathCall(Context, MidpointRounding.ToEven);
  254. if (SizeF == 0)
  255. {
  256. AVectorHelper.EmitCall(Context, Signed
  257. ? nameof(AVectorHelper.SatF32ToS32)
  258. : nameof(AVectorHelper.SatF32ToU32));
  259. Context.Emit(OpCodes.Conv_U8);
  260. }
  261. else /* if (SizeF == 1) */
  262. {
  263. AVectorHelper.EmitCall(Context, Signed
  264. ? nameof(AVectorHelper.SatF64ToS64)
  265. : nameof(AVectorHelper.SatF64ToU64));
  266. }
  267. EmitVectorInsertTmp(Context, Index, SizeI);
  268. }
  269. Context.EmitLdvectmp();
  270. Context.EmitStvec(Op.Rd);
  271. if ((Op.RegisterSize == ARegisterSize.SIMD64) || Scalar)
  272. {
  273. EmitVectorZeroUpper(Context, Op.Rd);
  274. }
  275. }
  276. private static void EmitFcvt_s_Gp(AILEmitterCtx Context, Action Emit)
  277. {
  278. EmitFcvt___Gp(Context, Emit, true);
  279. }
  280. private static void EmitFcvt_u_Gp(AILEmitterCtx Context, Action Emit)
  281. {
  282. EmitFcvt___Gp(Context, Emit, false);
  283. }
  284. private static void EmitFcvt___Gp(AILEmitterCtx Context, Action Emit, bool Signed)
  285. {
  286. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  287. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  288. Emit();
  289. if (Signed)
  290. {
  291. EmitScalarFcvts(Context, Op.Size, 0);
  292. }
  293. else
  294. {
  295. EmitScalarFcvtu(Context, Op.Size, 0);
  296. }
  297. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  298. {
  299. Context.Emit(OpCodes.Conv_U8);
  300. }
  301. Context.EmitStintzr(Op.Rd);
  302. }
  303. private static void EmitFcvtzs_Gp_Fix(AILEmitterCtx Context)
  304. {
  305. EmitFcvtz__Gp_Fix(Context, true);
  306. }
  307. private static void EmitFcvtzu_Gp_Fix(AILEmitterCtx Context)
  308. {
  309. EmitFcvtz__Gp_Fix(Context, false);
  310. }
  311. private static void EmitFcvtz__Gp_Fix(AILEmitterCtx Context, bool Signed)
  312. {
  313. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  314. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  315. if (Signed)
  316. {
  317. EmitScalarFcvts(Context, Op.Size, Op.FBits);
  318. }
  319. else
  320. {
  321. EmitScalarFcvtu(Context, Op.Size, Op.FBits);
  322. }
  323. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  324. {
  325. Context.Emit(OpCodes.Conv_U8);
  326. }
  327. Context.EmitStintzr(Op.Rd);
  328. }
  329. private static void EmitVectorScvtf(AILEmitterCtx Context)
  330. {
  331. EmitVectorCvtf(Context, true);
  332. }
  333. private static void EmitVectorUcvtf(AILEmitterCtx Context)
  334. {
  335. EmitVectorCvtf(Context, false);
  336. }
  337. private static void EmitVectorCvtf(AILEmitterCtx Context, bool Signed)
  338. {
  339. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  340. int SizeF = Op.Size & 1;
  341. int SizeI = SizeF + 2;
  342. int FBits = GetFBits(Context);
  343. int Bytes = Op.GetBitsCount() >> 3;
  344. for (int Index = 0; Index < (Bytes >> SizeI); Index++)
  345. {
  346. EmitVectorExtract(Context, Op.Rn, Index, SizeI, Signed);
  347. if (!Signed)
  348. {
  349. Context.Emit(OpCodes.Conv_R_Un);
  350. }
  351. Context.Emit(SizeF == 0
  352. ? OpCodes.Conv_R4
  353. : OpCodes.Conv_R8);
  354. EmitI2fFBitsMul(Context, SizeF, FBits);
  355. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  356. }
  357. if (Op.RegisterSize == ARegisterSize.SIMD64)
  358. {
  359. EmitVectorZeroUpper(Context, Op.Rd);
  360. }
  361. }
  362. private static void EmitScalarFcvtzs(AILEmitterCtx Context)
  363. {
  364. EmitScalarFcvtz(Context, true);
  365. }
  366. private static void EmitScalarFcvtzu(AILEmitterCtx Context)
  367. {
  368. EmitScalarFcvtz(Context, false);
  369. }
  370. private static void EmitScalarFcvtz(AILEmitterCtx Context, bool Signed)
  371. {
  372. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  373. int SizeF = Op.Size & 1;
  374. int SizeI = SizeF + 2;
  375. int FBits = GetFBits(Context);
  376. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  377. EmitF2iFBitsMul(Context, SizeF, FBits);
  378. if (SizeF == 0)
  379. {
  380. AVectorHelper.EmitCall(Context, Signed
  381. ? nameof(AVectorHelper.SatF32ToS32)
  382. : nameof(AVectorHelper.SatF32ToU32));
  383. }
  384. else /* if (SizeF == 1) */
  385. {
  386. AVectorHelper.EmitCall(Context, Signed
  387. ? nameof(AVectorHelper.SatF64ToS64)
  388. : nameof(AVectorHelper.SatF64ToU64));
  389. }
  390. if (SizeF == 0)
  391. {
  392. Context.Emit(OpCodes.Conv_U8);
  393. }
  394. EmitScalarSet(Context, Op.Rd, SizeI);
  395. }
  396. private static void EmitVectorFcvtzs(AILEmitterCtx Context)
  397. {
  398. EmitVectorFcvtz(Context, true);
  399. }
  400. private static void EmitVectorFcvtzu(AILEmitterCtx Context)
  401. {
  402. EmitVectorFcvtz(Context, false);
  403. }
  404. private static void EmitVectorFcvtz(AILEmitterCtx Context, bool Signed)
  405. {
  406. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  407. int SizeF = Op.Size & 1;
  408. int SizeI = SizeF + 2;
  409. int FBits = GetFBits(Context);
  410. int Bytes = Op.GetBitsCount() >> 3;
  411. for (int Index = 0; Index < (Bytes >> SizeI); Index++)
  412. {
  413. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  414. EmitF2iFBitsMul(Context, SizeF, FBits);
  415. if (SizeF == 0)
  416. {
  417. AVectorHelper.EmitCall(Context, Signed
  418. ? nameof(AVectorHelper.SatF32ToS32)
  419. : nameof(AVectorHelper.SatF32ToU32));
  420. }
  421. else /* if (SizeF == 1) */
  422. {
  423. AVectorHelper.EmitCall(Context, Signed
  424. ? nameof(AVectorHelper.SatF64ToS64)
  425. : nameof(AVectorHelper.SatF64ToU64));
  426. }
  427. if (SizeF == 0)
  428. {
  429. Context.Emit(OpCodes.Conv_U8);
  430. }
  431. EmitVectorInsert(Context, Op.Rd, Index, SizeI);
  432. }
  433. if (Op.RegisterSize == ARegisterSize.SIMD64)
  434. {
  435. EmitVectorZeroUpper(Context, Op.Rd);
  436. }
  437. }
  438. private static void EmitScalarFcvts(AILEmitterCtx Context, int Size, int FBits)
  439. {
  440. if (Size < 0 || Size > 1)
  441. {
  442. throw new ArgumentOutOfRangeException(nameof(Size));
  443. }
  444. EmitF2iFBitsMul(Context, Size, FBits);
  445. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  446. {
  447. if (Size == 0)
  448. {
  449. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF32ToS32));
  450. }
  451. else /* if (Size == 1) */
  452. {
  453. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF64ToS32));
  454. }
  455. }
  456. else
  457. {
  458. if (Size == 0)
  459. {
  460. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF32ToS64));
  461. }
  462. else /* if (Size == 1) */
  463. {
  464. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF64ToS64));
  465. }
  466. }
  467. }
  468. private static void EmitScalarFcvtu(AILEmitterCtx Context, int Size, int FBits)
  469. {
  470. if (Size < 0 || Size > 1)
  471. {
  472. throw new ArgumentOutOfRangeException(nameof(Size));
  473. }
  474. EmitF2iFBitsMul(Context, Size, FBits);
  475. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  476. {
  477. if (Size == 0)
  478. {
  479. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF32ToU32));
  480. }
  481. else /* if (Size == 1) */
  482. {
  483. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF64ToU32));
  484. }
  485. }
  486. else
  487. {
  488. if (Size == 0)
  489. {
  490. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF32ToU64));
  491. }
  492. else /* if (Size == 1) */
  493. {
  494. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF64ToU64));
  495. }
  496. }
  497. }
  498. private static void EmitF2iFBitsMul(AILEmitterCtx Context, int Size, int FBits)
  499. {
  500. if (FBits != 0)
  501. {
  502. if (Size == 0)
  503. {
  504. Context.EmitLdc_R4(MathF.Pow(2, FBits));
  505. }
  506. else if (Size == 1)
  507. {
  508. Context.EmitLdc_R8(Math.Pow(2, FBits));
  509. }
  510. else
  511. {
  512. throw new ArgumentOutOfRangeException(nameof(Size));
  513. }
  514. Context.Emit(OpCodes.Mul);
  515. }
  516. }
  517. private static void EmitI2fFBitsMul(AILEmitterCtx Context, int Size, int FBits)
  518. {
  519. if (FBits != 0)
  520. {
  521. if (Size == 0)
  522. {
  523. Context.EmitLdc_R4(1f / MathF.Pow(2, FBits));
  524. }
  525. else if (Size == 1)
  526. {
  527. Context.EmitLdc_R8(1 / Math.Pow(2, FBits));
  528. }
  529. else
  530. {
  531. throw new ArgumentOutOfRangeException(nameof(Size));
  532. }
  533. Context.Emit(OpCodes.Mul);
  534. }
  535. }
  536. }
  537. }