AInstEmitSimdHelper.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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.Intrinsics;
  8. using System.Runtime.Intrinsics.X86;
  9. namespace ChocolArm64.Instruction
  10. {
  11. static class AInstEmitSimdHelper
  12. {
  13. public static readonly Type[] IntTypesPerSizeLog2 = new Type[]
  14. {
  15. typeof(sbyte),
  16. typeof(short),
  17. typeof(int),
  18. typeof(long)
  19. };
  20. public static readonly Type[] UIntTypesPerSizeLog2 = new Type[]
  21. {
  22. typeof(byte),
  23. typeof(ushort),
  24. typeof(uint),
  25. typeof(ulong)
  26. };
  27. public static readonly Type[] VectorIntTypesPerSizeLog2 = new Type[]
  28. {
  29. typeof(Vector128<sbyte>),
  30. typeof(Vector128<short>),
  31. typeof(Vector128<int>),
  32. typeof(Vector128<long>)
  33. };
  34. public static readonly Type[] VectorUIntTypesPerSizeLog2 = new Type[]
  35. {
  36. typeof(Vector128<byte>),
  37. typeof(Vector128<ushort>),
  38. typeof(Vector128<uint>),
  39. typeof(Vector128<ulong>)
  40. };
  41. [Flags]
  42. public enum OperFlags
  43. {
  44. Rd = 1 << 0,
  45. Rn = 1 << 1,
  46. Rm = 1 << 2,
  47. Ra = 1 << 3,
  48. RnRm = Rn | Rm,
  49. RdRn = Rd | Rn,
  50. RaRnRm = Ra | Rn | Rm,
  51. RdRnRm = Rd | Rn | Rm
  52. }
  53. public static int GetImmShl(AOpCodeSimdShImm Op)
  54. {
  55. return Op.Imm - (8 << Op.Size);
  56. }
  57. public static int GetImmShr(AOpCodeSimdShImm Op)
  58. {
  59. return (8 << (Op.Size + 1)) - Op.Imm;
  60. }
  61. public static void EmitSse2Op(AILEmitterCtx Context, string Name)
  62. {
  63. EmitSseOp(Context, Name, typeof(Sse2));
  64. }
  65. public static void EmitSse41Op(AILEmitterCtx Context, string Name)
  66. {
  67. EmitSseOp(Context, Name, typeof(Sse41));
  68. }
  69. public static void EmitSse42Op(AILEmitterCtx Context, string Name)
  70. {
  71. EmitSseOp(Context, Name, typeof(Sse42));
  72. }
  73. private static void EmitSseOp(AILEmitterCtx Context, string Name, Type Type)
  74. {
  75. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  76. EmitLdvecWithSignedCast(Context, Op.Rn, Op.Size);
  77. Type BaseType = VectorIntTypesPerSizeLog2[Op.Size];
  78. if (Op is AOpCodeSimdReg BinOp)
  79. {
  80. EmitLdvecWithSignedCast(Context, BinOp.Rm, Op.Size);
  81. Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType, BaseType }));
  82. }
  83. else
  84. {
  85. Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType }));
  86. }
  87. EmitStvecWithSignedCast(Context, Op.Rd, Op.Size);
  88. if (Op.RegisterSize == ARegisterSize.SIMD64)
  89. {
  90. EmitVectorZeroUpper(Context, Op.Rd);
  91. }
  92. }
  93. public static void EmitLdvecWithSignedCast(AILEmitterCtx Context, int Reg, int Size)
  94. {
  95. Context.EmitLdvec(Reg);
  96. switch (Size)
  97. {
  98. case 0: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToSByte)); break;
  99. case 1: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt16)); break;
  100. case 2: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt32)); break;
  101. case 3: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt64)); break;
  102. default: throw new ArgumentOutOfRangeException(nameof(Size));
  103. }
  104. }
  105. public static void EmitLdvecWithCastToDouble(AILEmitterCtx Context, int Reg)
  106. {
  107. Context.EmitLdvec(Reg);
  108. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToDouble));
  109. }
  110. public static void EmitStvecWithCastFromDouble(AILEmitterCtx Context, int Reg)
  111. {
  112. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorDoubleToSingle));
  113. Context.EmitStvec(Reg);
  114. }
  115. public static void EmitLdvecWithUnsignedCast(AILEmitterCtx Context, int Reg, int Size)
  116. {
  117. Context.EmitLdvec(Reg);
  118. switch (Size)
  119. {
  120. case 0: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToByte)); break;
  121. case 1: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToUInt16)); break;
  122. case 2: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToUInt32)); break;
  123. case 3: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToUInt64)); break;
  124. default: throw new ArgumentOutOfRangeException(nameof(Size));
  125. }
  126. }
  127. public static void EmitStvecWithSignedCast(AILEmitterCtx Context, int Reg, int Size)
  128. {
  129. switch (Size)
  130. {
  131. case 0: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSByteToSingle)); break;
  132. case 1: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt16ToSingle)); break;
  133. case 2: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt32ToSingle)); break;
  134. case 3: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt64ToSingle)); break;
  135. default: throw new ArgumentOutOfRangeException(nameof(Size));
  136. }
  137. Context.EmitStvec(Reg);
  138. }
  139. public static void EmitStvecWithUnsignedCast(AILEmitterCtx Context, int Reg, int Size)
  140. {
  141. switch (Size)
  142. {
  143. case 0: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorByteToSingle)); break;
  144. case 1: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorUInt16ToSingle)); break;
  145. case 2: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorUInt32ToSingle)); break;
  146. case 3: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorUInt64ToSingle)); break;
  147. default: throw new ArgumentOutOfRangeException(nameof(Size));
  148. }
  149. Context.EmitStvec(Reg);
  150. }
  151. public static void EmitScalarSseOrSse2OpF(AILEmitterCtx Context, string Name)
  152. {
  153. EmitSseOrSse2OpF(Context, Name, true);
  154. }
  155. public static void EmitVectorSseOrSse2OpF(AILEmitterCtx Context, string Name)
  156. {
  157. EmitSseOrSse2OpF(Context, Name, false);
  158. }
  159. public static void EmitSseOrSse2OpF(AILEmitterCtx Context, string Name, bool Scalar)
  160. {
  161. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  162. int SizeF = Op.Size & 1;
  163. void Ldvec(int Reg)
  164. {
  165. Context.EmitLdvec(Reg);
  166. if (SizeF == 1)
  167. {
  168. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToDouble));
  169. }
  170. }
  171. Ldvec(Op.Rn);
  172. Type Type;
  173. Type BaseType;
  174. if (SizeF == 0)
  175. {
  176. Type = typeof(Sse);
  177. BaseType = typeof(Vector128<float>);
  178. }
  179. else /* if (SizeF == 1) */
  180. {
  181. Type = typeof(Sse2);
  182. BaseType = typeof(Vector128<double>);
  183. }
  184. if (Op is AOpCodeSimdReg BinOp)
  185. {
  186. Ldvec(BinOp.Rm);
  187. Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType, BaseType }));
  188. }
  189. else
  190. {
  191. Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType }));
  192. }
  193. if (SizeF == 1)
  194. {
  195. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorDoubleToSingle));
  196. }
  197. Context.EmitStvec(Op.Rd);
  198. if (Scalar)
  199. {
  200. if (SizeF == 0)
  201. {
  202. EmitVectorZero32_128(Context, Op.Rd);
  203. }
  204. else /* if (SizeF == 1) */
  205. {
  206. EmitVectorZeroUpper(Context, Op.Rd);
  207. }
  208. }
  209. else if (Op.RegisterSize == ARegisterSize.SIMD64)
  210. {
  211. EmitVectorZeroUpper(Context, Op.Rd);
  212. }
  213. }
  214. public static void EmitUnaryMathCall(AILEmitterCtx Context, string Name)
  215. {
  216. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  217. int SizeF = Op.Size & 1;
  218. MethodInfo MthdInfo;
  219. if (SizeF == 0)
  220. {
  221. MthdInfo = typeof(MathF).GetMethod(Name, new Type[] { typeof(float) });
  222. }
  223. else /* if (SizeF == 1) */
  224. {
  225. MthdInfo = typeof(Math).GetMethod(Name, new Type[] { typeof(double) });
  226. }
  227. Context.EmitCall(MthdInfo);
  228. }
  229. public static void EmitBinaryMathCall(AILEmitterCtx Context, string Name)
  230. {
  231. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  232. int SizeF = Op.Size & 1;
  233. MethodInfo MthdInfo;
  234. if (SizeF == 0)
  235. {
  236. MthdInfo = typeof(MathF).GetMethod(Name, new Type[] { typeof(float), typeof(float) });
  237. }
  238. else /* if (SizeF == 1) */
  239. {
  240. MthdInfo = typeof(Math).GetMethod(Name, new Type[] { typeof(double), typeof(double) });
  241. }
  242. Context.EmitCall(MthdInfo);
  243. }
  244. public static void EmitRoundMathCall(AILEmitterCtx Context, MidpointRounding RoundMode)
  245. {
  246. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  247. int SizeF = Op.Size & 1;
  248. MethodInfo MthdInfo;
  249. if (SizeF == 0)
  250. {
  251. MthdInfo = typeof(MathF).GetMethod(nameof(MathF.Round), new Type[] { typeof(float), typeof(MidpointRounding) });
  252. }
  253. else /* if (SizeF == 1) */
  254. {
  255. MthdInfo = typeof(Math).GetMethod(nameof(Math.Round), new Type[] { typeof(double), typeof(MidpointRounding) });
  256. }
  257. Context.EmitLdc_I4((int)RoundMode);
  258. Context.EmitCall(MthdInfo);
  259. }
  260. public static void EmitUnarySoftFloatCall(AILEmitterCtx Context, string Name)
  261. {
  262. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  263. int SizeF = Op.Size & 1;
  264. MethodInfo MthdInfo;
  265. if (SizeF == 0)
  266. {
  267. MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(float) });
  268. }
  269. else /* if (SizeF == 1) */
  270. {
  271. MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(double) });
  272. }
  273. Context.EmitCall(MthdInfo);
  274. }
  275. public static void EmitSoftFloatCall(AILEmitterCtx Context, string Name)
  276. {
  277. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  278. Type Type = (Op.Size & 1) == 0
  279. ? typeof(ASoftFloat_32)
  280. : typeof(ASoftFloat_64);
  281. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  282. Context.EmitCall(Type, Name);
  283. }
  284. public static void EmitScalarBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  285. {
  286. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  287. EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: false);
  288. }
  289. public static void EmitScalarTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  290. {
  291. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  292. EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: true);
  293. }
  294. public static void EmitScalarOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  295. {
  296. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  297. int SizeF = Op.Size & 1;
  298. if (Ternary)
  299. {
  300. EmitVectorExtractF(Context, Op.Rd, 0, SizeF);
  301. }
  302. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  303. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  304. Emit();
  305. EmitScalarSetF(Context, Op.Rd, SizeF);
  306. }
  307. public static void EmitScalarUnaryOpSx(AILEmitterCtx Context, Action Emit)
  308. {
  309. EmitScalarOp(Context, Emit, OperFlags.Rn, true);
  310. }
  311. public static void EmitScalarBinaryOpSx(AILEmitterCtx Context, Action Emit)
  312. {
  313. EmitScalarOp(Context, Emit, OperFlags.RnRm, true);
  314. }
  315. public static void EmitScalarUnaryOpZx(AILEmitterCtx Context, Action Emit)
  316. {
  317. EmitScalarOp(Context, Emit, OperFlags.Rn, false);
  318. }
  319. public static void EmitScalarBinaryOpZx(AILEmitterCtx Context, Action Emit)
  320. {
  321. EmitScalarOp(Context, Emit, OperFlags.RnRm, false);
  322. }
  323. public static void EmitScalarTernaryOpZx(AILEmitterCtx Context, Action Emit)
  324. {
  325. EmitScalarOp(Context, Emit, OperFlags.RdRnRm, false);
  326. }
  327. public static void EmitScalarOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  328. {
  329. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  330. bool Rd = (Opers & OperFlags.Rd) != 0;
  331. bool Rn = (Opers & OperFlags.Rn) != 0;
  332. bool Rm = (Opers & OperFlags.Rm) != 0;
  333. if (Rd)
  334. {
  335. EmitVectorExtract(Context, Op.Rd, 0, Op.Size, Signed);
  336. }
  337. if (Rn)
  338. {
  339. EmitVectorExtract(Context, Op.Rn, 0, Op.Size, Signed);
  340. }
  341. if (Rm)
  342. {
  343. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, 0, Op.Size, Signed);
  344. }
  345. Emit();
  346. EmitScalarSet(Context, Op.Rd, Op.Size);
  347. }
  348. public static void EmitScalarUnaryOpF(AILEmitterCtx Context, Action Emit)
  349. {
  350. EmitScalarOpF(Context, Emit, OperFlags.Rn);
  351. }
  352. public static void EmitScalarBinaryOpF(AILEmitterCtx Context, Action Emit)
  353. {
  354. EmitScalarOpF(Context, Emit, OperFlags.RnRm);
  355. }
  356. public static void EmitScalarTernaryRaOpF(AILEmitterCtx Context, Action Emit)
  357. {
  358. EmitScalarOpF(Context, Emit, OperFlags.RaRnRm);
  359. }
  360. public static void EmitScalarOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  361. {
  362. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  363. int SizeF = Op.Size & 1;
  364. bool Ra = (Opers & OperFlags.Ra) != 0;
  365. bool Rn = (Opers & OperFlags.Rn) != 0;
  366. bool Rm = (Opers & OperFlags.Rm) != 0;
  367. if (Ra)
  368. {
  369. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Ra, 0, SizeF);
  370. }
  371. if (Rn)
  372. {
  373. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  374. }
  375. if (Rm)
  376. {
  377. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, 0, SizeF);
  378. }
  379. Emit();
  380. EmitScalarSetF(Context, Op.Rd, SizeF);
  381. }
  382. public static void EmitVectorUnaryOpF(AILEmitterCtx Context, Action Emit)
  383. {
  384. EmitVectorOpF(Context, Emit, OperFlags.Rn);
  385. }
  386. public static void EmitVectorBinaryOpF(AILEmitterCtx Context, Action Emit)
  387. {
  388. EmitVectorOpF(Context, Emit, OperFlags.RnRm);
  389. }
  390. public static void EmitVectorTernaryOpF(AILEmitterCtx Context, Action Emit)
  391. {
  392. EmitVectorOpF(Context, Emit, OperFlags.RdRnRm);
  393. }
  394. public static void EmitVectorOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  395. {
  396. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  397. int SizeF = Op.Size & 1;
  398. int Bytes = Op.GetBitsCount() >> 3;
  399. int Elems = Bytes >> SizeF + 2;
  400. bool Rd = (Opers & OperFlags.Rd) != 0;
  401. bool Rn = (Opers & OperFlags.Rn) != 0;
  402. bool Rm = (Opers & OperFlags.Rm) != 0;
  403. for (int Index = 0; Index < Elems; Index++)
  404. {
  405. if (Rd)
  406. {
  407. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  408. }
  409. if (Rn)
  410. {
  411. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  412. }
  413. if (Rm)
  414. {
  415. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, Index, SizeF);
  416. }
  417. Emit();
  418. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  419. }
  420. if (Op.RegisterSize == ARegisterSize.SIMD64)
  421. {
  422. EmitVectorZeroUpper(Context, Op.Rd);
  423. }
  424. }
  425. public static void EmitVectorBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  426. {
  427. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  428. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: false);
  429. }
  430. public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  431. {
  432. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  433. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: true);
  434. }
  435. public static void EmitVectorOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  436. {
  437. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  438. int SizeF = Op.Size & 1;
  439. int Bytes = Op.GetBitsCount() >> 3;
  440. int Elems = Bytes >> SizeF + 2;
  441. for (int Index = 0; Index < Elems; Index++)
  442. {
  443. if (Ternary)
  444. {
  445. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  446. }
  447. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  448. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  449. Emit();
  450. EmitVectorInsertTmpF(Context, Index, SizeF);
  451. }
  452. Context.EmitLdvectmp();
  453. Context.EmitStvec(Op.Rd);
  454. if (Op.RegisterSize == ARegisterSize.SIMD64)
  455. {
  456. EmitVectorZeroUpper(Context, Op.Rd);
  457. }
  458. }
  459. public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
  460. {
  461. EmitVectorOp(Context, Emit, OperFlags.Rn, true);
  462. }
  463. public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
  464. {
  465. EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
  466. }
  467. public static void EmitVectorTernaryOpSx(AILEmitterCtx Context, Action Emit)
  468. {
  469. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, true);
  470. }
  471. public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
  472. {
  473. EmitVectorOp(Context, Emit, OperFlags.Rn, false);
  474. }
  475. public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
  476. {
  477. EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
  478. }
  479. public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
  480. {
  481. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
  482. }
  483. public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  484. {
  485. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  486. int Bytes = Op.GetBitsCount() >> 3;
  487. int Elems = Bytes >> Op.Size;
  488. bool Rd = (Opers & OperFlags.Rd) != 0;
  489. bool Rn = (Opers & OperFlags.Rn) != 0;
  490. bool Rm = (Opers & OperFlags.Rm) != 0;
  491. for (int Index = 0; Index < Elems; Index++)
  492. {
  493. if (Rd)
  494. {
  495. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  496. }
  497. if (Rn)
  498. {
  499. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  500. }
  501. if (Rm)
  502. {
  503. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  504. }
  505. Emit();
  506. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  507. }
  508. if (Op.RegisterSize == ARegisterSize.SIMD64)
  509. {
  510. EmitVectorZeroUpper(Context, Op.Rd);
  511. }
  512. }
  513. public static void EmitVectorBinaryOpByElemSx(AILEmitterCtx Context, Action Emit)
  514. {
  515. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  516. EmitVectorOpByElem(Context, Emit, Op.Index, false, true);
  517. }
  518. public static void EmitVectorBinaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  519. {
  520. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  521. EmitVectorOpByElem(Context, Emit, Op.Index, false, false);
  522. }
  523. public static void EmitVectorTernaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  524. {
  525. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  526. EmitVectorOpByElem(Context, Emit, Op.Index, true, false);
  527. }
  528. public static void EmitVectorOpByElem(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary, bool Signed)
  529. {
  530. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  531. int Bytes = Op.GetBitsCount() >> 3;
  532. int Elems = Bytes >> Op.Size;
  533. EmitVectorExtract(Context, Op.Rm, Elem, Op.Size, Signed);
  534. Context.EmitSttmp();
  535. for (int Index = 0; Index < Elems; Index++)
  536. {
  537. if (Ternary)
  538. {
  539. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  540. }
  541. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  542. Context.EmitLdtmp();
  543. Emit();
  544. EmitVectorInsertTmp(Context, Index, Op.Size);
  545. }
  546. Context.EmitLdvectmp();
  547. Context.EmitStvec(Op.Rd);
  548. if (Op.RegisterSize == ARegisterSize.SIMD64)
  549. {
  550. EmitVectorZeroUpper(Context, Op.Rd);
  551. }
  552. }
  553. public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
  554. {
  555. EmitVectorImmOp(Context, Emit, false);
  556. }
  557. public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
  558. {
  559. EmitVectorImmOp(Context, Emit, true);
  560. }
  561. public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
  562. {
  563. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  564. int Bytes = Op.GetBitsCount() >> 3;
  565. int Elems = Bytes >> Op.Size;
  566. for (int Index = 0; Index < Elems; Index++)
  567. {
  568. if (Binary)
  569. {
  570. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  571. }
  572. Context.EmitLdc_I8(Op.Imm);
  573. Emit();
  574. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  575. }
  576. if (Op.RegisterSize == ARegisterSize.SIMD64)
  577. {
  578. EmitVectorZeroUpper(Context, Op.Rd);
  579. }
  580. }
  581. public static void EmitVectorWidenRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  582. {
  583. EmitVectorWidenRmBinaryOp(Context, Emit, true);
  584. }
  585. public static void EmitVectorWidenRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  586. {
  587. EmitVectorWidenRmBinaryOp(Context, Emit, false);
  588. }
  589. public static void EmitVectorWidenRmBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  590. {
  591. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  592. int Elems = 8 >> Op.Size;
  593. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  594. for (int Index = 0; Index < Elems; Index++)
  595. {
  596. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  597. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  598. Emit();
  599. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  600. }
  601. Context.EmitLdvectmp();
  602. Context.EmitStvec(Op.Rd);
  603. }
  604. public static void EmitVectorWidenRnRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  605. {
  606. EmitVectorWidenRnRmOp(Context, Emit, false, true);
  607. }
  608. public static void EmitVectorWidenRnRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  609. {
  610. EmitVectorWidenRnRmOp(Context, Emit, false, false);
  611. }
  612. public static void EmitVectorWidenRnRmTernaryOpSx(AILEmitterCtx Context, Action Emit)
  613. {
  614. EmitVectorWidenRnRmOp(Context, Emit, true, true);
  615. }
  616. public static void EmitVectorWidenRnRmTernaryOpZx(AILEmitterCtx Context, Action Emit)
  617. {
  618. EmitVectorWidenRnRmOp(Context, Emit, true, false);
  619. }
  620. public static void EmitVectorWidenRnRmOp(AILEmitterCtx Context, Action Emit, bool Ternary, bool Signed)
  621. {
  622. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  623. int Elems = 8 >> Op.Size;
  624. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  625. for (int Index = 0; Index < Elems; Index++)
  626. {
  627. if (Ternary)
  628. {
  629. EmitVectorExtract(Context, Op.Rd, Index, Op.Size + 1, Signed);
  630. }
  631. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  632. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  633. Emit();
  634. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  635. }
  636. Context.EmitLdvectmp();
  637. Context.EmitStvec(Op.Rd);
  638. }
  639. public static void EmitVectorPairwiseOpSx(AILEmitterCtx Context, Action Emit)
  640. {
  641. EmitVectorPairwiseOp(Context, Emit, true);
  642. }
  643. public static void EmitVectorPairwiseOpZx(AILEmitterCtx Context, Action Emit)
  644. {
  645. EmitVectorPairwiseOp(Context, Emit, false);
  646. }
  647. public static void EmitVectorPairwiseOp(AILEmitterCtx Context, Action Emit, bool Signed)
  648. {
  649. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  650. int Words = Op.GetBitsCount() >> 4;
  651. int Pairs = Words >> Op.Size;
  652. for (int Index = 0; Index < Pairs; Index++)
  653. {
  654. int Idx = Index << 1;
  655. EmitVectorExtract(Context, Op.Rn, Idx, Op.Size, Signed);
  656. EmitVectorExtract(Context, Op.Rn, Idx + 1, Op.Size, Signed);
  657. Emit();
  658. EmitVectorExtract(Context, Op.Rm, Idx, Op.Size, Signed);
  659. EmitVectorExtract(Context, Op.Rm, Idx + 1, Op.Size, Signed);
  660. Emit();
  661. EmitVectorInsertTmp(Context, Pairs + Index, Op.Size);
  662. EmitVectorInsertTmp(Context, Index, Op.Size);
  663. }
  664. Context.EmitLdvectmp();
  665. Context.EmitStvec(Op.Rd);
  666. if (Op.RegisterSize == ARegisterSize.SIMD64)
  667. {
  668. EmitVectorZeroUpper(Context, Op.Rd);
  669. }
  670. }
  671. public static void EmitVectorPairwiseOpF(AILEmitterCtx Context, Action Emit)
  672. {
  673. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  674. int SizeF = Op.Size & 1;
  675. int Words = Op.GetBitsCount() >> 4;
  676. int Pairs = Words >> SizeF + 2;
  677. for (int Index = 0; Index < Pairs; Index++)
  678. {
  679. int Idx = Index << 1;
  680. EmitVectorExtractF(Context, Op.Rn, Idx, SizeF);
  681. EmitVectorExtractF(Context, Op.Rn, Idx + 1, SizeF);
  682. Emit();
  683. EmitVectorExtractF(Context, Op.Rm, Idx, SizeF);
  684. EmitVectorExtractF(Context, Op.Rm, Idx + 1, SizeF);
  685. Emit();
  686. EmitVectorInsertTmpF(Context, Pairs + Index, SizeF);
  687. EmitVectorInsertTmpF(Context, Index, SizeF);
  688. }
  689. Context.EmitLdvectmp();
  690. Context.EmitStvec(Op.Rd);
  691. if (Op.RegisterSize == ARegisterSize.SIMD64)
  692. {
  693. EmitVectorZeroUpper(Context, Op.Rd);
  694. }
  695. }
  696. [Flags]
  697. public enum SaturatingFlags
  698. {
  699. Scalar = 1 << 0,
  700. Signed = 1 << 1,
  701. Add = 1 << 2,
  702. Sub = 1 << 3,
  703. Accumulate = 1 << 4,
  704. ScalarSx = Scalar | Signed,
  705. ScalarZx = Scalar,
  706. VectorSx = Signed,
  707. VectorZx = 0
  708. }
  709. public static void EmitScalarSaturatingUnaryOpSx(AILEmitterCtx Context, Action Emit)
  710. {
  711. EmitSaturatingUnaryOpSx(Context, Emit, SaturatingFlags.ScalarSx);
  712. }
  713. public static void EmitVectorSaturatingUnaryOpSx(AILEmitterCtx Context, Action Emit)
  714. {
  715. EmitSaturatingUnaryOpSx(Context, Emit, SaturatingFlags.VectorSx);
  716. }
  717. public static void EmitSaturatingUnaryOpSx(AILEmitterCtx Context, Action Emit, SaturatingFlags Flags)
  718. {
  719. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  720. bool Scalar = (Flags & SaturatingFlags.Scalar) != 0;
  721. int Bytes = Op.GetBitsCount() >> 3;
  722. int Elems = !Scalar ? Bytes >> Op.Size : 1;
  723. if (Scalar)
  724. {
  725. EmitVectorZeroLowerTmp(Context);
  726. }
  727. for (int Index = 0; Index < Elems; Index++)
  728. {
  729. EmitVectorExtractSx(Context, Op.Rn, Index, Op.Size);
  730. Emit();
  731. if (Op.Size <= 2)
  732. {
  733. EmitSatQ(Context, Op.Size, true, true);
  734. }
  735. else /* if (Op.Size == 3) */
  736. {
  737. EmitUnarySignedSatQAbsOrNeg(Context);
  738. }
  739. EmitVectorInsertTmp(Context, Index, Op.Size);
  740. }
  741. Context.EmitLdvectmp();
  742. Context.EmitStvec(Op.Rd);
  743. if ((Op.RegisterSize == ARegisterSize.SIMD64) || Scalar)
  744. {
  745. EmitVectorZeroUpper(Context, Op.Rd);
  746. }
  747. }
  748. public static void EmitScalarSaturatingBinaryOpSx(AILEmitterCtx Context, SaturatingFlags Flags)
  749. {
  750. EmitSaturatingBinaryOp(Context, () => { }, SaturatingFlags.ScalarSx | Flags);
  751. }
  752. public static void EmitScalarSaturatingBinaryOpZx(AILEmitterCtx Context, SaturatingFlags Flags)
  753. {
  754. EmitSaturatingBinaryOp(Context, () => { }, SaturatingFlags.ScalarZx | Flags);
  755. }
  756. public static void EmitVectorSaturatingBinaryOpSx(AILEmitterCtx Context, SaturatingFlags Flags)
  757. {
  758. EmitSaturatingBinaryOp(Context, () => { }, SaturatingFlags.VectorSx | Flags);
  759. }
  760. public static void EmitVectorSaturatingBinaryOpZx(AILEmitterCtx Context, SaturatingFlags Flags)
  761. {
  762. EmitSaturatingBinaryOp(Context, () => { }, SaturatingFlags.VectorZx | Flags);
  763. }
  764. public static void EmitSaturatingBinaryOp(AILEmitterCtx Context, Action Emit, SaturatingFlags Flags)
  765. {
  766. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  767. bool Scalar = (Flags & SaturatingFlags.Scalar) != 0;
  768. bool Signed = (Flags & SaturatingFlags.Signed) != 0;
  769. bool Add = (Flags & SaturatingFlags.Add) != 0;
  770. bool Sub = (Flags & SaturatingFlags.Sub) != 0;
  771. bool Accumulate = (Flags & SaturatingFlags.Accumulate) != 0;
  772. int Bytes = Op.GetBitsCount() >> 3;
  773. int Elems = !Scalar ? Bytes >> Op.Size : 1;
  774. if (Scalar)
  775. {
  776. EmitVectorZeroLowerTmp(Context);
  777. }
  778. if (Add || Sub)
  779. {
  780. for (int Index = 0; Index < Elems; Index++)
  781. {
  782. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  783. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  784. if (Op.Size <= 2)
  785. {
  786. Context.Emit(Add ? OpCodes.Add : OpCodes.Sub);
  787. EmitSatQ(Context, Op.Size, true, Signed);
  788. }
  789. else /* if (Op.Size == 3) */
  790. {
  791. if (Add)
  792. {
  793. EmitBinarySatQAdd(Context, Signed);
  794. }
  795. else /* if (Sub) */
  796. {
  797. EmitBinarySatQSub(Context, Signed);
  798. }
  799. }
  800. EmitVectorInsertTmp(Context, Index, Op.Size);
  801. }
  802. }
  803. else if (Accumulate)
  804. {
  805. for (int Index = 0; Index < Elems; Index++)
  806. {
  807. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, !Signed);
  808. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  809. if (Op.Size <= 2)
  810. {
  811. Context.Emit(OpCodes.Add);
  812. EmitSatQ(Context, Op.Size, true, Signed);
  813. }
  814. else /* if (Op.Size == 3) */
  815. {
  816. EmitBinarySatQAccumulate(Context, Signed);
  817. }
  818. EmitVectorInsertTmp(Context, Index, Op.Size);
  819. }
  820. }
  821. else
  822. {
  823. for (int Index = 0; Index < Elems; Index++)
  824. {
  825. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  826. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  827. Emit();
  828. EmitSatQ(Context, Op.Size, true, Signed);
  829. EmitVectorInsertTmp(Context, Index, Op.Size);
  830. }
  831. }
  832. Context.EmitLdvectmp();
  833. Context.EmitStvec(Op.Rd);
  834. if ((Op.RegisterSize == ARegisterSize.SIMD64) || Scalar)
  835. {
  836. EmitVectorZeroUpper(Context, Op.Rd);
  837. }
  838. }
  839. [Flags]
  840. public enum SaturatingNarrowFlags
  841. {
  842. Scalar = 1 << 0,
  843. SignedSrc = 1 << 1,
  844. SignedDst = 1 << 2,
  845. ScalarSxSx = Scalar | SignedSrc | SignedDst,
  846. ScalarSxZx = Scalar | SignedSrc,
  847. ScalarZxZx = Scalar,
  848. VectorSxSx = SignedSrc | SignedDst,
  849. VectorSxZx = SignedSrc,
  850. VectorZxZx = 0
  851. }
  852. public static void EmitSaturatingNarrowOp(AILEmitterCtx Context, SaturatingNarrowFlags Flags)
  853. {
  854. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  855. bool Scalar = (Flags & SaturatingNarrowFlags.Scalar) != 0;
  856. bool SignedSrc = (Flags & SaturatingNarrowFlags.SignedSrc) != 0;
  857. bool SignedDst = (Flags & SaturatingNarrowFlags.SignedDst) != 0;
  858. int Elems = !Scalar ? 8 >> Op.Size : 1;
  859. int Part = !Scalar && (Op.RegisterSize == ARegisterSize.SIMD128) ? Elems : 0;
  860. if (Scalar)
  861. {
  862. EmitVectorZeroLowerTmp(Context);
  863. }
  864. if (Part != 0)
  865. {
  866. Context.EmitLdvec(Op.Rd);
  867. Context.EmitStvectmp();
  868. }
  869. for (int Index = 0; Index < Elems; Index++)
  870. {
  871. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, SignedSrc);
  872. EmitSatQ(Context, Op.Size, SignedSrc, SignedDst);
  873. EmitVectorInsertTmp(Context, Part + Index, Op.Size);
  874. }
  875. Context.EmitLdvectmp();
  876. Context.EmitStvec(Op.Rd);
  877. if (Part == 0)
  878. {
  879. EmitVectorZeroUpper(Context, Op.Rd);
  880. }
  881. }
  882. // TSrc (16bit, 32bit, 64bit; signed, unsigned) > TDst (8bit, 16bit, 32bit; signed, unsigned).
  883. public static void EmitSatQ(
  884. AILEmitterCtx Context,
  885. int SizeDst,
  886. bool SignedSrc,
  887. bool SignedDst)
  888. {
  889. if (SizeDst > 2)
  890. {
  891. throw new ArgumentOutOfRangeException(nameof(SizeDst));
  892. }
  893. Context.EmitLdc_I4(SizeDst);
  894. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  895. if (SignedSrc)
  896. {
  897. ASoftFallback.EmitCall(Context, SignedDst
  898. ? nameof(ASoftFallback.SignedSrcSignedDstSatQ)
  899. : nameof(ASoftFallback.SignedSrcUnsignedDstSatQ));
  900. }
  901. else
  902. {
  903. ASoftFallback.EmitCall(Context, SignedDst
  904. ? nameof(ASoftFallback.UnsignedSrcSignedDstSatQ)
  905. : nameof(ASoftFallback.UnsignedSrcUnsignedDstSatQ));
  906. }
  907. }
  908. // TSrc (64bit) == TDst (64bit); signed.
  909. public static void EmitUnarySignedSatQAbsOrNeg(AILEmitterCtx Context)
  910. {
  911. if (((AOpCodeSimd)Context.CurrOp).Size < 3)
  912. {
  913. throw new InvalidOperationException();
  914. }
  915. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  916. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.UnarySignedSatQAbsOrNeg));
  917. }
  918. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  919. public static void EmitBinarySatQAdd(AILEmitterCtx Context, bool Signed)
  920. {
  921. if (((AOpCodeSimdReg)Context.CurrOp).Size < 3)
  922. {
  923. throw new InvalidOperationException();
  924. }
  925. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  926. ASoftFallback.EmitCall(Context, Signed
  927. ? nameof(ASoftFallback.BinarySignedSatQAdd)
  928. : nameof(ASoftFallback.BinaryUnsignedSatQAdd));
  929. }
  930. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  931. public static void EmitBinarySatQSub(AILEmitterCtx Context, bool Signed)
  932. {
  933. if (((AOpCodeSimdReg)Context.CurrOp).Size < 3)
  934. {
  935. throw new InvalidOperationException();
  936. }
  937. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  938. ASoftFallback.EmitCall(Context, Signed
  939. ? nameof(ASoftFallback.BinarySignedSatQSub)
  940. : nameof(ASoftFallback.BinaryUnsignedSatQSub));
  941. }
  942. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  943. public static void EmitBinarySatQAccumulate(AILEmitterCtx Context, bool Signed)
  944. {
  945. if (((AOpCodeSimd)Context.CurrOp).Size < 3)
  946. {
  947. throw new InvalidOperationException();
  948. }
  949. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  950. ASoftFallback.EmitCall(Context, Signed
  951. ? nameof(ASoftFallback.BinarySignedSatQAcc)
  952. : nameof(ASoftFallback.BinaryUnsignedSatQAcc));
  953. }
  954. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  955. {
  956. EmitVectorZeroAll(Context, Reg);
  957. EmitVectorInsert(Context, Reg, 0, Size);
  958. }
  959. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  960. {
  961. if (AOptimizations.UseSse41 && Size == 0)
  962. {
  963. //If the type is float, we can perform insertion and
  964. //zero the upper bits with a single instruction (INSERTPS);
  965. Context.EmitLdvec(Reg);
  966. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.Sse41VectorInsertScalarSingle));
  967. Context.EmitStvec(Reg);
  968. }
  969. else
  970. {
  971. EmitVectorZeroAll(Context, Reg);
  972. EmitVectorInsertF(Context, Reg, 0, Size);
  973. }
  974. }
  975. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  976. {
  977. EmitVectorExtract(Context, Reg, Index, Size, true);
  978. }
  979. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  980. {
  981. EmitVectorExtract(Context, Reg, Index, Size, false);
  982. }
  983. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  984. {
  985. ThrowIfInvalid(Index, Size);
  986. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  987. Context.EmitLdvec(Reg);
  988. Context.EmitLdc_I4(Index);
  989. Context.EmitLdc_I4(Size);
  990. AVectorHelper.EmitCall(Context, Signed
  991. ? nameof(AVectorHelper.VectorExtractIntSx)
  992. : nameof(AVectorHelper.VectorExtractIntZx));
  993. }
  994. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  995. {
  996. ThrowIfInvalidF(Index, Size);
  997. Context.EmitLdvec(Reg);
  998. Context.EmitLdc_I4(Index);
  999. if (Size == 0)
  1000. {
  1001. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractSingle));
  1002. }
  1003. else if (Size == 1)
  1004. {
  1005. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractDouble));
  1006. }
  1007. else
  1008. {
  1009. throw new ArgumentOutOfRangeException(nameof(Size));
  1010. }
  1011. }
  1012. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  1013. {
  1014. if (AOptimizations.UseSse2)
  1015. {
  1016. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleZero));
  1017. Context.EmitStvec(Rd);
  1018. }
  1019. else
  1020. {
  1021. EmitVectorZeroLower(Context, Rd);
  1022. EmitVectorZeroUpper(Context, Rd);
  1023. }
  1024. }
  1025. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  1026. {
  1027. EmitVectorInsert(Context, Rd, 0, 3, 0);
  1028. }
  1029. public static void EmitVectorZeroLowerTmp(AILEmitterCtx Context)
  1030. {
  1031. EmitVectorInsertTmp(Context, 0, 3, 0);
  1032. }
  1033. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Reg)
  1034. {
  1035. if (AOptimizations.UseSse2)
  1036. {
  1037. //TODO: Use MoveScalar once it is fixed, as of the
  1038. //time of writing it just crashes the JIT.
  1039. EmitLdvecWithUnsignedCast(Context, Reg, 3);
  1040. Type[] Types = new Type[] { typeof(Vector128<ulong>), typeof(byte) };
  1041. //Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MoveScalar), Types));
  1042. Context.EmitLdc_I4(8);
  1043. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical128BitLane), Types));
  1044. Context.EmitLdc_I4(8);
  1045. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), Types));
  1046. EmitStvecWithUnsignedCast(Context, Reg, 3);
  1047. }
  1048. else
  1049. {
  1050. EmitVectorInsert(Context, Reg, 1, 3, 0);
  1051. }
  1052. }
  1053. public static void EmitVectorZero32_128(AILEmitterCtx Context, int Reg)
  1054. {
  1055. Context.EmitLdvec(Reg);
  1056. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorZero32_128));
  1057. Context.EmitStvec(Reg);
  1058. }
  1059. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  1060. {
  1061. ThrowIfInvalid(Index, Size);
  1062. Context.EmitLdvec(Reg);
  1063. Context.EmitLdc_I4(Index);
  1064. Context.EmitLdc_I4(Size);
  1065. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  1066. Context.EmitStvec(Reg);
  1067. }
  1068. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  1069. {
  1070. ThrowIfInvalid(Index, Size);
  1071. Context.EmitLdvectmp();
  1072. Context.EmitLdc_I4(Index);
  1073. Context.EmitLdc_I4(Size);
  1074. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  1075. Context.EmitStvectmp();
  1076. }
  1077. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  1078. {
  1079. ThrowIfInvalid(Index, Size);
  1080. Context.EmitLdc_I8(Value);
  1081. Context.EmitLdvec(Reg);
  1082. Context.EmitLdc_I4(Index);
  1083. Context.EmitLdc_I4(Size);
  1084. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  1085. Context.EmitStvec(Reg);
  1086. }
  1087. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size, long Value)
  1088. {
  1089. ThrowIfInvalid(Index, Size);
  1090. Context.EmitLdc_I8(Value);
  1091. Context.EmitLdvectmp();
  1092. Context.EmitLdc_I4(Index);
  1093. Context.EmitLdc_I4(Size);
  1094. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
  1095. Context.EmitStvectmp();
  1096. }
  1097. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  1098. {
  1099. ThrowIfInvalidF(Index, Size);
  1100. Context.EmitLdvec(Reg);
  1101. Context.EmitLdc_I4(Index);
  1102. if (Size == 0)
  1103. {
  1104. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
  1105. }
  1106. else if (Size == 1)
  1107. {
  1108. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
  1109. }
  1110. else
  1111. {
  1112. throw new ArgumentOutOfRangeException(nameof(Size));
  1113. }
  1114. Context.EmitStvec(Reg);
  1115. }
  1116. public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
  1117. {
  1118. ThrowIfInvalidF(Index, Size);
  1119. Context.EmitLdvectmp();
  1120. Context.EmitLdc_I4(Index);
  1121. if (Size == 0)
  1122. {
  1123. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
  1124. }
  1125. else if (Size == 1)
  1126. {
  1127. AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
  1128. }
  1129. else
  1130. {
  1131. throw new ArgumentOutOfRangeException(nameof(Size));
  1132. }
  1133. Context.EmitStvectmp();
  1134. }
  1135. private static void ThrowIfInvalid(int Index, int Size)
  1136. {
  1137. if ((uint)Size > 3)
  1138. {
  1139. throw new ArgumentOutOfRangeException(nameof(Size));
  1140. }
  1141. if ((uint)Index >= 16 >> Size)
  1142. {
  1143. throw new ArgumentOutOfRangeException(nameof(Index));
  1144. }
  1145. }
  1146. private static void ThrowIfInvalidF(int Index, int Size)
  1147. {
  1148. if ((uint)Size > 1)
  1149. {
  1150. throw new ArgumentOutOfRangeException(nameof(Size));
  1151. }
  1152. if ((uint)Index >= 4 >> Size)
  1153. {
  1154. throw new ArgumentOutOfRangeException(nameof(Index));
  1155. }
  1156. }
  1157. }
  1158. }