AInstEmitSimdCvt.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 Fcvtps_Gp(AILEmitterCtx Context)
  84. {
  85. EmitFcvt_s_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Ceiling)));
  86. }
  87. public static void Fcvtpu_Gp(AILEmitterCtx Context)
  88. {
  89. EmitFcvt_u_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Ceiling)));
  90. }
  91. public static void Fcvtzs_Gp(AILEmitterCtx Context)
  92. {
  93. EmitFcvt_s_Gp(Context, () => { });
  94. }
  95. public static void Fcvtzs_Gp_Fix(AILEmitterCtx Context)
  96. {
  97. EmitFcvtzs_Gp_Fix(Context);
  98. }
  99. public static void Fcvtzs_S(AILEmitterCtx Context)
  100. {
  101. EmitScalarFcvtzs(Context);
  102. }
  103. public static void Fcvtzs_V(AILEmitterCtx Context)
  104. {
  105. EmitVectorFcvtzs(Context);
  106. }
  107. public static void Fcvtzu_Gp(AILEmitterCtx Context)
  108. {
  109. EmitFcvt_u_Gp(Context, () => { });
  110. }
  111. public static void Fcvtzu_Gp_Fix(AILEmitterCtx Context)
  112. {
  113. EmitFcvtzu_Gp_Fix(Context);
  114. }
  115. public static void Fcvtzu_S(AILEmitterCtx Context)
  116. {
  117. EmitScalarFcvtzu(Context);
  118. }
  119. public static void Fcvtzu_V(AILEmitterCtx Context)
  120. {
  121. EmitVectorFcvtzu(Context);
  122. }
  123. public static void Scvtf_Gp(AILEmitterCtx Context)
  124. {
  125. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  126. Context.EmitLdintzr(Op.Rn);
  127. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  128. {
  129. Context.Emit(OpCodes.Conv_U4);
  130. }
  131. EmitFloatCast(Context, Op.Size);
  132. EmitScalarSetF(Context, Op.Rd, Op.Size);
  133. }
  134. public static void Scvtf_S(AILEmitterCtx Context)
  135. {
  136. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  137. EmitVectorExtractSx(Context, Op.Rn, 0, Op.Size + 2);
  138. EmitFloatCast(Context, Op.Size);
  139. EmitScalarSetF(Context, Op.Rd, Op.Size);
  140. }
  141. public static void Scvtf_V(AILEmitterCtx Context)
  142. {
  143. EmitVectorCvtf(Context, Signed: true);
  144. }
  145. public static void Ucvtf_Gp(AILEmitterCtx Context)
  146. {
  147. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  148. Context.EmitLdintzr(Op.Rn);
  149. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  150. {
  151. Context.Emit(OpCodes.Conv_U4);
  152. }
  153. Context.Emit(OpCodes.Conv_R_Un);
  154. EmitFloatCast(Context, Op.Size);
  155. EmitScalarSetF(Context, Op.Rd, Op.Size);
  156. }
  157. public static void Ucvtf_S(AILEmitterCtx Context)
  158. {
  159. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  160. EmitVectorExtractZx(Context, Op.Rn, 0, Op.Size + 2);
  161. Context.Emit(OpCodes.Conv_R_Un);
  162. EmitFloatCast(Context, Op.Size);
  163. EmitScalarSetF(Context, Op.Rd, Op.Size);
  164. }
  165. public static void Ucvtf_V(AILEmitterCtx Context)
  166. {
  167. EmitVectorCvtf(Context, Signed: false);
  168. }
  169. private static int GetFBits(AILEmitterCtx Context)
  170. {
  171. if (Context.CurrOp is AOpCodeSimdShImm Op)
  172. {
  173. return GetImmShr(Op);
  174. }
  175. return 0;
  176. }
  177. private static void EmitFloatCast(AILEmitterCtx Context, int Size)
  178. {
  179. if (Size == 0)
  180. {
  181. Context.Emit(OpCodes.Conv_R4);
  182. }
  183. else if (Size == 1)
  184. {
  185. Context.Emit(OpCodes.Conv_R8);
  186. }
  187. else
  188. {
  189. throw new ArgumentOutOfRangeException(nameof(Size));
  190. }
  191. }
  192. private static void EmitFcvt_s_Gp(AILEmitterCtx Context, Action Emit)
  193. {
  194. EmitFcvt___Gp(Context, Emit, true);
  195. }
  196. private static void EmitFcvt_u_Gp(AILEmitterCtx Context, Action Emit)
  197. {
  198. EmitFcvt___Gp(Context, Emit, false);
  199. }
  200. private static void EmitFcvt___Gp(AILEmitterCtx Context, Action Emit, bool Signed)
  201. {
  202. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  203. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  204. Emit();
  205. if (Signed)
  206. {
  207. EmitScalarFcvts(Context, Op.Size, 0);
  208. }
  209. else
  210. {
  211. EmitScalarFcvtu(Context, Op.Size, 0);
  212. }
  213. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  214. {
  215. Context.Emit(OpCodes.Conv_U8);
  216. }
  217. Context.EmitStintzr(Op.Rd);
  218. }
  219. private static void EmitFcvtzs_Gp_Fix(AILEmitterCtx Context)
  220. {
  221. EmitFcvtz__Gp_Fix(Context, true);
  222. }
  223. private static void EmitFcvtzu_Gp_Fix(AILEmitterCtx Context)
  224. {
  225. EmitFcvtz__Gp_Fix(Context, false);
  226. }
  227. private static void EmitFcvtz__Gp_Fix(AILEmitterCtx Context, bool Signed)
  228. {
  229. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  230. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  231. if (Signed)
  232. {
  233. EmitScalarFcvts(Context, Op.Size, Op.FBits);
  234. }
  235. else
  236. {
  237. EmitScalarFcvtu(Context, Op.Size, Op.FBits);
  238. }
  239. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  240. {
  241. Context.Emit(OpCodes.Conv_U8);
  242. }
  243. Context.EmitStintzr(Op.Rd);
  244. }
  245. private static void EmitVectorScvtf(AILEmitterCtx Context)
  246. {
  247. EmitVectorCvtf(Context, true);
  248. }
  249. private static void EmitVectorUcvtf(AILEmitterCtx Context)
  250. {
  251. EmitVectorCvtf(Context, false);
  252. }
  253. private static void EmitVectorCvtf(AILEmitterCtx Context, bool Signed)
  254. {
  255. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  256. int SizeF = Op.Size & 1;
  257. int SizeI = SizeF + 2;
  258. int FBits = GetFBits(Context);
  259. int Bytes = Op.GetBitsCount() >> 3;
  260. for (int Index = 0; Index < (Bytes >> SizeI); Index++)
  261. {
  262. EmitVectorExtract(Context, Op.Rn, Index, SizeI, Signed);
  263. if (!Signed)
  264. {
  265. Context.Emit(OpCodes.Conv_R_Un);
  266. }
  267. Context.Emit(SizeF == 0
  268. ? OpCodes.Conv_R4
  269. : OpCodes.Conv_R8);
  270. EmitI2fFBitsMul(Context, SizeF, FBits);
  271. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  272. }
  273. if (Op.RegisterSize == ARegisterSize.SIMD64)
  274. {
  275. EmitVectorZeroUpper(Context, Op.Rd);
  276. }
  277. }
  278. private static void EmitScalarFcvtzs(AILEmitterCtx Context)
  279. {
  280. EmitScalarFcvtz(Context, true);
  281. }
  282. private static void EmitScalarFcvtzu(AILEmitterCtx Context)
  283. {
  284. EmitScalarFcvtz(Context, false);
  285. }
  286. private static void EmitScalarFcvtz(AILEmitterCtx Context, bool Signed)
  287. {
  288. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  289. int SizeF = Op.Size & 1;
  290. int SizeI = SizeF + 2;
  291. int FBits = GetFBits(Context);
  292. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  293. EmitF2iFBitsMul(Context, SizeF, FBits);
  294. if (SizeF == 0)
  295. {
  296. AVectorHelper.EmitCall(Context, Signed
  297. ? nameof(AVectorHelper.SatF32ToS32)
  298. : nameof(AVectorHelper.SatF32ToU32));
  299. }
  300. else /* if (SizeF == 1) */
  301. {
  302. AVectorHelper.EmitCall(Context, Signed
  303. ? nameof(AVectorHelper.SatF64ToS64)
  304. : nameof(AVectorHelper.SatF64ToU64));
  305. }
  306. if (SizeF == 0)
  307. {
  308. Context.Emit(OpCodes.Conv_U8);
  309. }
  310. EmitScalarSet(Context, Op.Rd, SizeI);
  311. }
  312. private static void EmitVectorFcvtzs(AILEmitterCtx Context)
  313. {
  314. EmitVectorFcvtz(Context, true);
  315. }
  316. private static void EmitVectorFcvtzu(AILEmitterCtx Context)
  317. {
  318. EmitVectorFcvtz(Context, false);
  319. }
  320. private static void EmitVectorFcvtz(AILEmitterCtx Context, bool Signed)
  321. {
  322. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  323. int SizeF = Op.Size & 1;
  324. int SizeI = SizeF + 2;
  325. int FBits = GetFBits(Context);
  326. int Bytes = Op.GetBitsCount() >> 3;
  327. for (int Index = 0; Index < (Bytes >> SizeI); Index++)
  328. {
  329. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  330. EmitF2iFBitsMul(Context, SizeF, FBits);
  331. if (SizeF == 0)
  332. {
  333. AVectorHelper.EmitCall(Context, Signed
  334. ? nameof(AVectorHelper.SatF32ToS32)
  335. : nameof(AVectorHelper.SatF32ToU32));
  336. }
  337. else /* if (SizeF == 1) */
  338. {
  339. AVectorHelper.EmitCall(Context, Signed
  340. ? nameof(AVectorHelper.SatF64ToS64)
  341. : nameof(AVectorHelper.SatF64ToU64));
  342. }
  343. if (SizeF == 0)
  344. {
  345. Context.Emit(OpCodes.Conv_U8);
  346. }
  347. EmitVectorInsert(Context, Op.Rd, Index, SizeI);
  348. }
  349. if (Op.RegisterSize == ARegisterSize.SIMD64)
  350. {
  351. EmitVectorZeroUpper(Context, Op.Rd);
  352. }
  353. }
  354. private static void EmitScalarFcvts(AILEmitterCtx Context, int Size, int FBits)
  355. {
  356. if (Size < 0 || Size > 1)
  357. {
  358. throw new ArgumentOutOfRangeException(nameof(Size));
  359. }
  360. EmitF2iFBitsMul(Context, Size, FBits);
  361. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  362. {
  363. if (Size == 0)
  364. {
  365. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF32ToS32));
  366. }
  367. else /* if (Size == 1) */
  368. {
  369. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF64ToS32));
  370. }
  371. }
  372. else
  373. {
  374. if (Size == 0)
  375. {
  376. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF32ToS64));
  377. }
  378. else /* if (Size == 1) */
  379. {
  380. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF64ToS64));
  381. }
  382. }
  383. }
  384. private static void EmitScalarFcvtu(AILEmitterCtx Context, int Size, int FBits)
  385. {
  386. if (Size < 0 || Size > 1)
  387. {
  388. throw new ArgumentOutOfRangeException(nameof(Size));
  389. }
  390. EmitF2iFBitsMul(Context, Size, FBits);
  391. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  392. {
  393. if (Size == 0)
  394. {
  395. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF32ToU32));
  396. }
  397. else /* if (Size == 1) */
  398. {
  399. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF64ToU32));
  400. }
  401. }
  402. else
  403. {
  404. if (Size == 0)
  405. {
  406. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF32ToU64));
  407. }
  408. else /* if (Size == 1) */
  409. {
  410. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.SatF64ToU64));
  411. }
  412. }
  413. }
  414. private static void EmitF2iFBitsMul(AILEmitterCtx Context, int Size, int FBits)
  415. {
  416. if (FBits != 0)
  417. {
  418. if (Size == 0)
  419. {
  420. Context.EmitLdc_R4(MathF.Pow(2, FBits));
  421. }
  422. else if (Size == 1)
  423. {
  424. Context.EmitLdc_R8(Math.Pow(2, FBits));
  425. }
  426. else
  427. {
  428. throw new ArgumentOutOfRangeException(nameof(Size));
  429. }
  430. Context.Emit(OpCodes.Mul);
  431. }
  432. }
  433. private static void EmitI2fFBitsMul(AILEmitterCtx Context, int Size, int FBits)
  434. {
  435. if (FBits != 0)
  436. {
  437. if (Size == 0)
  438. {
  439. Context.EmitLdc_R4(1f / MathF.Pow(2, FBits));
  440. }
  441. else if (Size == 1)
  442. {
  443. Context.EmitLdc_R8(1 / Math.Pow(2, FBits));
  444. }
  445. else
  446. {
  447. throw new ArgumentOutOfRangeException(nameof(Size));
  448. }
  449. Context.Emit(OpCodes.Mul);
  450. }
  451. }
  452. }
  453. }