InstEmitAlu.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 dest = GetDest(context);
  209. context.Copy(dest, context.ConditionalSelect(pred, resMin, resMax));
  210. SetZnFlags(context, dest, 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. Operand dest = GetDest(context);
  335. context.Copy(dest, res);
  336. SetZnFlags(context, dest, op.SetCondCode, op.Extended);
  337. }
  338. public static void Lop3(EmitterContext context)
  339. {
  340. IOpCodeLop op = (IOpCodeLop)context.CurrOp;
  341. Operand srcA = GetSrcA(context);
  342. Operand srcB = GetSrcB(context);
  343. Operand srcC = GetSrcC(context);
  344. bool regVariant = op is OpCodeLopReg;
  345. int truthTable = regVariant
  346. ? op.RawOpCode.Extract(28, 8)
  347. : op.RawOpCode.Extract(48, 8);
  348. Operand res = Lop3Expression.GetFromTruthTable(context, srcA, srcB, srcC, truthTable);
  349. if (regVariant)
  350. {
  351. EmitLopPredWrite(context, op, res, (ConditionalOperation)context.CurrOp.RawOpCode.Extract(36, 2));
  352. }
  353. Operand dest = GetDest(context);
  354. context.Copy(dest, res);
  355. SetZnFlags(context, dest, op.SetCondCode, op.Extended);
  356. }
  357. public static void Popc(EmitterContext context)
  358. {
  359. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  360. bool invert = op.RawOpCode.Extract(40);
  361. Operand srcB = context.BitwiseNot(GetSrcB(context), invert);
  362. Operand res = context.BitCount(srcB);
  363. context.Copy(GetDest(context), res);
  364. }
  365. public static void Pset(EmitterContext context)
  366. {
  367. OpCodePset op = (OpCodePset)context.CurrOp;
  368. bool boolFloat = op.RawOpCode.Extract(44);
  369. Operand srcA = context.BitwiseNot(Register(op.Predicate12), op.InvertA);
  370. Operand srcB = context.BitwiseNot(Register(op.Predicate29), op.InvertB);
  371. Operand srcC = context.BitwiseNot(Register(op.Predicate39), op.InvertP);
  372. Operand res = GetPredLogicalOp(context, op.LogicalOpAB, srcA, srcB);
  373. res = GetPredLogicalOp(context, op.LogicalOp, res, srcC);
  374. Operand dest = GetDest(context);
  375. if (boolFloat)
  376. {
  377. context.Copy(dest, context.ConditionalSelect(res, ConstF(1), Const(0)));
  378. }
  379. else
  380. {
  381. context.Copy(dest, res);
  382. }
  383. }
  384. public static void Psetp(EmitterContext context)
  385. {
  386. OpCodePset op = (OpCodePset)context.CurrOp;
  387. Operand srcA = context.BitwiseNot(Register(op.Predicate12), op.InvertA);
  388. Operand srcB = context.BitwiseNot(Register(op.Predicate29), op.InvertB);
  389. Operand p0Res = GetPredLogicalOp(context, op.LogicalOpAB, srcA, srcB);
  390. Operand p1Res = context.BitwiseNot(p0Res);
  391. Operand pred = GetPredicate39(context);
  392. p0Res = GetPredLogicalOp(context, op.LogicalOp, p0Res, pred);
  393. p1Res = GetPredLogicalOp(context, op.LogicalOp, p1Res, pred);
  394. context.Copy(Register(op.Predicate3), p0Res);
  395. context.Copy(Register(op.Predicate0), p1Res);
  396. }
  397. public static void Rro(EmitterContext context)
  398. {
  399. // This is the range reduction operator,
  400. // we translate it as a simple move, as it
  401. // should be always followed by a matching
  402. // MUFU instruction.
  403. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  404. bool negateB = op.RawOpCode.Extract(45);
  405. bool absoluteB = op.RawOpCode.Extract(49);
  406. Operand srcB = GetSrcB(context);
  407. srcB = context.FPAbsNeg(srcB, absoluteB, negateB);
  408. context.Copy(GetDest(context), srcB);
  409. }
  410. public static void Shl(EmitterContext context)
  411. {
  412. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  413. bool isMasked = op.RawOpCode.Extract(39);
  414. Operand srcB = GetSrcB(context);
  415. if (isMasked)
  416. {
  417. srcB = context.BitwiseAnd(srcB, Const(0x1f));
  418. }
  419. Operand res = context.ShiftLeft(GetSrcA(context), srcB);
  420. if (!isMasked)
  421. {
  422. // Clamped shift value.
  423. Operand isLessThan32 = context.ICompareLessUnsigned(srcB, Const(32));
  424. res = context.ConditionalSelect(isLessThan32, res, Const(0));
  425. }
  426. // TODO: X, CC
  427. context.Copy(GetDest(context), res);
  428. }
  429. public static void Shr(EmitterContext context)
  430. {
  431. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  432. bool isMasked = op.RawOpCode.Extract(39);
  433. bool isReverse = op.RawOpCode.Extract(40);
  434. bool isSigned = op.RawOpCode.Extract(48);
  435. Operand srcA = GetSrcA(context);
  436. Operand srcB = GetSrcB(context);
  437. if (isReverse)
  438. {
  439. srcA = context.BitfieldReverse(srcA);
  440. }
  441. if (isMasked)
  442. {
  443. srcB = context.BitwiseAnd(srcB, Const(0x1f));
  444. }
  445. Operand res = isSigned
  446. ? context.ShiftRightS32(srcA, srcB)
  447. : context.ShiftRightU32(srcA, srcB);
  448. if (!isMasked)
  449. {
  450. // Clamped shift value.
  451. Operand resShiftBy32;
  452. if (isSigned)
  453. {
  454. resShiftBy32 = context.ShiftRightS32(srcA, Const(31));
  455. }
  456. else
  457. {
  458. resShiftBy32 = Const(0);
  459. }
  460. Operand isLessThan32 = context.ICompareLessUnsigned(srcB, Const(32));
  461. res = context.ConditionalSelect(isLessThan32, res, resShiftBy32);
  462. }
  463. // TODO: X, CC
  464. context.Copy(GetDest(context), res);
  465. }
  466. public static void Xmad(EmitterContext context)
  467. {
  468. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  469. bool signedA = context.CurrOp.RawOpCode.Extract(48);
  470. bool signedB = context.CurrOp.RawOpCode.Extract(49);
  471. bool highA = context.CurrOp.RawOpCode.Extract(53);
  472. bool isReg = (op is OpCodeAluReg) && !(op is OpCodeAluRegCbuf);
  473. bool isImm = (op is OpCodeAluImm);
  474. XmadCMode mode = isReg || isImm
  475. ? (XmadCMode)context.CurrOp.RawOpCode.Extract(50, 3)
  476. : (XmadCMode)context.CurrOp.RawOpCode.Extract(50, 2);
  477. bool highB = false;
  478. if (isReg)
  479. {
  480. highB = context.CurrOp.RawOpCode.Extract(35);
  481. }
  482. else if (!isImm)
  483. {
  484. highB = context.CurrOp.RawOpCode.Extract(52);
  485. }
  486. Operand srcA = GetSrcA(context);
  487. Operand srcB = GetSrcB(context);
  488. Operand srcC = GetSrcC(context);
  489. // XMAD immediates are 16-bits unsigned integers.
  490. if (srcB.Type == OperandType.Constant)
  491. {
  492. srcB = Const(srcB.Value & 0xffff);
  493. }
  494. Operand Extend16To32(Operand src, bool high, bool signed)
  495. {
  496. if (signed && high)
  497. {
  498. return context.ShiftRightS32(src, Const(16));
  499. }
  500. else if (signed)
  501. {
  502. return context.BitfieldExtractS32(src, Const(0), Const(16));
  503. }
  504. else if (high)
  505. {
  506. return context.ShiftRightU32(src, Const(16));
  507. }
  508. else
  509. {
  510. return context.BitwiseAnd(src, Const(0xffff));
  511. }
  512. }
  513. srcA = Extend16To32(srcA, highA, signedA);
  514. srcB = Extend16To32(srcB, highB, signedB);
  515. bool productShiftLeft = false;
  516. bool merge = false;
  517. if (op is OpCodeAluCbuf)
  518. {
  519. productShiftLeft = context.CurrOp.RawOpCode.Extract(55);
  520. merge = context.CurrOp.RawOpCode.Extract(56);
  521. }
  522. else if (!(op is OpCodeAluRegCbuf))
  523. {
  524. productShiftLeft = context.CurrOp.RawOpCode.Extract(36);
  525. merge = context.CurrOp.RawOpCode.Extract(37);
  526. }
  527. bool extended;
  528. if ((op is OpCodeAluReg) || (op is OpCodeAluImm))
  529. {
  530. extended = context.CurrOp.RawOpCode.Extract(38);
  531. }
  532. else
  533. {
  534. extended = context.CurrOp.RawOpCode.Extract(54);
  535. }
  536. Operand res = context.IMultiply(srcA, srcB);
  537. if (productShiftLeft)
  538. {
  539. res = context.ShiftLeft(res, Const(16));
  540. }
  541. switch (mode)
  542. {
  543. case XmadCMode.Cfull: break;
  544. case XmadCMode.Clo: srcC = Extend16To32(srcC, high: false, signed: false); break;
  545. case XmadCMode.Chi: srcC = Extend16To32(srcC, high: true, signed: false); break;
  546. case XmadCMode.Cbcc:
  547. {
  548. srcC = context.IAdd(srcC, context.ShiftLeft(GetSrcB(context), Const(16)));
  549. break;
  550. }
  551. case XmadCMode.Csfu:
  552. {
  553. Operand signAdjustA = context.ShiftLeft(context.ShiftRightU32(srcA, Const(31)), Const(16));
  554. Operand signAdjustB = context.ShiftLeft(context.ShiftRightU32(srcB, Const(31)), Const(16));
  555. srcC = context.ISubtract(srcC, context.IAdd(signAdjustA, signAdjustB));
  556. break;
  557. }
  558. default: /* TODO: Warning */ break;
  559. }
  560. Operand product = res;
  561. if (extended)
  562. {
  563. // Add with carry.
  564. res = context.IAdd(res, context.BitwiseAnd(GetCF(), Const(1)));
  565. }
  566. else
  567. {
  568. // Add (no carry in).
  569. res = context.IAdd(res, srcC);
  570. }
  571. SetIaddFlags(context, res, product, srcC, op.SetCondCode, extended);
  572. if (merge)
  573. {
  574. res = context.BitwiseAnd(res, Const(0xffff));
  575. res = context.BitwiseOr(res, context.ShiftLeft(GetSrcB(context), Const(16)));
  576. }
  577. context.Copy(GetDest(context), res);
  578. }
  579. private static Operand GetIntComparison(
  580. EmitterContext context,
  581. IntegerCondition cond,
  582. Operand srcA,
  583. Operand srcB,
  584. bool isSigned,
  585. bool extended)
  586. {
  587. return extended
  588. ? GetIntComparisonExtended(context, cond, srcA, srcB, isSigned)
  589. : GetIntComparison (context, cond, srcA, srcB, isSigned);
  590. }
  591. private static Operand GetIntComparisonExtended(
  592. EmitterContext context,
  593. IntegerCondition cond,
  594. Operand srcA,
  595. Operand srcB,
  596. bool isSigned)
  597. {
  598. Operand res;
  599. if (cond == IntegerCondition.Always)
  600. {
  601. res = Const(IrConsts.True);
  602. }
  603. else if (cond == IntegerCondition.Never)
  604. {
  605. res = Const(IrConsts.False);
  606. }
  607. else
  608. {
  609. res = context.ISubtract(srcA, srcB);
  610. res = context.IAdd(res, context.BitwiseNot(GetCF()));
  611. switch (cond)
  612. {
  613. case Decoders.IntegerCondition.Equal: // r = xh == yh && xl == yl
  614. res = context.BitwiseAnd(context.ICompareEqual(srcA, srcB), GetZF());
  615. break;
  616. case Decoders.IntegerCondition.Less: // r = xh < yh || (xh == yh && xl < yl)
  617. Operand notC = context.BitwiseNot(GetCF());
  618. Operand prevLt = context.BitwiseAnd(context.ICompareEqual(srcA, srcB), notC);
  619. res = isSigned
  620. ? context.BitwiseOr(context.ICompareLess(srcA, srcB), prevLt)
  621. : context.BitwiseOr(context.ICompareLessUnsigned(srcA, srcB), prevLt);
  622. break;
  623. case Decoders.IntegerCondition.LessOrEqual: // r = xh < yh || (xh == yh && xl <= yl)
  624. Operand zOrNotC = context.BitwiseOr(GetZF(), context.BitwiseNot(GetCF()));
  625. Operand prevLe = context.BitwiseAnd(context.ICompareEqual(srcA, srcB), zOrNotC);
  626. res = isSigned
  627. ? context.BitwiseOr(context.ICompareLess(srcA, srcB), prevLe)
  628. : context.BitwiseOr(context.ICompareLessUnsigned(srcA, srcB), prevLe);
  629. break;
  630. case Decoders.IntegerCondition.Greater: // r = xh > yh || (xh == yh && xl > yl)
  631. Operand notZAndC = context.BitwiseAnd(context.BitwiseNot(GetZF()), GetCF());
  632. Operand prevGt = context.BitwiseAnd(context.ICompareEqual(srcA, srcB), notZAndC);
  633. res = isSigned
  634. ? context.BitwiseOr(context.ICompareGreater(srcA, srcB), prevGt)
  635. : context.BitwiseOr(context.ICompareGreaterUnsigned(srcA, srcB), prevGt);
  636. break;
  637. case Decoders.IntegerCondition.GreaterOrEqual: // r = xh > yh || (xh == yh && xl >= yl)
  638. Operand prevGe = context.BitwiseAnd(context.ICompareEqual(srcA, srcB), GetCF());
  639. res = isSigned
  640. ? context.BitwiseOr(context.ICompareGreater(srcA, srcB), prevGe)
  641. : context.BitwiseOr(context.ICompareGreaterUnsigned(srcA, srcB), prevGe);
  642. break;
  643. case Decoders.IntegerCondition.NotEqual: // r = xh != yh || xl != yl
  644. context.BitwiseOr(context.ICompareNotEqual(srcA, srcB), context.BitwiseNot(GetZF()));
  645. break;
  646. default:
  647. throw new InvalidOperationException($"Unexpected condition \"{cond}\".");
  648. }
  649. }
  650. return res;
  651. }
  652. private static Operand GetIntComparison(
  653. EmitterContext context,
  654. IntegerCondition cond,
  655. Operand srcA,
  656. Operand srcB,
  657. bool isSigned)
  658. {
  659. Operand res;
  660. if (cond == IntegerCondition.Always)
  661. {
  662. res = Const(IrConsts.True);
  663. }
  664. else if (cond == IntegerCondition.Never)
  665. {
  666. res = Const(IrConsts.False);
  667. }
  668. else
  669. {
  670. var inst = cond switch
  671. {
  672. IntegerCondition.Less => Instruction.CompareLessU32,
  673. IntegerCondition.Equal => Instruction.CompareEqual,
  674. IntegerCondition.LessOrEqual => Instruction.CompareLessOrEqualU32,
  675. IntegerCondition.Greater => Instruction.CompareGreaterU32,
  676. IntegerCondition.NotEqual => Instruction.CompareNotEqual,
  677. IntegerCondition.GreaterOrEqual => Instruction.CompareGreaterOrEqualU32,
  678. _ => throw new InvalidOperationException($"Unexpected condition \"{cond}\".")
  679. };
  680. if (isSigned)
  681. {
  682. switch (cond)
  683. {
  684. case IntegerCondition.Less: inst = Instruction.CompareLess; break;
  685. case IntegerCondition.LessOrEqual: inst = Instruction.CompareLessOrEqual; break;
  686. case IntegerCondition.Greater: inst = Instruction.CompareGreater; break;
  687. case IntegerCondition.GreaterOrEqual: inst = Instruction.CompareGreaterOrEqual; break;
  688. }
  689. }
  690. res = context.Add(inst, Local(), srcA, srcB);
  691. }
  692. return res;
  693. }
  694. private static void EmitLopPredWrite(EmitterContext context, IOpCodeLop op, Operand result, ConditionalOperation condOp)
  695. {
  696. if (op is OpCodeLop opLop && !opLop.Predicate48.IsPT)
  697. {
  698. Operand pRes;
  699. if (condOp == ConditionalOperation.False)
  700. {
  701. pRes = Const(IrConsts.False);
  702. }
  703. else if (condOp == ConditionalOperation.True)
  704. {
  705. pRes = Const(IrConsts.True);
  706. }
  707. else if (condOp == ConditionalOperation.Zero)
  708. {
  709. pRes = context.ICompareEqual(result, Const(0));
  710. }
  711. else /* if (opLop.CondOp == ConditionalOperation.NotZero) */
  712. {
  713. pRes = context.ICompareNotEqual(result, Const(0));
  714. }
  715. context.Copy(Register(opLop.Predicate48), pRes);
  716. }
  717. }
  718. private static void SetIaddFlags(
  719. EmitterContext context,
  720. Operand res,
  721. Operand srcA,
  722. Operand srcB,
  723. bool setCC,
  724. bool extended)
  725. {
  726. if (!setCC)
  727. {
  728. return;
  729. }
  730. if (extended)
  731. {
  732. // C = (d == a && CIn) || d < a
  733. Operand tempC0 = context.ICompareEqual (res, srcA);
  734. Operand tempC1 = context.ICompareLessUnsigned(res, srcA);
  735. tempC0 = context.BitwiseAnd(tempC0, GetCF());
  736. context.Copy(GetCF(), context.BitwiseOr(tempC0, tempC1));
  737. }
  738. else
  739. {
  740. // C = d < a
  741. context.Copy(GetCF(), context.ICompareLessUnsigned(res, srcA));
  742. }
  743. // V = (d ^ a) & ~(a ^ b) < 0
  744. Operand tempV0 = context.BitwiseExclusiveOr(res, srcA);
  745. Operand tempV1 = context.BitwiseExclusiveOr(srcA, srcB);
  746. tempV1 = context.BitwiseNot(tempV1);
  747. Operand tempV = context.BitwiseAnd(tempV0, tempV1);
  748. context.Copy(GetVF(), context.ICompareLess(tempV, Const(0)));
  749. SetZnFlags(context, res, setCC: true, extended: extended);
  750. }
  751. }
  752. }