AInstEmitSimdCvt.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. //TODO: This need the half precision floating point type,
  37. //that is not yet supported on .NET. We should probably
  38. //do our own implementation on the meantime.
  39. throw new NotImplementedException();
  40. }
  41. else /* if (SizeF == 1) */
  42. {
  43. EmitVectorExtractF(Context, Op.Rn, Part + Index, 0);
  44. Context.Emit(OpCodes.Conv_R8);
  45. }
  46. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  47. }
  48. }
  49. public static void Fcvtms_Gp(AILEmitterCtx Context)
  50. {
  51. EmitFcvt_s_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Floor)));
  52. }
  53. public static void Fcvtmu_Gp(AILEmitterCtx Context)
  54. {
  55. EmitFcvt_u_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Floor)));
  56. }
  57. public static void Fcvtn_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. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  66. if (SizeF == 0)
  67. {
  68. //TODO: This need the half precision floating point type,
  69. //that is not yet supported on .NET. We should probably
  70. //do our own implementation on the meantime.
  71. throw new NotImplementedException();
  72. }
  73. else /* if (SizeF == 1) */
  74. {
  75. Context.Emit(OpCodes.Conv_R4);
  76. EmitVectorInsertF(Context, Op.Rd, Part + Index, 0);
  77. }
  78. }
  79. if (Op.RegisterSize == ARegisterSize.SIMD64)
  80. {
  81. EmitVectorZeroUpper(Context, Op.Rd);
  82. }
  83. }
  84. public static void Fcvtps_Gp(AILEmitterCtx Context)
  85. {
  86. EmitFcvt_s_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Ceiling)));
  87. }
  88. public static void Fcvtpu_Gp(AILEmitterCtx Context)
  89. {
  90. EmitFcvt_u_Gp(Context, () => EmitUnaryMathCall(Context, nameof(Math.Ceiling)));
  91. }
  92. public static void Fcvtzs_Gp(AILEmitterCtx Context)
  93. {
  94. EmitFcvt_s_Gp(Context, () => { });
  95. }
  96. public static void Fcvtzs_Gp_Fix(AILEmitterCtx Context)
  97. {
  98. EmitFcvtzs_Gp_Fix(Context);
  99. }
  100. public static void Fcvtzs_V(AILEmitterCtx Context)
  101. {
  102. EmitVectorFcvtzs(Context);
  103. }
  104. public static void Fcvtzu_Gp(AILEmitterCtx Context)
  105. {
  106. EmitFcvt_u_Gp(Context, () => { });
  107. }
  108. public static void Fcvtzu_Gp_Fix(AILEmitterCtx Context)
  109. {
  110. EmitFcvtzu_Gp_Fix(Context);
  111. }
  112. public static void Fcvtzu_V(AILEmitterCtx Context)
  113. {
  114. EmitVectorFcvtzu(Context);
  115. }
  116. public static void Scvtf_Gp(AILEmitterCtx Context)
  117. {
  118. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  119. Context.EmitLdintzr(Op.Rn);
  120. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  121. {
  122. Context.Emit(OpCodes.Conv_U4);
  123. }
  124. EmitFloatCast(Context, Op.Size);
  125. EmitScalarSetF(Context, Op.Rd, Op.Size);
  126. }
  127. public static void Scvtf_S(AILEmitterCtx Context)
  128. {
  129. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  130. EmitVectorExtractSx(Context, Op.Rn, 0, Op.Size + 2);
  131. EmitFloatCast(Context, Op.Size);
  132. EmitScalarSetF(Context, Op.Rd, Op.Size);
  133. }
  134. public static void Scvtf_V(AILEmitterCtx Context)
  135. {
  136. EmitVectorCvtf(Context, Signed: true);
  137. }
  138. public static void Ucvtf_Gp(AILEmitterCtx Context)
  139. {
  140. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  141. Context.EmitLdintzr(Op.Rn);
  142. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  143. {
  144. Context.Emit(OpCodes.Conv_U4);
  145. }
  146. Context.Emit(OpCodes.Conv_R_Un);
  147. EmitFloatCast(Context, Op.Size);
  148. EmitScalarSetF(Context, Op.Rd, Op.Size);
  149. }
  150. public static void Ucvtf_S(AILEmitterCtx Context)
  151. {
  152. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  153. EmitVectorExtractZx(Context, Op.Rn, 0, Op.Size + 2);
  154. Context.Emit(OpCodes.Conv_R_Un);
  155. EmitFloatCast(Context, Op.Size);
  156. EmitScalarSetF(Context, Op.Rd, Op.Size);
  157. }
  158. public static void Ucvtf_V(AILEmitterCtx Context)
  159. {
  160. EmitVectorCvtf(Context, Signed: false);
  161. }
  162. private static int GetFBits(AILEmitterCtx Context)
  163. {
  164. if (Context.CurrOp is AOpCodeSimdShImm Op)
  165. {
  166. return GetImmShr(Op);
  167. }
  168. return 0;
  169. }
  170. private static void EmitFloatCast(AILEmitterCtx Context, int Size)
  171. {
  172. if (Size == 0)
  173. {
  174. Context.Emit(OpCodes.Conv_R4);
  175. }
  176. else if (Size == 1)
  177. {
  178. Context.Emit(OpCodes.Conv_R8);
  179. }
  180. else
  181. {
  182. throw new ArgumentOutOfRangeException(nameof(Size));
  183. }
  184. }
  185. private static void EmitFcvt_s_Gp(AILEmitterCtx Context, Action Emit)
  186. {
  187. EmitFcvt___Gp(Context, Emit, true);
  188. }
  189. private static void EmitFcvt_u_Gp(AILEmitterCtx Context, Action Emit)
  190. {
  191. EmitFcvt___Gp(Context, Emit, false);
  192. }
  193. private static void EmitFcvt___Gp(AILEmitterCtx Context, Action Emit, bool Signed)
  194. {
  195. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  196. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  197. Emit();
  198. if (Signed)
  199. {
  200. EmitScalarFcvts(Context, Op.Size, 0);
  201. }
  202. else
  203. {
  204. EmitScalarFcvtu(Context, Op.Size, 0);
  205. }
  206. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  207. {
  208. Context.Emit(OpCodes.Conv_U8);
  209. }
  210. Context.EmitStintzr(Op.Rd);
  211. }
  212. private static void EmitFcvtzs_Gp_Fix(AILEmitterCtx Context)
  213. {
  214. EmitFcvtz__Gp_Fix(Context, true);
  215. }
  216. private static void EmitFcvtzu_Gp_Fix(AILEmitterCtx Context)
  217. {
  218. EmitFcvtz__Gp_Fix(Context, false);
  219. }
  220. private static void EmitFcvtz__Gp_Fix(AILEmitterCtx Context, bool Signed)
  221. {
  222. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  223. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  224. if (Signed)
  225. {
  226. EmitScalarFcvts(Context, Op.Size, Op.FBits);
  227. }
  228. else
  229. {
  230. EmitScalarFcvtu(Context, Op.Size, Op.FBits);
  231. }
  232. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  233. {
  234. Context.Emit(OpCodes.Conv_U8);
  235. }
  236. Context.EmitStintzr(Op.Rd);
  237. }
  238. private static void EmitVectorScvtf(AILEmitterCtx Context)
  239. {
  240. EmitVectorCvtf(Context, true);
  241. }
  242. private static void EmitVectorUcvtf(AILEmitterCtx Context)
  243. {
  244. EmitVectorCvtf(Context, false);
  245. }
  246. private static void EmitVectorCvtf(AILEmitterCtx Context, bool Signed)
  247. {
  248. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  249. int SizeF = Op.Size & 1;
  250. int SizeI = SizeF + 2;
  251. int FBits = GetFBits(Context);
  252. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  253. for (int Index = 0; Index < (Bytes >> SizeI); Index++)
  254. {
  255. EmitVectorExtract(Context, Op.Rn, Index, SizeI, Signed);
  256. if (!Signed)
  257. {
  258. Context.Emit(OpCodes.Conv_R_Un);
  259. }
  260. Context.Emit(SizeF == 0
  261. ? OpCodes.Conv_R4
  262. : OpCodes.Conv_R8);
  263. EmitI2fFBitsMul(Context, SizeF, FBits);
  264. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  265. }
  266. if (Op.RegisterSize == ARegisterSize.SIMD64)
  267. {
  268. EmitVectorZeroUpper(Context, Op.Rd);
  269. }
  270. }
  271. private static void EmitVectorFcvtzs(AILEmitterCtx Context)
  272. {
  273. EmitVectorFcvtz(Context, true);
  274. }
  275. private static void EmitVectorFcvtzu(AILEmitterCtx Context)
  276. {
  277. EmitVectorFcvtz(Context, false);
  278. }
  279. private static void EmitVectorFcvtz(AILEmitterCtx Context, bool Signed)
  280. {
  281. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  282. int SizeF = Op.Size & 1;
  283. int SizeI = SizeF + 2;
  284. int FBits = GetFBits(Context);
  285. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  286. for (int Index = 0; Index < (Bytes >> SizeI); Index++)
  287. {
  288. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  289. EmitF2iFBitsMul(Context, SizeF, FBits);
  290. if (SizeF == 0)
  291. {
  292. ASoftFallback.EmitCall(Context, Signed
  293. ? nameof(ASoftFallback.SatF32ToS32)
  294. : nameof(ASoftFallback.SatF32ToU32));
  295. }
  296. else /* if (SizeF == 1) */
  297. {
  298. ASoftFallback.EmitCall(Context, Signed
  299. ? nameof(ASoftFallback.SatF64ToS64)
  300. : nameof(ASoftFallback.SatF64ToU64));
  301. }
  302. if (SizeF == 0)
  303. {
  304. Context.Emit(OpCodes.Conv_U8);
  305. }
  306. EmitVectorInsert(Context, Op.Rd, Index, SizeI);
  307. }
  308. if (Op.RegisterSize == ARegisterSize.SIMD64)
  309. {
  310. EmitVectorZeroUpper(Context, Op.Rd);
  311. }
  312. }
  313. private static void EmitScalarFcvts(AILEmitterCtx Context, int Size, int FBits)
  314. {
  315. if (Size < 0 || Size > 1)
  316. {
  317. throw new ArgumentOutOfRangeException(nameof(Size));
  318. }
  319. EmitF2iFBitsMul(Context, Size, FBits);
  320. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  321. {
  322. if (Size == 0)
  323. {
  324. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToS32));
  325. }
  326. else /* if (Size == 1) */
  327. {
  328. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToS32));
  329. }
  330. }
  331. else
  332. {
  333. if (Size == 0)
  334. {
  335. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToS64));
  336. }
  337. else /* if (Size == 1) */
  338. {
  339. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToS64));
  340. }
  341. }
  342. }
  343. private static void EmitScalarFcvtu(AILEmitterCtx Context, int Size, int FBits)
  344. {
  345. if (Size < 0 || Size > 1)
  346. {
  347. throw new ArgumentOutOfRangeException(nameof(Size));
  348. }
  349. EmitF2iFBitsMul(Context, Size, FBits);
  350. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  351. {
  352. if (Size == 0)
  353. {
  354. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToU32));
  355. }
  356. else /* if (Size == 1) */
  357. {
  358. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToU32));
  359. }
  360. }
  361. else
  362. {
  363. if (Size == 0)
  364. {
  365. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToU64));
  366. }
  367. else /* if (Size == 1) */
  368. {
  369. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToU64));
  370. }
  371. }
  372. }
  373. private static void EmitF2iFBitsMul(AILEmitterCtx Context, int Size, int FBits)
  374. {
  375. if (FBits != 0)
  376. {
  377. if (Size == 0)
  378. {
  379. Context.EmitLdc_R4(MathF.Pow(2, FBits));
  380. }
  381. else if (Size == 1)
  382. {
  383. Context.EmitLdc_R8(Math.Pow(2, FBits));
  384. }
  385. else
  386. {
  387. throw new ArgumentOutOfRangeException(nameof(Size));
  388. }
  389. Context.Emit(OpCodes.Mul);
  390. }
  391. }
  392. private static void EmitI2fFBitsMul(AILEmitterCtx Context, int Size, int FBits)
  393. {
  394. if (FBits != 0)
  395. {
  396. if (Size == 0)
  397. {
  398. Context.EmitLdc_R4(1f / MathF.Pow(2, FBits));
  399. }
  400. else if (Size == 1)
  401. {
  402. Context.EmitLdc_R8(1 / Math.Pow(2, FBits));
  403. }
  404. else
  405. {
  406. throw new ArgumentOutOfRangeException(nameof(Size));
  407. }
  408. Context.Emit(OpCodes.Mul);
  409. }
  410. }
  411. }
  412. }