arithmetic_integer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "common/common_types.h"
  6. #include "video_core/engines/shader_bytecode.h"
  7. #include "video_core/shader/shader_ir.h"
  8. namespace VideoCommon::Shader {
  9. using Tegra::Shader::IAdd3Height;
  10. using Tegra::Shader::Instruction;
  11. using Tegra::Shader::OpCode;
  12. using Tegra::Shader::Pred;
  13. using Tegra::Shader::Register;
  14. u32 ShaderIR::DecodeArithmeticInteger(NodeBlock& bb, u32 pc) {
  15. const Instruction instr = {program_code[pc]};
  16. const auto opcode = OpCode::Decode(instr);
  17. Node op_a = GetRegister(instr.gpr8);
  18. Node op_b = [&]() {
  19. if (instr.is_b_imm) {
  20. return Immediate(instr.alu.GetSignedImm20_20());
  21. } else if (instr.is_b_gpr) {
  22. return GetRegister(instr.gpr20);
  23. } else {
  24. return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
  25. }
  26. }();
  27. switch (opcode->get().GetId()) {
  28. case OpCode::Id::IADD_C:
  29. case OpCode::Id::IADD_R:
  30. case OpCode::Id::IADD_IMM: {
  31. UNIMPLEMENTED_IF_MSG(instr.alu.saturate_d, "IADD saturation not implemented");
  32. op_a = GetOperandAbsNegInteger(op_a, false, instr.alu_integer.negate_a, true);
  33. op_b = GetOperandAbsNegInteger(op_b, false, instr.alu_integer.negate_b, true);
  34. const Node value = Operation(OperationCode::IAdd, PRECISE, op_a, op_b);
  35. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  36. SetRegister(bb, instr.gpr0, value);
  37. break;
  38. }
  39. case OpCode::Id::IADD3_C:
  40. case OpCode::Id::IADD3_R:
  41. case OpCode::Id::IADD3_IMM: {
  42. Node op_c = GetRegister(instr.gpr39);
  43. const auto ApplyHeight = [&](IAdd3Height height, Node value) {
  44. switch (height) {
  45. case IAdd3Height::None:
  46. return value;
  47. case IAdd3Height::LowerHalfWord:
  48. return BitfieldExtract(value, 0, 16);
  49. case IAdd3Height::UpperHalfWord:
  50. return BitfieldExtract(value, 16, 16);
  51. default:
  52. UNIMPLEMENTED_MSG("Unhandled IADD3 height: {}", static_cast<u32>(height));
  53. return Immediate(0);
  54. }
  55. };
  56. if (opcode->get().GetId() == OpCode::Id::IADD3_R) {
  57. op_a = ApplyHeight(instr.iadd3.height_a, op_a);
  58. op_b = ApplyHeight(instr.iadd3.height_b, op_b);
  59. op_c = ApplyHeight(instr.iadd3.height_c, op_c);
  60. }
  61. op_a = GetOperandAbsNegInteger(op_a, false, instr.iadd3.neg_a, true);
  62. op_b = GetOperandAbsNegInteger(op_b, false, instr.iadd3.neg_b, true);
  63. op_c = GetOperandAbsNegInteger(op_c, false, instr.iadd3.neg_c, true);
  64. const Node value = [&]() {
  65. const Node add_ab = Operation(OperationCode::IAdd, NO_PRECISE, op_a, op_b);
  66. if (opcode->get().GetId() != OpCode::Id::IADD3_R) {
  67. return Operation(OperationCode::IAdd, NO_PRECISE, add_ab, op_c);
  68. }
  69. const Node shifted = [&]() {
  70. switch (instr.iadd3.mode) {
  71. case Tegra::Shader::IAdd3Mode::RightShift:
  72. // TODO(tech4me): According to
  73. // https://envytools.readthedocs.io/en/latest/hw/graph/maxwell/cuda/int.html?highlight=iadd3
  74. // The addition between op_a and op_b should be done in uint33, more
  75. // investigation required
  76. return Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, add_ab,
  77. Immediate(16));
  78. case Tegra::Shader::IAdd3Mode::LeftShift:
  79. return Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, add_ab,
  80. Immediate(16));
  81. default:
  82. return add_ab;
  83. }
  84. }();
  85. return Operation(OperationCode::IAdd, NO_PRECISE, shifted, op_c);
  86. }();
  87. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  88. SetRegister(bb, instr.gpr0, value);
  89. break;
  90. }
  91. case OpCode::Id::ISCADD_C:
  92. case OpCode::Id::ISCADD_R:
  93. case OpCode::Id::ISCADD_IMM: {
  94. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  95. "Condition codes generation in ISCADD is not implemented");
  96. op_a = GetOperandAbsNegInteger(op_a, false, instr.alu_integer.negate_a, true);
  97. op_b = GetOperandAbsNegInteger(op_b, false, instr.alu_integer.negate_b, true);
  98. const Node shift = Immediate(static_cast<u32>(instr.alu_integer.shift_amount));
  99. const Node shifted_a = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, op_a, shift);
  100. const Node value = Operation(OperationCode::IAdd, NO_PRECISE, shifted_a, op_b);
  101. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  102. SetRegister(bb, instr.gpr0, value);
  103. break;
  104. }
  105. case OpCode::Id::POPC_C:
  106. case OpCode::Id::POPC_R:
  107. case OpCode::Id::POPC_IMM: {
  108. if (instr.popc.invert) {
  109. op_b = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_b);
  110. }
  111. const Node value = Operation(OperationCode::IBitCount, PRECISE, op_b);
  112. SetRegister(bb, instr.gpr0, value);
  113. break;
  114. }
  115. case OpCode::Id::SEL_C:
  116. case OpCode::Id::SEL_R:
  117. case OpCode::Id::SEL_IMM: {
  118. const Node condition = GetPredicate(instr.sel.pred, instr.sel.neg_pred != 0);
  119. const Node value = Operation(OperationCode::Select, PRECISE, condition, op_a, op_b);
  120. SetRegister(bb, instr.gpr0, value);
  121. break;
  122. }
  123. case OpCode::Id::LOP_C:
  124. case OpCode::Id::LOP_R:
  125. case OpCode::Id::LOP_IMM: {
  126. if (instr.alu.lop.invert_a)
  127. op_a = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_a);
  128. if (instr.alu.lop.invert_b)
  129. op_b = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_b);
  130. WriteLogicOperation(bb, instr.gpr0, instr.alu.lop.operation, op_a, op_b,
  131. instr.alu.lop.pred_result_mode, instr.alu.lop.pred48,
  132. instr.generates_cc);
  133. break;
  134. }
  135. case OpCode::Id::LOP3_C:
  136. case OpCode::Id::LOP3_R:
  137. case OpCode::Id::LOP3_IMM: {
  138. const Node op_c = GetRegister(instr.gpr39);
  139. const Node lut = [&]() {
  140. if (opcode->get().GetId() == OpCode::Id::LOP3_R) {
  141. return Immediate(instr.alu.lop3.GetImmLut28());
  142. } else {
  143. return Immediate(instr.alu.lop3.GetImmLut48());
  144. }
  145. }();
  146. WriteLop3Instruction(bb, instr.gpr0, op_a, op_b, op_c, lut, instr.generates_cc);
  147. break;
  148. }
  149. case OpCode::Id::IMNMX_C:
  150. case OpCode::Id::IMNMX_R:
  151. case OpCode::Id::IMNMX_IMM: {
  152. UNIMPLEMENTED_IF(instr.imnmx.exchange != Tegra::Shader::IMinMaxExchange::None);
  153. const bool is_signed = instr.imnmx.is_signed;
  154. const Node condition = GetPredicate(instr.imnmx.pred, instr.imnmx.negate_pred != 0);
  155. const Node min = SignedOperation(OperationCode::IMin, is_signed, NO_PRECISE, op_a, op_b);
  156. const Node max = SignedOperation(OperationCode::IMax, is_signed, NO_PRECISE, op_a, op_b);
  157. const Node value = Operation(OperationCode::Select, NO_PRECISE, condition, min, max);
  158. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  159. SetRegister(bb, instr.gpr0, value);
  160. break;
  161. }
  162. case OpCode::Id::LEA_R2:
  163. case OpCode::Id::LEA_R1:
  164. case OpCode::Id::LEA_IMM:
  165. case OpCode::Id::LEA_RZ:
  166. case OpCode::Id::LEA_HI: {
  167. const auto [op_a, op_b, op_c] = [&]() -> std::tuple<Node, Node, Node> {
  168. switch (opcode->get().GetId()) {
  169. case OpCode::Id::LEA_R2: {
  170. return {GetRegister(instr.gpr20), GetRegister(instr.gpr39),
  171. Immediate(static_cast<u32>(instr.lea.r2.entry_a))};
  172. }
  173. case OpCode::Id::LEA_R1: {
  174. const bool neg = instr.lea.r1.neg != 0;
  175. return {GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  176. GetRegister(instr.gpr20),
  177. Immediate(static_cast<u32>(instr.lea.r1.entry_a))};
  178. }
  179. case OpCode::Id::LEA_IMM: {
  180. const bool neg = instr.lea.imm.neg != 0;
  181. return {Immediate(static_cast<u32>(instr.lea.imm.entry_a)),
  182. GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  183. Immediate(static_cast<u32>(instr.lea.imm.entry_b))};
  184. }
  185. case OpCode::Id::LEA_RZ: {
  186. const bool neg = instr.lea.rz.neg != 0;
  187. return {GetConstBuffer(instr.lea.rz.cb_index, instr.lea.rz.cb_offset),
  188. GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  189. Immediate(static_cast<u32>(instr.lea.rz.entry_a))};
  190. }
  191. case OpCode::Id::LEA_HI:
  192. default:
  193. UNIMPLEMENTED_MSG("Unhandled LEA subinstruction: {}", opcode->get().GetName());
  194. return {Immediate(static_cast<u32>(instr.lea.imm.entry_a)), GetRegister(instr.gpr8),
  195. Immediate(static_cast<u32>(instr.lea.imm.entry_b))};
  196. }
  197. }();
  198. UNIMPLEMENTED_IF_MSG(instr.lea.pred48 != static_cast<u64>(Pred::UnusedIndex),
  199. "Unhandled LEA Predicate");
  200. const Node shifted_c =
  201. Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, Immediate(1), op_c);
  202. const Node mul_bc = Operation(OperationCode::IMul, NO_PRECISE, op_b, shifted_c);
  203. const Node value = Operation(OperationCode::IAdd, NO_PRECISE, op_a, mul_bc);
  204. SetRegister(bb, instr.gpr0, value);
  205. break;
  206. }
  207. default:
  208. UNIMPLEMENTED_MSG("Unhandled ArithmeticInteger instruction: {}", opcode->get().GetName());
  209. }
  210. return pc;
  211. }
  212. void ShaderIR::WriteLop3Instruction(NodeBlock& bb, Register dest, Node op_a, Node op_b, Node op_c,
  213. Node imm_lut, bool sets_cc) {
  214. constexpr u32 lop_iterations = 32;
  215. const Node one = Immediate(1);
  216. const Node two = Immediate(2);
  217. Node value{};
  218. for (u32 i = 0; i < lop_iterations; ++i) {
  219. const Node shift_amount = Immediate(i);
  220. const Node a = Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, op_c, shift_amount);
  221. const Node pack_0 = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, a, one);
  222. const Node b = Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, op_b, shift_amount);
  223. const Node c = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, b, one);
  224. const Node pack_1 = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, c, one);
  225. const Node d = Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, op_a, shift_amount);
  226. const Node e = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, d, one);
  227. const Node pack_2 = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, e, two);
  228. const Node pack_01 = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, pack_0, pack_1);
  229. const Node pack_012 = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, pack_01, pack_2);
  230. const Node shifted_bit =
  231. Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, imm_lut, pack_012);
  232. const Node bit = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, shifted_bit, one);
  233. const Node right =
  234. Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, bit, shift_amount);
  235. if (i > 0) {
  236. value = Operation(OperationCode::IBitwiseOr, NO_PRECISE, value, right);
  237. } else {
  238. value = right;
  239. }
  240. }
  241. SetInternalFlagsFromInteger(bb, value, sets_cc);
  242. SetRegister(bb, dest, value);
  243. }
  244. } // namespace VideoCommon::Shader