无需查找表并具有公共许可证的C中CRC32算法/实现 [英] CRC32 algorithm/implementation in C without a look up table and with a public license
本文介绍了无需查找表并具有公共许可证的C中CRC32算法/实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在不使用查找表的C语言中实现CRC32算法(我需要在没有足够可用内存的引导加载程序中使用该算法)。
I am trying to implement a CRC32 algorithm in C that does not use a look up table (I need to use it in a boot loader that doesn't have enough memory available to have one). Is there an available solution to this that has a public license?
推荐答案
快速搜索得到的此网页。我找不到这些代码段的许可证。
A quick search harvested this webpage. I wasn't able to find the license for these code snippets.
以下应完成此工作:
// ----------------------------- crc32b --------------------------------
/* This is the basic CRC-32 calculation with some optimization but no
table lookup. The the byte reversal is avoided by shifting the crc reg
right instead of left and by using a reversed 32-bit word to represent
the polynomial.
When compiled to Cyclops with GCC, this function executes in 8 + 72n
instructions, where n is the number of bytes in the input message. It
should be doable in 4 + 61n instructions.
If the inner loop is strung out (approx. 5*8 = 40 instructions),
it would take about 6 + 46n instructions. */
unsigned int crc32b(unsigned char *message) {
int i, j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0) {
byte = message[i]; // Get next byte.
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
这篇关于无需查找表并具有公共许可证的C中CRC32算法/实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文