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