diff --git a/src/ComputeSharp/Core/Helpers/FormatInterpolatedStringHandler.cs b/src/ComputeSharp/Core/Helpers/FormatInterpolatedStringHandler.cs new file mode 100644 index 00000000..9a16a49e --- /dev/null +++ b/src/ComputeSharp/Core/Helpers/FormatInterpolatedStringHandler.cs @@ -0,0 +1,297 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Buffers; +using System.Globalization; +using System.Runtime.InteropServices; + +namespace System.Runtime.CompilerServices; + +/// +/// Provides a handler used by the language compiler to process interpolated strings into instances. +/// +[InterpolatedStringHandler] +public ref struct FormatInterpolatedStringHandler +{ + /// + /// Minimum size array to rent from the pool. + /// + private const int MinimumArrayPoolLength = 256; + + /// + /// The format to use to print individual values to the target buffer. + /// + private readonly string? format; + + /// + /// Optional provider to pass to or calls. + /// + private readonly IFormatProvider? formatProvider; + + /// + /// Array rented from the array pool and used to back . + /// + private char[]? arrayToReturnToPool; + + /// + /// The span to write into. + /// + private Span characters; + + /// + /// Position at which to write the next character. + /// + private int position; + + /// + /// Whether provides an . + /// + private readonly bool hasCustomFormatter; + + /// + /// Creates a handler used to translate an interpolated string into a . + /// + /// The number of constant characters outside of interpolation expressions in the interpolated string. + /// The number of interpolation expressions in the interpolated string. + /// The format to use, if available. + /// An object that supplies culture-specific formatting information. + /// A buffer temporarily transferred to the handler for use as part of its formatting. Contents may be overwritten. + /// This is intended to be called only by compiler-generated code. Arguments are not validated as they'd otherwise be for members intended to be used directly. + public FormatInterpolatedStringHandler(int literalLength, int formattedCount, string? format, IFormatProvider? formatProvider, Span initialBuffer) + { + this.format = format; + this.formatProvider = formatProvider; + characters = initialBuffer; + arrayToReturnToPool = null; + position = 0; + hasCustomFormatter = formatProvider is not null && HasCustomFormatter(formatProvider); + } + + /// + /// Creates a new with the specified arguments. + /// + /// The format to use, if available. + /// An object that supplies culture-specific formatting information. + /// A buffer temporarily transferred to the handler for use as part of its formatting. Contents may be overwritten. + /// The input instance responsible for formatting the resulting . + /// The resulting formatted instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string Create( + string? format, + IFormatProvider? formatProvider, + Span initialBuffer, + [InterpolatedStringHandlerArgument("format", "formatProvider", "initialBuffer")] ref FormatInterpolatedStringHandler handler) + { + return handler.ToStringAndClear(); + } + + /// Gets the built and clears the handler. + /// The built string. + /// + /// This releases any resources used by the handler. The method should be invoked only + /// once and as the last thing performed on the handler. Subsequent use is erroneous, ill-defined, + /// and may destabilize the process, as may using any other copies of the handler after ToStringAndClear + /// is called on any one of them. + /// + public string ToStringAndClear() + { + string result = new(characters.Slice(0, position)); + + Clear(); + + return result; + } + + /// + /// Clears the handler, returning any rented array to the pool. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void Clear() + { + char[]? toReturn = arrayToReturnToPool; + + this = default; + + if (toReturn is not null) + { + ArrayPool.Shared.Return(toReturn); + } + } + + /// + /// Writes the specified string to the handler. + /// + /// The string to write. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendLiteral(string value) + { + if (value.Length == 1) + { + Span chars = characters; + int pos = position; + + if ((uint)pos < (uint)chars.Length) + { + chars[pos] = value[0]; + position = pos + 1; + } + else + { + GrowThenCopyString(value); + } + return; + } + + if (value.Length == 2) + { + Span chars = characters; + int pos = position; + + if ((uint)pos < chars.Length - 1) + { + Unsafe.WriteUnaligned( + ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(chars), pos)), + Unsafe.ReadUnaligned(ref Unsafe.As(ref Unsafe.AsRef(in value.GetPinnableReference())))); + + position = pos + 2; + } + else + { + GrowThenCopyString(value); + } + return; + } + + AppendStringDirect(value); + } + + /// + /// Writes the specified string to the handler. + /// + /// The string to write. + private void AppendStringDirect(string value) + { + if (value.TryCopyTo(characters.Slice(position))) + { + position += value.Length; + } + else + { + GrowThenCopyString(value); + } + } + + /// + /// Writes the specified value to the handler. + /// + /// The value to write. + public void AppendFormatted(string value) + { + AppendLiteral(value); + } + + /// + /// Writes the specified value to the handler. + /// + /// The value to write. + public void AppendFormatted(T value) + where T : ISpanFormattable + { + if (hasCustomFormatter) + { + AppendCustomFormatter(value, format); + return; + } + + int charsWritten; + + while (!value.TryFormat(characters.Slice(position), out charsWritten, format, formatProvider)) + { + Grow(); + } + + position += charsWritten; + } + + /// + /// Gets whether the provider provides a custom formatter. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static bool HasCustomFormatter(IFormatProvider provider) + { + return + provider.GetType() != typeof(CultureInfo) && + provider.GetFormat(typeof(ICustomFormatter)) != null; + } + + /// + /// Formats the value using the custom formatter from the provider. + /// + /// The value to write. + /// The format string. + [MethodImpl(MethodImplOptions.NoInlining)] + private void AppendCustomFormatter(T value, string? format) + where T : ISpanFormattable + { + ICustomFormatter? formatter = (ICustomFormatter?)formatProvider?.GetFormat(typeof(ICustomFormatter)); + + if (formatter is not null && formatter.Format(format, value, formatProvider) is string customFormatted) + { + AppendStringDirect(customFormatted); + } + } + + /// + /// Fallback for fast path in when there's not enough space in the destination. + /// + /// The string to write. + [MethodImpl(MethodImplOptions.NoInlining)] + private void GrowThenCopyString(string value) + { + Grow(value.Length); + + value.CopyTo(characters.Slice(position)); + + position += value.Length; + } + + /// + /// Grows to have the capacity to store at least beyond . + /// + [MethodImpl(MethodImplOptions.NoInlining)] + private void Grow(int additionalChars) + { + GrowCore((uint)position + (uint)additionalChars); + } + + /// + /// Grows the size of . + /// + [MethodImpl(MethodImplOptions.NoInlining)] + private void Grow() + { + GrowCore((uint)characters.Length + 1); + } + + /// + /// Grow the size of to at least the specified . + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void GrowCore(uint requiredMinCapacity) + { + uint newCapacity = Math.Max(requiredMinCapacity, Math.Min((uint)characters.Length * 2, 0x3FFFFFDF)); + int arraySize = (int)Math.Clamp(newCapacity, MinimumArrayPoolLength, int.MaxValue); + char[] newArray = ArrayPool.Shared.Rent(arraySize); + + characters.Slice(0, position).CopyTo(newArray); + + char[]? toReturn = arrayToReturnToPool; + + characters = arrayToReturnToPool = newArray; + + if (toReturn is not null) + { + ArrayPool.Shared.Return(toReturn); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Primitives/Bool/Bool.cs b/src/ComputeSharp/Core/Primitives/Bool/Bool.cs index 8f457f2c..74e7f7a1 100644 --- a/src/ComputeSharp/Core/Primitives/Bool/Bool.cs +++ b/src/ComputeSharp/Core/Primitives/Bool/Bool.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +using System; +using System.Runtime.InteropServices; namespace ComputeSharp; @@ -41,6 +42,22 @@ public readonly struct Bool return Value.ToString(); } + /// + public string ToString(IFormatProvider? formatProvider) + { + return Value.ToString(); + } + +#if !SOURCE_GENERATOR + + /// + public bool TryFormat(Span destination, out int charsWritten) + { + return Value.TryFormat(destination, out charsWritten); + } + +#endif + /// /// Checks whether or not two instances represent the same value. /// diff --git a/src/ComputeSharp/Core/Primitives/Bool/Bool2.g.cs b/src/ComputeSharp/Core/Primitives/Bool/Bool2.g.cs index a5818229..7ad9ee55 100644 --- a/src/ComputeSharp/Core/Primitives/Bool/Bool2.g.cs +++ b/src/ComputeSharp/Core/Primitives/Bool/Bool2.g.cs @@ -1,6 +1,5 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -388,20 +387,16 @@ public unsafe partial struct Bool2 /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Bool4 GGGG => ref *(Bool4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - StringBuilder sb = new(); - - sb.Append('<'); - sb.Append(this.x); - sb.Append(", "); - sb.Append(this.y); - sb.Append('>'); - - return sb.ToString(); + return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Bool/Bool3.g.cs b/src/ComputeSharp/Core/Primitives/Bool/Bool3.g.cs index 0bc86fc1..91a8c494 100644 --- a/src/ComputeSharp/Core/Primitives/Bool/Bool3.g.cs +++ b/src/ComputeSharp/Core/Primitives/Bool/Bool3.g.cs @@ -1,6 +1,5 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -1469,22 +1468,16 @@ public unsafe partial struct Bool3 /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Bool4 BBBB => ref *(Bool4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - StringBuilder sb = new(); - - sb.Append('<'); - sb.Append(this.x); - sb.Append(", "); - sb.Append(this.y); - sb.Append(", "); - sb.Append(this.z); - sb.Append('>'); - - return sb.ToString(); + return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}, {this.z}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Bool/Bool4.g.cs b/src/ComputeSharp/Core/Primitives/Bool/Bool4.g.cs index a23e5de7..61d9d1f5 100644 --- a/src/ComputeSharp/Core/Primitives/Bool/Bool4.g.cs +++ b/src/ComputeSharp/Core/Primitives/Bool/Bool4.g.cs @@ -1,6 +1,5 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -4110,24 +4109,16 @@ public unsafe partial struct Bool4 /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Bool4 AAAA => ref *(Bool4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - StringBuilder sb = new(); - - sb.Append('<'); - sb.Append(this.x); - sb.Append(", "); - sb.Append(this.y); - sb.Append(", "); - sb.Append(this.z); - sb.Append(", "); - sb.Append(this.w); - sb.Append('>'); - - return sb.ToString(); + return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}, {this.z}, {this.w}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Double/Double2.g.cs b/src/ComputeSharp/Core/Primitives/Double/Double2.g.cs index ff0fd365..07d00f73 100644 --- a/src/ComputeSharp/Core/Primitives/Double/Double2.g.cs +++ b/src/ComputeSharp/Core/Primitives/Double/Double2.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 8)] -public unsafe partial struct Double2 : IFormattable +public unsafe partial struct Double2 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -390,29 +392,30 @@ public unsafe partial struct Double2 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Double4 GGGG => ref *(Double4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Double/Double3.g.cs b/src/ComputeSharp/Core/Primitives/Double/Double3.g.cs index 5cf6e969..3e58733f 100644 --- a/src/ComputeSharp/Core/Primitives/Double/Double3.g.cs +++ b/src/ComputeSharp/Core/Primitives/Double/Double3.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 24, Pack = 8)] -public unsafe partial struct Double3 : IFormattable +public unsafe partial struct Double3 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -1471,32 +1473,30 @@ public unsafe partial struct Double3 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Double4 BBBB => ref *(Double4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.z.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Double/Double4.g.cs b/src/ComputeSharp/Core/Primitives/Double/Double4.g.cs index 618707f8..7d59f9f6 100644 --- a/src/ComputeSharp/Core/Primitives/Double/Double4.g.cs +++ b/src/ComputeSharp/Core/Primitives/Double/Double4.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 32, Pack = 8)] -public unsafe partial struct Double4 : IFormattable +public unsafe partial struct Double4 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -4112,35 +4114,30 @@ public unsafe partial struct Double4 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Double4 AAAA => ref *(Double4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.z.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.w.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Float/Float2.g.cs b/src/ComputeSharp/Core/Primitives/Float/Float2.g.cs index db950822..66289283 100644 --- a/src/ComputeSharp/Core/Primitives/Float/Float2.g.cs +++ b/src/ComputeSharp/Core/Primitives/Float/Float2.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)] -public unsafe partial struct Float2 : IFormattable +public unsafe partial struct Float2 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -390,29 +392,30 @@ public unsafe partial struct Float2 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Float4 GGGG => ref *(Float4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Float/Float3.g.cs b/src/ComputeSharp/Core/Primitives/Float/Float3.g.cs index 4ee708b3..ea7003c9 100644 --- a/src/ComputeSharp/Core/Primitives/Float/Float3.g.cs +++ b/src/ComputeSharp/Core/Primitives/Float/Float3.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)] -public unsafe partial struct Float3 : IFormattable +public unsafe partial struct Float3 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -1471,32 +1473,30 @@ public unsafe partial struct Float3 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Float4 BBBB => ref *(Float4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.z.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Float/Float4.g.cs b/src/ComputeSharp/Core/Primitives/Float/Float4.g.cs index f3cf4c17..1f7f084b 100644 --- a/src/ComputeSharp/Core/Primitives/Float/Float4.g.cs +++ b/src/ComputeSharp/Core/Primitives/Float/Float4.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)] -public unsafe partial struct Float4 : IFormattable +public unsafe partial struct Float4 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -4112,35 +4114,30 @@ public unsafe partial struct Float4 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Float4 AAAA => ref *(Float4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.z.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.w.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Int/Int2.g.cs b/src/ComputeSharp/Core/Primitives/Int/Int2.g.cs index f26cfd11..3d201d3f 100644 --- a/src/ComputeSharp/Core/Primitives/Int/Int2.g.cs +++ b/src/ComputeSharp/Core/Primitives/Int/Int2.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)] -public unsafe partial struct Int2 : IFormattable +public unsafe partial struct Int2 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -390,29 +392,30 @@ public unsafe partial struct Int2 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Int4 GGGG => ref *(Int4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Int/Int3.g.cs b/src/ComputeSharp/Core/Primitives/Int/Int3.g.cs index db4f6b55..cf8fa1a5 100644 --- a/src/ComputeSharp/Core/Primitives/Int/Int3.g.cs +++ b/src/ComputeSharp/Core/Primitives/Int/Int3.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)] -public unsafe partial struct Int3 : IFormattable +public unsafe partial struct Int3 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -1471,32 +1473,30 @@ public unsafe partial struct Int3 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Int4 BBBB => ref *(Int4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.z.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/Int/Int4.g.cs b/src/ComputeSharp/Core/Primitives/Int/Int4.g.cs index d48d6a99..14103406 100644 --- a/src/ComputeSharp/Core/Primitives/Int/Int4.g.cs +++ b/src/ComputeSharp/Core/Primitives/Int/Int4.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)] -public unsafe partial struct Int4 : IFormattable +public unsafe partial struct Int4 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -4112,35 +4114,30 @@ public unsafe partial struct Int4 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly Int4 AAAA => ref *(Int4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.z.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.w.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } +#endif + /// /// Negates a value. /// diff --git a/src/ComputeSharp/Core/Primitives/UInt/UInt2.g.cs b/src/ComputeSharp/Core/Primitives/UInt/UInt2.g.cs index 82f576c2..698dfc83 100644 --- a/src/ComputeSharp/Core/Primitives/UInt/UInt2.g.cs +++ b/src/ComputeSharp/Core/Primitives/UInt/UInt2.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)] -public unsafe partial struct UInt2 : IFormattable +public unsafe partial struct UInt2 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -390,29 +392,30 @@ public unsafe partial struct UInt2 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly UInt4 GGGG => ref *(UInt4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}>"); } +#endif + /// /// Sums two values. /// diff --git a/src/ComputeSharp/Core/Primitives/UInt/UInt3.g.cs b/src/ComputeSharp/Core/Primitives/UInt/UInt3.g.cs index d2fab955..f05fb538 100644 --- a/src/ComputeSharp/Core/Primitives/UInt/UInt3.g.cs +++ b/src/ComputeSharp/Core/Primitives/UInt/UInt3.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)] -public unsafe partial struct UInt3 : IFormattable +public unsafe partial struct UInt3 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -1471,32 +1473,30 @@ public unsafe partial struct UInt3 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly UInt4 BBBB => ref *(UInt4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.z.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } +#endif + /// /// Sums two values. /// diff --git a/src/ComputeSharp/Core/Primitives/UInt/UInt4.g.cs b/src/ComputeSharp/Core/Primitives/UInt/UInt4.g.cs index cf6735af..8e8e2d19 100644 --- a/src/ComputeSharp/Core/Primitives/UInt/UInt4.g.cs +++ b/src/ComputeSharp/Core/Primitives/UInt/UInt4.g.cs @@ -2,7 +2,6 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -14,7 +13,10 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)] -public unsafe partial struct UInt4 : IFormattable +public unsafe partial struct UInt4 +#if !SOURCE_GENERATOR + : IFormattable +#endif { /// /// A private buffer to which the undefined properties will point to. @@ -4112,35 +4114,30 @@ public unsafe partial struct UInt4 : IFormattable /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. public readonly ref readonly UInt4 AAAA => ref *(UInt4*)UndefinedData; +#if !SOURCE_GENERATOR + /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); - sb.Append(this.x.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.y.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.z.ToString(format, formatProvider)); - sb.Append(separator); - sb.Append(' '); - sb.Append(this.w.ToString(format, formatProvider)); - sb.Append('>'); - - return sb.ToString(); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } +#endif + /// /// Sums two values. /// diff --git a/src/ComputeSharp/Core/Primitives/VectorType.ttinclude b/src/ComputeSharp/Core/Primitives/VectorType.ttinclude index 2b64676a..34939eef 100644 --- a/src/ComputeSharp/Core/Primitives/VectorType.ttinclude +++ b/src/ComputeSharp/Core/Primitives/VectorType.ttinclude @@ -50,7 +50,7 @@ void GenerateVectorProperties(string typeName, int elementSize) string elementTypeName = Regex.Match(typeName, @"^[A-Za-z]+").Value; string hlslElementTypeName = elementTypeName.ToLower(); int i = int.Parse(Regex.Match(typeName, @"\d+$").Value); - string formattable = elementTypeName == "Bool" ? "" : " : IFormattable"; + string formattable = elementTypeName == "Bool" ? "" : "IFormattable"; if (elementTypeName != "Bool") { @@ -60,7 +60,6 @@ void GenerateVectorProperties(string typeName, int elementSize) #> using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; #if !NET6_0 using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers; using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal; @@ -72,7 +71,17 @@ namespace ComputeSharp; /// [StructLayout(LayoutKind.Explicit, Size = <#=(elementSize * i)#>, Pack = <#=elementSize#>)] -public unsafe partial struct <#=typeName#><#=formattable#> +public unsafe partial struct <#=typeName#> +<# + if (formattable != "") + { +#> +#if !SOURCE_GENERATOR + : <#=formattable#> +#endif +<# + } +#> { /// /// A private buffer to which the undefined properties will point to. @@ -147,35 +156,18 @@ public unsafe partial struct <#=typeName#><#=formattable#> PopIndent(); WriteLine(""); +#> +#if !SOURCE_GENERATOR + +<# + if (elementTypeName == "Bool") { #> /// public override readonly string ToString() { - StringBuilder sb = new(); - - sb.Append('<'); -<# - PushIndent(" "); - PushIndent(" "); - - for (int j = 0; j < i; j++) - { - WriteLine($"sb.Append(this.{"xyzw"[j]});"); - - if (j < i - 1) - { - WriteLine("sb.Append(\", \");"); - } - } - - PopIndent(); - PopIndent(); -#> - sb.Append('>'); - - return sb.ToString(); + return string.Create(null, stackalloc char[32], $"<<#=string.Join(", ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>"); } <# } @@ -185,41 +177,29 @@ public unsafe partial struct <#=typeName#><#=formattable#> /// public override readonly string ToString() { - return ToString("G", CultureInfo.CurrentCulture); + string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator; + + return string.Create(null, stackalloc char[64], $"<<#=string.Join("{separator} ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>"); } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { - StringBuilder sb = new(); - string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; - sb.Append('<'); + return FormatInterpolatedStringHandler.Create( + format, + formatProvider, + stackalloc char[64], + $"<<#=string.Join("{separator} ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>"); + } <# - PushIndent(" "); - PushIndent(" "); - - for (int j = 0; j < i; j++) - { - WriteLine($"sb.Append(this.{"xyzw"[j]}.ToString(format, formatProvider));"); - - if (j < i - 1) - { - WriteLine("sb.Append(separator);"); - WriteLine("sb.Append(' ');"); - } } - PopIndent(); - PopIndent(); #> - sb.Append('>'); - return sb.ToString(); - } +#endif <# - } // Generate the negation operator if (elementTypeName != "UInt")