AInstEmitSimdHelper.cs 27 KB

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