AInstEmitSimdHelper.cs 17 KB

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