Skip to content

Commit 71f9a70

Browse files
committed
Optimized memcpy and mempcpy
1 parent fcd61c8 commit 71f9a70

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

src/libc/memcpy.src

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
assume adl=1
22

33
section .text
4+
45
public _memcpy
56

67
if PREFER_OS_LIBC
@@ -10,19 +11,17 @@ _memcpy := $0000A4
1011
else
1112

1213
_memcpy:
13-
ld iy,0
14-
add iy,sp
15-
ld bc,(iy + 9) ; Load count
16-
sbc hl,hl
17-
sbc hl,bc
18-
jr z,.zero
19-
ld de,(iy + 3) ; Load destination
20-
ld hl,(iy + 6) ; Load source
14+
ld iy, -1
15+
add iy, sp
16+
ld bc, (iy + 10) ; Load count
17+
sbc hl, hl
18+
add hl, bc
19+
jr nc, .zero
20+
ld de, (iy + 4) ; Load destination
21+
ld hl, (iy + 7) ; Load source
2122
ldir
2223
.zero:
23-
ld hl,(iy + 3) ; Return the destination pointer
24+
ld hl, (iy + 4) ; Return the destination pointer
2425
ret
2526

2627
end if
27-
28-

src/libc/mempcpy.src

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ if 0
88

99
; faster when count is zero
1010
_mempcpy:
11-
ld iy, 0
11+
ld iy, -1
1212
add iy, sp
13-
ld bc, (iy + 9) ; Load count
13+
ld bc, (iy + 10) ; Load count
1414
sbc hl, hl
15-
sbc hl, bc
16-
ld hl, (iy + 3) ; Load destination
17-
ret z ; zero bytes to copy
18-
ld de, (iy + 6) ; Load source
15+
add hl, bc
16+
ld hl, (iy + 4) ; Load destination
17+
ret nc ; zero bytes to copy
18+
ld de, (iy + 7) ; Load source
1919
ex de, hl
2020
ldir
2121
ex de, hl
@@ -25,14 +25,14 @@ else
2525

2626
; faster in full execution case by 0F + 1 clock cycles
2727
_mempcpy:
28-
ld iy, 0
28+
ld iy, -1
2929
add iy, sp
30-
ld bc, (iy + 9) ; Load count
30+
ld bc, (iy + 10) ; Load count
3131
sbc hl, hl
32-
sbc hl, bc
33-
ld de, (iy + 3) ; Load destination
34-
jr z, .zero_byte_copy ; zero bytes to copy
35-
ld hl, (iy + 6) ; Load source
32+
add hl, bc
33+
ld de, (iy + 4) ; Load destination
34+
jr nc, .zero_byte_copy ; zero bytes to copy
35+
ld hl, (iy + 7) ; Load source
3636
ldir
3737
.zero_byte_copy:
3838
ex de, hl

0 commit comments

Comments
 (0)