Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uint8_t gmul(uint8_t a, uint8_t b) {
- uint8_t p = 0; /* init product value */
- uint8_t counter; /* init loop counter */
- uint8_t carry; /* init carry variable for checking if ax has an x^8 term */
- for (counter = 0; counter < 8; counter++) { /* for each of the 8 bits in b */
- if (b & 1) /* if the least significant bit is set */
- p ^= a; /* add a to p */
- carry = a & 0x80; /* Set carry to the x^7 term of a */
- a <<= 1; /* Shift a left 1, turning a into a*x */
- if (carry) /* if ax should have had an x^8 term (x^7*x=x^8) */
- /* Using rewrite rule, turn x^8 into x^4+x^3+x+1
- =00011011=0x001B */
- a ^= 0x001B; /* Add this on to a, so a now stores ax */
- b >>= 1; /* right shift b, so the least significant bit is coef of x */
- } /* end loop, and since b=b/x, a=ax; loop will now add on a*x if required */
- return p; /* finally, return the product */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement