[PATCH 2/3] RISC-V: memcpy() Use inline functions instead of macros and gotos

ma.mandourr@gmail.com ma.mandourr@gmail.com
Sun Apr 27 11:58:25 GMT 2025


From: Mahmoud Abumandour <ma.mandourr@gmail.com>

Reviewed-by: Christian Herber <christian.herber@oss.nxp.com>
Signed-off-by: Mahmoud Abumandour <ma.mandourr@gmail.com>
---
 newlib/libc/machine/riscv/memcpy.c | 42 +++++++++++++++++-------------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/newlib/libc/machine/riscv/memcpy.c b/newlib/libc/machine/riscv/memcpy.c
index 7df35dd85..427005efd 100644
--- a/newlib/libc/machine/riscv/memcpy.c
+++ b/newlib/libc/machine/riscv/memcpy.c
@@ -1,4 +1,5 @@
 /* Copyright (c) 2017  SiFive Inc. All rights reserved.
+   Copyright (c) 2025 Mahmoud Abumandour <ma.mandourr@gmail.com>
 
    This copyrighted material is made available to anyone wishing to use,
    modify, copy, or redistribute it subject to the terms and conditions
@@ -21,19 +22,22 @@
 
 #define unlikely(X) __builtin_expect (!!(X), 0)
 
+static inline void
+__libc_memcpy_bytewise (unsigned char *dst, const unsigned char *src,
+                        const size_t sz)
+{
+  const unsigned char *end = dst + sz;
+  while (dst < end)
+    *dst++ = *src++;
+}
+
 void *
 __inhibit_loop_to_libcall
 memcpy (void *__restrict aa, const void *__restrict bb, size_t n)
 {
-  #define BODY(a, b, t) { \
-    t tt = *b; \
-    a++, b++; \
-    *(a - 1) = tt; \
-  }
-
-  char *a = (char *)aa;
-  const char *b = (const char *)bb;
-  char *end = a + n;
+  unsigned char *a = (unsigned char *)aa;
+  const unsigned char *b = (const unsigned char *)bb;
+  unsigned char *end = a + n;
   uintptr_t msk = SZREG - 1;
 #if __riscv_misaligned_slow || __riscv_misaligned_fast
   if (n < SZREG)
@@ -41,16 +45,18 @@ memcpy (void *__restrict aa, const void *__restrict bb, size_t n)
   if (unlikely ((((uintptr_t)a & msk) != ((uintptr_t)b & msk)) || n < SZREG))
 #endif
     {
-    small:
       if (__builtin_expect (a < end, 1))
-        while (a < end)
-          BODY (a, b, char);
+        __libc_memcpy_bytewise (a, b, n);
       return aa;
     }
 
   if (unlikely (((uintptr_t)a & msk) != 0))
-    while ((uintptr_t)a & msk)
-      BODY (a, b, char);
+    {
+      size_t rem = SZREG - ((uintptr_t)a & msk);
+      __libc_memcpy_bytewise (a, b, rem);
+      a += rem;
+      b += rem;
+    }
 
   uintxlen_t *la = (uintxlen_t *)a;
   const uintxlen_t *lb = (const uintxlen_t *)b;
@@ -82,12 +88,12 @@ memcpy (void *__restrict aa, const void *__restrict bb, size_t n)
     }
 
   while (la < lend)
-    BODY (la, lb, uintxlen_t);
+      *la++ = *lb++;
 
-  a = (char *)la;
-  b = (const char *)lb;
+  a = (unsigned char *)la;
+  b = (const unsigned char *)lb;
   if (unlikely (a < end))
-    goto small;
+    __libc_memcpy_bytewise (a, b, end - a);
   return aa;
 }
 #endif
-- 
2.43.0



More information about the Newlib mailing list