Lop3Expression.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  4. namespace Ryujinx.Graphics.Shader.Instructions
  5. {
  6. static class Lop3Expression
  7. {
  8. public static Operand GetFromTruthTable(EmitterContext context, Operand srcA, Operand srcB, Operand srcC, int imm)
  9. {
  10. Operand expr = null;
  11. // Handle some simple cases, or cases where
  12. // the KMap would yield poor results (like XORs).
  13. if (imm == 0x96 || imm == 0x69)
  14. {
  15. // XOR (0x96) and XNOR (0x69).
  16. if (imm == 0x69)
  17. {
  18. srcA = context.BitwiseNot(srcA);
  19. }
  20. expr = context.BitwiseExclusiveOr(srcA, srcB);
  21. expr = context.BitwiseExclusiveOr(expr, srcC);
  22. return expr;
  23. }
  24. else if (imm == 0)
  25. {
  26. // Always false.
  27. return Const(IrConsts.False);
  28. }
  29. else if (imm == 0xff)
  30. {
  31. // Always true.
  32. return Const(IrConsts.True);
  33. }
  34. int map;
  35. // Encode into gray code.
  36. map = ((imm >> 0) & 1) << 0;
  37. map |= ((imm >> 1) & 1) << 4;
  38. map |= ((imm >> 2) & 1) << 1;
  39. map |= ((imm >> 3) & 1) << 5;
  40. map |= ((imm >> 4) & 1) << 3;
  41. map |= ((imm >> 5) & 1) << 7;
  42. map |= ((imm >> 6) & 1) << 2;
  43. map |= ((imm >> 7) & 1) << 6;
  44. // Solve KMap, get sum of products.
  45. int visited = 0;
  46. for (int index = 0; index < 8 && visited != 0xff; index++)
  47. {
  48. if ((map & (1 << index)) == 0)
  49. {
  50. continue;
  51. }
  52. int mask = 0;
  53. for (int mSize = 4; mSize != 0; mSize >>= 1)
  54. {
  55. mask = RotateLeft4((1 << mSize) - 1, index & 3) << (index & 4);
  56. if ((map & mask) == mask)
  57. {
  58. break;
  59. }
  60. }
  61. // The mask should wrap, if we are on the high row, shift to low etc.
  62. int mask2 = (index & 4) != 0 ? mask >> 4 : mask << 4;
  63. if ((map & mask2) == mask2)
  64. {
  65. mask |= mask2;
  66. }
  67. if ((mask & visited) == mask)
  68. {
  69. continue;
  70. }
  71. bool notA = (mask & 0x33) != 0;
  72. bool notB = (mask & 0x99) != 0;
  73. bool notC = (mask & 0x0f) != 0;
  74. bool aChanges = (mask & 0xcc) != 0 && notA;
  75. bool bChanges = (mask & 0x66) != 0 && notB;
  76. bool cChanges = (mask & 0xf0) != 0 && notC;
  77. Operand localExpr = null;
  78. void And(Operand source)
  79. {
  80. if (localExpr != null)
  81. {
  82. localExpr = context.BitwiseAnd(localExpr, source);
  83. }
  84. else
  85. {
  86. localExpr = source;
  87. }
  88. }
  89. if (!aChanges)
  90. {
  91. And(context.BitwiseNot(srcA, notA));
  92. }
  93. if (!bChanges)
  94. {
  95. And(context.BitwiseNot(srcB, notB));
  96. }
  97. if (!cChanges)
  98. {
  99. And(context.BitwiseNot(srcC, notC));
  100. }
  101. if (expr != null)
  102. {
  103. expr = context.BitwiseOr(expr, localExpr);
  104. }
  105. else
  106. {
  107. expr = localExpr;
  108. }
  109. visited |= mask;
  110. }
  111. return expr;
  112. }
  113. private static int RotateLeft4(int value, int shift)
  114. {
  115. return ((value << shift) | (value >> (4 - shift))) & 0xf;
  116. }
  117. }
  118. }