ctype size optimization

This commit is contained in:
IrgendwerA8
2020-01-02 18:57:03 +01:00
committed by Oliver Schmidt
parent dc5114b071
commit ce80624f62
43 changed files with 1491 additions and 3563 deletions

View File

@@ -8,9 +8,8 @@
.export _atoi, _atol
.import negeax, __ctype
.importzp sreg, ptr1, ptr2, tmp1
.import ctype_preprocessor_no_check
.include "ctype.inc"
;
; Conversion routine (32 bit)
;
@@ -27,8 +26,9 @@ _atol: sta ptr1 ; Store s
; Skip whitespace
L1: lda (ptr1),y
tax
lda __ctype,x ; get character classification
; get character classification
jsr ctype_preprocessor_no_check
and #CT_SPACE_TAB ; tab or space?
beq L2 ; jump if no
iny
@@ -36,10 +36,10 @@ L1: lda (ptr1),y
inc ptr1+1
bne L1 ; branch always
; Check for a sign. The character is in X
; Check for a sign. Refetch character, X is cleared by preprocessor
L2: txa ; get char
ldx #0 ; flag: positive
L2: lda (ptr1),y ; get char
; x=0 -> flag: positive
cmp #'+' ; ### portable?
beq L3
cmp #'-' ; ### portable?
@@ -54,9 +54,9 @@ L3: iny
L5: stx tmp1 ; remember sign flag
L6: lda (ptr1),y ; get next char
tax
lda __ctype,x ; get character classification
and #$04 ; digit?
; get character classification
jsr ctype_preprocessor_no_check
and #CT_DIGIT ; digit?
beq L8 ; done
; Multiply ptr2 (the converted value) by 10
@@ -91,7 +91,7 @@ L6: lda (ptr1),y ; get next char
; Get the character back and add it
txa ; get char back
lda (ptr1),y ; fetch char again
sec
sbc #'0' ; make numeric value
clc

View File

@@ -0,0 +1,50 @@
; ctype_preprocessor.s
;
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; ctype_preprocessor(int c)
;
; converts a character to test via the is*-functions to the matching ctype-masks
; If c is out of the 8-bit range, the function returns with carry set and accu cleared.
; Return value is in accu and x has to be always clear when returning
; (makes calling code shorter)!
;
; IMPORTANT: stricmp, strlower, strnicmp, strupper and atoi rely that Y is not changed
; while calling this function!
;
.export ctype_preprocessor
.export ctype_preprocessor_no_check
.import __ctype
.import __ctypeIdx
ctype_preprocessor:
cpx #$00 ; char range ok?
bne SC ; branch if not
ctype_preprocessor_no_check:
lsr a
tax
lda __ctypeIdx, x
bcc @lowerNibble
@upperNibble:
lsr a
lsr a
lsr a
lsr a
clc ; remove out of bounds flag
@lowerNibble:
and #%1111
tax
lda __ctype, x
ldx #0
rts
SC: sec
lda #0
tax
rts

View File

@@ -1,21 +1,21 @@
; isalnum.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isalnum (int c);
;
.export _isalnum
.include "ctype.inc"
.import ctype_preprocessor
_isalnum:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_ALNUM ; Mask character/digit bits
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_ALNUM ; mask character/digit bits
@L1: rts

View File

@@ -1,21 +1,21 @@
; isalpha.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isalpha (int c);
;
.export _isalpha
.include "ctype.inc"
.import ctype_preprocessor
_isalpha:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_ALPHA ; Mask character bits
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_ALPHA ; mask character bits
@L1: rts

27
libsrc/common/isascii.s Normal file
View File

@@ -0,0 +1,27 @@
; isascii.s
;
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isascii (int c);
;
.export _isascii
_isascii:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
bmi @L1
inx
rts
@L1: lda #$00 ; Return false
tax
rts

View File

@@ -1,5 +1,11 @@
; isblank.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isblank (int c);
;
@@ -8,16 +14,10 @@
.export _isblank
.include "ctype.inc"
.import ctype_preprocessor
_isblank:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_SPACE_TAB ; Mask blank bit
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_SPACE_TAB ; mask blank bit
@L1: rts

View File

@@ -1,21 +1,21 @@
; iscntrl.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int iscntrl (int c);
;
.export _iscntrl
.include "ctype.inc"
.import ctype_preprocessor
_iscntrl:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_CTRL ; Mask control character bit
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_CTRL ; mask control character bit
@L1: rts

View File

@@ -1,21 +1,21 @@
; isdigit.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isdigit (int c);
;
.export _isdigit
.include "ctype.inc"
.import ctype_preprocessor
_isdigit:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_DIGIT ; Mask digit bit
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_DIGIT ; mask digit bit
@L1: rts

View File

@@ -1,25 +1,25 @@
; isgraph.s
;
; 1998-06-02, Ullrich von Bassewitz
; 2014-09-10, Greg King
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isgraph (int c);
;
.export _isgraph
.include "ctype.inc"
.import ctype_preprocessor
_isgraph:
cpx #>$0000 ; Char range OK?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_CTRL_SPACE ; Mask character bits
cmp #1 ; If false, then set "borrow" flag
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_CTRL_SPACE ; mask character bits
cmp #1 ; if false, then set "borrow" flag
lda #0
sbc #0 ; Invert logic
rts ; Return NOT control and NOT space
@L1: lda #<0 ; Return false
tax
rts
sbc #0 ; invert logic (return NOT control and NOT space)
@L1: rts

View File

@@ -1,21 +1,23 @@
; islower.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int islower (int c);
;
.export _islower
.include "ctype.inc"
.import ctype_preprocessor
_islower:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_LOWER ; Mask lower char bit
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_LOWER ; mask lower char bit
@L1: rts
@L1: lda #$00 ; Return false
tax
rts

View File

@@ -1,22 +1,22 @@
; isprint.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isprint (int c);
;
.export _isprint
.include "ctype.inc"
.import ctype_preprocessor
_isprint:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
eor #CT_CTRL ; NOT a control char
and #CT_CTRL ; Mask control char bit
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
eor #CT_CTRL ; NOT a control char
and #CT_CTRL ; mask control char bit
@L1: rts

View File

@@ -1,25 +1,24 @@
; ispunct.s
;
; 1998-06-02, Ullrich von Bassewitz
; 2014-09-10, Greg King
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int ispunct (int c);
;
.export _ispunct
.include "ctype.inc"
.import ctype_preprocessor
_ispunct:
cpx #>$0000 ; Char range OK?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_NOT_PUNCT ; Mask relevant bits
cmp #1 ; If false, then set "borrow" flag
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_NOT_PUNCT ; mask relevant bits
cmp #1 ; if false, then set "borrow" flag
lda #0
sbc #0 ; Invert logic
rts ; Return NOT (space | control | digit | alpha)
@L1: lda #<0 ; Return false
tax
rts
sbc #0 ; invert logic (return NOT (space | control | digit | alpha))
@L1: rts

View File

@@ -1,21 +1,21 @@
; isspace.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isspace (int c);
;
.export _isspace
.include "ctype.inc"
.import ctype_preprocessor
_isspace:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #(CT_SPACE | CT_OTHER_WS) ; Mask space bits
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #(CT_SPACE | CT_OTHER_WS) ; mask space bits
@L1: rts

View File

@@ -1,21 +1,21 @@
; isupper.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isupper (int c);
;
.export _isupper
.include "ctype.inc"
.import ctype_preprocessor
_isupper:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_UPPER ; Mask upper char bit
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_UPPER ; mask upper char bit
@L1: rts

View File

@@ -1,21 +1,21 @@
; isxdigit.s
;
; Ullrich von Bassewitz, 02.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int isxdigit (int c);
;
.export _isxdigit
.include "ctype.inc"
.import ctype_preprocessor
_isxdigit:
cpx #$00 ; Char range ok?
bne @L1 ; Jump if no
tay
lda __ctype,y ; Get character classification
and #CT_XDIGIT ; Mask xdigit bit
rts
@L1: lda #$00 ; Return false
tax
rts
jsr ctype_preprocessor ; (clears always x)
bcs @L1 ; out of range? (everything already clear -> false)
and #CT_XDIGIT ; mask xdigit bit
@L1: rts

View File

@@ -1,5 +1,11 @@
; stricmp.s
;
; Ullrich von Bassewitz, 03.06.1998
; This file is part of
; cc65 - a freeware C compiler for 6502 based systems
;
; https://github.com/cc65/cc65
;
; See "LICENSE" file for legal information.
;
; int stricmp (const char* s1, const char* s2); /* DOS way */
; int strcasecmp (const char* s1, const char* s2); /* UNIX way */
@@ -7,9 +13,8 @@
.export _stricmp, _strcasecmp
.import popptr1
.import __ctype
.importzp ptr1, ptr2, tmp1
.importzp ptr1, ptr2, tmp1, tmp2
.import ctype_preprocessor_no_check
.include "ctype.inc"
_stricmp:
@@ -20,27 +25,27 @@ _strcasecmp:
; ldy #0 ; Y=0 guaranteed by popptr1
loop: lda (ptr2),y ; get char from second string
tax
lda __ctype,x ; get character classification
sta tmp2 ; and save it
; get character classification
jsr ctype_preprocessor_no_check
and #CT_LOWER ; lower case char?
beq L1 ; jump if no
txa ; get character back
clc
adc #<('A'-'a') ; make upper case char
tax ;
L1: stx tmp1 ; remember upper case equivalent
lda #<('A'-'a') ; make upper case char
adc tmp2 ; ctype_preprocessor_no_check ensures carry clear!
sta tmp2 ; remember upper case equivalent
lda (ptr1),y ; get character from first string
tax
lda __ctype,x ; get character classification
L1: lda (ptr1),y ; get character from first string
sta tmp1
; get character classification
jsr ctype_preprocessor_no_check
and #CT_LOWER ; lower case char?
beq L2 ; jump if no
txa ; get character back
clc
adc #<('A'-'a') ; make upper case char
tax
lda #<('A'-'a') ; make upper case char
adc tmp1 ; ctype_preprocessor_no_check ensures carry clear!
sta tmp1 ; remember upper case equivalent
L2: cpx tmp1 ; compare characters
L2: ldx tmp1
cpx tmp2 ; compare characters
bne L3
txa ; end of strings?
beq L5 ; a/x both zero

View File

@@ -10,9 +10,8 @@
.export _strlower, _strlwr
.import popax
.import __ctype
.importzp ptr1, ptr2
.import ctype_preprocessor_no_check
.include "ctype.inc"
_strlower:
@@ -25,11 +24,11 @@ _strlwr:
loop: lda (ptr1),y ; get character
beq L9 ; jump if done
tax
lda __ctype,x ; get character classification
; get character classification
jsr ctype_preprocessor_no_check
and #CT_UPPER ; upper case char?
beq L1 ; jump if no
txa ; get character back into accu
lda (ptr1),y ; fetch character again
sec
sbc #<('A'-'a') ; make lower case char
sta (ptr1),y ; store back

View File

@@ -7,9 +7,9 @@
;
.export _strnicmp, _strncasecmp
.import popax, popptr1, __ctype
.importzp ptr1, ptr2, ptr3, tmp1
.import popax, popptr1
.importzp ptr1, ptr2, ptr3, tmp1, tmp2
.import ctype_preprocessor_no_check
.include "ctype.inc"
_strnicmp:
@@ -46,27 +46,27 @@ Loop: inc ptr3
; Compare a byte from the strings
Comp: lda (ptr2),y
tax
lda __ctype,x ; get character classification
sta tmp2 ; remember original char
; get character classification
jsr ctype_preprocessor_no_check
and #CT_LOWER ; lower case char?
beq L1 ; jump if no
txa ; get character back
sec
sbc #<('a'-'A') ; make upper case char
tax ;
L1: stx tmp1 ; remember upper case equivalent
lda #<('A'-'a') ; make upper case char
adc tmp2 ; ctype_preprocessor_no_check ensures carry clear!
sta tmp2 ; remember upper case equivalent
lda (ptr1),y ; get character from first string
tax
lda __ctype,x ; get character classification
L1: lda (ptr1),y ; get character from first string
sta tmp1 ; remember original char
; get character classification
jsr ctype_preprocessor_no_check
and #CT_LOWER ; lower case char?
beq L2 ; jump if no
txa ; get character back
sec
sbc #<('a'-'A') ; make upper case char
tax
lda #<('A'-'a') ; make upper case char
adc tmp1 ; ctype_preprocessor_no_check ensures carry clear!
sta tmp1 ; remember upper case equivalent
L2: cpx tmp1 ; compare characters
L2: ldx tmp1
cpx tmp2 ; compare characters
bne NotEqual ; Jump if strings different
txa ; End of strings?
beq Equal1 ; Jump if EOS reached, a/x == 0

View File

@@ -10,9 +10,8 @@
.export _strupper, _strupr
.import popax
.import __ctype
.importzp ptr1, ptr2
.import ctype_preprocessor_no_check
.include "ctype.inc"
_strupper:
@@ -25,11 +24,10 @@ _strupr:
loop: lda (ptr1),y ; get character
beq L9 ; jump if done
tax
lda __ctype,x ; get character classification
jsr ctype_preprocessor_no_check
and #CT_LOWER ; lower case char?
beq L1 ; jump if no
txa ; get character back into accu
lda (ptr1),y ; fetch character again
clc
adc #<('A'-'a') ; make upper case char
sta (ptr1),y ; store back