xref: /spdk/lib/util/crc32.c (revision ba20950a539d0b71a20f8a1199cbf759de92e854)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2017 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "util_internal.h"
7 #include "crc_internal.h"
8 #include "spdk/crc32.h"
9 
10 void
11 crc32_table_init(struct spdk_crc32_table *table, uint32_t polynomial_reflect)
12 {
13 	int i, j;
14 	uint32_t val;
15 
16 	for (i = 0; i < 256; i++) {
17 		val = i;
18 		for (j = 0; j < 8; j++) {
19 			if (val & 1) {
20 				val = (val >> 1) ^ polynomial_reflect;
21 			} else {
22 				val = (val >> 1);
23 			}
24 		}
25 		table->table[i] = val;
26 	}
27 }
28 
29 #ifdef SPDK_HAVE_ARM_CRC
30 
31 uint32_t
32 crc32_update(const struct spdk_crc32_table *table, const void *buf, size_t len, uint32_t crc)
33 {
34 	size_t count;
35 	const uint64_t *dword_buf;
36 
37 	count = len & 7;
38 	while (count--) {
39 		crc = __crc32b(crc, *(const uint8_t *)buf);
40 		buf++;
41 	}
42 	dword_buf = (const uint64_t *)buf;
43 
44 	count = len / 8;
45 	while (count--) {
46 		crc = __crc32d(crc, *dword_buf);
47 		dword_buf++;
48 	}
49 
50 	return crc;
51 }
52 
53 #else
54 
55 uint32_t
56 crc32_update(const struct spdk_crc32_table *table, const void *buf, size_t len, uint32_t crc)
57 {
58 	const uint8_t *buf_u8 = buf;
59 	size_t i;
60 
61 	for (i = 0; i < len; i++) {
62 		crc = (crc >> 8) ^ table->table[(crc ^ buf_u8[i]) & 0xff];
63 	}
64 
65 	return crc;
66 }
67 
68 #endif
69