integer_funnel_shift.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/bit_field.h"
  5. #include "common/common_types.h"
  6. #include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
  7. namespace Shader::Maxwell {
  8. namespace {
  9. enum class MaxShift : u64 {
  10. U32,
  11. Undefined,
  12. U64,
  13. S64,
  14. };
  15. IR::U64 PackedShift(IR::IREmitter& ir, const IR::U64& packed_int, const IR::U32& safe_shift,
  16. bool right_shift, bool is_signed) {
  17. if (!right_shift) {
  18. return ir.ShiftLeftLogical(packed_int, safe_shift);
  19. }
  20. if (is_signed) {
  21. return ir.ShiftRightArithmetic(packed_int, safe_shift);
  22. }
  23. return ir.ShiftRightLogical(packed_int, safe_shift);
  24. }
  25. void SHF(TranslatorVisitor& v, u64 insn, const IR::U32& shift, const IR::U32& high_bits,
  26. bool right_shift) {
  27. union {
  28. u64 insn;
  29. BitField<0, 8, IR::Reg> dest_reg;
  30. BitField<0, 8, IR::Reg> lo_bits_reg;
  31. BitField<37, 2, MaxShift> max_shift;
  32. BitField<48, 2, u64> x_mode;
  33. BitField<50, 1, u64> wrap;
  34. } const shf{insn};
  35. if (shf.x_mode != 0) {
  36. throw NotImplementedException("SHF X Mode");
  37. }
  38. if (shf.max_shift == MaxShift::Undefined) {
  39. throw NotImplementedException("SHF Use of undefined MaxShift value");
  40. }
  41. const IR::U32 low_bits{v.X(shf.lo_bits_reg)};
  42. const IR::U64 packed_int{v.ir.PackUint2x32(v.ir.CompositeConstruct(low_bits, high_bits))};
  43. const IR::U32 max_shift{shf.max_shift == MaxShift::U32 ? v.ir.Imm32(32) : v.ir.Imm32(63)};
  44. const IR::U32 safe_shift{shf.wrap != 0
  45. ? v.ir.BitwiseAnd(shift, v.ir.ISub(max_shift, v.ir.Imm32(1)))
  46. : v.ir.UMin(shift, max_shift)};
  47. const bool is_signed{shf.max_shift == MaxShift::S64};
  48. const IR::U64 shifted_value{PackedShift(v.ir, packed_int, safe_shift, right_shift, is_signed)};
  49. const IR::Value unpacked_value{v.ir.UnpackUint2x32(shifted_value)};
  50. const IR::U32 result{v.ir.CompositeExtract(unpacked_value, right_shift ? 0 : 1)};
  51. v.X(shf.dest_reg, result);
  52. }
  53. } // Anonymous namespace
  54. void TranslatorVisitor::SHF_l_reg(u64 insn) {
  55. SHF(*this, insn, GetReg20(insn), GetReg39(insn), false);
  56. }
  57. void TranslatorVisitor::SHF_l_imm(u64 insn) {
  58. SHF(*this, insn, GetImm20(insn), GetReg39(insn), false);
  59. }
  60. void TranslatorVisitor::SHF_r_reg(u64 insn) {
  61. SHF(*this, insn, GetReg20(insn), GetReg39(insn), true);
  62. }
  63. void TranslatorVisitor::SHF_r_imm(u64 insn) {
  64. SHF(*this, insn, GetImm20(insn), GetReg39(insn), true);
  65. }
  66. } // namespace Shader::Maxwell