[PATCH 1/3] RISC-V: memcpy() Use uintxlen_t for xlen-sized copy

[email protected]
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
From: Mahmoud Abumandour <[email protected]>

Reviewed-by: Christian Herber <[email protected]>
Signed-off-by: Mahmoud Abumandour <[email protected]>
---
 newlib/libc/machine/riscv/memcpy.c | 71 +++++++++++++++---------------
 1 file changed, 36 insertions(+), 35 deletions(-)

diff --git a/newlib/libc/machine/riscv/memcpy.c b/newlib/libc/machine/riscv/memcpy.c
index e1a34a8c8..7df35dd85 100644
--- a/newlib/libc/machine/riscv/memcpy.c
+++ b/newlib/libc/machine/riscv/memcpy.c
@@ -10,18 +10,20 @@
 */
 
 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
-//memcpy defined in memcpy-asm.S
+// memcpy defined in memcpy-asm.S
 #else
 
-#include <string.h>
-#include <stdint.h>
 #include "../../string/local.h"
+#include "xlenint.h"
+#include <stdint.h>
+#include <string.h>
+#include <sys/asm.h>
 
 #define unlikely(X) __builtin_expect (!!(X), 0)
 
 void *
 __inhibit_loop_to_libcall
-memcpy(void *__restrict aa, const void *__restrict bb, size_t n)
+memcpy (void *__restrict aa, const void *__restrict bb, size_t n)
 {
   #define BODY(a, b, t) { \
     t tt = *b; \
@@ -32,18 +34,17 @@ memcpy(void *__restrict aa, const void *__restrict bb, size_t n)
   char *a = (char *)aa;
   const char *b = (const char *)bb;
   char *end = a + n;
-  uintptr_t msk = sizeof (long) - 1;
+  uintptr_t msk = SZREG - 1;
 #if __riscv_misaligned_slow || __riscv_misaligned_fast
-  if (n < sizeof (long))
+  if (n < SZREG)
 #else
-  if (unlikely ((((uintptr_t)a & msk) != ((uintptr_t)b & msk))
-	       || n < sizeof (long)))
+  if (unlikely ((((uintptr_t)a & msk) != ((uintptr_t)b & msk)) || n < SZREG))
 #endif
     {
-small:
+    small:
       if (__builtin_expect (a < end, 1))
-	while (a < end)
-	  BODY (a, b, char);
+        while (a < end)
+          BODY (a, b, char);
       return aa;
     }
 
@@ -51,37 +52,37 @@ small:
     while ((uintptr_t)a & msk)
       BODY (a, b, char);
 
-  long *la = (long *)a;
-  const long *lb = (const long *)b;
-  long *lend = (long *)((uintptr_t)end & ~msk);
+  uintxlen_t *la = (uintxlen_t *)a;
+  const uintxlen_t *lb = (const uintxlen_t *)b;
+  uintxlen_t *lend = (uintxlen_t *)((uintptr_t)end & ~msk);
 
   if (unlikely (lend - la > 8))
     {
       while (lend - la > 8)
-	{
-	  long b0 = *lb++;
-	  long b1 = *lb++;
-	  long b2 = *lb++;
-	  long b3 = *lb++;
-	  long b4 = *lb++;
-	  long b5 = *lb++;
-	  long b6 = *lb++;
-	  long b7 = *lb++;
-	  long b8 = *lb++;
-	  *la++ = b0;
-	  *la++ = b1;
-	  *la++ = b2;
-	  *la++ = b3;
-	  *la++ = b4;
-	  *la++ = b5;
-	  *la++ = b6;
-	  *la++ = b7;
-	  *la++ = b8;
-	}
+        {
+          uintxlen_t b0 = *lb++;
+          uintxlen_t b1 = *lb++;
+          uintxlen_t b2 = *lb++;
+          uintxlen_t b3 = *lb++;
+          uintxlen_t b4 = *lb++;
+          uintxlen_t b5 = *lb++;
+          uintxlen_t b6 = *lb++;
+          uintxlen_t b7 = *lb++;
+          uintxlen_t b8 = *lb++;
+          *la++ = b0;
+          *la++ = b1;
+          *la++ = b2;
+          *la++ = b3;
+          *la++ = b4;
+          *la++ = b5;
+          *la++ = b6;
+          *la++ = b7;
+          *la++ = b8;
+        }
     }
 
   while (la < lend)
-    BODY (la, lb, long);
+    BODY (la, lb, uintxlen_t);
 
   a = (char *)la;
   b = (const char *)lb;
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.