InstEmitSimdCvt.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. context.EmitLdvec(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. context.EmitStvec(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. if (Optimizations.UseSse2 && sizeF == 1)
  62. {
  63. Type[] typesCvt = new Type[] { typeof(Vector128<float>) };
  64. string nameMov = op.RegisterSize == RegisterSize.Simd128
  65. ? nameof(Sse.MoveHighToLow)
  66. : nameof(Sse.MoveLowToHigh);
  67. context.EmitLdvec(op.Rn);
  68. context.Emit(OpCodes.Dup);
  69. context.EmitCall(typeof(Sse).GetMethod(nameMov));
  70. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Double), typesCvt));
  71. context.EmitStvec(op.Rd);
  72. }
  73. else
  74. {
  75. int elems = 4 >> sizeF;
  76. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  77. for (int index = 0; index < elems; index++)
  78. {
  79. if (sizeF == 0)
  80. {
  81. EmitVectorExtractZx(context, op.Rn, part + index, 1);
  82. context.Emit(OpCodes.Conv_U2);
  83. context.EmitLdarg(TranslatedSub.StateArgIdx);
  84. context.EmitCall(typeof(SoftFloat16_32), nameof(SoftFloat16_32.FPConvert));
  85. }
  86. else /* if (sizeF == 1) */
  87. {
  88. EmitVectorExtractF(context, op.Rn, part + index, 0);
  89. context.Emit(OpCodes.Conv_R8);
  90. }
  91. EmitVectorInsertTmpF(context, index, sizeF);
  92. }
  93. context.EmitLdvectmp();
  94. context.EmitStvec(op.Rd);
  95. }
  96. }
  97. public static void Fcvtms_Gp(ILEmitterCtx context)
  98. {
  99. EmitFcvt_s_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Floor)));
  100. }
  101. public static void Fcvtmu_Gp(ILEmitterCtx context)
  102. {
  103. EmitFcvt_u_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Floor)));
  104. }
  105. public static void Fcvtn_V(ILEmitterCtx context)
  106. {
  107. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  108. int sizeF = op.Size & 1;
  109. if (Optimizations.UseSse2 && sizeF == 1)
  110. {
  111. Type[] typesCvt = new Type[] { typeof(Vector128<double>) };
  112. string nameMov = op.RegisterSize == RegisterSize.Simd128
  113. ? nameof(Sse.MoveLowToHigh)
  114. : nameof(Sse.MoveHighToLow);
  115. context.EmitLdvec(op.Rd);
  116. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  117. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  118. context.EmitLdvec(op.Rn);
  119. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
  120. context.Emit(OpCodes.Dup);
  121. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  122. context.EmitCall(typeof(Sse).GetMethod(nameMov));
  123. context.EmitStvec(op.Rd);
  124. }
  125. else
  126. {
  127. int elems = 4 >> sizeF;
  128. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  129. if (part != 0)
  130. {
  131. context.EmitLdvec(op.Rd);
  132. context.EmitStvectmp();
  133. }
  134. for (int index = 0; index < elems; index++)
  135. {
  136. EmitVectorExtractF(context, op.Rn, index, sizeF);
  137. if (sizeF == 0)
  138. {
  139. context.EmitLdarg(TranslatedSub.StateArgIdx);
  140. context.EmitCall(typeof(SoftFloat32_16), nameof(SoftFloat32_16.FPConvert));
  141. context.Emit(OpCodes.Conv_U8);
  142. EmitVectorInsertTmp(context, part + index, 1);
  143. }
  144. else /* if (sizeF == 1) */
  145. {
  146. context.Emit(OpCodes.Conv_R4);
  147. EmitVectorInsertTmpF(context, part + index, 0);
  148. }
  149. }
  150. context.EmitLdvectmp();
  151. context.EmitStvec(op.Rd);
  152. if (part == 0)
  153. {
  154. EmitVectorZeroUpper(context, op.Rd);
  155. }
  156. }
  157. }
  158. public static void Fcvtns_S(ILEmitterCtx context)
  159. {
  160. EmitFcvtn(context, signed: true, scalar: true);
  161. }
  162. public static void Fcvtns_V(ILEmitterCtx context)
  163. {
  164. EmitFcvtn(context, signed: true, scalar: false);
  165. }
  166. public static void Fcvtnu_S(ILEmitterCtx context)
  167. {
  168. EmitFcvtn(context, signed: false, scalar: true);
  169. }
  170. public static void Fcvtnu_V(ILEmitterCtx context)
  171. {
  172. EmitFcvtn(context, signed: false, scalar: false);
  173. }
  174. public static void Fcvtps_Gp(ILEmitterCtx context)
  175. {
  176. EmitFcvt_s_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Ceiling)));
  177. }
  178. public static void Fcvtpu_Gp(ILEmitterCtx context)
  179. {
  180. EmitFcvt_u_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Ceiling)));
  181. }
  182. public static void Fcvtzs_Gp(ILEmitterCtx context)
  183. {
  184. EmitFcvt_s_Gp(context, () => { });
  185. }
  186. public static void Fcvtzs_Gp_Fixed(ILEmitterCtx context)
  187. {
  188. EmitFcvtzs_Gp_Fixed(context);
  189. }
  190. public static void Fcvtzs_S(ILEmitterCtx context)
  191. {
  192. EmitScalarFcvtzs(context);
  193. }
  194. public static void Fcvtzs_V(ILEmitterCtx context)
  195. {
  196. EmitVectorFcvtzs(context);
  197. }
  198. public static void Fcvtzu_Gp(ILEmitterCtx context)
  199. {
  200. EmitFcvt_u_Gp(context, () => { });
  201. }
  202. public static void Fcvtzu_Gp_Fixed(ILEmitterCtx context)
  203. {
  204. EmitFcvtzu_Gp_Fixed(context);
  205. }
  206. public static void Fcvtzu_S(ILEmitterCtx context)
  207. {
  208. EmitScalarFcvtzu(context);
  209. }
  210. public static void Fcvtzu_V(ILEmitterCtx context)
  211. {
  212. EmitVectorFcvtzu(context);
  213. }
  214. public static void Scvtf_Gp(ILEmitterCtx context)
  215. {
  216. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  217. context.EmitLdintzr(op.Rn);
  218. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  219. {
  220. context.Emit(OpCodes.Conv_U4);
  221. }
  222. EmitFloatCast(context, op.Size);
  223. EmitScalarSetF(context, op.Rd, op.Size);
  224. }
  225. public static void Scvtf_Gp_Fixed(ILEmitterCtx context)
  226. {
  227. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  228. context.EmitLdintzr(op.Rn);
  229. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  230. {
  231. context.Emit(OpCodes.Conv_I4);
  232. }
  233. EmitFloatCast(context, op.Size);
  234. EmitI2fFBitsMul(context, op.Size, op.FBits);
  235. EmitScalarSetF(context, op.Rd, op.Size);
  236. }
  237. public static void Scvtf_S(ILEmitterCtx context)
  238. {
  239. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  240. EmitVectorExtractSx(context, op.Rn, 0, op.Size + 2);
  241. EmitFloatCast(context, op.Size);
  242. EmitScalarSetF(context, op.Rd, op.Size);
  243. }
  244. public static void Scvtf_V(ILEmitterCtx context)
  245. {
  246. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  247. int sizeF = op.Size & 1;
  248. if (Optimizations.UseSse2 && sizeF == 0)
  249. {
  250. Type[] typesCvt = new Type[] { typeof(Vector128<int>) };
  251. context.EmitLdvec(op.Rn);
  252. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
  253. context.EmitStvec(op.Rd);
  254. if (op.RegisterSize == RegisterSize.Simd64)
  255. {
  256. EmitVectorZeroUpper(context, op.Rd);
  257. }
  258. }
  259. else
  260. {
  261. EmitVectorCvtf(context, signed: true);
  262. }
  263. }
  264. public static void Ucvtf_Gp(ILEmitterCtx context)
  265. {
  266. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  267. context.EmitLdintzr(op.Rn);
  268. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  269. {
  270. context.Emit(OpCodes.Conv_U4);
  271. }
  272. context.Emit(OpCodes.Conv_R_Un);
  273. EmitFloatCast(context, op.Size);
  274. EmitScalarSetF(context, op.Rd, op.Size);
  275. }
  276. public static void Ucvtf_Gp_Fixed(ILEmitterCtx context)
  277. {
  278. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  279. context.EmitLdintzr(op.Rn);
  280. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  281. {
  282. context.Emit(OpCodes.Conv_U4);
  283. }
  284. context.Emit(OpCodes.Conv_R_Un);
  285. EmitFloatCast(context, op.Size);
  286. EmitI2fFBitsMul(context, op.Size, op.FBits);
  287. EmitScalarSetF(context, op.Rd, op.Size);
  288. }
  289. public static void Ucvtf_S(ILEmitterCtx context)
  290. {
  291. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  292. EmitVectorExtractZx(context, op.Rn, 0, op.Size + 2);
  293. context.Emit(OpCodes.Conv_R_Un);
  294. EmitFloatCast(context, op.Size);
  295. EmitScalarSetF(context, op.Rd, op.Size);
  296. }
  297. public static void Ucvtf_V(ILEmitterCtx context)
  298. {
  299. EmitVectorCvtf(context, signed: false);
  300. }
  301. private static void EmitFcvtn(ILEmitterCtx context, bool signed, bool scalar)
  302. {
  303. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  304. int sizeF = op.Size & 1;
  305. int sizeI = sizeF + 2;
  306. int bytes = op.GetBitsCount() >> 3;
  307. int elems = !scalar ? bytes >> sizeI : 1;
  308. if (scalar && (sizeF == 0))
  309. {
  310. EmitVectorZeroLowerTmp(context);
  311. }
  312. for (int index = 0; index < elems; index++)
  313. {
  314. EmitVectorExtractF(context, op.Rn, index, sizeF);
  315. EmitRoundMathCall(context, MidpointRounding.ToEven);
  316. if (sizeF == 0)
  317. {
  318. VectorHelper.EmitCall(context, signed
  319. ? nameof(VectorHelper.SatF32ToS32)
  320. : nameof(VectorHelper.SatF32ToU32));
  321. context.Emit(OpCodes.Conv_U8);
  322. }
  323. else /* if (sizeF == 1) */
  324. {
  325. VectorHelper.EmitCall(context, signed
  326. ? nameof(VectorHelper.SatF64ToS64)
  327. : nameof(VectorHelper.SatF64ToU64));
  328. }
  329. EmitVectorInsertTmp(context, index, sizeI);
  330. }
  331. context.EmitLdvectmp();
  332. context.EmitStvec(op.Rd);
  333. if ((op.RegisterSize == RegisterSize.Simd64) || scalar)
  334. {
  335. EmitVectorZeroUpper(context, op.Rd);
  336. }
  337. }
  338. private static void EmitFcvt_s_Gp(ILEmitterCtx context, Action emit)
  339. {
  340. EmitFcvt___Gp(context, emit, true);
  341. }
  342. private static void EmitFcvt_u_Gp(ILEmitterCtx context, Action emit)
  343. {
  344. EmitFcvt___Gp(context, emit, false);
  345. }
  346. private static void EmitFcvt___Gp(ILEmitterCtx context, Action emit, bool signed)
  347. {
  348. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  349. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  350. emit();
  351. if (signed)
  352. {
  353. EmitScalarFcvts(context, op.Size, 0);
  354. }
  355. else
  356. {
  357. EmitScalarFcvtu(context, op.Size, 0);
  358. }
  359. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  360. {
  361. context.Emit(OpCodes.Conv_U8);
  362. }
  363. context.EmitStintzr(op.Rd);
  364. }
  365. private static void EmitFcvtzs_Gp_Fixed(ILEmitterCtx context)
  366. {
  367. EmitFcvtz__Gp_Fixed(context, true);
  368. }
  369. private static void EmitFcvtzu_Gp_Fixed(ILEmitterCtx context)
  370. {
  371. EmitFcvtz__Gp_Fixed(context, false);
  372. }
  373. private static void EmitFcvtz__Gp_Fixed(ILEmitterCtx context, bool signed)
  374. {
  375. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  376. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  377. if (signed)
  378. {
  379. EmitScalarFcvts(context, op.Size, op.FBits);
  380. }
  381. else
  382. {
  383. EmitScalarFcvtu(context, op.Size, op.FBits);
  384. }
  385. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  386. {
  387. context.Emit(OpCodes.Conv_U8);
  388. }
  389. context.EmitStintzr(op.Rd);
  390. }
  391. private static void EmitVectorCvtf(ILEmitterCtx context, bool signed)
  392. {
  393. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  394. int sizeF = op.Size & 1;
  395. int sizeI = sizeF + 2;
  396. int fBits = GetFBits(context);
  397. int bytes = op.GetBitsCount() >> 3;
  398. int elems = bytes >> sizeI;
  399. for (int index = 0; index < elems; index++)
  400. {
  401. EmitVectorExtract(context, op.Rn, index, sizeI, signed);
  402. if (!signed)
  403. {
  404. context.Emit(OpCodes.Conv_R_Un);
  405. }
  406. EmitFloatCast(context, sizeF);
  407. EmitI2fFBitsMul(context, sizeF, fBits);
  408. EmitVectorInsertF(context, op.Rd, index, sizeF);
  409. }
  410. if (op.RegisterSize == RegisterSize.Simd64)
  411. {
  412. EmitVectorZeroUpper(context, op.Rd);
  413. }
  414. }
  415. private static void EmitScalarFcvtzs(ILEmitterCtx context)
  416. {
  417. EmitScalarFcvtz(context, true);
  418. }
  419. private static void EmitScalarFcvtzu(ILEmitterCtx context)
  420. {
  421. EmitScalarFcvtz(context, false);
  422. }
  423. private static void EmitScalarFcvtz(ILEmitterCtx context, bool signed)
  424. {
  425. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  426. int sizeF = op.Size & 1;
  427. int sizeI = sizeF + 2;
  428. int fBits = GetFBits(context);
  429. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  430. EmitF2iFBitsMul(context, sizeF, fBits);
  431. if (sizeF == 0)
  432. {
  433. VectorHelper.EmitCall(context, signed
  434. ? nameof(VectorHelper.SatF32ToS32)
  435. : nameof(VectorHelper.SatF32ToU32));
  436. }
  437. else /* if (sizeF == 1) */
  438. {
  439. VectorHelper.EmitCall(context, signed
  440. ? nameof(VectorHelper.SatF64ToS64)
  441. : nameof(VectorHelper.SatF64ToU64));
  442. }
  443. if (sizeF == 0)
  444. {
  445. context.Emit(OpCodes.Conv_U8);
  446. }
  447. EmitScalarSet(context, op.Rd, sizeI);
  448. }
  449. private static void EmitVectorFcvtzs(ILEmitterCtx context)
  450. {
  451. EmitVectorFcvtz(context, true);
  452. }
  453. private static void EmitVectorFcvtzu(ILEmitterCtx context)
  454. {
  455. EmitVectorFcvtz(context, false);
  456. }
  457. private static void EmitVectorFcvtz(ILEmitterCtx context, bool signed)
  458. {
  459. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  460. int sizeF = op.Size & 1;
  461. int sizeI = sizeF + 2;
  462. int fBits = GetFBits(context);
  463. int bytes = op.GetBitsCount() >> 3;
  464. int elems = bytes >> sizeI;
  465. for (int index = 0; index < elems; index++)
  466. {
  467. EmitVectorExtractF(context, op.Rn, index, sizeF);
  468. EmitF2iFBitsMul(context, sizeF, fBits);
  469. if (sizeF == 0)
  470. {
  471. VectorHelper.EmitCall(context, signed
  472. ? nameof(VectorHelper.SatF32ToS32)
  473. : nameof(VectorHelper.SatF32ToU32));
  474. }
  475. else /* if (sizeF == 1) */
  476. {
  477. VectorHelper.EmitCall(context, signed
  478. ? nameof(VectorHelper.SatF64ToS64)
  479. : nameof(VectorHelper.SatF64ToU64));
  480. }
  481. if (sizeF == 0)
  482. {
  483. context.Emit(OpCodes.Conv_U8);
  484. }
  485. EmitVectorInsert(context, op.Rd, index, sizeI);
  486. }
  487. if (op.RegisterSize == RegisterSize.Simd64)
  488. {
  489. EmitVectorZeroUpper(context, op.Rd);
  490. }
  491. }
  492. private static int GetFBits(ILEmitterCtx context)
  493. {
  494. if (context.CurrOp is OpCodeSimdShImm64 op)
  495. {
  496. return GetImmShr(op);
  497. }
  498. return 0;
  499. }
  500. private static void EmitFloatCast(ILEmitterCtx context, int size)
  501. {
  502. if (size == 0)
  503. {
  504. context.Emit(OpCodes.Conv_R4);
  505. }
  506. else if (size == 1)
  507. {
  508. context.Emit(OpCodes.Conv_R8);
  509. }
  510. else
  511. {
  512. throw new ArgumentOutOfRangeException(nameof(size));
  513. }
  514. }
  515. private static void EmitScalarFcvts(ILEmitterCtx context, int size, int fBits)
  516. {
  517. if (size < 0 || size > 1)
  518. {
  519. throw new ArgumentOutOfRangeException(nameof(size));
  520. }
  521. EmitF2iFBitsMul(context, size, fBits);
  522. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  523. {
  524. if (size == 0)
  525. {
  526. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS32));
  527. }
  528. else /* if (size == 1) */
  529. {
  530. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS32));
  531. }
  532. }
  533. else
  534. {
  535. if (size == 0)
  536. {
  537. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS64));
  538. }
  539. else /* if (size == 1) */
  540. {
  541. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS64));
  542. }
  543. }
  544. }
  545. private static void EmitScalarFcvtu(ILEmitterCtx context, int size, int fBits)
  546. {
  547. if (size < 0 || size > 1)
  548. {
  549. throw new ArgumentOutOfRangeException(nameof(size));
  550. }
  551. EmitF2iFBitsMul(context, size, fBits);
  552. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  553. {
  554. if (size == 0)
  555. {
  556. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU32));
  557. }
  558. else /* if (size == 1) */
  559. {
  560. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU32));
  561. }
  562. }
  563. else
  564. {
  565. if (size == 0)
  566. {
  567. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU64));
  568. }
  569. else /* if (size == 1) */
  570. {
  571. VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU64));
  572. }
  573. }
  574. }
  575. private static void EmitF2iFBitsMul(ILEmitterCtx context, int size, int fBits)
  576. {
  577. if (fBits != 0)
  578. {
  579. if (size == 0)
  580. {
  581. context.EmitLdc_R4(MathF.Pow(2f, fBits));
  582. }
  583. else if (size == 1)
  584. {
  585. context.EmitLdc_R8(Math.Pow(2d, fBits));
  586. }
  587. else
  588. {
  589. throw new ArgumentOutOfRangeException(nameof(size));
  590. }
  591. context.Emit(OpCodes.Mul);
  592. }
  593. }
  594. private static void EmitI2fFBitsMul(ILEmitterCtx context, int size, int fBits)
  595. {
  596. if (fBits != 0)
  597. {
  598. if (size == 0)
  599. {
  600. context.EmitLdc_R4(1f / MathF.Pow(2f, fBits));
  601. }
  602. else if (size == 1)
  603. {
  604. context.EmitLdc_R8(1d / Math.Pow(2d, fBits));
  605. }
  606. else
  607. {
  608. throw new ArgumentOutOfRangeException(nameof(size));
  609. }
  610. context.Emit(OpCodes.Mul);
  611. }
  612. }
  613. }
  614. }