casaroli commented on code in PR #21445:
URL: https://github.com/apache/nuttx/pull/21445#discussion_r4473332614


##########
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:
   The result depends on the address of the buffer: the head bytes all go into 
the low byte of `sum`, and the words go in by their position in memory. The 
same 2046-byte frame gives 8 different values at address+0 to +7; crc16ibm 
gives one. Peers that hold a frame at different alignments then fail the check: 
qemu-armv8a rpserver and rpproxy over the port UART with 
CONFIG_RPMSG_PORT_UART_CRC=y and a test-only .txlen/.rxlen = 2044 give `Recv 
error` on both sides after a proxy restart; the base with crc16ibm does not.
   
   Fix: XOR each head and tail byte into the lane of its address, and swap the 
result for an odd start, so that every alignment gives the same value.



-- 
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