AInstEmitSimdHelper.cs 33 KB

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