Skip to content

Commit e39b439

Browse files
committed
Use UInt128 in BitStream
1 parent f2da43b commit e39b439

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

AssetRipper.TextureDecoder/Bc/BitStream.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,34 @@
22

33
internal ref struct BitStream
44
{
5-
private ulong low;
6-
private ulong high;
5+
private UInt128 value;
6+
7+
private readonly uint BottomBits
8+
{
9+
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
10+
get => unchecked((uint)this.value);
11+
}
712

813
public BitStream(ulong low, ulong high)
914
{
10-
this.low = low;
11-
this.high = high;
15+
value = new UInt128(high, low);
1216
}
13-
17+
18+
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
1419
public uint ReadBits(int numBits)
1520
{
1621
uint mask = (1u << numBits) - 1u;
17-
// Read the low N bits
18-
uint bits = unchecked((uint)(this.low & mask));
19-
20-
this.low >>= numBits;
21-
// Put the low N bits of "high" into the high 64-N bits of "low".
22-
this.low |= (this.high & mask) << ((sizeof(ulong) * 8) - numBits);
23-
this.high >>= numBits;
24-
22+
uint bits = BottomBits & mask;
23+
this.value >>= numBits;
2524
return bits;
2625
}
2726

2827
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
2928
public uint ReadBit()
3029
{
31-
return this.ReadBits(1);
30+
uint result = BottomBits & 1u;
31+
this.value >>= 1;
32+
return result;
3233
}
3334

3435
/// <summary>

0 commit comments

Comments
 (0)