Skip to content

Commit 55d3a6e

Browse files
committed
Optimize stpcpy's size and speed
1 parent 5e5dd1d commit 55d3a6e

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

libsrc/common/stpcpy.c

-7
This file was deleted.

libsrc/common/stpcpy.s

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;
2+
; Colin Leroy-Mira, 4 Sept. 2024
3+
;
4+
; char* stpcpy (char* dest, const char* src);
5+
;
6+
7+
.export _stpcpy
8+
.import _strcpy
9+
10+
.importzp tmp1, ptr2
11+
12+
_stpcpy:
13+
jsr _strcpy
14+
15+
ldx ptr2+1 ; Load dest pointer's last high byte
16+
tya ; Get the last offset strcpy wrote to
17+
18+
clc
19+
adc ptr2 ; Add to low byte value
20+
bcc :+
21+
inx
22+
: rts ; Return pointer to dest's terminator

libsrc/common/strcpy.s

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ L1: lda (ptr1),y
2525
inc ptr2+1
2626
bne L1
2727

28-
L9: lda ptr2 ; X still contains high byte
29-
rts
28+
L9: lda ptr2 ; X still contains dest's original high byte
3029

30+
; On exit, we want AX to be dest (as this is what strcpy returns).
31+
; We also want (ptr2),y to still point to dest's terminator, as this
32+
; is used by stpcpy().
33+
rts

test/val/stpcpy.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
#define STR_SHORT "Hello, World!"
99
#define STR_LONG "This is a longer test string for stpcpy."
1010

11+
char dest[512];
12+
char multi_page[300];
13+
1114
int
1215
main ()
1316
{
14-
char dest[50];
1517
const char *src_empty;
1618
const char *src_short;
1719
const char *src_long;
@@ -38,7 +40,14 @@ main ()
3840
assert(end == &dest[sizeof (STR_LONG) - 1]);
3941
printf ("Test 3 passed.\n");
4042

43+
memset(multi_page, 'a', sizeof(multi_page)-1);
44+
multi_page[sizeof(multi_page)-1] = '\0';
45+
end = stpcpy (dest, multi_page);
46+
assert(!strcmp (dest, multi_page));
47+
assert(!*end);
48+
assert(end == &dest[sizeof (multi_page) - 1]);
49+
printf ("Test 4 passed.\n");
50+
4151
printf ("All tests passed.\n");
4252
return EXIT_SUCCESS;
4353
}
44-

0 commit comments

Comments
 (0)