AInstEmitSimdArithmetic.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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 static ChocolArm64.Instruction.AInstEmitSimdHelper;
  8. namespace ChocolArm64.Instruction
  9. {
  10. static partial class AInstEmit
  11. {
  12. public static void Abs_S(AILEmitterCtx Context)
  13. {
  14. EmitScalarUnaryOpSx(Context, () => EmitAbs(Context));
  15. }
  16. public static void Abs_V(AILEmitterCtx Context)
  17. {
  18. EmitVectorUnaryOpSx(Context, () => EmitAbs(Context));
  19. }
  20. private static void EmitAbs(AILEmitterCtx Context)
  21. {
  22. AILLabel LblTrue = new AILLabel();
  23. Context.Emit(OpCodes.Dup);
  24. Context.Emit(OpCodes.Ldc_I4_0);
  25. Context.Emit(OpCodes.Bge_S, LblTrue);
  26. Context.Emit(OpCodes.Neg);
  27. Context.MarkLabel(LblTrue);
  28. }
  29. public static void Add_S(AILEmitterCtx Context)
  30. {
  31. EmitScalarBinaryOpZx(Context, () => Context.Emit(OpCodes.Add));
  32. }
  33. public static void Add_V(AILEmitterCtx Context)
  34. {
  35. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Add));
  36. }
  37. public static void Addhn_V(AILEmitterCtx Context)
  38. {
  39. EmitHighNarrow(Context, () => Context.Emit(OpCodes.Add), Round: false);
  40. }
  41. public static void Addp_S(AILEmitterCtx Context)
  42. {
  43. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  44. EmitVectorExtractZx(Context, Op.Rn, 0, Op.Size);
  45. EmitVectorExtractZx(Context, Op.Rn, 1, Op.Size);
  46. Context.Emit(OpCodes.Add);
  47. EmitScalarSet(Context, Op.Rd, Op.Size);
  48. }
  49. public static void Addp_V(AILEmitterCtx Context)
  50. {
  51. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  52. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  53. int Elems = Bytes >> Op.Size;
  54. int Half = Elems >> 1;
  55. for (int Index = 0; Index < Elems; Index++)
  56. {
  57. int Elem = (Index & (Half - 1)) << 1;
  58. EmitVectorExtractZx(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 0, Op.Size);
  59. EmitVectorExtractZx(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 1, Op.Size);
  60. Context.Emit(OpCodes.Add);
  61. EmitVectorInsertTmp(Context, Index, Op.Size);
  62. }
  63. Context.EmitLdvectmp();
  64. Context.EmitStvec(Op.Rd);
  65. if (Op.RegisterSize == ARegisterSize.SIMD64)
  66. {
  67. EmitVectorZeroUpper(Context, Op.Rd);
  68. }
  69. }
  70. public static void Addv_V(AILEmitterCtx Context)
  71. {
  72. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  73. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  74. EmitVectorExtractZx(Context, Op.Rn, 0, Op.Size);
  75. for (int Index = 1; Index < (Bytes >> Op.Size); Index++)
  76. {
  77. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
  78. Context.Emit(OpCodes.Add);
  79. }
  80. EmitScalarSet(Context, Op.Rd, Op.Size);
  81. }
  82. public static void Cls_V(AILEmitterCtx Context)
  83. {
  84. MethodInfo MthdInfo = typeof(ASoftFallback).GetMethod(nameof(ASoftFallback.CountLeadingSigns));
  85. EmitCountLeadingBits(Context, () => Context.EmitCall(MthdInfo));
  86. }
  87. public static void Clz_V(AILEmitterCtx Context)
  88. {
  89. MethodInfo MthdInfo = typeof(ASoftFallback).GetMethod(nameof(ASoftFallback.CountLeadingZeros));
  90. EmitCountLeadingBits(Context, () => Context.EmitCall(MthdInfo));
  91. }
  92. private static void EmitCountLeadingBits(AILEmitterCtx Context, Action Emit)
  93. {
  94. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  95. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  96. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  97. {
  98. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
  99. Context.EmitLdc_I4(8 << Op.Size);
  100. Emit();
  101. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  102. }
  103. if (Op.RegisterSize == ARegisterSize.SIMD64)
  104. {
  105. EmitVectorZeroUpper(Context, Op.Rd);
  106. }
  107. }
  108. public static void Cnt_V(AILEmitterCtx Context)
  109. {
  110. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  111. int Elems = Op.RegisterSize == ARegisterSize.SIMD128 ? 16 : 8;
  112. for (int Index = 0; Index < Elems; Index++)
  113. {
  114. EmitVectorExtractZx(Context, Op.Rn, Index, 0);
  115. Context.Emit(OpCodes.Conv_U1);
  116. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.CountSetBits8));
  117. Context.Emit(OpCodes.Conv_U8);
  118. EmitVectorInsert(Context, Op.Rd, Index, 0);
  119. }
  120. if (Op.RegisterSize == ARegisterSize.SIMD64)
  121. {
  122. EmitVectorZeroUpper(Context, Op.Rd);
  123. }
  124. }
  125. private static void EmitHighNarrow(AILEmitterCtx Context, Action Emit, bool Round)
  126. {
  127. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  128. int Elems = 8 >> Op.Size;
  129. int ESize = 8 << Op.Size;
  130. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  131. for (int Index = 0; Index < Elems; Index++)
  132. {
  133. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1);
  134. EmitVectorExtractZx(Context, Op.Rm, Index, Op.Size + 1);
  135. Emit();
  136. if (Round)
  137. {
  138. Context.EmitLdc_I8(1L << (ESize - 1));
  139. Context.Emit(OpCodes.Add);
  140. }
  141. Context.EmitLsr(ESize);
  142. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  143. }
  144. if (Part == 0)
  145. {
  146. EmitVectorZeroUpper(Context, Op.Rd);
  147. }
  148. }
  149. private static void EmitQxtn(AILEmitterCtx Context, bool Signed, bool Scalar)
  150. {
  151. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  152. int Elems = (!Scalar ? 8 >> Op.Size : 1);
  153. int ESize = 8 << Op.Size;
  154. int TMaxValue = (Signed ? (1 << (ESize - 1)) - 1 : (int)((1L << ESize) - 1L));
  155. int TMinValue = (Signed ? -((1 << (ESize - 1))) : 0);
  156. int Part = (!Scalar & (Op.RegisterSize == ARegisterSize.SIMD128) ? Elems : 0);
  157. Context.EmitLdc_I8(0L);
  158. Context.EmitSttmp();
  159. for (int Index = 0; Index < Elems; Index++)
  160. {
  161. AILLabel LblLe = new AILLabel();
  162. AILLabel LblGeEnd = new AILLabel();
  163. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  164. Context.Emit(OpCodes.Dup);
  165. Context.EmitLdc_I4(TMaxValue);
  166. Context.Emit(OpCodes.Conv_U8);
  167. Context.Emit(Signed ? OpCodes.Ble_S : OpCodes.Ble_Un_S, LblLe);
  168. Context.Emit(OpCodes.Pop);
  169. Context.EmitLdc_I4(TMaxValue);
  170. Context.EmitLdc_I8(0x8000000L);
  171. Context.EmitSttmp();
  172. Context.Emit(OpCodes.Br_S, LblGeEnd);
  173. Context.MarkLabel(LblLe);
  174. Context.Emit(OpCodes.Dup);
  175. Context.EmitLdc_I4(TMinValue);
  176. Context.Emit(OpCodes.Conv_I8);
  177. Context.Emit(Signed ? OpCodes.Bge_S : OpCodes.Bge_Un_S, LblGeEnd);
  178. Context.Emit(OpCodes.Pop);
  179. Context.EmitLdc_I4(TMinValue);
  180. Context.EmitLdc_I8(0x8000000L);
  181. Context.EmitSttmp();
  182. Context.MarkLabel(LblGeEnd);
  183. if (Scalar)
  184. {
  185. EmitVectorZeroLower(Context, Op.Rd);
  186. }
  187. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  188. }
  189. if (Part == 0)
  190. {
  191. EmitVectorZeroUpper(Context, Op.Rd);
  192. }
  193. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  194. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  195. Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Fpsr));
  196. Context.EmitLdtmp();
  197. Context.Emit(OpCodes.Conv_I4);
  198. Context.Emit(OpCodes.Or);
  199. Context.EmitCallPropSet(typeof(AThreadState), nameof(AThreadState.Fpsr));
  200. }
  201. public static void Fabd_S(AILEmitterCtx Context)
  202. {
  203. EmitScalarBinaryOpF(Context, () =>
  204. {
  205. Context.Emit(OpCodes.Sub);
  206. EmitUnaryMathCall(Context, nameof(Math.Abs));
  207. });
  208. }
  209. public static void Fabs_S(AILEmitterCtx Context)
  210. {
  211. EmitScalarUnaryOpF(Context, () =>
  212. {
  213. EmitUnaryMathCall(Context, nameof(Math.Abs));
  214. });
  215. }
  216. public static void Fadd_S(AILEmitterCtx Context)
  217. {
  218. EmitScalarBinaryOpF(Context, () => Context.Emit(OpCodes.Add));
  219. }
  220. public static void Fadd_V(AILEmitterCtx Context)
  221. {
  222. EmitVectorBinaryOpF(Context, () => Context.Emit(OpCodes.Add));
  223. }
  224. public static void Faddp_V(AILEmitterCtx Context)
  225. {
  226. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  227. int SizeF = Op.Size & 1;
  228. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  229. int Elems = Bytes >> SizeF + 2;
  230. int Half = Elems >> 1;
  231. for (int Index = 0; Index < Elems; Index++)
  232. {
  233. int Elem = (Index & (Half - 1)) << 1;
  234. EmitVectorExtractF(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 0, SizeF);
  235. EmitVectorExtractF(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 1, SizeF);
  236. Context.Emit(OpCodes.Add);
  237. EmitVectorInsertTmpF(Context, Index, SizeF);
  238. }
  239. Context.EmitLdvectmp();
  240. Context.EmitStvec(Op.Rd);
  241. if (Op.RegisterSize == ARegisterSize.SIMD64)
  242. {
  243. EmitVectorZeroUpper(Context, Op.Rd);
  244. }
  245. }
  246. public static void Fdiv_S(AILEmitterCtx Context)
  247. {
  248. EmitScalarBinaryOpF(Context, () => Context.Emit(OpCodes.Div));
  249. }
  250. public static void Fdiv_V(AILEmitterCtx Context)
  251. {
  252. EmitVectorBinaryOpF(Context, () => Context.Emit(OpCodes.Div));
  253. }
  254. public static void Fmadd_S(AILEmitterCtx Context)
  255. {
  256. EmitScalarTernaryRaOpF(Context, () =>
  257. {
  258. Context.Emit(OpCodes.Mul);
  259. Context.Emit(OpCodes.Add);
  260. });
  261. }
  262. public static void Fmax_S(AILEmitterCtx Context)
  263. {
  264. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  265. EmitScalarBinaryOpF(Context, () =>
  266. {
  267. if (Op.Size == 0)
  268. {
  269. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.MaxF));
  270. }
  271. else if (Op.Size == 1)
  272. {
  273. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Max));
  274. }
  275. else
  276. {
  277. throw new InvalidOperationException();
  278. }
  279. });
  280. }
  281. public static void Fmax_V(AILEmitterCtx Context)
  282. {
  283. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  284. EmitVectorBinaryOpF(Context, () =>
  285. {
  286. if (Op.Size == 0)
  287. {
  288. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.MaxF));
  289. }
  290. else if (Op.Size == 1)
  291. {
  292. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Max));
  293. }
  294. else
  295. {
  296. throw new InvalidOperationException();
  297. }
  298. });
  299. }
  300. public static void Fmin_S(AILEmitterCtx Context)
  301. {
  302. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  303. EmitScalarBinaryOpF(Context, () =>
  304. {
  305. if (Op.Size == 0)
  306. {
  307. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.MinF));
  308. }
  309. else if (Op.Size == 1)
  310. {
  311. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Min));
  312. }
  313. else
  314. {
  315. throw new InvalidOperationException();
  316. }
  317. });
  318. }
  319. public static void Fmin_V(AILEmitterCtx Context)
  320. {
  321. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  322. int SizeF = Op.Size & 1;
  323. EmitVectorBinaryOpF(Context, () =>
  324. {
  325. if (SizeF == 0)
  326. {
  327. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.MinF));
  328. }
  329. else if (SizeF == 1)
  330. {
  331. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Min));
  332. }
  333. else
  334. {
  335. throw new InvalidOperationException();
  336. }
  337. });
  338. }
  339. public static void Fmaxnm_S(AILEmitterCtx Context)
  340. {
  341. Fmax_S(Context);
  342. }
  343. public static void Fminnm_S(AILEmitterCtx Context)
  344. {
  345. Fmin_S(Context);
  346. }
  347. public static void Fmla_V(AILEmitterCtx Context)
  348. {
  349. EmitVectorTernaryOpF(Context, () =>
  350. {
  351. Context.Emit(OpCodes.Mul);
  352. Context.Emit(OpCodes.Add);
  353. });
  354. }
  355. public static void Fmla_Ve(AILEmitterCtx Context)
  356. {
  357. EmitVectorTernaryOpByElemF(Context, () =>
  358. {
  359. Context.Emit(OpCodes.Mul);
  360. Context.Emit(OpCodes.Add);
  361. });
  362. }
  363. public static void Fmls_V(AILEmitterCtx Context)
  364. {
  365. EmitVectorTernaryOpF(Context, () =>
  366. {
  367. Context.Emit(OpCodes.Mul);
  368. Context.Emit(OpCodes.Sub);
  369. });
  370. }
  371. public static void Fmls_Ve(AILEmitterCtx Context)
  372. {
  373. EmitVectorTernaryOpByElemF(Context, () =>
  374. {
  375. Context.Emit(OpCodes.Mul);
  376. Context.Emit(OpCodes.Sub);
  377. });
  378. }
  379. public static void Fmsub_S(AILEmitterCtx Context)
  380. {
  381. EmitScalarTernaryRaOpF(Context, () =>
  382. {
  383. Context.Emit(OpCodes.Mul);
  384. Context.Emit(OpCodes.Sub);
  385. });
  386. }
  387. public static void Fmul_S(AILEmitterCtx Context)
  388. {
  389. EmitScalarBinaryOpF(Context, () => Context.Emit(OpCodes.Mul));
  390. }
  391. public static void Fmul_Se(AILEmitterCtx Context)
  392. {
  393. EmitScalarBinaryOpByElemF(Context, () => Context.Emit(OpCodes.Mul));
  394. }
  395. public static void Fmul_V(AILEmitterCtx Context)
  396. {
  397. EmitVectorBinaryOpF(Context, () => Context.Emit(OpCodes.Mul));
  398. }
  399. public static void Fmul_Ve(AILEmitterCtx Context)
  400. {
  401. EmitVectorBinaryOpByElemF(Context, () => Context.Emit(OpCodes.Mul));
  402. }
  403. public static void Fneg_S(AILEmitterCtx Context)
  404. {
  405. EmitScalarUnaryOpF(Context, () => Context.Emit(OpCodes.Neg));
  406. }
  407. public static void Fneg_V(AILEmitterCtx Context)
  408. {
  409. EmitVectorUnaryOpF(Context, () => Context.Emit(OpCodes.Neg));
  410. }
  411. public static void Fnmadd_S(AILEmitterCtx Context)
  412. {
  413. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  414. int SizeF = Op.Size & 1;
  415. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  416. Context.Emit(OpCodes.Neg);
  417. EmitVectorExtractF(Context, Op.Rm, 0, SizeF);
  418. Context.Emit(OpCodes.Mul);
  419. EmitVectorExtractF(Context, Op.Ra, 0, SizeF);
  420. Context.Emit(OpCodes.Sub);
  421. EmitScalarSetF(Context, Op.Rd, SizeF);
  422. }
  423. public static void Fnmsub_S(AILEmitterCtx Context)
  424. {
  425. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  426. int SizeF = Op.Size & 1;
  427. EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
  428. EmitVectorExtractF(Context, Op.Rm, 0, SizeF);
  429. Context.Emit(OpCodes.Mul);
  430. EmitVectorExtractF(Context, Op.Ra, 0, SizeF);
  431. Context.Emit(OpCodes.Sub);
  432. EmitScalarSetF(Context, Op.Rd, SizeF);
  433. }
  434. public static void Fnmul_S(AILEmitterCtx Context)
  435. {
  436. EmitScalarBinaryOpF(Context, () =>
  437. {
  438. Context.Emit(OpCodes.Mul);
  439. Context.Emit(OpCodes.Neg);
  440. });
  441. }
  442. public static void Frecpe_S(AILEmitterCtx Context)
  443. {
  444. EmitFrecpe(Context, 0, Scalar: true);
  445. }
  446. public static void Frecpe_V(AILEmitterCtx Context)
  447. {
  448. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  449. int SizeF = Op.Size & 1;
  450. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  451. for (int Index = 0; Index < Bytes >> SizeF + 2; Index++)
  452. {
  453. EmitFrecpe(Context, Index, Scalar: false);
  454. }
  455. if (Op.RegisterSize == ARegisterSize.SIMD64)
  456. {
  457. EmitVectorZeroUpper(Context, Op.Rd);
  458. }
  459. }
  460. private static void EmitFrecpe(AILEmitterCtx Context, int Index, bool Scalar)
  461. {
  462. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  463. int SizeF = Op.Size & 1;
  464. if (SizeF == 0)
  465. {
  466. Context.EmitLdc_R4(1);
  467. }
  468. else /* if (SizeF == 1) */
  469. {
  470. Context.EmitLdc_R8(1);
  471. }
  472. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  473. Context.Emit(OpCodes.Div);
  474. if (Scalar)
  475. {
  476. EmitVectorZeroAll(Context, Op.Rd);
  477. }
  478. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  479. }
  480. public static void Frecps_S(AILEmitterCtx Context)
  481. {
  482. EmitFrecps(Context, 0, Scalar: true);
  483. }
  484. public static void Frecps_V(AILEmitterCtx Context)
  485. {
  486. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  487. int SizeF = Op.Size & 1;
  488. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  489. for (int Index = 0; Index < Bytes >> SizeF + 2; Index++)
  490. {
  491. EmitFrecps(Context, Index, Scalar: false);
  492. }
  493. if (Op.RegisterSize == ARegisterSize.SIMD64)
  494. {
  495. EmitVectorZeroUpper(Context, Op.Rd);
  496. }
  497. }
  498. private static void EmitFrecps(AILEmitterCtx Context, int Index, bool Scalar)
  499. {
  500. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  501. int SizeF = Op.Size & 1;
  502. if (SizeF == 0)
  503. {
  504. Context.EmitLdc_R4(2);
  505. }
  506. else /* if (SizeF == 1) */
  507. {
  508. Context.EmitLdc_R8(2);
  509. }
  510. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  511. EmitVectorExtractF(Context, Op.Rm, Index, SizeF);
  512. Context.Emit(OpCodes.Mul);
  513. Context.Emit(OpCodes.Sub);
  514. if (Scalar)
  515. {
  516. EmitVectorZeroAll(Context, Op.Rd);
  517. }
  518. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  519. }
  520. public static void Frinta_S(AILEmitterCtx Context)
  521. {
  522. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  523. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  524. EmitRoundMathCall(Context, MidpointRounding.AwayFromZero);
  525. EmitScalarSetF(Context, Op.Rd, Op.Size);
  526. }
  527. public static void Frinta_V(AILEmitterCtx Context)
  528. {
  529. EmitVectorUnaryOpF(Context, () =>
  530. {
  531. EmitRoundMathCall(Context, MidpointRounding.AwayFromZero);
  532. });
  533. }
  534. public static void Frinti_S(AILEmitterCtx Context)
  535. {
  536. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  537. EmitScalarUnaryOpF(Context, () =>
  538. {
  539. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  540. Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Fpcr));
  541. if (Op.Size == 0)
  542. {
  543. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.RoundF));
  544. }
  545. else if (Op.Size == 1)
  546. {
  547. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Round));
  548. }
  549. else
  550. {
  551. throw new InvalidOperationException();
  552. }
  553. });
  554. }
  555. public static void Frinti_V(AILEmitterCtx Context)
  556. {
  557. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  558. int SizeF = Op.Size & 1;
  559. EmitVectorUnaryOpF(Context, () =>
  560. {
  561. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  562. Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Fpcr));
  563. if (SizeF == 0)
  564. {
  565. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.RoundF));
  566. }
  567. else if (SizeF == 1)
  568. {
  569. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Round));
  570. }
  571. else
  572. {
  573. throw new InvalidOperationException();
  574. }
  575. });
  576. }
  577. public static void Frintm_S(AILEmitterCtx Context)
  578. {
  579. EmitScalarUnaryOpF(Context, () =>
  580. {
  581. EmitUnaryMathCall(Context, nameof(Math.Floor));
  582. });
  583. }
  584. public static void Frintm_V(AILEmitterCtx Context)
  585. {
  586. EmitVectorUnaryOpF(Context, () =>
  587. {
  588. EmitUnaryMathCall(Context, nameof(Math.Floor));
  589. });
  590. }
  591. public static void Frintn_S(AILEmitterCtx Context)
  592. {
  593. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  594. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  595. EmitRoundMathCall(Context, MidpointRounding.ToEven);
  596. EmitScalarSetF(Context, Op.Rd, Op.Size);
  597. }
  598. public static void Frintn_V(AILEmitterCtx Context)
  599. {
  600. EmitVectorUnaryOpF(Context, () =>
  601. {
  602. EmitRoundMathCall(Context, MidpointRounding.ToEven);
  603. });
  604. }
  605. public static void Frintp_S(AILEmitterCtx Context)
  606. {
  607. EmitScalarUnaryOpF(Context, () =>
  608. {
  609. EmitUnaryMathCall(Context, nameof(Math.Ceiling));
  610. });
  611. }
  612. public static void Frintp_V(AILEmitterCtx Context)
  613. {
  614. EmitVectorUnaryOpF(Context, () =>
  615. {
  616. EmitUnaryMathCall(Context, nameof(Math.Ceiling));
  617. });
  618. }
  619. public static void Frintx_S(AILEmitterCtx Context)
  620. {
  621. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  622. EmitScalarUnaryOpF(Context, () =>
  623. {
  624. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  625. Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Fpcr));
  626. if (Op.Size == 0)
  627. {
  628. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.RoundF));
  629. }
  630. else if (Op.Size == 1)
  631. {
  632. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Round));
  633. }
  634. else
  635. {
  636. throw new InvalidOperationException();
  637. }
  638. });
  639. }
  640. public static void Frintx_V(AILEmitterCtx Context)
  641. {
  642. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  643. EmitVectorUnaryOpF(Context, () =>
  644. {
  645. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  646. Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Fpcr));
  647. if (Op.Size == 0)
  648. {
  649. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.RoundF));
  650. }
  651. else if (Op.Size == 1)
  652. {
  653. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Round));
  654. }
  655. else
  656. {
  657. throw new InvalidOperationException();
  658. }
  659. });
  660. }
  661. public static void Frsqrte_S(AILEmitterCtx Context)
  662. {
  663. EmitScalarUnaryOpF(Context, () =>
  664. {
  665. EmitUnarySoftFloatCall(Context, nameof(ASoftFloat.InvSqrtEstimate));
  666. });
  667. }
  668. public static void Frsqrte_V(AILEmitterCtx Context)
  669. {
  670. EmitVectorUnaryOpF(Context, () =>
  671. {
  672. EmitUnarySoftFloatCall(Context, nameof(ASoftFloat.InvSqrtEstimate));
  673. });
  674. }
  675. public static void Frsqrts_S(AILEmitterCtx Context)
  676. {
  677. EmitFrsqrts(Context, 0, Scalar: true);
  678. }
  679. public static void Frsqrts_V(AILEmitterCtx Context)
  680. {
  681. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  682. int SizeF = Op.Size & 1;
  683. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  684. for (int Index = 0; Index < Bytes >> SizeF + 2; Index++)
  685. {
  686. EmitFrsqrts(Context, Index, Scalar: false);
  687. }
  688. if (Op.RegisterSize == ARegisterSize.SIMD64)
  689. {
  690. EmitVectorZeroUpper(Context, Op.Rd);
  691. }
  692. }
  693. private static void EmitFrsqrts(AILEmitterCtx Context, int Index, bool Scalar)
  694. {
  695. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  696. int SizeF = Op.Size & 1;
  697. if (SizeF == 0)
  698. {
  699. Context.EmitLdc_R4(3);
  700. }
  701. else /* if (SizeF == 1) */
  702. {
  703. Context.EmitLdc_R8(3);
  704. }
  705. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  706. EmitVectorExtractF(Context, Op.Rm, Index, SizeF);
  707. Context.Emit(OpCodes.Mul);
  708. Context.Emit(OpCodes.Sub);
  709. if (SizeF == 0)
  710. {
  711. Context.EmitLdc_R4(0.5f);
  712. }
  713. else /* if (SizeF == 1) */
  714. {
  715. Context.EmitLdc_R8(0.5);
  716. }
  717. Context.Emit(OpCodes.Mul);
  718. if (Scalar)
  719. {
  720. EmitVectorZeroAll(Context, Op.Rd);
  721. }
  722. EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
  723. }
  724. public static void Fsqrt_S(AILEmitterCtx Context)
  725. {
  726. EmitScalarUnaryOpF(Context, () =>
  727. {
  728. EmitUnaryMathCall(Context, nameof(Math.Sqrt));
  729. });
  730. }
  731. public static void Fsub_S(AILEmitterCtx Context)
  732. {
  733. EmitScalarBinaryOpF(Context, () => Context.Emit(OpCodes.Sub));
  734. }
  735. public static void Fsub_V(AILEmitterCtx Context)
  736. {
  737. EmitVectorBinaryOpF(Context, () => Context.Emit(OpCodes.Sub));
  738. }
  739. public static void Mla_V(AILEmitterCtx Context)
  740. {
  741. EmitVectorTernaryOpZx(Context, () =>
  742. {
  743. Context.Emit(OpCodes.Mul);
  744. Context.Emit(OpCodes.Add);
  745. });
  746. }
  747. public static void Mla_Ve(AILEmitterCtx Context)
  748. {
  749. EmitVectorTernaryOpByElemZx(Context, () =>
  750. {
  751. Context.Emit(OpCodes.Mul);
  752. Context.Emit(OpCodes.Add);
  753. });
  754. }
  755. public static void Mls_V(AILEmitterCtx Context)
  756. {
  757. EmitVectorTernaryOpZx(Context, () =>
  758. {
  759. Context.Emit(OpCodes.Mul);
  760. Context.Emit(OpCodes.Sub);
  761. });
  762. }
  763. public static void Mul_V(AILEmitterCtx Context)
  764. {
  765. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Mul));
  766. }
  767. public static void Mul_Ve(AILEmitterCtx Context)
  768. {
  769. EmitVectorBinaryOpByElemZx(Context, () => Context.Emit(OpCodes.Mul));
  770. }
  771. public static void Neg_S(AILEmitterCtx Context)
  772. {
  773. EmitScalarUnaryOpSx(Context, () => Context.Emit(OpCodes.Neg));
  774. }
  775. public static void Neg_V(AILEmitterCtx Context)
  776. {
  777. EmitVectorUnaryOpSx(Context, () => Context.Emit(OpCodes.Neg));
  778. }
  779. public static void Raddhn_V(AILEmitterCtx Context)
  780. {
  781. EmitHighNarrow(Context, () => Context.Emit(OpCodes.Add), Round: true);
  782. }
  783. public static void Rsubhn_V(AILEmitterCtx Context)
  784. {
  785. EmitHighNarrow(Context, () => Context.Emit(OpCodes.Sub), Round: true);
  786. }
  787. public static void Saddw_V(AILEmitterCtx Context)
  788. {
  789. EmitVectorWidenRmBinaryOpSx(Context, () => Context.Emit(OpCodes.Add));
  790. }
  791. public static void Smax_V(AILEmitterCtx Context)
  792. {
  793. Type[] Types = new Type[] { typeof(long), typeof(long) };
  794. MethodInfo MthdInfo = typeof(Math).GetMethod(nameof(Math.Max), Types);
  795. EmitVectorBinaryOpSx(Context, () => Context.EmitCall(MthdInfo));
  796. }
  797. public static void Smin_V(AILEmitterCtx Context)
  798. {
  799. Type[] Types = new Type[] { typeof(long), typeof(long) };
  800. MethodInfo MthdInfo = typeof(Math).GetMethod(nameof(Math.Min), Types);
  801. EmitVectorBinaryOpSx(Context, () => Context.EmitCall(MthdInfo));
  802. }
  803. public static void Smlal_V(AILEmitterCtx Context)
  804. {
  805. EmitVectorWidenRnRmTernaryOpSx(Context, () =>
  806. {
  807. Context.Emit(OpCodes.Mul);
  808. Context.Emit(OpCodes.Add);
  809. });
  810. }
  811. public static void Smull_V(AILEmitterCtx Context)
  812. {
  813. EmitVectorWidenRnRmBinaryOpSx(Context, () => Context.Emit(OpCodes.Mul));
  814. }
  815. public static void Sqxtn_S(AILEmitterCtx Context)
  816. {
  817. EmitQxtn(Context, Signed: true, Scalar: true);
  818. }
  819. public static void Sqxtn_V(AILEmitterCtx Context)
  820. {
  821. EmitQxtn(Context, Signed: true, Scalar: false);
  822. }
  823. public static void Sub_S(AILEmitterCtx Context)
  824. {
  825. EmitScalarBinaryOpZx(Context, () => Context.Emit(OpCodes.Sub));
  826. }
  827. public static void Sub_V(AILEmitterCtx Context)
  828. {
  829. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Sub));
  830. }
  831. public static void Subhn_V(AILEmitterCtx Context)
  832. {
  833. EmitHighNarrow(Context, () => Context.Emit(OpCodes.Sub), Round: false);
  834. }
  835. public static void Uabd_V(AILEmitterCtx Context)
  836. {
  837. EmitVectorBinaryOpZx(Context, () => EmitAbd(Context));
  838. }
  839. public static void Uabdl_V(AILEmitterCtx Context)
  840. {
  841. EmitVectorWidenRnRmBinaryOpZx(Context, () => EmitAbd(Context));
  842. }
  843. private static void EmitAbd(AILEmitterCtx Context)
  844. {
  845. Context.Emit(OpCodes.Sub);
  846. Type[] Types = new Type[] { typeof(long) };
  847. Context.EmitCall(typeof(Math).GetMethod(nameof(Math.Abs), Types));
  848. }
  849. public static void Uaddl_V(AILEmitterCtx Context)
  850. {
  851. EmitVectorWidenRnRmBinaryOpZx(Context, () => Context.Emit(OpCodes.Add));
  852. }
  853. public static void Uaddlv_V(AILEmitterCtx Context)
  854. {
  855. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  856. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  857. EmitVectorExtractZx(Context, Op.Rn, 0, Op.Size);
  858. for (int Index = 1; Index < (Bytes >> Op.Size); Index++)
  859. {
  860. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
  861. Context.Emit(OpCodes.Add);
  862. }
  863. EmitScalarSet(Context, Op.Rd, Op.Size + 1);
  864. }
  865. public static void Uaddw_V(AILEmitterCtx Context)
  866. {
  867. EmitVectorWidenRmBinaryOpZx(Context, () => Context.Emit(OpCodes.Add));
  868. }
  869. public static void Uhadd_V(AILEmitterCtx Context)
  870. {
  871. EmitVectorBinaryOpZx(Context, () =>
  872. {
  873. Context.Emit(OpCodes.Add);
  874. Context.EmitLdc_I4(1);
  875. Context.Emit(OpCodes.Shr_Un);
  876. });
  877. }
  878. public static void Umull_V(AILEmitterCtx Context)
  879. {
  880. EmitVectorWidenRnRmBinaryOpZx(Context, () => Context.Emit(OpCodes.Mul));
  881. }
  882. public static void Uqxtn_S(AILEmitterCtx Context)
  883. {
  884. EmitQxtn(Context, Signed: false, Scalar: true);
  885. }
  886. public static void Uqxtn_V(AILEmitterCtx Context)
  887. {
  888. EmitQxtn(Context, Signed: false, Scalar: false);
  889. }
  890. }
  891. }