AInstEmitSimdCvt.cs 13 KB

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