[PATCH] newlib: riscv: Remove unnecessary byte load for strlen()
Jeff Johnston
jjohnstn@redhat.com
Fri May 2 21:07:19 GMT 2025
Patch pushed.
-- Jeff J.
On Fri, Apr 25, 2025 at 6:05 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 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/strlen.c | 14 +++++++-------
> 1 file changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/newlib/libc/machine/riscv/strlen.c
> b/newlib/libc/machine/riscv/strlen.c
> index 9bfd2a136753..9f1be1b0e70e 100644
> --- a/newlib/libc/machine/riscv/strlen.c
> +++ b/newlib/libc/machine/riscv/strlen.c
> @@ -47,16 +47,16 @@ size_t strlen(const char *str)
> return ret + (psval >> 3) - sp;
> #else
> char c0 = str[0 - sp], c1 = str[1 - sp], c2 = str[2 - sp], c3 = str[3
> - sp];
> - if (c0 == 0) return ret + 0 - sp;
> - if (c1 == 0) return ret + 1 - sp;
> - if (c2 == 0) return ret + 2 - sp;
> - if (c3 == 0) return ret + 3 - sp;
> + if (c0 == 0) return ret + 0 - sp;
> + if (c1 == 0) return ret + 1 - sp;
> + if (c2 == 0) return ret + 2 - sp;
> + if (__riscv_xlen == 32 || c3 == 0) return ret + 3 - sp;
>
> #if __riscv_xlen == 64
> c0 = str[4 - sp], c1 = str[5 - sp], c2 = str[6 - sp];
> - if (c0 == 0) return ret + 4 - sp;
> - if (c1 == 0) return ret + 5 - sp;
> - if (c2 == 0) return ret + 6 - sp;
> + if (c0 == 0) return ret + 4 - sp;
> + if (c1 == 0) return ret + 5 - sp;
> + if (c2 == 0) return ret + 6 - sp;
> #endif
>
> return ret + 7 - sp;
> --
> 2.49.0
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/newlib/attachments/20250502/39b4ce5b/attachment.htm>
More information about the Newlib
mailing list