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