xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/crc32c_defs.h (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * There are multiple 16-bit CRC polynomials in common use, but this is
3*6a54128fSAndroid Build Coastguard Worker  * *the* standard CRC-32 polynomial, first popularized by Ethernet.
4*6a54128fSAndroid Build Coastguard Worker  * x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0
5*6a54128fSAndroid Build Coastguard Worker  */
6*6a54128fSAndroid Build Coastguard Worker #define CRCPOLY_LE 0xedb88320
7*6a54128fSAndroid Build Coastguard Worker #define CRCPOLY_BE 0x04c11db7
8*6a54128fSAndroid Build Coastguard Worker 
9*6a54128fSAndroid Build Coastguard Worker /*
10*6a54128fSAndroid Build Coastguard Worker  * This is the CRC32c polynomial, as outlined by Castagnoli.
11*6a54128fSAndroid Build Coastguard Worker  * x^32+x^28+x^27+x^26+x^25+x^23+x^22+x^20+x^19+x^18+x^14+x^13+x^11+x^10+x^9+
12*6a54128fSAndroid Build Coastguard Worker  * x^8+x^6+x^0
13*6a54128fSAndroid Build Coastguard Worker  */
14*6a54128fSAndroid Build Coastguard Worker #define CRC32C_POLY_LE 0x82F63B78
15*6a54128fSAndroid Build Coastguard Worker #define CRC32C_POLY_BE 0x1EDC6F41
16*6a54128fSAndroid Build Coastguard Worker 
17*6a54128fSAndroid Build Coastguard Worker /* How many bits at a time to use.  Valid values are 1, 2, 4, 8, 32 and 64. */
18*6a54128fSAndroid Build Coastguard Worker /* For less performance-sensitive, use 4 */
19*6a54128fSAndroid Build Coastguard Worker #ifndef CRC_LE_BITS
20*6a54128fSAndroid Build Coastguard Worker # define CRC_LE_BITS 64
21*6a54128fSAndroid Build Coastguard Worker #endif
22*6a54128fSAndroid Build Coastguard Worker #ifndef CRC_BE_BITS
23*6a54128fSAndroid Build Coastguard Worker # define CRC_BE_BITS 64
24*6a54128fSAndroid Build Coastguard Worker #endif
25*6a54128fSAndroid Build Coastguard Worker 
26*6a54128fSAndroid Build Coastguard Worker /*
27*6a54128fSAndroid Build Coastguard Worker  * Little-endian CRC computation.  Used with serial bit streams sent
28*6a54128fSAndroid Build Coastguard Worker  * lsbit-first.  Be sure to use cpu_to_le32() to append the computed CRC.
29*6a54128fSAndroid Build Coastguard Worker  */
30*6a54128fSAndroid Build Coastguard Worker #if CRC_LE_BITS > 64 || CRC_LE_BITS < 1 || CRC_LE_BITS == 16 || \
31*6a54128fSAndroid Build Coastguard Worker 	CRC_LE_BITS & CRC_LE_BITS-1
32*6a54128fSAndroid Build Coastguard Worker # error "CRC_LE_BITS must be one of {1, 2, 4, 8, 32, 64}"
33*6a54128fSAndroid Build Coastguard Worker #endif
34*6a54128fSAndroid Build Coastguard Worker 
35*6a54128fSAndroid Build Coastguard Worker /*
36*6a54128fSAndroid Build Coastguard Worker  * Big-endian CRC computation.  Used with serial bit streams sent
37*6a54128fSAndroid Build Coastguard Worker  * msbit-first.  Be sure to use cpu_to_be32() to append the computed CRC.
38*6a54128fSAndroid Build Coastguard Worker  */
39*6a54128fSAndroid Build Coastguard Worker #if CRC_BE_BITS > 64 || CRC_BE_BITS < 1 || CRC_BE_BITS == 16 || \
40*6a54128fSAndroid Build Coastguard Worker 	CRC_BE_BITS & CRC_BE_BITS-1
41*6a54128fSAndroid Build Coastguard Worker # error "CRC_BE_BITS must be one of {1, 2, 4, 8, 32, 64}"
42*6a54128fSAndroid Build Coastguard Worker #endif
43*6a54128fSAndroid Build Coastguard Worker 
44*6a54128fSAndroid Build Coastguard Worker 
45*6a54128fSAndroid Build Coastguard Worker #define ___constant_swab32(x) \
46*6a54128fSAndroid Build Coastguard Worker 	((uint32_t)( \
47*6a54128fSAndroid Build Coastguard Worker 		(((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
48*6a54128fSAndroid Build Coastguard Worker 		(((uint32_t)(x) & (uint32_t)0x0000ff00UL) <<  8) | \
49*6a54128fSAndroid Build Coastguard Worker 		(((uint32_t)(x) & (uint32_t)0x00ff0000UL) >>  8) | \
50*6a54128fSAndroid Build Coastguard Worker 		(((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
51*6a54128fSAndroid Build Coastguard Worker 
52*6a54128fSAndroid Build Coastguard Worker 
53*6a54128fSAndroid Build Coastguard Worker #if (__GNUC__ >= 3)
54*6a54128fSAndroid Build Coastguard Worker #define likely(x)	__builtin_expect(!!(x), 1)
55*6a54128fSAndroid Build Coastguard Worker #define unlikely(x)	__builtin_expect(!!(x), 0)
56*6a54128fSAndroid Build Coastguard Worker #else
57*6a54128fSAndroid Build Coastguard Worker #define likely(x)	(x)
58*6a54128fSAndroid Build Coastguard Worker #define unlikely(x)	(x)
59*6a54128fSAndroid Build Coastguard Worker #endif
60