InstEmitAlu.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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())
  93. : context.BitwiseAnd(GetCF(), 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 Icmp(EmitterContext context)
  152. {
  153. OpCode op = context.CurrOp;
  154. bool isSigned = op.RawOpCode.Extract(48);
  155. IntegerCondition cmpOp = (IntegerCondition)op.RawOpCode.Extract(49, 3);
  156. Operand srcA = GetSrcA(context);
  157. Operand srcB = GetSrcB(context);
  158. Operand srcC = GetSrcC(context);
  159. Operand cmpRes = GetIntComparison(context, cmpOp, srcC, Const(0), isSigned);
  160. Operand res = context.ConditionalSelect(cmpRes, srcA, srcB);
  161. context.Copy(GetDest(context), res);
  162. }
  163. public static void Imad(EmitterContext context)
  164. {
  165. bool signedA = context.CurrOp.RawOpCode.Extract(48);
  166. bool signedB = context.CurrOp.RawOpCode.Extract(53);
  167. bool high = context.CurrOp.RawOpCode.Extract(54);
  168. Operand srcA = GetSrcA(context);
  169. Operand srcB = GetSrcB(context);
  170. Operand srcC = GetSrcC(context);
  171. Operand res;
  172. if (high)
  173. {
  174. if (signedA && signedB)
  175. {
  176. res = context.MultiplyHighS32(srcA, srcB);
  177. }
  178. else
  179. {
  180. res = context.MultiplyHighU32(srcA, srcB);
  181. if (signedA)
  182. {
  183. res = context.IAdd(res, context.IMultiply(srcB, context.ShiftRightS32(srcA, Const(31))));
  184. }
  185. else if (signedB)
  186. {
  187. res = context.IAdd(res, context.IMultiply(srcA, context.ShiftRightS32(srcB, Const(31))));
  188. }
  189. }
  190. }
  191. else
  192. {
  193. res = context.IMultiply(srcA, srcB);
  194. }
  195. res = context.IAdd(res, srcC);
  196. // TODO: CC, X, SAT, and more?
  197. context.Copy(GetDest(context), res);
  198. }
  199. public static void Imnmx(EmitterContext context)
  200. {
  201. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  202. bool isSignedInt = op.RawOpCode.Extract(48);
  203. Operand srcA = GetSrcA(context);
  204. Operand srcB = GetSrcB(context);
  205. Operand resMin = isSignedInt
  206. ? context.IMinimumS32(srcA, srcB)
  207. : context.IMinimumU32(srcA, srcB);
  208. Operand resMax = isSignedInt
  209. ? context.IMaximumS32(srcA, srcB)
  210. : context.IMaximumU32(srcA, srcB);
  211. Operand pred = GetPredicate39(context);
  212. Operand dest = GetDest(context);
  213. context.Copy(dest, context.ConditionalSelect(pred, resMin, resMax));
  214. SetZnFlags(context, dest, op.SetCondCode);
  215. // TODO: X flags.
  216. }
  217. public static void Iscadd(EmitterContext context)
  218. {
  219. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  220. bool negateA = false, negateB = false;
  221. if (!(op is OpCodeAluImm32))
  222. {
  223. negateB = op.RawOpCode.Extract(48);
  224. negateA = op.RawOpCode.Extract(49);
  225. }
  226. int shift = op is OpCodeAluImm32
  227. ? op.RawOpCode.Extract(53, 5)
  228. : op.RawOpCode.Extract(39, 5);
  229. Operand srcA = GetSrcA(context);
  230. Operand srcB = GetSrcB(context);
  231. srcA = context.ShiftLeft(srcA, Const(shift));
  232. srcA = context.INegate(srcA, negateA);
  233. srcB = context.INegate(srcB, negateB);
  234. Operand res = context.IAdd(srcA, srcB);
  235. context.Copy(GetDest(context), res);
  236. // TODO: CC, X
  237. }
  238. public static void Iset(EmitterContext context)
  239. {
  240. OpCodeSet op = (OpCodeSet)context.CurrOp;
  241. bool boolFloat = op.RawOpCode.Extract(44);
  242. bool isSigned = op.RawOpCode.Extract(48);
  243. IntegerCondition cmpOp = (IntegerCondition)op.RawOpCode.Extract(49, 3);
  244. Operand srcA = GetSrcA(context);
  245. Operand srcB = GetSrcB(context);
  246. Operand res = GetIntComparison(context, cmpOp, srcA, srcB, isSigned);
  247. Operand pred = GetPredicate39(context);
  248. res = GetPredLogicalOp(context, op.LogicalOp, res, pred);
  249. Operand dest = GetDest(context);
  250. if (boolFloat)
  251. {
  252. res = context.ConditionalSelect(res, ConstF(1), Const(0));
  253. context.Copy(dest, res);
  254. SetFPZnFlags(context, res, op.SetCondCode);
  255. }
  256. else
  257. {
  258. context.Copy(dest, res);
  259. SetZnFlags(context, res, op.SetCondCode, op.Extended);
  260. }
  261. // TODO: X
  262. }
  263. public static void Isetp(EmitterContext context)
  264. {
  265. OpCodeSet op = (OpCodeSet)context.CurrOp;
  266. bool isSigned = op.RawOpCode.Extract(48);
  267. IntegerCondition cmpOp = (IntegerCondition)op.RawOpCode.Extract(49, 3);
  268. Operand srcA = GetSrcA(context);
  269. Operand srcB = GetSrcB(context);
  270. Operand p0Res = GetIntComparison(context, cmpOp, srcA, srcB, isSigned);
  271. Operand p1Res = context.BitwiseNot(p0Res);
  272. Operand pred = GetPredicate39(context);
  273. p0Res = GetPredLogicalOp(context, op.LogicalOp, p0Res, pred);
  274. p1Res = GetPredLogicalOp(context, op.LogicalOp, p1Res, pred);
  275. context.Copy(Register(op.Predicate3), p0Res);
  276. context.Copy(Register(op.Predicate0), p1Res);
  277. }
  278. public static void Lea(EmitterContext context)
  279. {
  280. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  281. bool negateA = op.RawOpCode.Extract(45);
  282. int shift = op.RawOpCode.Extract(39, 5);
  283. Operand srcA = GetSrcA(context);
  284. Operand srcB = GetSrcB(context);
  285. srcA = context.ShiftLeft(srcA, Const(shift));
  286. srcA = context.INegate(srcA, negateA);
  287. Operand res = context.IAdd(srcA, srcB);
  288. context.Copy(GetDest(context), res);
  289. // TODO: CC, X
  290. }
  291. public static void Lea_Hi(EmitterContext context)
  292. {
  293. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  294. bool isReg = op is OpCodeAluReg;
  295. bool negateA;
  296. int shift;
  297. if (isReg)
  298. {
  299. negateA = op.RawOpCode.Extract(37);
  300. shift = op.RawOpCode.Extract(28, 5);
  301. }
  302. else
  303. {
  304. negateA = op.RawOpCode.Extract(56);
  305. shift = op.RawOpCode.Extract(51, 5);
  306. }
  307. Operand srcA = GetSrcA(context);
  308. Operand srcB = GetSrcB(context);
  309. Operand srcC = GetSrcC(context);
  310. Operand aLow = context.ShiftLeft(srcA, Const(shift));
  311. Operand aHigh = shift == 0 ? Const(0) : context.ShiftRightU32(srcA, Const(32 - shift));
  312. aHigh = context.BitwiseOr(aHigh, context.ShiftLeft(srcC, Const(shift)));
  313. if (negateA)
  314. {
  315. // Perform 64-bit negation by doing bitwise not of the value,
  316. // then adding 1 and carrying over from low to high.
  317. aLow = context.BitwiseNot(aLow);
  318. aHigh = context.BitwiseNot(aHigh);
  319. aLow = AddWithCarry(context, aLow, Const(1), out Operand aLowCOut);
  320. aHigh = context.IAdd(aHigh, aLowCOut);
  321. }
  322. Operand res = context.IAdd(aHigh, srcB);
  323. context.Copy(GetDest(context), res);
  324. // TODO: CC, X
  325. }
  326. public static void Lop(EmitterContext context)
  327. {
  328. IOpCodeLop op = (IOpCodeLop)context.CurrOp;
  329. Operand srcA = context.BitwiseNot(GetSrcA(context), op.InvertA);
  330. Operand srcB = context.BitwiseNot(GetSrcB(context), op.InvertB);
  331. Operand res = srcB;
  332. switch (op.LogicalOp)
  333. {
  334. case LogicalOperation.And: res = context.BitwiseAnd (srcA, srcB); break;
  335. case LogicalOperation.Or: res = context.BitwiseOr (srcA, srcB); break;
  336. case LogicalOperation.ExclusiveOr: res = context.BitwiseExclusiveOr(srcA, srcB); break;
  337. }
  338. EmitLopPredWrite(context, op, res);
  339. Operand dest = GetDest(context);
  340. context.Copy(dest, res);
  341. SetZnFlags(context, dest, op.SetCondCode, op.Extended);
  342. }
  343. public static void Lop3(EmitterContext context)
  344. {
  345. IOpCodeLop op = (IOpCodeLop)context.CurrOp;
  346. Operand srcA = GetSrcA(context);
  347. Operand srcB = GetSrcB(context);
  348. Operand srcC = GetSrcC(context);
  349. bool regVariant = op is OpCodeLopReg;
  350. int truthTable = regVariant
  351. ? op.RawOpCode.Extract(28, 8)
  352. : op.RawOpCode.Extract(48, 8);
  353. Operand res = Lop3Expression.GetFromTruthTable(context, srcA, srcB, srcC, truthTable);
  354. if (regVariant)
  355. {
  356. EmitLopPredWrite(context, op, res);
  357. }
  358. Operand dest = GetDest(context);
  359. context.Copy(dest, res);
  360. SetZnFlags(context, dest, op.SetCondCode, op.Extended);
  361. }
  362. public static void Popc(EmitterContext context)
  363. {
  364. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  365. bool invert = op.RawOpCode.Extract(40);
  366. Operand srcB = context.BitwiseNot(GetSrcB(context), invert);
  367. Operand res = context.BitCount(srcB);
  368. context.Copy(GetDest(context), res);
  369. }
  370. public static void Pset(EmitterContext context)
  371. {
  372. OpCodePset op = (OpCodePset)context.CurrOp;
  373. bool boolFloat = op.RawOpCode.Extract(44);
  374. Operand srcA = context.BitwiseNot(Register(op.Predicate12), op.InvertA);
  375. Operand srcB = context.BitwiseNot(Register(op.Predicate29), op.InvertB);
  376. Operand srcC = context.BitwiseNot(Register(op.Predicate39), op.InvertP);
  377. Operand res = GetPredLogicalOp(context, op.LogicalOpAB, srcA, srcB);
  378. res = GetPredLogicalOp(context, op.LogicalOp, res, srcC);
  379. Operand dest = GetDest(context);
  380. if (boolFloat)
  381. {
  382. context.Copy(dest, context.ConditionalSelect(res, ConstF(1), Const(0)));
  383. }
  384. else
  385. {
  386. context.Copy(dest, res);
  387. }
  388. }
  389. public static void Psetp(EmitterContext context)
  390. {
  391. OpCodePset op = (OpCodePset)context.CurrOp;
  392. Operand srcA = context.BitwiseNot(Register(op.Predicate12), op.InvertA);
  393. Operand srcB = context.BitwiseNot(Register(op.Predicate29), op.InvertB);
  394. Operand p0Res = GetPredLogicalOp(context, op.LogicalOpAB, srcA, srcB);
  395. Operand p1Res = context.BitwiseNot(p0Res);
  396. Operand pred = GetPredicate39(context);
  397. p0Res = GetPredLogicalOp(context, op.LogicalOp, p0Res, pred);
  398. p1Res = GetPredLogicalOp(context, op.LogicalOp, p1Res, pred);
  399. context.Copy(Register(op.Predicate3), p0Res);
  400. context.Copy(Register(op.Predicate0), p1Res);
  401. }
  402. public static void Rro(EmitterContext context)
  403. {
  404. // This is the range reduction operator,
  405. // we translate it as a simple move, as it
  406. // should be always followed by a matching
  407. // MUFU instruction.
  408. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  409. bool negateB = op.RawOpCode.Extract(45);
  410. bool absoluteB = op.RawOpCode.Extract(49);
  411. Operand srcB = GetSrcB(context);
  412. srcB = context.FPAbsNeg(srcB, absoluteB, negateB);
  413. context.Copy(GetDest(context), srcB);
  414. }
  415. public static void Shl(EmitterContext context)
  416. {
  417. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  418. bool isMasked = op.RawOpCode.Extract(39);
  419. Operand srcB = GetSrcB(context);
  420. if (isMasked)
  421. {
  422. srcB = context.BitwiseAnd(srcB, Const(0x1f));
  423. }
  424. Operand res = context.ShiftLeft(GetSrcA(context), srcB);
  425. if (!isMasked)
  426. {
  427. // Clamped shift value.
  428. Operand isLessThan32 = context.ICompareLessUnsigned(srcB, Const(32));
  429. res = context.ConditionalSelect(isLessThan32, res, Const(0));
  430. }
  431. // TODO: X, CC
  432. context.Copy(GetDest(context), res);
  433. }
  434. public static void Shr(EmitterContext context)
  435. {
  436. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  437. bool isMasked = op.RawOpCode.Extract(39);
  438. bool isReverse = op.RawOpCode.Extract(40);
  439. bool isSigned = op.RawOpCode.Extract(48);
  440. Operand srcA = GetSrcA(context);
  441. Operand srcB = GetSrcB(context);
  442. if (isReverse)
  443. {
  444. srcA = context.BitfieldReverse(srcA);
  445. }
  446. if (isMasked)
  447. {
  448. srcB = context.BitwiseAnd(srcB, Const(0x1f));
  449. }
  450. Operand res = isSigned
  451. ? context.ShiftRightS32(srcA, srcB)
  452. : context.ShiftRightU32(srcA, srcB);
  453. if (!isMasked)
  454. {
  455. // Clamped shift value.
  456. Operand resShiftBy32;
  457. if (isSigned)
  458. {
  459. resShiftBy32 = context.ShiftRightS32(srcA, Const(31));
  460. }
  461. else
  462. {
  463. resShiftBy32 = Const(0);
  464. }
  465. Operand isLessThan32 = context.ICompareLessUnsigned(srcB, Const(32));
  466. res = context.ConditionalSelect(isLessThan32, res, resShiftBy32);
  467. }
  468. // TODO: X, CC
  469. context.Copy(GetDest(context), res);
  470. }
  471. public static void Xmad(EmitterContext context)
  472. {
  473. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  474. bool signedA = context.CurrOp.RawOpCode.Extract(48);
  475. bool signedB = context.CurrOp.RawOpCode.Extract(49);
  476. bool highA = context.CurrOp.RawOpCode.Extract(53);
  477. bool isReg = (op is OpCodeAluReg) && !(op is OpCodeAluRegCbuf);
  478. bool isImm = (op is OpCodeAluImm);
  479. XmadCMode mode = isReg || isImm
  480. ? (XmadCMode)context.CurrOp.RawOpCode.Extract(50, 3)
  481. : (XmadCMode)context.CurrOp.RawOpCode.Extract(50, 2);
  482. bool highB = false;
  483. if (isReg)
  484. {
  485. highB = context.CurrOp.RawOpCode.Extract(35);
  486. }
  487. else if (!isImm)
  488. {
  489. highB = context.CurrOp.RawOpCode.Extract(52);
  490. }
  491. Operand srcA = GetSrcA(context);
  492. Operand srcB = GetSrcB(context);
  493. Operand srcC = GetSrcC(context);
  494. // XMAD immediates are 16-bits unsigned integers.
  495. if (srcB.Type == OperandType.Constant)
  496. {
  497. srcB = Const(srcB.Value & 0xffff);
  498. }
  499. Operand Extend16To32(Operand src, bool high, bool signed)
  500. {
  501. if (signed && high)
  502. {
  503. return context.ShiftRightS32(src, Const(16));
  504. }
  505. else if (signed)
  506. {
  507. return context.BitfieldExtractS32(src, Const(0), Const(16));
  508. }
  509. else if (high)
  510. {
  511. return context.ShiftRightU32(src, Const(16));
  512. }
  513. else
  514. {
  515. return context.BitwiseAnd(src, Const(0xffff));
  516. }
  517. }
  518. srcA = Extend16To32(srcA, highA, signedA);
  519. srcB = Extend16To32(srcB, highB, signedB);
  520. bool productShiftLeft = false;
  521. bool merge = false;
  522. if (op is OpCodeAluCbuf)
  523. {
  524. productShiftLeft = context.CurrOp.RawOpCode.Extract(55);
  525. merge = context.CurrOp.RawOpCode.Extract(56);
  526. }
  527. else if (!(op is OpCodeAluRegCbuf))
  528. {
  529. productShiftLeft = context.CurrOp.RawOpCode.Extract(36);
  530. merge = context.CurrOp.RawOpCode.Extract(37);
  531. }
  532. bool extended;
  533. if ((op is OpCodeAluReg) || (op is OpCodeAluImm))
  534. {
  535. extended = context.CurrOp.RawOpCode.Extract(38);
  536. }
  537. else
  538. {
  539. extended = context.CurrOp.RawOpCode.Extract(54);
  540. }
  541. Operand res = context.IMultiply(srcA, srcB);
  542. if (productShiftLeft)
  543. {
  544. res = context.ShiftLeft(res, Const(16));
  545. }
  546. switch (mode)
  547. {
  548. case XmadCMode.Cfull: break;
  549. case XmadCMode.Clo: srcC = Extend16To32(srcC, high: false, signed: false); break;
  550. case XmadCMode.Chi: srcC = Extend16To32(srcC, high: true, signed: false); break;
  551. case XmadCMode.Cbcc:
  552. {
  553. srcC = context.IAdd(srcC, context.ShiftLeft(GetSrcB(context), Const(16)));
  554. break;
  555. }
  556. case XmadCMode.Csfu:
  557. {
  558. Operand signAdjustA = context.ShiftLeft(context.ShiftRightU32(srcA, Const(31)), Const(16));
  559. Operand signAdjustB = context.ShiftLeft(context.ShiftRightU32(srcB, Const(31)), Const(16));
  560. srcC = context.ISubtract(srcC, context.IAdd(signAdjustA, signAdjustB));
  561. break;
  562. }
  563. default: /* TODO: Warning */ break;
  564. }
  565. Operand product = res;
  566. if (extended)
  567. {
  568. // Add with carry.
  569. res = context.IAdd(res, context.BitwiseAnd(GetCF(), Const(1)));
  570. }
  571. else
  572. {
  573. // Add (no carry in).
  574. res = context.IAdd(res, srcC);
  575. }
  576. SetIaddFlags(context, res, product, srcC, op.SetCondCode, extended);
  577. if (merge)
  578. {
  579. res = context.BitwiseAnd(res, Const(0xffff));
  580. res = context.BitwiseOr(res, context.ShiftLeft(GetSrcB(context), Const(16)));
  581. }
  582. context.Copy(GetDest(context), res);
  583. }
  584. private static Operand GetIntComparison(
  585. EmitterContext context,
  586. IntegerCondition cond,
  587. Operand srcA,
  588. Operand srcB,
  589. bool isSigned)
  590. {
  591. Operand res;
  592. if (cond == IntegerCondition.Always)
  593. {
  594. res = Const(IrConsts.True);
  595. }
  596. else if (cond == IntegerCondition.Never)
  597. {
  598. res = Const(IrConsts.False);
  599. }
  600. else
  601. {
  602. var inst = cond switch
  603. {
  604. IntegerCondition.Less => Instruction.CompareLessU32,
  605. IntegerCondition.Equal => Instruction.CompareEqual,
  606. IntegerCondition.LessOrEqual => Instruction.CompareLessOrEqualU32,
  607. IntegerCondition.Greater => Instruction.CompareGreaterU32,
  608. IntegerCondition.NotEqual => Instruction.CompareNotEqual,
  609. IntegerCondition.GreaterOrEqual => Instruction.CompareGreaterOrEqualU32,
  610. _ => throw new InvalidOperationException($"Unexpected condition \"{cond}\".")
  611. };
  612. if (isSigned)
  613. {
  614. switch (cond)
  615. {
  616. case IntegerCondition.Less: inst = Instruction.CompareLess; break;
  617. case IntegerCondition.LessOrEqual: inst = Instruction.CompareLessOrEqual; break;
  618. case IntegerCondition.Greater: inst = Instruction.CompareGreater; break;
  619. case IntegerCondition.GreaterOrEqual: inst = Instruction.CompareGreaterOrEqual; break;
  620. }
  621. }
  622. res = context.Add(inst, Local(), srcA, srcB);
  623. }
  624. return res;
  625. }
  626. private static void EmitLopPredWrite(EmitterContext context, IOpCodeLop op, Operand result)
  627. {
  628. if (op is OpCodeLop opLop && !opLop.Predicate48.IsPT)
  629. {
  630. Operand pRes;
  631. if (opLop.CondOp == ConditionalOperation.False)
  632. {
  633. pRes = Const(IrConsts.False);
  634. }
  635. else if (opLop.CondOp == ConditionalOperation.True)
  636. {
  637. pRes = Const(IrConsts.True);
  638. }
  639. else if (opLop.CondOp == ConditionalOperation.Zero)
  640. {
  641. pRes = context.ICompareEqual(result, Const(0));
  642. }
  643. else /* if (opLop.CondOp == ConditionalOperation.NotZero) */
  644. {
  645. pRes = context.ICompareNotEqual(result, Const(0));
  646. }
  647. context.Copy(Register(opLop.Predicate48), pRes);
  648. }
  649. }
  650. private static void SetIaddFlags(
  651. EmitterContext context,
  652. Operand res,
  653. Operand srcA,
  654. Operand srcB,
  655. bool setCC,
  656. bool extended,
  657. bool isSubtraction = false)
  658. {
  659. if (!setCC)
  660. {
  661. return;
  662. }
  663. if (!extended || isSubtraction)
  664. {
  665. // C = d < a
  666. context.Copy(GetCF(), context.ICompareLessUnsigned(res, srcA));
  667. }
  668. else
  669. {
  670. // C = (d == a && CIn) || d < a
  671. Operand tempC0 = context.ICompareEqual (res, srcA);
  672. Operand tempC1 = context.ICompareLessUnsigned(res, srcA);
  673. tempC0 = context.BitwiseAnd(tempC0, GetCF());
  674. context.Copy(GetCF(), context.BitwiseOr(tempC0, tempC1));
  675. }
  676. // V = (d ^ a) & ~(a ^ b) < 0
  677. Operand tempV0 = context.BitwiseExclusiveOr(res, srcA);
  678. Operand tempV1 = context.BitwiseExclusiveOr(srcA, srcB);
  679. tempV1 = context.BitwiseNot(tempV1);
  680. Operand tempV = context.BitwiseAnd(tempV0, tempV1);
  681. context.Copy(GetVF(), context.ICompareLess(tempV, Const(0)));
  682. SetZnFlags(context, res, setCC: true, extended: extended);
  683. }
  684. }
  685. }