AInstEmitSimdHelper.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 EmitScalarBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  203. {
  204. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  205. EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: false);
  206. }
  207. public static void EmitScalarTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  208. {
  209. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  210. EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: true);
  211. }
  212. public static void EmitScalarOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  213. {
  214. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  215. int SizeF = Op.Size & 1;
  216. if (Ternary)
  217. {
  218. EmitVectorExtractF(Context, Op.Rd, 0, SizeF);
  219. }
  220. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  221. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  222. Emit();
  223. EmitScalarSetF(Context, Op.Rd, SizeF);
  224. }
  225. public static void EmitScalarUnaryOpSx(AILEmitterCtx Context, Action Emit)
  226. {
  227. EmitScalarOp(Context, Emit, OperFlags.Rn, true);
  228. }
  229. public static void EmitScalarBinaryOpSx(AILEmitterCtx Context, Action Emit)
  230. {
  231. EmitScalarOp(Context, Emit, OperFlags.RnRm, true);
  232. }
  233. public static void EmitScalarUnaryOpZx(AILEmitterCtx Context, Action Emit)
  234. {
  235. EmitScalarOp(Context, Emit, OperFlags.Rn, false);
  236. }
  237. public static void EmitScalarBinaryOpZx(AILEmitterCtx Context, Action Emit)
  238. {
  239. EmitScalarOp(Context, Emit, OperFlags.RnRm, false);
  240. }
  241. public static void EmitScalarTernaryOpZx(AILEmitterCtx Context, Action Emit)
  242. {
  243. EmitScalarOp(Context, Emit, OperFlags.RdRnRm, false);
  244. }
  245. public static void EmitScalarOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  246. {
  247. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  248. if (Opers.HasFlag(OperFlags.Rd))
  249. {
  250. EmitVectorExtract(Context, Op.Rd, 0, Op.Size, Signed);
  251. }
  252. if (Opers.HasFlag(OperFlags.Rn))
  253. {
  254. EmitVectorExtract(Context, Op.Rn, 0, Op.Size, Signed);
  255. }
  256. if (Opers.HasFlag(OperFlags.Rm))
  257. {
  258. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, 0, Op.Size, Signed);
  259. }
  260. Emit();
  261. EmitScalarSet(Context, Op.Rd, Op.Size);
  262. }
  263. public static void EmitScalarUnaryOpF(AILEmitterCtx Context, Action Emit)
  264. {
  265. EmitScalarOpF(Context, Emit, OperFlags.Rn);
  266. }
  267. public static void EmitScalarBinaryOpF(AILEmitterCtx Context, Action Emit)
  268. {
  269. EmitScalarOpF(Context, Emit, OperFlags.RnRm);
  270. }
  271. public static void EmitScalarTernaryRaOpF(AILEmitterCtx Context, Action Emit)
  272. {
  273. EmitScalarOpF(Context, Emit, OperFlags.RaRnRm);
  274. }
  275. public static void EmitScalarOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  276. {
  277. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  278. int SizeF = Op.Size & 1;
  279. if (Opers.HasFlag(OperFlags.Ra))
  280. {
  281. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Ra, 0, SizeF);
  282. }
  283. if (Opers.HasFlag(OperFlags.Rn))
  284. {
  285. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  286. }
  287. if (Opers.HasFlag(OperFlags.Rm))
  288. {
  289. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, 0, SizeF);
  290. }
  291. Emit();
  292. EmitScalarSetF(Context, Op.Rd, SizeF);
  293. }
  294. public static void EmitVectorUnaryOpF(AILEmitterCtx Context, Action Emit)
  295. {
  296. EmitVectorOpF(Context, Emit, OperFlags.Rn);
  297. }
  298. public static void EmitVectorBinaryOpF(AILEmitterCtx Context, Action Emit)
  299. {
  300. EmitVectorOpF(Context, Emit, OperFlags.RnRm);
  301. }
  302. public static void EmitVectorTernaryOpF(AILEmitterCtx Context, Action Emit)
  303. {
  304. EmitVectorOpF(Context, Emit, OperFlags.RdRnRm);
  305. }
  306. public static void EmitVectorOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  307. {
  308. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  309. int SizeF = Op.Size & 1;
  310. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  311. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  312. {
  313. if (Opers.HasFlag(OperFlags.Rd))
  314. {
  315. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  316. }
  317. if (Opers.HasFlag(OperFlags.Rn))
  318. {
  319. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  320. }
  321. if (Opers.HasFlag(OperFlags.Rm))
  322. {
  323. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, Index, SizeF);
  324. }
  325. Emit();
  326. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  327. }
  328. if (Op.RegisterSize == ARegisterSize.SIMD64)
  329. {
  330. EmitVectorZeroUpper(Context, Op.Rd);
  331. }
  332. }
  333. public static void EmitVectorBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  334. {
  335. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  336. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: false);
  337. }
  338. public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  339. {
  340. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  341. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: true);
  342. }
  343. public static void EmitVectorOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  344. {
  345. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  346. int SizeF = Op.Size & 1;
  347. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  348. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  349. {
  350. if (Ternary)
  351. {
  352. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  353. }
  354. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  355. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  356. Emit();
  357. EmitVectorInsertTmpF(Context, Index, SizeF);
  358. }
  359. Context.EmitLdvectmp();
  360. Context.EmitStvec(Op.Rd);
  361. if (Op.RegisterSize == ARegisterSize.SIMD64)
  362. {
  363. EmitVectorZeroUpper(Context, Op.Rd);
  364. }
  365. }
  366. public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
  367. {
  368. EmitVectorOp(Context, Emit, OperFlags.Rn, true);
  369. }
  370. public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
  371. {
  372. EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
  373. }
  374. public static void EmitVectorTernaryOpSx(AILEmitterCtx Context, Action Emit)
  375. {
  376. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, true);
  377. }
  378. public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
  379. {
  380. EmitVectorOp(Context, Emit, OperFlags.Rn, false);
  381. }
  382. public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
  383. {
  384. EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
  385. }
  386. public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
  387. {
  388. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
  389. }
  390. public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  391. {
  392. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  393. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  394. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  395. {
  396. if (Opers.HasFlag(OperFlags.Rd))
  397. {
  398. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  399. }
  400. if (Opers.HasFlag(OperFlags.Rn))
  401. {
  402. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  403. }
  404. if (Opers.HasFlag(OperFlags.Rm))
  405. {
  406. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  407. }
  408. Emit();
  409. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  410. }
  411. if (Op.RegisterSize == ARegisterSize.SIMD64)
  412. {
  413. EmitVectorZeroUpper(Context, Op.Rd);
  414. }
  415. }
  416. public static void EmitVectorBinaryOpByElemSx(AILEmitterCtx Context, Action Emit)
  417. {
  418. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  419. EmitVectorOpByElem(Context, Emit, Op.Index, false, true);
  420. }
  421. public static void EmitVectorBinaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  422. {
  423. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  424. EmitVectorOpByElem(Context, Emit, Op.Index, false, false);
  425. }
  426. public static void EmitVectorTernaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  427. {
  428. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  429. EmitVectorOpByElem(Context, Emit, Op.Index, true, false);
  430. }
  431. public static void EmitVectorOpByElem(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary, bool Signed)
  432. {
  433. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  434. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  435. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  436. {
  437. if (Ternary)
  438. {
  439. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  440. }
  441. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  442. EmitVectorExtract(Context, Op.Rm, Elem, Op.Size, Signed);
  443. Emit();
  444. EmitVectorInsertTmp(Context, Index, Op.Size);
  445. }
  446. Context.EmitLdvectmp();
  447. Context.EmitStvec(Op.Rd);
  448. if (Op.RegisterSize == ARegisterSize.SIMD64)
  449. {
  450. EmitVectorZeroUpper(Context, Op.Rd);
  451. }
  452. }
  453. public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
  454. {
  455. EmitVectorImmOp(Context, Emit, false);
  456. }
  457. public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
  458. {
  459. EmitVectorImmOp(Context, Emit, true);
  460. }
  461. public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
  462. {
  463. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  464. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  465. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  466. {
  467. if (Binary)
  468. {
  469. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  470. }
  471. Context.EmitLdc_I8(Op.Imm);
  472. Emit();
  473. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  474. }
  475. if (Op.RegisterSize == ARegisterSize.SIMD64)
  476. {
  477. EmitVectorZeroUpper(Context, Op.Rd);
  478. }
  479. }
  480. public static void EmitVectorWidenRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  481. {
  482. EmitVectorWidenRmBinaryOp(Context, Emit, true);
  483. }
  484. public static void EmitVectorWidenRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  485. {
  486. EmitVectorWidenRmBinaryOp(Context, Emit, false);
  487. }
  488. public static void EmitVectorWidenRmBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  489. {
  490. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  491. Context.EmitLdvec(Op.Rd);
  492. Context.EmitStvectmp();
  493. int Elems = 8 >> Op.Size;
  494. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  495. for (int Index = 0; Index < Elems; Index++)
  496. {
  497. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  498. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  499. Emit();
  500. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  501. }
  502. Context.EmitLdvectmp();
  503. Context.EmitStvec(Op.Rd);
  504. }
  505. public static void EmitVectorWidenRnRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  506. {
  507. EmitVectorWidenRnRmOp(Context, Emit, false, true);
  508. }
  509. public static void EmitVectorWidenRnRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  510. {
  511. EmitVectorWidenRnRmOp(Context, Emit, false, false);
  512. }
  513. public static void EmitVectorWidenRnRmTernaryOpSx(AILEmitterCtx Context, Action Emit)
  514. {
  515. EmitVectorWidenRnRmOp(Context, Emit, true, true);
  516. }
  517. public static void EmitVectorWidenRnRmTernaryOpZx(AILEmitterCtx Context, Action Emit)
  518. {
  519. EmitVectorWidenRnRmOp(Context, Emit, true, false);
  520. }
  521. public static void EmitVectorWidenRnRmOp(AILEmitterCtx Context, Action Emit, bool Ternary, bool Signed)
  522. {
  523. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  524. Context.EmitLdvec(Op.Rd);
  525. Context.EmitStvectmp();
  526. int Elems = 8 >> Op.Size;
  527. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  528. for (int Index = 0; Index < Elems; Index++)
  529. {
  530. if (Ternary)
  531. {
  532. EmitVectorExtract(Context, Op.Rd, Index, Op.Size + 1, Signed);
  533. }
  534. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  535. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  536. Emit();
  537. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  538. }
  539. Context.EmitLdvectmp();
  540. Context.EmitStvec(Op.Rd);
  541. }
  542. public static void EmitVectorPairwiseOpSx(AILEmitterCtx Context, Action Emit)
  543. {
  544. EmitVectorPairwiseOp(Context, Emit, true);
  545. }
  546. public static void EmitVectorPairwiseOpZx(AILEmitterCtx Context, Action Emit)
  547. {
  548. EmitVectorPairwiseOp(Context, Emit, false);
  549. }
  550. private static void EmitVectorPairwiseOp(AILEmitterCtx Context, Action Emit, bool Signed)
  551. {
  552. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  553. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  554. int Elems = Bytes >> Op.Size;
  555. int Half = Elems >> 1;
  556. for (int Index = 0; Index < Elems; Index++)
  557. {
  558. int Elem = (Index & (Half - 1)) << 1;
  559. EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 0, Op.Size, Signed);
  560. EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 1, Op.Size, Signed);
  561. Emit();
  562. EmitVectorInsertTmp(Context, Index, Op.Size);
  563. }
  564. Context.EmitLdvectmp();
  565. Context.EmitStvec(Op.Rd);
  566. if (Op.RegisterSize == ARegisterSize.SIMD64)
  567. {
  568. EmitVectorZeroUpper(Context, Op.Rd);
  569. }
  570. }
  571. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  572. {
  573. EmitVectorZeroAll(Context, Reg);
  574. EmitVectorInsert(Context, Reg, 0, Size);
  575. }
  576. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  577. {
  578. EmitVectorZeroAll(Context, Reg);
  579. EmitVectorInsertF(Context, Reg, 0, Size);
  580. }
  581. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  582. {
  583. EmitVectorExtract(Context, Reg, Index, Size, true);
  584. }
  585. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  586. {
  587. EmitVectorExtract(Context, Reg, Index, Size, false);
  588. }
  589. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  590. {
  591. ThrowIfInvalid(Index, Size);
  592. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  593. Context.EmitLdvec(Reg);
  594. Context.EmitLdc_I4(Index);
  595. Context.EmitLdc_I4(Size);
  596. AVectorHelper.EmitCall(Context, Signed
  597. ? nameof(AVectorHelper.VectorExtractIntSx)
  598. : nameof(AVectorHelper.VectorExtractIntZx));
  599. }
  600. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  601. {
  602. ThrowIfInvalidF(Index, Size);
  603. Context.EmitLdvec(Reg);
  604. Context.EmitLdc_I4(Index);
  605. if (Size == 0)
  606. {
  607. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractSingle));
  608. }
  609. else if (Size == 1)
  610. {
  611. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractDouble));
  612. }
  613. else
  614. {
  615. throw new ArgumentOutOfRangeException(nameof(Size));
  616. }
  617. }
  618. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  619. {
  620. EmitVectorZeroLower(Context, Rd);
  621. EmitVectorZeroUpper(Context, Rd);
  622. }
  623. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  624. {
  625. EmitVectorInsert(Context, Rd, 0, 3, 0);
  626. }
  627. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  628. {
  629. EmitVectorInsert(Context, Rd, 1, 3, 0);
  630. }
  631. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  632. {
  633. ThrowIfInvalid(Index, Size);
  634. Context.EmitLdvec(Reg);
  635. Context.EmitLdc_I4(Index);
  636. Context.EmitLdc_I4(Size);
  637. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  638. Context.EmitStvec(Reg);
  639. }
  640. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  641. {
  642. ThrowIfInvalid(Index, Size);
  643. Context.EmitLdvectmp();
  644. Context.EmitLdc_I4(Index);
  645. Context.EmitLdc_I4(Size);
  646. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  647. Context.EmitStvectmp();
  648. }
  649. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  650. {
  651. ThrowIfInvalid(Index, Size);
  652. Context.EmitLdc_I8(Value);
  653. Context.EmitLdvec(Reg);
  654. Context.EmitLdc_I4(Index);
  655. Context.EmitLdc_I4(Size);
  656. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  657. Context.EmitStvec(Reg);
  658. }
  659. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  660. {
  661. ThrowIfInvalidF(Index, Size);
  662. Context.EmitLdvec(Reg);
  663. Context.EmitLdc_I4(Index);
  664. if (Size == 0)
  665. {
  666. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
  667. }
  668. else if (Size == 1)
  669. {
  670. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
  671. }
  672. else
  673. {
  674. throw new ArgumentOutOfRangeException(nameof(Size));
  675. }
  676. Context.EmitStvec(Reg);
  677. }
  678. public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
  679. {
  680. ThrowIfInvalidF(Index, Size);
  681. Context.EmitLdvectmp();
  682. Context.EmitLdc_I4(Index);
  683. if (Size == 0)
  684. {
  685. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
  686. }
  687. else if (Size == 1)
  688. {
  689. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
  690. }
  691. else
  692. {
  693. throw new ArgumentOutOfRangeException(nameof(Size));
  694. }
  695. Context.EmitStvectmp();
  696. }
  697. private static void ThrowIfInvalid(int Index, int Size)
  698. {
  699. if ((uint)Size > 3)
  700. {
  701. throw new ArgumentOutOfRangeException(nameof(Size));
  702. }
  703. if ((uint)Index >= 16 >> Size)
  704. {
  705. throw new ArgumentOutOfRangeException(nameof(Index));
  706. }
  707. }
  708. private static void ThrowIfInvalidF(int Index, int Size)
  709. {
  710. if ((uint)Size > 1)
  711. {
  712. throw new ArgumentOutOfRangeException(nameof(Size));
  713. }
  714. if ((uint)Index >= 4 >> Size)
  715. {
  716. throw new ArgumentOutOfRangeException(nameof(Index));
  717. }
  718. }
  719. }
  720. }