InstEmitSimdCvt.cs 22 KB

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