[newlib-cygwin/main] newlib: riscv: Remove unnecessary byte load/store for stpcpy()/strcpy()

Jeff Johnston jjohnstn@sourceware.org
Tue Apr 15 17:20:52 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=f36442cd598443cde0e1bf82a12cede05ec52c5e

commit f36442cd598443cde0e1bf82a12cede05ec52c5e
Author: Eric Salem <ericsalem@gmail.com>
Date:   Sun Apr 6 15:03:23 2025 -0500

    newlib: riscv: Remove unnecessary byte load/store for stpcpy()/strcpy()
    
    For architectures where XLEN is 32 bits, when detecting a null byte, a
    word is read at a time. Once a null is found in the word, its precise
    location is then determined. Make clear to the compiler that if the
    first three bytes are not null, the last byte must be null, and does not
    need to be read from the source string, since its value is always zero.
    
    Reviewed-by: Christian Herber <christian.herber@oss.nxp.com>
    Signed-off-by: Eric Salem <ericsalem@gmail.com>

Diff:
---
 newlib/libc/machine/riscv/rv_string.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/newlib/libc/machine/riscv/rv_string.h b/newlib/libc/machine/riscv/rv_string.h
index 362f66a02..775430306 100644
--- a/newlib/libc/machine/riscv/rv_string.h
+++ b/newlib/libc/machine/riscv/rv_string.h
@@ -82,8 +82,8 @@ static __inline char *__libc_strcpy(char *dst, const char *src, bool ret_start)
           if (!(*dst++ = src[0])) return dst0;
           if (!(*dst++ = src[1])) return dst0;
           if (!(*dst++ = src[2])) return dst0;
-          if (!(*dst++ = src[3])) return dst0;
           #if __riscv_xlen == 64
+            if (!(*dst++ = src[3])) return dst0;
             if (!(*dst++ = src[4])) return dst0;
             if (!(*dst++ = src[5])) return dst0;
             if (!(*dst++ = src[6])) return dst0;
@@ -94,13 +94,13 @@ static __inline char *__libc_strcpy(char *dst, const char *src, bool ret_start)
           if (!(*dst++ = src[0])) return dst - 1;
           if (!(*dst++ = src[1])) return dst - 1;
           if (!(*dst++ = src[2])) return dst - 1;
-          if (!(*dst++ = src[3])) return dst - 1;
           #if __riscv_xlen == 64
+            if (!(*dst++ = src[3])) return dst - 1;
             if (!(*dst++ = src[4])) return dst - 1;
             if (!(*dst++ = src[5])) return dst - 1;
             if (!(*dst++ = src[6])) return dst - 1;
-            dst0 = dst;
           #endif
+          dst0 = dst;
         }
 
       *dst = 0;


More information about the Newlib-cvs mailing list