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