AInstEmitSimdHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. EmitVectorOpByElemF(Context, Emit, Op.Index);
  170. }
  171. public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  172. {
  173. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  174. EmitVectorOpByElemF(Context, Emit, Op.Index);
  175. }
  176. public static void EmitVectorOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  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. EmitVectorExtractF(Context, Op.Rm, Index, SizeF);
  194. }
  195. Emit();
  196. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  197. }
  198. if (Op.RegisterSize == ARegisterSize.SIMD64)
  199. {
  200. EmitVectorZeroUpper(Context, Op.Rd);
  201. }
  202. }
  203. public static void EmitVectorOpByElemF(AILEmitterCtx Context, Action Emit, int Elem)
  204. {
  205. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  206. int SizeF = Op.Size & 1;
  207. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  208. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  209. {
  210. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  211. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  212. Emit();
  213. EmitVectorInsertTmpF(Context, Index, SizeF);
  214. }
  215. Context.EmitLdvectmp();
  216. Context.EmitStvec(Op.Rd);
  217. if (Op.RegisterSize == ARegisterSize.SIMD64)
  218. {
  219. EmitVectorZeroUpper(Context, Op.Rd);
  220. }
  221. }
  222. public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
  223. {
  224. EmitVectorOp(Context, Emit, OperFlags.Rn, true);
  225. }
  226. public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
  227. {
  228. EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
  229. }
  230. public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
  231. {
  232. EmitVectorOp(Context, Emit, OperFlags.Rn, false);
  233. }
  234. public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
  235. {
  236. EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
  237. }
  238. public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
  239. {
  240. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
  241. }
  242. public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  243. {
  244. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  245. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  246. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  247. {
  248. if (Opers.HasFlag(OperFlags.Rd))
  249. {
  250. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  251. }
  252. if (Opers.HasFlag(OperFlags.Rn))
  253. {
  254. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  255. }
  256. if (Opers.HasFlag(OperFlags.Rm))
  257. {
  258. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  259. }
  260. Emit();
  261. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  262. }
  263. if (Op.RegisterSize == ARegisterSize.SIMD64)
  264. {
  265. EmitVectorZeroUpper(Context, Op.Rd);
  266. }
  267. }
  268. public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
  269. {
  270. EmitVectorImmOp(Context, Emit, false);
  271. }
  272. public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
  273. {
  274. EmitVectorImmOp(Context, Emit, true);
  275. }
  276. public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
  277. {
  278. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  279. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  280. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  281. {
  282. if (Binary)
  283. {
  284. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  285. }
  286. Context.EmitLdc_I8(Op.Imm);
  287. Emit();
  288. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  289. }
  290. if (Op.RegisterSize == ARegisterSize.SIMD64)
  291. {
  292. EmitVectorZeroUpper(Context, Op.Rd);
  293. }
  294. }
  295. public static void EmitVectorWidenBinaryOpSx(AILEmitterCtx Context, Action Emit)
  296. {
  297. EmitVectorWidenBinaryOp(Context, Emit, true);
  298. }
  299. public static void EmitVectorWidenBinaryOpZx(AILEmitterCtx Context, Action Emit)
  300. {
  301. EmitVectorWidenBinaryOp(Context, Emit, false);
  302. }
  303. public static void EmitVectorWidenBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  304. {
  305. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  306. int Elems = 8 >> Op.Size;
  307. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  308. for (int Index = 0; Index < Elems; Index++)
  309. {
  310. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  311. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  312. Emit();
  313. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  314. }
  315. Context.EmitLdvectmp();
  316. Context.EmitStvec(Op.Rd);
  317. }
  318. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  319. {
  320. EmitVectorZeroAll(Context, Reg);
  321. EmitVectorInsert(Context, Reg, 0, Size);
  322. }
  323. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  324. {
  325. EmitVectorZeroAll(Context, Reg);
  326. EmitVectorInsertF(Context, Reg, 0, Size);
  327. }
  328. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  329. {
  330. EmitVectorExtract(Context, Reg, Index, Size, true);
  331. }
  332. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  333. {
  334. EmitVectorExtract(Context, Reg, Index, Size, false);
  335. }
  336. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  337. {
  338. if (Size < 0 || Size > 3)
  339. {
  340. throw new ArgumentOutOfRangeException(nameof(Size));
  341. }
  342. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  343. Context.EmitLdvec(Reg);
  344. Context.EmitLdc_I4(Index);
  345. Context.EmitLdc_I4(Size);
  346. ASoftFallback.EmitCall(Context, Signed
  347. ? nameof(ASoftFallback.VectorExtractIntSx)
  348. : nameof(ASoftFallback.VectorExtractIntZx));
  349. }
  350. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  351. {
  352. Context.EmitLdvec(Reg);
  353. Context.EmitLdc_I4(Index);
  354. if (Size == 0)
  355. {
  356. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractSingle));
  357. }
  358. else if (Size == 1)
  359. {
  360. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractDouble));
  361. }
  362. else
  363. {
  364. throw new ArgumentOutOfRangeException(nameof(Size));
  365. }
  366. }
  367. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  368. {
  369. EmitVectorZeroLower(Context, Rd);
  370. EmitVectorZeroUpper(Context, Rd);
  371. }
  372. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  373. {
  374. EmitVectorInsert(Context, Rd, 0, 3, 0);
  375. }
  376. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  377. {
  378. EmitVectorInsert(Context, Rd, 1, 3, 0);
  379. }
  380. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  381. {
  382. if (Size < 0 || Size > 3)
  383. {
  384. throw new ArgumentOutOfRangeException(nameof(Size));
  385. }
  386. Context.EmitLdvec(Reg);
  387. Context.EmitLdc_I4(Index);
  388. Context.EmitLdc_I4(Size);
  389. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  390. Context.EmitStvec(Reg);
  391. }
  392. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  393. {
  394. if (Size < 0 || Size > 3)
  395. {
  396. throw new ArgumentOutOfRangeException(nameof(Size));
  397. }
  398. Context.EmitLdvectmp();
  399. Context.EmitLdc_I4(Index);
  400. Context.EmitLdc_I4(Size);
  401. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  402. Context.EmitStvectmp();
  403. }
  404. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  405. {
  406. if (Size < 0 || Size > 3)
  407. {
  408. throw new ArgumentOutOfRangeException(nameof(Size));
  409. }
  410. Context.EmitLdc_I8(Value);
  411. Context.EmitLdvec(Reg);
  412. Context.EmitLdc_I4(Index);
  413. Context.EmitLdc_I4(Size);
  414. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  415. Context.EmitStvec(Reg);
  416. }
  417. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  418. {
  419. Context.EmitLdvec(Reg);
  420. Context.EmitLdc_I4(Index);
  421. if (Size == 0)
  422. {
  423. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertSingle));
  424. }
  425. else if (Size == 1)
  426. {
  427. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertDouble));
  428. }
  429. else
  430. {
  431. throw new ArgumentOutOfRangeException(nameof(Size));
  432. }
  433. Context.EmitStvec(Reg);
  434. }
  435. public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
  436. {
  437. Context.EmitLdvectmp();
  438. Context.EmitLdc_I4(Index);
  439. if (Size == 0)
  440. {
  441. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertSingle));
  442. }
  443. else if (Size == 1)
  444. {
  445. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertDouble));
  446. }
  447. else
  448. {
  449. throw new ArgumentOutOfRangeException(nameof(Size));
  450. }
  451. Context.EmitStvectmp();
  452. }
  453. }
  454. }