AInstEmitSimdHelper.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  328. {
  329. if (Opers.HasFlag(OperFlags.Rd))
  330. {
  331. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  332. }
  333. if (Opers.HasFlag(OperFlags.Rn))
  334. {
  335. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  336. }
  337. if (Opers.HasFlag(OperFlags.Rm))
  338. {
  339. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, Index, SizeF);
  340. }
  341. Emit();
  342. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  343. }
  344. if (Op.RegisterSize == ARegisterSize.SIMD64)
  345. {
  346. EmitVectorZeroUpper(Context, Op.Rd);
  347. }
  348. }
  349. public static void EmitVectorBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  350. {
  351. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  352. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: false);
  353. }
  354. public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  355. {
  356. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  357. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: true);
  358. }
  359. public static void EmitVectorOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  360. {
  361. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  362. int SizeF = Op.Size & 1;
  363. int Bytes = Op.GetBitsCount() >> 3;
  364. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  365. {
  366. if (Ternary)
  367. {
  368. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  369. }
  370. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  371. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  372. Emit();
  373. EmitVectorInsertTmpF(Context, Index, SizeF);
  374. }
  375. Context.EmitLdvectmp();
  376. Context.EmitStvec(Op.Rd);
  377. if (Op.RegisterSize == ARegisterSize.SIMD64)
  378. {
  379. EmitVectorZeroUpper(Context, Op.Rd);
  380. }
  381. }
  382. public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
  383. {
  384. EmitVectorOp(Context, Emit, OperFlags.Rn, true);
  385. }
  386. public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
  387. {
  388. EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
  389. }
  390. public static void EmitVectorTernaryOpSx(AILEmitterCtx Context, Action Emit)
  391. {
  392. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, true);
  393. }
  394. public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
  395. {
  396. EmitVectorOp(Context, Emit, OperFlags.Rn, false);
  397. }
  398. public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
  399. {
  400. EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
  401. }
  402. public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
  403. {
  404. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
  405. }
  406. public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  407. {
  408. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  409. int Bytes = Op.GetBitsCount() >> 3;
  410. int Elems = Bytes >> Op.Size;
  411. for (int Index = 0; Index < Elems; Index++)
  412. {
  413. if (Opers.HasFlag(OperFlags.Rd))
  414. {
  415. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  416. }
  417. if (Opers.HasFlag(OperFlags.Rn))
  418. {
  419. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  420. }
  421. if (Opers.HasFlag(OperFlags.Rm))
  422. {
  423. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  424. }
  425. Emit();
  426. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  427. }
  428. if (Op.RegisterSize == ARegisterSize.SIMD64)
  429. {
  430. EmitVectorZeroUpper(Context, Op.Rd);
  431. }
  432. }
  433. public static void EmitVectorBinaryOpByElemSx(AILEmitterCtx Context, Action Emit)
  434. {
  435. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  436. EmitVectorOpByElem(Context, Emit, Op.Index, false, true);
  437. }
  438. public static void EmitVectorBinaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  439. {
  440. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  441. EmitVectorOpByElem(Context, Emit, Op.Index, false, false);
  442. }
  443. public static void EmitVectorTernaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  444. {
  445. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  446. EmitVectorOpByElem(Context, Emit, Op.Index, true, false);
  447. }
  448. public static void EmitVectorOpByElem(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary, bool Signed)
  449. {
  450. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  451. int Bytes = Op.GetBitsCount() >> 3;
  452. int Elems = Bytes >> Op.Size;
  453. for (int Index = 0; Index < Elems; Index++)
  454. {
  455. if (Ternary)
  456. {
  457. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  458. }
  459. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  460. EmitVectorExtract(Context, Op.Rm, Elem, Op.Size, Signed);
  461. Emit();
  462. EmitVectorInsertTmp(Context, Index, Op.Size);
  463. }
  464. Context.EmitLdvectmp();
  465. Context.EmitStvec(Op.Rd);
  466. if (Op.RegisterSize == ARegisterSize.SIMD64)
  467. {
  468. EmitVectorZeroUpper(Context, Op.Rd);
  469. }
  470. }
  471. public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
  472. {
  473. EmitVectorImmOp(Context, Emit, false);
  474. }
  475. public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
  476. {
  477. EmitVectorImmOp(Context, Emit, true);
  478. }
  479. public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
  480. {
  481. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  482. int Bytes = Op.GetBitsCount() >> 3;
  483. int Elems = Bytes >> Op.Size;
  484. for (int Index = 0; Index < Elems; Index++)
  485. {
  486. if (Binary)
  487. {
  488. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  489. }
  490. Context.EmitLdc_I8(Op.Imm);
  491. Emit();
  492. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  493. }
  494. if (Op.RegisterSize == ARegisterSize.SIMD64)
  495. {
  496. EmitVectorZeroUpper(Context, Op.Rd);
  497. }
  498. }
  499. public static void EmitVectorWidenRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  500. {
  501. EmitVectorWidenRmBinaryOp(Context, Emit, true);
  502. }
  503. public static void EmitVectorWidenRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  504. {
  505. EmitVectorWidenRmBinaryOp(Context, Emit, false);
  506. }
  507. public static void EmitVectorWidenRmBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  508. {
  509. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  510. Context.EmitLdvec(Op.Rd);
  511. Context.EmitStvectmp();
  512. int Elems = 8 >> Op.Size;
  513. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  514. for (int Index = 0; Index < Elems; Index++)
  515. {
  516. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  517. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  518. Emit();
  519. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  520. }
  521. Context.EmitLdvectmp();
  522. Context.EmitStvec(Op.Rd);
  523. }
  524. public static void EmitVectorWidenRnRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  525. {
  526. EmitVectorWidenRnRmOp(Context, Emit, false, true);
  527. }
  528. public static void EmitVectorWidenRnRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  529. {
  530. EmitVectorWidenRnRmOp(Context, Emit, false, false);
  531. }
  532. public static void EmitVectorWidenRnRmTernaryOpSx(AILEmitterCtx Context, Action Emit)
  533. {
  534. EmitVectorWidenRnRmOp(Context, Emit, true, true);
  535. }
  536. public static void EmitVectorWidenRnRmTernaryOpZx(AILEmitterCtx Context, Action Emit)
  537. {
  538. EmitVectorWidenRnRmOp(Context, Emit, true, false);
  539. }
  540. public static void EmitVectorWidenRnRmOp(AILEmitterCtx Context, Action Emit, bool Ternary, bool Signed)
  541. {
  542. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  543. Context.EmitLdvec(Op.Rd);
  544. Context.EmitStvectmp();
  545. int Elems = 8 >> Op.Size;
  546. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  547. for (int Index = 0; Index < Elems; Index++)
  548. {
  549. if (Ternary)
  550. {
  551. EmitVectorExtract(Context, Op.Rd, Index, Op.Size + 1, Signed);
  552. }
  553. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  554. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  555. Emit();
  556. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  557. }
  558. Context.EmitLdvectmp();
  559. Context.EmitStvec(Op.Rd);
  560. }
  561. public static void EmitVectorPairwiseOpSx(AILEmitterCtx Context, Action Emit)
  562. {
  563. EmitVectorPairwiseOp(Context, Emit, true);
  564. }
  565. public static void EmitVectorPairwiseOpZx(AILEmitterCtx Context, Action Emit)
  566. {
  567. EmitVectorPairwiseOp(Context, Emit, false);
  568. }
  569. public static void EmitVectorPairwiseOp(AILEmitterCtx Context, Action Emit, bool Signed)
  570. {
  571. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  572. int Bytes = Op.GetBitsCount() >> 3;
  573. int Elems = Bytes >> Op.Size;
  574. int Half = Elems >> 1;
  575. for (int Index = 0; Index < Elems; Index++)
  576. {
  577. int Elem = (Index & (Half - 1)) << 1;
  578. EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 0, Op.Size, Signed);
  579. EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 1, Op.Size, Signed);
  580. Emit();
  581. EmitVectorInsertTmp(Context, Index, Op.Size);
  582. }
  583. Context.EmitLdvectmp();
  584. Context.EmitStvec(Op.Rd);
  585. if (Op.RegisterSize == ARegisterSize.SIMD64)
  586. {
  587. EmitVectorZeroUpper(Context, Op.Rd);
  588. }
  589. }
  590. public static void EmitScalarSaturatingNarrowOpSxSx(AILEmitterCtx Context, Action Emit)
  591. {
  592. EmitSaturatingNarrowOp(Context, Emit, true, true, true);
  593. }
  594. public static void EmitScalarSaturatingNarrowOpSxZx(AILEmitterCtx Context, Action Emit)
  595. {
  596. EmitSaturatingNarrowOp(Context, Emit, true, false, true);
  597. }
  598. public static void EmitScalarSaturatingNarrowOpZxZx(AILEmitterCtx Context, Action Emit)
  599. {
  600. EmitSaturatingNarrowOp(Context, Emit, false, false, true);
  601. }
  602. public static void EmitVectorSaturatingNarrowOpSxSx(AILEmitterCtx Context, Action Emit)
  603. {
  604. EmitSaturatingNarrowOp(Context, Emit, true, true, false);
  605. }
  606. public static void EmitVectorSaturatingNarrowOpSxZx(AILEmitterCtx Context, Action Emit)
  607. {
  608. EmitSaturatingNarrowOp(Context, Emit, true, false, false);
  609. }
  610. public static void EmitVectorSaturatingNarrowOpZxZx(AILEmitterCtx Context, Action Emit)
  611. {
  612. EmitSaturatingNarrowOp(Context, Emit, false, false, false);
  613. }
  614. public static void EmitSaturatingNarrowOp(
  615. AILEmitterCtx Context,
  616. Action Emit,
  617. bool SignedSrc,
  618. bool SignedDst,
  619. bool Scalar)
  620. {
  621. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  622. int Elems = !Scalar ? 8 >> Op.Size : 1;
  623. int ESize = 8 << Op.Size;
  624. int Part = !Scalar && (Op.RegisterSize == ARegisterSize.SIMD128) ? Elems : 0;
  625. long TMaxValue = SignedDst ? (1 << (ESize - 1)) - 1 : (1L << ESize) - 1L;
  626. long TMinValue = SignedDst ? -((1 << (ESize - 1))) : 0;
  627. Context.EmitLdc_I8(0L);
  628. Context.EmitSttmp();
  629. if (Part != 0)
  630. {
  631. Context.EmitLdvec(Op.Rd);
  632. Context.EmitStvectmp();
  633. }
  634. for (int Index = 0; Index < Elems; Index++)
  635. {
  636. AILLabel LblLe = new AILLabel();
  637. AILLabel LblGeEnd = new AILLabel();
  638. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, SignedSrc);
  639. Emit();
  640. Context.Emit(OpCodes.Dup);
  641. Context.EmitLdc_I8(TMaxValue);
  642. Context.Emit(SignedSrc ? OpCodes.Ble_S : OpCodes.Ble_Un_S, LblLe);
  643. Context.Emit(OpCodes.Pop);
  644. Context.EmitLdc_I8(TMaxValue);
  645. Context.EmitLdc_I8(0x8000000L);
  646. Context.EmitSttmp();
  647. Context.Emit(OpCodes.Br_S, LblGeEnd);
  648. Context.MarkLabel(LblLe);
  649. Context.Emit(OpCodes.Dup);
  650. Context.EmitLdc_I8(TMinValue);
  651. Context.Emit(SignedSrc ? OpCodes.Bge_S : OpCodes.Bge_Un_S, LblGeEnd);
  652. Context.Emit(OpCodes.Pop);
  653. Context.EmitLdc_I8(TMinValue);
  654. Context.EmitLdc_I8(0x8000000L);
  655. Context.EmitSttmp();
  656. Context.MarkLabel(LblGeEnd);
  657. if (Scalar)
  658. {
  659. EmitVectorZeroLower(Context, Op.Rd);
  660. }
  661. EmitVectorInsertTmp(Context, Part + Index, Op.Size);
  662. }
  663. Context.EmitLdvectmp();
  664. Context.EmitStvec(Op.Rd);
  665. if (Part == 0)
  666. {
  667. EmitVectorZeroUpper(Context, Op.Rd);
  668. }
  669. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  670. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  671. Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Fpsr));
  672. Context.EmitLdtmp();
  673. Context.Emit(OpCodes.Conv_I4);
  674. Context.Emit(OpCodes.Or);
  675. Context.EmitCallPropSet(typeof(AThreadState), nameof(AThreadState.Fpsr));
  676. }
  677. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  678. {
  679. EmitVectorZeroAll(Context, Reg);
  680. EmitVectorInsert(Context, Reg, 0, Size);
  681. }
  682. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  683. {
  684. EmitVectorZeroAll(Context, Reg);
  685. EmitVectorInsertF(Context, Reg, 0, Size);
  686. }
  687. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  688. {
  689. EmitVectorExtract(Context, Reg, Index, Size, true);
  690. }
  691. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  692. {
  693. EmitVectorExtract(Context, Reg, Index, Size, false);
  694. }
  695. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  696. {
  697. ThrowIfInvalid(Index, Size);
  698. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  699. Context.EmitLdvec(Reg);
  700. Context.EmitLdc_I4(Index);
  701. Context.EmitLdc_I4(Size);
  702. AVectorHelper.EmitCall(Context, Signed
  703. ? nameof(AVectorHelper.VectorExtractIntSx)
  704. : nameof(AVectorHelper.VectorExtractIntZx));
  705. }
  706. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  707. {
  708. ThrowIfInvalidF(Index, Size);
  709. Context.EmitLdvec(Reg);
  710. Context.EmitLdc_I4(Index);
  711. if (Size == 0)
  712. {
  713. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractSingle));
  714. }
  715. else if (Size == 1)
  716. {
  717. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractDouble));
  718. }
  719. else
  720. {
  721. throw new ArgumentOutOfRangeException(nameof(Size));
  722. }
  723. }
  724. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  725. {
  726. EmitVectorZeroLower(Context, Rd);
  727. EmitVectorZeroUpper(Context, Rd);
  728. }
  729. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  730. {
  731. EmitVectorInsert(Context, Rd, 0, 3, 0);
  732. }
  733. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  734. {
  735. EmitVectorInsert(Context, Rd, 1, 3, 0);
  736. }
  737. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  738. {
  739. ThrowIfInvalid(Index, Size);
  740. Context.EmitLdvec(Reg);
  741. Context.EmitLdc_I4(Index);
  742. Context.EmitLdc_I4(Size);
  743. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  744. Context.EmitStvec(Reg);
  745. }
  746. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  747. {
  748. ThrowIfInvalid(Index, Size);
  749. Context.EmitLdvectmp();
  750. Context.EmitLdc_I4(Index);
  751. Context.EmitLdc_I4(Size);
  752. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  753. Context.EmitStvectmp();
  754. }
  755. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  756. {
  757. ThrowIfInvalid(Index, Size);
  758. Context.EmitLdc_I8(Value);
  759. Context.EmitLdvec(Reg);
  760. Context.EmitLdc_I4(Index);
  761. Context.EmitLdc_I4(Size);
  762. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  763. Context.EmitStvec(Reg);
  764. }
  765. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  766. {
  767. ThrowIfInvalidF(Index, Size);
  768. Context.EmitLdvec(Reg);
  769. Context.EmitLdc_I4(Index);
  770. if (Size == 0)
  771. {
  772. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
  773. }
  774. else if (Size == 1)
  775. {
  776. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
  777. }
  778. else
  779. {
  780. throw new ArgumentOutOfRangeException(nameof(Size));
  781. }
  782. Context.EmitStvec(Reg);
  783. }
  784. public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
  785. {
  786. ThrowIfInvalidF(Index, Size);
  787. Context.EmitLdvectmp();
  788. Context.EmitLdc_I4(Index);
  789. if (Size == 0)
  790. {
  791. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
  792. }
  793. else if (Size == 1)
  794. {
  795. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
  796. }
  797. else
  798. {
  799. throw new ArgumentOutOfRangeException(nameof(Size));
  800. }
  801. Context.EmitStvectmp();
  802. }
  803. private static void ThrowIfInvalid(int Index, int Size)
  804. {
  805. if ((uint)Size > 3)
  806. {
  807. throw new ArgumentOutOfRangeException(nameof(Size));
  808. }
  809. if ((uint)Index >= 16 >> Size)
  810. {
  811. throw new ArgumentOutOfRangeException(nameof(Index));
  812. }
  813. }
  814. private static void ThrowIfInvalidF(int Index, int Size)
  815. {
  816. if ((uint)Size > 1)
  817. {
  818. throw new ArgumentOutOfRangeException(nameof(Size));
  819. }
  820. if ((uint)Index >= 4 >> Size)
  821. {
  822. throw new ArgumentOutOfRangeException(nameof(Index));
  823. }
  824. }
  825. }
  826. }