AInstEmitSimdHelper.cs 22 KB

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