xref: /spdk/lib/util/crc32c.c (revision bbcb35f58b3a9e78b7b69adf4179e2cbd7beb7b3)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/crc32.h"
35 
36 #ifdef SPDK_HAVE_SSE4_2
37 
38 uint32_t
39 spdk_crc32c_update(const void *buf, size_t len, uint32_t crc)
40 {
41 	uint64_t crc_tmp64;
42 	size_t count;
43 
44 	/* _mm_crc32_u64() needs a 64-bit intermediate value */
45 	crc_tmp64 = crc;
46 
47 	/* Process as much of the buffer as possible in 64-bit blocks. */
48 	count = len / 8;
49 	while (count--) {
50 		uint64_t block;
51 
52 		/*
53 		 * Use memcpy() to avoid unaligned loads, which are undefined behavior in C.
54 		 * The compiler will optimize out the memcpy() in release builds.
55 		 */
56 		memcpy(&block, buf, sizeof(block));
57 		crc_tmp64 = _mm_crc32_u64(crc_tmp64, block);
58 		buf += sizeof(block);
59 	}
60 	crc = (uint32_t)crc_tmp64;
61 
62 	/* Handle any trailing bytes. */
63 	count = len & 7;
64 	while (count--) {
65 		crc = _mm_crc32_u8(crc, *(const uint8_t *)buf);
66 		buf++;
67 	}
68 
69 	return crc;
70 }
71 
72 #elif defined(SPDK_HAVE_ARM_CRC)
73 
74 uint32_t
75 spdk_crc32c_update(const void *buf, size_t len, uint32_t crc)
76 {
77 	size_t count;
78 
79 	count = len / 8;
80 	while (count--) {
81 		uint64_t block;
82 
83 		memcpy(&block, buf, sizeof(block));
84 		crc = __crc32cd(crc, block);
85 		buf += sizeof(block);
86 	}
87 
88 	count = len & 7;
89 	while (count--) {
90 		crc = __crc32cb(crc, *(const uint8_t *)buf);
91 		buf++;
92 	}
93 
94 	return crc;
95 }
96 
97 #else /* Neither SSE 4.2 nor ARM CRC32 instructions available */
98 
99 static struct spdk_crc32_table g_crc32c_table;
100 
101 __attribute__((constructor)) static void
102 spdk_crc32c_init(void)
103 {
104 	spdk_crc32_table_init(&g_crc32c_table, SPDK_CRC32C_POLYNOMIAL_REFLECT);
105 }
106 
107 uint32_t
108 spdk_crc32c_update(const void *buf, size_t len, uint32_t crc)
109 {
110 	return spdk_crc32_update(&g_crc32c_table, buf, len, crc);
111 }
112 
113 #endif
114