1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 07:29:33 +00:00

Use more compact loops.

This commit is contained in:
Sven Michael Klose 2020-11-01 22:59:07 +01:00 committed by greg-king5
parent 99c0815cdb
commit f59cb9af06
13 changed files with 90 additions and 94 deletions

View File

@ -20,18 +20,19 @@
sta ptr3 sta ptr3
stx ptr3+1 ; save count as result stx ptr3+1 ; save count as result
eor #$FF
sta ptr2 inx
txa stx ptr2+1
eor #$FF tax
sta ptr2+1 ; Remember -count-1 inx
stx ptr2 ; save count with each byte incremented separately
jsr popptr1 ; get buf jsr popptr1 ; get buf
jsr popax ; get fd and discard jsr popax ; get fd and discard
L1: inc ptr2 L1: dec ptr2
bnz L2 bnz L2
inc ptr2+1 dec ptr2+1
bze L9 ; no more room in buf bze L9 ; no more room in buf
; If there are no more characters in BASIC's input buffer, then get a line from ; If there are no more characters in BASIC's input buffer, then get a line from

View File

@ -17,17 +17,17 @@
sta ptr3 sta ptr3
stx ptr3+1 ; save count as result stx ptr3+1 ; save count as result
eor #$FF inx
sta ptr2 stx ptr2+1
txa tax
eor #$FF inx
sta ptr2+1 ; Remember -count-1 stx ptr2 ; save count with each byte incremented separately
jsr popptr1 ; get buf jsr popptr1 ; get buf
jsr popax ; get fd and discard jsr popax ; get fd and discard
L1: inc ptr2 L1: dec ptr2
bne L2 bne L2
inc ptr2+1 dec ptr2+1
beq L9 beq L9
L2: ldy #0 L2: ldy #0
lda (ptr1),y lda (ptr1),y

View File

@ -106,9 +106,9 @@
; Decrement the count ; Decrement the count
@L3: inc ptr2 @L3: dec ptr2
bne @L0 bne @L0
inc ptr2+1 dec ptr2+1
bne @L0 bne @L0
beq done ; Branch always beq done ; Branch always

View File

@ -21,11 +21,11 @@
.proc rwcommon .proc rwcommon
eor #$FF inx
sta ptr2 stx ptr2+1
txa tax
eor #$FF inx
sta ptr2+1 ; Remember -count-1 stx ptr2 ; Save count with each byte incremented separately
jsr popptr1 ; Get buf to ptr1, Y=0 by call jsr popptr1 ; Get buf to ptr1, Y=0 by call

View File

@ -83,9 +83,9 @@
; Decrement count ; Decrement count
@L2: inc ptr2 @L2: dec ptr2
bne @L0 bne @L0
inc ptr2+1 dec ptr2+1
bne @L0 bne @L0
; Wrote all chars or disk full. Close the output channel ; Wrote all chars or disk full. Close the output channel

View File

@ -13,11 +13,11 @@ _memcmp:
; Calculate (-count-1) and store it into ptr3. This is some overhead here but ; Calculate (-count-1) and store it into ptr3. This is some overhead here but
; saves time in the compare loop ; saves time in the compare loop
eor #$FF inx
sta ptr3 stx ptr3+1
txa tax
eor #$FF inx
sta ptr3+1 stx ptr3 ; Save count with each byte incremented separately
; Get the pointer parameters ; Get the pointer parameters
@ -33,7 +33,7 @@ _memcmp:
; Head of compare loop: Test for the end condition ; Head of compare loop: Test for the end condition
Loop: inx ; Bump low byte of (-count-1) Loop: dex ; Bump low byte of (-count-1)
beq BumpHiCnt ; Jump on overflow beq BumpHiCnt ; Jump on overflow
; Do the compare ; Do the compare
@ -53,7 +53,7 @@ Comp: lda (ptr1),y
; Entry on low counter byte overflow ; Entry on low counter byte overflow
BumpHiCnt: BumpHiCnt:
inc ptr3+1 ; Bump high byte of (-count-1) dec ptr3+1 ; Bump high byte of (-count-1)
bne Comp ; Jump if not done bne Comp ; Jump if not done
jmp return0 ; Count is zero, areas are identical jmp return0 ; Count is zero, areas are identical
@ -67,4 +67,3 @@ NotEqual:
Greater: Greater:
ldx #$01 ; Make result positive ldx #$01 ; Make result positive
rts rts

View File

@ -5,17 +5,17 @@
; char* strncat (char* dest, const char* src, size_t n); ; char* strncat (char* dest, const char* src, size_t n);
; ;
.export _strncat .export _strncat
.import popax, popptr1 .import popax, popptr1
.importzp ptr1, ptr2, ptr3, tmp1, tmp2 .importzp ptr1, ptr2, ptr3, tmp1, tmp2
.macpack cpu .macpack cpu
_strncat: _strncat:
eor #$FF ; one's complement to count upwards inx
sta tmp1 stx tmp2
txa tax
eor #$FF inx
sta tmp2 stx tmp1 ; save count with each byte incremented separately
jsr popptr1 ; get src jsr popptr1 ; get src
@ -49,9 +49,9 @@ L2: sty ptr2
L3: ldy #0 L3: ldy #0
ldx tmp1 ; low counter byte ldx tmp1 ; low counter byte
L4: inx L4: dex
bne L5 bne L5
inc tmp2 dec tmp2
beq L6 ; jump if done beq L6 ; jump if done
L5: lda (ptr1),y L5: lda (ptr1),y
sta (ptr2),y sta (ptr2),y

View File

@ -10,11 +10,11 @@
.proc _strncpy .proc _strncpy
eor #$FF inx
sta tmp1 stx tmp2
txa tax
eor #$FF inx
sta tmp2 ; Store -size - 1 stx tmp1 ; save count with each byte incremented separately
jsr popptr1 ; get src jsr popptr1 ; get src
jsr popax ; get dest jsr popax ; get dest
@ -26,9 +26,9 @@
ldx tmp1 ; Load low byte of ones complement of size ldx tmp1 ; Load low byte of ones complement of size
ldy #$00 ldy #$00
L1: inx L1: dex
bne L2 bne L2
inc tmp2 dec tmp2
beq L9 beq L9
L2: lda (ptr1),y ; Copy one character L2: lda (ptr1),y ; Copy one character
@ -42,7 +42,7 @@ L2: lda (ptr1),y ; Copy one character
; Fill the remaining bytes. ; Fill the remaining bytes.
L3: inx ; Counter low byte L3: dex ; Counter low byte
beq L6 ; Branch on overflow beq L6 ; Branch on overflow
L4: sta (ptr2),y ; Clear one byte L4: sta (ptr2),y ; Clear one byte
L5: iny ; Bump pointer L5: iny ; Bump pointer
@ -52,7 +52,7 @@ L5: iny ; Bump pointer
; Bump the counter high byte ; Bump the counter high byte
L6: inc tmp2 L6: dec tmp2
bne L4 bne L4
; Done, return dest ; Done, return dest

View File

@ -15,17 +15,11 @@
_strnicmp: _strnicmp:
_strncasecmp: _strncasecmp:
; Convert the given counter value in a/x from a downward counter into an inx
; upward counter, so we can increment the counter in the loop below instead stx ptr3+1
; of decrementing it. This adds some overhead now, but is cheaper than tax
; executing a more complex test in each iteration of the loop. We do also inx
; correct the value by one, so we can do the test on top of the loop. stx ptr3 ; save count with each byte incremented separately
eor #$FF
sta ptr3
txa
eor #$FF
sta ptr3+1
; Get the remaining arguments ; Get the remaining arguments
@ -40,7 +34,7 @@ _strncasecmp:
; Start of compare loop. Check the counter. ; Start of compare loop. Check the counter.
Loop: inc ptr3 Loop: dec ptr3
beq IncHi ; increment high byte beq IncHi ; increment high byte
; Compare a byte from the strings ; Compare a byte from the strings
@ -79,7 +73,7 @@ L2: ldx tmp1
; Increment hi byte ; Increment hi byte
IncHi: inc ptr3+1 IncHi: dec ptr3+1
bne Comp ; jump if counter not zero bne Comp ; jump if counter not zero
; Exit code if strings are equal. a/x not set ; Exit code if strings are equal. a/x not set

View File

@ -47,12 +47,12 @@ outdesc: ; Static outdesc structure
out: jsr popax ; count out: jsr popax ; count
sta ptr2 sta ptr2
eor #$FF stx ptr2+1
sta outdesc+6 inx
txa stx outdesc+7
sta ptr2+1 tax
eor #$FF inx
sta outdesc+7 stx outdesc+6
jsr popptr1 ; buf jsr popptr1 ; buf
@ -74,7 +74,7 @@ out: jsr popax ; count
; Loop outputting characters ; Loop outputting characters
@L1: inc outdesc+6 @L1: dec outdesc+6
beq @L4 beq @L4
@L2: ldy tmp1 @L2: ldy tmp1
lda (ptr1),y lda (ptr1),y
@ -85,7 +85,7 @@ out: jsr popax ; count
jsr _cputc jsr _cputc
jmp @L1 jmp @L1
@L4: inc outdesc+7 @L4: dec outdesc+7
bne @L2 bne @L2
rts rts

View File

@ -94,11 +94,12 @@ _read:
; popax - fd, must be == to the above one ; popax - fd, must be == to the above one
; return -1+__oserror or number of bytes read ; return -1+__oserror or number of bytes read
eor #$ff inx
sta ptr1 stx ptr1+1
txa tax
eor #$ff inx
sta ptr1+1 ; -(# of bytes to read)-1 stx ptr1 ; save count with each byte incremented separately
jsr popax jsr popax
sta ptr2 sta ptr2
stx ptr2+1 ; buffer ptr stx ptr2+1 ; buffer ptr
@ -152,9 +153,9 @@ _read:
beq @done ; yes, we're done beq @done ; yes, we're done
jmp __mappederrno ; no, we're screwed jmp __mappederrno ; no, we're screwed
@L3: inc ptr1 ; decrement the count @L3: dec ptr1 ; decrement the count
bne @L0 bne @L0
inc ptr1+1 dec ptr1+1
bne @L0 bne @L0
@done: @done:

View File

@ -50,16 +50,17 @@ LINEDIST = $20 ; Offset in video RAM between two lines
ldx #>load_addr ldx #>load_addr
sta load sta load
stx load+1 stx load+1
lda #<load_size
eor #$FF
sta count ; store (-size - 1)
lda #>load_size
eor #$FF
sta count+1
L1: inc count ; pre-count one's-complement upwards ldx #<load_size
inx
stx count
ldx #>load_size
inx
stx count+1 ; save size with each byte incremented separately
L1: dec count ; pre-count one's-complement upwards
bnz L2 bnz L2
inc count+1 dec count+1
bze L3 bze L3
L2: jsr GETCHAR ; (doesn't change .Y) L2: jsr GETCHAR ; (doesn't change .Y)
sta (load),y sta (load),y

View File

@ -13,11 +13,11 @@
sta ptr3 sta ptr3
stx ptr3+1 ; save count as result stx ptr3+1 ; save count as result
eor #$FF inx
sta ptr2 stx ptr2+1
txa tax
eor #$FF inx
sta ptr2+1 ; remember -count-1 stx ptr2 ; save count with each byte incremented separately
jsr popptr1 ; get buf jsr popptr1 ; get buf
jsr popax ; get fd and discard jsr popax ; get fd and discard
@ -51,9 +51,9 @@ next:
rts rts
L1: inc ptr2 L1: dec ptr2
bne L2 bne L2
inc ptr2+1 dec ptr2+1
beq L9 beq L9
L2: ldy #0 L2: ldy #0
lda (ptr1),y lda (ptr1),y