CV-Bowen commented on code in PR #21445:
URL: https://github.com/apache/nuttx/pull/21445#discussion_r4476239290


##########
libs/libc/misc/lib_crc16xor.c:
##########
@@ -0,0 +1,89 @@
+/****************************************************************************
+ * libs/libc/misc/lib_crc16xor.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* CRC16 XOR checksum with automatic alignment and loop unrolling.
+ * Uses uint64_t reads with 8x unrolling (64 bytes per iteration).
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/types.h>
+#include <stdint.h>
+
+#include <nuttx/crc16.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: crc16xor
+ *
+ * Description:
+ *   Return a 16-bit XOR checksum. Uses uint64_t reads with automatic
+ *   alignment handling and 8x loop unrolling for maximum throughput.
+ *
+ ****************************************************************************/
+
+uint16_t crc16xor(FAR const uint8_t *src, size_t len)
+{
+  uint64_t sum = 0;
+  FAR const uint64_t *p;
+
+  /* Align to 8-byte boundary */
+
+  while (((uintptr_t)src & 7) && len)
+    {
+      sum ^= *src++;

Review Comment:
   Fixed: renamed to xorsum16(); head and tail bytes now go into the lane of 
their address and the result is swapped for an odd start, so the value only 
depends on the offset from the buffer. 3224 cases over lengths 0..4100 and 
offsets 0..7 match the per-word reference.



##########
drivers/rpmsg/rpmsg_port_uart.c:
##########
@@ -84,8 +84,8 @@
 #define RPMSG_PORT_UART_EVT_TX             (1 << 3) /* TX is ready or not */
 
 #ifdef CONFIG_RPMSG_PORT_UART_CRC
-#  define rpmsg_port_uart_crc16(hdr)       crc16ibm((FAR uint8_t 
*)&(hdr)->cmd, \
-                                                     (hdr)->len - 
sizeof((hdr)->crc))
+#  define rpmsg_port_uart_crc16(hdr)       crc16xor((FAR uint8_t 
*)&(hdr)->cmd, \

Review Comment:
   Measured and added to the body: all two-bit errors in a 512-byte frame, 
crc16ibm misses 0, xorsum16 misses 6.23%; for a 2046-byte frame xorsum16 is 71x 
faster. The check is meant to catch broken framing on a board-level link, so we 
keep only xorsum16 rather than a choice.



##########
include/nuttx/crc16.h:
##########
@@ -248,6 +248,18 @@ uint16_t crc16h8005_part(FAR const uint8_t *src,
 
 uint16_t crc16h8005(FAR const uint8_t *src, size_t len);
 
+/****************************************************************************
+ * Name: crc16xor
+ *
+ * Description:
+ *   Return a 16-bit XOR checksum of the contents of the 'src' buffer,
+ *   length 'len'. Uses automatic alignment handling and loop unrolling
+ *   for maximum performance on small MCUs.
+ *
+ ****************************************************************************/
+
+uint16_t crc16xor(FAR const uint8_t *src, size_t len);

Review Comment:
   Fixed: xorsum16() in nuttx/lib/xorsum.h, nothing left in crc16.h.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to