DoubleToFloat.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  4. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  5. {
  6. static class DoubleToFloat
  7. {
  8. public static void RunPass(HelperFunctionManager hfm, BasicBlock block)
  9. {
  10. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  11. {
  12. if (node.Value is not Operation)
  13. {
  14. continue;
  15. }
  16. node = InsertSoftFloat64(hfm, node);
  17. }
  18. }
  19. private static LinkedListNode<INode> InsertSoftFloat64(HelperFunctionManager hfm, LinkedListNode<INode> node)
  20. {
  21. Operation operation = (Operation)node.Value;
  22. if (operation.Inst == Instruction.PackDouble2x32)
  23. {
  24. int functionId = hfm.GetOrCreateFunctionId(HelperFunctionName.ConvertDoubleToFloat);
  25. Operand[] callArgs = [Const(functionId), operation.GetSource(0), operation.GetSource(1)];
  26. Operand floatValue = operation.Dest;
  27. operation.Dest = null;
  28. LinkedListNode<INode> newNode = node.List.AddBefore(node, new Operation(Instruction.Call, 0, floatValue, callArgs));
  29. Utils.DeleteNode(node, operation);
  30. return newNode;
  31. }
  32. else if (operation.Inst == Instruction.UnpackDouble2x32)
  33. {
  34. int functionId = hfm.GetOrCreateFunctionId(HelperFunctionName.ConvertFloatToDouble);
  35. // TODO: Allow UnpackDouble2x32 to produce two outputs and get rid of "operation.Index".
  36. Operand resultLow = operation.Index == 0 ? operation.Dest : Local();
  37. Operand resultHigh = operation.Index == 1 ? operation.Dest : Local();
  38. operation.Dest = null;
  39. Operand[] callArgs = [Const(functionId), operation.GetSource(0), resultLow, resultHigh];
  40. LinkedListNode<INode> newNode = node.List.AddBefore(node, new Operation(Instruction.Call, 0, (Operand)null, callArgs));
  41. Utils.DeleteNode(node, operation);
  42. return newNode;
  43. }
  44. else
  45. {
  46. operation.TurnDoubleIntoFloat();
  47. return node;
  48. }
  49. }
  50. }
  51. }