2023-08-04 03:21:22 +00:00
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.CodeGen.Msl
|
|
|
|
{
|
|
|
|
static class NumberFormatter
|
|
|
|
{
|
|
|
|
private const int MaxDecimal = 256;
|
|
|
|
|
|
|
|
public static bool TryFormat(int value, AggregateType dstType, out string formatted)
|
|
|
|
{
|
2024-06-21 09:31:21 +00:00
|
|
|
switch (dstType)
|
2023-08-04 03:21:22 +00:00
|
|
|
{
|
2024-06-21 09:31:21 +00:00
|
|
|
case AggregateType.FP32:
|
|
|
|
return TryFormatFloat(BitConverter.Int32BitsToSingle(value), out formatted);
|
|
|
|
case AggregateType.S32:
|
|
|
|
formatted = FormatInt(value);
|
|
|
|
break;
|
|
|
|
case AggregateType.U32:
|
|
|
|
formatted = FormatUint((uint)value);
|
|
|
|
break;
|
|
|
|
case AggregateType.Bool:
|
|
|
|
formatted = value != 0 ? "true" : "false";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new ArgumentException($"Invalid variable type \"{dstType}\".");
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string FormatFloat(float value)
|
|
|
|
{
|
|
|
|
if (!TryFormatFloat(value, out string formatted))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Failed to convert float value to string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool TryFormatFloat(float value, out string formatted)
|
|
|
|
{
|
|
|
|
if (float.IsNaN(value) || float.IsInfinity(value))
|
|
|
|
{
|
|
|
|
formatted = null;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted = value.ToString("F9", CultureInfo.InvariantCulture);
|
|
|
|
|
|
|
|
if (!formatted.Contains('.'))
|
|
|
|
{
|
|
|
|
formatted += ".0f";
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string FormatInt(int value, AggregateType dstType)
|
|
|
|
{
|
2024-06-21 09:31:21 +00:00
|
|
|
return dstType switch
|
2023-08-04 03:21:22 +00:00
|
|
|
{
|
2024-06-21 09:31:21 +00:00
|
|
|
AggregateType.S32 => FormatInt(value),
|
|
|
|
AggregateType.U32 => FormatUint((uint)value),
|
|
|
|
_ => throw new ArgumentException($"Invalid variable type \"{dstType}\".")
|
|
|
|
};
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static string FormatInt(int value)
|
|
|
|
{
|
|
|
|
if (value <= MaxDecimal && value >= -MaxDecimal)
|
|
|
|
{
|
|
|
|
return value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
}
|
|
|
|
|
2024-07-29 00:25:27 +00:00
|
|
|
return $"as_type<int>(0x{value.ToString("X", CultureInfo.InvariantCulture)})";
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static string FormatUint(uint value)
|
|
|
|
{
|
|
|
|
if (value <= MaxDecimal && value >= 0)
|
|
|
|
{
|
|
|
|
return value.ToString(CultureInfo.InvariantCulture) + "u";
|
|
|
|
}
|
|
|
|
|
2024-07-29 00:25:27 +00:00
|
|
|
return $"as_type<uint>(0x{value.ToString("X", CultureInfo.InvariantCulture)})";
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|