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