New upstream version 18.08
[deb_dpdk.git] / drivers / net / nfp / nfpcore / nfp_crc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Netronome Systems, Inc.
3  * All rights reserved.
4  */
5
6 #include <stdio.h>
7 #include <inttypes.h>
8
9 #include "nfp_crc.h"
10
11 static inline uint32_t
12 nfp_crc32_be_generic(uint32_t crc, unsigned char const *p, size_t len,
13                  uint32_t polynomial)
14 {
15         int i;
16         while (len--) {
17                 crc ^= *p++ << 24;
18                 for (i = 0; i < 8; i++)
19                         crc = (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
20                                           0);
21         }
22         return crc;
23 }
24
25 static inline uint32_t
26 nfp_crc32_be(uint32_t crc, unsigned char const *p, size_t len)
27 {
28         return nfp_crc32_be_generic(crc, p, len, CRCPOLY_BE);
29 }
30
31 static uint32_t
32 nfp_crc32_posix_end(uint32_t crc, size_t total_len)
33 {
34         /* Extend with the length of the string. */
35         while (total_len != 0) {
36                 uint8_t c = total_len & 0xff;
37
38                 crc = nfp_crc32_be(crc, &c, 1);
39                 total_len >>= 8;
40         }
41
42         return ~crc;
43 }
44
45 uint32_t
46 nfp_crc32_posix(const void *buff, size_t len)
47 {
48         return nfp_crc32_posix_end(nfp_crc32_be(0, buff, len), len);
49 }