AInstEmitSimdCvt.cs 18 KB

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