From ba3607102263c73af18105902a8e50063618bd96 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Tue, 30 Jan 2024 17:51:00 +0100 Subject: [PATCH 1/4] Rewrite fputs in assembly -28 bytes, -1% cycles --- libsrc/common/fputs.c | 28 ---------------------------- libsrc/common/fputs.s | 42 ++++++++++++++++++++++++++++++++++++++++++ test/ref/test_fputs.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 28 deletions(-) delete mode 100644 libsrc/common/fputs.c create mode 100644 libsrc/common/fputs.s create mode 100644 test/ref/test_fputs.c diff --git a/libsrc/common/fputs.c b/libsrc/common/fputs.c deleted file mode 100644 index be476a3f0..000000000 --- a/libsrc/common/fputs.c +++ /dev/null @@ -1,28 +0,0 @@ -/* -** int fputs (const char* s, FILE* f); -** -** Ullrich von Bassewitz, 11.08.1998 -*/ - - - -#include -#include -#include -#include "_file.h" - - - -int __fastcall__ fputs (const char* s, register FILE* f) -{ - /* Check if the file is open or if there is an error condition */ - if ((f->f_flags & _FOPEN) == 0 || (f->f_flags & (_FERROR | _FEOF)) != 0) { - return EOF; - } - - /* Write the string */ - return write (f->f_fd, s, strlen (s)); -} - - - diff --git a/libsrc/common/fputs.s b/libsrc/common/fputs.s new file mode 100644 index 000000000..e70374058 --- /dev/null +++ b/libsrc/common/fputs.s @@ -0,0 +1,42 @@ +; +; Colin Leroy-Mira, 2024 +; +; int __fastcall__ fputs (const char* s, register FILE* f) +; + + .export _fputs + .importzp ptr1, ptr2 + .import _write, _strlen + .import swapstk, pushax, returnFFFF + + .include "stdio.inc" + .include "_file.inc" + +_fputs: + sta ptr1 + stx ptr1+1 + + ldy #_FILE::f_flags + lda (ptr1),y + tax + and #_FOPEN ; Check for file open + beq ret_eof + txa + and #(_FERROR|_FEOF); Check for error/eof + bne ret_eof + + ; Push _write parameters + ldy #_FILE::f_fd + lda (ptr1),y + ldx #$00 + jsr swapstk ; Push fd, get s + + jsr pushax ; Push s + + jsr _strlen ; Get length + + ; Write + jmp _write + +ret_eof: + jmp returnFFFF diff --git a/test/ref/test_fputs.c b/test/ref/test_fputs.c new file mode 100644 index 000000000..ad0552317 --- /dev/null +++ b/test/ref/test_fputs.c @@ -0,0 +1,40 @@ +/* + !!DESCRIPTION!! fgets test + !!LICENCE!! Public domain +*/ + +#include "common.h" + +#include +#include +#include +#include +#include +#include + +FILE *in, *out; +char buf[512], err; + +#define INFILE "cf.in" + +int main(int argc,char **argv) +{ + in = fopen(INFILE, "rb"); + if (in == NULL) { + return EXIT_FAILURE; + } + + strcpy(buf, "test"); + if (fputs(buf, in) != EOF) { + printf("Error: can fputs to a file opened for reading\n"); + return EXIT_FAILURE; + } + clearerr(in); + + while (fgets(buf, 512, in) != NULL) { + fputs(buf, stdout); + } + + fclose(in); + return 0; +} From 1a5a7d67a7ebb4881a90234b596fdee4612b09ef Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Tue, 30 Jan 2024 17:59:33 +0100 Subject: [PATCH 2/4] Rewrite fputc in assembly -36 bytes, -12% cycles --- libsrc/common/fputc.c | 41 ------------------------ libsrc/common/fputc.s | 72 +++++++++++++++++++++++++++++++++++++++++++ libsrc/common/fputs.s | 4 +-- test/ref/test_fputc.c | 39 +++++++++++++++++++++++ 4 files changed, 113 insertions(+), 43 deletions(-) delete mode 100644 libsrc/common/fputc.c create mode 100644 libsrc/common/fputc.s create mode 100644 test/ref/test_fputc.c diff --git a/libsrc/common/fputc.c b/libsrc/common/fputc.c deleted file mode 100644 index b623949d3..000000000 --- a/libsrc/common/fputc.c +++ /dev/null @@ -1,41 +0,0 @@ -/* -** fputc.c -** -** Ullrich von Bassewitz, 02.06.1998 -*/ - - - -#include -#include -#include "_file.h" - - - -/*****************************************************************************/ -/* Code */ -/*****************************************************************************/ - - - -int __fastcall__ fputc (int c, register FILE* f) -{ - /* Check if the file is open or if there is an error condition */ - if ((f->f_flags & _FOPEN) == 0 || (f->f_flags & (_FERROR | _FEOF)) != 0) { - goto ReturnEOF; - } - - /* Write the byte */ - if (write (f->f_fd, &c, 1) != 1) { - /* Error */ - f->f_flags |= _FERROR; -ReturnEOF: - return EOF; - } - - /* Return the byte written */ - return c & 0xFF; -} - - - diff --git a/libsrc/common/fputc.s b/libsrc/common/fputc.s new file mode 100644 index 000000000..4633f24f1 --- /dev/null +++ b/libsrc/common/fputc.s @@ -0,0 +1,72 @@ +; +; Colin Leroy-Mira, 2024 +; +; int __fastcall__ fputc (int c, FILE* f); +; + + .export _fputc + .importzp ptr1 + .import _write + .import pushax, pusha0, popax, incsp2 + .import pushptr1, popptr1, returnFFFF + + .include "stdio.inc" + .include "_file.inc" + +_fputc: + sta ptr1 + stx ptr1+1 + + jsr popax ; Get char, as we'll have + sta c ; to return it anyway + stx c+1 + + ldy #_FILE::f_flags + lda (ptr1),y + tax + and #_FOPEN ; Check for file open + beq ret_eof + txa + and #(_FERROR|_FEOF); Check for error/eof + bne ret_eof + + jsr pushptr1 ; Backup fp pointer + + ; Push _write parameters + ldy #_FILE::f_fd + lda (ptr1),y + jsr pusha0 + + lda #c + jsr pushax + + lda #$01 + ldx #$00 + + ; Write + jsr _write + + ; Check for errors + cmp #$01 + bne set_ferror + + ; Return char + lda c + ldx #$00 + jmp incsp2 ; Drop fp pointer copy + +ret_eof: + jmp returnFFFF + +set_ferror: + jsr popptr1 + lda #_FERROR + ldy #_FILE::f_flags + ora (ptr1),y + sta (ptr1),y + jmp returnFFFF + + .bss + +c: .res 2 diff --git a/libsrc/common/fputs.s b/libsrc/common/fputs.s index e70374058..00415aef1 100644 --- a/libsrc/common/fputs.s +++ b/libsrc/common/fputs.s @@ -32,9 +32,9 @@ _fputs: jsr swapstk ; Push fd, get s jsr pushax ; Push s - + jsr _strlen ; Get length - + ; Write jmp _write diff --git a/test/ref/test_fputc.c b/test/ref/test_fputc.c new file mode 100644 index 000000000..a19aeafaf --- /dev/null +++ b/test/ref/test_fputc.c @@ -0,0 +1,39 @@ +/* + !!DESCRIPTION!! fgets test + !!LICENCE!! Public domain +*/ + +#include "common.h" + +#include +#include +#include +#include +#include +#include + +FILE *in, *out; +int c, err; + +#define INFILE "cf.in" + +int main(int argc,char **argv) +{ + in = fopen(INFILE, "rb"); + if (in == NULL) { + return EXIT_FAILURE; + } + + if (fputc(c, in) != EOF) { + printf("Error: can fputc to a file opened for reading\n"); + return EXIT_FAILURE; + } + clearerr(in); + + while ((c = fgetc(in)) != EOF) { + fputc(c, stdout); + } + + fclose(in); + return 0; +} From 2b97735d5d150f1ce9d568b10fd878fa503ca232 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Tue, 30 Jan 2024 18:11:10 +0100 Subject: [PATCH 3/4] Factorize file pointer check --- libsrc/common/checkferror.s | 24 ++++++++++++++++++++++++ libsrc/common/fgetc.s | 13 ++++--------- libsrc/common/fputc.s | 10 ++-------- libsrc/common/fputs.s | 10 ++-------- 4 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 libsrc/common/checkferror.s diff --git a/libsrc/common/checkferror.s b/libsrc/common/checkferror.s new file mode 100644 index 000000000..736fa3ccd --- /dev/null +++ b/libsrc/common/checkferror.s @@ -0,0 +1,24 @@ +; +; Colin Leroy-Mira, 2024 +; +; Helper to check for file opened, not eof, not ferror +; Expects file pointer in ptr1, +; Returns with Z flag set if everything is OK, +; Destroys A, X, Y, +; Sets file flags in A +; + + .export checkferror + .importzp ptr1 + + .include "_file.inc" + +checkferror: + ldy #_FILE::f_flags + lda (ptr1),y + tax + and #(_FOPEN|_FERROR|_FEOF); Check for file open, error/eof + tay + txa + cpy #_FOPEN + rts diff --git a/libsrc/common/fgetc.s b/libsrc/common/fgetc.s index 777696b7a..98e6bb2f7 100644 --- a/libsrc/common/fgetc.s +++ b/libsrc/common/fgetc.s @@ -5,7 +5,8 @@ ; .export _fgetc - .import _read, pusha0, pushax, popptr1, incsp2, returnFFFF + .import _read, checkferror + .import pusha0, pushax, popptr1, incsp2, returnFFFF .importzp ptr1 .include "stdio.inc" @@ -16,16 +17,10 @@ _fgetc: stx ptr1+1 jsr pushax ; Backup our ptr - ldy #_FILE::f_flags - lda (ptr1),y - tax - and #_FOPEN ; Check for file open - beq ret_eof - txa - and #(_FERROR|_FEOF); Check for error/eof + jsr checkferror bne ret_eof - txa + tax and #_FPUSHBACK ; Check for pushed back char beq do_read diff --git a/libsrc/common/fputc.s b/libsrc/common/fputc.s index 4633f24f1..358723538 100644 --- a/libsrc/common/fputc.s +++ b/libsrc/common/fputc.s @@ -6,7 +6,7 @@ .export _fputc .importzp ptr1 - .import _write + .import _write, checkferror .import pushax, pusha0, popax, incsp2 .import pushptr1, popptr1, returnFFFF @@ -21,13 +21,7 @@ _fputc: sta c ; to return it anyway stx c+1 - ldy #_FILE::f_flags - lda (ptr1),y - tax - and #_FOPEN ; Check for file open - beq ret_eof - txa - and #(_FERROR|_FEOF); Check for error/eof + jsr checkferror bne ret_eof jsr pushptr1 ; Backup fp pointer diff --git a/libsrc/common/fputs.s b/libsrc/common/fputs.s index 00415aef1..b79a4707f 100644 --- a/libsrc/common/fputs.s +++ b/libsrc/common/fputs.s @@ -6,7 +6,7 @@ .export _fputs .importzp ptr1, ptr2 - .import _write, _strlen + .import _write, _strlen, checkferror .import swapstk, pushax, returnFFFF .include "stdio.inc" @@ -16,13 +16,7 @@ _fputs: sta ptr1 stx ptr1+1 - ldy #_FILE::f_flags - lda (ptr1),y - tax - and #_FOPEN ; Check for file open - beq ret_eof - txa - and #(_FERROR|_FEOF); Check for error/eof + jsr checkferror bne ret_eof ; Push _write parameters From afd8ee627e4e71900f9cbacda20e010fa529684d Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Tue, 30 Jan 2024 19:52:48 +0100 Subject: [PATCH 4/4] Remove useless branching code in fgets --- libsrc/common/fgets.s | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libsrc/common/fgets.s b/libsrc/common/fgets.s index 465658191..172ca10dd 100644 --- a/libsrc/common/fgets.s +++ b/libsrc/common/fgets.s @@ -90,9 +90,7 @@ read_loop: : cmp #$0A ; Stop at \n beq done - - clc - bcc read_loop + bne read_loop got_eof: lda didread