32 行
1008 B
C#
32 行
1008 B
C#
using System.Runtime.CompilerServices;
|
|
|
|
namespace ComputeSharp.D2D1.NetStandard.System.Buffers;
|
|
|
|
/// <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;
|
|
}
|
|
} |