Skip to content

Commit afa896c

Browse files
committed
Aggressively optimize strerror for size
- Size: 744 -> 500 bytes. - Worst-case speed: 0.1 -> 3.2 ms.
1 parent 4fb8622 commit afa896c

3 files changed

Lines changed: 237 additions & 208 deletions

File tree

src/libc/errno_str.src

Lines changed: 0 additions & 208 deletions
This file was deleted.

src/libc/perror.src

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
.assume adl=1
2+
3+
;-------------------------------------------------------------------------------
4+
5+
.section .text
6+
.global _perror
7+
.type _perror, @function
8+
9+
; void perror(const char*)
10+
_perror:
11+
pop de
12+
ex (sp), hl
13+
push de
14+
add hl, bc
15+
xor a, a
16+
sbc hl, bc
17+
jr z, .L.null_string
18+
cp a, (hl)
19+
jr z, .L.empty_string
20+
; fprintf(stderr, "%s: ", str)
21+
call .L.errchar_puts
22+
ld hl, _perror_colon_space_str
23+
call .L.errchar_puts
24+
25+
.L.null_string:
26+
.L.empty_string:
27+
ld hl, (_errno)
28+
call _strerror.hijack_hl
29+
call .L.errchar_puts
30+
ld a, $0A ; '\n'
31+
.L.errchar_putchar:
32+
; input:
33+
; - A = character to display
34+
; - HL = nul terminated string to display afterward
35+
; output:
36+
; - HL points to nul terminator
37+
push hl
38+
ld l, a
39+
push hl
40+
call _errchar
41+
pop hl
42+
pop hl
43+
.L.errchar_puts:
44+
ld a, (hl)
45+
or a, a
46+
ret z
47+
inc hl
48+
jr .L.errchar_putchar
49+
50+
.extern _errno
51+
.extern _errchar
52+
53+
;-------------------------------------------------------------------------------
54+
55+
.section .rodata._perror_colon_space_str
56+
.local _perror_colon_space_str
57+
_perror_colon_space_str:
58+
.asciz ": "

0 commit comments

Comments
 (0)