在 x86/x86-64 上编写比特流的最快方法是什么? (码字 <= 32 位)
通过编写比特流,我指的是将可变比特长度符号连接到连续内存缓冲区中的过程。
目前我有一个带有 32 位中间缓冲区的标准容器可以写入
void write_bits(SomeContainer<unsigned int>& dst,unsigned int& buffer, unsigned int& bits_left_in_buffer,int codeword, short bits_to_write){
if(bits_to_write < bits_left_in_buffer){
buffer|= codeword << (32-bits_left_in_buffer);
bits_left_in_buffer -= bits_to_write;
}else{
unsigned int full_bits = bits_to_write - bits_left_in_buffer;
unsigned int towrite = buffer|(codeword<<(32-bits_left_in_buffer));
buffer= full_bits ? (codeword >> bits_left_in_buffer) : 0;
dst.push_back(towrite);
bits_left_in_buffer = 32-full_bits;
}
}
有人知道任何好的优化、快速指令或其他可能有用的信息吗?
干杯,
最佳答案
我曾经写过一个非常快速的实现,但它有几个限制:当您写入和读取比特流时,它可以在 32 位 x86 上运行。我在这里不检查缓冲区限制,我分配了更大的缓冲区并不时从调用代码中检查它。
unsigned char* membuff;
unsigned bit_pos; // current BIT position in the buffer, so it's max size is 512Mb
// input bit buffer: we'll decode the byte address so that it's even, and the DWORD from that address will surely have at least 17 free bits
inline unsigned int get_bits(unsigned int bit_cnt){ // bit_cnt MUST be in range 0..17
unsigned int byte_offset = bit_pos >> 3;
byte_offset &= ~1; // rounding down by 2.
unsigned int bits = *(unsigned int*)(membuff + byte_offset);
bits >>= bit_pos & 0xF;
bit_pos += bit_cnt;
return bits & BIT_MASKS[bit_cnt];
};
// output buffer, the whole destination should be memset'ed to 0
inline unsigned int put_bits(unsigned int val, unsigned int bit_cnt){
unsigned int byte_offset = bit_pos >> 3;
byte_offset &= ~1;
*(unsigned int*)(membuff + byte_offset) |= val << (bit_pos & 0xf);
bit_pos += bit_cnt;
};
关于c++ - 在现代 x86 硬件上编写比特流的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5704597/