Skip to content

Commit 0cda91c

Browse files
committed
Optimized memset and strlen, also added bzero
1 parent 71f9a70 commit 0cda91c

File tree

7 files changed

+80
-20
lines changed

7 files changed

+80
-20
lines changed

src/libc/bzero.src

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _bzero
6+
7+
; void bzero(void* buf, size_t n)
8+
_bzero:
9+
ld hl, 6
10+
add hl, sp
11+
ld bc, (hl) ; size
12+
inc bc
13+
cpd ; dec hl
14+
ret po ; zero size
15+
dec hl
16+
dec hl
17+
ld de, (hl) ; buf
18+
ld hl, $E40000 ; large region of all zeros on the Ti84CE
19+
ldir
20+
ret

src/libc/include/string.h

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ void *memmove(void *dest, const void *src, size_t n)
1414
void *memset(void *s, int c, size_t n)
1515
__attribute__((nonnull(1)));
1616

17+
void bzero(void *s, size_t n)
18+
__attribute__((nonnull(1)));
19+
1720
int memcmp(const void *s1, const void *s2, size_t n)
1821
__attribute__((nonnull(1, 2)));
1922

src/libc/memset.src

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
assume adl=1
22

33
section .text
4+
45
public _memset
6+
57
_memset:
6-
pop iy
7-
pop hl
8-
pop de
9-
pop bc
10-
push bc
11-
push de
12-
push hl
13-
push iy
8+
ld iy, 0
9+
add iy, sp
10+
ld hl, (iy + 3)
11+
ld bc, (iy + 9)
1412
cpi
15-
add hl,bc
13+
add hl, bc
1614
ret c
1715
dec hl
18-
ld (hl),e
16+
ld e, (iy + 6)
17+
ld (hl), e
1918
ret po
2019
push hl
2120
pop de

src/libc/strlen.src

+7-10
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ _strlen := $0000D4
1010
else
1111

1212
_strlen:
13-
pop bc
14-
ex (sp),hl
15-
push bc
16-
xor a,a
17-
ld bc,0
13+
pop iy
14+
ex (sp), hl
15+
xor a, a
16+
ld bc, 0
1817
cpir
19-
or a,a
20-
sbc hl,hl
18+
sbc hl, hl
2119
scf
22-
sbc hl,bc
23-
ret
20+
sbc hl, bc
21+
jp (iy)
2422

2523
end if
26-

src/libcxx/include/cstring

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using ::size_t;
1212
using ::memcpy;
1313
using ::memmove;
1414
using ::memset;
15+
using ::bzero;
1516
using ::memcmp;
1617
using ::memchr;
1718
using ::memrchr;

test/standalone/asprintf_fprintf/src/main.c

+36
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ size_t T_strlen(const char *s)
4949
int T_strcmp(const char *s1, const char *s2)
5050
__attribute__((nonnull(1, 2)));
5151

52+
void T_bzero(void* s, size_t n);
53+
5254
#else
5355

5456
#define T_memcpy memcpy
@@ -59,6 +61,7 @@ int T_strcmp(const char *s1, const char *s2)
5961
#define T_stpcpy stpcpy
6062
#define T_strlen strlen
6163
#define T_strcmp strcmp
64+
#define T_bzero bzero
6265

6366
#endif
6467

@@ -439,6 +442,35 @@ int mempcpy_test(void) {
439442
return 0;
440443
}
441444

445+
int bzero_test(void) {
446+
char truth[32];
447+
char data[32];
448+
T_memset(data, 0x8F, sizeof(data));
449+
T_memset(&truth[ 0], 0x8F, 8);
450+
T_memset(&truth[ 2], 0x00, 1);
451+
T_memset(&truth[ 8], 0x00, 17);
452+
T_memset(&truth[25], 0x8F, 7);
453+
T_bzero(&data[2], 0);
454+
T_bzero(&data[2], 1);
455+
if (T_strlen(&data[0]) != 2) {
456+
return __LINE__;
457+
}
458+
if (T_strlen(&data[1]) != 1) {
459+
return __LINE__;
460+
}
461+
if (T_strlen(&data[2]) != 0) {
462+
return __LINE__;
463+
}
464+
T_bzero(NULL, 0);
465+
T_bzero(&data[8], 17);
466+
int cmp = T_memcmp(data, truth, 32);
467+
if (cmp != 0) {
468+
printf("cmp: %d\n", cmp);
469+
return __LINE__;
470+
}
471+
return 0;
472+
}
473+
442474
int run_tests(void) {
443475
int ret = 0;
444476
/* boot_asprintf */
@@ -464,6 +496,10 @@ int run_tests(void) {
464496
ret = mempcpy_test();
465497
if (ret != 0) { return ret; }
466498

499+
/* bzero */
500+
ret = bzero_test();
501+
if (ret != 0) { return ret; }
502+
467503
return 0;
468504
}
469505

test/standalone/asprintf_fprintf/src/rename.asm

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public _T_memset, _T_memcpy, _T_memcmp, _T_memccpy, _T_mempcpy
66
public _T_strlen, _T_strcmp, _T_stpcpy
7+
public _T_bzero
78

89
_T_memset := _memset
910
_T_memcpy := _memcpy
@@ -15,5 +16,8 @@ _T_strlen := _strlen
1516
_T_strcmp := _strcmp
1617
_T_stpcpy := _stpcpy
1718

19+
_T_bzero := _bzero
20+
1821
extern _memset, _memcpy, _memcmp, _memccpy, _mempcpy
1922
extern _strlen, _strcmp, _stpcpy
23+
extern _bzero

0 commit comments

Comments
 (0)