8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
19 module ALib.BitBuffer;
55 && terminationIndex.
bit == 0
56 &&
data[terminationIndex.
pos - 1] != 0,
57 "BITBUFFER",
"Given index is no termination index" )
60 terminationIndex.
pos--;
65 terminationIndex.
bit--;
69 SetWord(terminationIndex, word );
70 return terminationIndex;
78 static int isLittleEndianEncoding= 0;
80bool IsLittleEndianEncoding() {
81 if( isLittleEndianEncoding == 0 ) {
86 "Platform not supported")
88 isLittleEndianEncoding= 2;
91 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &testWord );
92 if( *bytes == 0x11 && *(bytes+1) == 0x22)
93 isLittleEndianEncoding= 1;
98 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &testWord );
99 if( * bytes == 0x11 && *(bytes+1) == 0x22
100 && *(bytes+2) == 0x33 && *(bytes+3) == 0x44 )
101 isLittleEndianEncoding= 1;
106 testWord= 0x8877665544332211u;
107 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &testWord );
108 if( * bytes == 0x11 && *(bytes+1) == 0x22
109 && *(bytes+2) == 0x33 && *(bytes+3) == 0x44
110 && *(bytes+4) == 0x55 && *(bytes+5) == 0x66
111 && *(bytes+6) == 0x77 && *(bytes+7) == 0x88 )
112 isLittleEndianEncoding= 1;
117 return (isLittleEndianEncoding == 1);
124 "Given start index is not algined. The easiest way to get an aligned "
125 "index is to terminate the buffer." )
127 if( IsLittleEndianEncoding())
130 const size_t end= endIndex.
pos + (endIndex.
bit != 0 );
131 for (
size_t pos= startIndex.
pos; pos < end; ++pos) {
133 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &
data[pos] );
134 bytes[0]= word & 0xFF;
135 word>>= 8; bytes[1]= word & 0xFF;
138 word>>= 8; bytes[2]= word & 0xFF;
139 word>>= 8; bytes[3]= word & 0xFF;
143 word>>= 8; bytes[4]= word & 0xFF;
144 word>>= 8; bytes[5]= word & 0xFF;
145 word>>= 8; bytes[6]= word & 0xFF;
146 word>>= 8; bytes[7]= word & 0xFF;
150 if( IsLittleEndianEncoding())
153 size_t end= endIndex.
pos + (endIndex.
bit != 0 );
154 for (
size_t pos= startIndex.
pos; pos < end; ++pos) {
156 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &
data[pos] );
201 if( val < (1<< 8) ) {
Write<10>( val << 2 ); }
202 else if( val < (1<<16) ) {
Write<18>(( val << 2) | 1u ); }
203 else if( val < (1<<24) ) {
Write<26>(( val << 2) | 2u ); }
210 if( val < uint64_t(1) << 8 ) {
Write<11>( int16_t (val) << 3 ); }
211 else if( val < uint64_t(1) << 16 ) {
Write<19>( ( uint32_t (val) << 3 ) | 1u); }
212 else if( val < uint64_t(1) << 24 ) {
Write<27>( ( uint32_t (val) << 3 ) | 2u); }
213 else if( val < uint64_t(1) << 32 ) {
Write<35>( ( val << 3 ) | 3u); }
214 else if( val < uint64_t(1) << 40 ) {
Write<43>( ( val << 3 ) | 4u); }
215 else if( val < uint64_t(1) << 48 ) {
Write<51>( ( val << 3 ) | 5u); }
216 else if( val < uint64_t(1) << 56 ) {
Write<59>( ( val << 3 ) | 6u); }
226 return uint8_t(result >> 1);
228 return uint8_t( result >> 1
235 return uint16_t(result >> 1);
237 return uint16_t( result >> 1
243 switch (result & 3) {
244 case 0:
return uint32_t(result >> 2);
252 switch (result & 7) {
253 case 0:
return uint64_t(result >> 3);
lang::ShiftOpRHS bit
Current bit index in the current word.
uinteger pos
Index of the current word to read/write.
ALIB_DLL Index Unterminate(Index terminationIndex)
void SetWord(const Index &index, TStorage value)
ALIB_DLL Index Terminate(Index writerIndex)
ALIB_DLL void ToLittleEndianEncoding(const Index &startIndex, const Index &endIndex)
ALIB_DLL void FromLittleEndianEncoding(const Index &startIndex, const Index &endIndex)
TStorage GetWord(const Index &index) const
BitBufferBase::Index GetIndex() const
ALIB_DLL uint64_t readUIntegral64()
ALIB_DLL uint32_t readUIntegral32()
ALIB_DLL uint16_t readUIntegral16()
ALIB_DLL uint8_t readUIntegral8()
Writes bits into a BitBufferBase.
void Write(TIntegral value)
ALIB_DLL void writeUIntegral(uint8_t value)
#define ALIB_WARNINGS_IGNORE_INTEGRAL_CONSTANT_OVERFLOW
#define ALIB_WARNINGS_RESTORE
#define ALIB_STATIC_ASSERT(CondVariable, Cond, Message)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_WARNINGS_IGNORE_INTEGER_OVERFLOW
constexpr int MSB(TIntegral value)