diff options
Diffstat (limited to 'drivers/net/nfp/nfpcore/nfp_crc.c')
-rw-r--r-- | drivers/net/nfp/nfpcore/nfp_crc.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/drivers/net/nfp/nfpcore/nfp_crc.c b/drivers/net/nfp/nfpcore/nfp_crc.c new file mode 100644 index 00000000..20431bf8 --- /dev/null +++ b/drivers/net/nfp/nfpcore/nfp_crc.c @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2018 Netronome Systems, Inc. + * All rights reserved. + */ + +#include <stdio.h> +#include <inttypes.h> + +#include "nfp_crc.h" + +static inline uint32_t +nfp_crc32_be_generic(uint32_t crc, unsigned char const *p, size_t len, + uint32_t polynomial) +{ + int i; + while (len--) { + crc ^= *p++ << 24; + for (i = 0; i < 8; i++) + crc = (crc << 1) ^ ((crc & 0x80000000) ? polynomial : + 0); + } + return crc; +} + +static inline uint32_t +nfp_crc32_be(uint32_t crc, unsigned char const *p, size_t len) +{ + return nfp_crc32_be_generic(crc, p, len, CRCPOLY_BE); +} + +static uint32_t +nfp_crc32_posix_end(uint32_t crc, size_t total_len) +{ + /* Extend with the length of the string. */ + while (total_len != 0) { + uint8_t c = total_len & 0xff; + + crc = nfp_crc32_be(crc, &c, 1); + total_len >>= 8; + } + + return ~crc; +} + +uint32_t +nfp_crc32_posix(const void *buff, size_t len) +{ + return nfp_crc32_posix_end(nfp_crc32_be(0, buff, len), len); +} |