[05/12] RISC-V: memmove() size optimized version: Replace lb with lbu

Message ID 20250409064701.3435-6-marlene.fally@gmail.com
State New
Headers
Series newlib: RISC-V: Optimize memory and string functions for code size |

Commit Message

m fally April 9, 2025, 6:46 a.m. UTC
  Replace lb with lbu to avoid unnecessary sign extension.

Reviewed-by: Christian Herber <christian.herber@oss.nxp.com>
Signed-off-by: m fally <marlene.fally@gmail.com>
---
 newlib/libc/machine/riscv/memmove.S | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Kito Cheng April 9, 2025, 7:20 a.m. UTC | #1
LGTM

On Wed, Apr 9, 2025 at 2:58 PM m fally <marlene.fally@gmail.com> wrote:
>
> Replace lb with lbu to avoid unnecessary sign extension.
>
> Reviewed-by: Christian Herber <christian.herber@oss.nxp.com>
> Signed-off-by: m fally <marlene.fally@gmail.com>
> ---
>  newlib/libc/machine/riscv/memmove.S | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/newlib/libc/machine/riscv/memmove.S b/newlib/libc/machine/riscv/memmove.S
> index 36fe00eba..671a6a158 100644
> --- a/newlib/libc/machine/riscv/memmove.S
> +++ b/newlib/libc/machine/riscv/memmove.S
> @@ -20,15 +20,15 @@ memmove:
>    li a3, 1
>    bgtu  a1, a0, .Lcopy         /* in case of source address > destination address, copy from start to end of the specified memory area */
>
> -  li a3, -1                    /* otherwhise, we need to start copying from the end of the specified memory area, therefore after each copied byte, increment the addresses by -1 */
> -  add   a4, a4, a2             /* add the number of bytes to be copied to both addresses. this gives an incorrect address, */
> +  li a3, -1                    /* otherwhise, start copying from the end of the specified memory area in order to prevent data loss in case of overlapping memory areas.*/
> +  add   a4, a4, a2             /* add the number of bytes to be copied to both addresses. this gives us the address one byte past the end of the memory area we want to copy, */
>    add   a1, a1, a2             /* therefore we need to subtract 1 from both addresses in the next step before starting the copying process. */
>
>  .Lincrement:
>    add   a4, a4, a3             /* in case of source address < destination address, increment both addresses by -1 before copying any data to obtain the correct start addresses */
>    add   a1, a1, a3
>  .Lcopy:
> -  lb a5, 0(a1)                 /* copy bytes as long as a2 (= the number of bytes to be copied) > 0 */
> +  lbu a5, 0(a1)                        /* copy bytes as long as a2 (= the number of bytes to be copied) > 0 */
>    sb a5, 0(a4)
>    add   a2, a2, -1
>    bnez a2, .Lincrement
> --
> 2.49.0
>
  

Patch

diff --git a/newlib/libc/machine/riscv/memmove.S b/newlib/libc/machine/riscv/memmove.S
index 36fe00eba..671a6a158 100644
--- a/newlib/libc/machine/riscv/memmove.S
+++ b/newlib/libc/machine/riscv/memmove.S
@@ -20,15 +20,15 @@  memmove:
   li a3, 1
   bgtu  a1, a0, .Lcopy		/* in case of source address > destination address, copy from start to end of the specified memory area */
 
-  li a3, -1			/* otherwhise, we need to start copying from the end of the specified memory area, therefore after each copied byte, increment the addresses by -1 */
-  add   a4, a4, a2		/* add the number of bytes to be copied to both addresses. this gives an incorrect address, */
+  li a3, -1			/* otherwhise, start copying from the end of the specified memory area in order to prevent data loss in case of overlapping memory areas.*/
+  add   a4, a4, a2		/* add the number of bytes to be copied to both addresses. this gives us the address one byte past the end of the memory area we want to copy, */
   add   a1, a1, a2		/* therefore we need to subtract 1 from both addresses in the next step before starting the copying process. */
 
 .Lincrement:
   add   a4, a4, a3 		/* in case of source address < destination address, increment both addresses by -1 before copying any data to obtain the correct start addresses */
   add   a1, a1, a3
 .Lcopy:
-  lb a5, 0(a1)			/* copy bytes as long as a2 (= the number of bytes to be copied) > 0 */
+  lbu a5, 0(a1)			/* copy bytes as long as a2 (= the number of bytes to be copied) > 0 */
   sb a5, 0(a4)
   add   a2, a2, -1
   bnez a2, .Lincrement