AInstEmitSimdHelper.cs 26 KB

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