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

Kito Cheng kito.cheng@gmail.com
Tue Apr 15 13:01:39 GMT 2025


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


More information about the Newlib mailing list