AInstEmitSimdHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 EmitScalarUnaryOpSx(AILEmitterCtx Context, Action Emit)
  67. {
  68. EmitScalarOp(Context, Emit, OperFlags.Rn, true);
  69. }
  70. public static void EmitScalarBinaryOpSx(AILEmitterCtx Context, Action Emit)
  71. {
  72. EmitScalarOp(Context, Emit, OperFlags.RnRm, true);
  73. }
  74. public static void EmitScalarUnaryOpZx(AILEmitterCtx Context, Action Emit)
  75. {
  76. EmitScalarOp(Context, Emit, OperFlags.Rn, false);
  77. }
  78. public static void EmitScalarBinaryOpZx(AILEmitterCtx Context, Action Emit)
  79. {
  80. EmitScalarOp(Context, Emit, OperFlags.RnRm, false);
  81. }
  82. public static void EmitScalarTernaryOpZx(AILEmitterCtx Context, Action Emit)
  83. {
  84. EmitScalarOp(Context, Emit, OperFlags.RdRnRm, false);
  85. }
  86. public static void EmitScalarOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  87. {
  88. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  89. if (Opers.HasFlag(OperFlags.Rd))
  90. {
  91. EmitVectorExtract(Context, Op.Rd, 0, Op.Size, Signed);
  92. }
  93. if (Opers.HasFlag(OperFlags.Rn))
  94. {
  95. EmitVectorExtract(Context, Op.Rn, 0, Op.Size, Signed);
  96. }
  97. if (Opers.HasFlag(OperFlags.Rm))
  98. {
  99. EmitVectorExtract(Context, Op.Rm, 0, Op.Size, Signed);
  100. }
  101. Emit();
  102. EmitScalarSet(Context, Op.Rd, Op.Size);
  103. }
  104. public static void EmitScalarUnaryOpF(AILEmitterCtx Context, Action Emit)
  105. {
  106. EmitScalarOpF(Context, Emit, OperFlags.Rn);
  107. }
  108. public static void EmitScalarBinaryOpF(AILEmitterCtx Context, Action Emit)
  109. {
  110. EmitScalarOpF(Context, Emit, OperFlags.RnRm);
  111. }
  112. public static void EmitScalarTernaryRaOpF(AILEmitterCtx Context, Action Emit)
  113. {
  114. EmitScalarOpF(Context, Emit, OperFlags.RaRnRm);
  115. }
  116. public static void EmitScalarOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  117. {
  118. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  119. int SizeF = Op.Size & 1;
  120. if (Opers.HasFlag(OperFlags.Ra))
  121. {
  122. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Ra, 0, SizeF);
  123. }
  124. if (Opers.HasFlag(OperFlags.Rn))
  125. {
  126. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  127. }
  128. if (Opers.HasFlag(OperFlags.Rm))
  129. {
  130. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, 0, SizeF);
  131. }
  132. Emit();
  133. EmitScalarSetF(Context, Op.Rd, SizeF);
  134. }
  135. public static void EmitVectorBinaryOpF(AILEmitterCtx Context, Action Emit)
  136. {
  137. EmitVectorOpF(Context, Emit, OperFlags.RnRm);
  138. }
  139. public static void EmitVectorTernaryOpF(AILEmitterCtx Context, Action Emit)
  140. {
  141. EmitVectorOpF(Context, Emit, OperFlags.RdRnRm);
  142. }
  143. public static void EmitVectorBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  144. {
  145. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  146. EmitVectorOpF(Context, Emit, OperFlags.RnRm, Op.Index);
  147. }
  148. public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  149. {
  150. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  151. EmitVectorOpF(Context, Emit, OperFlags.RdRnRm, Op.Index);
  152. }
  153. public static void EmitVectorOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers, int Elem = -1)
  154. {
  155. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  156. int SizeF = Op.Size & 1;
  157. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  158. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  159. {
  160. if (Opers.HasFlag(OperFlags.Rd))
  161. {
  162. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  163. }
  164. if (Opers.HasFlag(OperFlags.Rn))
  165. {
  166. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  167. }
  168. if (Opers.HasFlag(OperFlags.Rm))
  169. {
  170. if (Elem != -1)
  171. {
  172. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  173. }
  174. else
  175. {
  176. EmitVectorExtractF(Context, Op.Rm, Index, SizeF);
  177. }
  178. }
  179. Emit();
  180. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  181. }
  182. if (Op.RegisterSize == ARegisterSize.SIMD64)
  183. {
  184. EmitVectorZeroUpper(Context, Op.Rd);
  185. }
  186. }
  187. public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
  188. {
  189. EmitVectorOp(Context, Emit, OperFlags.Rn, true);
  190. }
  191. public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
  192. {
  193. EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
  194. }
  195. public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
  196. {
  197. EmitVectorOp(Context, Emit, OperFlags.Rn, false);
  198. }
  199. public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
  200. {
  201. EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
  202. }
  203. public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
  204. {
  205. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
  206. }
  207. public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  208. {
  209. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  210. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  211. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  212. {
  213. if (Opers.HasFlag(OperFlags.Rd))
  214. {
  215. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  216. }
  217. if (Opers.HasFlag(OperFlags.Rn))
  218. {
  219. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  220. }
  221. if (Opers.HasFlag(OperFlags.Rm))
  222. {
  223. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  224. }
  225. Emit();
  226. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  227. }
  228. if (Op.RegisterSize == ARegisterSize.SIMD64)
  229. {
  230. EmitVectorZeroUpper(Context, Op.Rd);
  231. }
  232. }
  233. public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
  234. {
  235. EmitVectorImmOp(Context, Emit, false);
  236. }
  237. public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
  238. {
  239. EmitVectorImmOp(Context, Emit, true);
  240. }
  241. public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
  242. {
  243. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  244. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  245. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  246. {
  247. if (Binary)
  248. {
  249. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  250. }
  251. Context.EmitLdc_I8(Op.Imm);
  252. Emit();
  253. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  254. }
  255. if (Op.RegisterSize == ARegisterSize.SIMD64)
  256. {
  257. EmitVectorZeroUpper(Context, Op.Rd);
  258. }
  259. }
  260. public static void EmitVectorWidenBinaryOpSx(AILEmitterCtx Context, Action Emit)
  261. {
  262. EmitVectorWidenBinaryOp(Context, Emit, true);
  263. }
  264. public static void EmitVectorWidenBinaryOpZx(AILEmitterCtx Context, Action Emit)
  265. {
  266. EmitVectorWidenBinaryOp(Context, Emit, false);
  267. }
  268. public static void EmitVectorWidenBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  269. {
  270. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  271. int Elems = 8 >> Op.Size;
  272. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  273. for (int Index = 0; Index < Elems; Index++)
  274. {
  275. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  276. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  277. Emit();
  278. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  279. }
  280. Context.EmitLdvectmp();
  281. Context.EmitStvec(Op.Rd);
  282. }
  283. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  284. {
  285. EmitVectorZeroAll(Context, Reg);
  286. EmitVectorInsert(Context, Reg, 0, Size);
  287. }
  288. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  289. {
  290. EmitVectorZeroAll(Context, Reg);
  291. EmitVectorInsertF(Context, Reg, 0, Size);
  292. }
  293. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  294. {
  295. EmitVectorExtract(Context, Reg, Index, Size, true);
  296. }
  297. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  298. {
  299. EmitVectorExtract(Context, Reg, Index, Size, false);
  300. }
  301. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  302. {
  303. if (Size < 0 || Size > 3)
  304. {
  305. throw new ArgumentOutOfRangeException(nameof(Size));
  306. }
  307. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  308. Context.EmitLdvec(Reg);
  309. Context.EmitLdc_I4(Index);
  310. Context.EmitLdc_I4(Size);
  311. ASoftFallback.EmitCall(Context, Signed
  312. ? nameof(ASoftFallback.VectorExtractIntSx)
  313. : nameof(ASoftFallback.VectorExtractIntZx));
  314. }
  315. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  316. {
  317. Context.EmitLdvec(Reg);
  318. Context.EmitLdc_I4(Index);
  319. if (Size == 0)
  320. {
  321. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractSingle));
  322. }
  323. else if (Size == 1)
  324. {
  325. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractDouble));
  326. }
  327. else
  328. {
  329. throw new ArgumentOutOfRangeException(nameof(Size));
  330. }
  331. }
  332. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  333. {
  334. EmitVectorZeroLower(Context, Rd);
  335. EmitVectorZeroUpper(Context, Rd);
  336. }
  337. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  338. {
  339. EmitVectorInsert(Context, Rd, 0, 3, 0);
  340. }
  341. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  342. {
  343. EmitVectorInsert(Context, Rd, 1, 3, 0);
  344. }
  345. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  346. {
  347. if (Size < 0 || Size > 3)
  348. {
  349. throw new ArgumentOutOfRangeException(nameof(Size));
  350. }
  351. Context.EmitLdvec(Reg);
  352. Context.EmitLdc_I4(Index);
  353. Context.EmitLdc_I4(Size);
  354. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  355. Context.EmitStvec(Reg);
  356. }
  357. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  358. {
  359. if (Size < 0 || Size > 3)
  360. {
  361. throw new ArgumentOutOfRangeException(nameof(Size));
  362. }
  363. Context.EmitLdvectmp();
  364. Context.EmitLdc_I4(Index);
  365. Context.EmitLdc_I4(Size);
  366. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  367. Context.EmitStvectmp();
  368. }
  369. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  370. {
  371. if (Size < 0 || Size > 3)
  372. {
  373. throw new ArgumentOutOfRangeException(nameof(Size));
  374. }
  375. Context.EmitLdc_I8(Value);
  376. Context.EmitLdvec(Reg);
  377. Context.EmitLdc_I4(Index);
  378. Context.EmitLdc_I4(Size);
  379. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  380. Context.EmitStvec(Reg);
  381. }
  382. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  383. {
  384. Context.EmitLdvec(Reg);
  385. Context.EmitLdc_I4(Index);
  386. if (Size == 0)
  387. {
  388. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertSingle));
  389. }
  390. else if (Size == 1)
  391. {
  392. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertDouble));
  393. }
  394. else
  395. {
  396. throw new ArgumentOutOfRangeException(nameof(Size));
  397. }
  398. Context.EmitStvec(Reg);
  399. }
  400. }
  401. }