-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBitStream.cs
More file actions
168 lines (147 loc) · 4.25 KB
/
Copy pathBitStream.cs
File metadata and controls
168 lines (147 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Textures.Compression.Astc.Core;
namespace SixLabors.ImageSharp.Textures.Compression.Astc.BiseEncoding;
/// <summary>
/// A simple bit stream used for reading/writing arbitrary-sized chunks.
/// </summary>
internal struct BitStream
{
private ulong low;
private ulong high;
private uint dataSize; // number of valid bits in the 128-bit buffer
public BitStream(ulong data = 0, uint dataSize = 0)
{
this.low = data;
this.high = 0;
this.dataSize = dataSize;
}
public BitStream(UInt128 data, uint dataSize)
{
this.low = data.Low();
this.high = data.High();
this.dataSize = dataSize;
}
public readonly uint Bits => this.dataSize;
public void PutBits(ulong value, int size)
{
if (this.dataSize + (uint)size > 128)
{
throw new InvalidOperationException("Not enough space in BitStream");
}
if (this.dataSize < 64)
{
int lowFree = (int)(64 - this.dataSize);
if (size <= lowFree)
{
this.low |= (value & MaskFor(size)) << (int)this.dataSize;
}
else
{
this.low |= (value & MaskFor(lowFree)) << (int)this.dataSize;
this.high |= (value >> lowFree) & MaskFor(size - lowFree);
}
}
else
{
int shift = (int)(this.dataSize - 64);
this.high |= (value & MaskFor(size)) << shift;
}
this.dataSize += (uint)size;
}
/// <summary>
/// Attempt to retrieve the specified number of bits from the buffer as a <see cref="UInt128"/>.
/// The buffer is shifted accordingly if successful.
/// </summary>
public bool TryGetBits(int count, out UInt128 bits)
{
UInt128? result = this.GetBitsUInt128(count);
bits = result ?? default;
return result is not null;
}
public bool TryGetBits(int count, out ulong bits)
{
if (count > this.dataSize)
{
bits = 0;
return false;
}
bits = count switch
{
0 => 0,
<= 64 => this.low & MaskFor(count),
_ => this.low
};
this.ShiftBuffer(count);
return true;
}
public bool TryGetBits(int count, out uint bits)
{
if (count > this.dataSize)
{
bits = 0;
return false;
}
bits = (uint)(count switch
{
0 => 0UL,
<= 64 => this.low & MaskFor(count),
_ => this.low
});
this.ShiftBuffer(count);
return true;
}
private static ulong MaskFor(int bits)
=> bits == 64
? ~0UL
: ((1UL << bits) - 1UL);
private UInt128? GetBitsUInt128(int count)
{
if (count > this.dataSize)
{
return null;
}
UInt128 result = count switch
{
0 => UInt128.Zero,
<= 64 => (UInt128)(this.low & MaskFor(count)),
128 => new UInt128(this.high, this.low),
_ => new UInt128(
(count - 64 == 64) ? this.high : (this.high & MaskFor(count - 64)),
this.low)
};
this.ShiftBuffer(count);
return result;
}
private void ShiftBuffer(int count)
{
// C# masks shift amounts to the width of the operand, so `ulong << 64` and `ulong >> 64`
// are identity, not zero. Special-case count == 0 and count >= 128 to avoid polluting
// the low/high halves on boundary shifts.
if (count == 0)
{
// Reading zero bits is a no-op.
}
else if (count < 64)
{
this.low = (this.low >> count) | (this.high << (64 - count));
this.high >>= count;
}
else if (count == 64)
{
this.low = this.high;
this.high = 0;
}
else if (count < 128)
{
this.low = this.high >> (count - 64);
this.high = 0;
}
else
{
this.low = 0;
this.high = 0;
}
this.dataSize -= (uint)count;
}
}