]>
Commit | Line | Data |
---|---|---|
5a4d6a2d DW |
1 | /* |
2 | * Use slice-by-8, which is the fastest variant. | |
3 | * | |
4 | * Calculate checksum 8 bytes at a time with a clever slicing algorithm. | |
5 | * This is the fastest algorithm, but comes with a 8KiB lookup table. | |
6 | * Most modern processors have enough cache to hold this table without | |
7 | * thrashing the cache. | |
8 | * | |
9 | * The Linux kernel uses this as the default implementation "unless you | |
10 | * have a good reason not to". The reason why Kconfig urges you to pick | |
11 | * SLICEBY8 is because people challenged the assertion that we should | |
12 | * always use slice by 8, so Darrick wrote a crc microbenchmark utility | |
13 | * and ran it on as many machines as he could get his hands on to show | |
14 | * that sb8 was the fastest. | |
15 | * | |
16 | * Every 64-bit machine (and most of the 32-bit ones too) saw the best | |
17 | * results with sb8. Any machine with more than 4K of cache saw better | |
18 | * results. The spreadsheet still exists today[1]; note that | |
19 | * 'crc32-kern-le' corresponds to the slice by 4 algorithm which is the | |
20 | * default unless CRC_LE_BITS is defined explicitly. | |
21 | * | |
22 | * FWIW, there are a handful of board defconfigs in the kernel that | |
23 | * don't pick sliceby8. These are all embedded 32-bit mips/ppc systems | |
24 | * with very small cache sizes which experience cache thrashing with the | |
25 | * slice by 8 algorithm, and therefore chose to pick defaults that are | |
26 | * saner for their particular board configuration. For nearly all of | |
27 | * XFS' perceived userbase (which we assume are 32 and 64-bit machines | |
28 | * with sufficiently large CPU cache and largeish storage devices) slice | |
29 | * by 8 is the right choice. | |
30 | * | |
31 | * [1] https://goo.gl/0LSzsG ("crc32c_bench") | |
32 | */ | |
33 | #define CRC_LE_BITS 64 | |
34 | ||
7e280e68 DC |
35 | /* |
36 | * There are multiple 16-bit CRC polynomials in common use, but this is | |
37 | * *the* standard CRC-32 polynomial, first popularized by Ethernet. | |
38 | * 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 | |
39 | */ | |
40 | #define CRCPOLY_LE 0xedb88320 | |
41 | #define CRCPOLY_BE 0x04c11db7 | |
42 | ||
43 | /* | |
44 | * This is the CRC32c polynomial, as outlined by Castagnoli. | |
45 | * 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+ | |
46 | * x^8+x^6+x^0 | |
47 | */ | |
48 | #define CRC32C_POLY_LE 0x82F63B78 | |
49 | ||
50 | /* Try to choose an implementation variant via Kconfig */ | |
51 | #ifdef CONFIG_CRC32_SLICEBY8 | |
52 | # define CRC_LE_BITS 64 | |
53 | # define CRC_BE_BITS 64 | |
54 | #endif | |
55 | #ifdef CONFIG_CRC32_SLICEBY4 | |
56 | # define CRC_LE_BITS 32 | |
57 | # define CRC_BE_BITS 32 | |
58 | #endif | |
59 | #ifdef CONFIG_CRC32_SARWATE | |
60 | # define CRC_LE_BITS 8 | |
61 | # define CRC_BE_BITS 8 | |
62 | #endif | |
63 | #ifdef CONFIG_CRC32_BIT | |
64 | # define CRC_LE_BITS 1 | |
65 | # define CRC_BE_BITS 1 | |
66 | #endif | |
67 | ||
68 | /* | |
69 | * How many bits at a time to use. Valid values are 1, 2, 4, 8, 32 and 64. | |
70 | * For less performance-sensitive, use 4 or 8 to save table size. | |
71 | * For larger systems choose same as CPU architecture as default. | |
72 | * This works well on X86_64, SPARC64 systems. This may require some | |
73 | * elaboration after experiments with other architectures. | |
74 | */ | |
75 | #ifndef CRC_LE_BITS | |
76 | # ifdef CONFIG_64BIT | |
77 | # define CRC_LE_BITS 64 | |
78 | # else | |
79 | # define CRC_LE_BITS 32 | |
80 | # endif | |
81 | #endif | |
82 | #ifndef CRC_BE_BITS | |
83 | # ifdef CONFIG_64BIT | |
84 | # define CRC_BE_BITS 64 | |
85 | # else | |
86 | # define CRC_BE_BITS 32 | |
87 | # endif | |
88 | #endif | |
89 | ||
90 | /* | |
91 | * Little-endian CRC computation. Used with serial bit streams sent | |
92 | * lsbit-first. Be sure to use cpu_to_le32() to append the computed CRC. | |
93 | */ | |
94 | #if CRC_LE_BITS > 64 || CRC_LE_BITS < 1 || CRC_LE_BITS == 16 || \ | |
95 | CRC_LE_BITS & CRC_LE_BITS-1 | |
96 | # error "CRC_LE_BITS must be one of {1, 2, 4, 8, 32, 64}" | |
97 | #endif | |
98 | ||
99 | /* | |
100 | * Big-endian CRC computation. Used with serial bit streams sent | |
101 | * msbit-first. Be sure to use cpu_to_be32() to append the computed CRC. | |
102 | */ | |
103 | #if CRC_BE_BITS > 64 || CRC_BE_BITS < 1 || CRC_BE_BITS == 16 || \ | |
104 | CRC_BE_BITS & CRC_BE_BITS-1 | |
105 | # error "CRC_BE_BITS must be one of {1, 2, 4, 8, 32, 64}" | |
106 | #endif |