InstEmitConversion.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.Instructions.InstEmitAluHelper;
  6. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  7. namespace Ryujinx.Graphics.Shader.Instructions
  8. {
  9. static partial class InstEmit
  10. {
  11. public static void F2F(EmitterContext context)
  12. {
  13. OpCodeFArith op = (OpCodeFArith)context.CurrOp;
  14. FPType srcType = (FPType)op.RawOpCode.Extract(8, 2);
  15. FPType dstType = (FPType)op.RawOpCode.Extract(10, 2);
  16. bool pass = op.RawOpCode.Extract(40);
  17. bool negateB = op.RawOpCode.Extract(45);
  18. bool absoluteB = op.RawOpCode.Extract(49);
  19. pass &= op.RoundingMode == RoundingMode.TowardsNegativeInfinity;
  20. Operand srcB = context.FPAbsNeg(GetSrcB(context, srcType), absoluteB, negateB);
  21. if (!pass)
  22. {
  23. switch (op.RoundingMode)
  24. {
  25. case RoundingMode.TowardsNegativeInfinity:
  26. srcB = context.FPFloor(srcB);
  27. break;
  28. case RoundingMode.TowardsPositiveInfinity:
  29. srcB = context.FPCeiling(srcB);
  30. break;
  31. case RoundingMode.TowardsZero:
  32. srcB = context.FPTruncate(srcB);
  33. break;
  34. }
  35. }
  36. srcB = context.FPSaturate(srcB, op.Saturate);
  37. WriteFP(context, dstType, srcB);
  38. // TODO: CC.
  39. }
  40. public static void F2I(EmitterContext context)
  41. {
  42. OpCodeFArith op = (OpCodeFArith)context.CurrOp;
  43. IntegerType intType = (IntegerType)op.RawOpCode.Extract(8, 2);
  44. bool isSmallInt = intType <= IntegerType.U16;
  45. FPType floatType = (FPType)op.RawOpCode.Extract(10, 2);
  46. bool isSignedInt = op.RawOpCode.Extract(12);
  47. bool negateB = op.RawOpCode.Extract(45);
  48. bool absoluteB = op.RawOpCode.Extract(49);
  49. if (isSignedInt)
  50. {
  51. intType |= IntegerType.S8;
  52. }
  53. Operand srcB = context.FPAbsNeg(GetSrcB(context, floatType), absoluteB, negateB);
  54. switch (op.RoundingMode)
  55. {
  56. case RoundingMode.TowardsNegativeInfinity:
  57. srcB = context.FPFloor(srcB);
  58. break;
  59. case RoundingMode.TowardsPositiveInfinity:
  60. srcB = context.FPCeiling(srcB);
  61. break;
  62. case RoundingMode.TowardsZero:
  63. srcB = context.FPTruncate(srcB);
  64. break;
  65. }
  66. srcB = context.FPConvertToS32(srcB);
  67. // TODO: S/U64, conversion overflow handling.
  68. if (intType != IntegerType.S32)
  69. {
  70. int min = GetIntMin(intType);
  71. int max = GetIntMax(intType);
  72. srcB = isSignedInt
  73. ? context.IClampS32(srcB, Const(min), Const(max))
  74. : context.IClampU32(srcB, Const(min), Const(max));
  75. }
  76. Operand dest = GetDest(context);
  77. context.Copy(dest, srcB);
  78. // TODO: CC.
  79. }
  80. public static void I2F(EmitterContext context)
  81. {
  82. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  83. FPType dstType = (FPType)op.RawOpCode.Extract(8, 2);
  84. IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);
  85. bool isSmallInt = srcType <= IntegerType.U16;
  86. bool isSignedInt = op.RawOpCode.Extract(13);
  87. bool negateB = op.RawOpCode.Extract(45);
  88. bool absoluteB = op.RawOpCode.Extract(49);
  89. Operand srcB = context.IAbsNeg(GetSrcB(context), absoluteB, negateB);
  90. if (isSmallInt)
  91. {
  92. int size = srcType == IntegerType.U16 ? 16 : 8;
  93. srcB = isSignedInt
  94. ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
  95. : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
  96. }
  97. srcB = isSignedInt
  98. ? context.IConvertS32ToFP(srcB)
  99. : context.IConvertU32ToFP(srcB);
  100. WriteFP(context, dstType, srcB);
  101. // TODO: CC.
  102. }
  103. public static void I2I(EmitterContext context)
  104. {
  105. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  106. IntegerType dstType = (IntegerType)op.RawOpCode.Extract(8, 2);
  107. IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);
  108. if (srcType == IntegerType.U64 || dstType == IntegerType.U64)
  109. {
  110. // TODO: Warning. This instruction doesn't support 64-bits integers
  111. }
  112. bool srcIsSmallInt = srcType <= IntegerType.U16;
  113. bool dstIsSignedInt = op.RawOpCode.Extract(12);
  114. bool srcIsSignedInt = op.RawOpCode.Extract(13);
  115. bool negateB = op.RawOpCode.Extract(45);
  116. bool absoluteB = op.RawOpCode.Extract(49);
  117. Operand srcB = GetSrcB(context);
  118. if (srcIsSmallInt)
  119. {
  120. int size = srcType == IntegerType.U16 ? 16 : 8;
  121. srcB = srcIsSignedInt
  122. ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
  123. : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
  124. }
  125. srcB = context.IAbsNeg(srcB, absoluteB, negateB);
  126. if (op.Saturate)
  127. {
  128. if (dstIsSignedInt)
  129. {
  130. dstType |= IntegerType.S8;
  131. }
  132. int min = GetIntMin(dstType);
  133. int max = GetIntMax(dstType);
  134. srcB = dstIsSignedInt
  135. ? context.IClampS32(srcB, Const(min), Const(max))
  136. : context.IClampU32(srcB, Const(min), Const(max));
  137. }
  138. context.Copy(GetDest(context), srcB);
  139. // TODO: CC.
  140. }
  141. private static void WriteFP(EmitterContext context, FPType type, Operand srcB)
  142. {
  143. Operand dest = GetDest(context);
  144. if (type == FPType.FP32)
  145. {
  146. context.Copy(dest, srcB);
  147. }
  148. else if (type == FPType.FP16)
  149. {
  150. context.Copy(dest, context.PackHalf2x16(srcB, ConstF(0)));
  151. }
  152. else
  153. {
  154. // TODO.
  155. }
  156. }
  157. }
  158. }