arithmetic_integer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/node_helper.h"
  8. #include "video_core/shader/shader_ir.h"
  9. namespace VideoCommon::Shader {
  10. using Tegra::Shader::IAdd3Height;
  11. using Tegra::Shader::Instruction;
  12. using Tegra::Shader::OpCode;
  13. using Tegra::Shader::Pred;
  14. using Tegra::Shader::Register;
  15. u32 ShaderIR::DecodeArithmeticInteger(NodeBlock& bb, u32 pc) {
  16. const Instruction instr = {program_code[pc]};
  17. const auto opcode = OpCode::Decode(instr);
  18. Node op_a = GetRegister(instr.gpr8);
  19. Node op_b = [&]() {
  20. if (instr.is_b_imm) {
  21. return Immediate(instr.alu.GetSignedImm20_20());
  22. } else if (instr.is_b_gpr) {
  23. return GetRegister(instr.gpr20);
  24. } else {
  25. return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset());
  26. }
  27. }();
  28. switch (opcode->get().GetId()) {
  29. case OpCode::Id::IADD_C:
  30. case OpCode::Id::IADD_R:
  31. case OpCode::Id::IADD_IMM: {
  32. UNIMPLEMENTED_IF_MSG(instr.alu.saturate_d, "IADD.SAT");
  33. UNIMPLEMENTED_IF_MSG(instr.iadd.x && instr.generates_cc, "IADD.X Rd.CC");
  34. op_a = GetOperandAbsNegInteger(op_a, false, instr.alu_integer.negate_a, true);
  35. op_b = GetOperandAbsNegInteger(op_b, false, instr.alu_integer.negate_b, true);
  36. Node value = Operation(OperationCode::UAdd, op_a, op_b);
  37. if (instr.iadd.x) {
  38. Node carry = GetInternalFlag(InternalFlag::Carry);
  39. Node x = Operation(OperationCode::Select, std::move(carry), Immediate(1), Immediate(0));
  40. value = Operation(OperationCode::UAdd, std::move(value), std::move(x));
  41. }
  42. if (instr.generates_cc) {
  43. const Node i0 = Immediate(0);
  44. Node zero = Operation(OperationCode::LogicalIEqual, value, i0);
  45. Node sign = Operation(OperationCode::LogicalILessThan, value, i0);
  46. Node carry = Operation(OperationCode::LogicalAddCarry, op_a, op_b);
  47. Node pos_a = Operation(OperationCode::LogicalIGreaterThan, op_a, i0);
  48. Node pos_b = Operation(OperationCode::LogicalIGreaterThan, op_b, i0);
  49. Node pos = Operation(OperationCode::LogicalAnd, std::move(pos_a), std::move(pos_b));
  50. Node overflow = Operation(OperationCode::LogicalAnd, pos, sign);
  51. SetInternalFlag(bb, InternalFlag::Zero, std::move(zero));
  52. SetInternalFlag(bb, InternalFlag::Sign, std::move(sign));
  53. SetInternalFlag(bb, InternalFlag::Carry, std::move(carry));
  54. SetInternalFlag(bb, InternalFlag::Overflow, std::move(overflow));
  55. }
  56. SetRegister(bb, instr.gpr0, std::move(value));
  57. break;
  58. }
  59. case OpCode::Id::IADD3_C:
  60. case OpCode::Id::IADD3_R:
  61. case OpCode::Id::IADD3_IMM: {
  62. Node op_c = GetRegister(instr.gpr39);
  63. const auto ApplyHeight = [&](IAdd3Height height, Node value) {
  64. switch (height) {
  65. case IAdd3Height::None:
  66. return value;
  67. case IAdd3Height::LowerHalfWord:
  68. return BitfieldExtract(value, 0, 16);
  69. case IAdd3Height::UpperHalfWord:
  70. return BitfieldExtract(value, 16, 16);
  71. default:
  72. UNIMPLEMENTED_MSG("Unhandled IADD3 height: {}", static_cast<u32>(height));
  73. return Immediate(0);
  74. }
  75. };
  76. if (opcode->get().GetId() == OpCode::Id::IADD3_R) {
  77. op_a = ApplyHeight(instr.iadd3.height_a, op_a);
  78. op_b = ApplyHeight(instr.iadd3.height_b, op_b);
  79. op_c = ApplyHeight(instr.iadd3.height_c, op_c);
  80. }
  81. op_a = GetOperandAbsNegInteger(op_a, false, instr.iadd3.neg_a, true);
  82. op_b = GetOperandAbsNegInteger(op_b, false, instr.iadd3.neg_b, true);
  83. op_c = GetOperandAbsNegInteger(op_c, false, instr.iadd3.neg_c, true);
  84. const Node value = [&]() {
  85. const Node add_ab = Operation(OperationCode::IAdd, NO_PRECISE, op_a, op_b);
  86. if (opcode->get().GetId() != OpCode::Id::IADD3_R) {
  87. return Operation(OperationCode::IAdd, NO_PRECISE, add_ab, op_c);
  88. }
  89. const Node shifted = [&]() {
  90. switch (instr.iadd3.mode) {
  91. case Tegra::Shader::IAdd3Mode::RightShift:
  92. // TODO(tech4me): According to
  93. // https://envytools.readthedocs.io/en/latest/hw/graph/maxwell/cuda/int.html?highlight=iadd3
  94. // The addition between op_a and op_b should be done in uint33, more
  95. // investigation required
  96. return Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, add_ab,
  97. Immediate(16));
  98. case Tegra::Shader::IAdd3Mode::LeftShift:
  99. return Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, add_ab,
  100. Immediate(16));
  101. default:
  102. return add_ab;
  103. }
  104. }();
  105. return Operation(OperationCode::IAdd, NO_PRECISE, shifted, op_c);
  106. }();
  107. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  108. SetRegister(bb, instr.gpr0, value);
  109. break;
  110. }
  111. case OpCode::Id::ISCADD_C:
  112. case OpCode::Id::ISCADD_R:
  113. case OpCode::Id::ISCADD_IMM: {
  114. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  115. "Condition codes generation in ISCADD is not implemented");
  116. op_a = GetOperandAbsNegInteger(op_a, false, instr.alu_integer.negate_a, true);
  117. op_b = GetOperandAbsNegInteger(op_b, false, instr.alu_integer.negate_b, true);
  118. const Node shift = Immediate(static_cast<u32>(instr.alu_integer.shift_amount));
  119. const Node shifted_a = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, op_a, shift);
  120. const Node value = Operation(OperationCode::IAdd, NO_PRECISE, shifted_a, op_b);
  121. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  122. SetRegister(bb, instr.gpr0, value);
  123. break;
  124. }
  125. case OpCode::Id::POPC_C:
  126. case OpCode::Id::POPC_R:
  127. case OpCode::Id::POPC_IMM: {
  128. if (instr.popc.invert) {
  129. op_b = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_b);
  130. }
  131. const Node value = Operation(OperationCode::IBitCount, PRECISE, op_b);
  132. SetRegister(bb, instr.gpr0, value);
  133. break;
  134. }
  135. case OpCode::Id::FLO_R:
  136. case OpCode::Id::FLO_C:
  137. case OpCode::Id::FLO_IMM: {
  138. Node value;
  139. if (instr.flo.invert) {
  140. op_b = Operation(OperationCode::IBitwiseNot, NO_PRECISE, std::move(op_b));
  141. }
  142. if (instr.flo.is_signed) {
  143. value = Operation(OperationCode::IBitMSB, NO_PRECISE, std::move(op_b));
  144. } else {
  145. value = Operation(OperationCode::UBitMSB, NO_PRECISE, std::move(op_b));
  146. }
  147. if (instr.flo.sh) {
  148. value =
  149. Operation(OperationCode::UBitwiseXor, NO_PRECISE, std::move(value), Immediate(31));
  150. }
  151. SetRegister(bb, instr.gpr0, std::move(value));
  152. break;
  153. }
  154. case OpCode::Id::SEL_C:
  155. case OpCode::Id::SEL_R:
  156. case OpCode::Id::SEL_IMM: {
  157. const Node condition = GetPredicate(instr.sel.pred, instr.sel.neg_pred != 0);
  158. const Node value = Operation(OperationCode::Select, PRECISE, condition, op_a, op_b);
  159. SetRegister(bb, instr.gpr0, value);
  160. break;
  161. }
  162. case OpCode::Id::ICMP_CR:
  163. case OpCode::Id::ICMP_R:
  164. case OpCode::Id::ICMP_RC:
  165. case OpCode::Id::ICMP_IMM: {
  166. const Node zero = Immediate(0);
  167. const auto [op_rhs, test] = [&]() -> std::pair<Node, Node> {
  168. switch (opcode->get().GetId()) {
  169. case OpCode::Id::ICMP_CR:
  170. return {GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset()),
  171. GetRegister(instr.gpr39)};
  172. case OpCode::Id::ICMP_R:
  173. return {GetRegister(instr.gpr20), GetRegister(instr.gpr39)};
  174. case OpCode::Id::ICMP_RC:
  175. return {GetRegister(instr.gpr39),
  176. GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset())};
  177. case OpCode::Id::ICMP_IMM:
  178. return {Immediate(instr.alu.GetSignedImm20_20()), GetRegister(instr.gpr39)};
  179. default:
  180. UNREACHABLE();
  181. return {zero, zero};
  182. }
  183. }();
  184. const Node op_lhs = GetRegister(instr.gpr8);
  185. const Node comparison =
  186. GetPredicateComparisonInteger(instr.icmp.cond, instr.icmp.is_signed != 0, test, zero);
  187. SetRegister(bb, instr.gpr0, Operation(OperationCode::Select, comparison, op_lhs, op_rhs));
  188. break;
  189. }
  190. case OpCode::Id::LOP_C:
  191. case OpCode::Id::LOP_R:
  192. case OpCode::Id::LOP_IMM: {
  193. if (instr.alu.lop.invert_a)
  194. op_a = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_a);
  195. if (instr.alu.lop.invert_b)
  196. op_b = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_b);
  197. WriteLogicOperation(bb, instr.gpr0, instr.alu.lop.operation, op_a, op_b,
  198. instr.alu.lop.pred_result_mode, instr.alu.lop.pred48,
  199. instr.generates_cc);
  200. break;
  201. }
  202. case OpCode::Id::LOP3_C:
  203. case OpCode::Id::LOP3_R:
  204. case OpCode::Id::LOP3_IMM: {
  205. const Node op_c = GetRegister(instr.gpr39);
  206. const Node lut = [&]() {
  207. if (opcode->get().GetId() == OpCode::Id::LOP3_R) {
  208. return Immediate(instr.alu.lop3.GetImmLut28());
  209. } else {
  210. return Immediate(instr.alu.lop3.GetImmLut48());
  211. }
  212. }();
  213. WriteLop3Instruction(bb, instr.gpr0, op_a, op_b, op_c, lut, instr.generates_cc);
  214. break;
  215. }
  216. case OpCode::Id::IMNMX_C:
  217. case OpCode::Id::IMNMX_R:
  218. case OpCode::Id::IMNMX_IMM: {
  219. UNIMPLEMENTED_IF(instr.imnmx.exchange != Tegra::Shader::IMinMaxExchange::None);
  220. const bool is_signed = instr.imnmx.is_signed;
  221. const Node condition = GetPredicate(instr.imnmx.pred, instr.imnmx.negate_pred != 0);
  222. const Node min = SignedOperation(OperationCode::IMin, is_signed, NO_PRECISE, op_a, op_b);
  223. const Node max = SignedOperation(OperationCode::IMax, is_signed, NO_PRECISE, op_a, op_b);
  224. const Node value = Operation(OperationCode::Select, NO_PRECISE, condition, min, max);
  225. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  226. SetRegister(bb, instr.gpr0, value);
  227. break;
  228. }
  229. case OpCode::Id::LEA_R2:
  230. case OpCode::Id::LEA_R1:
  231. case OpCode::Id::LEA_IMM:
  232. case OpCode::Id::LEA_RZ:
  233. case OpCode::Id::LEA_HI: {
  234. auto [op_a, op_b, op_c] = [&]() -> std::tuple<Node, Node, Node> {
  235. switch (opcode->get().GetId()) {
  236. case OpCode::Id::LEA_R2: {
  237. return {GetRegister(instr.gpr20), GetRegister(instr.gpr39),
  238. Immediate(static_cast<u32>(instr.lea.r2.entry_a))};
  239. }
  240. case OpCode::Id::LEA_R1: {
  241. const bool neg = instr.lea.r1.neg != 0;
  242. return {GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  243. GetRegister(instr.gpr20),
  244. Immediate(static_cast<u32>(instr.lea.r1.entry_a))};
  245. }
  246. case OpCode::Id::LEA_IMM: {
  247. const bool neg = instr.lea.imm.neg != 0;
  248. return {GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  249. Immediate(static_cast<u32>(instr.lea.imm.entry_a)),
  250. Immediate(static_cast<u32>(instr.lea.imm.entry_b))};
  251. }
  252. case OpCode::Id::LEA_RZ: {
  253. const bool neg = instr.lea.rz.neg != 0;
  254. return {GetConstBuffer(instr.lea.rz.cb_index, instr.lea.rz.cb_offset),
  255. GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  256. Immediate(static_cast<u32>(instr.lea.rz.entry_a))};
  257. }
  258. case OpCode::Id::LEA_HI:
  259. default:
  260. UNIMPLEMENTED_MSG("Unhandled LEA subinstruction: {}", opcode->get().GetName());
  261. return {Immediate(static_cast<u32>(instr.lea.imm.entry_a)), GetRegister(instr.gpr8),
  262. Immediate(static_cast<u32>(instr.lea.imm.entry_b))};
  263. }
  264. }();
  265. UNIMPLEMENTED_IF_MSG(instr.lea.pred48 != static_cast<u64>(Pred::UnusedIndex),
  266. "Unhandled LEA Predicate");
  267. Node value = Operation(OperationCode::ILogicalShiftLeft, std::move(op_a), std::move(op_c));
  268. value = Operation(OperationCode::IAdd, std::move(op_b), std::move(value));
  269. SetRegister(bb, instr.gpr0, std::move(value));
  270. break;
  271. }
  272. default:
  273. UNIMPLEMENTED_MSG("Unhandled ArithmeticInteger instruction: {}", opcode->get().GetName());
  274. }
  275. return pc;
  276. }
  277. void ShaderIR::WriteLop3Instruction(NodeBlock& bb, Register dest, Node op_a, Node op_b, Node op_c,
  278. Node imm_lut, bool sets_cc) {
  279. const Node lop3_fast = [&](const Node na, const Node nb, const Node nc, const Node ttbl) {
  280. Node value = Immediate(0);
  281. const ImmediateNode imm = std::get<ImmediateNode>(*ttbl);
  282. if (imm.GetValue() & 0x01) {
  283. const Node a = Operation(OperationCode::IBitwiseNot, na);
  284. const Node b = Operation(OperationCode::IBitwiseNot, nb);
  285. const Node c = Operation(OperationCode::IBitwiseNot, nc);
  286. Node r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, a, b);
  287. r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, r, c);
  288. value = Operation(OperationCode::IBitwiseOr, value, r);
  289. }
  290. if (imm.GetValue() & 0x02) {
  291. const Node a = Operation(OperationCode::IBitwiseNot, na);
  292. const Node b = Operation(OperationCode::IBitwiseNot, nb);
  293. Node r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, a, b);
  294. r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, r, nc);
  295. value = Operation(OperationCode::IBitwiseOr, value, r);
  296. }
  297. if (imm.GetValue() & 0x04) {
  298. const Node a = Operation(OperationCode::IBitwiseNot, na);
  299. const Node c = Operation(OperationCode::IBitwiseNot, nc);
  300. Node r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, a, nb);
  301. r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, r, c);
  302. value = Operation(OperationCode::IBitwiseOr, value, r);
  303. }
  304. if (imm.GetValue() & 0x08) {
  305. const Node a = Operation(OperationCode::IBitwiseNot, na);
  306. Node r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, a, nb);
  307. r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, r, nc);
  308. value = Operation(OperationCode::IBitwiseOr, value, r);
  309. }
  310. if (imm.GetValue() & 0x10) {
  311. const Node b = Operation(OperationCode::IBitwiseNot, nb);
  312. const Node c = Operation(OperationCode::IBitwiseNot, nc);
  313. Node r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, na, b);
  314. r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, r, c);
  315. value = Operation(OperationCode::IBitwiseOr, value, r);
  316. }
  317. if (imm.GetValue() & 0x20) {
  318. const Node b = Operation(OperationCode::IBitwiseNot, nb);
  319. Node r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, na, b);
  320. r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, r, nc);
  321. value = Operation(OperationCode::IBitwiseOr, value, r);
  322. }
  323. if (imm.GetValue() & 0x40) {
  324. const Node c = Operation(OperationCode::IBitwiseNot, nc);
  325. Node r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, na, nb);
  326. r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, r, c);
  327. value = Operation(OperationCode::IBitwiseOr, value, r);
  328. }
  329. if (imm.GetValue() & 0x80) {
  330. Node r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, na, nb);
  331. r = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, r, nc);
  332. value = Operation(OperationCode::IBitwiseOr, value, r);
  333. }
  334. return value;
  335. }(op_a, op_b, op_c, imm_lut);
  336. SetInternalFlagsFromInteger(bb, lop3_fast, sets_cc);
  337. SetRegister(bb, dest, lop3_fast);
  338. }
  339. } // namespace VideoCommon::Shader