AInstEmitSimdCvt.cs 12 KB

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