xref: /spdk/lib/util/crc32c.c (revision 94a84ae98590bea46939eb1dcd7a9876bd393b54)
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_CONFIG_ISAL
37 #define SPDK_HAVE_ISAL
38 #include <isa-l/include/crc.h>
39 #elif defined(__aarch64__) && defined(__ARM_FEATURE_CRC32)
40 #define SPDK_HAVE_ARM_CRC
41 #include <arm_acle.h>
42 #elif defined(__x86_64__) && defined(__SSE4_2__)
43 #define SPDK_HAVE_SSE4_2
44 #include <x86intrin.h>
45 #endif
46 
47 #ifdef SPDK_HAVE_ISAL
48 
49 uint32_t
50 spdk_crc32c_update(const void *buf, size_t len, uint32_t crc)
51 {
52 	return crc32_iscsi((unsigned char *)buf, len, crc);
53 }
54 
55 #elif defined(SPDK_HAVE_SSE4_2)
56 
57 uint32_t
58 spdk_crc32c_update(const void *buf, size_t len, uint32_t crc)
59 {
60 	uint64_t crc_tmp64;
61 	size_t count;
62 
63 	/* _mm_crc32_u64() needs a 64-bit intermediate value */
64 	crc_tmp64 = crc;
65 
66 	/* Process as much of the buffer as possible in 64-bit blocks. */
67 	count = len / 8;
68 	while (count--) {
69 		uint64_t block;
70 
71 		/*
72 		 * Use memcpy() to avoid unaligned loads, which are undefined behavior in C.
73 		 * The compiler will optimize out the memcpy() in release builds.
74 		 */
75 		memcpy(&block, buf, sizeof(block));
76 		crc_tmp64 = _mm_crc32_u64(crc_tmp64, block);
77 		buf += sizeof(block);
78 	}
79 	crc = (uint32_t)crc_tmp64;
80 
81 	/* Handle any trailing bytes. */
82 	count = len & 7;
83 	while (count--) {
84 		crc = _mm_crc32_u8(crc, *(const uint8_t *)buf);
85 		buf++;
86 	}
87 
88 	return crc;
89 }
90 
91 #elif defined(SPDK_HAVE_ARM_CRC)
92 
93 uint32_t
94 spdk_crc32c_update(const void *buf, size_t len, uint32_t crc)
95 {
96 	size_t count;
97 
98 	count = len / 8;
99 	while (count--) {
100 		uint64_t block;
101 
102 		memcpy(&block, buf, sizeof(block));
103 		crc = __crc32cd(crc, block);
104 		buf += sizeof(block);
105 	}
106 
107 	count = len & 7;
108 	while (count--) {
109 		crc = __crc32cb(crc, *(const uint8_t *)buf);
110 		buf++;
111 	}
112 
113 	return crc;
114 }
115 
116 #else /* Neither SSE 4.2 nor ARM CRC32 instructions available */
117 
118 static struct spdk_crc32_table g_crc32c_table;
119 
120 __attribute__((constructor)) static void
121 spdk_crc32c_init(void)
122 {
123 	spdk_crc32_table_init(&g_crc32c_table, SPDK_CRC32C_POLYNOMIAL_REFLECT);
124 }
125 
126 uint32_t
127 spdk_crc32c_update(const void *buf, size_t len, uint32_t crc)
128 {
129 	return spdk_crc32_update(&g_crc32c_table, buf, len, crc);
130 }
131 
132 #endif
133