xref: /freebsd/sys/sys/gsb_crc32.h (revision 95ee2897)
1 /*-
2  *  COPYRIGHT (C) 1986 Gary S. Brown.  You may use this program, or
3  *  code or tables extracted from it, as desired without restriction.
4  */
5 
6 #ifndef _SYS_GSB_CRC32_H_
7 #define _SYS_GSB_CRC32_H_
8 
9 #include <sys/types.h>
10 
11 #ifdef _KERNEL
12 
13 extern const uint32_t crc32_tab[];
14 
15 static __inline uint32_t
crc32_raw(const void * buf,size_t size,uint32_t crc)16 crc32_raw(const void *buf, size_t size, uint32_t crc)
17 {
18 	const uint8_t *p = (const uint8_t *)buf;
19 
20 	while (size--)
21 		crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
22 	return (crc);
23 }
24 
25 static __inline uint32_t
crc32(const void * buf,size_t size)26 crc32(const void *buf, size_t size)
27 {
28 	uint32_t crc;
29 
30 	crc = crc32_raw(buf, size, ~0U);
31 	return (crc ^ ~0U);
32 }
33 #endif
34 
35 uint32_t calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
36     unsigned int length);
37 
38 #if defined(__amd64__) || defined(__i386__)
39 uint32_t sse42_crc32c(uint32_t, const unsigned char *, unsigned);
40 #endif
41 #if defined(__aarch64__)
42 uint32_t armv8_crc32c(uint32_t, const unsigned char *, unsigned int);
43 #endif
44 
45 #ifdef TESTING
46 uint32_t singletable_crc32c(uint32_t, const void *, size_t);
47 uint32_t multitable_crc32c(uint32_t, const void *, size_t);
48 #endif
49 
50 #endif /* !_SYS_GSB_CRC32_H_ */
51