BitStream.h(3)
π BitStream.h(3) β the libpgf bit-wrangling page π
okay bestie, sit down, hydrate, because this is BitStream.h from libpgf, and it's a section 3 manual page β so yes, it's a library page, not a "type this in your shell" page. this is where all the bit-stream sorcery lives: bits, words, masks, and every lil helper that shuffles them around. it's giving β¨ low-level β¨ and honestly? we're obsessed. π
πΈ NAME πΈ
BitStream.h
β¨ SYNOPSIS β¨
#include 'PGFtypes.h'
π Macros π
#define MAKEU64(a, b)β Make 64 bit unsigned integer from two 32 bit unsigned integers. (two 32s, one 64, no cap π«Ά)
π¦ Functions π¦
void SetBit (UINT32 *stream, UINT32 pos)void ClearBit (UINT32 *stream, UINT32 pos)bool GetBit (UINT32 *stream, UINT32 pos)bool CompareBitBlock (UINT32 *stream, UINT32 pos, UINT32 k, UINT32 val)void SetValueBlock (UINT32 *stream, UINT32 pos, UINT32 val, UINT32 k)UINT32 GetValueBlock (UINT32 *stream, UINT32 pos, UINT32 k)void ClearBitBlock (UINT32 *stream, UINT32 pos, UINT32 len)void SetBitBlock (UINT32 *stream, UINT32 pos, UINT32 len)UINT32 SeekBitRange (UINT32 *stream, UINT32 pos, UINT32 len)UINT32 SeekBit1Range (UINT32 *stream, UINT32 pos, UINT32 len)UINT32 AlignWordPos (UINT32 pos)UINT32 NumberOfWords (UINT32 pos)
π Variables π
static const UINT32 Filled = 0xFFFFFFFFβ literally all the ones. the full snack. π
π Macro Definition Documentation π
π #define MAKEU64( a, b)
Value:
((UINT64) (((UINT32) (a)) | ((UINT64) ((UINT32) (b))) << 32))
Make 64 bit unsigned integer from two 32 bit unsigned integers.
Definition at line 41 of file BitStream.h.
π§ Function Documentation π§
β UINT32 AlignWordPos (UINT32 pos) [inline]
Compute bit position of the next 32-bit word. it lines you up on the next word boundary, main character behavior only.
Parameters
posβ current bit stream position
Returns
- bit position of next 32-bit word
Definition at line 328 of file BitStream.h.
328 {
329 // return ((pos + WordWidth - 1) >> WordWidthLog) << WordWidthLog;
330 return DWWIDTHBITS(pos);
331 }
π void ClearBit (UINT32 * stream, UINT32 pos) [inline]
Set one bit of a bit stream to 0. just one bit, gently, to zero.
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit stream
Definition at line 70 of file BitStream.h.
70 {
71 stream[pos >> WordWidthLog] &= ~(1 << (pos%WordWidth));
72 }
π void ClearBitBlock (UINT32 * stream, UINT32 pos, UINT32 len) [inline]
Clear block of size at least len at position pos in stream. a whole lil region, zeroed out.
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit streamlenβ Number of bits set to 0
Definition at line 169 of file BitStream.h.
169 {
170 ASSERT(len > 0);
171 const UINT32 iFirstInt = pos >> WordWidthLog;
172 const UINT32 iLastInt = (pos + len - 1) >> WordWidthLog;
173
174 const UINT32 startMask = Filled << (pos%WordWidth);
175 // const UINT32 endMask=Filled>>(WordWidth-1-((pos+len-1)%WordWidth));
176
177 if (iFirstInt == iLastInt) {
178 stream[iFirstInt] &= ~(startMask /*& endMask*/);
179 } else {
180 stream[iFirstInt] &= ~startMask;
181 for (UINT32 i = iFirstInt + 1; i <= iLastInt; i++) { // changed <=
182 stream[i] = 0;
183 }
184 //stream[iLastInt] &= ~endMask;
185 }
186 }
π¦ bool CompareBitBlock (UINT32 * stream, UINT32 pos, UINT32 k, UINT32 val) [inline]
Compare the k-bit binary representation of stream at position pos with val. it's a vibe check for bits, fr fr.
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit streamkβ Number of bits to comparevalβ Value to compare with
Returns
trueif equal
Definition at line 91 of file BitStream.h.
91 {
92 const UINT32 iLoInt = pos >> WordWidthLog;
93 const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog;
94 ASSERT(iLoInt <= iHiInt);
95 const UINT32 mask = (Filled >> (WordWidth - k));
96
97 if (iLoInt == iHiInt) {
98 // fits into one integer
99 val &= mask;
100 val <<= (pos%WordWidth);
101 return (stream[iLoInt] & val) == val;
102 } else {
103 // must be splitted over integer boundary
104 UINT64 v1 = MAKEU64(stream[iLoInt], stream[iHiInt]);
105 UINT64 v2 = UINT64(val & mask) << (pos%WordWidth);
106 return (v1 & v2) == v2;
107 }
108 }
π bool GetBit (UINT32 * stream, UINT32 pos) [inline]
Return one bit of a bit stream. one (1) single bit, served fresh.
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit stream
Returns
- bit at position
posof bit streamstream
Definition at line 79 of file BitStream.h.
79 {
80 return (stream[pos >> WordWidthLog] & (1 << (pos%WordWidth))) > 0;
81
82 }
π UINT32 GetValueBlock (UINT32 * stream, UINT32 pos, UINT32 k) [inline]
Read a k-bit number from stream at position pos. lil number, big energy.
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit streamkβ Number of bits to read:1 <= k <= 32
Definition at line 142 of file BitStream.h.
142 {
143 UINT32 count, hiCount;
144 const UINT32 iLoInt = pos >> WordWidthLog; // integer of first bit
145 const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog; // integer of last bit
146 const UINT32 loMask = Filled << (pos%WordWidth);
147 const UINT32 hiMask = Filled >> (WordWidth - 1 - ((pos + k - 1)%WordWidth));
148
149 if (iLoInt == iHiInt) {
150 // inside integer boundary
151 count = stream[iLoInt] & (loMask & hiMask);
152 count >>= pos%WordWidth;
153 } else {
154 // overlapping integer boundary
155 count = stream[iLoInt] & loMask;
156 count >>= pos%WordWidth;
157 hiCount = stream[iHiInt] & hiMask;
158 hiCount <<= WordWidth - (pos%WordWidth);
159 count |= hiCount;
160 }
161 return count;
162 }
πΈ UINT32 NumberOfWords (UINT32 pos) [inline]
Compute number of the 32-bit words. counting words, bestie, someone's gotta do it.
Parameters
posβ Current bit stream position
Returns
- Number of 32-bit words
Definition at line 337 of file BitStream.h.
337 {
338 return (pos + WordWidth - 1) >> WordWidthLog;
339 }
π
UINT32 SeekBit1Range (UINT32 * stream, UINT32 pos, UINT32 len) [inline]
Returns the distance to the next 0 in stream at position pos. If no 0 is found within len bits, then len is returned. (yes, it's called SeekBit1Range and it hunts for zeros β we don't make the rules, we just report them π)
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit streamlenβ size of search area (in bits) return The distance to the next 0 in stream at positionpos
Definition at line 249 of file BitStream.h.
249 {
250 UINT32 count = 0;
251 UINT32 testMask = 1 << (pos%WordWidth);
252 UINT32* word = stream + (pos >> WordWidthLog);
253
254 while (((*word & testMask) != 0) && (count < len)) {
255 count++;
256 testMask <<= 1;
257 if (!testMask) {
258 word++; testMask = 1;
259
260 // fast steps if all bits in a word are one
261 while ((count + WordWidth <= len) && (*word == Filled)) {
262 word++;
263 count += WordWidth;
264 }
265 }
266 }
267 return count;
268 }
π₯ UINT32 SeekBitRange (UINT32 * stream, UINT32 pos, UINT32 len) [inline]
Returns the distance to the next 1 in stream at position pos. If no 1 is found within len bits, then len is returned.
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit streamlenβ size of search area (in bits) return The distance to the next 1 in stream at positionpos
Definition at line 220 of file BitStream.h.
220 {
221 UINT32 count = 0;
222 UINT32 testMask = 1 << (pos%WordWidth);
223 UINT32* word = stream + (pos >> WordWidthLog);
224
225 while (((*word & testMask) == 0) && (count < len)) {
226 count++;
227 testMask <<= 1;
228 if (!testMask) {
229 word++; testMask = 1;
230
231 // fast steps if all bits in a word are zero
232 while ((count + WordWidth <= len) && (*word == 0)) {
233 word++;
234 count += WordWidth;
235 }
236 }
237 }
238
239 return count;
240 }
π«Ά void SetBit (UINT32 * stream, UINT32 pos) [inline]
Set one bit of a bit stream to 1. glow up, single bit edition.
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit stream
Definition at line 62 of file BitStream.h.
62 {
63 stream[pos >> WordWidthLog] |= (1 << (pos%WordWidth));
64 }
π§ void SetBitBlock (UINT32 * stream, UINT32 pos, UINT32 len) [inline]
Set block of size at least len at position pos in stream. whole squad of bits, all turned on.
Parameters
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit streamlenβ Number of bits set to 1
β¦and bestie, hold ON, this page keeps going β there's literally more of this coming. don't go anywhere π
...and then the body just drops right in at line 193, no cap:
193 {
194 ASSERT(len > 0);
195
196 const UINT32 iFirstInt = pos >> WordWidthLog;
197 const UINT32 iLastInt = (pos + len - 1) >> WordWidthLog;
198
199 const UINT32 startMask = Filled << (pos%WordWidth);
200 // const UINT32 endMask=Filled>>(WordWidth-1-((pos+len-1)%WordWidth));
201
202 if (iFirstInt == iLastInt) {
203 stream[iFirstInt] |= (startMask /*& endMask*/);
204 } else {
205 stream[iFirstInt] |= startMask;
206 for (UINT32 i = iFirstInt + 1; i <= iLastInt; i++) { // changed <=
207 stream[i] = Filled;
208 }
209 //stream[iLastInt] &= ~endMask;
210 }
211 }
okay so the vibe here: it checks that len is actually bigger than zero (the ASSERT(len > 0) is giving "bestie please"), works out which word the start lands in and which word the end lands in, then branches. Same word? Just OR in the start mask. Different words? OR the start mask into the first one, then straight-up overwrite every word in between with Filled, all the way through iLastInt β note the // changed <= comment, someone was fiddling with that boundary and left receipts π. And see those commented-out endMask lines? Still sitting there, dormant, unbothered, iconic. They are not compiled. Do not come for them.
SetValueBlock β¨
void SetValueBlock (UINT32 * stream, UINT32 pos, UINT32 val, UINT32 k)
[inline]
Store k-bit binary representation of val in stream at position pos β like, squeeze exactly k bits of val into the stream starting at pos. She's precise. She's surgical. She's inline.
Parameters π«Ά
streamβ A bit stream stored in array of unsigned integersposβ A valid zero-based position in the bit streamvalβ Value to store in stream at positionposkβ Number of bits of integer representation ofval
Definition at line 116 of file BitStream.h.
116 {
117 const UINT32 offset = pos%WordWidth;
118 const UINT32 iLoInt = pos >> WordWidthLog;
119 const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog;
120 ASSERT(iLoInt <= iHiInt);
121 const UINT32 loMask = Filled << offset;
122 const UINT32 hiMask = Filled >> (WordWidth - 1 - ((pos + k - 1)%WordWidth));
123
124 if (iLoInt == iHiInt) {
125 // fits into one integer
126 stream[iLoInt] &= ~(loMask & hiMask); // clear bits
127 stream[iLoInt] |= val << offset; // write value
128 } else {
129 // must be splitted over integer boundary
130 stream[iLoInt] &= ~loMask; // clear bits
131 stream[iLoInt] |= val << offset; // write lower part of value
132 stream[iHiInt] &= ~hiMask; // clear bits
133 stream[iHiInt] |= val >> (WordWidth - offset); // write higher part of value
134 }
135 }
okay so let's break this down because she's actually so smart π
. First it grabs offset (where inside the word we start), then works out the low word index iLoInt and the high word index iHiInt, and asserts iLoInt <= iHiInt because obviously. Then two masks: loMask = Filled << offset and hiMask = Filled >> (WordWidth - 1 - ((pos + k - 1)%WordWidth)).
Then the tea: one integer? clear the bits with ~(loMask & hiMask) and write val << offset, done, no drama. Split over an integer boundary? (the comment literally says // must be splitted over integer boundary, and yes she said "splitted", leave her alone π) β clear ~loMask in the low word and write val << offset as the lower part, then clear ~hiMask in the high word and write val >> (WordWidth - offset) as the higher part. Both halves, both cleared first, both written. Clean. Obsessed.
π VARIABLE DOCUMENTATION π
const UINT32 Filled = 0xFFFFFFFF[static]β that's the all-ones word, the "everything is on" girly. Definition at line 38 of fileBitStream.h.
she's static, she's constant, she's 0xFFFFFFFF and she is NOT changing for anybody. π
π AUTHOR π
Generated automatically by Doxygen for libpgf from the source code.
ngl nobody "wrote" this one, Doxygen just spawned it fully formed like a little documentation fairy π§ and we love her for it.
and that's the whole page bestie!! Version 7.21.2, dated Thu Jul 24 2025 00:00:00, from BitStream.h(3) β go forth and bit-twiddle responsibly, mwah π
β¨π¦πΈ
π¦ where this page comes from
- fedora 43: libpgf-devel 7.21.7-8.fc43 Β· LicenseRef-Callaway-LGPLv2+
- fedora 43: libpgf-devel 7.21.7-8.fc43 Β· LicenseRef-Callaway-LGPLv2+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms