InstEmitAlu.cs 33 KB

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