xref: /linux/lib/crc32.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
11da177e4SLinus Torvalds /*
278dff418SBob Pearson  * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin
378dff418SBob Pearson  * cleaned up code to current version of sparse and added the slicing-by-8
478dff418SBob Pearson  * algorithm to the closely similar existing slicing-by-4 algorithm.
578dff418SBob Pearson  *
61da177e4SLinus Torvalds  * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
71da177e4SLinus Torvalds  * Nicer crc32 functions/docs submitted by linux@horizon.com.  Thanks!
81da177e4SLinus Torvalds  * Code was from the public domain, copyright abandoned.  Code was
91da177e4SLinus Torvalds  * subsequently included in the kernel, thus was re-licensed under the
101da177e4SLinus Torvalds  * GNU GPL v2.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
131da177e4SLinus Torvalds  * Same crc32 function was used in 5 other places in the kernel.
141da177e4SLinus Torvalds  * I made one version, and deleted the others.
151da177e4SLinus Torvalds  * There are various incantations of crc32().  Some use a seed of 0 or ~0.
161da177e4SLinus Torvalds  * Some xor at the end with ~0.  The generic crc32() function takes
171da177e4SLinus Torvalds  * seed as an argument, and doesn't xor at the end.  Then individual
181da177e4SLinus Torvalds  * users can do whatever they need.
191da177e4SLinus Torvalds  *   drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
201da177e4SLinus Torvalds  *   fs/jffs2 uses seed 0, doesn't xor with ~0.
211da177e4SLinus Torvalds  *   fs/partitions/efi.c uses seed ~0, xor's with ~0.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * This source code is licensed under the GNU General Public License,
241da177e4SLinus Torvalds  * Version 2.  See the file COPYING for more details.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
278e2a46a4SMauro Carvalho Chehab /* see: Documentation/staging/crc32.rst for a description of algorithms */
28fbedceb1SBob Pearson 
291da177e4SLinus Torvalds #include <linux/crc32.h>
301fb2e3f2SKrzysztof Kozlowski #include <linux/crc32poly.h>
311da177e4SLinus Torvalds #include <linux/module.h>
321da177e4SLinus Torvalds #include <linux/types.h>
33cc0ac199SDaniel Borkmann #include <linux/sched.h>
341da177e4SLinus Torvalds #include "crc32defs.h"
3560e58d5cSBob Pearson 
369a1dbf6aSBob Pearson #if CRC_LE_BITS > 8
3738b4fe5fSFabian Frederick # define tole(x) ((__force u32) cpu_to_le32(x))
381da177e4SLinus Torvalds #else
391da177e4SLinus Torvalds # define tole(x) (x)
404f2a9463SJoakim Tjernlund #endif
414f2a9463SJoakim Tjernlund 
429a1dbf6aSBob Pearson #if CRC_BE_BITS > 8
4338b4fe5fSFabian Frederick # define tobe(x) ((__force u32) cpu_to_be32(x))
444f2a9463SJoakim Tjernlund #else
451da177e4SLinus Torvalds # define tobe(x) (x)
461da177e4SLinus Torvalds #endif
4760e58d5cSBob Pearson 
481da177e4SLinus Torvalds #include "crc32table.h"
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
5146c5801eSDarrick J. Wong MODULE_DESCRIPTION("Various CRC32 calculations");
521da177e4SLinus Torvalds MODULE_LICENSE("GPL");
531da177e4SLinus Torvalds 
549a1dbf6aSBob Pearson #if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
55ddcaccbcSJoakim Tjernlund 
56324eb0f1SBob Pearson /* implements slicing-by-4 or slicing-by-8 algorithm */
57d8f1c477SGeorge Spelvin static inline u32 __pure
crc32_body(u32 crc,unsigned char const * buf,size_t len,const u32 (* tab)[256])58836e2af9SJoakim Tjernlund crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
59ddcaccbcSJoakim Tjernlund {
600d2daf5cSAndrew Morton # ifdef __LITTLE_ENDIAN
615742332dSJoakim Tjernlund #  define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
62324eb0f1SBob Pearson #  define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
63324eb0f1SBob Pearson 		   t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
64324eb0f1SBob Pearson #  define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
65324eb0f1SBob Pearson 		   t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
66ddcaccbcSJoakim Tjernlund # else
675742332dSJoakim Tjernlund #  define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
68324eb0f1SBob Pearson #  define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
69324eb0f1SBob Pearson 		   t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
70324eb0f1SBob Pearson #  define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
71324eb0f1SBob Pearson 		   t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
72ddcaccbcSJoakim Tjernlund # endif
734f2a9463SJoakim Tjernlund 	const u32 *b;
74ddcaccbcSJoakim Tjernlund 	size_t    rem_len;
750292c497SBob Pearson # ifdef CONFIG_X86
760292c497SBob Pearson 	size_t i;
770292c497SBob Pearson # endif
785742332dSJoakim Tjernlund 	const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
7949ac572bSThiago Rafael Becker # if CRC_LE_BITS != 32
80324eb0f1SBob Pearson 	const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
8149ac572bSThiago Rafael Becker # endif
82324eb0f1SBob Pearson 	u32 q;
83ddcaccbcSJoakim Tjernlund 
84ddcaccbcSJoakim Tjernlund 	/* Align it */
854f2a9463SJoakim Tjernlund 	if (unlikely((long)buf & 3 && len)) {
86ddcaccbcSJoakim Tjernlund 		do {
874f2a9463SJoakim Tjernlund 			DO_CRC(*buf++);
884f2a9463SJoakim Tjernlund 		} while ((--len) && ((long)buf)&3);
89ddcaccbcSJoakim Tjernlund 	}
90324eb0f1SBob Pearson 
91324eb0f1SBob Pearson # if CRC_LE_BITS == 32
92ddcaccbcSJoakim Tjernlund 	rem_len = len & 3;
93ddcaccbcSJoakim Tjernlund 	len = len >> 2;
94324eb0f1SBob Pearson # else
95324eb0f1SBob Pearson 	rem_len = len & 7;
96324eb0f1SBob Pearson 	len = len >> 3;
97324eb0f1SBob Pearson # endif
98324eb0f1SBob Pearson 
994f2a9463SJoakim Tjernlund 	b = (const u32 *)buf;
1000292c497SBob Pearson # ifdef CONFIG_X86
1010292c497SBob Pearson 	--b;
1020292c497SBob Pearson 	for (i = 0; i < len; i++) {
1030292c497SBob Pearson # else
104ddcaccbcSJoakim Tjernlund 	for (--b; len; --len) {
1050292c497SBob Pearson # endif
106324eb0f1SBob Pearson 		q = crc ^ *++b; /* use pre increment for speed */
107324eb0f1SBob Pearson # if CRC_LE_BITS == 32
108324eb0f1SBob Pearson 		crc = DO_CRC4;
109324eb0f1SBob Pearson # else
110324eb0f1SBob Pearson 		crc = DO_CRC8;
111324eb0f1SBob Pearson 		q = *++b;
112324eb0f1SBob Pearson 		crc ^= DO_CRC4;
113324eb0f1SBob Pearson # endif
114ddcaccbcSJoakim Tjernlund 	}
115ddcaccbcSJoakim Tjernlund 	len = rem_len;
116ddcaccbcSJoakim Tjernlund 	/* And the last few bytes */
117ddcaccbcSJoakim Tjernlund 	if (len) {
118ddcaccbcSJoakim Tjernlund 		u8 *p = (u8 *)(b + 1) - 1;
1190292c497SBob Pearson # ifdef CONFIG_X86
1200292c497SBob Pearson 		for (i = 0; i < len; i++)
1210292c497SBob Pearson 			DO_CRC(*++p); /* use pre increment for speed */
1220292c497SBob Pearson # else
123ddcaccbcSJoakim Tjernlund 		do {
124ddcaccbcSJoakim Tjernlund 			DO_CRC(*++p); /* use pre increment for speed */
125ddcaccbcSJoakim Tjernlund 		} while (--len);
1260292c497SBob Pearson # endif
127ddcaccbcSJoakim Tjernlund 	}
128ddcaccbcSJoakim Tjernlund 	return crc;
1294f2a9463SJoakim Tjernlund #undef DO_CRC
130836e2af9SJoakim Tjernlund #undef DO_CRC4
131324eb0f1SBob Pearson #undef DO_CRC8
132ddcaccbcSJoakim Tjernlund }
133ddcaccbcSJoakim Tjernlund #endif
13460e58d5cSBob Pearson 
1356e95fcaaSDaniel Borkmann 
1362f72100cSRandy Dunlap /**
137f2e1d2acSGu Zheng  * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
138f2e1d2acSGu Zheng  *			CRC32/CRC32C
139f2e1d2acSGu Zheng  * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for other
140f2e1d2acSGu Zheng  *	 uses, or the previous crc32/crc32c value if computing incrementally.
141f2e1d2acSGu Zheng  * @p: pointer to buffer over which CRC32/CRC32C is run
1422f72100cSRandy Dunlap  * @len: length of buffer @p
143f2e1d2acSGu Zheng  * @tab: little-endian Ethernet table
144f2e1d2acSGu Zheng  * @polynomial: CRC32/CRC32c LE polynomial
1452f72100cSRandy Dunlap  */
14646c5801eSDarrick J. Wong static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
14746c5801eSDarrick J. Wong 					  size_t len, const u32 (*tab)[256],
14846c5801eSDarrick J. Wong 					  u32 polynomial)
1491da177e4SLinus Torvalds {
15060e58d5cSBob Pearson #if CRC_LE_BITS == 1
1511da177e4SLinus Torvalds 	int i;
1521da177e4SLinus Torvalds 	while (len--) {
1531da177e4SLinus Torvalds 		crc ^= *p++;
1541da177e4SLinus Torvalds 		for (i = 0; i < 8; i++)
15546c5801eSDarrick J. Wong 			crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
1561da177e4SLinus Torvalds 	}
15760e58d5cSBob Pearson # elif CRC_LE_BITS == 2
15860e58d5cSBob Pearson 	while (len--) {
15960e58d5cSBob Pearson 		crc ^= *p++;
16046c5801eSDarrick J. Wong 		crc = (crc >> 2) ^ tab[0][crc & 3];
16146c5801eSDarrick J. Wong 		crc = (crc >> 2) ^ tab[0][crc & 3];
16246c5801eSDarrick J. Wong 		crc = (crc >> 2) ^ tab[0][crc & 3];
16346c5801eSDarrick J. Wong 		crc = (crc >> 2) ^ tab[0][crc & 3];
1641da177e4SLinus Torvalds 	}
16560e58d5cSBob Pearson # elif CRC_LE_BITS == 4
16660e58d5cSBob Pearson 	while (len--) {
16760e58d5cSBob Pearson 		crc ^= *p++;
16846c5801eSDarrick J. Wong 		crc = (crc >> 4) ^ tab[0][crc & 15];
16946c5801eSDarrick J. Wong 		crc = (crc >> 4) ^ tab[0][crc & 15];
17060e58d5cSBob Pearson 	}
17160e58d5cSBob Pearson # elif CRC_LE_BITS == 8
1729a1dbf6aSBob Pearson 	/* aka Sarwate algorithm */
1739a1dbf6aSBob Pearson 	while (len--) {
1749a1dbf6aSBob Pearson 		crc ^= *p++;
17546c5801eSDarrick J. Wong 		crc = (crc >> 8) ^ tab[0][crc & 255];
1769a1dbf6aSBob Pearson 	}
1779a1dbf6aSBob Pearson # else
178ce4320ddSBob Pearson 	crc = (__force u32) __cpu_to_le32(crc);
179ddcaccbcSJoakim Tjernlund 	crc = crc32_body(crc, p, len, tab);
180ce4320ddSBob Pearson 	crc = __le32_to_cpu((__force __le32)crc);
1811da177e4SLinus Torvalds #endif
18260e58d5cSBob Pearson 	return crc;
1831da177e4SLinus Torvalds }
18446c5801eSDarrick J. Wong 
18546c5801eSDarrick J. Wong #if CRC_LE_BITS == 1
1869784d82dSArd Biesheuvel u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
18746c5801eSDarrick J. Wong {
188e37f2f93SKrzysztof Kozlowski 	return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE);
18946c5801eSDarrick J. Wong }
1909784d82dSArd Biesheuvel u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
19146c5801eSDarrick J. Wong {
19246c5801eSDarrick J. Wong 	return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
19346c5801eSDarrick J. Wong }
19446c5801eSDarrick J. Wong #else
1959784d82dSArd Biesheuvel u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
19646c5801eSDarrick J. Wong {
197163a4e7fSKevin Bracey 	return crc32_le_generic(crc, p, len, crc32table_le, CRC32_POLY_LE);
19846c5801eSDarrick J. Wong }
1999784d82dSArd Biesheuvel u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
20046c5801eSDarrick J. Wong {
201163a4e7fSKevin Bracey 	return crc32_le_generic(crc, p, len, crc32ctable_le, CRC32C_POLY_LE);
20246c5801eSDarrick J. Wong }
20346c5801eSDarrick J. Wong #endif
2046d514b4eSGeorge Spelvin EXPORT_SYMBOL(crc32_le);
2056d514b4eSGeorge Spelvin EXPORT_SYMBOL(__crc32c_le);
2066d514b4eSGeorge Spelvin 
207ff98e20eSMiguel Ojeda u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
208ff98e20eSMiguel Ojeda u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
209*5cb29be4SKevin Bracey u32 __pure crc32_be_base(u32, unsigned char const *, size_t) __alias(crc32_be);
2109784d82dSArd Biesheuvel 
2116d514b4eSGeorge Spelvin /*
2126d514b4eSGeorge Spelvin  * This multiplies the polynomials x and y modulo the given modulus.
2136d514b4eSGeorge Spelvin  * This follows the "little-endian" CRC convention that the lsbit
2146d514b4eSGeorge Spelvin  * represents the highest power of x, and the msbit represents x^0.
2156d514b4eSGeorge Spelvin  */
2166d514b4eSGeorge Spelvin static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
2176e95fcaaSDaniel Borkmann {
2186d514b4eSGeorge Spelvin 	u32 product = x & 1 ? y : 0;
2196d514b4eSGeorge Spelvin 	int i;
2206d514b4eSGeorge Spelvin 
2216d514b4eSGeorge Spelvin 	for (i = 0; i < 31; i++) {
2226d514b4eSGeorge Spelvin 		product = (product >> 1) ^ (product & 1 ? modulus : 0);
2236d514b4eSGeorge Spelvin 		x >>= 1;
2246d514b4eSGeorge Spelvin 		product ^= x & 1 ? y : 0;
2256e95fcaaSDaniel Borkmann 	}
2266e95fcaaSDaniel Borkmann 
2276d514b4eSGeorge Spelvin 	return product;
2286e95fcaaSDaniel Borkmann }
2296d514b4eSGeorge Spelvin 
2306d514b4eSGeorge Spelvin /**
2318a29896aSRandy Dunlap  * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
2326d514b4eSGeorge Spelvin  * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
2336d514b4eSGeorge Spelvin  * @len: The number of bytes. @crc is multiplied by x^(8*@len)
2346d514b4eSGeorge Spelvin  * @polynomial: The modulus used to reduce the result to 32 bits.
2356d514b4eSGeorge Spelvin  *
2366d514b4eSGeorge Spelvin  * It's possible to parallelize CRC computations by computing a CRC
2376d514b4eSGeorge Spelvin  * over separate ranges of a buffer, then summing them.
2386d514b4eSGeorge Spelvin  * This shifts the given CRC by 8*len bits (i.e. produces the same effect
2396d514b4eSGeorge Spelvin  * as appending len bytes of zero to the data), in time proportional
2406d514b4eSGeorge Spelvin  * to log(len).
2416d514b4eSGeorge Spelvin  */
2426d514b4eSGeorge Spelvin static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
2436d514b4eSGeorge Spelvin 						   u32 polynomial)
2446d514b4eSGeorge Spelvin {
2456d514b4eSGeorge Spelvin 	u32 power = polynomial;	/* CRC of x^32 */
2466d514b4eSGeorge Spelvin 	int i;
2476d514b4eSGeorge Spelvin 
2486d514b4eSGeorge Spelvin 	/* Shift up to 32 bits in the simple linear way */
2496d514b4eSGeorge Spelvin 	for (i = 0; i < 8 * (int)(len & 3); i++)
2506d514b4eSGeorge Spelvin 		crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);
2516d514b4eSGeorge Spelvin 
2526d514b4eSGeorge Spelvin 	len >>= 2;
2536d514b4eSGeorge Spelvin 	if (!len)
2546d514b4eSGeorge Spelvin 		return crc;
2556d514b4eSGeorge Spelvin 
2566d514b4eSGeorge Spelvin 	for (;;) {
2576d514b4eSGeorge Spelvin 		/* "power" is x^(2^i), modulo the polynomial */
2586d514b4eSGeorge Spelvin 		if (len & 1)
2596d514b4eSGeorge Spelvin 			crc = gf2_multiply(crc, power, polynomial);
2606d514b4eSGeorge Spelvin 
2616d514b4eSGeorge Spelvin 		len >>= 1;
2626d514b4eSGeorge Spelvin 		if (!len)
2636d514b4eSGeorge Spelvin 			break;
2646d514b4eSGeorge Spelvin 
2656d514b4eSGeorge Spelvin 		/* Square power, advancing to x^(2^(i+1)) */
2666d514b4eSGeorge Spelvin 		power = gf2_multiply(power, power, polynomial);
2676d514b4eSGeorge Spelvin 	}
2686d514b4eSGeorge Spelvin 
2696d514b4eSGeorge Spelvin 	return crc;
2706d514b4eSGeorge Spelvin }
2716d514b4eSGeorge Spelvin 
2726d514b4eSGeorge Spelvin u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
2736d514b4eSGeorge Spelvin {
274e37f2f93SKrzysztof Kozlowski 	return crc32_generic_shift(crc, len, CRC32_POLY_LE);
2756d514b4eSGeorge Spelvin }
2766d514b4eSGeorge Spelvin 
2776d514b4eSGeorge Spelvin u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
2786d514b4eSGeorge Spelvin {
2796d514b4eSGeorge Spelvin 	return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
2806d514b4eSGeorge Spelvin }
2816d514b4eSGeorge Spelvin EXPORT_SYMBOL(crc32_le_shift);
2826d514b4eSGeorge Spelvin EXPORT_SYMBOL(__crc32c_le_shift);
2831da177e4SLinus Torvalds 
2842f72100cSRandy Dunlap /**
285f2e1d2acSGu Zheng  * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
2862f72100cSRandy Dunlap  * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for
2872f72100cSRandy Dunlap  *	other uses, or the previous crc32 value if computing incrementally.
288f2e1d2acSGu Zheng  * @p: pointer to buffer over which CRC32 is run
2892f72100cSRandy Dunlap  * @len: length of buffer @p
290f2e1d2acSGu Zheng  * @tab: big-endian Ethernet table
291f2e1d2acSGu Zheng  * @polynomial: CRC32 BE polynomial
2922f72100cSRandy Dunlap  */
29346c5801eSDarrick J. Wong static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
29446c5801eSDarrick J. Wong 					  size_t len, const u32 (*tab)[256],
29546c5801eSDarrick J. Wong 					  u32 polynomial)
2961da177e4SLinus Torvalds {
29760e58d5cSBob Pearson #if CRC_BE_BITS == 1
2981da177e4SLinus Torvalds 	int i;
2991da177e4SLinus Torvalds 	while (len--) {
3001da177e4SLinus Torvalds 		crc ^= *p++ << 24;
3011da177e4SLinus Torvalds 		for (i = 0; i < 8; i++)
3021da177e4SLinus Torvalds 			crc =
30346c5801eSDarrick J. Wong 			    (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
3041da177e4SLinus Torvalds 					  0);
3051da177e4SLinus Torvalds 	}
30660e58d5cSBob Pearson # elif CRC_BE_BITS == 2
30760e58d5cSBob Pearson 	while (len--) {
30860e58d5cSBob Pearson 		crc ^= *p++ << 24;
30946c5801eSDarrick J. Wong 		crc = (crc << 2) ^ tab[0][crc >> 30];
31046c5801eSDarrick J. Wong 		crc = (crc << 2) ^ tab[0][crc >> 30];
31146c5801eSDarrick J. Wong 		crc = (crc << 2) ^ tab[0][crc >> 30];
31246c5801eSDarrick J. Wong 		crc = (crc << 2) ^ tab[0][crc >> 30];
3131da177e4SLinus Torvalds 	}
31460e58d5cSBob Pearson # elif CRC_BE_BITS == 4
31560e58d5cSBob Pearson 	while (len--) {
31660e58d5cSBob Pearson 		crc ^= *p++ << 24;
31746c5801eSDarrick J. Wong 		crc = (crc << 4) ^ tab[0][crc >> 28];
31846c5801eSDarrick J. Wong 		crc = (crc << 4) ^ tab[0][crc >> 28];
31960e58d5cSBob Pearson 	}
32060e58d5cSBob Pearson # elif CRC_BE_BITS == 8
3219a1dbf6aSBob Pearson 	while (len--) {
3229a1dbf6aSBob Pearson 		crc ^= *p++ << 24;
32346c5801eSDarrick J. Wong 		crc = (crc << 8) ^ tab[0][crc >> 24];
3249a1dbf6aSBob Pearson 	}
3259a1dbf6aSBob Pearson # else
326ce4320ddSBob Pearson 	crc = (__force u32) __cpu_to_be32(crc);
327ddcaccbcSJoakim Tjernlund 	crc = crc32_body(crc, p, len, tab);
328ce4320ddSBob Pearson 	crc = __be32_to_cpu((__force __be32)crc);
3291da177e4SLinus Torvalds # endif
33060e58d5cSBob Pearson 	return crc;
3311da177e4SLinus Torvalds }
33246c5801eSDarrick J. Wong 
333904542dcSTobias Jordan #if CRC_BE_BITS == 1
334*5cb29be4SKevin Bracey u32 __pure __weak crc32_be(u32 crc, unsigned char const *p, size_t len)
33546c5801eSDarrick J. Wong {
336e37f2f93SKrzysztof Kozlowski 	return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
33746c5801eSDarrick J. Wong }
33846c5801eSDarrick J. Wong #else
339*5cb29be4SKevin Bracey u32 __pure __weak crc32_be(u32 crc, unsigned char const *p, size_t len)
34046c5801eSDarrick J. Wong {
341163a4e7fSKevin Bracey 	return crc32_be_generic(crc, p, len, crc32table_be, CRC32_POLY_BE);
34246c5801eSDarrick J. Wong }
34346c5801eSDarrick J. Wong #endif
3441da177e4SLinus Torvalds EXPORT_SYMBOL(crc32_be);
345