diff mbox series

[API-NEXT,v2,2/3] linux-gen: chksum: implement ones complement sum

Message ID 1506452407-23070-3-git-send-email-odpbot@yandex.ru
State Superseded
Headers show
Series [API-NEXT,v2,1/3] api: chksum: add ones complement sum function | expand

Commit Message

Github ODP bot Sept. 26, 2017, 7 p.m. UTC
From: Petri Savolainen <petri.savolainen@linaro.org>


Add implementation of 16 bit ones complement sum function. This
is non-performance optimized but simple and based on RFC 1071
and its errata.

Signed-off-by: Petri Savolainen <petri.savolainen@linaro.org>

---
/** Email created from pull request 193 (psavol:next-ip-checksum)
 ** https://github.com/Linaro/odp/pull/193
 ** Patch: https://github.com/Linaro/odp/pull/193.patch
 ** Base sha: e04e5f90df69e3031622b77fb5273b85d47eb966
 ** Merge commit sha: 18d37c68b9d3a13db328316de06eed14113d0fc2
 **/
 platform/linux-generic/Makefile.am  |  1 +
 platform/linux-generic/odp_chksum.c | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 platform/linux-generic/odp_chksum.c
diff mbox series

Patch

diff --git a/platform/linux-generic/Makefile.am b/platform/linux-generic/Makefile.am
index 994bbbeee..1117e3470 100644
--- a/platform/linux-generic/Makefile.am
+++ b/platform/linux-generic/Makefile.am
@@ -236,6 +236,7 @@  __LIB__libodp_linux_la_SOURCES = \
 			   odp_bitmap.c \
 			   odp_buffer.c \
 			   odp_byteorder.c \
+			   odp_chksum.c \
 			   odp_classification.c \
 			   odp_cpu.c \
 			   odp_cpumask.c \
diff --git a/platform/linux-generic/odp_chksum.c b/platform/linux-generic/odp_chksum.c
new file mode 100644
index 000000000..349bdaf46
--- /dev/null
+++ b/platform/linux-generic/odp_chksum.c
@@ -0,0 +1,36 @@ 
+/* Copyright (c) 2017, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:     BSD-3-Clause
+ */
+
+#include <odp/api/chksum.h>
+#include <odp/api/std_types.h>
+
+/* Simple implementation of ones complement sum.
+ * Based on RFC1071 and its errata.
+ */
+uint16_t odp_chksum_ones_comp16(const void *p, uint32_t len)
+{
+	uint32_t sum = 0;
+	const uint16_t *data = p;
+
+	while (len > 1) {
+		sum += *data++;
+		len -= 2;
+	}
+
+	/* Add left-over byte, if any */
+	if (len > 0) {
+		uint16_t left_over;
+
+		*(uint8_t *)&left_over = *(const uint8_t *)data;
+		sum += left_over;
+	}
+
+	/* Fold 32-bit sum to 16 bits */
+	while (sum >> 16)
+		sum = (sum & 0xffff) + (sum >> 16);
+
+	return sum;
+}