InstEmitSimdArithmetic32.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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 Vhadd(ArmEmitterContext context)
  250. {
  251. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  252. if (op.U)
  253. {
  254. EmitVectorBinaryOpZx32(context, (op1, op2) => context.ShiftRightUI(context.Add(op1, op2), Const(1)));
  255. }
  256. else
  257. {
  258. EmitVectorBinaryOpSx32(context, (op1, op2) => context.ShiftRightSI(context.Add(op1, op2), Const(1)));
  259. }
  260. }
  261. public static void Vmov_S(ArmEmitterContext context)
  262. {
  263. if (Optimizations.FastFP && Optimizations.UseSse2)
  264. {
  265. EmitScalarUnaryOpF32(context, 0, 0);
  266. }
  267. else
  268. {
  269. EmitScalarUnaryOpF32(context, (op1) => op1);
  270. }
  271. }
  272. public static void Vmovn(ArmEmitterContext context)
  273. {
  274. EmitVectorUnaryNarrowOp32(context, (op1) => op1);
  275. }
  276. public static void Vneg_S(ArmEmitterContext context)
  277. {
  278. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  279. if (Optimizations.UseSse2)
  280. {
  281. EmitScalarUnaryOpSimd32(context, (m) =>
  282. {
  283. if ((op.Size & 1) == 0)
  284. {
  285. Operand mask = X86GetScalar(context, -0f);
  286. return context.AddIntrinsic(Intrinsic.X86Xorps, mask, m);
  287. }
  288. else
  289. {
  290. Operand mask = X86GetScalar(context, -0d);
  291. return context.AddIntrinsic(Intrinsic.X86Xorpd, mask, m);
  292. }
  293. });
  294. }
  295. else
  296. {
  297. EmitScalarUnaryOpF32(context, (op1) => context.Negate(op1));
  298. }
  299. }
  300. public static void Vnmul_S(ArmEmitterContext context)
  301. {
  302. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  303. if (Optimizations.UseSse2)
  304. {
  305. EmitScalarBinaryOpSimd32(context, (n, m) =>
  306. {
  307. if ((op.Size & 1) == 0)
  308. {
  309. Operand res = context.AddIntrinsic(Intrinsic.X86Mulss, n, m);
  310. Operand mask = X86GetScalar(context, -0f);
  311. return context.AddIntrinsic(Intrinsic.X86Xorps, mask, res);
  312. }
  313. else
  314. {
  315. Operand res = context.AddIntrinsic(Intrinsic.X86Mulsd, n, m);
  316. Operand mask = X86GetScalar(context, -0d);
  317. return context.AddIntrinsic(Intrinsic.X86Xorpd, mask, res);
  318. }
  319. });
  320. }
  321. else
  322. {
  323. EmitScalarBinaryOpF32(context, (op1, op2) => context.Negate(context.Multiply(op1, op2)));
  324. }
  325. }
  326. public static void Vnmla_S(ArmEmitterContext context)
  327. {
  328. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  329. if (Optimizations.FastFP && Optimizations.UseSse2)
  330. {
  331. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  332. {
  333. if ((op.Size & 1) == 0)
  334. {
  335. Operand res = context.AddIntrinsic(Intrinsic.X86Mulss, n, m);
  336. res = context.AddIntrinsic(Intrinsic.X86Addss, d, res);
  337. Operand mask = X86GetScalar(context, -0f);
  338. return context.AddIntrinsic(Intrinsic.X86Xorps, mask, res);
  339. }
  340. else
  341. {
  342. Operand res = context.AddIntrinsic(Intrinsic.X86Mulsd, n, m);
  343. res = context.AddIntrinsic(Intrinsic.X86Addsd, d, res);
  344. Operand mask = X86GetScalar(context, -0d);
  345. return context.AddIntrinsic(Intrinsic.X86Xorpd, mask, res);
  346. }
  347. });
  348. }
  349. else if (Optimizations.FastFP)
  350. {
  351. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  352. {
  353. return context.Negate(context.Add(op1, context.Multiply(op2, op3)));
  354. });
  355. }
  356. else
  357. {
  358. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  359. {
  360. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPNegMulAdd), op1, op2, op3);
  361. });
  362. }
  363. }
  364. public static void Vnmls_S(ArmEmitterContext context)
  365. {
  366. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  367. if (Optimizations.FastFP && Optimizations.UseSse2)
  368. {
  369. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  370. {
  371. if ((op.Size & 1) == 0)
  372. {
  373. Operand res = context.AddIntrinsic(Intrinsic.X86Mulss, n, m);
  374. Operand mask = X86GetScalar(context, -0f);
  375. d = context.AddIntrinsic(Intrinsic.X86Xorps, mask, d);
  376. return context.AddIntrinsic(Intrinsic.X86Addss, d, res);
  377. }
  378. else
  379. {
  380. Operand res = context.AddIntrinsic(Intrinsic.X86Mulsd, n, m);
  381. Operand mask = X86GetScalar(context, -0d);
  382. d = context.AddIntrinsic(Intrinsic.X86Xorpd, mask, res);
  383. return context.AddIntrinsic(Intrinsic.X86Addsd, d, res);
  384. }
  385. });
  386. }
  387. else if (Optimizations.FastFP)
  388. {
  389. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  390. {
  391. return context.Add(context.Negate(op1), context.Multiply(op2, op3));
  392. });
  393. }
  394. else
  395. {
  396. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  397. {
  398. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPNegMulSub), op1, op2, op3);
  399. });
  400. }
  401. }
  402. public static void Vneg_V(ArmEmitterContext context)
  403. {
  404. OpCode32SimdCmpZ op = (OpCode32SimdCmpZ)context.CurrOp;
  405. if (op.F)
  406. {
  407. if (Optimizations.FastFP && Optimizations.UseSse2)
  408. {
  409. EmitVectorUnaryOpSimd32(context, (m) =>
  410. {
  411. if ((op.Size & 1) == 0)
  412. {
  413. Operand mask = X86GetAllElements(context, -0f);
  414. return context.AddIntrinsic(Intrinsic.X86Xorps, mask, m);
  415. }
  416. else
  417. {
  418. Operand mask = X86GetAllElements(context, -0d);
  419. return context.AddIntrinsic(Intrinsic.X86Xorpd, mask, m);
  420. }
  421. });
  422. }
  423. else
  424. {
  425. EmitVectorUnaryOpF32(context, (op1) => context.Negate(op1));
  426. }
  427. }
  428. else
  429. {
  430. EmitVectorUnaryOpSx32(context, (op1) => context.Negate(op1));
  431. }
  432. }
  433. public static void Vdiv_S(ArmEmitterContext context)
  434. {
  435. if (Optimizations.FastFP && Optimizations.UseSse2)
  436. {
  437. EmitScalarBinaryOpF32(context, Intrinsic.X86Divss, Intrinsic.X86Divsd);
  438. }
  439. else if (Optimizations.FastFP)
  440. {
  441. EmitScalarBinaryOpF32(context, (op1, op2) => context.Divide(op1, op2));
  442. }
  443. else
  444. {
  445. EmitScalarBinaryOpF32(context, (op1, op2) =>
  446. {
  447. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPDiv), op1, op2);
  448. });
  449. }
  450. }
  451. public static void Vmaxnm_S(ArmEmitterContext context)
  452. {
  453. if (Optimizations.FastFP && Optimizations.UseSse41)
  454. {
  455. EmitSse41MaxMinNumOpF32(context, true, true);
  456. }
  457. else
  458. {
  459. EmitScalarBinaryOpF32(context, (op1, op2) => EmitSoftFloatCall(context, nameof(SoftFloat32.FPMaxNum), op1, op2));
  460. }
  461. }
  462. public static void Vmaxnm_V(ArmEmitterContext context)
  463. {
  464. if (Optimizations.FastFP && Optimizations.UseSse41)
  465. {
  466. EmitSse41MaxMinNumOpF32(context, true, false);
  467. }
  468. else
  469. {
  470. EmitVectorBinaryOpSx32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMaxNumFpscr), op1, op2));
  471. }
  472. }
  473. public static void Vminnm_S(ArmEmitterContext context)
  474. {
  475. if (Optimizations.FastFP && Optimizations.UseSse41)
  476. {
  477. EmitSse41MaxMinNumOpF32(context, false, true);
  478. }
  479. else
  480. {
  481. EmitScalarBinaryOpF32(context, (op1, op2) => EmitSoftFloatCall(context, nameof(SoftFloat32.FPMinNum), op1, op2));
  482. }
  483. }
  484. public static void Vminnm_V(ArmEmitterContext context)
  485. {
  486. if (Optimizations.FastFP && Optimizations.UseSse41)
  487. {
  488. EmitSse41MaxMinNumOpF32(context, false, false);
  489. }
  490. else
  491. {
  492. EmitVectorBinaryOpSx32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMinNumFpscr), op1, op2));
  493. }
  494. }
  495. public static void Vmax_V(ArmEmitterContext context)
  496. {
  497. if (Optimizations.FastFP && Optimizations.UseSse2)
  498. {
  499. EmitVectorBinaryOpF32(context, Intrinsic.X86Maxps, Intrinsic.X86Maxpd);
  500. }
  501. else
  502. {
  503. EmitVectorBinaryOpF32(context, (op1, op2) =>
  504. {
  505. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMaxFpscr), op1, op2);
  506. });
  507. }
  508. }
  509. public static void Vmax_I(ArmEmitterContext context)
  510. {
  511. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  512. if (op.U)
  513. {
  514. if (Optimizations.UseSse2)
  515. {
  516. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PmaxuInstruction[op.Size], op1, op2));
  517. }
  518. else
  519. {
  520. EmitVectorBinaryOpZx32(context, (op1, op2) => context.ConditionalSelect(context.ICompareGreaterUI(op1, op2), op1, op2));
  521. }
  522. }
  523. else
  524. {
  525. if (Optimizations.UseSse2)
  526. {
  527. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PmaxsInstruction[op.Size], op1, op2));
  528. }
  529. else
  530. {
  531. EmitVectorBinaryOpSx32(context, (op1, op2) => context.ConditionalSelect(context.ICompareGreater(op1, op2), op1, op2));
  532. }
  533. }
  534. }
  535. public static void Vmin_V(ArmEmitterContext context)
  536. {
  537. if (Optimizations.FastFP && Optimizations.UseSse2)
  538. {
  539. EmitVectorBinaryOpF32(context, Intrinsic.X86Minps, Intrinsic.X86Minpd);
  540. }
  541. else
  542. {
  543. EmitVectorBinaryOpF32(context, (op1, op2) =>
  544. {
  545. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMinFpscr), op1, op2);
  546. });
  547. }
  548. }
  549. public static void Vmin_I(ArmEmitterContext context)
  550. {
  551. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  552. if (op.U)
  553. {
  554. if (Optimizations.UseSse2)
  555. {
  556. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PminuInstruction[op.Size], op1, op2));
  557. }
  558. else
  559. {
  560. EmitVectorBinaryOpZx32(context, (op1, op2) => context.ConditionalSelect(context.ICompareLessUI(op1, op2), op1, op2));
  561. }
  562. }
  563. else
  564. {
  565. if (Optimizations.UseSse2)
  566. {
  567. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PminsInstruction[op.Size], op1, op2));
  568. }
  569. else
  570. {
  571. EmitVectorBinaryOpSx32(context, (op1, op2) => context.ConditionalSelect(context.ICompareLess(op1, op2), op1, op2));
  572. }
  573. }
  574. }
  575. public static void Vmla_S(ArmEmitterContext context)
  576. {
  577. if (Optimizations.FastFP && Optimizations.UseSse2)
  578. {
  579. EmitScalarTernaryOpF32(context, Intrinsic.X86Mulss, Intrinsic.X86Mulsd, Intrinsic.X86Addss, Intrinsic.X86Addsd);
  580. }
  581. else if (Optimizations.FastFP)
  582. {
  583. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  584. {
  585. return context.Add(op1, context.Multiply(op2, op3));
  586. });
  587. }
  588. else
  589. {
  590. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  591. {
  592. Operand res = EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul), op2, op3);
  593. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd), op1, res);
  594. });
  595. }
  596. }
  597. public static void Vmla_V(ArmEmitterContext context)
  598. {
  599. if (Optimizations.FastFP && Optimizations.UseSse2)
  600. {
  601. EmitVectorTernaryOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd, Intrinsic.X86Addps, Intrinsic.X86Addpd);
  602. }
  603. else if (Optimizations.FastFP)
  604. {
  605. EmitVectorTernaryOpF32(context, (op1, op2, op3) => context.Add(op1, context.Multiply(op2, op3)));
  606. }
  607. else
  608. {
  609. EmitVectorTernaryOpF32(context, (op1, op2, op3) =>
  610. {
  611. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulAddFpscr), op1, op2, op3);
  612. });
  613. }
  614. }
  615. public static void Vmla_I(ArmEmitterContext context)
  616. {
  617. EmitVectorTernaryOpZx32(context, (op1, op2, op3) => context.Add(op1, context.Multiply(op2, op3)));
  618. }
  619. public static void Vmla_1(ArmEmitterContext context)
  620. {
  621. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  622. if (op.F)
  623. {
  624. if (Optimizations.FastFP && Optimizations.UseSse2)
  625. {
  626. EmitVectorsByScalarOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd, Intrinsic.X86Addps, Intrinsic.X86Addpd);
  627. }
  628. else if (Optimizations.FastFP)
  629. {
  630. EmitVectorsByScalarOpF32(context, (op1, op2, op3) => context.Add(op1, context.Multiply(op2, op3)));
  631. }
  632. else
  633. {
  634. EmitVectorsByScalarOpF32(context, (op1, op2, op3) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulAddFpscr), op1, op2, op3));
  635. }
  636. }
  637. else
  638. {
  639. EmitVectorsByScalarOpI32(context, (op1, op2, op3) => context.Add(op1, context.Multiply(op2, op3)), false);
  640. }
  641. }
  642. public static void Vmls_S(ArmEmitterContext context)
  643. {
  644. if (Optimizations.FastFP && Optimizations.UseSse2)
  645. {
  646. EmitScalarTernaryOpF32(context, Intrinsic.X86Mulss, Intrinsic.X86Mulsd, Intrinsic.X86Subss, Intrinsic.X86Subsd);
  647. }
  648. else if (Optimizations.FastFP)
  649. {
  650. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  651. {
  652. return context.Subtract(op1, context.Multiply(op2, op3));
  653. });
  654. }
  655. else
  656. {
  657. EmitScalarTernaryOpF32(context, (op1, op2, op3) =>
  658. {
  659. Operand res = EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul), op2, op3);
  660. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub), op1, res);
  661. });
  662. }
  663. }
  664. public static void Vmls_V(ArmEmitterContext context)
  665. {
  666. if (Optimizations.FastFP && Optimizations.UseSse2)
  667. {
  668. EmitVectorTernaryOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd, Intrinsic.X86Subps, Intrinsic.X86Subpd);
  669. }
  670. else if (Optimizations.FastFP)
  671. {
  672. EmitVectorTernaryOpF32(context, (op1, op2, op3) => context.Subtract(op1, context.Multiply(op2, op3)));
  673. }
  674. else
  675. {
  676. EmitVectorTernaryOpF32(context, (op1, op2, op3) =>
  677. {
  678. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulSubFpscr), op1, op2, op3);
  679. });
  680. }
  681. }
  682. public static void Vmls_I(ArmEmitterContext context)
  683. {
  684. EmitVectorTernaryOpZx32(context, (op1, op2, op3) => context.Subtract(op1, context.Multiply(op2, op3)));
  685. }
  686. public static void Vmls_1(ArmEmitterContext context)
  687. {
  688. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  689. if (op.F)
  690. {
  691. if (Optimizations.FastFP && Optimizations.UseSse2)
  692. {
  693. EmitVectorsByScalarOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd, Intrinsic.X86Subps, Intrinsic.X86Subpd);
  694. }
  695. else if (Optimizations.FastFP)
  696. {
  697. EmitVectorsByScalarOpF32(context, (op1, op2, op3) => context.Subtract(op1, context.Multiply(op2, op3)));
  698. }
  699. else
  700. {
  701. EmitVectorsByScalarOpF32(context, (op1, op2, op3) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulSubFpscr), op1, op2, op3));
  702. }
  703. }
  704. else
  705. {
  706. EmitVectorsByScalarOpI32(context, (op1, op2, op3) => context.Subtract(op1, context.Multiply(op2, op3)), false);
  707. }
  708. }
  709. public static void Vmlsl_I(ArmEmitterContext context)
  710. {
  711. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  712. EmitVectorTernaryLongOpI32(context, (opD, op1, op2) => context.Subtract(opD, context.Multiply(op1, op2)), !op.U);
  713. }
  714. public static void Vmul_S(ArmEmitterContext context)
  715. {
  716. if (Optimizations.FastFP && Optimizations.UseSse2)
  717. {
  718. EmitScalarBinaryOpF32(context, Intrinsic.X86Mulss, Intrinsic.X86Mulsd);
  719. }
  720. else if (Optimizations.FastFP)
  721. {
  722. EmitScalarBinaryOpF32(context, (op1, op2) => context.Multiply(op1, op2));
  723. }
  724. else
  725. {
  726. EmitScalarBinaryOpF32(context, (op1, op2) =>
  727. {
  728. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul), op1, op2);
  729. });
  730. }
  731. }
  732. public static void Vmul_V(ArmEmitterContext context)
  733. {
  734. if (Optimizations.FastFP && Optimizations.UseSse2)
  735. {
  736. EmitVectorBinaryOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd);
  737. }
  738. else if (Optimizations.FastFP)
  739. {
  740. EmitVectorBinaryOpF32(context, (op1, op2) => context.Multiply(op1, op2));
  741. }
  742. else
  743. {
  744. EmitVectorBinaryOpF32(context, (op1, op2) =>
  745. {
  746. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulFpscr), op1, op2);
  747. });
  748. }
  749. }
  750. public static void Vmul_I(ArmEmitterContext context)
  751. {
  752. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  753. if (op.U) // This instruction is always signed, U indicates polynomial mode.
  754. {
  755. EmitVectorBinaryOpZx32(context, (op1, op2) => EmitPolynomialMultiply(context, op1, op2, 8 << op.Size));
  756. }
  757. else
  758. {
  759. EmitVectorBinaryOpSx32(context, (op1, op2) => context.Multiply(op1, op2));
  760. }
  761. }
  762. public static void Vmul_1(ArmEmitterContext context)
  763. {
  764. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  765. if (op.F)
  766. {
  767. if (Optimizations.FastFP && Optimizations.UseSse2)
  768. {
  769. EmitVectorByScalarOpF32(context, Intrinsic.X86Mulps, Intrinsic.X86Mulpd);
  770. }
  771. else if (Optimizations.FastFP)
  772. {
  773. EmitVectorByScalarOpF32(context, (op1, op2) => context.Multiply(op1, op2));
  774. }
  775. else
  776. {
  777. EmitVectorByScalarOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMulFpscr), op1, op2));
  778. }
  779. }
  780. else
  781. {
  782. EmitVectorByScalarOpI32(context, (op1, op2) => context.Multiply(op1, op2), false);
  783. }
  784. }
  785. public static void Vmull_1(ArmEmitterContext context)
  786. {
  787. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  788. EmitVectorByScalarLongOpI32(context, (op1, op2) => context.Multiply(op1, op2), !op.U);
  789. }
  790. public static void Vmull_I(ArmEmitterContext context)
  791. {
  792. OpCode32SimdRegLong op = (OpCode32SimdRegLong)context.CurrOp;
  793. if (op.Polynomial)
  794. {
  795. EmitVectorBinaryLongOpI32(context, (op1, op2) => EmitPolynomialMultiply(context, op1, op2, 8 << op.Size), false);
  796. }
  797. else
  798. {
  799. EmitVectorBinaryLongOpI32(context, (op1, op2) => context.Multiply(op1, op2), !op.U);
  800. }
  801. }
  802. public static void Vpadd_V(ArmEmitterContext context)
  803. {
  804. if (Optimizations.FastFP && Optimizations.UseSse2)
  805. {
  806. EmitSse2VectorPairwiseOpF32(context, Intrinsic.X86Addps);
  807. }
  808. else
  809. {
  810. EmitVectorPairwiseOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPAddFpscr), op1, op2));
  811. }
  812. }
  813. public static void Vpadd_I(ArmEmitterContext context)
  814. {
  815. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  816. if (Optimizations.UseSsse3)
  817. {
  818. EmitSsse3VectorPairwiseOp32(context, X86PaddInstruction);
  819. }
  820. else
  821. {
  822. EmitVectorPairwiseOpI32(context, (op1, op2) => context.Add(op1, op2), !op.U);
  823. }
  824. }
  825. public static void Vpmax_V(ArmEmitterContext context)
  826. {
  827. if (Optimizations.FastFP && Optimizations.UseSse2)
  828. {
  829. EmitSse2VectorPairwiseOpF32(context, Intrinsic.X86Maxps);
  830. }
  831. else
  832. {
  833. EmitVectorPairwiseOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat64.FPMaxFpscr), op1, op2));
  834. }
  835. }
  836. public static void Vpmax_I(ArmEmitterContext context)
  837. {
  838. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  839. if (Optimizations.UseSsse3)
  840. {
  841. EmitSsse3VectorPairwiseOp32(context, op.U ? X86PmaxuInstruction : X86PmaxsInstruction);
  842. }
  843. else
  844. {
  845. EmitVectorPairwiseOpI32(context, (op1, op2) =>
  846. {
  847. Operand greater = op.U ? context.ICompareGreaterUI(op1, op2) : context.ICompareGreater(op1, op2);
  848. return context.ConditionalSelect(greater, op1, op2);
  849. }, !op.U);
  850. }
  851. }
  852. public static void Vpmin_V(ArmEmitterContext context)
  853. {
  854. if (Optimizations.FastFP && Optimizations.UseSse2)
  855. {
  856. EmitSse2VectorPairwiseOpF32(context, Intrinsic.X86Minps);
  857. }
  858. else
  859. {
  860. EmitVectorPairwiseOpF32(context, (op1, op2) => EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPMinFpscr), op1, op2));
  861. }
  862. }
  863. public static void Vpmin_I(ArmEmitterContext context)
  864. {
  865. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  866. if (Optimizations.UseSsse3)
  867. {
  868. EmitSsse3VectorPairwiseOp32(context, op.U ? X86PminuInstruction : X86PminsInstruction);
  869. }
  870. else
  871. {
  872. EmitVectorPairwiseOpI32(context, (op1, op2) =>
  873. {
  874. Operand greater = op.U ? context.ICompareLessUI(op1, op2) : context.ICompareLess(op1, op2);
  875. return context.ConditionalSelect(greater, op1, op2);
  876. }, !op.U);
  877. }
  878. }
  879. public static void Vrev(ArmEmitterContext context)
  880. {
  881. OpCode32SimdRev op = (OpCode32SimdRev)context.CurrOp;
  882. if (Optimizations.UseSsse3)
  883. {
  884. EmitVectorUnaryOpSimd32(context, (op1) =>
  885. {
  886. Operand mask;
  887. switch (op.Size)
  888. {
  889. case 3:
  890. // Rev64
  891. switch (op.Opc)
  892. {
  893. case 0:
  894. mask = X86GetElements(context, 0x08090a0b0c0d0e0fL, 0x0001020304050607L);
  895. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  896. case 1:
  897. mask = X86GetElements(context, 0x09080b0a0d0c0f0eL, 0x0100030205040706L);
  898. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  899. case 2:
  900. return context.AddIntrinsic(Intrinsic.X86Shufps, op1, op1, Const(1 | (0 << 2) | (3 << 4) | (2 << 6)));
  901. }
  902. break;
  903. case 2:
  904. // Rev32
  905. switch (op.Opc)
  906. {
  907. case 0:
  908. mask = X86GetElements(context, 0x0c0d0e0f_08090a0bL, 0x04050607_00010203L);
  909. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  910. case 1:
  911. mask = X86GetElements(context, 0x0d0c0f0e_09080b0aL, 0x05040706_01000302L);
  912. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  913. }
  914. break;
  915. case 1:
  916. // Rev16
  917. mask = X86GetElements(context, 0x0e0f_0c0d_0a0b_0809L, 0x_0607_0405_0203_0001L);
  918. return context.AddIntrinsic(Intrinsic.X86Pshufb, op1, mask);
  919. }
  920. throw new InvalidOperationException("Invalid VREV Opcode + Size combo."); // Should be unreachable.
  921. });
  922. }
  923. else
  924. {
  925. EmitVectorUnaryOpZx32(context, (op1) =>
  926. {
  927. switch (op.Opc)
  928. {
  929. case 0:
  930. switch (op.Size) // Swap bytes.
  931. {
  932. case 1:
  933. return InstEmitAluHelper.EmitReverseBytes16_32Op(context, op1);
  934. case 2:
  935. case 3:
  936. return context.ByteSwap(op1);
  937. }
  938. break;
  939. case 1:
  940. switch (op.Size)
  941. {
  942. case 2:
  943. return context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op1, Const(0xffff0000)), Const(16)),
  944. context.ShiftLeft(context.BitwiseAnd(op1, Const(0x0000ffff)), Const(16)));
  945. case 3:
  946. return context.BitwiseOr(
  947. context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op1, Const(0xffff000000000000ul)), Const(48)),
  948. context.ShiftLeft(context.BitwiseAnd(op1, Const(0x000000000000fffful)), Const(48))),
  949. context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op1, Const(0x0000ffff00000000ul)), Const(16)),
  950. context.ShiftLeft(context.BitwiseAnd(op1, Const(0x00000000ffff0000ul)), Const(16))));
  951. }
  952. break;
  953. case 2:
  954. // Swap upper and lower halves.
  955. return context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op1, Const(0xffffffff00000000ul)), Const(32)),
  956. context.ShiftLeft(context.BitwiseAnd(op1, Const(0x00000000fffffffful)), Const(32)));
  957. }
  958. throw new InvalidOperationException("Invalid VREV Opcode + Size combo."); // Should be unreachable.
  959. });
  960. }
  961. }
  962. public static void Vrecpe(ArmEmitterContext context)
  963. {
  964. OpCode32SimdSqrte op = (OpCode32SimdSqrte)context.CurrOp;
  965. if (op.F)
  966. {
  967. int sizeF = op.Size & 1;
  968. if (Optimizations.FastFP && Optimizations.UseSse2 && sizeF == 0)
  969. {
  970. EmitVectorUnaryOpF32(context, Intrinsic.X86Rcpps, 0);
  971. }
  972. else
  973. {
  974. EmitVectorUnaryOpF32(context, (op1) =>
  975. {
  976. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPRecipEstimateFpscr), op1);
  977. });
  978. }
  979. }
  980. else
  981. {
  982. throw new NotImplementedException("Integer Vrecpe not currently implemented.");
  983. }
  984. }
  985. public static void Vrecps(ArmEmitterContext context)
  986. {
  987. if (Optimizations.FastFP && Optimizations.UseSse2)
  988. {
  989. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  990. bool single = (op.Size & 1) == 0;
  991. // (2 - (n*m))
  992. EmitVectorBinaryOpSimd32(context, (n, m) =>
  993. {
  994. if (single)
  995. {
  996. Operand maskTwo = X86GetAllElements(context, 2f);
  997. Operand res = context.AddIntrinsic(Intrinsic.X86Mulps, n, m);
  998. return context.AddIntrinsic(Intrinsic.X86Subps, maskTwo, res);
  999. }
  1000. else
  1001. {
  1002. Operand maskTwo = X86GetAllElements(context, 2d);
  1003. Operand res = context.AddIntrinsic(Intrinsic.X86Mulpd, n, m);
  1004. return context.AddIntrinsic(Intrinsic.X86Subpd, maskTwo, res);
  1005. }
  1006. });
  1007. }
  1008. else
  1009. {
  1010. EmitVectorBinaryOpF32(context, (op1, op2) =>
  1011. {
  1012. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipStep), op1, op2);
  1013. });
  1014. }
  1015. }
  1016. public static void Vrsqrte(ArmEmitterContext context)
  1017. {
  1018. OpCode32SimdSqrte op = (OpCode32SimdSqrte)context.CurrOp;
  1019. if (op.F)
  1020. {
  1021. int sizeF = op.Size & 1;
  1022. if (Optimizations.FastFP && Optimizations.UseSse2 && sizeF == 0)
  1023. {
  1024. EmitVectorUnaryOpF32(context, Intrinsic.X86Rsqrtps, 0);
  1025. }
  1026. else
  1027. {
  1028. EmitVectorUnaryOpF32(context, (op1) =>
  1029. {
  1030. return EmitSoftFloatCallDefaultFpscr(context, nameof(SoftFloat32.FPRSqrtEstimateFpscr), op1);
  1031. });
  1032. }
  1033. }
  1034. else
  1035. {
  1036. throw new NotImplementedException("Integer Vrsqrte not currently implemented.");
  1037. }
  1038. }
  1039. public static void Vrsqrts(ArmEmitterContext context)
  1040. {
  1041. if (Optimizations.FastFP && Optimizations.UseSse2)
  1042. {
  1043. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  1044. bool single = (op.Size & 1) == 0;
  1045. // (3 - (n*m)) / 2
  1046. EmitVectorBinaryOpSimd32(context, (n, m) =>
  1047. {
  1048. if (single)
  1049. {
  1050. Operand maskHalf = X86GetAllElements(context, 0.5f);
  1051. Operand maskThree = X86GetAllElements(context, 3f);
  1052. Operand res = context.AddIntrinsic(Intrinsic.X86Mulps, n, m);
  1053. res = context.AddIntrinsic(Intrinsic.X86Subps, maskThree, res);
  1054. return context.AddIntrinsic(Intrinsic.X86Mulps, maskHalf, res);
  1055. }
  1056. else
  1057. {
  1058. Operand maskHalf = X86GetAllElements(context, 0.5d);
  1059. Operand maskThree = X86GetAllElements(context, 3d);
  1060. Operand res = context.AddIntrinsic(Intrinsic.X86Mulpd, n, m);
  1061. res = context.AddIntrinsic(Intrinsic.X86Subpd, maskThree, res);
  1062. return context.AddIntrinsic(Intrinsic.X86Mulpd, maskHalf, res);
  1063. }
  1064. });
  1065. }
  1066. else
  1067. {
  1068. EmitVectorBinaryOpF32(context, (op1, op2) =>
  1069. {
  1070. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtStep), op1, op2);
  1071. });
  1072. }
  1073. }
  1074. public static void Vsel(ArmEmitterContext context)
  1075. {
  1076. OpCode32SimdSel op = (OpCode32SimdSel)context.CurrOp;
  1077. Operand condition = null;
  1078. switch (op.Cc)
  1079. {
  1080. case OpCode32SimdSelMode.Eq:
  1081. condition = GetCondTrue(context, Condition.Eq);
  1082. break;
  1083. case OpCode32SimdSelMode.Ge:
  1084. condition = GetCondTrue(context, Condition.Ge);
  1085. break;
  1086. case OpCode32SimdSelMode.Gt:
  1087. condition = GetCondTrue(context, Condition.Gt);
  1088. break;
  1089. case OpCode32SimdSelMode.Vs:
  1090. condition = GetCondTrue(context, Condition.Vs);
  1091. break;
  1092. }
  1093. EmitScalarBinaryOpI32(context, (op1, op2) =>
  1094. {
  1095. return context.ConditionalSelect(condition, op1, op2);
  1096. });
  1097. }
  1098. public static void Vsqrt_S(ArmEmitterContext context)
  1099. {
  1100. if (Optimizations.FastFP && Optimizations.UseSse2)
  1101. {
  1102. EmitScalarUnaryOpF32(context, Intrinsic.X86Sqrtss, Intrinsic.X86Sqrtsd);
  1103. }
  1104. else
  1105. {
  1106. EmitScalarUnaryOpF32(context, (op1) =>
  1107. {
  1108. return EmitSoftFloatCall(context, nameof(SoftFloat32.FPSqrt), op1);
  1109. });
  1110. }
  1111. }
  1112. public static void Vsub_S(ArmEmitterContext context)
  1113. {
  1114. if (Optimizations.FastFP && Optimizations.UseSse2)
  1115. {
  1116. EmitScalarBinaryOpF32(context, Intrinsic.X86Subss, Intrinsic.X86Subsd);
  1117. }
  1118. else
  1119. {
  1120. EmitScalarBinaryOpF32(context, (op1, op2) => context.Subtract(op1, op2));
  1121. }
  1122. }
  1123. public static void Vsub_V(ArmEmitterContext context)
  1124. {
  1125. if (Optimizations.FastFP && Optimizations.UseSse2)
  1126. {
  1127. EmitVectorBinaryOpF32(context, Intrinsic.X86Subps, Intrinsic.X86Subpd);
  1128. }
  1129. else
  1130. {
  1131. EmitVectorBinaryOpF32(context, (op1, op2) => context.Subtract(op1, op2));
  1132. }
  1133. }
  1134. public static void Vsub_I(ArmEmitterContext context)
  1135. {
  1136. if (Optimizations.UseSse2)
  1137. {
  1138. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  1139. EmitVectorBinaryOpSimd32(context, (op1, op2) => context.AddIntrinsic(X86PsubInstruction[op.Size], op1, op2));
  1140. }
  1141. else
  1142. {
  1143. EmitVectorBinaryOpZx32(context, (op1, op2) => context.Subtract(op1, op2));
  1144. }
  1145. }
  1146. public static void Vsubw_I(ArmEmitterContext context)
  1147. {
  1148. OpCode32SimdRegWide op = (OpCode32SimdRegWide)context.CurrOp;
  1149. EmitVectorBinaryWideOpI32(context, (op1, op2) => context.Subtract(op1, op2), !op.U);
  1150. }
  1151. private static void EmitSse41MaxMinNumOpF32(ArmEmitterContext context, bool isMaxNum, bool scalar)
  1152. {
  1153. IOpCode32Simd op = (IOpCode32Simd)context.CurrOp;
  1154. Func<Operand, Operand, Operand> genericEmit = (n, m) =>
  1155. {
  1156. Operand nNum = context.Copy(n);
  1157. Operand mNum = context.Copy(m);
  1158. InstEmit.EmitSse2VectorIsNaNOpF(context, nNum, out Operand nQNaNMask, out _, isQNaN: true);
  1159. InstEmit.EmitSse2VectorIsNaNOpF(context, mNum, out Operand mQNaNMask, out _, isQNaN: true);
  1160. int sizeF = op.Size & 1;
  1161. if (sizeF == 0)
  1162. {
  1163. Operand negInfMask = X86GetAllElements(context, isMaxNum ? float.NegativeInfinity : float.PositiveInfinity);
  1164. Operand nMask = context.AddIntrinsic(Intrinsic.X86Andnps, mQNaNMask, nQNaNMask);
  1165. Operand mMask = context.AddIntrinsic(Intrinsic.X86Andnps, nQNaNMask, mQNaNMask);
  1166. nNum = context.AddIntrinsic(Intrinsic.X86Blendvps, nNum, negInfMask, nMask);
  1167. mNum = context.AddIntrinsic(Intrinsic.X86Blendvps, mNum, negInfMask, mMask);
  1168. return context.AddIntrinsic(isMaxNum ? Intrinsic.X86Maxps : Intrinsic.X86Minps, nNum, mNum);
  1169. }
  1170. else /* if (sizeF == 1) */
  1171. {
  1172. Operand negInfMask = X86GetAllElements(context, isMaxNum ? double.NegativeInfinity : double.PositiveInfinity);
  1173. Operand nMask = context.AddIntrinsic(Intrinsic.X86Andnpd, mQNaNMask, nQNaNMask);
  1174. Operand mMask = context.AddIntrinsic(Intrinsic.X86Andnpd, nQNaNMask, mQNaNMask);
  1175. nNum = context.AddIntrinsic(Intrinsic.X86Blendvpd, nNum, negInfMask, nMask);
  1176. mNum = context.AddIntrinsic(Intrinsic.X86Blendvpd, mNum, negInfMask, mMask);
  1177. return context.AddIntrinsic(isMaxNum ? Intrinsic.X86Maxpd : Intrinsic.X86Minpd, nNum, mNum);
  1178. }
  1179. };
  1180. if (scalar)
  1181. {
  1182. EmitScalarBinaryOpSimd32(context, genericEmit);
  1183. }
  1184. else
  1185. {
  1186. EmitVectorBinaryOpSimd32(context, genericEmit);
  1187. }
  1188. }
  1189. private static Operand EmitPolynomialMultiply(ArmEmitterContext context, Operand op1, Operand op2, int eSize)
  1190. {
  1191. Debug.Assert(eSize <= 32);
  1192. Operand result = eSize == 32 ? Const(0L) : Const(0);
  1193. if (eSize == 32)
  1194. {
  1195. op1 = context.ZeroExtend32(OperandType.I64, op1);
  1196. op2 = context.ZeroExtend32(OperandType.I64, op2);
  1197. }
  1198. for (int i = 0; i < eSize; i++)
  1199. {
  1200. Operand mask = context.BitwiseAnd(op1, Const(op1.Type, 1L << i));
  1201. result = context.BitwiseExclusiveOr(result, context.Multiply(op2, mask));
  1202. }
  1203. return result;
  1204. }
  1205. }
  1206. }