AInstEmitSimdHelper.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.Intrinsics;
  8. using System.Runtime.Intrinsics.X86;
  9. namespace ChocolArm64.Instruction
  10. {
  11. static class AInstEmitSimdHelper
  12. {
  13. [Flags]
  14. public enum OperFlags
  15. {
  16. Rd = 1 << 0,
  17. Rn = 1 << 1,
  18. Rm = 1 << 2,
  19. Ra = 1 << 3,
  20. RnRm = Rn | Rm,
  21. RdRn = Rd | Rn,
  22. RaRnRm = Ra | Rn | Rm,
  23. RdRnRm = Rd | Rn | Rm
  24. }
  25. public static int GetImmShl(AOpCodeSimdShImm Op)
  26. {
  27. return Op.Imm - (8 << Op.Size);
  28. }
  29. public static int GetImmShr(AOpCodeSimdShImm Op)
  30. {
  31. return (8 << (Op.Size + 1)) - Op.Imm;
  32. }
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public static void EmitSse2Call(AILEmitterCtx Context, string Name)
  35. {
  36. EmitSseCall(Context, Name, typeof(Sse2));
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public static void EmitSse41Call(AILEmitterCtx Context, string Name)
  40. {
  41. EmitSseCall(Context, Name, typeof(Sse41));
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public static void EmitSse42Call(AILEmitterCtx Context, string Name)
  45. {
  46. EmitSseCall(Context, Name, typeof(Sse42));
  47. }
  48. private static void EmitSseCall(AILEmitterCtx Context, string Name, Type Type)
  49. {
  50. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  51. void Ldvec(int Reg)
  52. {
  53. Context.EmitLdvec(Reg);
  54. switch (Op.Size)
  55. {
  56. case 0: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToSByte)); break;
  57. case 1: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt16)); break;
  58. case 2: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt32)); break;
  59. case 3: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt64)); break;
  60. }
  61. }
  62. Ldvec(Op.Rn);
  63. Type BaseType = null;
  64. switch (Op.Size)
  65. {
  66. case 0: BaseType = typeof(Vector128<sbyte>); break;
  67. case 1: BaseType = typeof(Vector128<short>); break;
  68. case 2: BaseType = typeof(Vector128<int>); break;
  69. case 3: BaseType = typeof(Vector128<long>); break;
  70. }
  71. if (Op is AOpCodeSimdReg BinOp)
  72. {
  73. Ldvec(BinOp.Rm);
  74. Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType, BaseType }));
  75. }
  76. else
  77. {
  78. Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType }));
  79. }
  80. switch (Op.Size)
  81. {
  82. case 0: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSByteToSingle)); break;
  83. case 1: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt16ToSingle)); break;
  84. case 2: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt32ToSingle)); break;
  85. case 3: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt64ToSingle)); break;
  86. }
  87. Context.EmitStvec(Op.Rd);
  88. if (Op.RegisterSize == ARegisterSize.SIMD64)
  89. {
  90. EmitVectorZeroUpper(Context, Op.Rd);
  91. }
  92. }
  93. public static void EmitSseOrSse2CallF(AILEmitterCtx Context, string Name)
  94. {
  95. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  96. int SizeF = Op.Size & 1;
  97. void Ldvec(int Reg)
  98. {
  99. Context.EmitLdvec(Reg);
  100. if (SizeF == 1)
  101. {
  102. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToDouble));
  103. }
  104. }
  105. Ldvec(Op.Rn);
  106. Type Type;
  107. Type BaseType;
  108. if (SizeF == 0)
  109. {
  110. Type = typeof(Sse);
  111. BaseType = typeof(Vector128<float>);
  112. }
  113. else /* if (SizeF == 1) */
  114. {
  115. Type = typeof(Sse2);
  116. BaseType = typeof(Vector128<double>);
  117. }
  118. if (Op is AOpCodeSimdReg BinOp)
  119. {
  120. Ldvec(BinOp.Rm);
  121. Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType, BaseType }));
  122. }
  123. else
  124. {
  125. Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType }));
  126. }
  127. if (SizeF == 1)
  128. {
  129. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorDoubleToSingle));
  130. }
  131. Context.EmitStvec(Op.Rd);
  132. if (Op.RegisterSize == ARegisterSize.SIMD64)
  133. {
  134. EmitVectorZeroUpper(Context, Op.Rd);
  135. }
  136. }
  137. public static void EmitUnaryMathCall(AILEmitterCtx Context, string Name)
  138. {
  139. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  140. int SizeF = Op.Size & 1;
  141. MethodInfo MthdInfo;
  142. if (SizeF == 0)
  143. {
  144. MthdInfo = typeof(MathF).GetMethod(Name, new Type[] { typeof(float) });
  145. }
  146. else /* if (SizeF == 1) */
  147. {
  148. MthdInfo = typeof(Math).GetMethod(Name, new Type[] { typeof(double) });
  149. }
  150. Context.EmitCall(MthdInfo);
  151. }
  152. public static void EmitBinaryMathCall(AILEmitterCtx Context, string Name)
  153. {
  154. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  155. int SizeF = Op.Size & 1;
  156. MethodInfo MthdInfo;
  157. if (SizeF == 0)
  158. {
  159. MthdInfo = typeof(MathF).GetMethod(Name, new Type[] { typeof(float), typeof(float) });
  160. }
  161. else /* if (SizeF == 1) */
  162. {
  163. MthdInfo = typeof(Math).GetMethod(Name, new Type[] { typeof(double), typeof(double) });
  164. }
  165. Context.EmitCall(MthdInfo);
  166. }
  167. public static void EmitRoundMathCall(AILEmitterCtx Context, MidpointRounding RoundMode)
  168. {
  169. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  170. int SizeF = Op.Size & 1;
  171. Context.EmitLdc_I4((int)RoundMode);
  172. MethodInfo MthdInfo;
  173. Type[] Types = new Type[] { null, typeof(MidpointRounding) };
  174. Types[0] = SizeF == 0
  175. ? typeof(float)
  176. : typeof(double);
  177. if (SizeF == 0)
  178. {
  179. MthdInfo = typeof(MathF).GetMethod(nameof(MathF.Round), Types);
  180. }
  181. else /* if (SizeF == 1) */
  182. {
  183. MthdInfo = typeof(Math).GetMethod(nameof(Math.Round), Types);
  184. }
  185. Context.EmitCall(MthdInfo);
  186. }
  187. public static void EmitUnarySoftFloatCall(AILEmitterCtx Context, string Name)
  188. {
  189. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  190. int SizeF = Op.Size & 1;
  191. MethodInfo MthdInfo;
  192. if (SizeF == 0)
  193. {
  194. MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(float) });
  195. }
  196. else /* if (SizeF == 1) */
  197. {
  198. MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(double) });
  199. }
  200. Context.EmitCall(MthdInfo);
  201. }
  202. public static void EmitBinarySoftFloatCall(AILEmitterCtx Context, string Name)
  203. {
  204. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  205. int SizeF = Op.Size & 1;
  206. MethodInfo MthdInfo;
  207. if (SizeF == 0)
  208. {
  209. MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(float), typeof(float) });
  210. }
  211. else /* if (SizeF == 1) */
  212. {
  213. MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(double), typeof(double) });
  214. }
  215. Context.EmitCall(MthdInfo);
  216. }
  217. public static void EmitScalarBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  218. {
  219. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  220. EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: false);
  221. }
  222. public static void EmitScalarTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  223. {
  224. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  225. EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: true);
  226. }
  227. public static void EmitScalarOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  228. {
  229. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  230. int SizeF = Op.Size & 1;
  231. if (Ternary)
  232. {
  233. EmitVectorExtractF(Context, Op.Rd, 0, SizeF);
  234. }
  235. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  236. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  237. Emit();
  238. EmitScalarSetF(Context, Op.Rd, SizeF);
  239. }
  240. public static void EmitScalarUnaryOpSx(AILEmitterCtx Context, Action Emit)
  241. {
  242. EmitScalarOp(Context, Emit, OperFlags.Rn, true);
  243. }
  244. public static void EmitScalarBinaryOpSx(AILEmitterCtx Context, Action Emit)
  245. {
  246. EmitScalarOp(Context, Emit, OperFlags.RnRm, true);
  247. }
  248. public static void EmitScalarUnaryOpZx(AILEmitterCtx Context, Action Emit)
  249. {
  250. EmitScalarOp(Context, Emit, OperFlags.Rn, false);
  251. }
  252. public static void EmitScalarBinaryOpZx(AILEmitterCtx Context, Action Emit)
  253. {
  254. EmitScalarOp(Context, Emit, OperFlags.RnRm, false);
  255. }
  256. public static void EmitScalarTernaryOpZx(AILEmitterCtx Context, Action Emit)
  257. {
  258. EmitScalarOp(Context, Emit, OperFlags.RdRnRm, false);
  259. }
  260. public static void EmitScalarOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  261. {
  262. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  263. if (Opers.HasFlag(OperFlags.Rd))
  264. {
  265. EmitVectorExtract(Context, Op.Rd, 0, Op.Size, Signed);
  266. }
  267. if (Opers.HasFlag(OperFlags.Rn))
  268. {
  269. EmitVectorExtract(Context, Op.Rn, 0, Op.Size, Signed);
  270. }
  271. if (Opers.HasFlag(OperFlags.Rm))
  272. {
  273. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, 0, Op.Size, Signed);
  274. }
  275. Emit();
  276. EmitScalarSet(Context, Op.Rd, Op.Size);
  277. }
  278. public static void EmitScalarUnaryOpF(AILEmitterCtx Context, Action Emit)
  279. {
  280. EmitScalarOpF(Context, Emit, OperFlags.Rn);
  281. }
  282. public static void EmitScalarBinaryOpF(AILEmitterCtx Context, Action Emit)
  283. {
  284. EmitScalarOpF(Context, Emit, OperFlags.RnRm);
  285. }
  286. public static void EmitScalarTernaryRaOpF(AILEmitterCtx Context, Action Emit)
  287. {
  288. EmitScalarOpF(Context, Emit, OperFlags.RaRnRm);
  289. }
  290. public static void EmitScalarOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  291. {
  292. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  293. int SizeF = Op.Size & 1;
  294. if (Opers.HasFlag(OperFlags.Ra))
  295. {
  296. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Ra, 0, SizeF);
  297. }
  298. if (Opers.HasFlag(OperFlags.Rn))
  299. {
  300. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  301. }
  302. if (Opers.HasFlag(OperFlags.Rm))
  303. {
  304. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, 0, SizeF);
  305. }
  306. Emit();
  307. EmitScalarSetF(Context, Op.Rd, SizeF);
  308. }
  309. public static void EmitVectorUnaryOpF(AILEmitterCtx Context, Action Emit)
  310. {
  311. EmitVectorOpF(Context, Emit, OperFlags.Rn);
  312. }
  313. public static void EmitVectorBinaryOpF(AILEmitterCtx Context, Action Emit)
  314. {
  315. EmitVectorOpF(Context, Emit, OperFlags.RnRm);
  316. }
  317. public static void EmitVectorTernaryOpF(AILEmitterCtx Context, Action Emit)
  318. {
  319. EmitVectorOpF(Context, Emit, OperFlags.RdRnRm);
  320. }
  321. public static void EmitVectorOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  322. {
  323. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  324. int SizeF = Op.Size & 1;
  325. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  326. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  327. {
  328. if (Opers.HasFlag(OperFlags.Rd))
  329. {
  330. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  331. }
  332. if (Opers.HasFlag(OperFlags.Rn))
  333. {
  334. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  335. }
  336. if (Opers.HasFlag(OperFlags.Rm))
  337. {
  338. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, Index, SizeF);
  339. }
  340. Emit();
  341. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  342. }
  343. if (Op.RegisterSize == ARegisterSize.SIMD64)
  344. {
  345. EmitVectorZeroUpper(Context, Op.Rd);
  346. }
  347. }
  348. public static void EmitVectorBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  349. {
  350. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  351. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: false);
  352. }
  353. public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  354. {
  355. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  356. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: true);
  357. }
  358. public static void EmitVectorOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  359. {
  360. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  361. int SizeF = Op.Size & 1;
  362. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  363. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  364. {
  365. if (Ternary)
  366. {
  367. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  368. }
  369. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  370. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  371. Emit();
  372. EmitVectorInsertTmpF(Context, Index, SizeF);
  373. }
  374. Context.EmitLdvectmp();
  375. Context.EmitStvec(Op.Rd);
  376. if (Op.RegisterSize == ARegisterSize.SIMD64)
  377. {
  378. EmitVectorZeroUpper(Context, Op.Rd);
  379. }
  380. }
  381. public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
  382. {
  383. EmitVectorOp(Context, Emit, OperFlags.Rn, true);
  384. }
  385. public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
  386. {
  387. EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
  388. }
  389. public static void EmitVectorTernaryOpSx(AILEmitterCtx Context, Action Emit)
  390. {
  391. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, true);
  392. }
  393. public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
  394. {
  395. EmitVectorOp(Context, Emit, OperFlags.Rn, false);
  396. }
  397. public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
  398. {
  399. EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
  400. }
  401. public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
  402. {
  403. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
  404. }
  405. public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  406. {
  407. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  408. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  409. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  410. {
  411. if (Opers.HasFlag(OperFlags.Rd))
  412. {
  413. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  414. }
  415. if (Opers.HasFlag(OperFlags.Rn))
  416. {
  417. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  418. }
  419. if (Opers.HasFlag(OperFlags.Rm))
  420. {
  421. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  422. }
  423. Emit();
  424. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  425. }
  426. if (Op.RegisterSize == ARegisterSize.SIMD64)
  427. {
  428. EmitVectorZeroUpper(Context, Op.Rd);
  429. }
  430. }
  431. public static void EmitVectorBinaryOpByElemSx(AILEmitterCtx Context, Action Emit)
  432. {
  433. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  434. EmitVectorOpByElem(Context, Emit, Op.Index, false, true);
  435. }
  436. public static void EmitVectorBinaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  437. {
  438. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  439. EmitVectorOpByElem(Context, Emit, Op.Index, false, false);
  440. }
  441. public static void EmitVectorTernaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  442. {
  443. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  444. EmitVectorOpByElem(Context, Emit, Op.Index, true, false);
  445. }
  446. public static void EmitVectorOpByElem(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary, bool Signed)
  447. {
  448. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  449. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  450. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  451. {
  452. if (Ternary)
  453. {
  454. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  455. }
  456. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  457. EmitVectorExtract(Context, Op.Rm, Elem, Op.Size, Signed);
  458. Emit();
  459. EmitVectorInsertTmp(Context, Index, Op.Size);
  460. }
  461. Context.EmitLdvectmp();
  462. Context.EmitStvec(Op.Rd);
  463. if (Op.RegisterSize == ARegisterSize.SIMD64)
  464. {
  465. EmitVectorZeroUpper(Context, Op.Rd);
  466. }
  467. }
  468. public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
  469. {
  470. EmitVectorImmOp(Context, Emit, false);
  471. }
  472. public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
  473. {
  474. EmitVectorImmOp(Context, Emit, true);
  475. }
  476. public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
  477. {
  478. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  479. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  480. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  481. {
  482. if (Binary)
  483. {
  484. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  485. }
  486. Context.EmitLdc_I8(Op.Imm);
  487. Emit();
  488. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  489. }
  490. if (Op.RegisterSize == ARegisterSize.SIMD64)
  491. {
  492. EmitVectorZeroUpper(Context, Op.Rd);
  493. }
  494. }
  495. public static void EmitVectorWidenRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  496. {
  497. EmitVectorWidenRmBinaryOp(Context, Emit, true);
  498. }
  499. public static void EmitVectorWidenRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  500. {
  501. EmitVectorWidenRmBinaryOp(Context, Emit, false);
  502. }
  503. public static void EmitVectorWidenRmBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  504. {
  505. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  506. Context.EmitLdvec(Op.Rd);
  507. Context.EmitStvectmp();
  508. int Elems = 8 >> Op.Size;
  509. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  510. for (int Index = 0; Index < Elems; Index++)
  511. {
  512. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  513. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  514. Emit();
  515. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  516. }
  517. Context.EmitLdvectmp();
  518. Context.EmitStvec(Op.Rd);
  519. }
  520. public static void EmitVectorWidenRnRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  521. {
  522. EmitVectorWidenRnRmOp(Context, Emit, false, true);
  523. }
  524. public static void EmitVectorWidenRnRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  525. {
  526. EmitVectorWidenRnRmOp(Context, Emit, false, false);
  527. }
  528. public static void EmitVectorWidenRnRmTernaryOpSx(AILEmitterCtx Context, Action Emit)
  529. {
  530. EmitVectorWidenRnRmOp(Context, Emit, true, true);
  531. }
  532. public static void EmitVectorWidenRnRmTernaryOpZx(AILEmitterCtx Context, Action Emit)
  533. {
  534. EmitVectorWidenRnRmOp(Context, Emit, true, false);
  535. }
  536. public static void EmitVectorWidenRnRmOp(AILEmitterCtx Context, Action Emit, bool Ternary, bool Signed)
  537. {
  538. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  539. Context.EmitLdvec(Op.Rd);
  540. Context.EmitStvectmp();
  541. int Elems = 8 >> Op.Size;
  542. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  543. for (int Index = 0; Index < Elems; Index++)
  544. {
  545. if (Ternary)
  546. {
  547. EmitVectorExtract(Context, Op.Rd, Index, Op.Size + 1, Signed);
  548. }
  549. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  550. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  551. Emit();
  552. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  553. }
  554. Context.EmitLdvectmp();
  555. Context.EmitStvec(Op.Rd);
  556. }
  557. public static void EmitVectorPairwiseOpSx(AILEmitterCtx Context, Action Emit)
  558. {
  559. EmitVectorPairwiseOp(Context, Emit, true);
  560. }
  561. public static void EmitVectorPairwiseOpZx(AILEmitterCtx Context, Action Emit)
  562. {
  563. EmitVectorPairwiseOp(Context, Emit, false);
  564. }
  565. private static void EmitVectorPairwiseOp(AILEmitterCtx Context, Action Emit, bool Signed)
  566. {
  567. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  568. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  569. int Elems = Bytes >> Op.Size;
  570. int Half = Elems >> 1;
  571. for (int Index = 0; Index < Elems; Index++)
  572. {
  573. int Elem = (Index & (Half - 1)) << 1;
  574. EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 0, Op.Size, Signed);
  575. EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 1, Op.Size, Signed);
  576. Emit();
  577. EmitVectorInsertTmp(Context, Index, Op.Size);
  578. }
  579. Context.EmitLdvectmp();
  580. Context.EmitStvec(Op.Rd);
  581. if (Op.RegisterSize == ARegisterSize.SIMD64)
  582. {
  583. EmitVectorZeroUpper(Context, Op.Rd);
  584. }
  585. }
  586. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  587. {
  588. EmitVectorZeroAll(Context, Reg);
  589. EmitVectorInsert(Context, Reg, 0, Size);
  590. }
  591. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  592. {
  593. EmitVectorZeroAll(Context, Reg);
  594. EmitVectorInsertF(Context, Reg, 0, Size);
  595. }
  596. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  597. {
  598. EmitVectorExtract(Context, Reg, Index, Size, true);
  599. }
  600. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  601. {
  602. EmitVectorExtract(Context, Reg, Index, Size, false);
  603. }
  604. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  605. {
  606. ThrowIfInvalid(Index, Size);
  607. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  608. Context.EmitLdvec(Reg);
  609. Context.EmitLdc_I4(Index);
  610. Context.EmitLdc_I4(Size);
  611. AVectorHelper.EmitCall(Context, Signed
  612. ? nameof(AVectorHelper.VectorExtractIntSx)
  613. : nameof(AVectorHelper.VectorExtractIntZx));
  614. }
  615. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  616. {
  617. ThrowIfInvalidF(Index, Size);
  618. Context.EmitLdvec(Reg);
  619. Context.EmitLdc_I4(Index);
  620. if (Size == 0)
  621. {
  622. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractSingle));
  623. }
  624. else if (Size == 1)
  625. {
  626. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractDouble));
  627. }
  628. else
  629. {
  630. throw new ArgumentOutOfRangeException(nameof(Size));
  631. }
  632. }
  633. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  634. {
  635. EmitVectorZeroLower(Context, Rd);
  636. EmitVectorZeroUpper(Context, Rd);
  637. }
  638. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  639. {
  640. EmitVectorInsert(Context, Rd, 0, 3, 0);
  641. }
  642. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  643. {
  644. EmitVectorInsert(Context, Rd, 1, 3, 0);
  645. }
  646. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  647. {
  648. ThrowIfInvalid(Index, Size);
  649. Context.EmitLdvec(Reg);
  650. Context.EmitLdc_I4(Index);
  651. Context.EmitLdc_I4(Size);
  652. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  653. Context.EmitStvec(Reg);
  654. }
  655. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  656. {
  657. ThrowIfInvalid(Index, Size);
  658. Context.EmitLdvectmp();
  659. Context.EmitLdc_I4(Index);
  660. Context.EmitLdc_I4(Size);
  661. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  662. Context.EmitStvectmp();
  663. }
  664. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  665. {
  666. ThrowIfInvalid(Index, Size);
  667. Context.EmitLdc_I8(Value);
  668. Context.EmitLdvec(Reg);
  669. Context.EmitLdc_I4(Index);
  670. Context.EmitLdc_I4(Size);
  671. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  672. Context.EmitStvec(Reg);
  673. }
  674. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  675. {
  676. ThrowIfInvalidF(Index, Size);
  677. Context.EmitLdvec(Reg);
  678. Context.EmitLdc_I4(Index);
  679. if (Size == 0)
  680. {
  681. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
  682. }
  683. else if (Size == 1)
  684. {
  685. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
  686. }
  687. else
  688. {
  689. throw new ArgumentOutOfRangeException(nameof(Size));
  690. }
  691. Context.EmitStvec(Reg);
  692. }
  693. public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
  694. {
  695. ThrowIfInvalidF(Index, Size);
  696. Context.EmitLdvectmp();
  697. Context.EmitLdc_I4(Index);
  698. if (Size == 0)
  699. {
  700. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
  701. }
  702. else if (Size == 1)
  703. {
  704. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
  705. }
  706. else
  707. {
  708. throw new ArgumentOutOfRangeException(nameof(Size));
  709. }
  710. Context.EmitStvectmp();
  711. }
  712. private static void ThrowIfInvalid(int Index, int Size)
  713. {
  714. if ((uint)Size > 3)
  715. {
  716. throw new ArgumentOutOfRangeException(nameof(Size));
  717. }
  718. if ((uint)Index >= 16 >> Size)
  719. {
  720. throw new ArgumentOutOfRangeException(nameof(Index));
  721. }
  722. }
  723. private static void ThrowIfInvalidF(int Index, int Size)
  724. {
  725. if ((uint)Size > 1)
  726. {
  727. throw new ArgumentOutOfRangeException(nameof(Size));
  728. }
  729. if ((uint)Index >= 4 >> Size)
  730. {
  731. throw new ArgumentOutOfRangeException(nameof(Index));
  732. }
  733. }
  734. }
  735. }