AInstEmitSimdHelper.cs 27 KB

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