[12/12] RISC-V: Size optimized versions: Replace add with addi

Message ID 20250409064701.3435-13-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:47 a.m. UTC
  Replace add instructions with addi where applicable in
the size optimized versions of memmove(), memset(), memcpy(),
and strcmp(). This change does not affect the functions themselves
and is only done to improve syntactic accuracy.

Reviewed-by: Christian Herber <christian.herber@oss.nxp.com>
Signed-off-by: m fally <marlene.fally@gmail.com>
---
 newlib/libc/machine/riscv/memcpy-asm.S | 6 +++---
 newlib/libc/machine/riscv/memmove.S    | 2 +-
 newlib/libc/machine/riscv/memset.S     | 4 ++--
 newlib/libc/machine/riscv/strcmp.S     | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
  

Comments

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

On Wed, Apr 9, 2025 at 2:56 PM m fally <marlene.fally@gmail.com> wrote:
>
> Replace add instructions with addi where applicable in
> the size optimized versions of memmove(), memset(), memcpy(),
> and strcmp(). This change does not affect the functions themselves
> and is only done to improve syntactic accuracy.
>
> Reviewed-by: Christian Herber <christian.herber@oss.nxp.com>
> Signed-off-by: m fally <marlene.fally@gmail.com>
> ---
>  newlib/libc/machine/riscv/memcpy-asm.S | 6 +++---
>  newlib/libc/machine/riscv/memmove.S    | 2 +-
>  newlib/libc/machine/riscv/memset.S     | 4 ++--
>  newlib/libc/machine/riscv/strcmp.S     | 4 ++--
>  4 files changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/newlib/libc/machine/riscv/memcpy-asm.S b/newlib/libc/machine/riscv/memcpy-asm.S
> index e9fe381d9..2771285f9 100644
> --- a/newlib/libc/machine/riscv/memcpy-asm.S
> +++ b/newlib/libc/machine/riscv/memcpy-asm.S
> @@ -20,9 +20,9 @@ memcpy:
>  1:
>    lbu a4, 0(a1)
>    sb a4, 0(a3)
> -  add   a2, a2, -1
> -  add   a3, a3, 1
> -  add   a1, a1, 1
> +  addi   a2, a2, -1
> +  addi   a3, a3, 1
> +  addi   a1, a1, 1
>    bnez a2, 1b
>
>  2:
> diff --git a/newlib/libc/machine/riscv/memmove.S b/newlib/libc/machine/riscv/memmove.S
> index 6ecad9ad6..061472ca2 100644
> --- a/newlib/libc/machine/riscv/memmove.S
> +++ b/newlib/libc/machine/riscv/memmove.S
> @@ -29,7 +29,7 @@ memmove:
>    add   a1, a1, a3
>  .Lcopy:
>    lbu a5, 0(a1)
> -  add   a2, a2, -1             /* copy bytes as long as a2 (= the number of bytes to be copied) > 0. the increment is done here to relax the RAW dependency between load and store */
> +  addi   a2, a2, -1            /* copy bytes as long as a2 (= the number of bytes to be copied) > 0. the increment is done here to relax the RAW dependency between load and store */
>    sb a5, 0(a4)
>    bnez a2, .Lincrement
>
> diff --git a/newlib/libc/machine/riscv/memset.S b/newlib/libc/machine/riscv/memset.S
> index 943835dc5..3d207e7eb 100644
> --- a/newlib/libc/machine/riscv/memset.S
> +++ b/newlib/libc/machine/riscv/memset.S
> @@ -19,8 +19,8 @@ memset:
>
>  .Lset:
>    sb a1, 0(a3)
> -  add   a2, a2, -1
> -  add   a3, a3, 1
> +  addi   a2, a2, -1
> +  addi   a3, a3, 1
>    bnez a2, .Lset
>
>  .Ldone:
> diff --git a/newlib/libc/machine/riscv/strcmp.S b/newlib/libc/machine/riscv/strcmp.S
> index b49569db5..2b2447df5 100644
> --- a/newlib/libc/machine/riscv/strcmp.S
> +++ b/newlib/libc/machine/riscv/strcmp.S
> @@ -19,8 +19,8 @@ strcmp:
>  1:
>    lbu   a2, 0(a0)
>    lbu   a3, 0(a1)
> -  add   a0, a0, 1
> -  add   a1, a1, 1
> +  addi   a0, a0, 1
> +  addi   a1, a1, 1
>    bne   a2, a3, 2f
>    bnez  a2, 1b
>
> --
> 2.49.0
>
  

Patch

diff --git a/newlib/libc/machine/riscv/memcpy-asm.S b/newlib/libc/machine/riscv/memcpy-asm.S
index e9fe381d9..2771285f9 100644
--- a/newlib/libc/machine/riscv/memcpy-asm.S
+++ b/newlib/libc/machine/riscv/memcpy-asm.S
@@ -20,9 +20,9 @@  memcpy:
 1:
   lbu a4, 0(a1)
   sb a4, 0(a3)
-  add   a2, a2, -1
-  add   a3, a3, 1
-  add   a1, a1, 1
+  addi   a2, a2, -1
+  addi   a3, a3, 1
+  addi   a1, a1, 1
   bnez a2, 1b
 
 2:
diff --git a/newlib/libc/machine/riscv/memmove.S b/newlib/libc/machine/riscv/memmove.S
index 6ecad9ad6..061472ca2 100644
--- a/newlib/libc/machine/riscv/memmove.S
+++ b/newlib/libc/machine/riscv/memmove.S
@@ -29,7 +29,7 @@  memmove:
   add   a1, a1, a3
 .Lcopy:
   lbu a5, 0(a1)
-  add   a2, a2, -1		/* copy bytes as long as a2 (= the number of bytes to be copied) > 0. the increment is done here to relax the RAW dependency between load and store */
+  addi   a2, a2, -1		/* copy bytes as long as a2 (= the number of bytes to be copied) > 0. the increment is done here to relax the RAW dependency between load and store */
   sb a5, 0(a4)
   bnez a2, .Lincrement
 
diff --git a/newlib/libc/machine/riscv/memset.S b/newlib/libc/machine/riscv/memset.S
index 943835dc5..3d207e7eb 100644
--- a/newlib/libc/machine/riscv/memset.S
+++ b/newlib/libc/machine/riscv/memset.S
@@ -19,8 +19,8 @@  memset:
 
 .Lset:
   sb a1, 0(a3)
-  add   a2, a2, -1
-  add   a3, a3, 1
+  addi   a2, a2, -1
+  addi   a3, a3, 1
   bnez a2, .Lset
 
 .Ldone:
diff --git a/newlib/libc/machine/riscv/strcmp.S b/newlib/libc/machine/riscv/strcmp.S
index b49569db5..2b2447df5 100644
--- a/newlib/libc/machine/riscv/strcmp.S
+++ b/newlib/libc/machine/riscv/strcmp.S
@@ -19,8 +19,8 @@  strcmp:
 1:
   lbu   a2, 0(a0)
   lbu   a3, 0(a1)
-  add   a0, a0, 1
-  add   a1, a1, 1
+  addi   a0, a0, 1
+  addi   a1, a1, 1
   bne   a2, a3, 2f
   bnez  a2, 1b