InstEmitSimdArithmetic32.cs 50 KB

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