Utilities.cs 921 B

1234567891011121314151617181920212223242526272829
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Numerics;
  6. using System.Runtime.CompilerServices;
  7. namespace System.Buffers
  8. {
  9. internal static class Utilities
  10. {
  11. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  12. internal static int SelectBucketIndex(int bufferSize)
  13. {
  14. Debug.Assert(bufferSize >= 0);
  15. uint bits = ((uint)bufferSize - 1) >> 4;
  16. return 32 - BitOperations.LeadingZeroCount(bits);
  17. }
  18. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  19. internal static int GetMaxSizeForBucket(int binIndex)
  20. {
  21. int maxSize = 16 << binIndex;
  22. Debug.Assert(maxSize >= 0);
  23. return maxSize;
  24. }
  25. }
  26. }