9#if !defined(HPP_AWORX_ALIB_BITBUFFER)
48 "Given index is no termination index" )
52 terminationIndex.
pos--;
57 terminationIndex.
bit--;
61 SetWord(terminationIndex, word );
62 return terminationIndex;
70 static int isLittleEndianEncoding= 0;
72 bool IsLittleEndianEncoding()
74 if( isLittleEndianEncoding == 0 )
79 "Platform not supported");
81 isLittleEndianEncoding= 2;
86 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &testWord );
88 if( *bytes == 0x11 && *(bytes+1) == 0x22)
89 isLittleEndianEncoding= 1;
96 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &testWord );
98 if( * bytes == 0x11 && *(bytes+1) == 0x22
99 && *(bytes+2) == 0x33 && *(bytes+3) == 0x44 )
100 isLittleEndianEncoding= 1;
106 testWord= 0x8877665544332211u;
108 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &testWord );
110 if( * bytes == 0x11 && *(bytes+1) == 0x22
111 && *(bytes+2) == 0x33 && *(bytes+3) == 0x44
112 && *(bytes+4) == 0x55 && *(bytes+5) == 0x66
113 && *(bytes+6) == 0x77 && *(bytes+7) == 0x88 )
114 isLittleEndianEncoding= 1;
120 return (isLittleEndianEncoding == 1);
128 "Given start index is not algined. The easiest way to get an aligned "
129 "index is to terminate the buffer." )
131 if( IsLittleEndianEncoding())
134 const size_t end= endIndex.
pos + (endIndex.
bit != 0 );
135 for (
size_t pos= startIndex.
pos; pos < end; ++pos)
139 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &
data[pos] );
141 bytes[0]= word & 0xFF;
142 word>>= 8; bytes[1]= word & 0xFF;
146 word>>= 8; bytes[2]= word & 0xFF;
147 word>>= 8; bytes[3]= word & 0xFF;
152 word>>= 8; bytes[4]= word & 0xFF;
153 word>>= 8; bytes[5]= word & 0xFF;
154 word>>= 8; bytes[6]= word & 0xFF;
155 word>>= 8; bytes[7]= word & 0xFF;
162 if( IsLittleEndianEncoding())
165 size_t end= endIndex.
pos + (endIndex.
bit != 0 );
166 for (
size_t pos= startIndex.
pos; pos < end; ++pos)
170 uint8_t* bytes=
reinterpret_cast<uint8_t*
>( &
data[pos] );
206 Write<9>( (val << 1) |1 ) ;
218 Write<17>( ( val << 1 ) | 1 ) ;
224 if( val < (1<< 8) ) { Write<10>( val << 2 ); }
225 else if( val < (1<<16) ) { Write<18>(( val << 2) | 1u ); }
226 else if( val < (1<<24) ) { Write<26>(( val << 2) | 2u ); }
227 else { Write< 2>( 3 );
234 if( val < uint64_t(1) << 8 ) { Write<11>( int16_t (val) << 3 ); }
235 else if( val < uint64_t(1) << 16 ) { Write<19>( ( uint32_t (val) << 3 ) | 1u); }
236 else if( val < uint64_t(1) << 24 ) { Write<27>( ( uint32_t (val) << 3 ) | 2u); }
237 else if( val < uint64_t(1) << 32 ) { Write<35>( ( val << 3 ) | 3u); }
238 else if( val < uint64_t(1) << 40 ) { Write<43>( ( val << 3 ) | 4u); }
239 else if( val < uint64_t(1) << 48 ) { Write<51>( ( val << 3 ) | 5u); }
240 else if( val < uint64_t(1) << 56 ) { Write<59>( ( val << 3 ) | 6u); }
241 else { Write< 3>( 7 );
249 auto result= Read<4>();
251 return uint8_t(result >> 1);
253 return uint8_t( result >> 1
259 auto result= Read< 9>();
261 return uint16_t(result >> 1);
263 return uint16_t( result >> 1
269 uint32_t result= Read< 10, uint32_t>();
272 case 0:
return uint32_t(result >> 2);
273 case 1:
return uint32_t(result >> 2 | (Read< 8, uint32_t>() << 8 ) );
274 case 2:
return uint32_t(result >> 2 | (Read< 16, uint32_t>() << 8 ) );
275 default:
return uint32_t(result >> 2 | (Read< 24, uint32_t>() << 8 ) );
281 uint64_t result= Read< 11, uint64_t>();
284 case 0:
return uint64_t(result >> 3);
285 case 1:
return uint64_t(result >> 3 | (Read< 8, uint64_t>() << 8 ));
286 case 2:
return uint64_t(result >> 3 | (Read< 16, uint64_t>() << 8 ));
287 case 3:
return uint64_t(result >> 3 | (Read< 24, uint64_t>() << 8 ));
288 case 4:
return uint64_t(result >> 3 | (Read< 32, uint64_t>() << 8 ));
289 case 5:
return uint64_t(result >> 3 | (Read< 40, uint64_t>() << 8 ));
290 case 6:
return uint64_t(result >> 3 | (Read< 48, uint64_t>() << 8 ));
291 default:
return uint64_t(result >> 3 | (Read< 56, uint64_t>() << 8 ));
lang::ShiftOpRHS bit
Current bit index in the current word.
uinteger pos
Index of the current word to read/write.
TStorage GetWord(const Index &index) const
ALIB_API Index Terminate(Index writerIndex)
void SetWord(const Index &index, TStorage value)
ALIB_API Index Unterminate(Index terminationIndex)
ALIB_API void ToLittleEndianEncoding(const Index &startIndex, const Index &endIndex)
ALIB_API void FromLittleEndianEncoding(const Index &startIndex, const Index &endIndex)
BitBufferBase::Index GetIndex() const
ALIB_API uint16_t readUIntegral16()
ALIB_API uint32_t readUIntegral32()
ALIB_API uint64_t readUIntegral64()
ALIB_API uint8_t readUIntegral8()
ALIB_API void writeUIntegral(uint8_t val)
void Write(TIntegral value)
#define ALIB_WARNINGS_IGNORE_INTEGRAL_CONSTANT_OVERFLOW
#define ALIB_WARNINGS_RESTORE
#define ALIB_ASSERT_ERROR(cond,...)
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
#define ALIB_WARNINGS_IGNORE_INTEGER_OVERFLOW
constexpr int MSB(TIntegral value)