home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.dcom.modems
- Path: sparky!uunet!indetech!wetware!wsrcc!wolfgang
- From: wolfgang@wsrcc.com (Wolfgang S. Rupprecht)
- Subject: Re: Efficient CRC routine?
- Message-ID: <By5Hrs.GI3@wsrcc.com>
- Keywords: CRC
- Organization: W S Rupprecht Computer Consulting, Fremont CA
- References: <1394@minya.UUCP>
- Date: Mon, 23 Nov 1992 03:59:51 GMT
- Lines: 34
-
- jc@minya.UUCP (John Chambers) writes:
- >I've generally dismissed the use of CRCs in software, because the only
- >algorithm I'd seen to generate (or check) them were so incredibly slow
- >(shifting the bits out of each byte, plugging each into a polynomial,
- >and all that) that the performance hit was totally out of the range of
- >acceptability. This, if you implement the algorithm in McNamara on a
- >386, calculating a CRC for a 1Kbyte chunk takes a major part of a
- >second. It might as well take a year. CRCs seemed like something of
- >interest only to hardware types, who could implement them with
- >parallel hardware and do it quickly.
-
- Its only one or two lines of C depending on how you count it. Any
- good compiler will render it into 10 or less instructions. Basically
- the inner loop is:
-
- unsigned char *cp, *ep; /* current and end pointer */
- unsigned short crc, crctab[];
-
- [...]
-
- while (cp < ep)
- crc = crctab[(unsigned char) crc ^ *cp++] ^ (crc >> 8);
-
- Filling in the crctab is left as an exercise to the reader. ;-)
-
- (The biggest trick is often re-writing the C and casting it enough
- that the compiler gets a clue and generates good code. For a typical
- 68k compiler you want to entice it to generate a dbz loop. For a
- typical RISC compiler you probably want to do a pointer loop.)
-
- -wolfgang
- --
- Wolfgang Rupprecht wolfgang@wsrcc.com (or) decwrl!wsrcc!wolfgang
- Snail Mail: 39469 Gallaudet Drive, Fremont, CA 94538-4511
-