AInstEmitSimdHelper.cs 34 KB

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