InstEmitSimdArithmetic32.cs 48 KB

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