Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. uint8_t gmul(uint8_t a, uint8_t b) {
  2. uint8_t p = 0; /* init product value */
  3. uint8_t counter; /* init loop counter */
  4. uint8_t carry; /* init carry variable for checking if ax has an x^8 term */
  5. for (counter = 0; counter < 8; counter++) { /* for each of the 8 bits in b */
  6. if (b & 1) /* if the least significant bit is set */
  7. p ^= a; /* add a to p */
  8. carry = a & 0x80; /* Set carry to the x^7 term of a */
  9. a <<= 1; /* Shift a left 1, turning a into a*x */
  10. if (carry) /* if ax should have had an x^8 term (x^7*x=x^8) */
  11. /* Using rewrite rule, turn x^8 into x^4+x^3+x+1
  12. =00011011=0x001B */
  13. a ^= 0x001B; /* Add this on to a, so a now stores ax */
  14. b >>= 1; /* right shift b, so the least significant bit is coef of x */
  15. } /* end loop, and since b=b/x, a=ax; loop will now add on a*x if required */
  16. return p; /* finally, return the product */
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement