crc32_braid_comb_p.h 908 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #ifndef CRC32_BRAID_COMB_P_H_
  2. #define CRC32_BRAID_COMB_P_H_
  3. /*
  4. Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial,
  5. reflected. For speed, this requires that a not be zero.
  6. */
  7. static uint32_t multmodp(uint32_t a, uint32_t b) {
  8. uint32_t m, p;
  9. m = (uint32_t)1 << 31;
  10. p = 0;
  11. for (;;) {
  12. if (a & m) {
  13. p ^= b;
  14. if ((a & (m - 1)) == 0)
  15. break;
  16. }
  17. m >>= 1;
  18. b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
  19. }
  20. return p;
  21. }
  22. /*
  23. Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been
  24. initialized.
  25. */
  26. static uint32_t x2nmodp(z_off64_t n, unsigned k) {
  27. uint32_t p;
  28. p = (uint32_t)1 << 31; /* x^0 == 1 */
  29. while (n) {
  30. if (n & 1)
  31. p = multmodp(x2n_table[k & 31], p);
  32. n >>= 1;
  33. k++;
  34. }
  35. return p;
  36. }
  37. #endif /* CRC32_BRAID_COMB_P_H_ */