newlib: riscv: Remove unnecessary byte load/store for stpcpy()/strcpy()

Message ID 20250406200323.52380-1-ericsalem@gmail.com
State New
Headers
Series newlib: riscv: Remove unnecessary byte load/store for stpcpy()/strcpy() |

Commit Message

Eric Salem April 6, 2025, 8:03 p.m. UTC
  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>
---
 newlib/libc/machine/riscv/rv_string.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Jeff Johnston April 14, 2025, 10:19 p.m. UTC | #1
Kito, this patch seemed to slip through the cracks.  Could you please
review?

Thanks

-- Jeff J.

On Sun, Apr 6, 2025 at 4:04 PM Eric Salem <ericsalem@gmail.com> wrote:

> 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>
> ---
>  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 362f66a024bf..7754303064c9 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;
> --
> 2.49.0
>
>
  
Kito Cheng April 15, 2025, 1:01 p.m. UTC | #2
This patch LGTM, thanks :)

On Tue, Apr 15, 2025 at 6:19 AM Jeff Johnston <jjohnstn@redhat.com> wrote:
>
> Kito, this patch seemed to slip through the cracks.  Could you please review?
>
> Thanks
>
> -- Jeff J.
>
> On Sun, Apr 6, 2025 at 4:04 PM Eric Salem <ericsalem@gmail.com> wrote:
>>
>> 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>
>> ---
>>  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 362f66a024bf..7754303064c9 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;
>> --
>> 2.49.0
>>
  
Jeff Johnston April 15, 2025, 5:21 p.m. UTC | #3
Thanks.  Patch pushed.

-- Jeff J.

On Tue, Apr 15, 2025 at 9:02 AM Kito Cheng <kito.cheng@gmail.com> wrote:

> This patch LGTM, thanks :)
>
> On Tue, Apr 15, 2025 at 6:19 AM Jeff Johnston <jjohnstn@redhat.com> wrote:
> >
> > Kito, this patch seemed to slip through the cracks.  Could you please
> review?
> >
> > Thanks
> >
> > -- Jeff J.
> >
> > On Sun, Apr 6, 2025 at 4:04 PM Eric Salem <ericsalem@gmail.com> wrote:
> >>
> >> 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>
> >> ---
> >>  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 362f66a024bf..7754303064c9 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;
> >> --
> >> 2.49.0
> >>
>
>
  

Patch

diff --git a/newlib/libc/machine/riscv/rv_string.h b/newlib/libc/machine/riscv/rv_string.h
index 362f66a024bf..7754303064c9 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;