InstEmitSimdCvt.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using System.Runtime.Intrinsics;
  7. using System.Runtime.Intrinsics.X86;
  8. using static ChocolArm64.Instructions.InstEmitSimdHelper;
  9. namespace ChocolArm64.Instructions
  10. {
  11. static partial class InstEmit
  12. {
  13. public static void Fcvt_S(ILEmitterCtx context)
  14. {
  15. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  16. if (Optimizations.UseSse2)
  17. {
  18. if (op.Size == 1 && op.Opc == 0)
  19. {
  20. //Double -> Single.
  21. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  22. EmitLdvecWithCastToDouble(context, op.Rn);
  23. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<double>) };
  24. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertScalarToVector128Single), types));
  25. context.EmitStvec(op.Rd);
  26. }
  27. else if (op.Size == 0 && op.Opc == 1)
  28. {
  29. //Single -> Double.
  30. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorDoubleZero));
  31. context.EmitLdvec(op.Rn);
  32. Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<float>) };
  33. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertScalarToVector128Double), types));
  34. EmitStvecWithCastFromDouble(context, op.Rd);
  35. }
  36. else
  37. {
  38. //Invalid encoding.
  39. throw new InvalidOperationException();
  40. }
  41. }
  42. else
  43. {
  44. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  45. EmitFloatCast(context, op.Opc);
  46. EmitScalarSetF(context, op.Rd, op.Opc);
  47. }
  48. }
  49. public static void Fcvtas_Gp(ILEmitterCtx context)
  50. {
  51. EmitFcvt_s_Gp(context, () => EmitRoundMathCall(context, MidpointRounding.AwayFromZero));
  52. }
  53. public static void Fcvtau_Gp(ILEmitterCtx context)
  54. {
  55. EmitFcvt_u_Gp(context, () => EmitRoundMathCall(context, MidpointRounding.AwayFromZero));
  56. }
  57. public static void Fcvtl_V(ILEmitterCtx context)
  58. {
  59. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  60. int sizeF = op.Size & 1;
  61. int elems = 4 >> sizeF;
  62. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  63. for (int index = 0; index < elems; index++)
  64. {
  65. if (sizeF == 0)
  66. {
  67. EmitVectorExtractZx(context, op.Rn, part + index, 1);
  68. context.Emit(OpCodes.Conv_U2);
  69. context.EmitLdarg(TranslatedSub.StateArgIdx);
  70. context.EmitCall(typeof(SoftFloat16_32), nameof(SoftFloat16_32.FPConvert));
  71. }
  72. else /* if (sizeF == 1) */
  73. {
  74. EmitVectorExtractF(context, op.Rn, part + index, 0);
  75. context.Emit(OpCodes.Conv_R8);
  76. }
  77. EmitVectorInsertTmpF(context, index, sizeF);
  78. }
  79. context.EmitLdvectmp();
  80. context.EmitStvec(op.Rd);
  81. }
  82. public static void Fcvtms_Gp(ILEmitterCtx context)
  83. {
  84. EmitFcvt_s_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Floor)));
  85. }
  86. public static void Fcvtmu_Gp(ILEmitterCtx context)
  87. {
  88. EmitFcvt_u_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Floor)));
  89. }
  90. public static void Fcvtn_V(ILEmitterCtx context)
  91. {
  92. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  93. int sizeF = op.Size & 1;
  94. int elems = 4 >> sizeF;
  95. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  96. if (part != 0)
  97. {
  98. context.EmitLdvec(op.Rd);
  99. context.EmitStvectmp();
  100. }
  101. for (int index = 0; index < elems; index++)
  102. {
  103. EmitVectorExtractF(context, op.Rn, index, sizeF);
  104. if (sizeF == 0)
  105. {
  106. context.EmitLdarg(TranslatedSub.StateArgIdx);
  107. context.EmitCall(typeof(SoftFloat32_16), nameof(SoftFloat32_16.FPConvert));
  108. context.Emit(OpCodes.Conv_U8);
  109. EmitVectorInsertTmp(context, part + index, 1);
  110. }
  111. else /* if (sizeF == 1) */
  112. {
  113. context.Emit(OpCodes.Conv_R4);
  114. EmitVectorInsertTmpF(context, part + index, 0);
  115. }
  116. }
  117. context.EmitLdvectmp();
  118. context.EmitStvec(op.Rd);
  119. if (part == 0)
  120. {
  121. EmitVectorZeroUpper(context, op.Rd);
  122. }
  123. }
  124. public static void Fcvtns_S(ILEmitterCtx context)
  125. {
  126. EmitFcvtn(context, signed: true, scalar: true);
  127. }
  128. public static void Fcvtns_V(ILEmitterCtx context)
  129. {
  130. EmitFcvtn(context, signed: true, scalar: false);
  131. }
  132. public static void Fcvtnu_S(ILEmitterCtx context)
  133. {
  134. EmitFcvtn(context, signed: false, scalar: true);
  135. }
  136. public static void Fcvtnu_V(ILEmitterCtx context)
  137. {
  138. EmitFcvtn(context, signed: false, scalar: false);
  139. }
  140. public static void Fcvtps_Gp(ILEmitterCtx context)
  141. {
  142. EmitFcvt_s_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Ceiling)));
  143. }
  144. public static void Fcvtpu_Gp(ILEmitterCtx context)
  145. {
  146. EmitFcvt_u_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Ceiling)));
  147. }
  148. public static void Fcvtzs_Gp(ILEmitterCtx context)
  149. {
  150. EmitFcvt_s_Gp(context, () => { });
  151. }
  152. public static void Fcvtzs_Gp_Fix(ILEmitterCtx context)
  153. {
  154. EmitFcvtzs_Gp_Fix(context);
  155. }
  156. public static void Fcvtzs_S(ILEmitterCtx context)
  157. {
  158. EmitScalarFcvtzs(context);
  159. }
  160. public static void Fcvtzs_V(ILEmitterCtx context)
  161. {
  162. EmitVectorFcvtzs(context);
  163. }
  164. public static void Fcvtzu_Gp(ILEmitterCtx context)
  165. {
  166. EmitFcvt_u_Gp(context, () => { });
  167. }
  168. public static void Fcvtzu_Gp_Fix(ILEmitterCtx context)
  169. {
  170. EmitFcvtzu_Gp_Fix(context);
  171. }
  172. public static void Fcvtzu_S(ILEmitterCtx context)
  173. {
  174. EmitScalarFcvtzu(context);
  175. }
  176. public static void Fcvtzu_V(ILEmitterCtx context)
  177. {
  178. EmitVectorFcvtzu(context);
  179. }
  180. public static void Scvtf_Gp(ILEmitterCtx context)
  181. {
  182. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  183. context.EmitLdintzr(op.Rn);
  184. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  185. {
  186. context.Emit(OpCodes.Conv_U4);
  187. }
  188. EmitFloatCast(context, op.Size);
  189. EmitScalarSetF(context, op.Rd, op.Size);
  190. }
  191. public static void Scvtf_S(ILEmitterCtx context)
  192. {
  193. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  194. EmitVectorExtractSx(context, op.Rn, 0, op.Size + 2);
  195. EmitFloatCast(context, op.Size);
  196. EmitScalarSetF(context, op.Rd, op.Size);
  197. }
  198. public static void Scvtf_V(ILEmitterCtx context)
  199. {
  200. EmitVectorCvtf(context, signed: true);
  201. }
  202. public static void Ucvtf_Gp(ILEmitterCtx context)
  203. {
  204. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  205. context.EmitLdintzr(op.Rn);
  206. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  207. {
  208. context.Emit(OpCodes.Conv_U4);
  209. }
  210. context.Emit(OpCodes.Conv_R_Un);
  211. EmitFloatCast(context, op.Size);
  212. EmitScalarSetF(context, op.Rd, op.Size);
  213. }
  214. public static void Ucvtf_S(ILEmitterCtx context)
  215. {
  216. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  217. EmitVectorExtractZx(context, op.Rn, 0, op.Size + 2);
  218. context.Emit(OpCodes.Conv_R_Un);
  219. EmitFloatCast(context, op.Size);
  220. EmitScalarSetF(context, op.Rd, op.Size);
  221. }
  222. public static void Ucvtf_V(ILEmitterCtx context)
  223. {
  224. EmitVectorCvtf(context, signed: false);
  225. }
  226. private static int GetFBits(ILEmitterCtx context)
  227. {
  228. if (context.CurrOp is OpCodeSimdShImm64 op)
  229. {
  230. return GetImmShr(op);
  231. }
  232. return 0;
  233. }
  234. private static void EmitFloatCast(ILEmitterCtx context, int size)
  235. {
  236. if (size == 0)
  237. {
  238. context.Emit(OpCodes.Conv_R4);
  239. }
  240. else if (size == 1)
  241. {
  242. context.Emit(OpCodes.Conv_R8);
  243. }
  244. else
  245. {
  246. throw new ArgumentOutOfRangeException(nameof(size));
  247. }
  248. }
  249. private static void EmitFcvtn(ILEmitterCtx context, bool signed, bool scalar)
  250. {
  251. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  252. int sizeF = op.Size & 1;
  253. int sizeI = sizeF + 2;
  254. int bytes = op.GetBitsCount() >> 3;
  255. int elems = !scalar ? bytes >> sizeI : 1;
  256. if (scalar && (sizeF == 0))
  257. {
  258. EmitVectorZeroLowerTmp(context);
  259. }
  260. for (int index = 0; index < elems; index++)
  261. {
  262. EmitVectorExtractF(context, op.Rn, index, sizeF);
  263. EmitRoundMathCall(context, MidpointRounding.ToEven);
  264. if (sizeF == 0)
  265. {
  266. VectorHelper.EmitCall(context, signed
  267. ? nameof(VectorHelper.SatF32ToS32)
  268. : nameof(VectorHelper.SatF32ToU32));
  269. context.Emit(OpCodes.Conv_U8);
  270. }
  271. else /* if (sizeF == 1) */
  272. {
  273. VectorHelper.EmitCall(context, signed
  274. ? nameof(VectorHelper.SatF64ToS64)
  275. : nameof(VectorHelper.SatF64ToU64));
  276. }
  277. EmitVectorInsertTmp(context, index, sizeI);
  278. }
  279. context.EmitLdvectmp();
  280. context.EmitStvec(op.Rd);
  281. if ((op.RegisterSize == RegisterSize.Simd64) || scalar)
  282. {
  283. EmitVectorZeroUpper(context, op.Rd);
  284. }
  285. }
  286. private static void EmitFcvt_s_Gp(ILEmitterCtx context, Action emit)
  287. {
  288. EmitFcvt___Gp(context, emit, true);
  289. }
  290. private static void EmitFcvt_u_Gp(ILEmitterCtx context, Action emit)
  291. {
  292. EmitFcvt___Gp(context, emit, false);
  293. }
  294. private static void EmitFcvt___Gp(ILEmitterCtx context, Action emit, bool signed)
  295. {
  296. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  297. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  298. emit();
  299. if (signed)
  300. {
  301. EmitScalarFcvts(context, op.Size, 0);
  302. }
  303. else
  304. {
  305. EmitScalarFcvtu(context, op.Size, 0);
  306. }
  307. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  308. {
  309. context.Emit(OpCodes.Conv_U8);
  310. }
  311. context.EmitStintzr(op.Rd);
  312. }
  313. private static void EmitFcvtzs_Gp_Fix(ILEmitterCtx context)
  314. {
  315. EmitFcvtz__Gp_Fix(context, true);
  316. }
  317. private static void EmitFcvtzu_Gp_Fix(ILEmitterCtx context)
  318. {
  319. EmitFcvtz__Gp_Fix(context, false);
  320. }
  321. private static void EmitFcvtz__Gp_Fix(ILEmitterCtx context, bool signed)
  322. {
  323. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  324. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  325. if (signed)
  326. {
  327. EmitScalarFcvts(context, op.Size, op.FBits);
  328. }
  329. else
  330. {
  331. EmitScalarFcvtu(context, op.Size, op.FBits);
  332. }
  333. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  334. {
  335. context.Emit(OpCodes.Conv_U8);
  336. }
  337. context.EmitStintzr(op.Rd);
  338. }
  339. private static void EmitVectorScvtf(ILEmitterCtx context)
  340. {
  341. EmitVectorCvtf(context, true);
  342. }
  343. private static void EmitVectorUcvtf(ILEmitterCtx context)
  344. {
  345. EmitVectorCvtf(context, false);
  346. }
  347. private static void EmitVectorCvtf(ILEmitterCtx context, bool signed)
  348. {
  349. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  350. int sizeF = op.Size & 1;
  351. int sizeI = sizeF + 2;
  352. int fBits = GetFBits(context);
  353. int bytes = op.GetBitsCount() >> 3;
  354. int elems = bytes >> sizeI;
  355. for (int index = 0; index < elems; index++)
  356. {
  357. EmitVectorExtract(context, op.Rn, index, sizeI, signed);
  358. if (!signed)
  359. {
  360. context.Emit(OpCodes.Conv_R_Un);
  361. }
  362. context.Emit(sizeF == 0
  363. ? OpCodes.Conv_R4
  364. : OpCodes.Conv_R8);
  365. EmitI2fFBitsMul(context, sizeF, fBits);
  366. EmitVectorInsertF(context, op.Rd, index, sizeF);
  367. }
  368. if (op.RegisterSize == RegisterSize.Simd64)
  369. {
  370. EmitVectorZeroUpper(context, op.Rd);
  371. }
  372. }
  373. private static void EmitScalarFcvtzs(ILEmitterCtx context)
  374. {
  375. EmitScalarFcvtz(context, true);
  376. }
  377. private static void EmitScalarFcvtzu(ILEmitterCtx context)
  378. {
  379. EmitScalarFcvtz(context, false);
  380. }
  381. private static void EmitScalarFcvtz(ILEmitterCtx context, bool signed)
  382. {
  383. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  384. int sizeF = op.Size & 1;
  385. int sizeI = sizeF + 2;
  386. int fBits = GetFBits(context);
  387. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  388. EmitF2iFBitsMul(context, sizeF, fBits);
  389. if (sizeF == 0)
  390. {
  391. VectorHelper.EmitCall(context, signed
  392. ? nameof(VectorHelper.SatF32ToS32)
  393. : nameof(VectorHelper.SatF32ToU32));
  394. }
  395. else /* if (sizeF == 1) */
  396. {
  397. VectorHelper.EmitCall(context, signed
  398. ? nameof(VectorHelper.SatF64ToS64)
  399. : nameof(VectorHelper.SatF64ToU64));
  400. }
  401. if (sizeF == 0)
  402. {
  403. context.Emit(OpCodes.Conv_U8);
  404. }
  405. EmitScalarSet(context, op.Rd, sizeI);
  406. }
  407. private static void EmitVectorFcvtzs(ILEmitterCtx context)
  408. {
  409. EmitVectorFcvtz(context, true);
  410. }
  411. private static void EmitVectorFcvtzu(ILEmitterCtx context)
  412. {
  413. EmitVectorFcvtz(context, false);
  414. }
  415. private static void EmitVectorFcvtz(ILEmitterCtx context, bool signed)
  416. {
  417. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  418. int sizeF = op.Size & 1;
  419. int sizeI = sizeF + 2;
  420. int fBits = GetFBits(context);
  421. int bytes = op.GetBitsCount() >> 3;
  422. int elems = bytes >> sizeI;
  423. for (int index = 0; index < elems; index++)
  424. {
  425. EmitVectorExtractF(context, op.Rn, index, sizeF);
  426. EmitF2iFBitsMul(context, sizeF, fBits);
  427. if (sizeF == 0)
  428. {
  429. VectorHelper.EmitCall(context, signed
  430. ? nameof(VectorHelper.SatF32ToS32)
  431. : nameof(VectorHelper.SatF32ToU32));
  432. }
  433. else /* if (sizeF == 1) */
  434. {
  435. VectorHelper.EmitCall(context, signed
  436. ? nameof(VectorHelper.SatF64ToS64)
  437. : nameof(VectorHelper.SatF64ToU64));
  438. }
  439. if (sizeF == 0)
  440. {
  441. context.Emit(OpCodes.Conv_U8);
  442. }
  443. EmitVectorInsert(context, op.Rd, index, sizeI);
  444. }
  445. if (op.RegisterSize == RegisterSize.Simd64)
  446. {
  447. EmitVectorZeroUpper(context, op.Rd);
  448. }
  449. }
  450. private static void EmitScalarFcvts(ILEmitterCtx context, int size, int fBits)
  451. {
  452. if (size < 0 || size > 1)
  453. {
  454. throw new ArgumentOutOfRangeException(nameof(size));
  455. }
  456. EmitF2iFBitsMul(context, size, fBits);
  457. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  458. {
  459. if (size == 0)
  460. {
  461. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS32));
  462. }
  463. else /* if (size == 1) */
  464. {
  465. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS32));
  466. }
  467. }
  468. else
  469. {
  470. if (size == 0)
  471. {
  472. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS64));
  473. }
  474. else /* if (size == 1) */
  475. {
  476. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS64));
  477. }
  478. }
  479. }
  480. private static void EmitScalarFcvtu(ILEmitterCtx context, int size, int fBits)
  481. {
  482. if (size < 0 || size > 1)
  483. {
  484. throw new ArgumentOutOfRangeException(nameof(size));
  485. }
  486. EmitF2iFBitsMul(context, size, fBits);
  487. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  488. {
  489. if (size == 0)
  490. {
  491. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU32));
  492. }
  493. else /* if (size == 1) */
  494. {
  495. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU32));
  496. }
  497. }
  498. else
  499. {
  500. if (size == 0)
  501. {
  502. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU64));
  503. }
  504. else /* if (size == 1) */
  505. {
  506. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU64));
  507. }
  508. }
  509. }
  510. private static void EmitF2iFBitsMul(ILEmitterCtx context, int size, int fBits)
  511. {
  512. if (fBits != 0)
  513. {
  514. if (size == 0)
  515. {
  516. context.EmitLdc_R4(MathF.Pow(2f, fBits));
  517. }
  518. else if (size == 1)
  519. {
  520. context.EmitLdc_R8(Math.Pow(2d, fBits));
  521. }
  522. else
  523. {
  524. throw new ArgumentOutOfRangeException(nameof(size));
  525. }
  526. context.Emit(OpCodes.Mul);
  527. }
  528. }
  529. private static void EmitI2fFBitsMul(ILEmitterCtx context, int size, int fBits)
  530. {
  531. if (fBits != 0)
  532. {
  533. if (size == 0)
  534. {
  535. context.EmitLdc_R4(1f / MathF.Pow(2f, fBits));
  536. }
  537. else if (size == 1)
  538. {
  539. context.EmitLdc_R8(1d / Math.Pow(2d, fBits));
  540. }
  541. else
  542. {
  543. throw new ArgumentOutOfRangeException(nameof(size));
  544. }
  545. context.Emit(OpCodes.Mul);
  546. }
  547. }
  548. }
  549. }