AInstEmitSimdHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. AOpCodeSimd Op = (AOpCodeSimd)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, ((AOpCodeSimdReg)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 EmitVectorWidenRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  300. {
  301. EmitVectorWidenRmBinaryOp(Context, Emit, true);
  302. }
  303. public static void EmitVectorWidenRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  304. {
  305. EmitVectorWidenRmBinaryOp(Context, Emit, false);
  306. }
  307. public static void EmitVectorWidenRmBinaryOp(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 EmitVectorWidenRnRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  323. {
  324. EmitVectorWidenRnRmBinaryOp(Context, Emit, true);
  325. }
  326. public static void EmitVectorWidenRnRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  327. {
  328. EmitVectorWidenRnRmBinaryOp(Context, Emit, false);
  329. }
  330. public static void EmitVectorWidenRnRmBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  331. {
  332. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  333. int Elems = 8 >> Op.Size;
  334. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  335. for (int Index = 0; Index < Elems; Index++)
  336. {
  337. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  338. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  339. Emit();
  340. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  341. }
  342. Context.EmitLdvectmp();
  343. Context.EmitStvec(Op.Rd);
  344. }
  345. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  346. {
  347. EmitVectorZeroAll(Context, Reg);
  348. EmitVectorInsert(Context, Reg, 0, Size);
  349. }
  350. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  351. {
  352. EmitVectorZeroAll(Context, Reg);
  353. EmitVectorInsertF(Context, Reg, 0, Size);
  354. }
  355. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  356. {
  357. EmitVectorExtract(Context, Reg, Index, Size, true);
  358. }
  359. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  360. {
  361. EmitVectorExtract(Context, Reg, Index, Size, false);
  362. }
  363. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  364. {
  365. if (Size < 0 || Size > 3)
  366. {
  367. throw new ArgumentOutOfRangeException(nameof(Size));
  368. }
  369. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  370. Context.EmitLdvec(Reg);
  371. Context.EmitLdc_I4(Index);
  372. Context.EmitLdc_I4(Size);
  373. ASoftFallback.EmitCall(Context, Signed
  374. ? nameof(ASoftFallback.VectorExtractIntSx)
  375. : nameof(ASoftFallback.VectorExtractIntZx));
  376. }
  377. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  378. {
  379. Context.EmitLdvec(Reg);
  380. Context.EmitLdc_I4(Index);
  381. if (Size == 0)
  382. {
  383. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractSingle));
  384. }
  385. else if (Size == 1)
  386. {
  387. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractDouble));
  388. }
  389. else
  390. {
  391. throw new ArgumentOutOfRangeException(nameof(Size));
  392. }
  393. }
  394. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  395. {
  396. EmitVectorZeroLower(Context, Rd);
  397. EmitVectorZeroUpper(Context, Rd);
  398. }
  399. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  400. {
  401. EmitVectorInsert(Context, Rd, 0, 3, 0);
  402. }
  403. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  404. {
  405. EmitVectorInsert(Context, Rd, 1, 3, 0);
  406. }
  407. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  408. {
  409. if (Size < 0 || Size > 3)
  410. {
  411. throw new ArgumentOutOfRangeException(nameof(Size));
  412. }
  413. Context.EmitLdvec(Reg);
  414. Context.EmitLdc_I4(Index);
  415. Context.EmitLdc_I4(Size);
  416. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  417. Context.EmitStvec(Reg);
  418. }
  419. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  420. {
  421. if (Size < 0 || Size > 3)
  422. {
  423. throw new ArgumentOutOfRangeException(nameof(Size));
  424. }
  425. Context.EmitLdvectmp();
  426. Context.EmitLdc_I4(Index);
  427. Context.EmitLdc_I4(Size);
  428. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  429. Context.EmitStvectmp();
  430. }
  431. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  432. {
  433. if (Size < 0 || Size > 3)
  434. {
  435. throw new ArgumentOutOfRangeException(nameof(Size));
  436. }
  437. Context.EmitLdc_I8(Value);
  438. Context.EmitLdvec(Reg);
  439. Context.EmitLdc_I4(Index);
  440. Context.EmitLdc_I4(Size);
  441. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  442. Context.EmitStvec(Reg);
  443. }
  444. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  445. {
  446. Context.EmitLdvec(Reg);
  447. Context.EmitLdc_I4(Index);
  448. if (Size == 0)
  449. {
  450. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertSingle));
  451. }
  452. else if (Size == 1)
  453. {
  454. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertDouble));
  455. }
  456. else
  457. {
  458. throw new ArgumentOutOfRangeException(nameof(Size));
  459. }
  460. Context.EmitStvec(Reg);
  461. }
  462. public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
  463. {
  464. Context.EmitLdvectmp();
  465. Context.EmitLdc_I4(Index);
  466. if (Size == 0)
  467. {
  468. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertSingle));
  469. }
  470. else if (Size == 1)
  471. {
  472. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertDouble));
  473. }
  474. else
  475. {
  476. throw new ArgumentOutOfRangeException(nameof(Size));
  477. }
  478. Context.EmitStvectmp();
  479. }
  480. }
  481. }