emit_glasm_integer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "shader_recompiler/backend/glasm/emit_context.h"
  5. #include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
  6. #include "shader_recompiler/frontend/ir/value.h"
  7. namespace Shader::Backend::GLASM {
  8. void EmitIAdd32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  9. ctx.Add("ADD.S {}.x,{},{};", inst, a, b);
  10. }
  11. void EmitIAdd64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] Register a,
  12. [[maybe_unused]] Register b) {
  13. throw NotImplementedException("GLASM instruction");
  14. }
  15. void EmitISub32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  16. ctx.Add("SUB.S {}.x,{},{};", inst, a, b);
  17. }
  18. void EmitISub64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] Register a,
  19. [[maybe_unused]] Register b) {
  20. throw NotImplementedException("GLASM instruction");
  21. }
  22. void EmitIMul32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  23. ctx.Add("MUL.S {}.x,{},{};", inst, a, b);
  24. }
  25. void EmitINeg32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  26. ctx.Add("MOV.S {},-{};", inst, value);
  27. }
  28. void EmitINeg64(EmitContext& ctx, IR::Inst& inst, Register value) {
  29. ctx.LongAdd("MOV.S64 {},-{};", inst, value);
  30. }
  31. void EmitIAbs32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  32. ctx.Add("ABS.S {},{};", inst, value);
  33. }
  34. void EmitIAbs64(EmitContext& ctx, IR::Inst& inst, Register value) {
  35. ctx.LongAdd("MOV.S64 {},|{}|;", inst, value);
  36. }
  37. void EmitShiftLeftLogical32(EmitContext& ctx, IR::Inst& inst, ScalarU32 base, ScalarU32 shift) {
  38. ctx.Add("SHL.U {}.x,{},{};", inst, base, shift);
  39. }
  40. void EmitShiftLeftLogical64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] Register base,
  41. [[maybe_unused]] Register shift) {
  42. throw NotImplementedException("GLASM instruction");
  43. }
  44. void EmitShiftRightLogical32(EmitContext& ctx, IR::Inst& inst, ScalarU32 base, ScalarU32 shift) {
  45. ctx.Add("SHR.U {}.x,{},{};", inst, base, shift);
  46. }
  47. void EmitShiftRightLogical64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] Register base,
  48. [[maybe_unused]] Register shift) {
  49. throw NotImplementedException("GLASM instruction");
  50. }
  51. void EmitShiftRightArithmetic32(EmitContext& ctx, IR::Inst& inst, ScalarS32 base, ScalarS32 shift) {
  52. ctx.Add("SHR.S {}.x,{},{};", inst, base, shift);
  53. }
  54. void EmitShiftRightArithmetic64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] Register base,
  55. [[maybe_unused]] Register shift) {
  56. throw NotImplementedException("GLASM instruction");
  57. }
  58. void EmitBitwiseAnd32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  59. ctx.Add("AND.S {}.x,{},{};", inst, a, b);
  60. }
  61. void EmitBitwiseOr32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  62. ctx.Add("OR.S {}.x,{},{};", inst, a, b);
  63. }
  64. void EmitBitwiseXor32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  65. ctx.Add("XOR.S {}.x,{},{};", inst, a, b);
  66. }
  67. void EmitBitFieldInsert(EmitContext& ctx, IR::Inst& inst, ScalarS32 base, ScalarS32 insert,
  68. ScalarS32 offset, ScalarS32 count) {
  69. const Register ret{ctx.reg_alloc.Define(inst)};
  70. if (count.type != Type::Register && offset.type != Type::Register) {
  71. ctx.Add("BFI.S {},{{{},{},0,0}},{},{};", ret, count, offset, insert, base);
  72. } else {
  73. ctx.Add("MOV.S RC.x,{};"
  74. "MOV.S RC.y,{};"
  75. "BFI.S {},RC,{},{};",
  76. count, offset, ret, insert, base);
  77. }
  78. }
  79. void EmitBitFieldSExtract(EmitContext& ctx, IR::Inst& inst, ScalarS32 base, ScalarS32 offset,
  80. ScalarS32 count) {
  81. const Register ret{ctx.reg_alloc.Define(inst)};
  82. if (count.type != Type::Register && offset.type != Type::Register) {
  83. ctx.Add("BFE.S {},{{{},{},0,0}},{};", ret, count, offset, base);
  84. } else {
  85. ctx.Add("MOV.S RC.x,{};"
  86. "MOV.S RC.y,{};"
  87. "BFE.S {},RC,{};",
  88. count, offset, ret, base);
  89. }
  90. }
  91. void EmitBitFieldUExtract(EmitContext& ctx, IR::Inst& inst, ScalarU32 base, ScalarU32 offset,
  92. ScalarU32 count) {
  93. const Register ret{ctx.reg_alloc.Define(inst)};
  94. if (count.type != Type::Register && offset.type != Type::Register) {
  95. ctx.Add("BFE.U {},{{{},{},0,0}},{};", ret, count, offset, base);
  96. } else {
  97. ctx.Add("MOV.U RC.x,{};"
  98. "MOV.U RC.y,{};"
  99. "BFE.U {},RC,{};",
  100. count, offset, ret, base);
  101. }
  102. }
  103. void EmitBitReverse32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  104. ctx.Add("BFR {},{};", inst, value);
  105. }
  106. void EmitBitCount32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  107. ctx.Add("BTC {},{};", inst, value);
  108. }
  109. void EmitBitwiseNot32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  110. ctx.Add("NOT.S {},{};", inst, value);
  111. }
  112. void EmitFindSMsb32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) {
  113. ctx.Add("BTFM.S {},{};", inst, value);
  114. }
  115. void EmitFindUMsb32(EmitContext& ctx, IR::Inst& inst, ScalarU32 value) {
  116. ctx.Add("BTFM.U {},{};", inst, value);
  117. }
  118. void EmitSMin32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  119. ctx.Add("MIN.S {},{},{};", inst, a, b);
  120. }
  121. void EmitUMin32(EmitContext& ctx, IR::Inst& inst, ScalarU32 a, ScalarU32 b) {
  122. ctx.Add("MIN.U {},{},{};", inst, a, b);
  123. }
  124. void EmitSMax32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) {
  125. ctx.Add("MAX.S {},{},{};", inst, a, b);
  126. }
  127. void EmitUMax32(EmitContext& ctx, IR::Inst& inst, ScalarU32 a, ScalarU32 b) {
  128. ctx.Add("MAX.U {},{},{};", inst, a, b);
  129. }
  130. void EmitSClamp32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value, ScalarS32 min, ScalarS32 max) {
  131. const Register ret{ctx.reg_alloc.Define(inst)};
  132. ctx.Add("MIN.S RC.x,{},{};"
  133. "MAX.S {}.x,RC.x,{};",
  134. max, value, ret, min);
  135. }
  136. void EmitUClamp32(EmitContext& ctx, IR::Inst& inst, ScalarU32 value, ScalarU32 min, ScalarU32 max) {
  137. const Register ret{ctx.reg_alloc.Define(inst)};
  138. ctx.Add("MIN.U RC.x,{},{};"
  139. "MAX.U {}.x,RC.x,{};",
  140. max, value, ret, min);
  141. }
  142. void EmitSLessThan(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  143. ctx.Add("SLT.S {},{},{};", inst, lhs, rhs);
  144. }
  145. void EmitULessThan(EmitContext& ctx, IR::Inst& inst, ScalarU32 lhs, ScalarU32 rhs) {
  146. ctx.Add("SLT.U {},{},{};", inst, lhs, rhs);
  147. }
  148. void EmitIEqual(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  149. ctx.Add("SEQ.S {},{},{};", inst, lhs, rhs);
  150. }
  151. void EmitSLessThanEqual(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  152. ctx.Add("SLE.S {},{},{};", inst, lhs, rhs);
  153. }
  154. void EmitULessThanEqual(EmitContext& ctx, IR::Inst& inst, ScalarU32 lhs, ScalarU32 rhs) {
  155. ctx.Add("SLE.U {},{},{};", inst, lhs, rhs);
  156. }
  157. void EmitSGreaterThan(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  158. ctx.Add("SGT.S {},{},{};", inst, lhs, rhs);
  159. }
  160. void EmitUGreaterThan(EmitContext& ctx, IR::Inst& inst, ScalarU32 lhs, ScalarU32 rhs) {
  161. ctx.Add("SGT.U {},{},{};", inst, lhs, rhs);
  162. }
  163. void EmitINotEqual(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  164. ctx.Add("SNE.U {},{},{};", inst, lhs, rhs);
  165. }
  166. void EmitSGreaterThanEqual(EmitContext& ctx, IR::Inst& inst, ScalarS32 lhs, ScalarS32 rhs) {
  167. ctx.Add("SGE.S {},{},{};", inst, lhs, rhs);
  168. }
  169. void EmitUGreaterThanEqual(EmitContext& ctx, IR::Inst& inst, ScalarU32 lhs, ScalarU32 rhs) {
  170. ctx.Add("SGE.U {},{},{};", inst, lhs, rhs);
  171. }
  172. } // namespace Shader::Backend::GLASM