aboutsummaryrefslogtreecommitdiff
path: root/newlib/libc/machine/riscv
diff options
context:
space:
mode:
Diffstat (limited to 'newlib/libc/machine/riscv')
-rw-r--r--newlib/libc/machine/riscv/memcpy-asm.S12
-rw-r--r--newlib/libc/machine/riscv/memmove.S28
-rw-r--r--newlib/libc/machine/riscv/memset.S16
-rw-r--r--newlib/libc/machine/riscv/rv_string.h6
-rw-r--r--newlib/libc/machine/riscv/strcmp.S4
5 files changed, 33 insertions, 33 deletions
diff --git a/newlib/libc/machine/riscv/memcpy-asm.S b/newlib/libc/machine/riscv/memcpy-asm.S
index 5571e47..2771285 100644
--- a/newlib/libc/machine/riscv/memcpy-asm.S
+++ b/newlib/libc/machine/riscv/memcpy-asm.S
@@ -14,15 +14,15 @@
.global memcpy
.type memcpy, @function
memcpy:
- mv t1, a0
+ mv a3, a0
beqz a2, 2f
1:
- lb t2, 0(a1)
- sb t2, 0(t1)
- add a2, a2, -1
- add t1, t1, 1
- add a1, a1, 1
+ lbu a4, 0(a1)
+ sb a4, 0(a3)
+ 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 66d9cd4..061472c 100644
--- a/newlib/libc/machine/riscv/memmove.S
+++ b/newlib/libc/machine/riscv/memmove.S
@@ -14,26 +14,26 @@
.global memmove
.type memmove, @function
memmove:
- beqz a2, 2f
+ beqz a2, .Ldone /* in case there are 0 bytes to be copied, return immediately */
- mv t1, a0
+ mv a4, a0 /* copy the destination address over to a4, since memmove should return that address in a0 at the end */
li a3, 1
- bgtu a1, a0, 1f
+ bgtu a1, a0, .Lcopy /* in case of source address > destination address, copy from start to end of the specified memory area */
- li a3, -1
- addi a4, a2 , -1
- add t1, t1, a4
- add a1, a1, a4
+ 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. */
-1:
- lb t2, 0(a1)
- sb t2, 0(t1)
- add a2, a2, -1
- add t1, t1, a3
+.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
- bnez a2, 1b
+.Lcopy:
+ lbu a5, 0(a1)
+ 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
-2:
+.Ldone:
ret
.size memmove, .-memmove
diff --git a/newlib/libc/machine/riscv/memset.S b/newlib/libc/machine/riscv/memset.S
index a717ae7..3d207e7 100644
--- a/newlib/libc/machine/riscv/memset.S
+++ b/newlib/libc/machine/riscv/memset.S
@@ -14,16 +14,16 @@
.type memset, @function
memset:
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
- mv t1, a0
- beqz a2, 2f
+ mv a3, a0
+ beqz a2, .Ldone
-1:
- sb a1, 0(t1)
- add a2, a2, -1
- add t1, t1, 1
- bnez a2, 1b
+.Lset:
+ sb a1, 0(a3)
+ addi a2, a2, -1
+ addi a3, a3, 1
+ bnez a2, .Lset
-2:
+.Ldone:
ret
#else
diff --git a/newlib/libc/machine/riscv/rv_string.h b/newlib/libc/machine/riscv/rv_string.h
index 362f66a..7754303 100644
--- a/newlib/libc/machine/riscv/rv_string.h
+++ b/newlib/libc/machine/riscv/rv_string.h
@@ -82,8 +82,8 @@ static __inline char *__libc_strcpy(char *dst, const char *src, bool ret_start)
if (!(*dst++ = src[0])) return dst0;
if (!(*dst++ = src[1])) return dst0;
if (!(*dst++ = src[2])) return dst0;
- if (!(*dst++ = src[3])) return dst0;
#if __riscv_xlen == 64
+ if (!(*dst++ = src[3])) return dst0;
if (!(*dst++ = src[4])) return dst0;
if (!(*dst++ = src[5])) return dst0;
if (!(*dst++ = src[6])) return dst0;
@@ -94,13 +94,13 @@ static __inline char *__libc_strcpy(char *dst, const char *src, bool ret_start)
if (!(*dst++ = src[0])) return dst - 1;
if (!(*dst++ = src[1])) return dst - 1;
if (!(*dst++ = src[2])) return dst - 1;
- if (!(*dst++ = src[3])) return dst - 1;
#if __riscv_xlen == 64
+ if (!(*dst++ = src[3])) return dst - 1;
if (!(*dst++ = src[4])) return dst - 1;
if (!(*dst++ = src[5])) return dst - 1;
if (!(*dst++ = src[6])) return dst - 1;
- dst0 = dst;
#endif
+ dst0 = dst;
}
*dst = 0;
diff --git a/newlib/libc/machine/riscv/strcmp.S b/newlib/libc/machine/riscv/strcmp.S
index cc29b7b..5d3370f 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