AInstEmitSimdHelper.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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 EmitScalarBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  96. {
  97. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  98. EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: false);
  99. }
  100. public static void EmitScalarOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  101. {
  102. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  103. int SizeF = Op.Size & 1;
  104. if (Ternary)
  105. {
  106. EmitVectorExtractF(Context, Op.Rd, 0, SizeF);
  107. }
  108. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  109. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  110. Emit();
  111. EmitScalarSetF(Context, Op.Rd, SizeF);
  112. }
  113. public static void EmitScalarUnaryOpSx(AILEmitterCtx Context, Action Emit)
  114. {
  115. EmitScalarOp(Context, Emit, OperFlags.Rn, true);
  116. }
  117. public static void EmitScalarBinaryOpSx(AILEmitterCtx Context, Action Emit)
  118. {
  119. EmitScalarOp(Context, Emit, OperFlags.RnRm, true);
  120. }
  121. public static void EmitScalarUnaryOpZx(AILEmitterCtx Context, Action Emit)
  122. {
  123. EmitScalarOp(Context, Emit, OperFlags.Rn, false);
  124. }
  125. public static void EmitScalarBinaryOpZx(AILEmitterCtx Context, Action Emit)
  126. {
  127. EmitScalarOp(Context, Emit, OperFlags.RnRm, false);
  128. }
  129. public static void EmitScalarTernaryOpZx(AILEmitterCtx Context, Action Emit)
  130. {
  131. EmitScalarOp(Context, Emit, OperFlags.RdRnRm, false);
  132. }
  133. public static void EmitScalarOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  134. {
  135. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  136. if (Opers.HasFlag(OperFlags.Rd))
  137. {
  138. EmitVectorExtract(Context, Op.Rd, 0, Op.Size, Signed);
  139. }
  140. if (Opers.HasFlag(OperFlags.Rn))
  141. {
  142. EmitVectorExtract(Context, Op.Rn, 0, Op.Size, Signed);
  143. }
  144. if (Opers.HasFlag(OperFlags.Rm))
  145. {
  146. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, 0, Op.Size, Signed);
  147. }
  148. Emit();
  149. EmitScalarSet(Context, Op.Rd, Op.Size);
  150. }
  151. public static void EmitScalarUnaryOpF(AILEmitterCtx Context, Action Emit)
  152. {
  153. EmitScalarOpF(Context, Emit, OperFlags.Rn);
  154. }
  155. public static void EmitScalarBinaryOpF(AILEmitterCtx Context, Action Emit)
  156. {
  157. EmitScalarOpF(Context, Emit, OperFlags.RnRm);
  158. }
  159. public static void EmitScalarTernaryRaOpF(AILEmitterCtx Context, Action Emit)
  160. {
  161. EmitScalarOpF(Context, Emit, OperFlags.RaRnRm);
  162. }
  163. public static void EmitScalarOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  164. {
  165. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  166. int SizeF = Op.Size & 1;
  167. if (Opers.HasFlag(OperFlags.Ra))
  168. {
  169. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Ra, 0, SizeF);
  170. }
  171. if (Opers.HasFlag(OperFlags.Rn))
  172. {
  173. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  174. }
  175. if (Opers.HasFlag(OperFlags.Rm))
  176. {
  177. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, 0, SizeF);
  178. }
  179. Emit();
  180. EmitScalarSetF(Context, Op.Rd, SizeF);
  181. }
  182. public static void EmitVectorUnaryOpF(AILEmitterCtx Context, Action Emit)
  183. {
  184. EmitVectorOpF(Context, Emit, OperFlags.Rn);
  185. }
  186. public static void EmitVectorBinaryOpF(AILEmitterCtx Context, Action Emit)
  187. {
  188. EmitVectorOpF(Context, Emit, OperFlags.RnRm);
  189. }
  190. public static void EmitVectorTernaryOpF(AILEmitterCtx Context, Action Emit)
  191. {
  192. EmitVectorOpF(Context, Emit, OperFlags.RdRnRm);
  193. }
  194. public static void EmitVectorOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
  195. {
  196. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  197. int SizeF = Op.Size & 1;
  198. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  199. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  200. {
  201. if (Opers.HasFlag(OperFlags.Rd))
  202. {
  203. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  204. }
  205. if (Opers.HasFlag(OperFlags.Rn))
  206. {
  207. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  208. }
  209. if (Opers.HasFlag(OperFlags.Rm))
  210. {
  211. EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, Index, SizeF);
  212. }
  213. Emit();
  214. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  215. }
  216. if (Op.RegisterSize == ARegisterSize.SIMD64)
  217. {
  218. EmitVectorZeroUpper(Context, Op.Rd);
  219. }
  220. }
  221. public static void EmitVectorBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
  222. {
  223. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  224. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: false);
  225. }
  226. public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
  227. {
  228. AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
  229. EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: true);
  230. }
  231. public static void EmitVectorOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
  232. {
  233. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  234. int SizeF = Op.Size & 1;
  235. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  236. for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
  237. {
  238. if (Ternary)
  239. {
  240. EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
  241. }
  242. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  243. EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
  244. Emit();
  245. EmitVectorInsertTmpF(Context, Index, SizeF);
  246. }
  247. Context.EmitLdvectmp();
  248. Context.EmitStvec(Op.Rd);
  249. if (Op.RegisterSize == ARegisterSize.SIMD64)
  250. {
  251. EmitVectorZeroUpper(Context, Op.Rd);
  252. }
  253. }
  254. public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
  255. {
  256. EmitVectorOp(Context, Emit, OperFlags.Rn, true);
  257. }
  258. public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
  259. {
  260. EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
  261. }
  262. public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
  263. {
  264. EmitVectorOp(Context, Emit, OperFlags.Rn, false);
  265. }
  266. public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
  267. {
  268. EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
  269. }
  270. public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
  271. {
  272. EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
  273. }
  274. public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
  275. {
  276. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  277. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  278. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  279. {
  280. if (Opers.HasFlag(OperFlags.Rd))
  281. {
  282. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  283. }
  284. if (Opers.HasFlag(OperFlags.Rn))
  285. {
  286. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  287. }
  288. if (Opers.HasFlag(OperFlags.Rm))
  289. {
  290. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
  291. }
  292. Emit();
  293. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  294. }
  295. if (Op.RegisterSize == ARegisterSize.SIMD64)
  296. {
  297. EmitVectorZeroUpper(Context, Op.Rd);
  298. }
  299. }
  300. public static void EmitVectorBinaryOpByElemSx(AILEmitterCtx Context, Action Emit)
  301. {
  302. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  303. EmitVectorOpByElem(Context, Emit, Op.Index, false, true);
  304. }
  305. public static void EmitVectorBinaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  306. {
  307. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  308. EmitVectorOpByElem(Context, Emit, Op.Index, false, false);
  309. }
  310. public static void EmitVectorTernaryOpByElemZx(AILEmitterCtx Context, Action Emit)
  311. {
  312. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  313. EmitVectorOpByElem(Context, Emit, Op.Index, true, false);
  314. }
  315. public static void EmitVectorOpByElem(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary, bool Signed)
  316. {
  317. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  318. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  319. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  320. {
  321. if (Ternary)
  322. {
  323. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  324. }
  325. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  326. EmitVectorExtract(Context, Op.Rm, Elem, Op.Size, Signed);
  327. Emit();
  328. EmitVectorInsertTmp(Context, Index, Op.Size);
  329. }
  330. Context.EmitLdvectmp();
  331. Context.EmitStvec(Op.Rd);
  332. if (Op.RegisterSize == ARegisterSize.SIMD64)
  333. {
  334. EmitVectorZeroUpper(Context, Op.Rd);
  335. }
  336. }
  337. public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
  338. {
  339. EmitVectorImmOp(Context, Emit, false);
  340. }
  341. public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
  342. {
  343. EmitVectorImmOp(Context, Emit, true);
  344. }
  345. public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
  346. {
  347. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  348. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  349. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  350. {
  351. if (Binary)
  352. {
  353. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  354. }
  355. Context.EmitLdc_I8(Op.Imm);
  356. Emit();
  357. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  358. }
  359. if (Op.RegisterSize == ARegisterSize.SIMD64)
  360. {
  361. EmitVectorZeroUpper(Context, Op.Rd);
  362. }
  363. }
  364. public static void EmitVectorWidenRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  365. {
  366. EmitVectorWidenRmBinaryOp(Context, Emit, true);
  367. }
  368. public static void EmitVectorWidenRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  369. {
  370. EmitVectorWidenRmBinaryOp(Context, Emit, false);
  371. }
  372. public static void EmitVectorWidenRmBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
  373. {
  374. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  375. Context.EmitLdvec(Op.Rd);
  376. Context.EmitStvectmp();
  377. int Elems = 8 >> Op.Size;
  378. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  379. for (int Index = 0; Index < Elems; Index++)
  380. {
  381. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  382. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  383. Emit();
  384. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  385. }
  386. Context.EmitLdvectmp();
  387. Context.EmitStvec(Op.Rd);
  388. }
  389. public static void EmitVectorWidenRnRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
  390. {
  391. EmitVectorWidenRnRmOp(Context, Emit, false, true);
  392. }
  393. public static void EmitVectorWidenRnRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
  394. {
  395. EmitVectorWidenRnRmOp(Context, Emit, false, false);
  396. }
  397. public static void EmitVectorWidenRnRmTernaryOpSx(AILEmitterCtx Context, Action Emit)
  398. {
  399. EmitVectorWidenRnRmOp(Context, Emit, true, true);
  400. }
  401. public static void EmitVectorWidenRnRmTernaryOpZx(AILEmitterCtx Context, Action Emit)
  402. {
  403. EmitVectorWidenRnRmOp(Context, Emit, true, false);
  404. }
  405. public static void EmitVectorWidenRnRmOp(AILEmitterCtx Context, Action Emit, bool Ternary, bool Signed)
  406. {
  407. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  408. Context.EmitLdvec(Op.Rd);
  409. Context.EmitStvectmp();
  410. int Elems = 8 >> Op.Size;
  411. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  412. for (int Index = 0; Index < Elems; Index++)
  413. {
  414. if (Ternary)
  415. {
  416. EmitVectorExtract(Context, Op.Rd, Index, Op.Size + 1, Signed);
  417. }
  418. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  419. EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
  420. Emit();
  421. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  422. }
  423. Context.EmitLdvectmp();
  424. Context.EmitStvec(Op.Rd);
  425. }
  426. public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
  427. {
  428. EmitVectorZeroAll(Context, Reg);
  429. EmitVectorInsert(Context, Reg, 0, Size);
  430. }
  431. public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
  432. {
  433. EmitVectorZeroAll(Context, Reg);
  434. EmitVectorInsertF(Context, Reg, 0, Size);
  435. }
  436. public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
  437. {
  438. EmitVectorExtract(Context, Reg, Index, Size, true);
  439. }
  440. public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
  441. {
  442. EmitVectorExtract(Context, Reg, Index, Size, false);
  443. }
  444. public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
  445. {
  446. ThrowIfInvalid(Index, Size);
  447. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  448. Context.EmitLdvec(Reg);
  449. Context.EmitLdc_I4(Index);
  450. Context.EmitLdc_I4(Size);
  451. ASoftFallback.EmitCall(Context, Signed
  452. ? nameof(ASoftFallback.VectorExtractIntSx)
  453. : nameof(ASoftFallback.VectorExtractIntZx));
  454. }
  455. public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
  456. {
  457. ThrowIfInvalidF(Index, Size);
  458. Context.EmitLdvec(Reg);
  459. Context.EmitLdc_I4(Index);
  460. if (Size == 0)
  461. {
  462. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractSingle));
  463. }
  464. else if (Size == 1)
  465. {
  466. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorExtractDouble));
  467. }
  468. else
  469. {
  470. throw new ArgumentOutOfRangeException(nameof(Size));
  471. }
  472. }
  473. public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
  474. {
  475. EmitVectorZeroLower(Context, Rd);
  476. EmitVectorZeroUpper(Context, Rd);
  477. }
  478. public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  479. {
  480. EmitVectorInsert(Context, Rd, 0, 3, 0);
  481. }
  482. public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  483. {
  484. EmitVectorInsert(Context, Rd, 1, 3, 0);
  485. }
  486. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
  487. {
  488. ThrowIfInvalid(Index, Size);
  489. Context.EmitLdvec(Reg);
  490. Context.EmitLdc_I4(Index);
  491. Context.EmitLdc_I4(Size);
  492. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  493. Context.EmitStvec(Reg);
  494. }
  495. public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
  496. {
  497. ThrowIfInvalid(Index, Size);
  498. Context.EmitLdvectmp();
  499. Context.EmitLdc_I4(Index);
  500. Context.EmitLdc_I4(Size);
  501. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  502. Context.EmitStvectmp();
  503. }
  504. public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  505. {
  506. ThrowIfInvalid(Index, Size);
  507. Context.EmitLdc_I8(Value);
  508. Context.EmitLdvec(Reg);
  509. Context.EmitLdc_I4(Index);
  510. Context.EmitLdc_I4(Size);
  511. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertInt));
  512. Context.EmitStvec(Reg);
  513. }
  514. public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
  515. {
  516. ThrowIfInvalidF(Index, Size);
  517. Context.EmitLdvec(Reg);
  518. Context.EmitLdc_I4(Index);
  519. if (Size == 0)
  520. {
  521. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertSingle));
  522. }
  523. else if (Size == 1)
  524. {
  525. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertDouble));
  526. }
  527. else
  528. {
  529. throw new ArgumentOutOfRangeException(nameof(Size));
  530. }
  531. Context.EmitStvec(Reg);
  532. }
  533. public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
  534. {
  535. ThrowIfInvalidF(Index, Size);
  536. Context.EmitLdvectmp();
  537. Context.EmitLdc_I4(Index);
  538. if (Size == 0)
  539. {
  540. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertSingle));
  541. }
  542. else if (Size == 1)
  543. {
  544. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.VectorInsertDouble));
  545. }
  546. else
  547. {
  548. throw new ArgumentOutOfRangeException(nameof(Size));
  549. }
  550. Context.EmitStvectmp();
  551. }
  552. private static void ThrowIfInvalid(int Index, int Size)
  553. {
  554. if ((uint)Size > 3)
  555. {
  556. throw new ArgumentOutOfRangeException(nameof(Size));
  557. }
  558. if ((uint)Index >= 16 >> Size)
  559. {
  560. throw new ArgumentOutOfRangeException(nameof(Index));
  561. }
  562. }
  563. private static void ThrowIfInvalidF(int Index, int Size)
  564. {
  565. if ((uint)Size > 1)
  566. {
  567. throw new ArgumentOutOfRangeException(nameof(Size));
  568. }
  569. if ((uint)Index >= 4 >> Size)
  570. {
  571. throw new ArgumentOutOfRangeException(nameof(Index));
  572. }
  573. }
  574. }
  575. }