Replace PVS dictionaries with memory magic (#4795)

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
Leon Friedrich
2024-03-17 06:57:13 +11:00
committed by GitHub
parent 7aa951ca48
commit a2d0504368
22 changed files with 1051 additions and 123 deletions

View File

@@ -697,6 +697,32 @@ namespace Robust.Shared.Maths
#endregion
/// <summary>
/// Round up (ceiling) a value to a multiple of a known power of two.
/// </summary>
/// <param name="value">The value to round up.</param>
/// <param name="powerOfTwo">
/// The power of two to round up to a multiple of. The result is undefined if this is not a power of two.
/// </param>
/// <remarks>
/// The result is undefined if either value is negative.
/// </remarks>
/// <typeparam name="T">The type of integer to operate on.</typeparam>
/// <example>
/// <code>
/// MathHelper.CeilMultiplyPowerOfTwo(5, 4) // 8
/// MathHelper.CeilMultiplyPowerOfTwo(4, 4) // 4
/// MathHelper.CeilMultiplyPowerOfTwo(8, 4) // 8
/// </code>
/// </example>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T CeilMultipleOfPowerOfTwo<T>(T value, T powerOfTwo) where T : IBinaryInteger<T>
{
var mask = powerOfTwo - T.One;
var remainder = value & mask;
return remainder == T.Zero ? value : (value | mask) + T.One;
}
#endregion Public Members
}
}