2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2022-11-01 20:26:26 +00:00
|
|
|
* Copyright (C) 2017 Intel Corporation.
|
2016-08-02 16:34:45 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2020-05-13 18:40:57 +00:00
|
|
|
#include "util_internal.h"
|
2023-02-03 07:48:18 +00:00
|
|
|
#include "crc_internal.h"
|
2017-07-21 21:44:43 +00:00
|
|
|
#include "spdk/crc32.h"
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-07-21 21:44:43 +00:00
|
|
|
void
|
2020-05-13 18:40:57 +00:00
|
|
|
crc32_table_init(struct spdk_crc32_table *table, uint32_t polynomial_reflect)
|
2017-07-21 21:44:43 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
uint32_t val;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-07-21 21:44:43 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
val = i;
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (val & 1) {
|
|
|
|
val = (val >> 1) ^ polynomial_reflect;
|
|
|
|
} else {
|
|
|
|
val = (val >> 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
table->table[i] = val;
|
|
|
|
}
|
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2019-01-17 02:04:31 +00:00
|
|
|
#ifdef SPDK_HAVE_ARM_CRC
|
|
|
|
|
|
|
|
uint32_t
|
2020-05-13 18:40:57 +00:00
|
|
|
crc32_update(const struct spdk_crc32_table *table, const void *buf, size_t len, uint32_t crc)
|
2019-01-17 02:04:31 +00:00
|
|
|
{
|
2023-02-15 04:22:06 +00:00
|
|
|
size_t count_pre, count_post, count_mid;
|
2019-01-17 02:04:31 +00:00
|
|
|
const uint64_t *dword_buf;
|
|
|
|
|
2023-02-15 04:22:06 +00:00
|
|
|
/* process the head and tail bytes seperately to make the buf address
|
|
|
|
* passed to crc32_d is 8 byte aligned. This can avoid unaligned loads.
|
|
|
|
*/
|
|
|
|
count_pre = ((uint64_t)buf & 7) == 0 ? 0 : 8 - ((uint64_t)buf & 7);
|
|
|
|
count_post = (uint64_t)(buf + len) & 7;
|
|
|
|
count_mid = (len - count_pre - count_post) / 8;
|
|
|
|
|
|
|
|
while (count_pre--) {
|
2019-01-17 02:04:31 +00:00
|
|
|
crc = __crc32b(crc, *(const uint8_t *)buf);
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
|
2023-02-15 04:22:06 +00:00
|
|
|
dword_buf = (const uint64_t *)buf;
|
|
|
|
while (count_mid--) {
|
2019-01-17 02:04:31 +00:00
|
|
|
crc = __crc32d(crc, *dword_buf);
|
|
|
|
dword_buf++;
|
|
|
|
}
|
|
|
|
|
2023-02-15 04:22:06 +00:00
|
|
|
buf = dword_buf;
|
|
|
|
while (count_post--) {
|
|
|
|
crc = __crc32b(crc, *(const uint8_t *)buf);
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
|
2019-01-17 02:04:31 +00:00
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-07-21 21:44:43 +00:00
|
|
|
uint32_t
|
2020-05-13 18:40:57 +00:00
|
|
|
crc32_update(const struct spdk_crc32_table *table, const void *buf, size_t len, uint32_t crc)
|
2017-07-21 21:44:43 +00:00
|
|
|
{
|
|
|
|
const uint8_t *buf_u8 = buf;
|
|
|
|
size_t i;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-07-21 21:44:43 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
crc = (crc >> 8) ^ table->table[(crc ^ buf_u8[i]) & 0xff];
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
}
|
2019-01-17 02:04:31 +00:00
|
|
|
|
|
|
|
#endif
|