Utilities.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. #if NETCORE
  5. using System.Diagnostics;
  6. using System.Runtime.CompilerServices;
  7. namespace SharpCompress.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 bitsRemaining = ((uint)bufferSize - 1) >> 4;
  16. int poolIndex = 0;
  17. if (bitsRemaining > 0xFFFF) { bitsRemaining >>= 16; poolIndex = 16; }
  18. if (bitsRemaining > 0xFF) { bitsRemaining >>= 8; poolIndex += 8; }
  19. if (bitsRemaining > 0xF) { bitsRemaining >>= 4; poolIndex += 4; }
  20. if (bitsRemaining > 0x3) { bitsRemaining >>= 2; poolIndex += 2; }
  21. if (bitsRemaining > 0x1) { bitsRemaining >>= 1; poolIndex += 1; }
  22. return poolIndex + (int)bitsRemaining;
  23. }
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. internal static int GetMaxSizeForBucket(int binIndex)
  26. {
  27. int maxSize = 16 << binIndex;
  28. Debug.Assert(maxSize >= 0);
  29. return maxSize;
  30. }
  31. }
  32. }
  33. #endif