AInstEmitSimdHelper.cs 18 KB

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