RISC-V: strcpy size optimized: do not compile unreacheble code

Message ID 09b3c02087c1bdaa27c9caa103be363cd2ce2af4.camel@espressif.com
State New
Headers
Series RISC-V: strcpy size optimized: do not compile unreacheble code |

Commit Message

Alexey Lapshin Aug. 25, 2025, 12:20 p.m. UTC
  If misaligned access is allowed on the target machine, there is no need
to compile the byte-to-byte copy loop at the end of the strcpy function.
All bytes are already in place due to the earlier logic.

Signed-off-by: Konstantin Kondrashov <konstantin@espressif.com>
---
 newlib/libc/machine/riscv/rv_string.h | 3 +++
 1 file changed, 3 insertions(+)

-- 
2.43.0
  

Comments

Jeff Johnston Aug. 26, 2025, 3:11 p.m. UTC | #1
Hi Alexey,

Could you please reformat the condition to avoid double negatives?

-- Jeff J.

On Mon, Aug 25, 2025 at 8:20 AM Alexey Lapshin <alexey.lapshin@espressif.com>
wrote:

> If misaligned access is allowed on the target machine, there is no need
> to compile the byte-to-byte copy loop at the end of the strcpy function.
> All bytes are already in place due to the earlier logic.
>
> Signed-off-by: Konstantin Kondrashov <konstantin@espressif.com>
> ---
>  newlib/libc/machine/riscv/rv_string.h | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/newlib/libc/machine/riscv/rv_string.h
> b/newlib/libc/machine/riscv/rv_string.h
> index dc2a26daf..9f5eb4099 100644
> --- a/newlib/libc/machine/riscv/rv_string.h
> +++ b/newlib/libc/machine/riscv/rv_string.h
> @@ -112,6 +112,8 @@ static __inline char *__libc_strcpy(char *dst, const
> char *src, bool ret_start)
>      }
>  #endif /* not PREFER_SIZE_OVER_SPEED */
>
> +#if !(!defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) &&
> +      !(__riscv_misaligned_slow || __riscv_misaligned_fast))
>    char ch;
>    do
>      {
> @@ -122,6 +124,7 @@ static __inline char *__libc_strcpy(char *dst, const
> char *src, bool ret_start)
>      } while (ch);
>
>    return ret_start ? dst0 : dst - 1;
> +#endif
>  }
>
>
> --
> 2.43.0
>
>
  

Patch

diff --git a/newlib/libc/machine/riscv/rv_string.h b/newlib/libc/machine/riscv/rv_string.h
index dc2a26daf..9f5eb4099 100644
--- a/newlib/libc/machine/riscv/rv_string.h
+++ b/newlib/libc/machine/riscv/rv_string.h
@@ -112,6 +112,8 @@  static __inline char *__libc_strcpy(char *dst, const char *src, bool ret_start)
     }
 #endif /* not PREFER_SIZE_OVER_SPEED */
 
+#if !(!defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) &&
+      !(__riscv_misaligned_slow || __riscv_misaligned_fast))
   char ch;
   do
     {
@@ -122,6 +124,7 @@  static __inline char *__libc_strcpy(char *dst, const char *src, bool ret_start)
     } while (ch);
 
   return ret_start ? dst0 : dst - 1;
+#endif
 }