xref: /OK3568_Linux_fs/kernel/lib/crc32defs.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /* Try to choose an implementation variant via Kconfig */
4*4882a593Smuzhiyun #ifdef CONFIG_CRC32_SLICEBY8
5*4882a593Smuzhiyun # define CRC_LE_BITS 64
6*4882a593Smuzhiyun # define CRC_BE_BITS 64
7*4882a593Smuzhiyun #endif
8*4882a593Smuzhiyun #ifdef CONFIG_CRC32_SLICEBY4
9*4882a593Smuzhiyun # define CRC_LE_BITS 32
10*4882a593Smuzhiyun # define CRC_BE_BITS 32
11*4882a593Smuzhiyun #endif
12*4882a593Smuzhiyun #ifdef CONFIG_CRC32_SARWATE
13*4882a593Smuzhiyun # define CRC_LE_BITS 8
14*4882a593Smuzhiyun # define CRC_BE_BITS 8
15*4882a593Smuzhiyun #endif
16*4882a593Smuzhiyun #ifdef CONFIG_CRC32_BIT
17*4882a593Smuzhiyun # define CRC_LE_BITS 1
18*4882a593Smuzhiyun # define CRC_BE_BITS 1
19*4882a593Smuzhiyun #endif
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun  * How many bits at a time to use.  Valid values are 1, 2, 4, 8, 32 and 64.
23*4882a593Smuzhiyun  * For less performance-sensitive, use 4 or 8 to save table size.
24*4882a593Smuzhiyun  * For larger systems choose same as CPU architecture as default.
25*4882a593Smuzhiyun  * This works well on X86_64, SPARC64 systems. This may require some
26*4882a593Smuzhiyun  * elaboration after experiments with other architectures.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun #ifndef CRC_LE_BITS
29*4882a593Smuzhiyun #  ifdef CONFIG_64BIT
30*4882a593Smuzhiyun #  define CRC_LE_BITS 64
31*4882a593Smuzhiyun #  else
32*4882a593Smuzhiyun #  define CRC_LE_BITS 32
33*4882a593Smuzhiyun #  endif
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun #ifndef CRC_BE_BITS
36*4882a593Smuzhiyun #  ifdef CONFIG_64BIT
37*4882a593Smuzhiyun #  define CRC_BE_BITS 64
38*4882a593Smuzhiyun #  else
39*4882a593Smuzhiyun #  define CRC_BE_BITS 32
40*4882a593Smuzhiyun #  endif
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun  * Little-endian CRC computation.  Used with serial bit streams sent
45*4882a593Smuzhiyun  * lsbit-first.  Be sure to use cpu_to_le32() to append the computed CRC.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun #if CRC_LE_BITS > 64 || CRC_LE_BITS < 1 || CRC_LE_BITS == 16 || \
48*4882a593Smuzhiyun 	CRC_LE_BITS & CRC_LE_BITS-1
49*4882a593Smuzhiyun # error "CRC_LE_BITS must be one of {1, 2, 4, 8, 32, 64}"
50*4882a593Smuzhiyun #endif
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun  * Big-endian CRC computation.  Used with serial bit streams sent
54*4882a593Smuzhiyun  * msbit-first.  Be sure to use cpu_to_be32() to append the computed CRC.
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun #if CRC_BE_BITS > 64 || CRC_BE_BITS < 1 || CRC_BE_BITS == 16 || \
57*4882a593Smuzhiyun 	CRC_BE_BITS & CRC_BE_BITS-1
58*4882a593Smuzhiyun # error "CRC_BE_BITS must be one of {1, 2, 4, 8, 32, 64}"
59*4882a593Smuzhiyun #endif
60