BitStream.h File Reference

#include "PGFtypes.h"

Go to the source code of this file.

Defines

#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

Define 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:
pos current bit stream position
Returns:
bit position of next 32-bit word

Definition at line 260 of file BitStream.h.

00260                                        {
00261 //      return ((pos + WordWidth - 1) >> WordWidthLog) << WordWidthLog;
00262         return DWWIDTHBITS(pos);
00263 }

void ClearBit ( UINT32 *  stream,
UINT32  pos 
) [inline]

Set one bit of a bit stream to 0

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

Definition at line 56 of file BitStream.h.

00056                                                  {
00057         stream[pos >> WordWidthLog] &= ~(1 << (pos%WordWidth)); 
00058 }

void ClearBitBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  len 
) [inline]

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

Parameters:
stream A bit stream stored in array of unsigned integers
pos A valid zero-based position in the bit stream
len Number of bits set to 0

Definition at line 155 of file BitStream.h.

00155                                                                   {
00156         ASSERT(len > 0);
00157         const UINT32 iFirstInt = pos >> WordWidthLog;
00158         const UINT32 iLastInt = (pos + len - 1) >> WordWidthLog;
00159 
00160         const UINT32 startMask = Filled << (pos%WordWidth);
00161 //      const UINT32 endMask=Filled>>(WordWidth-1-((pos+len-1)%WordWidth));
00162 
00163         if (iFirstInt == iLastInt) {
00164                 stream[iFirstInt] &= ~(startMask /*& endMask*/);
00165         } else {
00166                 stream[iFirstInt] &= ~startMask;
00167                 for (UINT32 i = iFirstInt + 1; i <= iLastInt; i++) { // changed <=
00168                         stream[i] = 0;
00169                 }
00170                 //stream[iLastInt] &= ~endMask;
00171         }
00172 }

bool CompareBitBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  k,
UINT32  val 
) [inline]

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

Parameters:
stream A bit stream stored in array of unsigned integers
pos A valid zero-based position in the bit stream
k Number of bits to compare
val Value to compare with
Returns:
true if equal

Definition at line 77 of file BitStream.h.

00077                                                                               {
00078         const UINT32 iLoInt = pos >> WordWidthLog;
00079         const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog;
00080         ASSERT(iLoInt <= iHiInt);
00081         const UINT32 mask = (Filled >> (WordWidth - k));
00082 
00083         if (iLoInt == iHiInt) {
00084                 // fits into one integer
00085                 val &= mask;
00086                 val <<= (pos%WordWidth);
00087                 return (stream[iLoInt] & val) == val;
00088         } else {
00089                 // must be splitted over integer boundary
00090                 UINT64 v1 = MAKEU64(stream[iLoInt], stream[iHiInt]);
00091                 UINT64 v2 = UINT64(val & mask) << (pos%WordWidth);
00092                 return (v1 & v2) == v2;
00093         }
00094 }

bool GetBit ( UINT32 *  stream,
UINT32  pos 
) [inline]

Return one bit of a bit stream

Parameters:
stream A bit stream stored in array of unsigned integers
pos A 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.

00065                                                 {
00066         return (stream[pos >> WordWidthLog] & (1 << (pos%WordWidth))) > 0;
00067 
00068 }

UINT32 GetValueBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  k 
) [inline]

Read k-bit number from stream at position pos

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

Definition at line 128 of file BitStream.h.

00128                                                                   {
00129         UINT32 count, hiCount;
00130         const UINT32 iLoInt = pos >> WordWidthLog;                              // integer of first bit
00131         const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog;            // integer of last bit
00132         const UINT32 loMask = Filled << (pos%WordWidth);
00133         const UINT32 hiMask = Filled >> (WordWidth - 1 - ((pos + k - 1)%WordWidth));
00134         
00135         if (iLoInt == iHiInt) {
00136                 // inside integer boundary
00137                 count = stream[iLoInt] & (loMask & hiMask);
00138                 count >>= pos%WordWidth;
00139         } else {
00140                 // overlapping integer boundary
00141                 count = stream[iLoInt] & loMask;
00142                 count >>= pos%WordWidth;
00143                 hiCount = stream[iHiInt] & hiMask;
00144                 hiCount <<= WordWidth - (pos%WordWidth);
00145                 count |= hiCount;
00146         }
00147         return count;
00148 }

UINT32 NumberOfWords ( UINT32  pos  )  [inline]

Compute number of the 32-bit words

Parameters:
pos Current bit stream position
Returns:
Number of 32-bit words

Definition at line 269 of file BitStream.h.

00269                                         {
00270         return (pos + WordWidth - 1) >> WordWidthLog;
00271 }

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:
stream A bit stream stored in array of unsigned integers
pos A valid zero-based position in the bit stream
len size 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.

00235                                                                     {
00236         UINT32 count = 0;
00237         UINT32 testMask = 1 << (pos%WordWidth);
00238         UINT32* word = stream + (pos >> WordWidthLog);
00239 
00240         while (((*word & testMask) != 0) && (count < len)) {
00241                 count++; 
00242                 testMask <<= 1;
00243                 if (!testMask) {
00244                         word++; testMask = 1;
00245 
00246                         // fast steps if all bits in a word are one
00247                         while ((count + WordWidth <= len) && (*word == Filled)) {
00248                                 word++; 
00249                                 count += WordWidth;
00250                         }
00251                 }
00252         }
00253         return count;
00254 }

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 integers
pos A valid zero-based position in the bit stream
len size 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.

00206                                                                    {
00207         UINT32 count = 0;
00208         UINT32 testMask = 1 << (pos%WordWidth);
00209         UINT32* word = stream + (pos >> WordWidthLog);
00210 
00211         while (((*word & testMask) == 0) && (count < len)) {
00212                 count++; 
00213                 testMask <<= 1;
00214                 if (!testMask) {
00215                         word++; testMask = 1;
00216 
00217                         // fast steps if all bits in a word are zero
00218                         while ((count + WordWidth <= len) && (*word == 0)) {
00219                                 word++; 
00220                                 count += WordWidth;
00221                         }
00222                 }
00223         }
00224 
00225         return count;
00226 }

void SetBit ( UINT32 *  stream,
UINT32  pos 
) [inline]

Set one bit of a bit stream to 1

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

Definition at line 48 of file BitStream.h.

00048                                                {
00049         stream[pos >> WordWidthLog] |= (1 << (pos%WordWidth));
00050 }

void SetBitBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  len 
) [inline]

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

Parameters:
stream A bit stream stored in array of unsigned integers
pos A valid zero-based position in the bit stream
len Number of bits set to 1

Definition at line 179 of file BitStream.h.

00179                                                                 {
00180         ASSERT(len > 0);
00181 
00182         const UINT32 iFirstInt = pos >> WordWidthLog;
00183         const UINT32 iLastInt = (pos + len - 1) >> WordWidthLog;
00184 
00185         const UINT32 startMask = Filled << (pos%WordWidth);
00186 //      const UINT32 endMask=Filled>>(WordWidth-1-((pos+len-1)%WordWidth));
00187 
00188         if (iFirstInt == iLastInt) {
00189                 stream[iFirstInt] |= (startMask /*& endMask*/);
00190         } else {
00191                 stream[iFirstInt] |= startMask;
00192                 for (UINT32 i = iFirstInt + 1; i <= iLastInt; i++) { // changed <=
00193                         stream[i] = Filled;
00194                 }
00195                 //stream[iLastInt] &= ~endMask;
00196         }
00197 }

void SetValueBlock ( UINT32 *  stream,
UINT32  pos,
UINT32  val,
UINT32  k 
) [inline]

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

Parameters:
stream A bit stream stored in array of unsigned integers
pos A valid zero-based position in the bit stream
val Value to store in stream at position pos
k Number of bits of integer representation of val

Definition at line 102 of file BitStream.h.

00102                                                                             {
00103         const UINT32 offset = pos%WordWidth;
00104         const UINT32 iLoInt = pos >> WordWidthLog;
00105         const UINT32 iHiInt = (pos + k - 1) >> WordWidthLog;
00106         ASSERT(iLoInt <= iHiInt);
00107         const UINT32 loMask = Filled << offset;
00108         const UINT32 hiMask = Filled >> (WordWidth - 1 - ((pos + k - 1)%WordWidth));
00109 
00110         if (iLoInt == iHiInt) {
00111                 // fits into one integer
00112                 stream[iLoInt] &= ~(loMask & hiMask); // clear bits
00113                 stream[iLoInt] |= val << offset; // write value
00114         } else {
00115                 // must be splitted over integer boundary
00116                 stream[iLoInt] &= ~loMask; // clear bits
00117                 stream[iLoInt] |= val << offset; // write lower part of value
00118                 stream[iHiInt] &= ~hiMask; // clear bits
00119                 stream[iHiInt] |= val >> (WordWidth - offset); // write higher part of value
00120         }
00121 }


Variable Documentation

const UINT32 Filled = 0xFFFFFFFF [static]

Definition at line 37 of file BitStream.h.


Generated on 18 Feb 2019 for libpgf by  doxygen 1.6.1