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