Skip to content

Commit

Permalink
Optimize stpcpy's size and speed
Browse files Browse the repository at this point in the history
  • Loading branch information
colinleroy committed Sep 4, 2024
1 parent 5e5dd1d commit 4917e50
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
7 changes: 0 additions & 7 deletions libsrc/common/stpcpy.c

This file was deleted.

22 changes: 22 additions & 0 deletions libsrc/common/stpcpy.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;
; Colin Leroy-Mira, 4 Sept. 2024
;
; char* stpcpy (char* dest, const char* src);
;

.export _stpcpy
.import _strcpy

.importzp tmp1, ptr2

_stpcpy:
jsr _strcpy

ldx ptr2+1 ; Load dest pointer's last high byte
sty tmp1 ; Store low byte's offset

clc
adc tmp1 ; Add to low byte value
bcc :+
inx
: rts
3 changes: 1 addition & 2 deletions libsrc/common/strcpy.s
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ L1: lda (ptr1),y
inc ptr2+1
bne L1

L9: lda ptr2 ; X still contains high byte
L9: lda ptr2 ; X still contains dest's original high byte
rts

13 changes: 11 additions & 2 deletions test/val/stpcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
#define STR_SHORT "Hello, World!"
#define STR_LONG "This is a longer test string for stpcpy."

char dest[512];
char multi_page[300];

int
main ()
{
char dest[50];
const char *src_empty;
const char *src_short;
const char *src_long;
Expand All @@ -38,7 +40,14 @@ main ()
assert(end == &dest[sizeof (STR_LONG) - 1]);
printf ("Test 3 passed.\n");

memset(multi_page, 'a', sizeof(multi_page)-1);
multi_page[sizeof(multi_page)-1] = '\0';
end = stpcpy (dest, multi_page);
assert(!strcmp (dest, multi_page));
assert(!*end);
assert(end == &dest[sizeof (multi_page) - 1]);
printf ("Test 4 passed.\n");

printf ("All tests passed.\n");
return EXIT_SUCCESS;
}

0 comments on commit 4917e50

Please sign in to comment.