AInstEmitSimdCvt.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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.Rn, 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. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  189. {
  190. Context.Emit(OpCodes.Conv_U8);
  191. }
  192. Context.EmitStintzr(Op.Rd);
  193. }
  194. private static void EmitVectorCvtf(AILEmitterCtx Context, bool Signed)
  195. {
  196. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  197. int SizeF = Op.Size & 1;
  198. int SizeI = SizeF + 2;
  199. int FBits = GetFBits(Context);
  200. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  201. for (int Index = 0; Index < (Bytes >> SizeI); Index++)
  202. {
  203. EmitVectorExtract(Context, Op.Rn, Index, SizeI, Signed);
  204. if (!Signed)
  205. {
  206. Context.Emit(OpCodes.Conv_R_Un);
  207. }
  208. Context.Emit(SizeF == 0
  209. ? OpCodes.Conv_R4
  210. : OpCodes.Conv_R8);
  211. EmitI2fFBitsMul(Context, SizeF, FBits);
  212. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  213. }
  214. if (Op.RegisterSize == ARegisterSize.SIMD64)
  215. {
  216. EmitVectorZeroUpper(Context, Op.Rd);
  217. }
  218. }
  219. private static void EmitVectorFcvt(AILEmitterCtx Context, bool Signed)
  220. {
  221. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  222. int SizeF = Op.Size & 1;
  223. int SizeI = SizeF + 2;
  224. int FBits = GetFBits(Context);
  225. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  226. for (int Index = 0; Index < (Bytes >> SizeI); Index++)
  227. {
  228. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  229. EmitF2iFBitsMul(Context, SizeF, FBits);
  230. if (SizeF == 0)
  231. {
  232. ASoftFallback.EmitCall(Context, Signed
  233. ? nameof(ASoftFallback.SatF32ToS32)
  234. : nameof(ASoftFallback.SatF32ToU32));
  235. }
  236. else /* if (SizeF == 1) */
  237. {
  238. ASoftFallback.EmitCall(Context, Signed
  239. ? nameof(ASoftFallback.SatF64ToS64)
  240. : nameof(ASoftFallback.SatF64ToU64));
  241. }
  242. if (SizeF == 0)
  243. {
  244. Context.Emit(OpCodes.Conv_U8);
  245. }
  246. EmitVectorInsert(Context, Op.Rd, Index, SizeI);
  247. }
  248. if (Op.RegisterSize == ARegisterSize.SIMD64)
  249. {
  250. EmitVectorZeroUpper(Context, Op.Rd);
  251. }
  252. }
  253. private static void EmitScalarFcvts(AILEmitterCtx Context, int Size, int FBits)
  254. {
  255. if (Size < 0 || Size > 1)
  256. {
  257. throw new ArgumentOutOfRangeException(nameof(Size));
  258. }
  259. EmitF2iFBitsMul(Context, Size, FBits);
  260. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  261. {
  262. if (Size == 0)
  263. {
  264. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToS32));
  265. }
  266. else /* if (Size == 1) */
  267. {
  268. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToS32));
  269. }
  270. }
  271. else
  272. {
  273. if (Size == 0)
  274. {
  275. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToS64));
  276. }
  277. else /* if (Size == 1) */
  278. {
  279. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToS64));
  280. }
  281. }
  282. }
  283. private static void EmitScalarFcvtu(AILEmitterCtx Context, int Size, int FBits)
  284. {
  285. if (Size < 0 || Size > 1)
  286. {
  287. throw new ArgumentOutOfRangeException(nameof(Size));
  288. }
  289. EmitF2iFBitsMul(Context, Size, FBits);
  290. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  291. {
  292. if (Size == 0)
  293. {
  294. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToU32));
  295. }
  296. else /* if (Size == 1) */
  297. {
  298. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToU32));
  299. }
  300. }
  301. else
  302. {
  303. if (Size == 0)
  304. {
  305. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToU64));
  306. }
  307. else /* if (Size == 1) */
  308. {
  309. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToU64));
  310. }
  311. }
  312. }
  313. private static void EmitF2iFBitsMul(AILEmitterCtx Context, int Size, int FBits)
  314. {
  315. if (FBits != 0)
  316. {
  317. if (Size == 0)
  318. {
  319. Context.EmitLdc_R4(MathF.Pow(2, FBits));
  320. }
  321. else if (Size == 1)
  322. {
  323. Context.EmitLdc_R8(Math.Pow(2, FBits));
  324. }
  325. else
  326. {
  327. throw new ArgumentOutOfRangeException(nameof(Size));
  328. }
  329. Context.Emit(OpCodes.Mul);
  330. }
  331. }
  332. private static void EmitI2fFBitsMul(AILEmitterCtx Context, int Size, int FBits)
  333. {
  334. if (FBits != 0)
  335. {
  336. if (Size == 0)
  337. {
  338. Context.EmitLdc_R4(1f / MathF.Pow(2, FBits));
  339. }
  340. else if (Size == 1)
  341. {
  342. Context.EmitLdc_R8(1 / Math.Pow(2, FBits));
  343. }
  344. else
  345. {
  346. throw new ArgumentOutOfRangeException(nameof(Size));
  347. }
  348. Context.Emit(OpCodes.Mul);
  349. }
  350. }
  351. }
  352. }