AInstEmitSimdHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection;
  6. namespace ChocolArm64.Instruction
  7. {
  8. static class AInstEmitSimdHelper
  9. {
  10. [Flags]
  11. public enum OperFlags
  12. {
  13. Rd = 1 << 0,
  14. Rn = 1 << 1,
  15. Rm = 1 << 2,
  16. Ra = 1 << 3,
  17. RnRm = Rn | Rm,
  18. RdRn = Rd | Rn,
  19. RaRnRm = Ra | Rn | Rm,
  20. RdRnRm = Rd | Rn | Rm
  21. }
  22. public static int GetImmShl(AOpCodeSimdShImm Op)
  23. {
  24. return Op.Imm - (8 << Op.Size);
  25. }
  26. public static int GetImmShr(AOpCodeSimdShImm Op)
  27. {
  28. return (8 << (Op.Size + 1)) - Op.Imm;
  29. }
  30. public static void EmitUnaryMathCall(AILEmitterCtx Context, string Name)
  31. {
  32. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  33. MethodInfo MthdInfo;
  34. if (Op.Size == 0)
  35. {
  36. MthdInfo = typeof(MathF).GetMethod(Name, new Type[] { typeof(float) });
  37. }
  38. else if (Op.Size == 1)
  39. {
  40. MthdInfo = typeof(Math).GetMethod(Name, new Type[] { typeof(double) });
  41. }
  42. else
  43. {
  44. throw new InvalidOperationException();
  45. }
  46. Context.EmitCall(MthdInfo);
  47. }
  48. public static void EmitBinaryMathCall(AILEmitterCtx Context, string Name)
  49. {
  50. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  51. MethodInfo MthdInfo;
  52. if (Op.Size == 0)
  53. {
  54. MthdInfo = typeof(MathF).GetMethod(Name, new Type[] { typeof(float), typeof(float) });
  55. }
  56. else if (Op.Size == 1)
  57. {
  58. MthdInfo = typeof(Math).GetMethod(Name, new Type[] { typeof(double), typeof(double) });
  59. }
  60. else
  61. {
  62. throw new InvalidOperationException();
  63. }
  64. Context.EmitCall(MthdInfo);
  65. }
  66. public static void EmitRoundMathCall(AILEmitterCtx Context, MidpointRounding RoundMode)
  67. {
  68. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  69. Context.EmitLdc_I4((int)RoundMode);
  70. MethodInfo MthdInfo;
  71. Type[] Types = new Type[] { null, typeof(MidpointRounding) };
  72. Types[0] = Op.Size == 0
  73. ? typeof(float)
  74. : typeof(double);
  75. if (Op.Size == 0)
  76. {
  77. MthdInfo = typeof(MathF).GetMethod(nameof(MathF.Round), Types);
  78. }
  79. else if (Op.Size == 1)
  80. {
  81. MthdInfo = typeof(Math).GetMethod(nameof(Math.Round), Types);
  82. }
  83. else
  84. {
  85. throw new InvalidOperationException();
  86. }
  87. Context.EmitCall(MthdInfo);
  88. }
  89. public static void EmitScalarUnaryOpSx(AILEmitterCtx Context, Action Emit)
  90. {
  91. EmitScalarOp(Context, Emit, OperFlags.Rn, true);
  92. }
  93. public static void EmitScalarBinaryOpSx(AILEmitterCtx Context, Action Emit)
  94. {
  95. EmitScalarOp(Context, Emit, OperFlags.RnRm, true);
  96. }
  97. public static void EmitScalarUnaryOpZx(AILEmitterCtx Context, Action Emit)
  98. {
  99. EmitScalarOp(Context, Emit, OperFlags.Rn, false);
  100. }
  101. public static void EmitScalarBinaryOpZx(AILEmitterCtx Context, Action Emit)
  102. {
  103. EmitScalarOp(Context, Emit, OperFlags.RnRm, false);
  104. }
  105. public static void EmitScalarTernaryOpZx(AILEmitterCtx Context, Action Emit)
  106. {
  107. EmitScalarOp(Context, Emit, OperFlags.RdRnRm, false);
  108. }
  109. public static void EmitScalarOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  110. {
  111. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  112. if (Opers.HasFlag(OperFlags.Rd))
  113. {
  114. EmitVectorExtract(Context, Op.Rd, 0, Op.Size, Signed);
  115. }
  116. if (Opers.HasFlag(OperFlags.Rn))
  117. {
  118. EmitVectorExtract(Context, Op.Rn, 0, Op.Size, Signed);
  119. }
  120. if (Opers.HasFlag(OperFlags.Rm))
  121. {
  122. EmitVectorExtract(Context, Op.Rm, 0, Op.Size, Signed);
  123. }
  124. Emit();
  125. EmitScalarSet(Context, Op.Rd, Op.Size);
  126. }
  127. public static void EmitScalarUnaryOpF(AILEmitterCtx Context, Action Emit)
  128. {
  129. EmitScalarOpF(Context, Emit, OperFlags.Rn);
  130. }
  131. public static void EmitScalarBinaryOpF(AILEmitterCtx Context, Action Emit)
  132. {
  133. EmitScalarOpF(Context, Emit, OperFlags.RnRm);
  134. }
  135. public static void EmitScalarTernaryRaOpF(AILEmitterCtx Context, Action Emit)
  136. {
  137. EmitScalarOpF(Context, Emit, OperFlags.RaRnRm);
  138. }
  139. public static void EmitScalarOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  140. {
  141. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  142. int SizeF = Op.Size & 1;
  143. if (Opers.HasFlag(OperFlags.Ra))
  144. {
  145. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Ra, 0, SizeF);
  146. }
  147. if (Opers.HasFlag(OperFlags.Rn))
  148. {
  149. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  150. }
  151. if (Opers.HasFlag(OperFlags.Rm))
  152. {
  153. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, 0, SizeF);
  154. }
  155. Emit();
  156. EmitScalarSetF(Context, Op.Rd, SizeF);
  157. }
  158. public static void EmitVectorBinaryOpF(AILEmitterCtx Context, Action Emit)
  159. {
  160. EmitVectorOpF(Context, Emit, OperFlags.RnRm);
  161. }
  162. public static void EmitVectorTernaryOpF(AILEmitterCtx Context, Action Emit)
  163. {
  164. EmitVectorOpF(Context, Emit, OperFlags.RdRnRm);
  165. }
  166. public static void EmitVectorBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  167. {
  168. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  169. EmitVectorOpF(Context, Emit, OperFlags.RnRm, Op.Index);
  170. }
  171. public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  172. {
  173. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  174. EmitVectorOpF(Context, Emit, OperFlags.RdRnRm, Op.Index);
  175. }
  176. public static void EmitVectorOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers, int Elem = -1)
  177. {
  178. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  179. int SizeF = Op.Size & 1;
  180. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  181. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  182. {
  183. if (Opers.HasFlag(OperFlags.Rd))
  184. {
  185. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  186. }
  187. if (Opers.HasFlag(OperFlags.Rn))
  188. {
  189. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  190. }
  191. if (Opers.HasFlag(OperFlags.Rm))
  192. {
  193. if (Elem != -1)
  194. {
  195. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  196. }
  197. else
  198. {
  199. EmitVectorExtractF(Context, Op.Rm, Index, SizeF);
  200. }
  201. }
  202. Emit();
  203. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  204. }
  205. if (Op.RegisterSize == ARegisterSize.SIMD64)
  206. {
  207. EmitVectorZeroUpper(Context, Op.Rd);
  208. }
  209. }
  210. public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
  211. {
  212. EmitVectorOp(Context, Emit, OperFlags.Rn, true);
  213. }
  214. public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
  215. {
  216. EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
  217. }
  218. public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
  219. {
  220. EmitVectorOp(Context, Emit, OperFlags.Rn, false);
  221. }
  222. public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
  223. {
  224. EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
  225. }
  226. public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
  227. {
  228. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
  229. }
  230. public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  231. {
  232. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  233. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  234. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  235. {
  236. if (Opers.HasFlag(OperFlags.Rd))
  237. {
  238. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  239. }
  240. if (Opers.HasFlag(OperFlags.Rn))
  241. {
  242. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  243. }
  244. if (Opers.HasFlag(OperFlags.Rm))
  245. {
  246. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  247. }
  248. Emit();
  249. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  250. }
  251. if (Op.RegisterSize == ARegisterSize.SIMD64)
  252. {
  253. EmitVectorZeroUpper(Context, Op.Rd);
  254. }
  255. }
  256. public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
  257. {
  258. EmitVectorImmOp(Context, Emit, false);
  259. }
  260. public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
  261. {
  262. EmitVectorImmOp(Context, Emit, true);
  263. }
  264. public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
  265. {
  266. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  267. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  268. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  269. {
  270. if (Binary)
  271. {
  272. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  273. }
  274. Context.EmitLdc_I8(Op.Imm);
  275. Emit();
  276. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  277. }
  278. if (Op.RegisterSize == ARegisterSize.SIMD64)
  279. {
  280. EmitVectorZeroUpper(Context, Op.Rd);
  281. }
  282. }
  283. public static void EmitVectorWidenBinaryOpSx(AILEmitterCtx Context, Action Emit)
  284. {
  285. EmitVectorWidenBinaryOp(Context, Emit, true);
  286. }
  287. public static void EmitVectorWidenBinaryOpZx(AILEmitterCtx Context, Action Emit)
  288. {
  289. EmitVectorWidenBinaryOp(Context, Emit, false);
  290. }
  291. public static void EmitVectorWidenBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  292. {
  293. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  294. int Elems = 8 >> Op.Size;
  295. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  296. for (int Index = 0; Index < Elems; Index++)
  297. {
  298. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  299. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  300. Emit();
  301. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  302. }
  303. Context.EmitLdvectmp();
  304. Context.EmitStvec(Op.Rd);
  305. }
  306. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  307. {
  308. EmitVectorZeroAll(Context, Reg);
  309. EmitVectorInsert(Context, Reg, 0, Size);
  310. }
  311. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  312. {
  313. EmitVectorZeroAll(Context, Reg);
  314. EmitVectorInsertF(Context, Reg, 0, Size);
  315. }
  316. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  317. {
  318. EmitVectorExtract(Context, Reg, Index, Size, true);
  319. }
  320. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  321. {
  322. EmitVectorExtract(Context, Reg, Index, Size, false);
  323. }
  324. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  325. {
  326. if (Size < 0 || Size > 3)
  327. {
  328. throw new ArgumentOutOfRangeException(nameof(Size));
  329. }
  330. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  331. Context.EmitLdvec(Reg);
  332. Context.EmitLdc_I4(Index);
  333. Context.EmitLdc_I4(Size);
  334. ASoftFallback.EmitCall(Context, Signed
  335. ? nameof(ASoftFallback.VectorExtractIntSx)
  336. : nameof(ASoftFallback.VectorExtractIntZx));
  337. }
  338. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  339. {
  340. Context.EmitLdvec(Reg);
  341. Context.EmitLdc_I4(Index);
  342. if (Size == 0)
  343. {
  344. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractSingle));
  345. }
  346. else if (Size == 1)
  347. {
  348. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractDouble));
  349. }
  350. else
  351. {
  352. throw new ArgumentOutOfRangeException(nameof(Size));
  353. }
  354. }
  355. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  356. {
  357. EmitVectorZeroLower(Context, Rd);
  358. EmitVectorZeroUpper(Context, Rd);
  359. }
  360. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  361. {
  362. EmitVectorInsert(Context, Rd, 0, 3, 0);
  363. }
  364. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  365. {
  366. EmitVectorInsert(Context, Rd, 1, 3, 0);
  367. }
  368. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  369. {
  370. if (Size < 0 || Size > 3)
  371. {
  372. throw new ArgumentOutOfRangeException(nameof(Size));
  373. }
  374. Context.EmitLdvec(Reg);
  375. Context.EmitLdc_I4(Index);
  376. Context.EmitLdc_I4(Size);
  377. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  378. Context.EmitStvec(Reg);
  379. }
  380. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  381. {
  382. if (Size < 0 || Size > 3)
  383. {
  384. throw new ArgumentOutOfRangeException(nameof(Size));
  385. }
  386. Context.EmitLdvectmp();
  387. Context.EmitLdc_I4(Index);
  388. Context.EmitLdc_I4(Size);
  389. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  390. Context.EmitStvectmp();
  391. }
  392. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  393. {
  394. if (Size < 0 || Size > 3)
  395. {
  396. throw new ArgumentOutOfRangeException(nameof(Size));
  397. }
  398. Context.EmitLdc_I8(Value);
  399. Context.EmitLdvec(Reg);
  400. Context.EmitLdc_I4(Index);
  401. Context.EmitLdc_I4(Size);
  402. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  403. Context.EmitStvec(Reg);
  404. }
  405. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  406. {
  407. Context.EmitLdvec(Reg);
  408. Context.EmitLdc_I4(Index);
  409. if (Size == 0)
  410. {
  411. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertSingle));
  412. }
  413. else if (Size == 1)
  414. {
  415. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertDouble));
  416. }
  417. else
  418. {
  419. throw new ArgumentOutOfRangeException(nameof(Size));
  420. }
  421. Context.EmitStvec(Reg);
  422. }
  423. }
  424. }