Sunteți pe pagina 1din 13

/* This is a collection of programs that computes the CRC-32 checksum.

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> // ---------------------------- reverse -------------------------------// Reverses (reflects) bits in a 32-bit word. unsigned reverse(unsigned x) { x = ((x & 0x55555555) << 1) | ((x >> 1) & 0x55555555); x = ((x & 0x33333333) << 2) | ((x >> 2) & 0x33333333); x = ((x & 0x0F0F0F0F) << 4) | ((x >> 4) & 0x0F0F0F0F); x = (x << 24) | ((x & 0xFF00) << 8) | ((x >> 8) & 0xFF00) | (x >> 24); return x; } // ----------------------------- crc32a -------------------------------/* This is the basic CRC algorithm with no optimizations. It follows the logic circuit as closely as possible. */ unsigned int crc32a(unsigned char *message) { int i, j; unsigned int byte, crc; 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); } // ----------------------------- 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; } // ----------------------------- crc32c -------------------------------/* 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. */ unsigned int crc32c(unsigned char *message) { int i, j; unsigned int byte, crc, mask; static unsigned int table[256]; /* Set up the table, if necessary. */ 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; } } /* Through with table setup, now calculate the CRC. */ i = 0; crc = 0xFFFFFFFF; while ((byte = message[i]) != 0) { crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF]; i = i + 1; } return ~crc; } // ---------------------------- crc32cx -------------------------------/* This is crc32b modified to load the message a fullword at a time. It assumes the message is word aligned and consists of an integral number of words before the 0-byte that marks the end of the message.

This works only on a little-endian machine. Not counting the table setup (which would probably be a separate function), this function should be doable in 3 + 22w instructions, where w is the number of fullwords in the input message. This is equivalent to 3 + 5.5n instructions, where n is the number of bytes. 1.25 of those 5.5 instructions are loads. This is Exercise 1 in the text. C.f. Christopher Dannemiller, who got it from Linux Source base, www.gelato.unsw.edu.au/lxr/source/lib/crc32.c, lines 105-111. */ unsigned int crc32cx(unsigned char *message) { int j; unsigned int byte, crc, mask, word; static unsigned int table[256]; /* Set up the table, if necessary. */ 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; } } /* Through with table setup, now calculate the 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; } // ----------------------------- crc32d -------------------------------/* 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. */ unsigned int crc32d(unsigned char *message) { int i, j; unsigned int byte, half, crc, mask; static unsigned int table[65536];

if (table[1] == 0) {

// If table has not yet // been set up: for (half = 0; half <= 65535; half++) { crc = half; for (j = 15; j >= 0; j--) { // Do 15 times. mask = -(crc & 1); crc = (crc >> 1) ^ (0xEDB88320 & mask); } table[half] = crc; }

} crc = 0xFFFFFFFF; // First, if message is aligned on an odd address, // take care of the first byte. i = (int)&message[0] & 1; // Start of halfwords. if (i) { // If i == 1: byte = message[0]; if (byte == 0) return 0; // If null message. crc = (crc >> 8) ^ table[(byte ^ 0xFF) << 8]; } // Next process the message two bytes at a time as long // as both bytes are nonzero. while (1) { half = *(unsigned short *)(&message[i]); if (half <= 0xFF || (half & 0xFF) == 0) break; crc = (crc >> 16) ^ table[(crc ^ half) & 0xFFFF]; i = i + 2; } // Lastly, process the odd byte at the end, if any. // "half" is of the form 00xx, xx00, or 0000. if (half & 0xFF) { crc = (crc >> 8) ^ table[((crc^half) & 0xFF) << 8]; } return ~crc; } // ----------------------------- crc32e -------------------------------/* 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. */ unsigned int crc32e(unsigned char *message) { int i, j; unsigned int byte, crc, c; const unsigned int g0 = 0xEDB88320, g1 = g0 >> 1, g2 = g0 >> 2, g3 = g0 >> 3;

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; } // ----------------------------- crc32f -------------------------------/* 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). */ unsigned int crc32f(unsigned char *message) { int i;

unsigned int byte, crc, c; const unsigned int g0 = 0xEDB88320; const unsigned int g1 = g0>>1, g2 = g0>>2, g3 = g0>>3, g4 = g0>>4, g5 = g0>>5, g6 = (g0>>6)^g0, g7 = ((g0>>6)^g0)>>1; 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; case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = g2; g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g3; g3^ g7; g3^ g6; g3^ g6^g7; g3^ g5; g3^ g5^ g7; g3^ g5^g6; g3^ g5^g6^g7; g3^g4; g3^g4^ g7; g3^g4^ g6; g3^g4^ g6^g7; g3^g4^g5; g3^g4^g5^ g7; g3^g4^g5^g6; g3^g4^g5^g6^g7; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break;

break; g7; break; g6; break; g6^g7; break; g5; break; g5^ g7; break; g5^g6; break; g5^g6^g7; break; g4; break; g4^ g7; break; g4^ g6; break; g4^ g6^g7; break; g4^g5; break; g4^g5^ g7; break; g4^g5^g6; break;

case 47: c = case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = g1; g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^ g1^

g2^

g4^g5^g6^g7; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break;

g2^g3; g2^g3^ g7; g2^g3^ g6; g2^g3^ g6^g7; g2^g3^ g5; g2^g3^ g5^ g7; g2^g3^ g5^g6; g2^g3^ g5^g6^g7; g2^g3^g4; g2^g3^g4^ g7; g2^g3^g4^ g6; g2^g3^g4^ g6^g7; g2^g3^g4^g5; g2^g3^g4^g5^ g7; g2^g3^g4^g5^g6; g2^g3^g4^g5^g6^g7;

break; g7; break; g6; break; g6^g7; break; g5; break; g5^ g7; break; g5^g6; break; g5^g6^g7; break; g4; break; g4^ g7; break; g4^ g6; break; g4^ g6^g7; break; g4^g5; break; g4^g5^ g7; break; g4^g5^g6; break; g4^g5^g6^g7; break; g3; g3^ g7; g3^ g6; g3^ g6^g7; g3^ g5; g3^ g5^ g7; g3^ g5^g6; g3^ g5^g6^g7; g3^g4; g3^g4^ g7; g3^g4^ g6; g3^g4^ g6^g7; g3^g4^g5; g3^g4^g5^ g7; g3^g4^g5^g6; g3^g4^g5^g6^g7; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break;

g1^g2; g1^g2^ g1^g2^ g1^g2^ g1^g2^ g1^g2^ g1^g2^

break; g7; break; g6; break; g6^g7; break; g5; break; g5^ g7; break; g5^g6; break;

case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case

103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159:

c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = g0; g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^

g1^g2^ g1^g2^ g1^g2^ g1^g2^ g1^g2^ g1^g2^ g1^g2^ g1^g2^ g1^g2^

g5^g6^g7; g4; g4^ g7; g4^ g6; g4^ g6^g7; g4^g5; g4^g5^ g7; g4^g5^g6; g4^g5^g6^g7;

break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break;

g1^g2^g3; g1^g2^g3^ g7; g1^g2^g3^ g6; g1^g2^g3^ g6^g7; g1^g2^g3^ g5; g1^g2^g3^ g5^ g7; g1^g2^g3^ g5^g6; g1^g2^g3^ g5^g6^g7; g1^g2^g3^g4; g1^g2^g3^g4^ g7; g1^g2^g3^g4^ g6; g1^g2^g3^g4^ g6^g7; g1^g2^g3^g4^g5; g1^g2^g3^g4^g5^ g7; g1^g2^g3^g4^g5^g6; g1^g2^g3^g4^g5^g6^g7;

break; g7; break; g6; break; g6^g7; break; g5; break; g5^ g7; break; g5^g6; break; g5^g6^g7; break; g4; break; g4^ g7; break; g4^ g6; break; g4^ g6^g7; break; g4^g5; break; g4^g5^ g7; break; g4^g5^g6; break; g4^g5^g6^g7; break; g3; g3^ g7; g3^ g6; g3^ g6^g7; g3^ g5; g3^ g5^ g7; g3^ g5^g6; g3^ g5^g6^g7; g3^g4; g3^g4^ g7; g3^g4^ g6; g3^g4^ g6^g7; g3^g4^g5; g3^g4^g5^ g7; g3^g4^g5^g6; g3^g4^g5^g6^g7; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break;

case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case case

160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215:

c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^ g0^g1; g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^

g2; g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^ g2^

break; g7; break; g6; break; g6^g7; break; g5; break; g5^ g7; break; g5^g6; break; g5^g6^g7; break; g4; break; g4^ g7; break; g4^ g6; break; g4^ g6^g7; break; g4^g5; break; g4^g5^ g7; break; g4^g5^g6; break; g4^g5^g6^g7; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break;

g2^g3; g2^g3^ g7; g2^g3^ g6; g2^g3^ g6^g7; g2^g3^ g5; g2^g3^ g5^ g7; g2^g3^ g5^g6; g2^g3^ g5^g6^g7; g2^g3^g4; g2^g3^g4^ g7; g2^g3^g4^ g6; g2^g3^g4^ g6^g7; g2^g3^g4^g5; g2^g3^g4^g5^ g7; g2^g3^g4^g5^g6; g2^g3^g4^g5^g6^g7;

break; g7; break; g6; break; g6^g7; break; g5; break; g5^ g7; break; g5^g6; break; g5^g6^g7; break; g4; break; g4^ g7; break; g4^ g6; break; g4^ g6^g7; break; g4^g5; break; g4^g5^ g7; break; g4^g5^g6; break; g4^g5^g6^g7; break; g3; g3^ g3^ g3^ g3^ g3^ g3^ g3^ break; g7; break; g6; break; g6^g7; break; g5; break; g5^ g7; break; g5^g6; break; g5^g6^g7; break;

case case case case case case case case case case case case case case case case case case case case case case case case

216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239:

c c c c c c c c c c c c c c c c c c c c c c c c

= = = = = = = = = = = = = = = = = = = = = = = =

g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^ g0^g1^g2; g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^ g0^g1^g2^

g3^g4; g3^g4^ g7; g3^g4^ g6; g3^g4^ g6^g7; g3^g4^g5; g3^g4^g5^ g7; g3^g4^g5^g6; g3^g4^g5^g6^g7;

break; break; break; break; break; break; break; break;

break; g7; break; g6; break; g6^g7; break; g5; break; g5^ g7; break; g5^g6; break; g5^g6^g7; break; g4; break; g4^ g7; break; g4^ g6; break; g4^ g6^g7; break; g4^g5; break; g4^g5^ g7; break; g4^g5^g6; break; g4^g5^g6^g7; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break;

case 240: c = case 241: c = case 242: c = case 243: c = case 244: c = case 245: c = case 246: c = case 247: c = case 248: c = case 249: c = case 250: c = case 251: c = case 252: c = case 253: c = case 254: c = case 255: c = } // end switch

g0^g1^g2^g3; g0^g1^g2^g3^ g7; g0^g1^g2^g3^ g6; g0^g1^g2^g3^ g6^g7; g0^g1^g2^g3^ g5; g0^g1^g2^g3^ g5^ g7; g0^g1^g2^g3^ g5^g6; g0^g1^g2^g3^ g5^g6^g7; g0^g1^g2^g3^g4; g0^g1^g2^g3^g4^ g7; g0^g1^g2^g3^g4^ g6; g0^g1^g2^g3^g4^ g6^g7; g0^g1^g2^g3^g4^g5; g0^g1^g2^g3^g4^g5^ g7; g0^g1^g2^g3^g4^g5^g6; g0^g1^g2^g3^g4^g5^g6^g7;

crc = (crc >> 8) ^ c; i = i + 1; } return ~crc; } // ----------------------------- crc32g -------------------------------/* This is derived from crc32e by constructing the constant c using algebraic expressions involving the rightmost four bits of the crc register, rather than using a 16-way switch statement. We rely on the compiler to compute the constants g>>1, g>>2, and g>>3, and load them into registers ahead of the loops. Note that crc is now a SIGNED integer, so the right shifts of 31 are sign-propagating shifts. When compiled to Cyclops with GCC, this function executes in 14 + 46n

instructions, where n is the number of bytes in the input message. There is only one load per byte executed (but three branches). */ unsigned int crc32g(unsigned char *message) { int i, j, crc; unsigned int byte, c; const unsigned int g0 = 0xEDB88320, g1 = g0 >> 1, g2 = g0 >> 2, g3 = g0 >> 3; 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; } // ----------------------------- crc32h -------------------------------/* This is derived from crc32f by constructing the constant c using algebraic expressions involving the rightmost eight bits of the crc register, rather than using a 256-way switch statement. We rely on the compiler to compute the constants g>>1, g>>2, etc., and load them into registers ahead of the loops. Note that crc is now a SIGNED integer, so the right shifts of 31 are sign-propagating shifts. When compiled to Cyclops with GCC, this function executes in 22 + 38n instructions, where n is the number of bytes in the input message. There is only one load and one branch executed per byte. */ unsigned int crc32h(unsigned char *message) { int i, crc; unsigned int byte, c; const unsigned int g0 = 0xEDB88320, g1 = g0>>1, g2 = g0>>2, g3 = g0>>3, g4 = g0>>4, g5 = g0>>5, g6 = (g0>>6)^g0, g7 = ((g0>>6)^g0)>>1; i = 0; crc = 0xFFFFFFFF; while ((byte = message[i]) != crc = crc ^ byte; c = ((crc<<31>>31) & g7) ^ ((crc<<29>>31) & g5) ^ ((crc<<27>>31) & g3) ^ ((crc<<25>>31) & g1) ^ crc = ((unsigned)crc >> 8) i = i + 1; } return ~crc; } // ------------------------------ main --------------------------------int main(int argc, char ** argv) {

0) {

// Get next byte. & & & & g6) ^ g4) ^ g2) ^ g0);

((crc<<30>>31) ((crc<<28>>31) ((crc<<26>>31) ((crc<<24>>31) ^ c;

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); } printf("crc32a = printf("crc32b = printf("crc32c = printf("crc32cx= . printf("crc32d printf("crc32e printf("crc32f printf("crc32g printf("crc32h = = = = = %08x\n", %08x\n", %08x\n", %08x\n", %08x\n", crc32d(argv[1])); crc32e(argv[1])); crc32f(argv[1])); crc32g(argv[1])); crc32h(argv[1])); %08x\n", %08x\n", %08x\n", %08x\n", crc32a(argv[1])); crc32b(argv[1])); crc32c(argv[1])); crc32cx(argv[1])); // Must be a mult of 4 in length

// This one skips the first character, to test the // halfword function with an odd starting address. printf("\ncrc32d = %08x\n", crc32d(argv[1] + 1)); printf("crc32a = %08x\n", crc32a(argv[1] + 1)); return 0; } /* 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. I investigated this because an early reviewer of Hacker's Delight suggested that I might try to find some trick to speed up the calculation of the CRC checksum. I have tried and don't see any way to speed it up, over the standard table lookup method, which does one table lookup per byte of message. The table size is 256 32-bit words. One could do two bytes at a time with a table of size 65536 words, but that's not very interesting. Not interesting, but maybe useful on a machine with a large data cache, so the code is shown above. 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. Another book reference is "Computer Networks," by Andrew S. Tanenbaum, second edition, pages 208-212. Another reference, which serves as a good introduction to the subject, and which I found very well-written and interesting, is: Peterson, W.W. and Brown, D.T. "Cyclic Codes for Error Detection." In Proceedings of the IRE, January 1961, 228-235. The web site http://www.ciphersbyritter.com/ARTS/CRCMYST.HTM by Terry Ritter is a good intro. This material also appeared as "The Great CRC Mystery," in Dr. Dobb's Journal of Software Tools, February 1986, 26-34 and 76-83. He gives several more references.

There are other programs for computing CRC-32 at the PC Magazine web site http://www.createwindow.com/programming/crc32/. This code is not in Hacker's Delight, although a few of the simpler programs may be included in a future edition. */

S-ar putea să vă placă și