InstEmitSimdArithmetic32.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Diagnostics;
  6. using static ARMeilleure.Instructions.InstEmitFlowHelper;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  9. using static ARMeilleure.Instructions.InstEmitSimdHelper32;
  10. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  11. namespace ARMeilleure.Instructions
  12. {
  13. static partial class InstEmit32
  14. {
  15. public static void Vabd_I(ArmEmitterContext context)
  16. {
  17. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  18. EmitVectorBinaryOpI32(context, (op1, op2) => EmitAbs(context, context.Subtract(op1, op2)), !op.U);
  19. }
  20. public static void Vabdl_I(ArmEmitterContext context)
  21. {
  22. OpCode32SimdRegLong op = (OpCode32SimdRegLong)context.CurrOp;
  23. EmitVectorBinaryLongOpI32(context, (op1, op2) => EmitAbs(context, context.Subtract(op1, op2)), !op.U);
  24. }
  25. public static void Vabs_S(ArmEmitterContext context)
  26. {
  27. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  28. if (Optimizations.FastFP && Optimizations.UseSse2)
  29. {
  30. EmitScalarUnaryOpSimd32(context, (m) =>
  31. {
  32. return EmitFloatAbs(context, m, (op.Size & 1) == 0, false);
  33. });
  34. }
  35. else
  36. {
  37. EmitScalarUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Abs), op1));
  38. }
  39. }
  40. public static void Vabs_V(ArmEmitterContext context)
  41. {
  42. OpCode32SimdCmpZ op = (OpCode32SimdCmpZ)context.CurrOp;
  43. if (op.F)
  44. {
  45. if (Optimizations.FastFP && Optimizations.UseSse2)
  46. {
  47. EmitVectorUnaryOpSimd32(context, (m) =>
  48. {
  49. return EmitFloatAbs(context, m, (op.Size & 1) == 0, true);
  50. });
  51. }
  52. else
  53. {
  54. EmitVectorUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Abs), op1));
  55. }
  56. }
  57. else
  58. {
  59. EmitVectorUnaryOpSx32(context, (op1) => EmitAbs(context, op1));
  60. }
  61. }
  62. private static Operand EmitAbs(ArmEmitterContext context, Operand value)
  63. {
  64. Operand isPositive = context.ICompareGreaterOrEqual(value, Const(value.Type, 0));
  65. return context.ConditionalSelect(isPositive, value, context.Negate(value));
  66. }
  67. public static void Vadd_S(ArmEmitterContext context)
  68. {
  69. if (Optimizations.FastFP && Optimizations.UseSse2)
  70. {
  71. EmitScalarBinaryOpF32(context, Intrinsic.X86Addss, Intrinsic.X86Addsd);
  72. }
  73. else if (Optimizations.FastFP)
  74. {
  75. EmitScalarBinaryOpF32(context, (op1, op2) => context.Add(op1, op2));
  76. }
  77. else
  78. {
  79. EmitScalarBinaryOpF32(context, (op1, op2) => EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd), op1, op2));
  80. }
  81. }
  82. public static void Vadd_V(ArmEmitterContext context)
  83. {
  84. if (Optimizations.FastFP && Optimizations.UseSse2)
  85. {
  86. EmitVectorBinaryOpF32(context, Intrinsic.X86Addps, Intrinsic.X86Addpd);
  87. }
  88. else if (Optimizations.FastFP)
  89. {
  90. EmitVectorBinaryOpF32(context, (op1, op2) => context.Add(op1, op2));
  91. }
  92. else
  93. {
  94. EmitVectorBinaryOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPAddFpscr), op1, op2));
  95. }
  96. }
  97. public static void Vadd_I(ArmEmitterContext context)
  98. {
  99. if (Optimizations.UseSse2)
  100. {
  101. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  102. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PaddInstruction[op.Size], op1, op2));
  103. }
  104. else
  105. {
  106. EmitVectorBinaryOpZx32(context, (op1, op2) => context.Add(op1, op2));
  107. }
  108. }
  109. public static void Vaddl_I(ArmEmitterContext context)
  110. {
  111. OpCode32SimdRegLong op = (OpCode32SimdRegLong)context.CurrOp;
  112. EmitVectorBinaryLongOpI32(context, (op1, op2) => context.Add(op1, op2), !op.U);
  113. }
  114. public static void Vaddw_I(ArmEmitterContext context)
  115. {
  116. OpCode32SimdRegWide op = (OpCode32SimdRegWide)context.CurrOp;
  117. EmitVectorBinaryWideOpI32(context, (op1, op2) => context.Add(op1, op2), !op.U);
  118. }
  119. public static void Vdup(ArmEmitterContext context)
  120. {
  121. OpCode32SimdDupGP op = (OpCode32SimdDupGP)context.CurrOp;
  122. Operand insert = GetIntA32(context, op.Rt);
  123. // Zero extend into an I64, then replicate. Saves the most time over elementwise inserts.
  124. insert = op.Size switch
  125. {
  126. 2 => context.Multiply(context.ZeroExtend32(OperandType.I64, insert), Const(0x0000000100000001u)),
  127. 1 => context.Multiply(context.ZeroExtend16(OperandType.I64, insert), Const(0x0001000100010001u)),
  128. 0 => context.Multiply(context.ZeroExtend8(OperandType.I64, insert), Const(0x0101010101010101u)),
  129. _ => throw new InvalidOperationException($"Invalid Vdup size \"{op.Size}\".")
  130. };
  131. InsertScalar(context, op.Vd, insert);
  132. if (op.Q)
  133. {
  134. InsertScalar(context, op.Vd + 1, insert);
  135. }
  136. }
  137. public static void Vdup_1(ArmEmitterContext context)
  138. {
  139. OpCode32SimdDupElem op = (OpCode32SimdDupElem)context.CurrOp;
  140. Operand insert = EmitVectorExtractZx32(context, op.Vm >> 1, ((op.Vm & 1) << (3 - op.Size)) + op.Index, op.Size);
  141. // Zero extend into an I64, then replicate. Saves the most time over elementwise inserts.
  142. insert = op.Size switch
  143. {
  144. 2 => context.Multiply(context.ZeroExtend32(OperandType.I64, insert), Const(0x0000000100000001u)),
  145. 1 => context.Multiply(context.ZeroExtend16(OperandType.I64, insert), Const(0x0001000100010001u)),
  146. 0 => context.Multiply(context.ZeroExtend8(OperandType.I64, insert), Const(0x0101010101010101u)),
  147. _ => throw new InvalidOperationException($"Invalid Vdup size \"{op.Size}\".")
  148. };
  149. InsertScalar(context, op.Vd, insert);
  150. if (op.Q)
  151. {
  152. InsertScalar(context, op.Vd | 1, insert);
  153. }
  154. }
  155. private static (long, long) MaskHelperByteSequence(int start, int length, int startByte)
  156. {
  157. int end = start + length;
  158. int b = startByte;
  159. long result = 0;
  160. long result2 = 0;
  161. for (int i = 0; i < 8; i++)
  162. {
  163. result |= (long)((i >= end || i < start) ? 0x80 : b++) << (i * 8);
  164. }
  165. for (int i = 8; i < 16; i++)
  166. {
  167. result2 |= (long)((i >= end || i < start) ? 0x80 : b++) << ((i - 8) * 8);
  168. }
  169. return (result2, result);
  170. }
  171. public static void Vext(ArmEmitterContext context)
  172. {
  173. OpCode32SimdExt op = (OpCode32SimdExt)context.CurrOp;
  174. int elems = op.GetBytesCount();
  175. int byteOff = op.Immediate;
  176. if (Optimizations.UseSsse3)
  177. {
  178. EmitVectorBinaryOpSimd32(context, (n, m) =>
  179. {
  180. // Writing low to high of d: start <imm> into n, overlap into m.
  181. // Then rotate n down by <imm>, m up by (elems)-imm.
  182. // Then OR them together for the result.
  183. (long nMaskHigh, long nMaskLow) = MaskHelperByteSequence(0, elems - byteOff, byteOff);
  184. (long mMaskHigh, long mMaskLow) = MaskHelperByteSequence(elems - byteOff, byteOff, 0);
  185. Operand nMask, mMask;
  186. if (!op.Q)
  187. {
  188. // Do the same operation to the bytes in the top doubleword too, as our target could be in either.
  189. nMaskHigh = nMaskLow + 0x0808080808080808L;
  190. mMaskHigh = mMaskLow + 0x0808080808080808L;
  191. }
  192. nMask = X86GetElements(context, nMaskHigh, nMaskLow);
  193. mMask = X86GetElements(context, mMaskHigh, mMaskLow);
  194. Operand nPart = context.AddIntrinsic(Intrinsic.X86Pshufb, n, nMask);
  195. Operand mPart = context.AddIntrinsic(Intrinsic.X86Pshufb, m, mMask);
  196. return context.AddIntrinsic(Intrinsic.X86Por, nPart, mPart);
  197. });
  198. }
  199. else
  200. {
  201. Operand res = GetVecA32(op.Qd);
  202. for (int index = 0; index < elems; index++)
  203. {
  204. Operand extract;
  205. if (byteOff >= elems)
  206. {
  207. extract = EmitVectorExtractZx32(context, op.Qm, op.Im + (byteOff - elems), op.Size);
  208. }
  209. else
  210. {
  211. extract = EmitVectorExtractZx32(context, op.Qn, op.In + byteOff, op.Size);
  212. }
  213. byteOff++;
  214. res = EmitVectorInsert(context, res, extract, op.Id + index, op.Size);
  215. }
  216. context.Copy(GetVecA32(op.Qd), res);
  217. }
  218. }
  219. public static void Vfma_S(ArmEmitterContext context) // Fused.
  220. {
  221. if (Optimizations.FastFP && Optimizations.UseSse2)
  222. {
  223. // TODO: Use FMA instruction set.
  224. EmitScalarTernaryOpF32(context, Intrinsic.X86Mulss, Intrinsic.X86Mulsd, Intrinsic.X86Addss, Intrinsic.X86Addsd);
  225. }
  226. else
  227. {
  228. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  229. {
  230. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulAdd), op1, op2, op3);
  231. });
  232. }
  233. }
  234. public static void Vfms_S(ArmEmitterContext context) // Fused.
  235. {
  236. if (Optimizations.FastFP && Optimizations.UseSse2)
  237. {
  238. // TODO: Use FMA instruction set.
  239. EmitScalarTernaryOpF32(context, Intrinsic.X86Mulss, Intrinsic.X86Mulsd, Intrinsic.X86Subss, Intrinsic.X86Subsd);
  240. }
  241. else
  242. {
  243. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  244. {
  245. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulSub), op1, op2, op3);
  246. });
  247. }
  248. }
  249. public static void Vfnms_S(ArmEmitterContext context) // Fused.
  250. {
  251. if (Optimizations.FastFP && Optimizations.UseFma)
  252. {
  253. EmitScalarTernaryOpF32(context, Intrinsic.X86Vfmsub231ss, Intrinsic.X86Vfmsub231sd);
  254. }
  255. else
  256. {
  257. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  258. {
  259. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulAdd), context.Negate(op1), op2, op3);
  260. });
  261. }
  262. }
  263. public static void Vhadd(ArmEmitterContext context)
  264. {
  265. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  266. if (op.U)
  267. {
  268. EmitVectorBinaryOpZx32(context, (op1, op2) => context.ShiftRightUI(context.Add(op1, op2), Const(1)));
  269. }
  270. else
  271. {
  272. EmitVectorBinaryOpSx32(context, (op1, op2) => context.ShiftRightSI(context.Add(op1, op2), Const(1)));
  273. }
  274. }
  275. public static void Vmov_S(ArmEmitterContext context)
  276. {
  277. if (Optimizations.FastFP && Optimizations.UseSse2)
  278. {
  279. EmitScalarUnaryOpF32(context, 0, 0);
  280. }
  281. else
  282. {
  283. EmitScalarUnaryOpF32(context, (op1) => op1);
  284. }
  285. }
  286. public static void Vmovn(ArmEmitterContext context)
  287. {
  288. EmitVectorUnaryNarrowOp32(context, (op1) => op1);
  289. }
  290. public static void Vneg_S(ArmEmitterContext context)
  291. {
  292. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  293. if (Optimizations.UseSse2)
  294. {
  295. EmitScalarUnaryOpSimd32(context, (m) =>
  296. {
  297. if ((op.Size & 1) == 0)
  298. {
  299. Operand mask = X86GetScalar(context, -0f);
  300. return context.AddIntrinsic(Intrinsic.X86Xorps, mask, m);
  301. }
  302. else
  303. {
  304. Operand mask = X86GetScalar(context, -0d);
  305. return context.AddIntrinsic(Intrinsic.X86Xorpd, mask, m);
  306. }
  307. });
  308. }
  309. else
  310. {
  311. EmitScalarUnaryOpF32(context, (op1) => context.Negate(op1));
  312. }
  313. }
  314. public static void Vnmul_S(ArmEmitterContext context)
  315. {
  316. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  317. if (Optimizations.UseSse2)
  318. {
  319. EmitScalarBinaryOpSimd32(context, (n, m) =>
  320. {
  321. if ((op.Size & 1) == 0)
  322. {
  323. Operand res = context.AddIntrinsic(Intrinsic.X86Mulss, n, m);
  324. Operand mask = X86GetScalar(context, -0f);
  325. return context.AddIntrinsic(Intrinsic.X86Xorps, mask, res);
  326. }
  327. else
  328. {
  329. Operand res = context.AddIntrinsic(Intrinsic.X86Mulsd, n, m);
  330. Operand mask = X86GetScalar(context, -0d);
  331. return context.AddIntrinsic(Intrinsic.X86Xorpd, mask, res);
  332. }
  333. });
  334. }
  335. else
  336. {
  337. EmitScalarBinaryOpF32(context, (op1, op2) => context.Negate(context.Multiply(op1, op2)));
  338. }
  339. }
  340. public static void Vnmla_S(ArmEmitterContext context)
  341. {
  342. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  343. if (Optimizations.FastFP && Optimizations.UseSse2)
  344. {
  345. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  346. {
  347. if ((op.Size & 1) == 0)
  348. {
  349. Operand res = context.AddIntrinsic(Intrinsic.X86Mulss, n, m);
  350. res = context.AddIntrinsic(Intrinsic.X86Addss, d, res);
  351. Operand mask = X86GetScalar(context, -0f);
  352. return context.AddIntrinsic(Intrinsic.X86Xorps, mask, res);
  353. }
  354. else
  355. {
  356. Operand res = context.AddIntrinsic(Intrinsic.X86Mulsd, n, m);
  357. res = context.AddIntrinsic(Intrinsic.X86Addsd, d, res);
  358. Operand mask = X86GetScalar(context, -0d);
  359. return context.AddIntrinsic(Intrinsic.X86Xorpd, mask, res);
  360. }
  361. });
  362. }
  363. else if (Optimizations.FastFP)
  364. {
  365. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  366. {
  367. return context.Negate(context.Add(op1, context.Multiply(op2, op3)));
  368. });
  369. }
  370. else
  371. {
  372. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  373. {
  374. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPNegMulAdd), op1, op2, op3);
  375. });
  376. }
  377. }
  378. public static void Vnmls_S(ArmEmitterContext context)
  379. {
  380. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  381. if (Optimizations.FastFP && Optimizations.UseSse2)
  382. {
  383. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  384. {
  385. if ((op.Size & 1) == 0)
  386. {
  387. Operand res = context.AddIntrinsic(Intrinsic.X86Mulss, n, m);
  388. Operand mask = X86GetScalar(context, -0f);
  389. d = context.AddIntrinsic(Intrinsic.X86Xorps, mask, d);
  390. return context.AddIntrinsic(Intrinsic.X86Addss, d, res);
  391. }
  392. else
  393. {
  394. Operand res = context.AddIntrinsic(Intrinsic.X86Mulsd, n, m);
  395. Operand mask = X86GetScalar(context, -0d);
  396. d = context.AddIntrinsic(Intrinsic.X86Xorpd, mask, res);
  397. return context.AddIntrinsic(Intrinsic.X86Addsd, d, res);
  398. }
  399. });
  400. }
  401. else if (Optimizations.FastFP)
  402. {
  403. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  404. {
  405. return context.Add(context.Negate(op1), context.Multiply(op2, op3));
  406. });
  407. }
  408. else
  409. {
  410. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  411. {
  412. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPNegMulSub), op1, op2, op3);
  413. });
  414. }
  415. }
  416. public static void Vneg_V(ArmEmitterContext context)
  417. {
  418. OpCode32SimdCmpZ op = (OpCode32SimdCmpZ)context.CurrOp;
  419. if (op.F)
  420. {
  421. if (Optimizations.FastFP && Optimizations.UseSse2)
  422. {
  423. EmitVectorUnaryOpSimd32(context, (m) =>
  424. {
  425. if ((op.Size & 1) == 0)
  426. {
  427. Operand mask = X86GetAllElements(context, -0f);
  428. return context.AddIntrinsic(Intrinsic.X86Xorps, mask, m);
  429. }
  430. else
  431. {
  432. Operand mask = X86GetAllElements(context, -0d);
  433. return context.AddIntrinsic(Intrinsic.X86Xorpd, mask, m);
  434. }
  435. });
  436. }
  437. else
  438. {
  439. EmitVectorUnaryOpF32(context, (op1) => context.Negate(op1));
  440. }
  441. }
  442. else
  443. {
  444. EmitVectorUnaryOpSx32(context, (op1) => context.Negate(op1));
  445. }
  446. }
  447. public static void Vdiv_S(ArmEmitterContext context)
  448. {
  449. if (Optimizations.FastFP && Optimizations.UseSse2)
  450. {
  451. EmitScalarBinaryOpF32(context, Intrinsic.X86Divss, Intrinsic.X86Divsd);
  452. }
  453. else if (Optimizations.FastFP)
  454. {
  455. EmitScalarBinaryOpF32(context, (op1, op2) => context.Divide(op1, op2));
  456. }
  457. else
  458. {
  459. EmitScalarBinaryOpF32(context, (op1, op2) =>
  460. {
  461. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPDiv), op1, op2);
  462. });
  463. }
  464. }
  465. public static void Vmaxnm_S(ArmEmitterContext context)
  466. {
  467. if (Optimizations.FastFP && Optimizations.UseSse41)
  468. {
  469. EmitSse41MaxMinNumOpF32(context, true, true);
  470. }
  471. else
  472. {
  473. EmitScalarBinaryOpF32(context, (op1, op2) => EmitSoftFloatCall(context, nameof(SoftFloat32.FPMaxNum), op1, op2));
  474. }
  475. }
  476. public static void Vmaxnm_V(ArmEmitterContext context)
  477. {
  478. if (Optimizations.FastFP && Optimizations.UseSse41)
  479. {
  480. EmitSse41MaxMinNumOpF32(context, true, false);
  481. }
  482. else
  483. {
  484. EmitVectorBinaryOpSx32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMaxNumFpscr), op1, op2));
  485. }
  486. }
  487. public static void Vminnm_S(ArmEmitterContext context)
  488. {
  489. if (Optimizations.FastFP && Optimizations.UseSse41)
  490. {
  491. EmitSse41MaxMinNumOpF32(context, false, true);
  492. }
  493. else
  494. {
  495. EmitScalarBinaryOpF32(context, (op1, op2) => EmitSoftFloatCall(context, nameof(SoftFloat32.FPMinNum), op1, op2));
  496. }
  497. }
  498. public static void Vminnm_V(ArmEmitterContext context)
  499. {
  500. if (Optimizations.FastFP && Optimizations.UseSse41)
  501. {
  502. EmitSse41MaxMinNumOpF32(context, false, false);
  503. }
  504. else
  505. {
  506. EmitVectorBinaryOpSx32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMinNumFpscr), op1, op2));
  507. }
  508. }
  509. public static void Vmax_V(ArmEmitterContext context)
  510. {
  511. if (Optimizations.FastFP && Optimizations.UseSse2)
  512. {
  513. EmitVectorBinaryOpF32(context, Intrinsic.X86Maxps, Intrinsic.X86Maxpd);
  514. }
  515. else
  516. {
  517. EmitVectorBinaryOpF32(context, (op1, op2) =>
  518. {
  519. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMaxFpscr), op1, op2);
  520. });
  521. }
  522. }
  523. public static void Vmax_I(ArmEmitterContext context)
  524. {
  525. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  526. if (op.U)
  527. {
  528. if (Optimizations.UseSse2)
  529. {
  530. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PmaxuInstruction[op.Size], op1, op2));
  531. }
  532. else
  533. {
  534. EmitVectorBinaryOpZx32(context, (op1, op2) => context.ConditionalSelect(context.ICompareGreaterUI(op1, op2), op1, op2));
  535. }
  536. }
  537. else
  538. {
  539. if (Optimizations.UseSse2)
  540. {
  541. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PmaxsInstruction[op.Size], op1, op2));
  542. }
  543. else
  544. {
  545. EmitVectorBinaryOpSx32(context, (op1, op2) => context.ConditionalSelect(context.ICompareGreater(op1, op2), op1, op2));
  546. }
  547. }
  548. }
  549. public static void Vmin_V(ArmEmitterContext context)
  550. {
  551. if (Optimizations.FastFP && Optimizations.UseSse2)
  552. {
  553. EmitVectorBinaryOpF32(context, Intrinsic.X86Minps, Intrinsic.X86Minpd);
  554. }
  555. else
  556. {
  557. EmitVectorBinaryOpF32(context, (op1, op2) =>
  558. {
  559. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMinFpscr), op1, op2);
  560. });
  561. }
  562. }
  563. public static void Vmin_I(ArmEmitterContext context)
  564. {
  565. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  566. if (op.U)
  567. {
  568. if (Optimizations.UseSse2)
  569. {
  570. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PminuInstruction[op.Size], op1, op2));
  571. }
  572. else
  573. {
  574. EmitVectorBinaryOpZx32(context, (op1, op2) => context.ConditionalSelect(context.ICompareLessUI(op1, op2), op1, op2));
  575. }
  576. }
  577. else
  578. {
  579. if (Optimizations.UseSse2)
  580. {
  581. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PminsInstruction[op.Size], op1, op2));
  582. }
  583. else
  584. {
  585. EmitVectorBinaryOpSx32(context, (op1, op2) => context.ConditionalSelect(context.ICompareLess(op1, op2), op1, op2));
  586. }
  587. }
  588. }
  589. public static void Vmla_S(ArmEmitterContext context)
  590. {
  591. if (Optimizations.FastFP && Optimizations.UseSse2)
  592. {
  593. EmitScalarTernaryOpF32(context, Intrinsic.X86Mulss, Intrinsic.X86Mulsd, Intrinsic.X86Addss, Intrinsic.X86Addsd);
  594. }
  595. else if (Optimizations.FastFP)
  596. {
  597. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  598. {
  599. return context.Add(op1, context.Multiply(op2, op3));
  600. });
  601. }
  602. else
  603. {
  604. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  605. {
  606. Operand res = EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul), op2, op3);
  607. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd), op1, res);
  608. });
  609. }
  610. }
  611. public static void Vmla_V(ArmEmitterContext context)
  612. {
  613. if (Optimizations.FastFP && Optimizations.UseSse2)
  614. {
  615. EmitVectorTernaryOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd, Intrinsic.X86Addps, Intrinsic.X86Addpd);
  616. }
  617. else if (Optimizations.FastFP)
  618. {
  619. EmitVectorTernaryOpF32(context, (op1, op2, op3) => context.Add(op1, context.Multiply(op2, op3)));
  620. }
  621. else
  622. {
  623. EmitVectorTernaryOpF32(context, (op1, op2, op3) =>
  624. {
  625. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulAddFpscr), op1, op2, op3);
  626. });
  627. }
  628. }
  629. public static void Vmla_I(ArmEmitterContext context)
  630. {
  631. EmitVectorTernaryOpZx32(context, (op1, op2, op3) => context.Add(op1, context.Multiply(op2, op3)));
  632. }
  633. public static void Vmla_1(ArmEmitterContext context)
  634. {
  635. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  636. if (op.F)
  637. {
  638. if (Optimizations.FastFP && Optimizations.UseSse2)
  639. {
  640. EmitVectorsByScalarOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd, Intrinsic.X86Addps, Intrinsic.X86Addpd);
  641. }
  642. else if (Optimizations.FastFP)
  643. {
  644. EmitVectorsByScalarOpF32(context, (op1, op2, op3) => context.Add(op1, context.Multiply(op2, op3)));
  645. }
  646. else
  647. {
  648. EmitVectorsByScalarOpF32(context, (op1, op2, op3) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulAddFpscr), op1, op2, op3));
  649. }
  650. }
  651. else
  652. {
  653. EmitVectorsByScalarOpI32(context, (op1, op2, op3) => context.Add(op1, context.Multiply(op2, op3)), false);
  654. }
  655. }
  656. public static void Vmls_S(ArmEmitterContext context)
  657. {
  658. if (Optimizations.FastFP && Optimizations.UseSse2)
  659. {
  660. EmitScalarTernaryOpF32(context, Intrinsic.X86Mulss, Intrinsic.X86Mulsd, Intrinsic.X86Subss, Intrinsic.X86Subsd);
  661. }
  662. else if (Optimizations.FastFP)
  663. {
  664. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  665. {
  666. return context.Subtract(op1, context.Multiply(op2, op3));
  667. });
  668. }
  669. else
  670. {
  671. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  672. {
  673. Operand res = EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul), op2, op3);
  674. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub), op1, res);
  675. });
  676. }
  677. }
  678. public static void Vmls_V(ArmEmitterContext context)
  679. {
  680. if (Optimizations.FastFP && Optimizations.UseSse2)
  681. {
  682. EmitVectorTernaryOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd, Intrinsic.X86Subps, Intrinsic.X86Subpd);
  683. }
  684. else if (Optimizations.FastFP)
  685. {
  686. EmitVectorTernaryOpF32(context, (op1, op2, op3) => context.Subtract(op1, context.Multiply(op2, op3)));
  687. }
  688. else
  689. {
  690. EmitVectorTernaryOpF32(context, (op1, op2, op3) =>
  691. {
  692. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulSubFpscr), op1, op2, op3);
  693. });
  694. }
  695. }
  696. public static void Vmls_I(ArmEmitterContext context)
  697. {
  698. EmitVectorTernaryOpZx32(context, (op1, op2, op3) => context.Subtract(op1, context.Multiply(op2, op3)));
  699. }
  700. public static void Vmls_1(ArmEmitterContext context)
  701. {
  702. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  703. if (op.F)
  704. {
  705. if (Optimizations.FastFP && Optimizations.UseSse2)
  706. {
  707. EmitVectorsByScalarOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd, Intrinsic.X86Subps, Intrinsic.X86Subpd);
  708. }
  709. else if (Optimizations.FastFP)
  710. {
  711. EmitVectorsByScalarOpF32(context, (op1, op2, op3) => context.Subtract(op1, context.Multiply(op2, op3)));
  712. }
  713. else
  714. {
  715. EmitVectorsByScalarOpF32(context, (op1, op2, op3) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulSubFpscr), op1, op2, op3));
  716. }
  717. }
  718. else
  719. {
  720. EmitVectorsByScalarOpI32(context, (op1, op2, op3) => context.Subtract(op1, context.Multiply(op2, op3)), false);
  721. }
  722. }
  723. public static void Vmlsl_I(ArmEmitterContext context)
  724. {
  725. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  726. EmitVectorTernaryLongOpI32(context, (opD, op1, op2) => context.Subtract(opD, context.Multiply(op1, op2)), !op.U);
  727. }
  728. public static void Vmul_S(ArmEmitterContext context)
  729. {
  730. if (Optimizations.FastFP && Optimizations.UseSse2)
  731. {
  732. EmitScalarBinaryOpF32(context, Intrinsic.X86Mulss, Intrinsic.X86Mulsd);
  733. }
  734. else if (Optimizations.FastFP)
  735. {
  736. EmitScalarBinaryOpF32(context, (op1, op2) => context.Multiply(op1, op2));
  737. }
  738. else
  739. {
  740. EmitScalarBinaryOpF32(context, (op1, op2) =>
  741. {
  742. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul), op1, op2);
  743. });
  744. }
  745. }
  746. public static void Vmul_V(ArmEmitterContext context)
  747. {
  748. if (Optimizations.FastFP && Optimizations.UseSse2)
  749. {
  750. EmitVectorBinaryOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd);
  751. }
  752. else if (Optimizations.FastFP)
  753. {
  754. EmitVectorBinaryOpF32(context, (op1, op2) => context.Multiply(op1, op2));
  755. }
  756. else
  757. {
  758. EmitVectorBinaryOpF32(context, (op1, op2) =>
  759. {
  760. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulFpscr), op1, op2);
  761. });
  762. }
  763. }
  764. public static void Vmul_I(ArmEmitterContext context)
  765. {
  766. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  767. if (op.U) // This instruction is always signed, U indicates polynomial mode.
  768. {
  769. EmitVectorBinaryOpZx32(context, (op1, op2) => EmitPolynomialMultiply(context, op1, op2, 8 << op.Size));
  770. }
  771. else
  772. {
  773. EmitVectorBinaryOpSx32(context, (op1, op2) => context.Multiply(op1, op2));
  774. }
  775. }
  776. public static void Vmul_1(ArmEmitterContext context)
  777. {
  778. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  779. if (op.F)
  780. {
  781. if (Optimizations.FastFP && Optimizations.UseSse2)
  782. {
  783. EmitVectorByScalarOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd);
  784. }
  785. else if (Optimizations.FastFP)
  786. {
  787. EmitVectorByScalarOpF32(context, (op1, op2) => context.Multiply(op1, op2));
  788. }
  789. else
  790. {
  791. EmitVectorByScalarOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulFpscr), op1, op2));
  792. }
  793. }
  794. else
  795. {
  796. EmitVectorByScalarOpI32(context, (op1, op2) => context.Multiply(op1, op2), false);
  797. }
  798. }
  799. public static void Vmull_1(ArmEmitterContext context)
  800. {
  801. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  802. EmitVectorByScalarLongOpI32(context, (op1, op2) => context.Multiply(op1, op2), !op.U);
  803. }
  804. public static void Vmull_I(ArmEmitterContext context)
  805. {
  806. OpCode32SimdRegLong op = (OpCode32SimdRegLong)context.CurrOp;
  807. if (op.Polynomial)
  808. {
  809. EmitVectorBinaryLongOpI32(context, (op1, op2) => EmitPolynomialMultiply(context, op1, op2, 8 << op.Size), false);
  810. }
  811. else
  812. {
  813. EmitVectorBinaryLongOpI32(context, (op1, op2) => context.Multiply(op1, op2), !op.U);
  814. }
  815. }
  816. public static void Vpadd_V(ArmEmitterContext context)
  817. {
  818. if (Optimizations.FastFP && Optimizations.UseSse2)
  819. {
  820. EmitSse2VectorPairwiseOpF32(context, Intrinsic.X86Addps);
  821. }
  822. else
  823. {
  824. EmitVectorPairwiseOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPAddFpscr), op1, op2));
  825. }
  826. }
  827. public static void Vpadd_I(ArmEmitterContext context)
  828. {
  829. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  830. if (Optimizations.UseSsse3)
  831. {
  832. EmitSsse3VectorPairwiseOp32(context, X86PaddInstruction);
  833. }
  834. else
  835. {
  836. EmitVectorPairwiseOpI32(context, (op1, op2) => context.Add(op1, op2), !op.U);
  837. }
  838. }
  839. public static void Vpmax_V(ArmEmitterContext context)
  840. {
  841. if (Optimizations.FastFP && Optimizations.UseSse2)
  842. {
  843. EmitSse2VectorPairwiseOpF32(context, Intrinsic.X86Maxps);
  844. }
  845. else
  846. {
  847. EmitVectorPairwiseOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat64.FPMaxFpscr), op1, op2));
  848. }
  849. }
  850. public static void Vpmax_I(ArmEmitterContext context)
  851. {
  852. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  853. if (Optimizations.UseSsse3)
  854. {
  855. EmitSsse3VectorPairwiseOp32(context, op.U ? X86PmaxuInstruction : X86PmaxsInstruction);
  856. }
  857. else
  858. {
  859. EmitVectorPairwiseOpI32(context, (op1, op2) =>
  860. {
  861. Operand greater = op.U ? context.ICompareGreaterUI(op1, op2) : context.ICompareGreater(op1, op2);
  862. return context.ConditionalSelect(greater, op1, op2);
  863. }, !op.U);
  864. }
  865. }
  866. public static void Vpmin_V(ArmEmitterContext context)
  867. {
  868. if (Optimizations.FastFP && Optimizations.UseSse2)
  869. {
  870. EmitSse2VectorPairwiseOpF32(context, Intrinsic.X86Minps);
  871. }
  872. else
  873. {
  874. EmitVectorPairwiseOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMinFpscr), op1, op2));
  875. }
  876. }
  877. public static void Vpmin_I(ArmEmitterContext context)
  878. {
  879. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  880. if (Optimizations.UseSsse3)
  881. {
  882. EmitSsse3VectorPairwiseOp32(context, op.U ? X86PminuInstruction : X86PminsInstruction);
  883. }
  884. else
  885. {
  886. EmitVectorPairwiseOpI32(context, (op1, op2) =>
  887. {
  888. Operand greater = op.U ? context.ICompareLessUI(op1, op2) : context.ICompareLess(op1, op2);
  889. return context.ConditionalSelect(greater, op1, op2);
  890. }, !op.U);
  891. }
  892. }
  893. public static void Vrev(ArmEmitterContext context)
  894. {
  895. OpCode32SimdRev op = (OpCode32SimdRev)context.CurrOp;
  896. if (Optimizations.UseSsse3)
  897. {
  898. EmitVectorUnaryOpSimd32(context, (op1) =>
  899. {
  900. Operand mask;
  901. switch (op.Size)
  902. {
  903. case 3:
  904. // Rev64
  905. switch (op.Opc)
  906. {
  907. case 0:
  908. mask = X86GetElements(context, 0x08090a0b0c0d0e0fL, 0x0001020304050607L);
  909. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  910. case 1:
  911. mask = X86GetElements(context, 0x09080b0a0d0c0f0eL, 0x0100030205040706L);
  912. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  913. case 2:
  914. return context.AddIntrinsic(Intrinsic.X86Shufps, op1, op1, Const(1 | (0 << 2) | (3 << 4) | (2 << 6)));
  915. }
  916. break;
  917. case 2:
  918. // Rev32
  919. switch (op.Opc)
  920. {
  921. case 0:
  922. mask = X86GetElements(context, 0x0c0d0e0f_08090a0bL, 0x04050607_00010203L);
  923. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  924. case 1:
  925. mask = X86GetElements(context, 0x0d0c0f0e_09080b0aL, 0x05040706_01000302L);
  926. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  927. }
  928. break;
  929. case 1:
  930. // Rev16
  931. mask = X86GetElements(context, 0x0e0f_0c0d_0a0b_0809L, 0x_0607_0405_0203_0001L);
  932. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  933. }
  934. throw new InvalidOperationException("Invalid VREV Opcode + Size combo."); // Should be unreachable.
  935. });
  936. }
  937. else
  938. {
  939. EmitVectorUnaryOpZx32(context, (op1) =>
  940. {
  941. switch (op.Opc)
  942. {
  943. case 0:
  944. switch (op.Size) // Swap bytes.
  945. {
  946. case 1:
  947. return InstEmitAluHelper.EmitReverseBytes16_32Op(context, op1);
  948. case 2:
  949. case 3:
  950. return context.ByteSwap(op1);
  951. }
  952. break;
  953. case 1:
  954. switch (op.Size)
  955. {
  956. case 2:
  957. return context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op1, Const(0xffff0000)), Const(16)),
  958. context.ShiftLeft(context.BitwiseAnd(op1, Const(0x0000ffff)), Const(16)));
  959. case 3:
  960. return context.BitwiseOr(
  961. context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op1, Const(0xffff000000000000ul)), Const(48)),
  962. context.ShiftLeft(context.BitwiseAnd(op1, Const(0x000000000000fffful)), Const(48))),
  963. context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op1, Const(0x0000ffff00000000ul)), Const(16)),
  964. context.ShiftLeft(context.BitwiseAnd(op1, Const(0x00000000ffff0000ul)), Const(16))));
  965. }
  966. break;
  967. case 2:
  968. // Swap upper and lower halves.
  969. return context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op1, Const(0xffffffff00000000ul)), Const(32)),
  970. context.ShiftLeft(context.BitwiseAnd(op1, Const(0x00000000fffffffful)), Const(32)));
  971. }
  972. throw new InvalidOperationException("Invalid VREV Opcode + Size combo."); // Should be unreachable.
  973. });
  974. }
  975. }
  976. public static void Vrecpe(ArmEmitterContext context)
  977. {
  978. OpCode32SimdSqrte op = (OpCode32SimdSqrte)context.CurrOp;
  979. if (op.F)
  980. {
  981. int sizeF = op.Size & 1;
  982. if (Optimizations.FastFP && Optimizations.UseSse2 && sizeF == 0)
  983. {
  984. EmitVectorUnaryOpF32(context, Intrinsic.X86Rcpps, 0);
  985. }
  986. else
  987. {
  988. EmitVectorUnaryOpF32(context, (op1) =>
  989. {
  990. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPRecipEstimateFpscr), op1);
  991. });
  992. }
  993. }
  994. else
  995. {
  996. throw new NotImplementedException("Integer Vrecpe not currently implemented.");
  997. }
  998. }
  999. public static void Vrecps(ArmEmitterContext context)
  1000. {
  1001. if (Optimizations.FastFP && Optimizations.UseSse2)
  1002. {
  1003. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  1004. bool single = (op.Size & 1) == 0;
  1005. // (2 - (n*m))
  1006. EmitVectorBinaryOpSimd32(context, (n, m) =>
  1007. {
  1008. if (single)
  1009. {
  1010. Operand maskTwo = X86GetAllElements(context, 2f);
  1011. Operand res = context.AddIntrinsic(Intrinsic.X86Mulps, n, m);
  1012. return context.AddIntrinsic(Intrinsic.X86Subps, maskTwo, res);
  1013. }
  1014. else
  1015. {
  1016. Operand maskTwo = X86GetAllElements(context, 2d);
  1017. Operand res = context.AddIntrinsic(Intrinsic.X86Mulpd, n, m);
  1018. return context.AddIntrinsic(Intrinsic.X86Subpd, maskTwo, res);
  1019. }
  1020. });
  1021. }
  1022. else
  1023. {
  1024. EmitVectorBinaryOpF32(context, (op1, op2) =>
  1025. {
  1026. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipStep), op1, op2);
  1027. });
  1028. }
  1029. }
  1030. public static void Vrsqrte(ArmEmitterContext context)
  1031. {
  1032. OpCode32SimdSqrte op = (OpCode32SimdSqrte)context.CurrOp;
  1033. if (op.F)
  1034. {
  1035. int sizeF = op.Size & 1;
  1036. if (Optimizations.FastFP && Optimizations.UseSse2 && sizeF == 0)
  1037. {
  1038. EmitVectorUnaryOpF32(context, Intrinsic.X86Rsqrtps, 0);
  1039. }
  1040. else
  1041. {
  1042. EmitVectorUnaryOpF32(context, (op1) =>
  1043. {
  1044. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPRSqrtEstimateFpscr), op1);
  1045. });
  1046. }
  1047. }
  1048. else
  1049. {
  1050. throw new NotImplementedException("Integer Vrsqrte not currently implemented.");
  1051. }
  1052. }
  1053. public static void Vrsqrts(ArmEmitterContext context)
  1054. {
  1055. if (Optimizations.FastFP && Optimizations.UseSse2)
  1056. {
  1057. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  1058. bool single = (op.Size & 1) == 0;
  1059. // (3 - (n*m)) / 2
  1060. EmitVectorBinaryOpSimd32(context, (n, m) =>
  1061. {
  1062. if (single)
  1063. {
  1064. Operand maskHalf = X86GetAllElements(context, 0.5f);
  1065. Operand maskThree = X86GetAllElements(context, 3f);
  1066. Operand res = context.AddIntrinsic(Intrinsic.X86Mulps, n, m);
  1067. res = context.AddIntrinsic(Intrinsic.X86Subps, maskThree, res);
  1068. return context.AddIntrinsic(Intrinsic.X86Mulps, maskHalf, res);
  1069. }
  1070. else
  1071. {
  1072. Operand maskHalf = X86GetAllElements(context, 0.5d);
  1073. Operand maskThree = X86GetAllElements(context, 3d);
  1074. Operand res = context.AddIntrinsic(Intrinsic.X86Mulpd, n, m);
  1075. res = context.AddIntrinsic(Intrinsic.X86Subpd, maskThree, res);
  1076. return context.AddIntrinsic(Intrinsic.X86Mulpd, maskHalf, res);
  1077. }
  1078. });
  1079. }
  1080. else
  1081. {
  1082. EmitVectorBinaryOpF32(context, (op1, op2) =>
  1083. {
  1084. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtStep), op1, op2);
  1085. });
  1086. }
  1087. }
  1088. public static void Vsel(ArmEmitterContext context)
  1089. {
  1090. OpCode32SimdSel op = (OpCode32SimdSel)context.CurrOp;
  1091. Operand condition = null;
  1092. switch (op.Cc)
  1093. {
  1094. case OpCode32SimdSelMode.Eq:
  1095. condition = GetCondTrue(context, Condition.Eq);
  1096. break;
  1097. case OpCode32SimdSelMode.Ge:
  1098. condition = GetCondTrue(context, Condition.Ge);
  1099. break;
  1100. case OpCode32SimdSelMode.Gt:
  1101. condition = GetCondTrue(context, Condition.Gt);
  1102. break;
  1103. case OpCode32SimdSelMode.Vs:
  1104. condition = GetCondTrue(context, Condition.Vs);
  1105. break;
  1106. }
  1107. EmitScalarBinaryOpI32(context, (op1, op2) =>
  1108. {
  1109. return context.ConditionalSelect(condition, op1, op2);
  1110. });
  1111. }
  1112. public static void Vsqrt_S(ArmEmitterContext context)
  1113. {
  1114. if (Optimizations.FastFP && Optimizations.UseSse2)
  1115. {
  1116. EmitScalarUnaryOpF32(context, Intrinsic.X86Sqrtss, Intrinsic.X86Sqrtsd);
  1117. }
  1118. else
  1119. {
  1120. EmitScalarUnaryOpF32(context, (op1) =>
  1121. {
  1122. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPSqrt), op1);
  1123. });
  1124. }
  1125. }
  1126. public static void Vsub_S(ArmEmitterContext context)
  1127. {
  1128. if (Optimizations.FastFP && Optimizations.UseSse2)
  1129. {
  1130. EmitScalarBinaryOpF32(context, Intrinsic.X86Subss, Intrinsic.X86Subsd);
  1131. }
  1132. else
  1133. {
  1134. EmitScalarBinaryOpF32(context, (op1, op2) => context.Subtract(op1, op2));
  1135. }
  1136. }
  1137. public static void Vsub_V(ArmEmitterContext context)
  1138. {
  1139. if (Optimizations.FastFP && Optimizations.UseSse2)
  1140. {
  1141. EmitVectorBinaryOpF32(context, Intrinsic.X86Subps, Intrinsic.X86Subpd);
  1142. }
  1143. else
  1144. {
  1145. EmitVectorBinaryOpF32(context, (op1, op2) => context.Subtract(op1, op2));
  1146. }
  1147. }
  1148. public static void Vsub_I(ArmEmitterContext context)
  1149. {
  1150. if (Optimizations.UseSse2)
  1151. {
  1152. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  1153. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PsubInstruction[op.Size], op1, op2));
  1154. }
  1155. else
  1156. {
  1157. EmitVectorBinaryOpZx32(context, (op1, op2) => context.Subtract(op1, op2));
  1158. }
  1159. }
  1160. public static void Vsubw_I(ArmEmitterContext context)
  1161. {
  1162. OpCode32SimdRegWide op = (OpCode32SimdRegWide)context.CurrOp;
  1163. EmitVectorBinaryWideOpI32(context, (op1, op2) => context.Subtract(op1, op2), !op.U);
  1164. }
  1165. private static void EmitSse41MaxMinNumOpF32(ArmEmitterContext context, bool isMaxNum, bool scalar)
  1166. {
  1167. IOpCode32Simd op = (IOpCode32Simd)context.CurrOp;
  1168. Func<Operand, Operand, Operand> genericEmit = (n, m) =>
  1169. {
  1170. Operand nNum = context.Copy(n);
  1171. Operand mNum = context.Copy(m);
  1172. InstEmit.EmitSse2VectorIsNaNOpF(context, nNum, out Operand nQNaNMask, out _, isQNaN: true);
  1173. InstEmit.EmitSse2VectorIsNaNOpF(context, mNum, out Operand mQNaNMask, out _, isQNaN: true);
  1174. int sizeF = op.Size & 1;
  1175. if (sizeF == 0)
  1176. {
  1177. Operand negInfMask = X86GetAllElements(context, isMaxNum ? float.NegativeInfinity : float.PositiveInfinity);
  1178. Operand nMask = context.AddIntrinsic(Intrinsic.X86Andnps, mQNaNMask, nQNaNMask);
  1179. Operand mMask = context.AddIntrinsic(Intrinsic.X86Andnps, nQNaNMask, mQNaNMask);
  1180. nNum = context.AddIntrinsic(Intrinsic.X86Blendvps, nNum, negInfMask, nMask);
  1181. mNum = context.AddIntrinsic(Intrinsic.X86Blendvps, mNum, negInfMask, mMask);
  1182. return context.AddIntrinsic(isMaxNum ? Intrinsic.X86Maxps : Intrinsic.X86Minps, nNum, mNum);
  1183. }
  1184. else /* if (sizeF == 1) */
  1185. {
  1186. Operand negInfMask = X86GetAllElements(context, isMaxNum ? double.NegativeInfinity : double.PositiveInfinity);
  1187. Operand nMask = context.AddIntrinsic(Intrinsic.X86Andnpd, mQNaNMask, nQNaNMask);
  1188. Operand mMask = context.AddIntrinsic(Intrinsic.X86Andnpd, nQNaNMask, mQNaNMask);
  1189. nNum = context.AddIntrinsic(Intrinsic.X86Blendvpd, nNum, negInfMask, nMask);
  1190. mNum = context.AddIntrinsic(Intrinsic.X86Blendvpd, mNum, negInfMask, mMask);
  1191. return context.AddIntrinsic(isMaxNum ? Intrinsic.X86Maxpd : Intrinsic.X86Minpd, nNum, mNum);
  1192. }
  1193. };
  1194. if (scalar)
  1195. {
  1196. EmitScalarBinaryOpSimd32(context, genericEmit);
  1197. }
  1198. else
  1199. {
  1200. EmitVectorBinaryOpSimd32(context, genericEmit);
  1201. }
  1202. }
  1203. private static Operand EmitPolynomialMultiply(ArmEmitterContext context, Operand op1, Operand op2, int eSize)
  1204. {
  1205. Debug.Assert(eSize <= 32);
  1206. Operand result = eSize == 32 ? Const(0L) : Const(0);
  1207. if (eSize == 32)
  1208. {
  1209. op1 = context.ZeroExtend32(OperandType.I64, op1);
  1210. op2 = context.ZeroExtend32(OperandType.I64, op2);
  1211. }
  1212. for (int i = 0; i < eSize; i++)
  1213. {
  1214. Operand mask = context.BitwiseAnd(op1, Const(op1.Type, 1L << i));
  1215. result = context.BitwiseExclusiveOr(result, context.Multiply(op2, mask));
  1216. }
  1217. return result;
  1218. }
  1219. }
  1220. }