InstEmitSimdCvt.cs 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  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.IntermediateRepresentation.Operand.Factory;
  11. namespace ARMeilleure.Instructions
  12. {
  13. using Func1I = Func<Operand, Operand>;
  14. static partial class InstEmit
  15. {
  16. public static void Fcvt_S(ArmEmitterContext context)
  17. {
  18. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  19. if (op.Size == 0 && op.Opc == 1) // Single -> Double.
  20. {
  21. if (Optimizations.UseSse2)
  22. {
  23. Operand n = GetVec(op.Rn);
  24. Operand res = context.AddIntrinsic(Intrinsic.X86Cvtss2sd, context.VectorZero(), n);
  25. context.Copy(GetVec(op.Rd), res);
  26. }
  27. else
  28. {
  29. Operand ne = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), 0);
  30. Operand res = context.ConvertToFP(OperandType.FP64, ne);
  31. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
  32. }
  33. }
  34. else if (op.Size == 1 && op.Opc == 0) // Double -> Single.
  35. {
  36. if (Optimizations.UseSse2)
  37. {
  38. Operand n = GetVec(op.Rn);
  39. Operand res = context.AddIntrinsic(Intrinsic.X86Cvtsd2ss, context.VectorZero(), n);
  40. context.Copy(GetVec(op.Rd), res);
  41. }
  42. else
  43. {
  44. Operand ne = context.VectorExtract(OperandType.FP64, GetVec(op.Rn), 0);
  45. Operand res = context.ConvertToFP(OperandType.FP32, ne);
  46. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
  47. }
  48. }
  49. else if (op.Size == 0 && op.Opc == 3) // Single -> Half.
  50. {
  51. if (Optimizations.UseF16c)
  52. {
  53. Debug.Assert(!Optimizations.ForceLegacySse);
  54. Operand n = GetVec(op.Rn);
  55. Operand res = context.AddIntrinsic(Intrinsic.X86Vcvtps2ph, n, Const(X86GetRoundControl(FPRoundingMode.ToNearest)));
  56. res = context.AddIntrinsic(Intrinsic.X86Pslldq, res, Const(14)); // VectorZeroUpper112()
  57. res = context.AddIntrinsic(Intrinsic.X86Psrldq, res, Const(14));
  58. context.Copy(GetVec(op.Rd), res);
  59. }
  60. else
  61. {
  62. Operand ne = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), 0);
  63. context.StoreToContext();
  64. Operand res = context.Call(typeof(SoftFloat32_16).GetMethod(nameof(SoftFloat32_16.FPConvert)), ne);
  65. context.LoadFromContext();
  66. res = context.ZeroExtend16(OperandType.I64, res);
  67. context.Copy(GetVec(op.Rd), EmitVectorInsert(context, context.VectorZero(), res, 0, 1));
  68. }
  69. }
  70. else if (op.Size == 3 && op.Opc == 0) // Half -> Single.
  71. {
  72. if (Optimizations.UseF16c)
  73. {
  74. Debug.Assert(!Optimizations.ForceLegacySse);
  75. Operand res = context.AddIntrinsic(Intrinsic.X86Vcvtph2ps, GetVec(op.Rn));
  76. res = context.VectorZeroUpper96(res);
  77. context.Copy(GetVec(op.Rd), res);
  78. }
  79. else
  80. {
  81. Operand ne = EmitVectorExtractZx(context, op.Rn, 0, 1);
  82. context.StoreToContext();
  83. Operand res = context.Call(typeof(SoftFloat16_32).GetMethod(nameof(SoftFloat16_32.FPConvert)), ne);
  84. context.LoadFromContext();
  85. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
  86. }
  87. }
  88. else if (op.Size == 1 && op.Opc == 3) // Double -> Half.
  89. {
  90. if (Optimizations.UseF16c)
  91. {
  92. Debug.Assert(!Optimizations.ForceLegacySse);
  93. Operand n = GetVec(op.Rn);
  94. Operand res = context.AddIntrinsic(Intrinsic.X86Cvtsd2ss, context.VectorZero(), n);
  95. res = context.AddIntrinsic(Intrinsic.X86Vcvtps2ph, res, Const(X86GetRoundControl(FPRoundingMode.ToNearest)));
  96. context.Copy(GetVec(op.Rd), res);
  97. }
  98. else
  99. {
  100. Operand ne = context.VectorExtract(OperandType.FP64, GetVec(op.Rn), 0);
  101. context.StoreToContext();
  102. Operand res = context.Call(typeof(SoftFloat64_16).GetMethod(nameof(SoftFloat64_16.FPConvert)), ne);
  103. context.LoadFromContext();
  104. res = context.ZeroExtend16(OperandType.I64, res);
  105. context.Copy(GetVec(op.Rd), EmitVectorInsert(context, context.VectorZero(), res, 0, 1));
  106. }
  107. }
  108. else if (op.Size == 3 && op.Opc == 1) // Half -> Double.
  109. {
  110. if (Optimizations.UseF16c)
  111. {
  112. Operand n = GetVec(op.Rn);
  113. Operand res = context.AddIntrinsic(Intrinsic.X86Vcvtph2ps, GetVec(op.Rn));
  114. res = context.AddIntrinsic(Intrinsic.X86Cvtss2sd, context.VectorZero(), res);
  115. res = context.VectorZeroUpper64(res);
  116. context.Copy(GetVec(op.Rd), res);
  117. }
  118. else
  119. {
  120. Operand ne = EmitVectorExtractZx(context, op.Rn, 0, 1);
  121. context.StoreToContext();
  122. Operand res = context.Call(typeof(SoftFloat16_64).GetMethod(nameof(SoftFloat16_64.FPConvert)), ne);
  123. context.LoadFromContext();
  124. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
  125. }
  126. }
  127. else // Invalid encoding.
  128. {
  129. Debug.Assert(false, $"type == {op.Size} && opc == {op.Opc}");
  130. }
  131. }
  132. public static void Fcvtas_Gp(ArmEmitterContext context)
  133. {
  134. if (Optimizations.UseAdvSimd)
  135. {
  136. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtasGp);
  137. }
  138. else if (Optimizations.UseSse41)
  139. {
  140. EmitSse41Fcvts_Gp(context, FPRoundingMode.ToNearestAway, isFixed: false);
  141. }
  142. else
  143. {
  144. EmitFcvt_s_Gp(context, (op1) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, op1));
  145. }
  146. }
  147. public static void Fcvtas_S(ArmEmitterContext context)
  148. {
  149. if (Optimizations.UseAdvSimd)
  150. {
  151. InstEmitSimdHelperArm64.EmitScalarUnaryOpF(context, Intrinsic.Arm64FcvtasS);
  152. }
  153. else if (Optimizations.UseSse41)
  154. {
  155. EmitSse41FcvtsOpF(context, FPRoundingMode.ToNearestAway, scalar: true);
  156. }
  157. else
  158. {
  159. EmitFcvt(context, (op1) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, op1), signed: true, scalar: true);
  160. }
  161. }
  162. public static void Fcvtas_V(ArmEmitterContext context)
  163. {
  164. if (Optimizations.UseAdvSimd)
  165. {
  166. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64FcvtasS);
  167. }
  168. else if (Optimizations.UseSse41)
  169. {
  170. EmitSse41FcvtsOpF(context, FPRoundingMode.ToNearestAway, scalar: false);
  171. }
  172. else
  173. {
  174. EmitFcvt(context, (op1) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, op1), signed: true, scalar: false);
  175. }
  176. }
  177. public static void Fcvtau_Gp(ArmEmitterContext context)
  178. {
  179. if (Optimizations.UseAdvSimd)
  180. {
  181. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtauGp);
  182. }
  183. else if (Optimizations.UseSse41)
  184. {
  185. EmitSse41Fcvtu_Gp(context, FPRoundingMode.ToNearestAway, isFixed: false);
  186. }
  187. else
  188. {
  189. EmitFcvt_u_Gp(context, (op1) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, op1));
  190. }
  191. }
  192. public static void Fcvtau_S(ArmEmitterContext context)
  193. {
  194. if (Optimizations.UseAdvSimd)
  195. {
  196. InstEmitSimdHelperArm64.EmitScalarUnaryOpF(context, Intrinsic.Arm64FcvtauS);
  197. }
  198. else if (Optimizations.UseSse41)
  199. {
  200. EmitSse41FcvtuOpF(context, FPRoundingMode.ToNearestAway, scalar: true);
  201. }
  202. else
  203. {
  204. EmitFcvt(context, (op1) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, op1), signed: false, scalar: true);
  205. }
  206. }
  207. public static void Fcvtau_V(ArmEmitterContext context)
  208. {
  209. if (Optimizations.UseAdvSimd)
  210. {
  211. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64FcvtauV);
  212. }
  213. else if (Optimizations.UseSse41)
  214. {
  215. EmitSse41FcvtuOpF(context, FPRoundingMode.ToNearestAway, scalar: false);
  216. }
  217. else
  218. {
  219. EmitFcvt(context, (op1) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, op1), signed: false, scalar: false);
  220. }
  221. }
  222. public static void Fcvtl_V(ArmEmitterContext context)
  223. {
  224. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  225. int sizeF = op.Size & 1;
  226. if (Optimizations.UseAdvSimd)
  227. {
  228. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64FcvtlV);
  229. }
  230. else if (Optimizations.UseSse2 && sizeF == 1)
  231. {
  232. Operand n = GetVec(op.Rn);
  233. Operand res = op.RegisterSize == RegisterSize.Simd128 ? context.AddIntrinsic(Intrinsic.X86Movhlps, n, n) : n;
  234. res = context.AddIntrinsic(Intrinsic.X86Cvtps2pd, res);
  235. context.Copy(GetVec(op.Rd), res);
  236. }
  237. else if (Optimizations.UseF16c && sizeF == 0)
  238. {
  239. Debug.Assert(!Optimizations.ForceLegacySse);
  240. Operand n = GetVec(op.Rn);
  241. Operand res = op.RegisterSize == RegisterSize.Simd128 ? context.AddIntrinsic(Intrinsic.X86Movhlps, n, n) : n;
  242. res = context.AddIntrinsic(Intrinsic.X86Vcvtph2ps, res);
  243. context.Copy(GetVec(op.Rd), res);
  244. }
  245. else
  246. {
  247. Operand res = context.VectorZero();
  248. int elems = 4 >> sizeF;
  249. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  250. for (int index = 0; index < elems; index++)
  251. {
  252. if (sizeF == 0)
  253. {
  254. Operand ne = EmitVectorExtractZx(context, op.Rn, part + index, 1);
  255. context.StoreToContext();
  256. Operand e = context.Call(typeof(SoftFloat16_32).GetMethod(nameof(SoftFloat16_32.FPConvert)), ne);
  257. context.LoadFromContext();
  258. res = context.VectorInsert(res, e, index);
  259. }
  260. else /* if (sizeF == 1) */
  261. {
  262. Operand ne = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), part + index);
  263. Operand e = context.ConvertToFP(OperandType.FP64, ne);
  264. res = context.VectorInsert(res, e, index);
  265. }
  266. }
  267. context.Copy(GetVec(op.Rd), res);
  268. }
  269. }
  270. public static void Fcvtms_Gp(ArmEmitterContext context)
  271. {
  272. if (Optimizations.UseAdvSimd)
  273. {
  274. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtmsGp);
  275. }
  276. else if (Optimizations.UseSse41)
  277. {
  278. EmitSse41Fcvts_Gp(context, FPRoundingMode.TowardsMinusInfinity, isFixed: false);
  279. }
  280. else
  281. {
  282. EmitFcvt_s_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Floor), op1));
  283. }
  284. }
  285. public static void Fcvtms_V(ArmEmitterContext context)
  286. {
  287. if (Optimizations.UseAdvSimd)
  288. {
  289. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64FcvtmsV);
  290. }
  291. else if (Optimizations.UseSse41)
  292. {
  293. EmitSse41FcvtsOpF(context, FPRoundingMode.TowardsMinusInfinity, scalar: false);
  294. }
  295. else
  296. {
  297. EmitFcvt(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Floor), op1), signed: true, scalar: false);
  298. }
  299. }
  300. public static void Fcvtmu_Gp(ArmEmitterContext context)
  301. {
  302. if (Optimizations.UseAdvSimd)
  303. {
  304. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtmuGp);
  305. }
  306. else if (Optimizations.UseSse41)
  307. {
  308. EmitSse41Fcvtu_Gp(context, FPRoundingMode.TowardsMinusInfinity, isFixed: false);
  309. }
  310. else
  311. {
  312. EmitFcvt_u_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Floor), op1));
  313. }
  314. }
  315. public static void Fcvtn_V(ArmEmitterContext context)
  316. {
  317. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  318. int sizeF = op.Size & 1;
  319. if (Optimizations.UseAdvSimd)
  320. {
  321. InstEmitSimdHelperArm64.EmitVectorBinaryOpFRd(context, Intrinsic.Arm64FcvtnV);
  322. }
  323. else if (Optimizations.UseSse2 && sizeF == 1)
  324. {
  325. Operand d = GetVec(op.Rd);
  326. Intrinsic movInst = op.RegisterSize == RegisterSize.Simd128 ? Intrinsic.X86Movlhps : Intrinsic.X86Movhlps;
  327. Operand nInt = context.AddIntrinsic(Intrinsic.X86Cvtpd2ps, GetVec(op.Rn));
  328. nInt = context.AddIntrinsic(Intrinsic.X86Movlhps, nInt, nInt);
  329. Operand res = context.VectorZeroUpper64(d);
  330. res = context.AddIntrinsic(movInst, res, nInt);
  331. context.Copy(d, res);
  332. }
  333. else if (Optimizations.UseF16c && sizeF == 0)
  334. {
  335. Debug.Assert(!Optimizations.ForceLegacySse);
  336. Operand d = GetVec(op.Rd);
  337. Operand n = GetVec(op.Rn);
  338. Intrinsic movInst = op.RegisterSize == RegisterSize.Simd128 ? Intrinsic.X86Movlhps : Intrinsic.X86Movhlps;
  339. Operand nInt = context.AddIntrinsic(Intrinsic.X86Vcvtps2ph, n, Const(X86GetRoundControl(FPRoundingMode.ToNearest)));
  340. nInt = context.AddIntrinsic(Intrinsic.X86Movlhps, nInt, nInt);
  341. Operand res = context.VectorZeroUpper64(d);
  342. res = context.AddIntrinsic(movInst, res, nInt);
  343. context.Copy(d, res);
  344. }
  345. else
  346. {
  347. OperandType type = sizeF == 0 ? OperandType.FP32 : OperandType.FP64;
  348. int elems = 4 >> sizeF;
  349. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  350. Operand d = GetVec(op.Rd);
  351. Operand res = part == 0 ? context.VectorZero() : context.Copy(d);
  352. for (int index = 0; index < elems; index++)
  353. {
  354. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  355. if (sizeF == 0)
  356. {
  357. context.StoreToContext();
  358. Operand e = context.Call(typeof(SoftFloat32_16).GetMethod(nameof(SoftFloat32_16.FPConvert)), ne);
  359. context.LoadFromContext();
  360. res = EmitVectorInsert(context, res, e, part + index, 1);
  361. }
  362. else /* if (sizeF == 1) */
  363. {
  364. Operand e = context.ConvertToFP(OperandType.FP32, ne);
  365. res = context.VectorInsert(res, e, part + index);
  366. }
  367. }
  368. context.Copy(d, res);
  369. }
  370. }
  371. public static void Fcvtns_Gp(ArmEmitterContext context)
  372. {
  373. if (Optimizations.UseAdvSimd)
  374. {
  375. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtnsGp);
  376. }
  377. else if (Optimizations.UseSse41)
  378. {
  379. EmitSse41Fcvts_Gp(context, FPRoundingMode.ToNearest, isFixed: false);
  380. }
  381. else
  382. {
  383. EmitFcvt_s_Gp(context, (op1) => EmitRoundMathCall(context, MidpointRounding.ToEven, op1));
  384. }
  385. }
  386. public static void Fcvtns_S(ArmEmitterContext context)
  387. {
  388. if (Optimizations.UseAdvSimd)
  389. {
  390. InstEmitSimdHelperArm64.EmitScalarUnaryOpF(context, Intrinsic.Arm64FcvtnsS);
  391. }
  392. else if (Optimizations.UseSse41)
  393. {
  394. EmitSse41FcvtsOpF(context, FPRoundingMode.ToNearest, scalar: true);
  395. }
  396. else
  397. {
  398. EmitFcvt(context, (op1) => EmitRoundMathCall(context, MidpointRounding.ToEven, op1), signed: true, scalar: true);
  399. }
  400. }
  401. public static void Fcvtns_V(ArmEmitterContext context)
  402. {
  403. if (Optimizations.UseAdvSimd)
  404. {
  405. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64FcvtnsV);
  406. }
  407. else if (Optimizations.UseSse41)
  408. {
  409. EmitSse41FcvtsOpF(context, FPRoundingMode.ToNearest, scalar: false);
  410. }
  411. else
  412. {
  413. EmitFcvt(context, (op1) => EmitRoundMathCall(context, MidpointRounding.ToEven, op1), signed: true, scalar: false);
  414. }
  415. }
  416. public static void Fcvtnu_S(ArmEmitterContext context)
  417. {
  418. if (Optimizations.UseAdvSimd)
  419. {
  420. InstEmitSimdHelperArm64.EmitScalarUnaryOpF(context, Intrinsic.Arm64FcvtnuS);
  421. }
  422. else if (Optimizations.UseSse41)
  423. {
  424. EmitSse41FcvtuOpF(context, FPRoundingMode.ToNearest, scalar: true);
  425. }
  426. else
  427. {
  428. EmitFcvt(context, (op1) => EmitRoundMathCall(context, MidpointRounding.ToEven, op1), signed: false, scalar: true);
  429. }
  430. }
  431. public static void Fcvtnu_V(ArmEmitterContext context)
  432. {
  433. if (Optimizations.UseAdvSimd)
  434. {
  435. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64FcvtnuV);
  436. }
  437. else if (Optimizations.UseSse41)
  438. {
  439. EmitSse41FcvtuOpF(context, FPRoundingMode.ToNearest, scalar: false);
  440. }
  441. else
  442. {
  443. EmitFcvt(context, (op1) => EmitRoundMathCall(context, MidpointRounding.ToEven, op1), signed: false, scalar: false);
  444. }
  445. }
  446. public static void Fcvtps_Gp(ArmEmitterContext context)
  447. {
  448. if (Optimizations.UseAdvSimd)
  449. {
  450. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtpsGp);
  451. }
  452. else if (Optimizations.UseSse41)
  453. {
  454. EmitSse41Fcvts_Gp(context, FPRoundingMode.TowardsPlusInfinity, isFixed: false);
  455. }
  456. else
  457. {
  458. EmitFcvt_s_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Ceiling), op1));
  459. }
  460. }
  461. public static void Fcvtpu_Gp(ArmEmitterContext context)
  462. {
  463. if (Optimizations.UseAdvSimd)
  464. {
  465. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtpuGp);
  466. }
  467. else if (Optimizations.UseSse41)
  468. {
  469. EmitSse41Fcvtu_Gp(context, FPRoundingMode.TowardsPlusInfinity, isFixed: false);
  470. }
  471. else
  472. {
  473. EmitFcvt_u_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Ceiling), op1));
  474. }
  475. }
  476. public static void Fcvtzs_Gp(ArmEmitterContext context)
  477. {
  478. if (Optimizations.UseAdvSimd)
  479. {
  480. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtzsGp);
  481. }
  482. else if (Optimizations.UseSse41)
  483. {
  484. EmitSse41Fcvts_Gp(context, FPRoundingMode.TowardsZero, isFixed: false);
  485. }
  486. else
  487. {
  488. EmitFcvt_s_Gp(context, (op1) => op1);
  489. }
  490. }
  491. public static void Fcvtzs_Gp_Fixed(ArmEmitterContext context)
  492. {
  493. if (Optimizations.UseAdvSimd)
  494. {
  495. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  496. InstEmitSimdHelperArm64.EmitScalarConvertBinaryOpFToGp(context, Intrinsic.Arm64FcvtzsGpFixed, op.FBits);
  497. }
  498. else if (Optimizations.UseSse41)
  499. {
  500. EmitSse41Fcvts_Gp(context, FPRoundingMode.TowardsZero, isFixed: true);
  501. }
  502. else
  503. {
  504. EmitFcvtzs_Gp_Fixed(context);
  505. }
  506. }
  507. public static void Fcvtzs_S(ArmEmitterContext context)
  508. {
  509. if (Optimizations.UseAdvSimd)
  510. {
  511. InstEmitSimdHelperArm64.EmitScalarUnaryOpF(context, Intrinsic.Arm64FcvtzsS);
  512. }
  513. else if (Optimizations.UseSse41)
  514. {
  515. EmitSse41FcvtsOpF(context, FPRoundingMode.TowardsZero, scalar: true);
  516. }
  517. else
  518. {
  519. EmitFcvtz(context, signed: true, scalar: true);
  520. }
  521. }
  522. public static void Fcvtzs_V(ArmEmitterContext context)
  523. {
  524. if (Optimizations.UseAdvSimd)
  525. {
  526. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64FcvtzsV);
  527. }
  528. else if (Optimizations.UseSse41)
  529. {
  530. EmitSse41FcvtsOpF(context, FPRoundingMode.TowardsZero, scalar: false);
  531. }
  532. else
  533. {
  534. EmitFcvtz(context, signed: true, scalar: false);
  535. }
  536. }
  537. public static void Fcvtzs_V_Fixed(ArmEmitterContext context)
  538. {
  539. if (Optimizations.UseAdvSimd)
  540. {
  541. InstEmitSimdHelperArm64.EmitVectorConvertBinaryOpF(context, Intrinsic.Arm64FcvtzsVFixed, GetFBits(context));
  542. }
  543. else if (Optimizations.UseSse41)
  544. {
  545. EmitSse41FcvtsOpF(context, FPRoundingMode.TowardsZero, scalar: false);
  546. }
  547. else
  548. {
  549. EmitFcvtz(context, signed: true, scalar: false);
  550. }
  551. }
  552. public static void Fcvtzu_Gp(ArmEmitterContext context)
  553. {
  554. if (Optimizations.UseAdvSimd)
  555. {
  556. InstEmitSimdHelperArm64.EmitScalarUnaryOpFToGp(context, Intrinsic.Arm64FcvtzuGp);
  557. }
  558. else if (Optimizations.UseSse41)
  559. {
  560. EmitSse41Fcvtu_Gp(context, FPRoundingMode.TowardsZero, isFixed: false);
  561. }
  562. else
  563. {
  564. EmitFcvt_u_Gp(context, (op1) => op1);
  565. }
  566. }
  567. public static void Fcvtzu_Gp_Fixed(ArmEmitterContext context)
  568. {
  569. if (Optimizations.UseAdvSimd)
  570. {
  571. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  572. InstEmitSimdHelperArm64.EmitScalarConvertBinaryOpFToGp(context, Intrinsic.Arm64FcvtzuGpFixed, op.FBits);
  573. }
  574. else if (Optimizations.UseSse41)
  575. {
  576. EmitSse41Fcvtu_Gp(context, FPRoundingMode.TowardsZero, isFixed: true);
  577. }
  578. else
  579. {
  580. EmitFcvtzu_Gp_Fixed(context);
  581. }
  582. }
  583. public static void Fcvtzu_S(ArmEmitterContext context)
  584. {
  585. if (Optimizations.UseAdvSimd)
  586. {
  587. InstEmitSimdHelperArm64.EmitScalarUnaryOpF(context, Intrinsic.Arm64FcvtzuS);
  588. }
  589. else if (Optimizations.UseSse41)
  590. {
  591. EmitSse41FcvtuOpF(context, FPRoundingMode.TowardsZero, scalar: true);
  592. }
  593. else
  594. {
  595. EmitFcvtz(context, signed: false, scalar: true);
  596. }
  597. }
  598. public static void Fcvtzu_V(ArmEmitterContext context)
  599. {
  600. if (Optimizations.UseAdvSimd)
  601. {
  602. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64FcvtzuV);
  603. }
  604. else if (Optimizations.UseSse41)
  605. {
  606. EmitSse41FcvtuOpF(context, FPRoundingMode.TowardsZero, scalar: false);
  607. }
  608. else
  609. {
  610. EmitFcvtz(context, signed: false, scalar: false);
  611. }
  612. }
  613. public static void Fcvtzu_V_Fixed(ArmEmitterContext context)
  614. {
  615. if (Optimizations.UseAdvSimd)
  616. {
  617. InstEmitSimdHelperArm64.EmitVectorConvertBinaryOpF(context, Intrinsic.Arm64FcvtzuVFixed, GetFBits(context));
  618. }
  619. else if (Optimizations.UseSse41)
  620. {
  621. EmitSse41FcvtuOpF(context, FPRoundingMode.TowardsZero, scalar: false);
  622. }
  623. else
  624. {
  625. EmitFcvtz(context, signed: false, scalar: false);
  626. }
  627. }
  628. public static void Scvtf_Gp(ArmEmitterContext context)
  629. {
  630. if (Optimizations.UseAdvSimd)
  631. {
  632. InstEmitSimdHelperArm64.EmitScalarUnaryOpFFromGp(context, Intrinsic.Arm64ScvtfGp);
  633. }
  634. else
  635. {
  636. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  637. Operand res = GetIntOrZR(context, op.Rn);
  638. if (op.RegisterSize == RegisterSize.Int32)
  639. {
  640. res = context.SignExtend32(OperandType.I64, res);
  641. }
  642. res = EmitFPConvert(context, res, op.Size, signed: true);
  643. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
  644. }
  645. }
  646. public static void Scvtf_Gp_Fixed(ArmEmitterContext context)
  647. {
  648. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  649. if (Optimizations.UseAdvSimd)
  650. {
  651. InstEmitSimdHelperArm64.EmitScalarConvertBinaryOpFFromGp(context, Intrinsic.Arm64ScvtfGpFixed, op.FBits);
  652. }
  653. else
  654. {
  655. Operand res = GetIntOrZR(context, op.Rn);
  656. if (op.RegisterSize == RegisterSize.Int32)
  657. {
  658. res = context.SignExtend32(OperandType.I64, res);
  659. }
  660. res = EmitFPConvert(context, res, op.Size, signed: true);
  661. res = EmitI2fFBitsMul(context, res, op.FBits);
  662. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
  663. }
  664. }
  665. public static void Scvtf_S(ArmEmitterContext context)
  666. {
  667. if (Optimizations.UseAdvSimd)
  668. {
  669. InstEmitSimdHelperArm64.EmitScalarUnaryOpF(context, Intrinsic.Arm64ScvtfS);
  670. }
  671. else if (Optimizations.UseSse2)
  672. {
  673. EmitSse2ScvtfOp(context, scalar: true);
  674. }
  675. else
  676. {
  677. EmitCvtf(context, signed: true, scalar: true);
  678. }
  679. }
  680. public static void Scvtf_S_Fixed(ArmEmitterContext context)
  681. {
  682. if (Optimizations.UseAdvSimd)
  683. {
  684. InstEmitSimdHelperArm64.EmitScalarConvertBinaryOpF(context, Intrinsic.Arm64ScvtfSFixed, GetFBits(context));
  685. }
  686. else if (Optimizations.UseSse2)
  687. {
  688. EmitSse2ScvtfOp(context, scalar: true);
  689. }
  690. else
  691. {
  692. EmitCvtf(context, signed: true, scalar: true);
  693. }
  694. }
  695. public static void Scvtf_V(ArmEmitterContext context)
  696. {
  697. if (Optimizations.UseAdvSimd)
  698. {
  699. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64ScvtfV);
  700. }
  701. else if (Optimizations.UseSse2)
  702. {
  703. EmitSse2ScvtfOp(context, scalar: false);
  704. }
  705. else
  706. {
  707. EmitCvtf(context, signed: true, scalar: false);
  708. }
  709. }
  710. public static void Scvtf_V_Fixed(ArmEmitterContext context)
  711. {
  712. if (Optimizations.UseAdvSimd)
  713. {
  714. InstEmitSimdHelperArm64.EmitVectorConvertBinaryOpF(context, Intrinsic.Arm64ScvtfVFixed, GetFBits(context));
  715. }
  716. else if (Optimizations.UseSse2)
  717. {
  718. EmitSse2ScvtfOp(context, scalar: false);
  719. }
  720. else
  721. {
  722. EmitCvtf(context, signed: true, scalar: false);
  723. }
  724. }
  725. public static void Ucvtf_Gp(ArmEmitterContext context)
  726. {
  727. if (Optimizations.UseAdvSimd)
  728. {
  729. InstEmitSimdHelperArm64.EmitScalarUnaryOpFFromGp(context, Intrinsic.Arm64UcvtfGp);
  730. }
  731. else
  732. {
  733. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  734. Operand res = GetIntOrZR(context, op.Rn);
  735. res = EmitFPConvert(context, res, op.Size, signed: false);
  736. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
  737. }
  738. }
  739. public static void Ucvtf_Gp_Fixed(ArmEmitterContext context)
  740. {
  741. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  742. if (Optimizations.UseAdvSimd)
  743. {
  744. InstEmitSimdHelperArm64.EmitScalarConvertBinaryOpFFromGp(context, Intrinsic.Arm64UcvtfGpFixed, op.FBits);
  745. }
  746. else
  747. {
  748. Operand res = GetIntOrZR(context, op.Rn);
  749. res = EmitFPConvert(context, res, op.Size, signed: false);
  750. res = EmitI2fFBitsMul(context, res, op.FBits);
  751. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
  752. }
  753. }
  754. public static void Ucvtf_S(ArmEmitterContext context)
  755. {
  756. if (Optimizations.UseAdvSimd)
  757. {
  758. InstEmitSimdHelperArm64.EmitScalarUnaryOpF(context, Intrinsic.Arm64UcvtfS);
  759. }
  760. else if (Optimizations.UseSse2)
  761. {
  762. EmitSse2UcvtfOp(context, scalar: true);
  763. }
  764. else
  765. {
  766. EmitCvtf(context, signed: false, scalar: true);
  767. }
  768. }
  769. public static void Ucvtf_S_Fixed(ArmEmitterContext context)
  770. {
  771. if (Optimizations.UseAdvSimd)
  772. {
  773. InstEmitSimdHelperArm64.EmitScalarConvertBinaryOpF(context, Intrinsic.Arm64UcvtfSFixed, GetFBits(context));
  774. }
  775. else if (Optimizations.UseSse2)
  776. {
  777. EmitSse2UcvtfOp(context, scalar: true);
  778. }
  779. else
  780. {
  781. EmitCvtf(context, signed: false, scalar: true);
  782. }
  783. }
  784. public static void Ucvtf_V(ArmEmitterContext context)
  785. {
  786. if (Optimizations.UseAdvSimd)
  787. {
  788. InstEmitSimdHelperArm64.EmitVectorUnaryOpF(context, Intrinsic.Arm64UcvtfV);
  789. }
  790. else if (Optimizations.UseSse2)
  791. {
  792. EmitSse2UcvtfOp(context, scalar: false);
  793. }
  794. else
  795. {
  796. EmitCvtf(context, signed: false, scalar: false);
  797. }
  798. }
  799. public static void Ucvtf_V_Fixed(ArmEmitterContext context)
  800. {
  801. if (Optimizations.UseAdvSimd)
  802. {
  803. InstEmitSimdHelperArm64.EmitVectorConvertBinaryOpF(context, Intrinsic.Arm64UcvtfVFixed, GetFBits(context));
  804. }
  805. else if (Optimizations.UseSse2)
  806. {
  807. EmitSse2UcvtfOp(context, scalar: false);
  808. }
  809. else
  810. {
  811. EmitCvtf(context, signed: false, scalar: false);
  812. }
  813. }
  814. private static void EmitFcvt(ArmEmitterContext context, Func1I emit, bool signed, bool scalar)
  815. {
  816. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  817. Operand res = context.VectorZero();
  818. Operand n = GetVec(op.Rn);
  819. int sizeF = op.Size & 1;
  820. int sizeI = sizeF + 2;
  821. OperandType type = sizeF == 0 ? OperandType.FP32 : OperandType.FP64;
  822. int elems = !scalar ? op.GetBytesCount() >> sizeI : 1;
  823. for (int index = 0; index < elems; index++)
  824. {
  825. Operand ne = context.VectorExtract(type, n, index);
  826. Operand e = emit(ne);
  827. if (sizeF == 0)
  828. {
  829. MethodInfo info = signed
  830. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToS32))
  831. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToU32));
  832. e = context.Call(info, e);
  833. e = context.ZeroExtend32(OperandType.I64, e);
  834. }
  835. else /* if (sizeF == 1) */
  836. {
  837. MethodInfo info = signed
  838. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToS64))
  839. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToU64));
  840. e = context.Call(info, e);
  841. }
  842. res = EmitVectorInsert(context, res, e, index, sizeI);
  843. }
  844. context.Copy(GetVec(op.Rd), res);
  845. }
  846. private static void EmitFcvtz(ArmEmitterContext context, bool signed, bool scalar)
  847. {
  848. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  849. Operand res = context.VectorZero();
  850. Operand n = GetVec(op.Rn);
  851. int sizeF = op.Size & 1;
  852. int sizeI = sizeF + 2;
  853. OperandType type = sizeF == 0 ? OperandType.FP32 : OperandType.FP64;
  854. int fBits = GetFBits(context);
  855. int elems = !scalar ? op.GetBytesCount() >> sizeI : 1;
  856. for (int index = 0; index < elems; index++)
  857. {
  858. Operand ne = context.VectorExtract(type, n, index);
  859. Operand e = EmitF2iFBitsMul(context, ne, fBits);
  860. if (sizeF == 0)
  861. {
  862. MethodInfo info = signed
  863. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToS32))
  864. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToU32));
  865. e = context.Call(info, e);
  866. e = context.ZeroExtend32(OperandType.I64, e);
  867. }
  868. else /* if (sizeF == 1) */
  869. {
  870. MethodInfo info = signed
  871. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToS64))
  872. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToU64));
  873. e = context.Call(info, e);
  874. }
  875. res = EmitVectorInsert(context, res, e, index, sizeI);
  876. }
  877. context.Copy(GetVec(op.Rd), res);
  878. }
  879. private static void EmitFcvt_s_Gp(ArmEmitterContext context, Func1I emit)
  880. {
  881. EmitFcvt___Gp(context, emit, signed: true);
  882. }
  883. private static void EmitFcvt_u_Gp(ArmEmitterContext context, Func1I emit)
  884. {
  885. EmitFcvt___Gp(context, emit, signed: false);
  886. }
  887. private static void EmitFcvt___Gp(ArmEmitterContext context, Func1I emit, bool signed)
  888. {
  889. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  890. OperandType type = op.Size == 0 ? OperandType.FP32 : OperandType.FP64;
  891. Operand ne = context.VectorExtract(type, GetVec(op.Rn), 0);
  892. Operand res = signed
  893. ? EmitScalarFcvts(context, emit(ne), 0)
  894. : EmitScalarFcvtu(context, emit(ne), 0);
  895. SetIntOrZR(context, op.Rd, res);
  896. }
  897. private static void EmitFcvtzs_Gp_Fixed(ArmEmitterContext context)
  898. {
  899. EmitFcvtz__Gp_Fixed(context, signed: true);
  900. }
  901. private static void EmitFcvtzu_Gp_Fixed(ArmEmitterContext context)
  902. {
  903. EmitFcvtz__Gp_Fixed(context, signed: false);
  904. }
  905. private static void EmitFcvtz__Gp_Fixed(ArmEmitterContext context, bool signed)
  906. {
  907. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  908. OperandType type = op.Size == 0 ? OperandType.FP32 : OperandType.FP64;
  909. Operand ne = context.VectorExtract(type, GetVec(op.Rn), 0);
  910. Operand res = signed
  911. ? EmitScalarFcvts(context, ne, op.FBits)
  912. : EmitScalarFcvtu(context, ne, op.FBits);
  913. SetIntOrZR(context, op.Rd, res);
  914. }
  915. private static void EmitCvtf(ArmEmitterContext context, bool signed, bool scalar)
  916. {
  917. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  918. Operand res = context.VectorZero();
  919. int sizeF = op.Size & 1;
  920. int sizeI = sizeF + 2;
  921. int fBits = GetFBits(context);
  922. int elems = !scalar ? op.GetBytesCount() >> sizeI : 1;
  923. for (int index = 0; index < elems; index++)
  924. {
  925. Operand ne = EmitVectorLongExtract(context, op.Rn, index, sizeI);
  926. Operand e = EmitFPConvert(context, ne, sizeF, signed);
  927. e = EmitI2fFBitsMul(context, e, fBits);
  928. res = context.VectorInsert(res, e, index);
  929. }
  930. context.Copy(GetVec(op.Rd), res);
  931. }
  932. private static int GetFBits(ArmEmitterContext context)
  933. {
  934. if (context.CurrOp is OpCodeSimdShImm op)
  935. {
  936. return GetImmShr(op);
  937. }
  938. return 0;
  939. }
  940. private static Operand EmitFPConvert(ArmEmitterContext context, Operand value, int size, bool signed)
  941. {
  942. Debug.Assert(value.Type == OperandType.I32 || value.Type == OperandType.I64);
  943. Debug.Assert((uint)size < 2);
  944. OperandType type = size == 0 ? OperandType.FP32 : OperandType.FP64;
  945. if (signed)
  946. {
  947. return context.ConvertToFP(type, value);
  948. }
  949. else
  950. {
  951. return context.ConvertToFPUI(type, value);
  952. }
  953. }
  954. private static Operand EmitScalarFcvts(ArmEmitterContext context, Operand value, int fBits)
  955. {
  956. Debug.Assert(value.Type == OperandType.FP32 || value.Type == OperandType.FP64);
  957. value = EmitF2iFBitsMul(context, value, fBits);
  958. MethodInfo info;
  959. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  960. {
  961. info = value.Type == OperandType.FP32
  962. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToS32))
  963. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToS32));
  964. }
  965. else
  966. {
  967. info = value.Type == OperandType.FP32
  968. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToS64))
  969. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToS64));
  970. }
  971. return context.Call(info, value);
  972. }
  973. private static Operand EmitScalarFcvtu(ArmEmitterContext context, Operand value, int fBits)
  974. {
  975. Debug.Assert(value.Type == OperandType.FP32 || value.Type == OperandType.FP64);
  976. value = EmitF2iFBitsMul(context, value, fBits);
  977. MethodInfo info;
  978. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  979. {
  980. info = value.Type == OperandType.FP32
  981. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToU32))
  982. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToU32));
  983. }
  984. else
  985. {
  986. info = value.Type == OperandType.FP32
  987. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF32ToU64))
  988. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.SatF64ToU64));
  989. }
  990. return context.Call(info, value);
  991. }
  992. private static Operand EmitF2iFBitsMul(ArmEmitterContext context, Operand value, int fBits)
  993. {
  994. Debug.Assert(value.Type == OperandType.FP32 || value.Type == OperandType.FP64);
  995. if (fBits == 0)
  996. {
  997. return value;
  998. }
  999. if (value.Type == OperandType.FP32)
  1000. {
  1001. return context.Multiply(value, ConstF(MathF.Pow(2f, fBits)));
  1002. }
  1003. else /* if (value.Type == OperandType.FP64) */
  1004. {
  1005. return context.Multiply(value, ConstF(Math.Pow(2d, fBits)));
  1006. }
  1007. }
  1008. private static Operand EmitI2fFBitsMul(ArmEmitterContext context, Operand value, int fBits)
  1009. {
  1010. Debug.Assert(value.Type == OperandType.FP32 || value.Type == OperandType.FP64);
  1011. if (fBits == 0)
  1012. {
  1013. return value;
  1014. }
  1015. if (value.Type == OperandType.FP32)
  1016. {
  1017. return context.Multiply(value, ConstF(1f / MathF.Pow(2f, fBits)));
  1018. }
  1019. else /* if (value.Type == OperandType.FP64) */
  1020. {
  1021. return context.Multiply(value, ConstF(1d / Math.Pow(2d, fBits)));
  1022. }
  1023. }
  1024. public static Operand EmitSse2CvtDoubleToInt64OpF(ArmEmitterContext context, Operand opF, bool scalar)
  1025. {
  1026. Debug.Assert(opF.Type == OperandType.V128);
  1027. Operand longL = context.AddIntrinsicLong (Intrinsic.X86Cvtsd2si, opF); // opFL
  1028. Operand res = context.VectorCreateScalar(longL);
  1029. if (!scalar)
  1030. {
  1031. Operand opFH = context.AddIntrinsic (Intrinsic.X86Movhlps, res, opF); // res doesn't matter.
  1032. Operand longH = context.AddIntrinsicLong (Intrinsic.X86Cvtsd2si, opFH);
  1033. Operand resH = context.VectorCreateScalar(longH);
  1034. res = context.AddIntrinsic (Intrinsic.X86Movlhps, res, resH);
  1035. }
  1036. return res;
  1037. }
  1038. private static Operand EmitSse2CvtInt64ToDoubleOp(ArmEmitterContext context, Operand op, bool scalar)
  1039. {
  1040. Debug.Assert(op.Type == OperandType.V128);
  1041. Operand longL = context.AddIntrinsicLong(Intrinsic.X86Cvtsi2si, op); // opL
  1042. Operand res = context.AddIntrinsic (Intrinsic.X86Cvtsi2sd, context.VectorZero(), longL);
  1043. if (!scalar)
  1044. {
  1045. Operand opH = context.AddIntrinsic (Intrinsic.X86Movhlps, res, op); // res doesn't matter.
  1046. Operand longH = context.AddIntrinsicLong(Intrinsic.X86Cvtsi2si, opH);
  1047. Operand resH = context.AddIntrinsic (Intrinsic.X86Cvtsi2sd, res, longH); // res doesn't matter.
  1048. res = context.AddIntrinsic (Intrinsic.X86Movlhps, res, resH);
  1049. }
  1050. return res;
  1051. }
  1052. private static void EmitSse2ScvtfOp(ArmEmitterContext context, bool scalar)
  1053. {
  1054. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1055. Operand n = GetVec(op.Rn);
  1056. // sizeF == ((OpCodeSimdShImm)op).Size - 2
  1057. int sizeF = op.Size & 1;
  1058. if (sizeF == 0)
  1059. {
  1060. Operand res = context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, n);
  1061. if (op is OpCodeSimdShImm fixedOp)
  1062. {
  1063. int fBits = GetImmShr(fixedOp);
  1064. // BitConverter.Int32BitsToSingle(fpScaled) == 1f / MathF.Pow(2f, fBits)
  1065. int fpScaled = 0x3F800000 - fBits * 0x800000;
  1066. Operand fpScaledMask = scalar
  1067. ? X86GetScalar (context, fpScaled)
  1068. : X86GetAllElements(context, fpScaled);
  1069. res = context.AddIntrinsic(Intrinsic.X86Mulps, res, fpScaledMask);
  1070. }
  1071. if (scalar)
  1072. {
  1073. res = context.VectorZeroUpper96(res);
  1074. }
  1075. else if (op.RegisterSize == RegisterSize.Simd64)
  1076. {
  1077. res = context.VectorZeroUpper64(res);
  1078. }
  1079. context.Copy(GetVec(op.Rd), res);
  1080. }
  1081. else /* if (sizeF == 1) */
  1082. {
  1083. Operand res = EmitSse2CvtInt64ToDoubleOp(context, n, scalar);
  1084. if (op is OpCodeSimdShImm fixedOp)
  1085. {
  1086. int fBits = GetImmShr(fixedOp);
  1087. // BitConverter.Int64BitsToDouble(fpScaled) == 1d / Math.Pow(2d, fBits)
  1088. long fpScaled = 0x3FF0000000000000L - fBits * 0x10000000000000L;
  1089. Operand fpScaledMask = scalar
  1090. ? X86GetScalar (context, fpScaled)
  1091. : X86GetAllElements(context, fpScaled);
  1092. res = context.AddIntrinsic(Intrinsic.X86Mulpd, res, fpScaledMask);
  1093. }
  1094. if (scalar)
  1095. {
  1096. res = context.VectorZeroUpper64(res);
  1097. }
  1098. context.Copy(GetVec(op.Rd), res);
  1099. }
  1100. }
  1101. private static void EmitSse2UcvtfOp(ArmEmitterContext context, bool scalar)
  1102. {
  1103. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1104. Operand n = GetVec(op.Rn);
  1105. // sizeF == ((OpCodeSimdShImm)op).Size - 2
  1106. int sizeF = op.Size & 1;
  1107. if (sizeF == 0)
  1108. {
  1109. Operand mask = scalar // 65536.000f (1 << 16)
  1110. ? X86GetScalar (context, 0x47800000)
  1111. : X86GetAllElements(context, 0x47800000);
  1112. Operand res = context.AddIntrinsic(Intrinsic.X86Psrld, n, Const(16));
  1113. res = context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, res);
  1114. res = context.AddIntrinsic(Intrinsic.X86Mulps, res, mask);
  1115. Operand res2 = context.AddIntrinsic(Intrinsic.X86Pslld, n, Const(16));
  1116. res2 = context.AddIntrinsic(Intrinsic.X86Psrld, res2, Const(16));
  1117. res2 = context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, res2);
  1118. res = context.AddIntrinsic(Intrinsic.X86Addps, res, res2);
  1119. if (op is OpCodeSimdShImm fixedOp)
  1120. {
  1121. int fBits = GetImmShr(fixedOp);
  1122. // BitConverter.Int32BitsToSingle(fpScaled) == 1f / MathF.Pow(2f, fBits)
  1123. int fpScaled = 0x3F800000 - fBits * 0x800000;
  1124. Operand fpScaledMask = scalar
  1125. ? X86GetScalar (context, fpScaled)
  1126. : X86GetAllElements(context, fpScaled);
  1127. res = context.AddIntrinsic(Intrinsic.X86Mulps, res, fpScaledMask);
  1128. }
  1129. if (scalar)
  1130. {
  1131. res = context.VectorZeroUpper96(res);
  1132. }
  1133. else if (op.RegisterSize == RegisterSize.Simd64)
  1134. {
  1135. res = context.VectorZeroUpper64(res);
  1136. }
  1137. context.Copy(GetVec(op.Rd), res);
  1138. }
  1139. else /* if (sizeF == 1) */
  1140. {
  1141. Operand mask = scalar // 4294967296.0000000d (1L << 32)
  1142. ? X86GetScalar (context, 0x41F0000000000000L)
  1143. : X86GetAllElements(context, 0x41F0000000000000L);
  1144. Operand res = context.AddIntrinsic (Intrinsic.X86Psrlq, n, Const(32));
  1145. res = EmitSse2CvtInt64ToDoubleOp(context, res, scalar);
  1146. res = context.AddIntrinsic (Intrinsic.X86Mulpd, res, mask);
  1147. Operand res2 = context.AddIntrinsic (Intrinsic.X86Psllq, n, Const(32));
  1148. res2 = context.AddIntrinsic (Intrinsic.X86Psrlq, res2, Const(32));
  1149. res2 = EmitSse2CvtInt64ToDoubleOp(context, res2, scalar);
  1150. res = context.AddIntrinsic(Intrinsic.X86Addpd, res, res2);
  1151. if (op is OpCodeSimdShImm fixedOp)
  1152. {
  1153. int fBits = GetImmShr(fixedOp);
  1154. // BitConverter.Int64BitsToDouble(fpScaled) == 1d / Math.Pow(2d, fBits)
  1155. long fpScaled = 0x3FF0000000000000L - fBits * 0x10000000000000L;
  1156. Operand fpScaledMask = scalar
  1157. ? X86GetScalar (context, fpScaled)
  1158. : X86GetAllElements(context, fpScaled);
  1159. res = context.AddIntrinsic(Intrinsic.X86Mulpd, res, fpScaledMask);
  1160. }
  1161. if (scalar)
  1162. {
  1163. res = context.VectorZeroUpper64(res);
  1164. }
  1165. context.Copy(GetVec(op.Rd), res);
  1166. }
  1167. }
  1168. private static void EmitSse41FcvtsOpF(ArmEmitterContext context, FPRoundingMode roundMode, bool scalar)
  1169. {
  1170. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1171. Operand n = GetVec(op.Rn);
  1172. // sizeF == ((OpCodeSimdShImm)op).Size - 2
  1173. int sizeF = op.Size & 1;
  1174. if (sizeF == 0)
  1175. {
  1176. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpps, n, n, Const((int)CmpCondition.OrderedQ));
  1177. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  1178. if (op is OpCodeSimdShImm fixedOp)
  1179. {
  1180. int fBits = GetImmShr(fixedOp);
  1181. // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
  1182. int fpScaled = 0x3F800000 + fBits * 0x800000;
  1183. Operand fpScaledMask = scalar
  1184. ? X86GetScalar (context, fpScaled)
  1185. : X86GetAllElements(context, fpScaled);
  1186. nRes = context.AddIntrinsic(Intrinsic.X86Mulps, nRes, fpScaledMask);
  1187. }
  1188. if (roundMode != FPRoundingMode.ToNearestAway)
  1189. {
  1190. nRes = context.AddIntrinsic(Intrinsic.X86Roundps, nRes, Const(X86GetRoundControl(roundMode)));
  1191. }
  1192. else
  1193. {
  1194. nRes = EmitSse41RoundToNearestWithTiesToAwayOpF(context, nRes, scalar);
  1195. }
  1196. Operand nInt = context.AddIntrinsic(Intrinsic.X86Cvtps2dq, nRes);
  1197. Operand fpMaxValMask = scalar // 2.14748365E9f (2147483648)
  1198. ? X86GetScalar (context, 0x4F000000)
  1199. : X86GetAllElements(context, 0x4F000000);
  1200. nRes = context.AddIntrinsic(Intrinsic.X86Cmpps, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  1201. Operand dRes = context.AddIntrinsic(Intrinsic.X86Pxor, nInt, nRes);
  1202. if (scalar)
  1203. {
  1204. dRes = context.VectorZeroUpper96(dRes);
  1205. }
  1206. else if (op.RegisterSize == RegisterSize.Simd64)
  1207. {
  1208. dRes = context.VectorZeroUpper64(dRes);
  1209. }
  1210. context.Copy(GetVec(op.Rd), dRes);
  1211. }
  1212. else /* if (sizeF == 1) */
  1213. {
  1214. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmppd, n, n, Const((int)CmpCondition.OrderedQ));
  1215. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  1216. if (op is OpCodeSimdShImm fixedOp)
  1217. {
  1218. int fBits = GetImmShr(fixedOp);
  1219. // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
  1220. long fpScaled = 0x3FF0000000000000L + fBits * 0x10000000000000L;
  1221. Operand fpScaledMask = scalar
  1222. ? X86GetScalar (context, fpScaled)
  1223. : X86GetAllElements(context, fpScaled);
  1224. nRes = context.AddIntrinsic(Intrinsic.X86Mulpd, nRes, fpScaledMask);
  1225. }
  1226. if (roundMode != FPRoundingMode.ToNearestAway)
  1227. {
  1228. nRes = context.AddIntrinsic(Intrinsic.X86Roundpd, nRes, Const(X86GetRoundControl(roundMode)));
  1229. }
  1230. else
  1231. {
  1232. nRes = EmitSse41RoundToNearestWithTiesToAwayOpF(context, nRes, scalar);
  1233. }
  1234. Operand nLong = EmitSse2CvtDoubleToInt64OpF(context, nRes, scalar);
  1235. Operand fpMaxValMask = scalar // 9.2233720368547760E18d (9223372036854775808)
  1236. ? X86GetScalar (context, 0x43E0000000000000L)
  1237. : X86GetAllElements(context, 0x43E0000000000000L);
  1238. nRes = context.AddIntrinsic(Intrinsic.X86Cmppd, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  1239. Operand dRes = context.AddIntrinsic(Intrinsic.X86Pxor, nLong, nRes);
  1240. if (scalar)
  1241. {
  1242. dRes = context.VectorZeroUpper64(dRes);
  1243. }
  1244. context.Copy(GetVec(op.Rd), dRes);
  1245. }
  1246. }
  1247. private static void EmitSse41FcvtuOpF(ArmEmitterContext context, FPRoundingMode roundMode, bool scalar)
  1248. {
  1249. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1250. Operand n = GetVec(op.Rn);
  1251. // sizeF == ((OpCodeSimdShImm)op).Size - 2
  1252. int sizeF = op.Size & 1;
  1253. if (sizeF == 0)
  1254. {
  1255. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpps, n, n, Const((int)CmpCondition.OrderedQ));
  1256. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  1257. if (op is OpCodeSimdShImm fixedOp)
  1258. {
  1259. int fBits = GetImmShr(fixedOp);
  1260. // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
  1261. int fpScaled = 0x3F800000 + fBits * 0x800000;
  1262. Operand fpScaledMask = scalar
  1263. ? X86GetScalar (context, fpScaled)
  1264. : X86GetAllElements(context, fpScaled);
  1265. nRes = context.AddIntrinsic(Intrinsic.X86Mulps, nRes, fpScaledMask);
  1266. }
  1267. if (roundMode != FPRoundingMode.ToNearestAway)
  1268. {
  1269. nRes = context.AddIntrinsic(Intrinsic.X86Roundps, nRes, Const(X86GetRoundControl(roundMode)));
  1270. }
  1271. else
  1272. {
  1273. nRes = EmitSse41RoundToNearestWithTiesToAwayOpF(context, nRes, scalar);
  1274. }
  1275. Operand zero = context.VectorZero();
  1276. Operand nCmp = context.AddIntrinsic(Intrinsic.X86Cmpps, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  1277. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  1278. Operand fpMaxValMask = scalar // 2.14748365E9f (2147483648)
  1279. ? X86GetScalar (context, 0x4F000000)
  1280. : X86GetAllElements(context, 0x4F000000);
  1281. Operand nInt = context.AddIntrinsic(Intrinsic.X86Cvtps2dq, nRes);
  1282. nRes = context.AddIntrinsic(Intrinsic.X86Subps, nRes, fpMaxValMask);
  1283. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpps, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  1284. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  1285. Operand nInt2 = context.AddIntrinsic(Intrinsic.X86Cvtps2dq, nRes);
  1286. nRes = context.AddIntrinsic(Intrinsic.X86Cmpps, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  1287. Operand dRes = context.AddIntrinsic(Intrinsic.X86Pxor, nInt2, nRes);
  1288. dRes = context.AddIntrinsic(Intrinsic.X86Paddd, dRes, nInt);
  1289. if (scalar)
  1290. {
  1291. dRes = context.VectorZeroUpper96(dRes);
  1292. }
  1293. else if (op.RegisterSize == RegisterSize.Simd64)
  1294. {
  1295. dRes = context.VectorZeroUpper64(dRes);
  1296. }
  1297. context.Copy(GetVec(op.Rd), dRes);
  1298. }
  1299. else /* if (sizeF == 1) */
  1300. {
  1301. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmppd, n, n, Const((int)CmpCondition.OrderedQ));
  1302. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  1303. if (op is OpCodeSimdShImm fixedOp)
  1304. {
  1305. int fBits = GetImmShr(fixedOp);
  1306. // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
  1307. long fpScaled = 0x3FF0000000000000L + fBits * 0x10000000000000L;
  1308. Operand fpScaledMask = scalar
  1309. ? X86GetScalar (context, fpScaled)
  1310. : X86GetAllElements(context, fpScaled);
  1311. nRes = context.AddIntrinsic(Intrinsic.X86Mulpd, nRes, fpScaledMask);
  1312. }
  1313. if (roundMode != FPRoundingMode.ToNearestAway)
  1314. {
  1315. nRes = context.AddIntrinsic(Intrinsic.X86Roundpd, nRes, Const(X86GetRoundControl(roundMode)));
  1316. }
  1317. else
  1318. {
  1319. nRes = EmitSse41RoundToNearestWithTiesToAwayOpF(context, nRes, scalar);
  1320. }
  1321. Operand zero = context.VectorZero();
  1322. Operand nCmp = context.AddIntrinsic(Intrinsic.X86Cmppd, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  1323. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  1324. Operand fpMaxValMask = scalar // 9.2233720368547760E18d (9223372036854775808)
  1325. ? X86GetScalar (context, 0x43E0000000000000L)
  1326. : X86GetAllElements(context, 0x43E0000000000000L);
  1327. Operand nLong = EmitSse2CvtDoubleToInt64OpF(context, nRes, scalar);
  1328. nRes = context.AddIntrinsic(Intrinsic.X86Subpd, nRes, fpMaxValMask);
  1329. nCmp = context.AddIntrinsic(Intrinsic.X86Cmppd, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  1330. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  1331. Operand nLong2 = EmitSse2CvtDoubleToInt64OpF(context, nRes, scalar);
  1332. nRes = context.AddIntrinsic(Intrinsic.X86Cmppd, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  1333. Operand dRes = context.AddIntrinsic(Intrinsic.X86Pxor, nLong2, nRes);
  1334. dRes = context.AddIntrinsic(Intrinsic.X86Paddq, dRes, nLong);
  1335. if (scalar)
  1336. {
  1337. dRes = context.VectorZeroUpper64(dRes);
  1338. }
  1339. context.Copy(GetVec(op.Rd), dRes);
  1340. }
  1341. }
  1342. private static void EmitSse41Fcvts_Gp(ArmEmitterContext context, FPRoundingMode roundMode, bool isFixed)
  1343. {
  1344. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  1345. Operand n = GetVec(op.Rn);
  1346. if (op.Size == 0)
  1347. {
  1348. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpss, n, n, Const((int)CmpCondition.OrderedQ));
  1349. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  1350. if (isFixed)
  1351. {
  1352. // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, op.FBits)
  1353. int fpScaled = 0x3F800000 + op.FBits * 0x800000;
  1354. Operand fpScaledMask = X86GetScalar(context, fpScaled);
  1355. nRes = context.AddIntrinsic(Intrinsic.X86Mulss, nRes, fpScaledMask);
  1356. }
  1357. if (roundMode != FPRoundingMode.ToNearestAway)
  1358. {
  1359. nRes = context.AddIntrinsic(Intrinsic.X86Roundss, nRes, Const(X86GetRoundControl(roundMode)));
  1360. }
  1361. else
  1362. {
  1363. nRes = EmitSse41RoundToNearestWithTiesToAwayOpF(context, nRes, scalar: true);
  1364. }
  1365. Operand nIntOrLong = op.RegisterSize == RegisterSize.Int32
  1366. ? context.AddIntrinsicInt (Intrinsic.X86Cvtss2si, nRes)
  1367. : context.AddIntrinsicLong(Intrinsic.X86Cvtss2si, nRes);
  1368. int fpMaxVal = op.RegisterSize == RegisterSize.Int32
  1369. ? 0x4F000000 // 2.14748365E9f (2147483648)
  1370. : 0x5F000000; // 9.223372E18f (9223372036854775808)
  1371. Operand fpMaxValMask = X86GetScalar(context, fpMaxVal);
  1372. nRes = context.AddIntrinsic(Intrinsic.X86Cmpss, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  1373. Operand nInt = context.AddIntrinsicInt(Intrinsic.X86Cvtsi2si, nRes);
  1374. if (op.RegisterSize == RegisterSize.Int64)
  1375. {
  1376. nInt = context.SignExtend32(OperandType.I64, nInt);
  1377. }
  1378. Operand dRes = context.BitwiseExclusiveOr(nIntOrLong, nInt);
  1379. SetIntOrZR(context, op.Rd, dRes);
  1380. }
  1381. else /* if (op.Size == 1) */
  1382. {
  1383. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpsd, n, n, Const((int)CmpCondition.OrderedQ));
  1384. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  1385. if (isFixed)
  1386. {
  1387. // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, op.FBits)
  1388. long fpScaled = 0x3FF0000000000000L + op.FBits * 0x10000000000000L;
  1389. Operand fpScaledMask = X86GetScalar(context, fpScaled);
  1390. nRes = context.AddIntrinsic(Intrinsic.X86Mulsd, nRes, fpScaledMask);
  1391. }
  1392. if (roundMode != FPRoundingMode.ToNearestAway)
  1393. {
  1394. nRes = context.AddIntrinsic(Intrinsic.X86Roundsd, nRes, Const(X86GetRoundControl(roundMode)));
  1395. }
  1396. else
  1397. {
  1398. nRes = EmitSse41RoundToNearestWithTiesToAwayOpF(context, nRes, scalar: true);
  1399. }
  1400. Operand nIntOrLong = op.RegisterSize == RegisterSize.Int32
  1401. ? context.AddIntrinsicInt (Intrinsic.X86Cvtsd2si, nRes)
  1402. : context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, nRes);
  1403. long fpMaxVal = op.RegisterSize == RegisterSize.Int32
  1404. ? 0x41E0000000000000L // 2147483648.0000000d (2147483648)
  1405. : 0x43E0000000000000L; // 9.2233720368547760E18d (9223372036854775808)
  1406. Operand fpMaxValMask = X86GetScalar(context, fpMaxVal);
  1407. nRes = context.AddIntrinsic(Intrinsic.X86Cmpsd, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  1408. Operand nLong = context.AddIntrinsicLong(Intrinsic.X86Cvtsi2si, nRes);
  1409. if (op.RegisterSize == RegisterSize.Int32)
  1410. {
  1411. nLong = context.ConvertI64ToI32(nLong);
  1412. }
  1413. Operand dRes = context.BitwiseExclusiveOr(nIntOrLong, nLong);
  1414. SetIntOrZR(context, op.Rd, dRes);
  1415. }
  1416. }
  1417. private static void EmitSse41Fcvtu_Gp(ArmEmitterContext context, FPRoundingMode roundMode, bool isFixed)
  1418. {
  1419. OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
  1420. Operand n = GetVec(op.Rn);
  1421. if (op.Size == 0)
  1422. {
  1423. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpss, n, n, Const((int)CmpCondition.OrderedQ));
  1424. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  1425. if (isFixed)
  1426. {
  1427. // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, op.FBits)
  1428. int fpScaled = 0x3F800000 + op.FBits * 0x800000;
  1429. Operand fpScaledMask = X86GetScalar(context, fpScaled);
  1430. nRes = context.AddIntrinsic(Intrinsic.X86Mulss, nRes, fpScaledMask);
  1431. }
  1432. if (roundMode != FPRoundingMode.ToNearestAway)
  1433. {
  1434. nRes = context.AddIntrinsic(Intrinsic.X86Roundss, nRes, Const(X86GetRoundControl(roundMode)));
  1435. }
  1436. else
  1437. {
  1438. nRes = EmitSse41RoundToNearestWithTiesToAwayOpF(context, nRes, scalar: true);
  1439. }
  1440. Operand zero = context.VectorZero();
  1441. Operand nCmp = context.AddIntrinsic(Intrinsic.X86Cmpss, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  1442. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  1443. int fpMaxVal = op.RegisterSize == RegisterSize.Int32
  1444. ? 0x4F000000 // 2.14748365E9f (2147483648)
  1445. : 0x5F000000; // 9.223372E18f (9223372036854775808)
  1446. Operand fpMaxValMask = X86GetScalar(context, fpMaxVal);
  1447. Operand nIntOrLong = op.RegisterSize == RegisterSize.Int32
  1448. ? context.AddIntrinsicInt (Intrinsic.X86Cvtss2si, nRes)
  1449. : context.AddIntrinsicLong(Intrinsic.X86Cvtss2si, nRes);
  1450. nRes = context.AddIntrinsic(Intrinsic.X86Subss, nRes, fpMaxValMask);
  1451. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpss, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  1452. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  1453. Operand nIntOrLong2 = op.RegisterSize == RegisterSize.Int32
  1454. ? context.AddIntrinsicInt (Intrinsic.X86Cvtss2si, nRes)
  1455. : context.AddIntrinsicLong(Intrinsic.X86Cvtss2si, nRes);
  1456. nRes = context.AddIntrinsic(Intrinsic.X86Cmpss, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  1457. Operand nInt = context.AddIntrinsicInt(Intrinsic.X86Cvtsi2si, nRes);
  1458. if (op.RegisterSize == RegisterSize.Int64)
  1459. {
  1460. nInt = context.SignExtend32(OperandType.I64, nInt);
  1461. }
  1462. Operand dRes = context.BitwiseExclusiveOr(nIntOrLong2, nInt);
  1463. dRes = context.Add(dRes, nIntOrLong);
  1464. SetIntOrZR(context, op.Rd, dRes);
  1465. }
  1466. else /* if (op.Size == 1) */
  1467. {
  1468. Operand nRes = context.AddIntrinsic(Intrinsic.X86Cmpsd, n, n, Const((int)CmpCondition.OrderedQ));
  1469. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, n);
  1470. if (isFixed)
  1471. {
  1472. // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, op.FBits)
  1473. long fpScaled = 0x3FF0000000000000L + op.FBits * 0x10000000000000L;
  1474. Operand fpScaledMask = X86GetScalar(context, fpScaled);
  1475. nRes = context.AddIntrinsic(Intrinsic.X86Mulsd, nRes, fpScaledMask);
  1476. }
  1477. if (roundMode != FPRoundingMode.ToNearestAway)
  1478. {
  1479. nRes = context.AddIntrinsic(Intrinsic.X86Roundsd, nRes, Const(X86GetRoundControl(roundMode)));
  1480. }
  1481. else
  1482. {
  1483. nRes = EmitSse41RoundToNearestWithTiesToAwayOpF(context, nRes, scalar: true);
  1484. }
  1485. Operand zero = context.VectorZero();
  1486. Operand nCmp = context.AddIntrinsic(Intrinsic.X86Cmpsd, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  1487. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  1488. long fpMaxVal = op.RegisterSize == RegisterSize.Int32
  1489. ? 0x41E0000000000000L // 2147483648.0000000d (2147483648)
  1490. : 0x43E0000000000000L; // 9.2233720368547760E18d (9223372036854775808)
  1491. Operand fpMaxValMask = X86GetScalar(context, fpMaxVal);
  1492. Operand nIntOrLong = op.RegisterSize == RegisterSize.Int32
  1493. ? context.AddIntrinsicInt (Intrinsic.X86Cvtsd2si, nRes)
  1494. : context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, nRes);
  1495. nRes = context.AddIntrinsic(Intrinsic.X86Subsd, nRes, fpMaxValMask);
  1496. nCmp = context.AddIntrinsic(Intrinsic.X86Cmpsd, nRes, zero, Const((int)CmpCondition.NotLessThanOrEqual));
  1497. nRes = context.AddIntrinsic(Intrinsic.X86Pand, nRes, nCmp);
  1498. Operand nIntOrLong2 = op.RegisterSize == RegisterSize.Int32
  1499. ? context.AddIntrinsicInt (Intrinsic.X86Cvtsd2si, nRes)
  1500. : context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, nRes);
  1501. nRes = context.AddIntrinsic(Intrinsic.X86Cmpsd, nRes, fpMaxValMask, Const((int)CmpCondition.NotLessThan));
  1502. Operand nLong = context.AddIntrinsicLong(Intrinsic.X86Cvtsi2si, nRes);
  1503. if (op.RegisterSize == RegisterSize.Int32)
  1504. {
  1505. nLong = context.ConvertI64ToI32(nLong);
  1506. }
  1507. Operand dRes = context.BitwiseExclusiveOr(nIntOrLong2, nLong);
  1508. dRes = context.Add(dRes, nIntOrLong);
  1509. SetIntOrZR(context, op.Rd, dRes);
  1510. }
  1511. }
  1512. private static Operand EmitVectorLongExtract(ArmEmitterContext context, int reg, int index, int size)
  1513. {
  1514. OperandType type = size == 3 ? OperandType.I64 : OperandType.I32;
  1515. return context.VectorExtract(type, GetVec(reg), index);
  1516. }
  1517. }
  1518. }