InstEmitShift.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
  5. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  6. namespace Ryujinx.Graphics.Shader.Instructions
  7. {
  8. static partial class InstEmit
  9. {
  10. public static void ShlR(EmitterContext context)
  11. {
  12. InstShlR op = context.GetOp<InstShlR>();
  13. EmitShl(context, GetSrcReg(context, op.SrcA), GetSrcReg(context, op.SrcB), op.Dest, op.M);
  14. }
  15. public static void ShlI(EmitterContext context)
  16. {
  17. InstShlI op = context.GetOp<InstShlI>();
  18. EmitShl(context, GetSrcReg(context, op.SrcA), GetSrcImm(context, Imm20ToSInt(op.Imm20)), op.Dest, op.M);
  19. }
  20. public static void ShlC(EmitterContext context)
  21. {
  22. InstShlC op = context.GetOp<InstShlC>();
  23. EmitShl(context, GetSrcReg(context, op.SrcA), GetSrcCbuf(context, op.CbufSlot, op.CbufOffset), op.Dest, op.M);
  24. }
  25. public static void ShrR(EmitterContext context)
  26. {
  27. InstShrR op = context.GetOp<InstShrR>();
  28. var srcA = GetSrcReg(context, op.SrcA);
  29. var srcB = GetSrcReg(context, op.SrcB);
  30. EmitShr(context, srcA, srcB, op.Dest, op.M, op.Brev, op.Signed);
  31. }
  32. public static void ShrI(EmitterContext context)
  33. {
  34. InstShrI op = context.GetOp<InstShrI>();
  35. var srcA = GetSrcReg(context, op.SrcA);
  36. var srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  37. EmitShr(context, srcA, srcB, op.Dest, op.M, op.Brev, op.Signed);
  38. }
  39. public static void ShrC(EmitterContext context)
  40. {
  41. InstShrC op = context.GetOp<InstShrC>();
  42. var srcA = GetSrcReg(context, op.SrcA);
  43. var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  44. EmitShr(context, srcA, srcB, op.Dest, op.M, op.Brev, op.Signed);
  45. }
  46. private static void EmitShl(EmitterContext context, Operand srcA, Operand srcB, int rd, bool mask)
  47. {
  48. if (mask)
  49. {
  50. srcB = context.BitwiseAnd(srcB, Const(0x1f));
  51. }
  52. Operand res = context.ShiftLeft(srcA, srcB);
  53. if (!mask)
  54. {
  55. // Clamped shift value.
  56. Operand isLessThan32 = context.ICompareLessUnsigned(srcB, Const(32));
  57. res = context.ConditionalSelect(isLessThan32, res, Const(0));
  58. }
  59. // TODO: X, CC.
  60. context.Copy(GetDest(rd), res);
  61. }
  62. private static void EmitShr(
  63. EmitterContext context,
  64. Operand srcA,
  65. Operand srcB,
  66. int rd,
  67. bool mask,
  68. bool bitReverse,
  69. bool isSigned)
  70. {
  71. if (bitReverse)
  72. {
  73. srcA = context.BitfieldReverse(srcA);
  74. }
  75. if (mask)
  76. {
  77. srcB = context.BitwiseAnd(srcB, Const(0x1f));
  78. }
  79. Operand res = isSigned
  80. ? context.ShiftRightS32(srcA, srcB)
  81. : context.ShiftRightU32(srcA, srcB);
  82. if (!mask)
  83. {
  84. // Clamped shift value.
  85. Operand resShiftBy32;
  86. if (isSigned)
  87. {
  88. resShiftBy32 = context.ShiftRightS32(srcA, Const(31));
  89. }
  90. else
  91. {
  92. resShiftBy32 = Const(0);
  93. }
  94. Operand isLessThan32 = context.ICompareLessUnsigned(srcB, Const(32));
  95. res = context.ConditionalSelect(isLessThan32, res, resShiftBy32);
  96. }
  97. // TODO: X, CC.
  98. context.Copy(GetDest(rd), res);
  99. }
  100. }
  101. }