Beruflich Dokumente
Kultur Dokumente
Probably only the first three, and maybe the fourth (CRC32d) are of any
interest. The others are just experiments that did not result in
anything useful. More comments are at the bottom of this file. */
#include <stdio.h>
#include <stdlib.h>
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0) {
byte = message[i]; // Get next byte.
byte = reverse(byte); // 32-bit reversal.
for (j = 0; j <= 7; j++) { // Do eight times.
if ((int)(crc ^ byte) < 0)
crc = (crc << 1) ^ 0x04C11DB7;
else crc = crc << 1;
byte = byte << 1; // Ready next msg bit.
}
i = i + 1;
}
return reverse(~crc);
}
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;
}
/* This is derived from crc32b but does table lookup. First the table
itself is calculated, if it has not yet been set up.
Not counting the table setup (which would probably be a separate
function), when compiled to Cyclops with GCC, this function executes in
7 + 13n instructions, where n is the number of bytes in the input
message. It should be doable in 4 + 9n instructions. In any case, two
of the 13 or 9 instrucions are load byte.
This is Figure 14-7 in the text. */
if (table[1] == 0) {
for (byte = 0; byte <= 255; byte++) {
crc = byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
table[byte] = crc;
}
}
i = 0;
crc = 0xFFFFFFFF;
while ((byte = message[i]) != 0) {
crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF];
i = i + 1;
}
return ~crc;
}
if (table[1] == 0) {
for (byte = 0; byte <= 255; byte++) {
crc = byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
table[byte] = crc;
}
}
crc = 0xFFFFFFFF;
while (((word = *(unsigned int *)message) & 0xFF) != 0) {
crc = crc ^ word;
crc = (crc >> 8) ^ table[crc & 0xFF];
crc = (crc >> 8) ^ table[crc & 0xFF];
crc = (crc >> 8) ^ table[crc & 0xFF];
crc = (crc >> 8) ^ table[crc & 0xFF];
message = message + 4;
}
return ~crc;
}
/* This is like crc32c (does table lookup) but it processes two bytes at
a time, except for a possible odd byte at the beginning or end of the
message. The table size is 65536 words.
Not counting the table setup (which would probably be a separate
function), when compiled to Cyclops with GCC, this function executes in
14 + 14n instructions, where n is the number of halfwords in the input
message. This assumes there are no odd bytes at either end.
Note: When accessing the table for a single byte b, the entry to use
is b << 8. I.e., if the byte is the letter 'a', the entry to use is that
with index 0x6100, not 0x0061. */
crc = 0xFFFFFFFF;
while (1) {
half = *(unsigned short *)(&message[i]);
if (half <= 0xFF || (half & 0xFF) == 0) break;
crc = (crc >> 16) ^ table[(crc ^ half) & 0xFFFF];
i = i + 2;
}
return ~crc;
}
/* This is sort of like the table lookup version (crc32c), but using
a 16-way switch statement instead.
When compiled to Cyclops with GCC, this function executes in 6 + 38n
instructions, where n is the number of bytes in the input message. The
38 instructions per byte include 3 loads and 5 branches (not good). It
is actually 6 branches if you count the unnecessary one that GCC
generates because it isn't smart enough to know that the switch argument
cannot exceed 15. */
i = 0;
crc = 0xFFFFFFFF;
while ((byte = message[i]) != 0) { // Get next byte.
crc = crc ^ byte;
for (j = 1; j >= 0; j--) { // Do two times.
switch(crc & 0xF) {
case 0: c = 0; break;
case 1: c = g3; break;
case 2: c = g2; break;
case 3: c = g2 ^ g3; break;
case 4: c = g1; break;
case 5: c = g1 ^ g3; break;
case 6: c = g1 ^ g2; break;
case 7: c = g1 ^ g2 ^ g3; break;
case 8: c = g0; break;
case 9: c = g0 ^ g3; break;
case 10: c = g0 ^ g2; break;
case 11: c = g0 ^ g2 ^ g3; break;
case 12: c = g0 ^ g1; break;
case 13: c = g0 ^ g1 ^ g3; break;
case 14: c = g0 ^ g1 ^ g2; break;
case 15: c = g0 ^ g1 ^ g2 ^ g3; break;
}
crc = (crc >> 4) ^ c;
}
i = i + 1;
}
return ~crc;
}
/* This is sort of like the table lookup version (crc32c), but using
a 256-way switch statement instead.
The expressions for g1, g2, ..., g7 are determined by examining what
the CRC-32 algorithm does to a byte of value 1, 2, 4, 8, 16, 32, 64, and
128, respectively. g6 and g7 are complicated because the rightmost 1-bit
in g0 enters the picture.
We rely on the compiler to evaluate, at compile time, all the
expressions involving the g's. They are the table values used in
function crc32c above (i.e., g7 = table[1], g6 = table[2], g5 =
table[4], etc.).
This idea of using a switch statement is a dumb idea if a compiler is
used, because the compiler (GCC anyway) implements the switch statement
with a 256-word label table. Thus the program still has the load from a
table, and it is larger than crc32c by the three words of instructions
used at each case statement (two instructions to load the constant, plus
a branch). However, since each case statement has the same amount of
code (three words), the label table could be avoided if the program were
coded in assembly language. But it would still have poor I-cache
performance.
At any rate, when compiled to Cyclops with GCC, this function
executes 6 + 19n instructions, where n is the number of bytes in the
input message. The 19 includes 2 loads and 3 branches (per byte), not
counting the one GCC generates to check that the switch argument doesn't
exceed 255 (it can't exceed 255). */
i = 0;
crc = 0xFFFFFFFF;
while ((byte = message[i]) != 0) { // Get next byte.
crc = crc ^ byte;
switch(crc & 0xFF) {
case 0: c = 0; break;
case 1: c = g7; break;
case 2: c = g6; break;
case 3: c = g6^g7; break;
case 4: c = g5; break;
case 5: c = g5^ g7; break;
case 6: c = g5^g6; break;
case 7: c = g5^g6^g7; break;
case 8: c = g4; break;
case 9: c = g4^ g7; break;
case 10: c = g4^ g6; break;
case 11: c = g4^ g6^g7; break;
case 12: c = g4^g5; break;
case 13: c = g4^g5^ g7; break;
case 14: c = g4^g5^g6; break;
case 15: c = g4^g5^g6^g7; break;
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0) {
byte = message[i]; // Get next byte.
crc = crc ^ byte;
for (j = 1; j >= 0; j--) { // Do two times.
c = ((crc<<31>>31) & g3) ^ ((crc<<30>>31) & g2) ^
((crc<<29>>31) & g1) ^ ((crc<<28>>31) & g0);
crc = ((unsigned)crc >> 4) ^ c;
}
i = i + 1;
}
return ~crc;
}
i = 0;
crc = 0xFFFFFFFF;
while ((byte = message[i]) != 0) { // Get next byte.
crc = crc ^ byte;
c = ((crc<<31>>31) & g7) ^ ((crc<<30>>31) & g6) ^
((crc<<29>>31) & g5) ^ ((crc<<28>>31) & g4) ^
((crc<<27>>31) & g3) ^ ((crc<<26>>31) & g2) ^
((crc<<25>>31) & g1) ^ ((crc<<24>>31) & g0);
crc = ((unsigned)crc >> 8) ^ c;
i = i + 1;
}
return ~crc;
}
if (argc != 2) {
printf("Must have exactly one argument, a message.\n"
"You can put quotes around it if it has blanks.\n");
exit(1);
}
/* The code above computes, in several ways, the cyclic redundancy check
usually referred to as CRC-32. This code is used by IEEE-802 (LAN/MAN
standard), PKZip, WinZip, Ethernet, and some DOD applications.
This file contains eight routines for doing the CRC-32 calculation, and
a simple test driver main program.
For references, there are a few web sites, and the book "Numerical
Recipes in Fortran, The Art of Scientific Computing," by William H.
Press, Saul A. Teukolsky, William T. Vetterling, and Brian P. Flannery,
Cambridge University Press, 1992 (2nd ed.), pps 888-894.
Peterson, W.W. and Brown, D.T. "Cyclic Codes for Error Detection." In
Proceedings of the IRE, January 1961, 228-235.
There are other programs for computing CRC-32 at the PC Magazine web
site http://www.createwindow.com/programming/crc32/.