Re: [PATCH] newlib: riscv: Remove unnecessary byte load for strlen()

Jeff Johnston <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <CAOox84vkkdJ95orBr25_eth4sUnZtAaVrRUw2Ka1Qu46tYCv-A@mail.gmail.com>
Patch pushed.

-- Jeff J.

On Fri, Apr 25, 2025 at 6:05 PM Eric Salem <[email protected]> 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 <[email protected]>
> Signed-off-by: Eric Salem <[email protected]>
> ---
>  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
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.