optimized gnomesorts

This commit is contained in:
Irmen de Jong
2025-01-23 23:47:37 +01:00
parent d0c184c7de
commit 8583a96519
2 changed files with 24 additions and 23 deletions
+21 -23
View File
@@ -20,12 +20,10 @@ sorting {
dey
+ dec P8ZP_SCRATCH_W2
sty P8ZP_SCRATCH_W2+1
ldy #0 ; pos
ldy #1 ; pos
_loop
cpy #0 ; modified
beq _done
cpy #0
beq +
lda (P8ZP_SCRATCH_W1),y
cmp (P8ZP_SCRATCH_W2),y
bcs +
@@ -36,7 +34,7 @@ _loop
txa
sta (P8ZP_SCRATCH_W2),y
dey
jmp _loop
bne _loop
+ iny
bne _loop
_done
@@ -48,11 +46,9 @@ _done
prog8 source code for the above routine:
sub gnomesort_ub(uword @requirezp values, ubyte num_elements) {
ubyte @zp pos
ubyte @zp pos=1
while pos != num_elements {
if pos==0
pos++
else if values[pos]>=values[pos-1]
if values[pos]>=values[pos-1]
pos++
else {
; swap elements
@@ -60,6 +56,8 @@ _done
values[pos-1] = values[pos]
values[pos] = cx16.r0L
pos--
if_z
pos++
}
}
}
@@ -67,22 +65,22 @@ _done
sub gnomesort_uw(uword values, ubyte num_elements) {
; TODO optimize this more, rewrite in asm?
uword @zp pos
cx16.r2 = num_elements*$0002
while pos != cx16.r2 {
uword @requirezp ptr = values+pos
if pos==0
pos += 2
ubyte @zp pos = 1
uword @requirezp ptr = values+2
while pos != num_elements {
cx16.r0 = peekw(ptr-2)
cx16.r1 = peekw(ptr)
if cx16.r0<=cx16.r1 {
pos++
ptr+=2
}
else {
cx16.r0 = peekw(ptr-2)
cx16.r1 = peekw(ptr)
if cx16.r0<=cx16.r1
pos += 2
else {
; swap elements
pokew(ptr-2, cx16.r1)
pokew(ptr, cx16.r0)
pos -= 2
; swap elements
pokew(ptr-2, cx16.r1)
pokew(ptr, cx16.r0)
if pos>1 {
pos--
ptr-=2
}
}
}
+3
View File
@@ -1,6 +1,9 @@
TODO
====
- word+byte*2 -> word +byte +byte, word-byte*2 -> word-byte-byte (check that it gets properly word-extended!)
- optimize pokew and peekw to no longer do a jsr
- Make some of the target machine config externally configurable (for 1 new target, the existing ones should stay as they are for the time being)
- add paypal donation button as well?