/src/zlib-ng/crc32_braid_comb_p.h
Line | Count | Source |
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 | | */ |
8 | 1.81M | static uint32_t multmodp(uint32_t a, uint32_t b) { |
9 | 1.81M | uint32_t m, p; |
10 | | |
11 | 1.81M | m = (uint32_t)1 << 31; |
12 | 1.81M | p = 0; |
13 | 31.1M | for (;;) { |
14 | 31.1M | if (a & m) { |
15 | 13.3M | p ^= b; |
16 | 13.3M | if ((a & (m - 1)) == 0) |
17 | 1.81M | break; |
18 | 13.3M | } |
19 | 29.3M | m >>= 1; |
20 | 29.3M | b = b & 1 ? (b >> 1) ^ POLY : b >> 1; |
21 | 29.3M | } |
22 | 1.81M | return p; |
23 | 1.81M | } |
24 | | |
25 | | /* |
26 | | Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been |
27 | | initialized. |
28 | | */ |
29 | 3.28k | static uint32_t x2nmodp(z_off64_t n, unsigned k) { |
30 | 3.28k | uint32_t p; |
31 | | |
32 | 3.28k | p = (uint32_t)1 << 31; /* x^0 == 1 */ |
33 | 30.2k | while (n) { |
34 | 26.9k | if (n & 1) |
35 | 13.6k | p = multmodp(x2n_table[k & 31], p); |
36 | 26.9k | n >>= 1; |
37 | 26.9k | k++; |
38 | 26.9k | } |
39 | 3.28k | return p; |
40 | 3.28k | } |
41 | | |
42 | | #endif /* CRC32_BRAID_COMB_P_H_ */ |