Skip to content

Commit

Permalink
Simplify 64bit mask
Browse files Browse the repository at this point in the history
  • Loading branch information
TechPizzaDev committed Jan 23, 2025
1 parent c8ae57e commit 2aeeb88
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
7 changes: 0 additions & 7 deletions NVorbis/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ internal static class Utils
private const float LowerClip = -0.99999994f;
private const float UpperClip = 0.99999994f;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong Mask64(int bitCount)
{
ulong mask = bitCount == 0 ? 0 : ulong.MaxValue;
return mask >> (64 - bitCount);
}

internal static int ilog(int x)
{
int cnt = 0;
Expand Down
12 changes: 9 additions & 3 deletions NVorbis/VorbisPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ private ulong RefillBits(ref int count)
{
count = _bitCount;
}
return _bitBucket & Utils.Mask64(count);

ulong value = _bitBucket;
ulong mask = ulong.MaxValue >> (64 - count);
return value & mask;
}

/// <summary>
Expand All @@ -194,14 +197,17 @@ private ulong RefillBits(ref int count)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong TryPeekBits(int count, out int bitsRead)
{
Debug.Assert((uint)count <= 64);
Debug.Assert((uint)(count - 1) < 64);

bitsRead = count;
if (_bitCount < count)
{
return RefillBits(ref bitsRead);
}
return _bitBucket & Utils.Mask64(count);

ulong value = _bitBucket;
ulong mask = ulong.MaxValue >> (64 - count);
return value & mask;
}

/// <summary>
Expand Down

0 comments on commit 2aeeb88

Please sign in to comment.