InstEmitAlu.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
  6. using static Ryujinx.Graphics.Shader.Instructions.InstEmitAluHelper;
  7. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  8. namespace Ryujinx.Graphics.Shader.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void Bfe(EmitterContext context)
  13. {
  14. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  15. bool isReverse = op.RawOpCode.Extract(40);
  16. bool isSigned = op.RawOpCode.Extract(48);
  17. Operand srcA = GetSrcA(context);
  18. Operand srcB = GetSrcB(context);
  19. if (isReverse)
  20. {
  21. srcA = context.BitfieldReverse(srcA);
  22. }
  23. Operand position = context.BitwiseAnd(srcB, Const(0xff));
  24. Operand size = context.BitfieldExtractU32(srcB, Const(8), Const(8));
  25. Operand res = isSigned
  26. ? context.BitfieldExtractS32(srcA, position, size)
  27. : context.BitfieldExtractU32(srcA, position, size);
  28. context.Copy(GetDest(context), res);
  29. // TODO: CC, X, corner cases
  30. }
  31. public static void Bfi(EmitterContext context)
  32. {
  33. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  34. Operand srcA = GetSrcA(context);
  35. Operand srcB = GetSrcB(context);
  36. Operand srcC = GetSrcC(context);
  37. Operand position = context.BitwiseAnd(srcB, Const(0xff));
  38. Operand size = context.BitfieldExtractU32(srcB, Const(8), Const(8));
  39. Operand res = context.BitfieldInsert(srcC, srcA, position, size);
  40. context.Copy(GetDest(context), res);
  41. }
  42. public static void Csetp(EmitterContext context)
  43. {
  44. OpCodePset op = (OpCodePset)context.CurrOp;
  45. // TODO: Implement that properly
  46. Operand p0Res = Const(IrConsts.True);
  47. Operand p1Res = context.BitwiseNot(p0Res);
  48. Operand pred = GetPredicate39(context);
  49. p0Res = GetPredLogicalOp(context, op.LogicalOp, p0Res, pred);
  50. p1Res = GetPredLogicalOp(context, op.LogicalOp, p1Res, pred);
  51. context.Copy(Register(op.Predicate3), p0Res);
  52. context.Copy(Register(op.Predicate0), p1Res);
  53. }
  54. public static void Flo(EmitterContext context)
  55. {
  56. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  57. bool invert = op.RawOpCode.Extract(40);
  58. bool countZeros = op.RawOpCode.Extract(41);
  59. bool isSigned = op.RawOpCode.Extract(48);
  60. Operand srcB = context.BitwiseNot(GetSrcB(context), invert);
  61. Operand res = isSigned
  62. ? context.FindFirstSetS32(srcB)
  63. : context.FindFirstSetU32(srcB);
  64. if (countZeros)
  65. {
  66. res = context.BitwiseExclusiveOr(res, Const(31));
  67. }
  68. context.Copy(GetDest(context), res);
  69. }
  70. public static void Iadd(EmitterContext context)
  71. {
  72. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  73. bool negateA = false, negateB = false;
  74. if (!(op is OpCodeAluImm32))
  75. {
  76. negateB = op.RawOpCode.Extract(48);
  77. negateA = op.RawOpCode.Extract(49);
  78. }
  79. else
  80. {
  81. // TODO: Other IADD32I variant without the negate.
  82. negateA = op.RawOpCode.Extract(56);
  83. }
  84. Operand srcA = context.INegate(GetSrcA(context), negateA);
  85. Operand srcB = context.INegate(GetSrcB(context), negateB);
  86. Operand res = context.IAdd(srcA, srcB);
  87. bool isSubtraction = negateA || negateB;
  88. if (op.Extended)
  89. {
  90. // Add carry, or subtract borrow.
  91. res = context.IAdd(res, isSubtraction
  92. ? context.BitwiseNot(GetCF(context))
  93. : context.BitwiseAnd(GetCF(context), Const(1)));
  94. }
  95. SetIaddFlags(context, res, srcA, srcB, op.SetCondCode, op.Extended, isSubtraction);
  96. context.Copy(GetDest(context), res);
  97. }
  98. public static void Iadd3(EmitterContext context)
  99. {
  100. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  101. IntegerHalfPart partC = (IntegerHalfPart)op.RawOpCode.Extract(31, 2);
  102. IntegerHalfPart partB = (IntegerHalfPart)op.RawOpCode.Extract(33, 2);
  103. IntegerHalfPart partA = (IntegerHalfPart)op.RawOpCode.Extract(35, 2);
  104. IntegerShift mode = (IntegerShift)op.RawOpCode.Extract(37, 2);
  105. bool negateC = op.RawOpCode.Extract(49);
  106. bool negateB = op.RawOpCode.Extract(50);
  107. bool negateA = op.RawOpCode.Extract(51);
  108. Operand Extend(Operand src, IntegerHalfPart part)
  109. {
  110. if (!(op is OpCodeAluReg) || part == IntegerHalfPart.B32)
  111. {
  112. return src;
  113. }
  114. if (part == IntegerHalfPart.H0)
  115. {
  116. return context.BitwiseAnd(src, Const(0xffff));
  117. }
  118. else if (part == IntegerHalfPart.H1)
  119. {
  120. return context.ShiftRightU32(src, Const(16));
  121. }
  122. else
  123. {
  124. // TODO: Warning.
  125. }
  126. return src;
  127. }
  128. Operand srcA = context.INegate(Extend(GetSrcA(context), partA), negateA);
  129. Operand srcB = context.INegate(Extend(GetSrcB(context), partB), negateB);
  130. Operand srcC = context.INegate(Extend(GetSrcC(context), partC), negateC);
  131. Operand res = context.IAdd(srcA, srcB);
  132. if (op is OpCodeAluReg && mode != IntegerShift.NoShift)
  133. {
  134. if (mode == IntegerShift.ShiftLeft)
  135. {
  136. res = context.ShiftLeft(res, Const(16));
  137. }
  138. else if (mode == IntegerShift.ShiftRight)
  139. {
  140. res = context.ShiftRightU32(res, Const(16));
  141. }
  142. else
  143. {
  144. // TODO: Warning.
  145. }
  146. }
  147. res = context.IAdd(res, srcC);
  148. context.Copy(GetDest(context), res);
  149. // TODO: CC, X, corner cases
  150. }
  151. public static void Imnmx(EmitterContext context)
  152. {
  153. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  154. bool isSignedInt = op.RawOpCode.Extract(48);
  155. Operand srcA = GetSrcA(context);
  156. Operand srcB = GetSrcB(context);
  157. Operand resMin = isSignedInt
  158. ? context.IMinimumS32(srcA, srcB)
  159. : context.IMinimumU32(srcA, srcB);
  160. Operand resMax = isSignedInt
  161. ? context.IMaximumS32(srcA, srcB)
  162. : context.IMaximumU32(srcA, srcB);
  163. Operand pred = GetPredicate39(context);
  164. Operand dest = GetDest(context);
  165. context.Copy(dest, context.ConditionalSelect(pred, resMin, resMax));
  166. SetZnFlags(context, dest, op.SetCondCode);
  167. // TODO: X flags.
  168. }
  169. public static void Iscadd(EmitterContext context)
  170. {
  171. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  172. bool negateA = false, negateB = false;
  173. if (!(op is OpCodeAluImm32))
  174. {
  175. negateB = op.RawOpCode.Extract(48);
  176. negateA = op.RawOpCode.Extract(49);
  177. }
  178. int shift = op is OpCodeAluImm32
  179. ? op.RawOpCode.Extract(53, 5)
  180. : op.RawOpCode.Extract(39, 5);
  181. Operand srcA = GetSrcA(context);
  182. Operand srcB = GetSrcB(context);
  183. srcA = context.ShiftLeft(srcA, Const(shift));
  184. srcA = context.INegate(srcA, negateA);
  185. srcB = context.INegate(srcB, negateB);
  186. Operand res = context.IAdd(srcA, srcB);
  187. context.Copy(GetDest(context), res);
  188. // TODO: CC, X
  189. }
  190. public static void Iset(EmitterContext context)
  191. {
  192. OpCodeSet op = (OpCodeSet)context.CurrOp;
  193. bool boolFloat = op.RawOpCode.Extract(44);
  194. bool isSigned = op.RawOpCode.Extract(48);
  195. IntegerCondition cmpOp = (IntegerCondition)op.RawOpCode.Extract(49, 3);
  196. Operand srcA = GetSrcA(context);
  197. Operand srcB = GetSrcB(context);
  198. Operand res = GetIntComparison(context, cmpOp, srcA, srcB, isSigned);
  199. Operand pred = GetPredicate39(context);
  200. res = GetPredLogicalOp(context, op.LogicalOp, res, pred);
  201. Operand dest = GetDest(context);
  202. if (boolFloat)
  203. {
  204. context.Copy(dest, context.ConditionalSelect(res, ConstF(1), Const(0)));
  205. }
  206. else
  207. {
  208. context.Copy(dest, res);
  209. }
  210. // TODO: CC, X
  211. }
  212. public static void Isetp(EmitterContext context)
  213. {
  214. OpCodeSet op = (OpCodeSet)context.CurrOp;
  215. bool isSigned = op.RawOpCode.Extract(48);
  216. IntegerCondition cmpOp = (IntegerCondition)op.RawOpCode.Extract(49, 3);
  217. Operand srcA = GetSrcA(context);
  218. Operand srcB = GetSrcB(context);
  219. Operand p0Res = GetIntComparison(context, cmpOp, srcA, srcB, isSigned);
  220. Operand p1Res = context.BitwiseNot(p0Res);
  221. Operand pred = GetPredicate39(context);
  222. p0Res = GetPredLogicalOp(context, op.LogicalOp, p0Res, pred);
  223. p1Res = GetPredLogicalOp(context, op.LogicalOp, p1Res, pred);
  224. context.Copy(Register(op.Predicate3), p0Res);
  225. context.Copy(Register(op.Predicate0), p1Res);
  226. }
  227. public static void Lop(EmitterContext context)
  228. {
  229. IOpCodeLop op = (IOpCodeLop)context.CurrOp;
  230. Operand srcA = context.BitwiseNot(GetSrcA(context), op.InvertA);
  231. Operand srcB = context.BitwiseNot(GetSrcB(context), op.InvertB);
  232. Operand res = srcB;
  233. switch (op.LogicalOp)
  234. {
  235. case LogicalOperation.And: res = context.BitwiseAnd (srcA, srcB); break;
  236. case LogicalOperation.Or: res = context.BitwiseOr (srcA, srcB); break;
  237. case LogicalOperation.ExclusiveOr: res = context.BitwiseExclusiveOr(srcA, srcB); break;
  238. }
  239. EmitLopPredWrite(context, op, res);
  240. Operand dest = GetDest(context);
  241. context.Copy(dest, res);
  242. SetZnFlags(context, dest, op.SetCondCode, op.Extended);
  243. }
  244. public static void Lop3(EmitterContext context)
  245. {
  246. IOpCodeLop op = (IOpCodeLop)context.CurrOp;
  247. Operand srcA = GetSrcA(context);
  248. Operand srcB = GetSrcB(context);
  249. Operand srcC = GetSrcC(context);
  250. bool regVariant = op is OpCodeLopReg;
  251. int truthTable = regVariant
  252. ? op.RawOpCode.Extract(28, 8)
  253. : op.RawOpCode.Extract(48, 8);
  254. Operand res = Lop3Expression.GetFromTruthTable(context, srcA, srcB, srcC, truthTable);
  255. if (regVariant)
  256. {
  257. EmitLopPredWrite(context, op, res);
  258. }
  259. Operand dest = GetDest(context);
  260. context.Copy(dest, res);
  261. SetZnFlags(context, dest, op.SetCondCode, op.Extended);
  262. }
  263. public static void Popc(EmitterContext context)
  264. {
  265. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  266. bool invert = op.RawOpCode.Extract(40);
  267. Operand srcB = context.BitwiseNot(GetSrcB(context), invert);
  268. Operand res = context.BitCount(srcB);
  269. context.Copy(GetDest(context), res);
  270. }
  271. public static void Pset(EmitterContext context)
  272. {
  273. OpCodePset op = (OpCodePset)context.CurrOp;
  274. bool boolFloat = op.RawOpCode.Extract(44);
  275. Operand srcA = context.BitwiseNot(Register(op.Predicate12), op.InvertA);
  276. Operand srcB = context.BitwiseNot(Register(op.Predicate29), op.InvertB);
  277. Operand srcC = context.BitwiseNot(Register(op.Predicate39), op.InvertP);
  278. Operand res = GetPredLogicalOp(context, op.LogicalOpAB, srcA, srcB);
  279. res = GetPredLogicalOp(context, op.LogicalOp, res, srcC);
  280. Operand dest = GetDest(context);
  281. if (boolFloat)
  282. {
  283. context.Copy(dest, context.ConditionalSelect(res, ConstF(1), Const(0)));
  284. }
  285. else
  286. {
  287. context.Copy(dest, res);
  288. }
  289. }
  290. public static void Psetp(EmitterContext context)
  291. {
  292. OpCodePset op = (OpCodePset)context.CurrOp;
  293. Operand srcA = context.BitwiseNot(Register(op.Predicate12), op.InvertA);
  294. Operand srcB = context.BitwiseNot(Register(op.Predicate29), op.InvertB);
  295. Operand p0Res = GetPredLogicalOp(context, op.LogicalOpAB, srcA, srcB);
  296. Operand p1Res = context.BitwiseNot(p0Res);
  297. Operand pred = GetPredicate39(context);
  298. p0Res = GetPredLogicalOp(context, op.LogicalOp, p0Res, pred);
  299. p1Res = GetPredLogicalOp(context, op.LogicalOp, p1Res, pred);
  300. context.Copy(Register(op.Predicate3), p0Res);
  301. context.Copy(Register(op.Predicate0), p1Res);
  302. }
  303. public static void Rro(EmitterContext context)
  304. {
  305. // This is the range reduction operator,
  306. // we translate it as a simple move, as it
  307. // should be always followed by a matching
  308. // MUFU instruction.
  309. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  310. bool negateB = op.RawOpCode.Extract(45);
  311. bool absoluteB = op.RawOpCode.Extract(49);
  312. Operand srcB = GetSrcB(context);
  313. srcB = context.FPAbsNeg(srcB, absoluteB, negateB);
  314. context.Copy(GetDest(context), srcB);
  315. }
  316. public static void Shl(EmitterContext context)
  317. {
  318. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  319. bool isMasked = op.RawOpCode.Extract(39);
  320. Operand srcB = GetSrcB(context);
  321. if (isMasked)
  322. {
  323. srcB = context.BitwiseAnd(srcB, Const(0x1f));
  324. }
  325. Operand res = context.ShiftLeft(GetSrcA(context), srcB);
  326. if (!isMasked)
  327. {
  328. // Clamped shift value.
  329. Operand isLessThan32 = context.ICompareLessUnsigned(srcB, Const(32));
  330. res = context.ConditionalSelect(isLessThan32, res, Const(0));
  331. }
  332. // TODO: X, CC
  333. context.Copy(GetDest(context), res);
  334. }
  335. public static void Shr(EmitterContext context)
  336. {
  337. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  338. bool isMasked = op.RawOpCode.Extract(39);
  339. bool isReverse = op.RawOpCode.Extract(40);
  340. bool isSigned = op.RawOpCode.Extract(48);
  341. Operand srcA = GetSrcA(context);
  342. Operand srcB = GetSrcB(context);
  343. if (isReverse)
  344. {
  345. srcA = context.BitfieldReverse(srcA);
  346. }
  347. if (isMasked)
  348. {
  349. srcB = context.BitwiseAnd(srcB, Const(0x1f));
  350. }
  351. Operand res = isSigned
  352. ? context.ShiftRightS32(srcA, srcB)
  353. : context.ShiftRightU32(srcA, srcB);
  354. if (!isMasked)
  355. {
  356. // Clamped shift value.
  357. Operand resShiftBy32;
  358. if (isSigned)
  359. {
  360. resShiftBy32 = context.ShiftRightS32(srcA, Const(31));
  361. }
  362. else
  363. {
  364. resShiftBy32 = Const(0);
  365. }
  366. Operand isLessThan32 = context.ICompareLessUnsigned(srcB, Const(32));
  367. res = context.ConditionalSelect(isLessThan32, res, resShiftBy32);
  368. }
  369. // TODO: X, CC
  370. context.Copy(GetDest(context), res);
  371. }
  372. public static void Xmad(EmitterContext context)
  373. {
  374. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  375. bool signedA = context.CurrOp.RawOpCode.Extract(48);
  376. bool signedB = context.CurrOp.RawOpCode.Extract(49);
  377. bool highA = context.CurrOp.RawOpCode.Extract(53);
  378. bool highB = false;
  379. XmadCMode mode;
  380. if (op is OpCodeAluReg)
  381. {
  382. highB = context.CurrOp.RawOpCode.Extract(35);
  383. mode = (XmadCMode)context.CurrOp.RawOpCode.Extract(50, 3);
  384. }
  385. else
  386. {
  387. mode = (XmadCMode)context.CurrOp.RawOpCode.Extract(50, 2);
  388. if (!(op is OpCodeAluImm))
  389. {
  390. highB = context.CurrOp.RawOpCode.Extract(52);
  391. }
  392. }
  393. Operand srcA = GetSrcA(context);
  394. Operand srcB = GetSrcB(context);
  395. Operand srcC = GetSrcC(context);
  396. // XMAD immediates are 16-bits unsigned integers.
  397. if (srcB.Type == OperandType.Constant)
  398. {
  399. srcB = Const(srcB.Value & 0xffff);
  400. }
  401. Operand Extend16To32(Operand src, bool high, bool signed)
  402. {
  403. if (signed && high)
  404. {
  405. return context.ShiftRightS32(src, Const(16));
  406. }
  407. else if (signed)
  408. {
  409. return context.BitfieldExtractS32(src, Const(0), Const(16));
  410. }
  411. else if (high)
  412. {
  413. return context.ShiftRightU32(src, Const(16));
  414. }
  415. else
  416. {
  417. return context.BitwiseAnd(src, Const(0xffff));
  418. }
  419. }
  420. srcA = Extend16To32(srcA, highA, signedA);
  421. srcB = Extend16To32(srcB, highB, signedB);
  422. bool productShiftLeft = false;
  423. bool merge = false;
  424. if (!(op is OpCodeAluRegCbuf))
  425. {
  426. productShiftLeft = context.CurrOp.RawOpCode.Extract(36);
  427. merge = context.CurrOp.RawOpCode.Extract(37);
  428. }
  429. bool extended;
  430. if ((op is OpCodeAluReg) || (op is OpCodeAluImm))
  431. {
  432. extended = context.CurrOp.RawOpCode.Extract(38);
  433. }
  434. else
  435. {
  436. extended = context.CurrOp.RawOpCode.Extract(54);
  437. }
  438. Operand res = context.IMultiply(srcA, srcB);
  439. if (productShiftLeft)
  440. {
  441. res = context.ShiftLeft(res, Const(16));
  442. }
  443. switch (mode)
  444. {
  445. case XmadCMode.Cfull: break;
  446. case XmadCMode.Clo: srcC = Extend16To32(srcC, high: false, signed: false); break;
  447. case XmadCMode.Chi: srcC = Extend16To32(srcC, high: true, signed: false); break;
  448. case XmadCMode.Cbcc:
  449. {
  450. srcC = context.IAdd(srcC, context.ShiftLeft(GetSrcB(context), Const(16)));
  451. break;
  452. }
  453. case XmadCMode.Csfu:
  454. {
  455. Operand signAdjustA = context.ShiftLeft(context.ShiftRightU32(srcA, Const(31)), Const(16));
  456. Operand signAdjustB = context.ShiftLeft(context.ShiftRightU32(srcB, Const(31)), Const(16));
  457. srcC = context.ISubtract(srcC, context.IAdd(signAdjustA, signAdjustB));
  458. break;
  459. }
  460. default: /* TODO: Warning */ break;
  461. }
  462. Operand product = res;
  463. if (extended)
  464. {
  465. // Add with carry.
  466. res = context.IAdd(res, context.BitwiseAnd(GetCF(context), Const(1)));
  467. }
  468. else
  469. {
  470. // Add (no carry in).
  471. res = context.IAdd(res, srcC);
  472. }
  473. SetIaddFlags(context, res, product, srcC, op.SetCondCode, extended);
  474. if (merge)
  475. {
  476. res = context.BitwiseAnd(res, Const(0xffff));
  477. res = context.BitwiseOr(res, context.ShiftLeft(GetSrcB(context), Const(16)));
  478. }
  479. context.Copy(GetDest(context), res);
  480. }
  481. private static Operand GetIntComparison(
  482. EmitterContext context,
  483. IntegerCondition cond,
  484. Operand srcA,
  485. Operand srcB,
  486. bool isSigned)
  487. {
  488. Operand res;
  489. if (cond == IntegerCondition.Always)
  490. {
  491. res = Const(IrConsts.True);
  492. }
  493. else if (cond == IntegerCondition.Never)
  494. {
  495. res = Const(IrConsts.False);
  496. }
  497. else
  498. {
  499. Instruction inst;
  500. switch (cond)
  501. {
  502. case IntegerCondition.Less: inst = Instruction.CompareLessU32; break;
  503. case IntegerCondition.Equal: inst = Instruction.CompareEqual; break;
  504. case IntegerCondition.LessOrEqual: inst = Instruction.CompareLessOrEqualU32; break;
  505. case IntegerCondition.Greater: inst = Instruction.CompareGreaterU32; break;
  506. case IntegerCondition.NotEqual: inst = Instruction.CompareNotEqual; break;
  507. case IntegerCondition.GreaterOrEqual: inst = Instruction.CompareGreaterOrEqualU32; break;
  508. default: throw new InvalidOperationException($"Unexpected condition \"{cond}\".");
  509. }
  510. if (isSigned)
  511. {
  512. switch (cond)
  513. {
  514. case IntegerCondition.Less: inst = Instruction.CompareLess; break;
  515. case IntegerCondition.LessOrEqual: inst = Instruction.CompareLessOrEqual; break;
  516. case IntegerCondition.Greater: inst = Instruction.CompareGreater; break;
  517. case IntegerCondition.GreaterOrEqual: inst = Instruction.CompareGreaterOrEqual; break;
  518. }
  519. }
  520. res = context.Add(inst, Local(), srcA, srcB);
  521. }
  522. return res;
  523. }
  524. private static void EmitLopPredWrite(EmitterContext context, IOpCodeLop op, Operand result)
  525. {
  526. if (op is OpCodeLop opLop && !opLop.Predicate48.IsPT)
  527. {
  528. Operand pRes;
  529. if (opLop.CondOp == ConditionalOperation.False)
  530. {
  531. pRes = Const(IrConsts.False);
  532. }
  533. else if (opLop.CondOp == ConditionalOperation.True)
  534. {
  535. pRes = Const(IrConsts.True);
  536. }
  537. else if (opLop.CondOp == ConditionalOperation.Zero)
  538. {
  539. pRes = context.ICompareEqual(result, Const(0));
  540. }
  541. else /* if (opLop.CondOp == ConditionalOperation.NotZero) */
  542. {
  543. pRes = context.ICompareNotEqual(result, Const(0));
  544. }
  545. context.Copy(Register(opLop.Predicate48), pRes);
  546. }
  547. }
  548. private static void SetIaddFlags(
  549. EmitterContext context,
  550. Operand res,
  551. Operand srcA,
  552. Operand srcB,
  553. bool setCC,
  554. bool extended,
  555. bool isSubtraction = false)
  556. {
  557. if (!setCC)
  558. {
  559. return;
  560. }
  561. if (!extended || isSubtraction)
  562. {
  563. // C = d < a
  564. context.Copy(GetCF(context), context.ICompareLessUnsigned(res, srcA));
  565. }
  566. else
  567. {
  568. // C = (d == a && CIn) || d < a
  569. Operand tempC0 = context.ICompareEqual (res, srcA);
  570. Operand tempC1 = context.ICompareLessUnsigned(res, srcA);
  571. tempC0 = context.BitwiseAnd(tempC0, GetCF(context));
  572. context.Copy(GetCF(context), context.BitwiseOr(tempC0, tempC1));
  573. }
  574. // V = (d ^ a) & ~(a ^ b) < 0
  575. Operand tempV0 = context.BitwiseExclusiveOr(res, srcA);
  576. Operand tempV1 = context.BitwiseExclusiveOr(srcA, srcB);
  577. tempV1 = context.BitwiseNot(tempV1);
  578. Operand tempV = context.BitwiseAnd(tempV0, tempV1);
  579. context.Copy(GetVF(context), context.ICompareLess(tempV, Const(0)));
  580. SetZnFlags(context, res, setCC: true, extended: extended);
  581. }
  582. }
  583. }