Crc 16 Calculation Example5/5/2021
Unfortunately, the modulo-2 arithmetic used to compute CRCs doesnt map easily into software.
Crc 16 Calculation Example How To Implement AnThis article shows how to implement an efficient CRC in C or C.Generally speaking, CRCs are most efficiently calculated in dedicated hardware.However, sometimes you must compute a CRC in software, for example in a C or C program that will run in an embedded system.![]() Ill start with a naive implementation and gradually improve the efficiency of the code as I go along. However, Im going to keep the discussion at the level of the C language; further steps could be taken to improve the efficiency of the final code by moving into the assembly language of your particular processor. Knowing that all CRC algorithms are simply long division algorithms in disguise doesnt help. Modulo-2 binary division doesnt map particularly well to the instruction sets of off-the-shelf processors. For one thing, generally no registers are available to hold the very long bit sequence that is the numerator. For another, modulo-2 binary division is not the same as ordinary division. So even if your processor has a division instruction, you wont be able to use it. The number to be divided is the message augmented with zeros at the end. The number of zero bits added to the message is the same as the width of the checksum (what I call c); in this case four bits were added. The divisor is a c1-bit number known as the generator polynomial. ![]() The bit thats shifted out will always be a zero, so no information is lost. So we wont actually need to track the quotient in our software implementation. Also note here that the result of each XOR with the generator polynomial is a remainder that has zero in its most significant bit. So we never lose any information when the next message bit is shifted into the remainder. It simply attempts to implement that algorithm as it was described above for this one particular generator polynomial. Even though the unnecessary steps have been eliminated, its extremely inefficient. Multiple C statements (at least the decrement and compare, binary AND, test for zero, and left shift operations) must be executed for each bit in the message. But what if the message contains several hundred bytes, as is typically the case in a real-world application You dont want to execute dozens of processor opcodes for each byte of input data.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |