libpgf  6.11.42
PGF - Progressive Graphics File
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BitStream.h File Reference
#include "PGFtypes.h"

Go to the source code of this file.

Macros

#define MAKEU64(a, b)   ((UINT64) (((UINT32) (a)) | ((UINT64) ((UINT32) (b))) << 32))
 Make 64 bit unsigned integer from two 32 bit unsigned integers.

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

Macro Definition Documentation

#define MAKEU64 (   a,
 
)    ((UINT64) (((UINT32) (a)) | ((UINT64) ((UINT32) (b))) << 32))

Make 64 bit unsigned integer from two 32 bit unsigned integers.

Definition at line 40 of file BitStream.h.


Function Documentation

UINT32 AlignWordPos ( UINT32  pos)
inline

Compute bit position of the next 32-bit word

Parameters:
poscurrent bit stream position
Returns:
bit position of next 32-bit word

Definition at line 260 of file BitStream.h.

{
// return ((pos + WordWidth - 1) >> WordWidthLog) << WordWidthLog;
return DWWIDTHBITS(pos);
}
void ClearBit ( UINT32 *  stream,
UINT32  pos 
)
inline

Set one bit of a bit stream to 0

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream

Definition at line 56 of file BitStream.h.

{
stream[pos >> WordWidthLog] &= ~(1 << (pos%WordWidth));
}
void ClearBitBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  len 
)
inline

Clear block of size at least len at position pos in stream

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream
lenNumber of bits set to 0

Definition at line 155 of file BitStream.h.

{
ASSERT(len > 0);
const UINT32 iFirstInt = pos >> WordWidthLog;
const UINT32 iLastInt = (pos + len - 1) >> WordWidthLog;
const UINT32 startMask = Filled << (pos%WordWidth);
// const UINT32 endMask=Filled>>(WordWidth-1-((pos+len-1)%WordWidth));
if (iFirstInt == iLastInt) {
stream[iFirstInt] &= ~(startMask /*& endMask*/);
} else {
stream[iFirstInt] &= ~startMask;
for (UINT32 i = iFirstInt + 1; i <= iLastInt; i++) { // changed <=
stream[i] = 0;
}
//stream[iLastInt] &= ~endMask;
}
}
bool CompareBitBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  k,
UINT32  val 
)
inline

Compare k-bit binary representation of stream at position pos with val

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream
kNumber of bits to compare
valValue to compare with
Returns:
true if equal

Definition at line 77 of file BitStream.h.

{
const UINT32 iLoInt = pos >> WordWidthLog;
const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog;
ASSERT(iLoInt <= iHiInt);
const UINT32 mask = (Filled >> (WordWidth - k));
if (iLoInt == iHiInt) {
// fits into one integer
val &= mask;
val <<= (pos%WordWidth);
return (stream[iLoInt] & val) == val;
} else {
// must be splitted over integer boundary
UINT64 v1 = MAKEU64(stream[iLoInt], stream[iHiInt]);
UINT64 v2 = UINT64(val & mask) << (pos%WordWidth);
return (v1 & v2) == v2;
}
}
bool GetBit ( UINT32 *  stream,
UINT32  pos 
)
inline

Return one bit of a bit stream

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream
Returns:
bit at position pos of bit stream stream

Definition at line 65 of file BitStream.h.

{
return (stream[pos >> WordWidthLog] & (1 << (pos%WordWidth))) > 0;
}
UINT32 GetValueBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  k 
)
inline

Read k-bit number from stream at position pos

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream
kNumber of bits to read: 1 <= k <= 32

Definition at line 128 of file BitStream.h.

{
UINT32 count, hiCount;
const UINT32 iLoInt = pos >> WordWidthLog; // integer of first bit
const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog; // integer of last bit
const UINT32 loMask = Filled << (pos%WordWidth);
const UINT32 hiMask = Filled >> (WordWidth - 1 - ((pos + k - 1)%WordWidth));
if (iLoInt == iHiInt) {
// inside integer boundary
count = stream[iLoInt] & (loMask & hiMask);
count >>= pos%WordWidth;
} else {
// overlapping integer boundary
count = stream[iLoInt] & loMask;
count >>= pos%WordWidth;
hiCount = stream[iHiInt] & hiMask;
hiCount <<= WordWidth - (pos%WordWidth);
count |= hiCount;
}
return count;
}
UINT32 NumberOfWords ( UINT32  pos)
inline

Compute number of the 32-bit words

Parameters:
posCurrent bit stream position
Returns:
Number of 32-bit words

Definition at line 269 of file BitStream.h.

{
return (pos + WordWidth - 1) >> WordWidthLog;
}
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.

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream
lensize of search area (in bits) return The distance to the next 0 in stream at position pos

Definition at line 235 of file BitStream.h.

{
UINT32 count = 0;
UINT32 testMask = 1 << (pos%WordWidth);
UINT32* word = stream + (pos >> WordWidthLog);
while (((*word & testMask) != 0) && (count < len)) {
count++;
testMask <<= 1;
if (!testMask) {
word++; testMask = 1;
// fast steps if all bits in a word are one
while ((count + WordWidth <= len) && (*word == Filled)) {
word++;
count += WordWidth;
}
}
}
return count;
}
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:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream
lensize of search area (in bits) return The distance to the next 1 in stream at position pos

Definition at line 206 of file BitStream.h.

{
UINT32 count = 0;
UINT32 testMask = 1 << (pos%WordWidth);
UINT32* word = stream + (pos >> WordWidthLog);
while (((*word & testMask) == 0) && (count < len)) {
count++;
testMask <<= 1;
if (!testMask) {
word++; testMask = 1;
// fast steps if all bits in a word are zero
while ((count + WordWidth <= len) && (*word == 0)) {
word++;
count += WordWidth;
}
}
}
return count;
}
void SetBit ( UINT32 *  stream,
UINT32  pos 
)
inline

Set one bit of a bit stream to 1

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream

Definition at line 48 of file BitStream.h.

{
stream[pos >> WordWidthLog] |= (1 << (pos%WordWidth));
}
void SetBitBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  len 
)
inline

Set block of size at least len at position pos in stream

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream
lenNumber of bits set to 1

Definition at line 179 of file BitStream.h.

{
ASSERT(len > 0);
const UINT32 iFirstInt = pos >> WordWidthLog;
const UINT32 iLastInt = (pos + len - 1) >> WordWidthLog;
const UINT32 startMask = Filled << (pos%WordWidth);
// const UINT32 endMask=Filled>>(WordWidth-1-((pos+len-1)%WordWidth));
if (iFirstInt == iLastInt) {
stream[iFirstInt] |= (startMask /*& endMask*/);
} else {
stream[iFirstInt] |= startMask;
for (UINT32 i = iFirstInt + 1; i <= iLastInt; i++) { // changed <=
stream[i] = Filled;
}
//stream[iLastInt] &= ~endMask;
}
}
void SetValueBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  val,
UINT32  k 
)
inline

Store k-bit binary representation of val in stream at position pos

Parameters:
streamA bit stream stored in array of unsigned integers
posA valid zero-based position in the bit stream
valValue to store in stream at position pos
kNumber of bits of integer representation of val

Definition at line 102 of file BitStream.h.

{
const UINT32 offset = pos%WordWidth;
const UINT32 iLoInt = pos >> WordWidthLog;
const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog;
ASSERT(iLoInt <= iHiInt);
const UINT32 loMask = Filled << offset;
const UINT32 hiMask = Filled >> (WordWidth - 1 - ((pos + k - 1)%WordWidth));
if (iLoInt == iHiInt) {
// fits into one integer
stream[iLoInt] &= ~(loMask & hiMask); // clear bits
stream[iLoInt] |= val << offset; // write value
} else {
// must be splitted over integer boundary
stream[iLoInt] &= ~loMask; // clear bits
stream[iLoInt] |= val << offset; // write lower part of value
stream[iHiInt] &= ~hiMask; // clear bits
stream[iHiInt] |= val >> (WordWidth - offset); // write higher part of value
}
}

Variable Documentation

const UINT32 Filled = 0xFFFFFFFF
static

Definition at line 37 of file BitStream.h.