Centralize polyfills in a single ComputeSharp.NetStandard project
このコミットが含まれているのは:
1 親
d7fba62884
コミット
a09a817b5c
68ファイル変更
+106
-257
検索条件に一致するファイルはありません
@@ -0,0 +1,32 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.NetStandard;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="BitOperations"/> on .NET 6.
|
||||
/// </summary>
|
||||
internal static class BitOperations
|
||||
{
|
||||
/// <summary>
|
||||
/// Rounds the given integral value up to a power of 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns>
|
||||
/// The smallest power of 2 which is greater than or equal to <paramref name="value"/>.
|
||||
/// If <paramref name="value"/> is 0 or the result overflows, returns 0.
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint RoundUpToPowerOf2(uint value)
|
||||
{
|
||||
// Based on https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
||||
--value;
|
||||
|
||||
value |= value >> 1;
|
||||
value |= value >> 2;
|
||||
value |= value >> 4;
|
||||
value |= value >> 8;
|
||||
value |= value >> 16;
|
||||
|
||||
return value + 1;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace ComputeSharp.NetStandard.System;
|
||||
namespace ComputeSharp.NetStandard;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="global::System.Enum"/> on .NET 6.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace ComputeSharp.NetStandard.System;
|
||||
namespace ComputeSharp.NetStandard;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="global::System.GC"/> on .NET 6.
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.NetStandard.System.IO;
|
||||
namespace ComputeSharp.NetStandard;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="global::System.IO.Path"/> on .NET 6.
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.NetStandard;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="global::System.Math"/> on .NET 6.
|
||||
/// </summary>
|
||||
internal static class Math
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns <paramref name="value"/> clamped to the inclusive range of <paramref name="min"/> and <paramref name="max"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to be clamped.</param>
|
||||
/// <param name="min">The lower bound of the result.</param>
|
||||
/// <param name="max">The upper bound of the result.</param>
|
||||
/// <returns>The input value clamped to the inclusive range of <paramref name="min"/> and <paramref name="max"/>.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Clamp(int value, int min, int max)
|
||||
{
|
||||
if (value < min)
|
||||
{
|
||||
return min;
|
||||
}
|
||||
|
||||
if (value > max)
|
||||
{
|
||||
return max;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="global::System.Math.Min(int, int)"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Min(int val1, int val2)
|
||||
{
|
||||
return global::System.Math.Min(val1, val2);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="global::System.Math.Max(int, int)"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Max(int val1, int val2)
|
||||
{
|
||||
return global::System.Math.Max(val1, val2);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ComputeSharp.NetStandard.System.Runtime.CompilerServices;
|
||||
namespace ComputeSharp.NetStandard;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="RuntimeHelpers"/> on .NET 5.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.NetStandard.System.Runtime.InteropServices;
|
||||
namespace ComputeSharp.NetStandard;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="MemoryMarshal"/> on .NET 6.
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace System.Text;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="Encoding"/> on .NET 5.
|
||||
/// </summary>
|
||||
internal static class EncodingExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Encodes into a span of bytes a set of characters from the specified read-only span.
|
||||
/// </summary>
|
||||
/// <param name="encoding">The input <see cref="Encoding"/> instance to use.</param>
|
||||
/// <param name="chars">The span containing the set of characters to encode.</param>
|
||||
/// <param name="bytes">The byte span to hold the encoded bytes.</param>
|
||||
/// <returns>The number of encoded bytes.</returns>
|
||||
public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan<char> chars, Span<byte> bytes)
|
||||
{
|
||||
fixed (char* charsPtr = &MemoryMarshal.GetReference(chars))
|
||||
fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes))
|
||||
{
|
||||
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a text from a sequence of bytes.
|
||||
/// </summary>
|
||||
/// <param name="encoding">The input <see cref="Encoding"/> instance to use.</param>
|
||||
/// <param name="bytes">The byte span that holds the encoded bytes.</param>
|
||||
/// <returns>The resulting text.</returns>
|
||||
public static unsafe string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes))
|
||||
{
|
||||
return encoding.GetString(bytesPtr, bytes.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする