InstEmitSimdCvt32.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Reflection;
  8. using static ARMeilleure.Instructions.InstEmitHelper;
  9. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  10. using static ARMeilleure.Instructions.InstEmitSimdHelper32;
  11. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  12. namespace ARMeilleure.Instructions
  13. {
  14. static partial class InstEmit32
  15. {
  16. private static int FlipVdBits(int vd, bool lowBit)
  17. {
  18. if (lowBit)
  19. {
  20. // Move the low bit to the top.
  21. return ((vd & 0x1) << 4) | (vd >> 1);
  22. }
  23. else
  24. {
  25. // Move the high bit to the bottom.
  26. return ((vd & 0xf) << 1) | (vd >> 4);
  27. }
  28. }
  29. private static Operand EmitSaturateFloatToInt(ArmEmitterContext context, Operand op1, bool unsigned)
  30. {
  31. MethodInfo info;
  32. if (op1.Type == OperandType.FP64)
  33. {
  34. info = unsigned
  35. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToU32))
  36. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToS32));
  37. }
  38. else
  39. {
  40. info = unsigned
  41. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToU32))
  42. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToS32));
  43. }
  44. return context.Call(info, op1);
  45. }
  46. public static void Vcvt_V(ArmEmitterContext context)
  47. {
  48. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  49. bool unsigned = (op.Opc & 1) != 0;
  50. bool toInteger = (op.Opc & 2) != 0;
  51. OperandType floatSize = (op.Size == 2) ? OperandType.FP32 : OperandType.FP64;
  52. if (toInteger)
  53. {
  54. if (Optimizations.UseSse41)
  55. {
  56. EmitSse41ConvertVector32(context, FPRoundingMode.TowardsZero, !unsigned);
  57. }
  58. else
  59. {
  60. EmitVectorUnaryOpF32(context, (op1) =>
  61. {
  62. return EmitSaturateFloatToInt(context, op1, unsigned);
  63. });
  64. }
  65. }
  66. else
  67. {
  68. if (Optimizations.UseSse2)
  69. {
  70. EmitVectorUnaryOpSimd32(context, (n) =>
  71. {
  72. if (unsigned)
  73. {
  74. Operand mask = X86GetAllElements(context, 0x47800000);
  75. Operand res = context.AddIntrinsic(Intrinsic.X86Psrld, n, Const(16));
  76. res = context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, res);
  77. res = context.AddIntrinsic(Intrinsic.X86Mulps, res, mask);
  78. Operand res2 = context.AddIntrinsic(Intrinsic.X86Pslld, n, Const(16));
  79. res2 = context.AddIntrinsic(Intrinsic.X86Psrld, res2, Const(16));
  80. res2 = context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, res2);
  81. return context.AddIntrinsic(Intrinsic.X86Addps, res, res2);
  82. }
  83. else
  84. {
  85. return context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, n);
  86. }
  87. });
  88. }
  89. else
  90. {
  91. if (unsigned)
  92. {
  93. EmitVectorUnaryOpZx32(context, (op1) => EmitFPConvert(context, op1, floatSize, false));
  94. }
  95. else
  96. {
  97. EmitVectorUnaryOpSx32(context, (op1) => EmitFPConvert(context, op1, floatSize, true));
  98. }
  99. }
  100. }
  101. }
  102. public static void Vcvt_FD(ArmEmitterContext context)
  103. {
  104. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  105. int vm = op.Vm;
  106. int vd;
  107. if (op.Size == 3)
  108. {
  109. vd = FlipVdBits(op.Vd, false);
  110. // Double to single.
  111. Operand fp = ExtractScalar(context, OperandType.FP64, vm);
  112. Operand res = context.ConvertToFP(OperandType.FP32, fp);
  113. InsertScalar(context, vd, res);
  114. }
  115. else
  116. {
  117. vd = FlipVdBits(op.Vd, true);
  118. // Single to double.
  119. Operand fp = ExtractScalar(context, OperandType.FP32, vm);
  120. Operand res = context.ConvertToFP(OperandType.FP64, fp);
  121. InsertScalar(context, vd, res);
  122. }
  123. }
  124. // VCVT (floating-point to integer, floating-point) | VCVT (integer to floating-point, floating-point).
  125. public static void Vcvt_FI(ArmEmitterContext context)
  126. {
  127. OpCode32SimdCvtFI op = (OpCode32SimdCvtFI)context.CurrOp;
  128. bool toInteger = (op.Opc2 & 0b100) != 0;
  129. OperandType floatSize = op.RegisterSize == RegisterSize.Int64 ? OperandType.FP64 : OperandType.FP32;
  130. if (toInteger)
  131. {
  132. bool unsigned = (op.Opc2 & 1) == 0;
  133. bool roundWithFpscr = op.Opc != 1;
  134. if (!roundWithFpscr && Optimizations.UseSse41)
  135. {
  136. EmitSse41ConvertInt32(context, FPRoundingMode.TowardsZero, !unsigned);
  137. }
  138. else
  139. {
  140. Operand toConvert = ExtractScalar(context, floatSize, op.Vm);
  141. Operand asInteger;
  142. // TODO: Fast Path.
  143. if (roundWithFpscr)
  144. {
  145. MethodInfo info;
  146. if (floatSize == OperandType.FP64)
  147. {
  148. info = unsigned
  149. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.DoubleToUInt32))
  150. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.DoubleToInt32));
  151. }
  152. else
  153. {
  154. info = unsigned
  155. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.FloatToUInt32))
  156. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.FloatToInt32));
  157. }
  158. asInteger = context.Call(info, toConvert);
  159. }
  160. else
  161. {
  162. // Round towards zero.
  163. asInteger = EmitSaturateFloatToInt(context, toConvert, unsigned);
  164. }
  165. InsertScalar(context, op.Vd, asInteger);
  166. }
  167. }
  168. else
  169. {
  170. bool unsigned = op.Opc == 0;
  171. Operand toConvert = ExtractScalar(context, OperandType.I32, op.Vm);
  172. Operand asFloat = EmitFPConvert(context, toConvert, floatSize, !unsigned);
  173. InsertScalar(context, op.Vd, asFloat);
  174. }
  175. }
  176. private static Operand EmitRoundMathCall(ArmEmitterContext context, MidpointRounding roundMode, Operand n)
  177. {
  178. IOpCode32Simd op = (IOpCode32Simd)context.CurrOp;
  179. string name = nameof(Math.Round);
  180. MethodInfo info = (op.Size & 1) == 0
  181. ? typeof(MathF).GetMethod(name, new Type[] { typeof(float), typeof(MidpointRounding) })
  182. : typeof(Math). GetMethod(name, new Type[] { typeof(double), typeof(MidpointRounding) });
  183. return context.Call(info, n, Const((int)roundMode));
  184. }
  185. private static FPRoundingMode RMToRoundMode(int rm)
  186. {
  187. FPRoundingMode roundMode;
  188. switch (rm)
  189. {
  190. case 0b01:
  191. roundMode = FPRoundingMode.ToNearest;
  192. break;
  193. case 0b10:
  194. roundMode = FPRoundingMode.TowardsPlusInfinity;
  195. break;
  196. case 0b11:
  197. roundMode = FPRoundingMode.TowardsMinusInfinity;
  198. break;
  199. default:
  200. throw new ArgumentOutOfRangeException(nameof(rm));
  201. }
  202. return roundMode;
  203. }
  204. // VCVTA/M/N/P (floating-point).
  205. public static void Vcvt_RM(ArmEmitterContext context)
  206. {
  207. OpCode32SimdCvtFI op = (OpCode32SimdCvtFI)context.CurrOp; // toInteger == true (opCode<18> == 1 => Opc2<2> == 1).
  208. OperandType floatSize = op.RegisterSize == RegisterSize.Int64 ? OperandType.FP64 : OperandType.FP32;
  209. bool unsigned = op.Opc == 0;
  210. int rm = op.Opc2 & 3;
  211. if (Optimizations.UseSse41 && rm != 0b00)
  212. {
  213. EmitSse41ConvertInt32(context, RMToRoundMode(rm), !unsigned);
  214. }
  215. else
  216. {
  217. Operand toConvert = ExtractScalar(context, floatSize, op.Vm);
  218. switch (rm)
  219. {
  220. case 0b00: // Away
  221. toConvert = EmitRoundMathCall(context, MidpointRounding.AwayFromZero, toConvert);
  222. break;
  223. case 0b01: // Nearest
  224. toConvert = EmitRoundMathCall(context, MidpointRounding.ToEven, toConvert);
  225. break;
  226. case 0b10: // Towards positive infinity
  227. toConvert = EmitUnaryMathCall(context, nameof(Math.Ceiling), toConvert);
  228. break;
  229. case 0b11: // Towards negative infinity
  230. toConvert = EmitUnaryMathCall(context, nameof(Math.Floor), toConvert);
  231. break;
  232. }
  233. Operand asInteger;
  234. asInteger = EmitSaturateFloatToInt(context, toConvert, unsigned);
  235. InsertScalar(context, op.Vd, asInteger);
  236. }
  237. }
  238. // VRINTA/M/N/P (floating-point).
  239. public static void Vrint_RM(ArmEmitterContext context)
  240. {
  241. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  242. OperandType floatSize = op.RegisterSize == RegisterSize.Int64 ? OperandType.FP64 : OperandType.FP32;
  243. int rm = op.Opc2 & 3;
  244. if (Optimizations.UseSse2 && rm != 0b00)
  245. {
  246. EmitScalarUnaryOpSimd32(context, (m) =>
  247. {
  248. Intrinsic inst = (op.Size & 1) == 0 ? Intrinsic.X86Roundss : Intrinsic.X86Roundsd;
  249. FPRoundingMode roundMode = RMToRoundMode(rm);
  250. return context.AddIntrinsic(inst, m, Const(X86GetRoundControl(roundMode)));
  251. });
  252. }
  253. else
  254. {
  255. Operand toConvert = ExtractScalar(context, floatSize, op.Vm);
  256. switch (rm)
  257. {
  258. case 0b00: // Away
  259. toConvert = EmitRoundMathCall(context, MidpointRounding.AwayFromZero, toConvert);
  260. break;
  261. case 0b01: // Nearest
  262. toConvert = EmitRoundMathCall(context, MidpointRounding.ToEven, toConvert);
  263. break;
  264. case 0b10: // Towards positive infinity
  265. toConvert = EmitUnaryMathCall(context, nameof(Math.Ceiling), toConvert);
  266. break;
  267. case 0b11: // Towards negative infinity
  268. toConvert = EmitUnaryMathCall(context, nameof(Math.Floor), toConvert);
  269. break;
  270. }
  271. InsertScalar(context, op.Vd, toConvert);
  272. }
  273. }
  274. // VRINTA (vector).
  275. public static void Vrinta_V(ArmEmitterContext context)
  276. {
  277. EmitVectorUnaryOpF32(context, (m) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, m));
  278. }
  279. // VRINTM (vector).
  280. public static void Vrintm_V(ArmEmitterContext context)
  281. {
  282. if (Optimizations.UseSse2)
  283. {
  284. EmitVectorUnaryOpSimd32(context, (m) =>
  285. {
  286. return context.AddIntrinsic(Intrinsic.X86Roundps, m, Const(X86GetRoundControl(FPRoundingMode.TowardsMinusInfinity)));
  287. });
  288. }
  289. else
  290. {
  291. EmitVectorUnaryOpF32(context, (m) => EmitUnaryMathCall(context, nameof(Math.Floor), m));
  292. }
  293. }
  294. // VRINTN (vector).
  295. public static void Vrintn_V(ArmEmitterContext context)
  296. {
  297. if (Optimizations.UseSse2)
  298. {
  299. EmitVectorUnaryOpSimd32(context, (m) =>
  300. {
  301. return context.AddIntrinsic(Intrinsic.X86Roundps, m, Const(X86GetRoundControl(FPRoundingMode.ToNearest)));
  302. });
  303. }
  304. else
  305. {
  306. EmitVectorUnaryOpF32(context, (m) => EmitRoundMathCall(context, MidpointRounding.ToEven, m));
  307. }
  308. }
  309. // VRINTP (vector).
  310. public static void Vrintp_V(ArmEmitterContext context)
  311. {
  312. if (Optimizations.UseSse2)
  313. {
  314. EmitVectorUnaryOpSimd32(context, (m) =>
  315. {
  316. return context.AddIntrinsic(Intrinsic.X86Roundps, m, Const(X86GetRoundControl(FPRoundingMode.TowardsPlusInfinity)));
  317. });
  318. }
  319. else
  320. {
  321. EmitVectorUnaryOpF32(context, (m) => EmitUnaryMathCall(context, nameof(Math.Ceiling), m));
  322. }
  323. }
  324. // VRINTZ (floating-point).
  325. public static void Vrint_Z(ArmEmitterContext context)
  326. {
  327. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  328. if (Optimizations.UseSse2)
  329. {
  330. EmitScalarUnaryOpSimd32(context, (m) =>
  331. {
  332. Intrinsic inst = (op.Size & 1) == 0 ? Intrinsic.X86Roundss : Intrinsic.X86Roundsd;
  333. return context.AddIntrinsic(inst, m, Const(X86GetRoundControl(FPRoundingMode.TowardsZero)));
  334. });
  335. }
  336. else
  337. {
  338. EmitScalarUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Truncate), op1));
  339. }
  340. }
  341. // VRINTX (floating-point).
  342. public static void Vrintx_S(ArmEmitterContext context)
  343. {
  344. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  345. bool doubleSize = (op.Size & 1) == 1;
  346. string methodName = doubleSize ? nameof(SoftFallback.Round) : nameof(SoftFallback.RoundF);
  347. EmitScalarUnaryOpF32(context, (op1) =>
  348. {
  349. MethodInfo info = typeof(SoftFallback).GetMethod(methodName);
  350. return context.Call(info, op1);
  351. });
  352. }
  353. private static Operand EmitFPConvert(ArmEmitterContext context, Operand value, OperandType type, bool signed)
  354. {
  355. Debug.Assert(value.Type == OperandType.I32 || value.Type == OperandType.I64);
  356. if (signed)
  357. {
  358. return context.ConvertToFP(type, value);
  359. }
  360. else
  361. {
  362. return context.ConvertToFPUI(type, value);
  363. }
  364. }
  365. private static void EmitSse41ConvertInt32(ArmEmitterContext context, FPRoundingMode roundMode, bool signed)
  366. {
  367. // A port of the similar round function in InstEmitSimdCvt.
  368. OpCode32SimdCvtFI op = (OpCode32SimdCvtFI)context.CurrOp;
  369. bool doubleSize = (op.Size & 1) != 0;
  370. int shift = doubleSize ? 1 : 2;
  371. Operand n = GetVecA32(op.Vm >> shift);
  372. n = EmitSwapScalar(context, n, op.Vm, doubleSize);
  373. if (!doubleSize)
  374. {
  375. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpss, n, n, Const((int)CmpCondition.OrderedQ));
  376. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  377. nRes = context.AddIntrinsic(Intrinsic.X86Roundss, nRes, Const(X86GetRoundControl(roundMode)));
  378. Operand zero = context.VectorZero();
  379. Operand nCmp;
  380. Operand nIntOrLong2 = default;
  381. if (!signed)
  382. {
  383. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpss, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  384. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  385. }
  386. int fpMaxVal = 0x4F000000; // 2.14748365E9f (2147483648)
  387. Operand fpMaxValMask = X86GetScalar(context, fpMaxVal);
  388. Operand nIntOrLong = context.AddIntrinsicInt(Intrinsic.X86Cvtss2si, nRes);
  389. if (!signed)
  390. {
  391. nRes = context.AddIntrinsic(Intrinsic.X86Subss, nRes, fpMaxValMask);
  392. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpss, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  393. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  394. nIntOrLong2 = context.AddIntrinsicInt(Intrinsic.X86Cvtss2si, nRes);
  395. }
  396. nRes = context.AddIntrinsic(Intrinsic.X86Cmpss, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  397. Operand nInt = context.AddIntrinsicInt(Intrinsic.X86Cvtsi2si, nRes);
  398. Operand dRes;
  399. if (signed)
  400. {
  401. dRes = context.BitwiseExclusiveOr(nIntOrLong, nInt);
  402. }
  403. else
  404. {
  405. dRes = context.BitwiseExclusiveOr(nIntOrLong2, nInt);
  406. dRes = context.Add(dRes, nIntOrLong);
  407. }
  408. InsertScalar(context, op.Vd, dRes);
  409. }
  410. else
  411. {
  412. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpsd, n, n, Const((int)CmpCondition.OrderedQ));
  413. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  414. nRes = context.AddIntrinsic(Intrinsic.X86Roundsd, nRes, Const(X86GetRoundControl(roundMode)));
  415. Operand zero = context.VectorZero();
  416. Operand nCmp;
  417. Operand nIntOrLong2 = default;
  418. if (!signed)
  419. {
  420. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpsd, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  421. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  422. }
  423. long fpMaxVal = 0x41E0000000000000L; // 2147483648.0000000d (2147483648)
  424. Operand fpMaxValMask = X86GetScalar(context, fpMaxVal);
  425. Operand nIntOrLong = context.AddIntrinsicInt(Intrinsic.X86Cvtsd2si, nRes);
  426. if (!signed)
  427. {
  428. nRes = context.AddIntrinsic(Intrinsic.X86Subsd, nRes, fpMaxValMask);
  429. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpsd, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  430. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  431. nIntOrLong2 = context.AddIntrinsicInt(Intrinsic.X86Cvtsd2si, nRes);
  432. }
  433. nRes = context.AddIntrinsic(Intrinsic.X86Cmpsd, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  434. Operand nLong = context.AddIntrinsicLong(Intrinsic.X86Cvtsi2si, nRes);
  435. nLong = context.ConvertI64ToI32(nLong);
  436. Operand dRes;
  437. if (signed)
  438. {
  439. dRes = context.BitwiseExclusiveOr(nIntOrLong, nLong);
  440. }
  441. else
  442. {
  443. dRes = context.BitwiseExclusiveOr(nIntOrLong2, nLong);
  444. dRes = context.Add(dRes, nIntOrLong);
  445. }
  446. InsertScalar(context, op.Vd, dRes);
  447. }
  448. }
  449. private static void EmitSse41ConvertVector32(ArmEmitterContext context, FPRoundingMode roundMode, bool signed)
  450. {
  451. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  452. EmitVectorUnaryOpSimd32(context, (n) =>
  453. {
  454. int sizeF = op.Size & 1;
  455. if (sizeF == 0)
  456. {
  457. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpps, n, n, Const((int)CmpCondition.OrderedQ));
  458. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  459. nRes = context.AddIntrinsic(Intrinsic.X86Roundps, nRes, Const(X86GetRoundControl(roundMode)));
  460. Operand zero = context.VectorZero();
  461. Operand nCmp;
  462. if (!signed)
  463. {
  464. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpps, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  465. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  466. }
  467. Operand fpMaxValMask = X86GetAllElements(context, 0x4F000000); // 2.14748365E9f (2147483648)
  468. Operand nInt = context.AddIntrinsic(Intrinsic.X86Cvtps2dq, nRes);
  469. Operand nInt2 = default;
  470. if (!signed)
  471. {
  472. nRes = context.AddIntrinsic(Intrinsic.X86Subps, nRes, fpMaxValMask);
  473. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpps, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  474. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  475. nInt2 = context.AddIntrinsic(Intrinsic.X86Cvtps2dq, nRes);
  476. }
  477. nRes = context.AddIntrinsic(Intrinsic.X86Cmpps, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  478. if (signed)
  479. {
  480. return context.AddIntrinsic(Intrinsic.X86Pxor, nInt, nRes);
  481. }
  482. else
  483. {
  484. Operand dRes = context.AddIntrinsic(Intrinsic.X86Pxor, nInt2, nRes);
  485. return context.AddIntrinsic(Intrinsic.X86Paddd, dRes, nInt);
  486. }
  487. }
  488. else /* if (sizeF == 1) */
  489. {
  490. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmppd, n, n, Const((int)CmpCondition.OrderedQ));
  491. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  492. nRes = context.AddIntrinsic(Intrinsic.X86Roundpd, nRes, Const(X86GetRoundControl(roundMode)));
  493. Operand zero = context.VectorZero();
  494. Operand nCmp;
  495. if (!signed)
  496. {
  497. nCmp = context.AddIntrinsic(Intrinsic.X86Cmppd, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  498. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  499. }
  500. Operand fpMaxValMask = X86GetAllElements(context, 0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
  501. Operand nLong = InstEmit.EmitSse2CvtDoubleToInt64OpF(context, nRes, false);
  502. Operand nLong2 = default;
  503. if (!signed)
  504. {
  505. nRes = context.AddIntrinsic(Intrinsic.X86Subpd, nRes, fpMaxValMask);
  506. nCmp = context.AddIntrinsic(Intrinsic.X86Cmppd, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  507. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  508. nLong2 = InstEmit.EmitSse2CvtDoubleToInt64OpF(context, nRes, false);
  509. }
  510. nRes = context.AddIntrinsic(Intrinsic.X86Cmppd, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  511. if (signed)
  512. {
  513. return context.AddIntrinsic(Intrinsic.X86Pxor, nLong, nRes);
  514. }
  515. else
  516. {
  517. Operand dRes = context.AddIntrinsic(Intrinsic.X86Pxor, nLong2, nRes);
  518. return context.AddIntrinsic(Intrinsic.X86Paddq, dRes, nLong);
  519. }
  520. }
  521. });
  522. }
  523. }
  524. }