InstEmitIntegerArithmetic.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using static Ryujinx.Graphics.Shader.Instructions.InstEmitAluHelper;
  5. using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
  6. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  7. namespace Ryujinx.Graphics.Shader.Instructions
  8. {
  9. static partial class InstEmit
  10. {
  11. public static void IaddR(EmitterContext context)
  12. {
  13. InstIaddR op = context.GetOp<InstIaddR>();
  14. Operand srcA = GetSrcReg(context, op.SrcA);
  15. Operand srcB = GetSrcReg(context, op.SrcB);
  16. EmitIadd(context, srcA, srcB, op.Dest, op.AvgMode, op.X, op.WriteCC);
  17. }
  18. public static void IaddI(EmitterContext context)
  19. {
  20. InstIaddI op = context.GetOp<InstIaddI>();
  21. Operand srcA = GetSrcReg(context, op.SrcA);
  22. Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  23. EmitIadd(context, srcA, srcB, op.Dest, op.AvgMode, op.X, op.WriteCC);
  24. }
  25. public static void IaddC(EmitterContext context)
  26. {
  27. InstIaddC op = context.GetOp<InstIaddC>();
  28. Operand srcA = GetSrcReg(context, op.SrcA);
  29. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  30. EmitIadd(context, srcA, srcB, op.Dest, op.AvgMode, op.X, op.WriteCC);
  31. }
  32. public static void Iadd32i(EmitterContext context)
  33. {
  34. InstIadd32i op = context.GetOp<InstIadd32i>();
  35. Operand srcA = GetSrcReg(context, op.SrcA);
  36. Operand srcB = GetSrcImm(context, op.Imm32);
  37. EmitIadd(context, srcA, srcB, op.Dest, op.AvgMode, op.X, op.WriteCC);
  38. }
  39. public static void Iadd3R(EmitterContext context)
  40. {
  41. InstIadd3R op = context.GetOp<InstIadd3R>();
  42. Operand srcA = GetSrcReg(context, op.SrcA);
  43. Operand srcB = GetSrcReg(context, op.SrcB);
  44. Operand srcC = GetSrcReg(context, op.SrcC);
  45. EmitIadd3(context, op.Lrs, srcA, srcB, srcC, op.Apart, op.Bpart, op.Cpart, op.Dest, op.NegA, op.NegB, op.NegC);
  46. }
  47. public static void Iadd3I(EmitterContext context)
  48. {
  49. InstIadd3I op = context.GetOp<InstIadd3I>();
  50. Operand srcA = GetSrcReg(context, op.SrcA);
  51. Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  52. Operand srcC = GetSrcReg(context, op.SrcC);
  53. EmitIadd3(context, Lrs.None, srcA, srcB, srcC, HalfSelect.B32, HalfSelect.B32, HalfSelect.B32, op.Dest, op.NegA, op.NegB, op.NegC);
  54. }
  55. public static void Iadd3C(EmitterContext context)
  56. {
  57. InstIadd3C op = context.GetOp<InstIadd3C>();
  58. Operand srcA = GetSrcReg(context, op.SrcA);
  59. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  60. Operand srcC = GetSrcReg(context, op.SrcC);
  61. EmitIadd3(context, Lrs.None, srcA, srcB, srcC, HalfSelect.B32, HalfSelect.B32, HalfSelect.B32, op.Dest, op.NegA, op.NegB, op.NegC);
  62. }
  63. public static void ImadR(EmitterContext context)
  64. {
  65. InstImadR op = context.GetOp<InstImadR>();
  66. Operand srcA = GetSrcReg(context, op.SrcA);
  67. Operand srcB = GetSrcReg(context, op.SrcB);
  68. Operand srcC = GetSrcReg(context, op.SrcC);
  69. EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
  70. }
  71. public static void ImadI(EmitterContext context)
  72. {
  73. InstImadI op = context.GetOp<InstImadI>();
  74. Operand srcA = GetSrcReg(context, op.SrcA);
  75. Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  76. Operand srcC = GetSrcReg(context, op.SrcC);
  77. EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
  78. }
  79. public static void ImadC(EmitterContext context)
  80. {
  81. InstImadC op = context.GetOp<InstImadC>();
  82. Operand srcA = GetSrcReg(context, op.SrcA);
  83. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  84. Operand srcC = GetSrcReg(context, op.SrcC);
  85. EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
  86. }
  87. public static void ImadRc(EmitterContext context)
  88. {
  89. InstImadRc op = context.GetOp<InstImadRc>();
  90. Operand srcA = GetSrcReg(context, op.SrcA);
  91. Operand srcB = GetSrcReg(context, op.SrcC);
  92. Operand srcC = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  93. EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
  94. }
  95. public static void Imad32i(EmitterContext context)
  96. {
  97. InstImad32i op = context.GetOp<InstImad32i>();
  98. Operand srcA = GetSrcReg(context, op.SrcA);
  99. Operand srcB = GetSrcImm(context, op.Imm32);
  100. Operand srcC = GetSrcReg(context, op.Dest);
  101. EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
  102. }
  103. public static void ImulR(EmitterContext context)
  104. {
  105. InstImulR op = context.GetOp<InstImulR>();
  106. Operand srcA = GetSrcReg(context, op.SrcA);
  107. Operand srcB = GetSrcReg(context, op.SrcB);
  108. EmitImad(context, srcA, srcB, Const(0), op.Dest, AvgMode.NoNeg, op.ASigned, op.BSigned, op.Hilo);
  109. }
  110. public static void ImulI(EmitterContext context)
  111. {
  112. InstImulI op = context.GetOp<InstImulI>();
  113. Operand srcA = GetSrcReg(context, op.SrcA);
  114. Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  115. EmitImad(context, srcA, srcB, Const(0), op.Dest, AvgMode.NoNeg, op.ASigned, op.BSigned, op.Hilo);
  116. }
  117. public static void ImulC(EmitterContext context)
  118. {
  119. InstImulC op = context.GetOp<InstImulC>();
  120. Operand srcA = GetSrcReg(context, op.SrcA);
  121. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  122. EmitImad(context, srcA, srcB, Const(0), op.Dest, AvgMode.NoNeg, op.ASigned, op.BSigned, op.Hilo);
  123. }
  124. public static void Imul32i(EmitterContext context)
  125. {
  126. InstImul32i op = context.GetOp<InstImul32i>();
  127. Operand srcA = GetSrcReg(context, op.SrcA);
  128. Operand srcB = GetSrcImm(context, op.Imm32);
  129. EmitImad(context, srcA, srcB, Const(0), op.Dest, AvgMode.NoNeg, op.ASigned, op.BSigned, op.Hilo);
  130. }
  131. public static void IscaddR(EmitterContext context)
  132. {
  133. InstIscaddR op = context.GetOp<InstIscaddR>();
  134. Operand srcA = GetSrcReg(context, op.SrcA);
  135. Operand srcB = GetSrcReg(context, op.SrcB);
  136. EmitIscadd(context, srcA, srcB, op.Dest, op.Imm5, op.AvgMode, op.WriteCC);
  137. }
  138. public static void IscaddI(EmitterContext context)
  139. {
  140. InstIscaddI op = context.GetOp<InstIscaddI>();
  141. Operand srcA = GetSrcReg(context, op.SrcA);
  142. Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  143. EmitIscadd(context, srcA, srcB, op.Dest, op.Imm5, op.AvgMode, op.WriteCC);
  144. }
  145. public static void IscaddC(EmitterContext context)
  146. {
  147. InstIscaddC op = context.GetOp<InstIscaddC>();
  148. Operand srcA = GetSrcReg(context, op.SrcA);
  149. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  150. EmitIscadd(context, srcA, srcB, op.Dest, op.Imm5, op.AvgMode, op.WriteCC);
  151. }
  152. public static void Iscadd32i(EmitterContext context)
  153. {
  154. InstIscadd32i op = context.GetOp<InstIscadd32i>();
  155. Operand srcA = GetSrcReg(context, op.SrcA);
  156. Operand srcB = GetSrcImm(context, op.Imm32);
  157. EmitIscadd(context, srcA, srcB, op.Dest, op.Imm5, AvgMode.NoNeg, op.WriteCC);
  158. }
  159. public static void LeaR(EmitterContext context)
  160. {
  161. InstLeaR op = context.GetOp<InstLeaR>();
  162. Operand srcA = GetSrcReg(context, op.SrcA);
  163. Operand srcB = GetSrcReg(context, op.SrcB);
  164. EmitLea(context, srcA, srcB, op.Dest, op.NegA, op.ImmU5);
  165. }
  166. public static void LeaI(EmitterContext context)
  167. {
  168. InstLeaI op = context.GetOp<InstLeaI>();
  169. Operand srcA = GetSrcReg(context, op.SrcA);
  170. Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  171. EmitLea(context, srcA, srcB, op.Dest, op.NegA, op.ImmU5);
  172. }
  173. public static void LeaC(EmitterContext context)
  174. {
  175. InstLeaC op = context.GetOp<InstLeaC>();
  176. Operand srcA = GetSrcReg(context, op.SrcA);
  177. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  178. EmitLea(context, srcA, srcB, op.Dest, op.NegA, op.ImmU5);
  179. }
  180. public static void LeaHiR(EmitterContext context)
  181. {
  182. InstLeaHiR op = context.GetOp<InstLeaHiR>();
  183. Operand srcA = GetSrcReg(context, op.SrcA);
  184. Operand srcB = GetSrcReg(context, op.SrcB);
  185. Operand srcC = GetSrcReg(context, op.SrcC);
  186. EmitLeaHi(context, srcA, srcB, srcC, op.Dest, op.NegA, op.ImmU5);
  187. }
  188. public static void LeaHiC(EmitterContext context)
  189. {
  190. InstLeaHiC op = context.GetOp<InstLeaHiC>();
  191. Operand srcA = GetSrcReg(context, op.SrcA);
  192. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  193. Operand srcC = GetSrcReg(context, op.SrcC);
  194. EmitLeaHi(context, srcA, srcB, srcC, op.Dest, op.NegA, op.ImmU5);
  195. }
  196. public static void XmadR(EmitterContext context)
  197. {
  198. InstXmadR op = context.GetOp<InstXmadR>();
  199. Operand srcA = GetSrcReg(context, op.SrcA);
  200. Operand srcB = GetSrcReg(context, op.SrcB);
  201. Operand srcC = GetSrcReg(context, op.SrcC);
  202. EmitXmad(context, op.XmadCop, srcA, srcB, srcC, op.Dest, op.ASigned, op.BSigned, op.HiloA, op.HiloB, op.Psl, op.Mrg, op.X, op.WriteCC);
  203. }
  204. public static void XmadI(EmitterContext context)
  205. {
  206. InstXmadI op = context.GetOp<InstXmadI>();
  207. Operand srcA = GetSrcReg(context, op.SrcA);
  208. Operand srcB = GetSrcImm(context, op.Imm16);
  209. Operand srcC = GetSrcReg(context, op.SrcC);
  210. EmitXmad(context, op.XmadCop, srcA, srcB, srcC, op.Dest, op.ASigned, op.BSigned, op.HiloA, false, op.Psl, op.Mrg, op.X, op.WriteCC);
  211. }
  212. public static void XmadC(EmitterContext context)
  213. {
  214. InstXmadC op = context.GetOp<InstXmadC>();
  215. Operand srcA = GetSrcReg(context, op.SrcA);
  216. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  217. Operand srcC = GetSrcReg(context, op.SrcC);
  218. EmitXmad(context, op.XmadCop, srcA, srcB, srcC, op.Dest, op.ASigned, op.BSigned, op.HiloA, op.HiloB, op.Psl, op.Mrg, op.X, op.WriteCC);
  219. }
  220. public static void XmadRc(EmitterContext context)
  221. {
  222. InstXmadRc op = context.GetOp<InstXmadRc>();
  223. Operand srcA = GetSrcReg(context, op.SrcA);
  224. Operand srcB = GetSrcReg(context, op.SrcC);
  225. Operand srcC = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  226. EmitXmad(context, op.XmadCop, srcA, srcB, srcC, op.Dest, op.ASigned, op.BSigned, op.HiloA, op.HiloB, false, false, op.X, op.WriteCC);
  227. }
  228. private static void EmitIadd(
  229. EmitterContext context,
  230. Operand srcA,
  231. Operand srcB,
  232. int rd,
  233. AvgMode avgMode,
  234. bool extended,
  235. bool writeCC)
  236. {
  237. srcA = context.INegate(srcA, avgMode == AvgMode.NegA);
  238. srcB = context.INegate(srcB, avgMode == AvgMode.NegB);
  239. Operand res = context.IAdd(srcA, srcB);
  240. if (extended)
  241. {
  242. res = context.IAdd(res, context.BitwiseAnd(GetCF(), Const(1)));
  243. }
  244. SetIaddFlags(context, res, srcA, srcB, writeCC, extended);
  245. // TODO: SAT.
  246. context.Copy(GetDest(rd), res);
  247. }
  248. private static void EmitIadd3(
  249. EmitterContext context,
  250. Lrs mode,
  251. Operand srcA,
  252. Operand srcB,
  253. Operand srcC,
  254. HalfSelect partA,
  255. HalfSelect partB,
  256. HalfSelect partC,
  257. int rd,
  258. bool negateA,
  259. bool negateB,
  260. bool negateC)
  261. {
  262. Operand Extend(Operand src, HalfSelect part)
  263. {
  264. if (part == HalfSelect.B32)
  265. {
  266. return src;
  267. }
  268. if (part == HalfSelect.H0)
  269. {
  270. return context.BitwiseAnd(src, Const(0xffff));
  271. }
  272. else if (part == HalfSelect.H1)
  273. {
  274. return context.ShiftRightU32(src, Const(16));
  275. }
  276. else
  277. {
  278. context.TranslatorContext.GpuAccessor.Log($"Iadd3 has invalid component selection {part}.");
  279. }
  280. return src;
  281. }
  282. srcA = context.INegate(Extend(srcA, partA), negateA);
  283. srcB = context.INegate(Extend(srcB, partB), negateB);
  284. srcC = context.INegate(Extend(srcC, partC), negateC);
  285. Operand res = context.IAdd(srcA, srcB);
  286. if (mode != Lrs.None)
  287. {
  288. if (mode == Lrs.LeftShift)
  289. {
  290. res = context.ShiftLeft(res, Const(16));
  291. }
  292. else if (mode == Lrs.RightShift)
  293. {
  294. res = context.ShiftRightU32(res, Const(16));
  295. }
  296. else
  297. {
  298. // TODO: Warning.
  299. }
  300. }
  301. res = context.IAdd(res, srcC);
  302. context.Copy(GetDest(rd), res);
  303. // TODO: CC, X, corner cases.
  304. }
  305. private static void EmitImad(
  306. EmitterContext context,
  307. Operand srcA,
  308. Operand srcB,
  309. Operand srcC,
  310. int rd,
  311. AvgMode avgMode,
  312. bool signedA,
  313. bool signedB,
  314. bool high)
  315. {
  316. srcB = context.INegate(srcB, avgMode == AvgMode.NegA);
  317. srcC = context.INegate(srcC, avgMode == AvgMode.NegB);
  318. Operand res;
  319. if (high)
  320. {
  321. if (signedA && signedB)
  322. {
  323. res = context.MultiplyHighS32(srcA, srcB);
  324. }
  325. else
  326. {
  327. res = context.MultiplyHighU32(srcA, srcB);
  328. if (signedA)
  329. {
  330. res = context.IAdd(res, context.IMultiply(srcB, context.ShiftRightS32(srcA, Const(31))));
  331. }
  332. else if (signedB)
  333. {
  334. res = context.IAdd(res, context.IMultiply(srcA, context.ShiftRightS32(srcB, Const(31))));
  335. }
  336. }
  337. }
  338. else
  339. {
  340. res = context.IMultiply(srcA, srcB);
  341. }
  342. if (srcC.Type != OperandType.Constant || srcC.Value != 0)
  343. {
  344. res = context.IAdd(res, srcC);
  345. }
  346. // TODO: CC, X, SAT, and more?
  347. context.Copy(GetDest(rd), res);
  348. }
  349. private static void EmitIscadd(
  350. EmitterContext context,
  351. Operand srcA,
  352. Operand srcB,
  353. int rd,
  354. int shift,
  355. AvgMode avgMode,
  356. bool writeCC)
  357. {
  358. srcA = context.ShiftLeft(srcA, Const(shift));
  359. srcA = context.INegate(srcA, avgMode == AvgMode.NegA);
  360. srcB = context.INegate(srcB, avgMode == AvgMode.NegB);
  361. Operand res = context.IAdd(srcA, srcB);
  362. SetIaddFlags(context, res, srcA, srcB, writeCC, false);
  363. context.Copy(GetDest(rd), res);
  364. }
  365. public static void EmitLea(EmitterContext context, Operand srcA, Operand srcB, int rd, bool negateA, int shift)
  366. {
  367. srcA = context.ShiftLeft(srcA, Const(shift));
  368. srcA = context.INegate(srcA, negateA);
  369. Operand res = context.IAdd(srcA, srcB);
  370. context.Copy(GetDest(rd), res);
  371. // TODO: CC, X.
  372. }
  373. private static void EmitLeaHi(
  374. EmitterContext context,
  375. Operand srcA,
  376. Operand srcB,
  377. Operand srcC,
  378. int rd,
  379. bool negateA,
  380. int shift)
  381. {
  382. Operand aLow = context.ShiftLeft(srcA, Const(shift));
  383. Operand aHigh = shift == 0 ? Const(0) : context.ShiftRightU32(srcA, Const(32 - shift));
  384. aHigh = context.BitwiseOr(aHigh, context.ShiftLeft(srcC, Const(shift)));
  385. if (negateA)
  386. {
  387. // Perform 64-bit negation by doing bitwise not of the value,
  388. // then adding 1 and carrying over from low to high.
  389. aLow = context.BitwiseNot(aLow);
  390. aHigh = context.BitwiseNot(aHigh);
  391. #pragma warning disable IDE0059 // Remove unnecessary value assignment
  392. aLow = AddWithCarry(context, aLow, Const(1), out Operand aLowCOut);
  393. #pragma warning restore IDE0059
  394. aHigh = context.IAdd(aHigh, aLowCOut);
  395. }
  396. Operand res = context.IAdd(aHigh, srcB);
  397. context.Copy(GetDest(rd), res);
  398. // TODO: CC, X.
  399. }
  400. public static void EmitXmad(
  401. EmitterContext context,
  402. XmadCop2 mode,
  403. Operand srcA,
  404. Operand srcB,
  405. Operand srcC,
  406. int rd,
  407. bool signedA,
  408. bool signedB,
  409. bool highA,
  410. bool highB,
  411. bool productShiftLeft,
  412. bool merge,
  413. bool extended,
  414. bool writeCC)
  415. {
  416. XmadCop modeConv;
  417. switch (mode)
  418. {
  419. case XmadCop2.Cfull:
  420. modeConv = XmadCop.Cfull;
  421. break;
  422. case XmadCop2.Clo:
  423. modeConv = XmadCop.Clo;
  424. break;
  425. case XmadCop2.Chi:
  426. modeConv = XmadCop.Chi;
  427. break;
  428. case XmadCop2.Csfu:
  429. modeConv = XmadCop.Csfu;
  430. break;
  431. default:
  432. context.TranslatorContext.GpuAccessor.Log($"Invalid XMAD mode \"{mode}\".");
  433. return;
  434. }
  435. EmitXmad(context, modeConv, srcA, srcB, srcC, rd, signedA, signedB, highA, highB, productShiftLeft, merge, extended, writeCC);
  436. }
  437. public static void EmitXmad(
  438. EmitterContext context,
  439. XmadCop mode,
  440. Operand srcA,
  441. Operand srcB,
  442. Operand srcC,
  443. int rd,
  444. bool signedA,
  445. bool signedB,
  446. bool highA,
  447. bool highB,
  448. bool productShiftLeft,
  449. bool merge,
  450. bool extended,
  451. bool writeCC)
  452. {
  453. Operand srcBUnmodified = srcB;
  454. Operand Extend16To32(Operand src, bool high, bool signed)
  455. {
  456. if (signed && high)
  457. {
  458. return context.ShiftRightS32(src, Const(16));
  459. }
  460. else if (signed)
  461. {
  462. return context.BitfieldExtractS32(src, Const(0), Const(16));
  463. }
  464. else if (high)
  465. {
  466. return context.ShiftRightU32(src, Const(16));
  467. }
  468. else
  469. {
  470. return context.BitwiseAnd(src, Const(0xffff));
  471. }
  472. }
  473. srcA = Extend16To32(srcA, highA, signedA);
  474. srcB = Extend16To32(srcB, highB, signedB);
  475. Operand res = context.IMultiply(srcA, srcB);
  476. if (productShiftLeft)
  477. {
  478. res = context.ShiftLeft(res, Const(16));
  479. }
  480. switch (mode)
  481. {
  482. case XmadCop.Cfull:
  483. break;
  484. case XmadCop.Clo:
  485. srcC = Extend16To32(srcC, high: false, signed: false);
  486. break;
  487. case XmadCop.Chi:
  488. srcC = Extend16To32(srcC, high: true, signed: false);
  489. break;
  490. case XmadCop.Cbcc:
  491. srcC = context.IAdd(srcC, context.ShiftLeft(srcBUnmodified, Const(16)));
  492. break;
  493. case XmadCop.Csfu:
  494. Operand signAdjustA = context.ShiftLeft(context.ShiftRightU32(srcA, Const(31)), Const(16));
  495. Operand signAdjustB = context.ShiftLeft(context.ShiftRightU32(srcB, Const(31)), Const(16));
  496. srcC = context.ISubtract(srcC, context.IAdd(signAdjustA, signAdjustB));
  497. break;
  498. default:
  499. context.TranslatorContext.GpuAccessor.Log($"Invalid XMAD mode \"{mode}\".");
  500. return;
  501. }
  502. Operand product = res;
  503. if (extended)
  504. {
  505. // Add with carry.
  506. res = context.IAdd(res, context.BitwiseAnd(GetCF(), Const(1)));
  507. }
  508. else
  509. {
  510. // Add (no carry in).
  511. res = context.IAdd(res, srcC);
  512. }
  513. SetIaddFlags(context, res, product, srcC, writeCC, extended);
  514. if (merge)
  515. {
  516. res = context.BitwiseAnd(res, Const(0xffff));
  517. res = context.BitwiseOr(res, context.ShiftLeft(srcBUnmodified, Const(16)));
  518. }
  519. context.Copy(GetDest(rd), res);
  520. }
  521. private static void SetIaddFlags(EmitterContext context, Operand res, Operand srcA, Operand srcB, bool setCC, bool extended)
  522. {
  523. if (!setCC)
  524. {
  525. return;
  526. }
  527. if (extended)
  528. {
  529. // C = (d == a && CIn) || d < a
  530. Operand tempC0 = context.ICompareEqual(res, srcA);
  531. Operand tempC1 = context.ICompareLessUnsigned(res, srcA);
  532. tempC0 = context.BitwiseAnd(tempC0, GetCF());
  533. context.Copy(GetCF(), context.BitwiseOr(tempC0, tempC1));
  534. }
  535. else
  536. {
  537. // C = d < a
  538. context.Copy(GetCF(), context.ICompareLessUnsigned(res, srcA));
  539. }
  540. // V = (d ^ a) & ~(a ^ b) < 0
  541. Operand tempV0 = context.BitwiseExclusiveOr(res, srcA);
  542. Operand tempV1 = context.BitwiseExclusiveOr(srcA, srcB);
  543. tempV1 = context.BitwiseNot(tempV1);
  544. Operand tempV = context.BitwiseAnd(tempV0, tempV1);
  545. context.Copy(GetVF(), context.ICompareLess(tempV, Const(0)));
  546. SetZnFlags(context, res, setCC: true, extended: extended);
  547. }
  548. }
  549. }