Optimize Sort4
This commit is contained in:
parent
fd188204b4
commit
0f5c426699
279
ucode/sort4.uc
279
ucode/sort4.uc
@ -1,19 +1,14 @@
|
|||||||
// Hi! If you're reading this, you probably want to know what this program does
|
// Hi! If you're reading this, you probably want to know what this program does
|
||||||
// To that, I say: Good luck and I hope you have patience and a strong will to
|
// To that, I say: Good luck and I hope you have patience and a strong will to
|
||||||
// live, 'cause both negatively impacted by trying to read the code below.
|
// live, 'cause both will be negatively impacted by trying to read the code
|
||||||
|
// below.
|
||||||
// This isn't a joke; I feel like someone telling a person to get off the edge
|
// This isn't a joke; I feel like someone telling a person to get off the edge
|
||||||
// of a tall building here: reading the code below WILL negatively impact your
|
// of a tall building here: reading the code below WILL negatively impact your
|
||||||
// life. You have been warned.
|
// life. You have been warned.
|
||||||
|
|
||||||
#define LIST_START 0xE0
|
#define LIST_START 0xE0
|
||||||
#define LIST_END 0xFE
|
#define LIST_END 0x00
|
||||||
#define LIST_START_MODIFIED 0xDF
|
#define HIGHEST_BUCKET 0x78
|
||||||
#define LIST_END_MODIFIED 0xFD
|
|
||||||
#define HASH_MASK 0b1111000
|
|
||||||
#define NEGATIVE_START 0x40
|
|
||||||
#define NEGATIVE_END 0x78
|
|
||||||
#define POSITIVE_START 0x00
|
|
||||||
#define POSITIVE_END 0x38
|
|
||||||
#define BUCKET_SIZE 8
|
#define BUCKET_SIZE 8
|
||||||
#define BUCKET_INDEX_TRACKER 0b11111000
|
#define BUCKET_INDEX_TRACKER 0b11111000
|
||||||
|
|
||||||
@ -56,178 +51,118 @@
|
|||||||
#data 0x78 0x00 // F (NEGATIVE)
|
#data 0x78 0x00 // F (NEGATIVE)
|
||||||
|
|
||||||
|
|
||||||
// Shift list down by one address, freeing up 0xFF for random-access use
|
// Set all GR-registers to -1, so that we always can call "sub gr" to increment
|
||||||
reset asr
|
// AR, no matter what value we have in IR ;)
|
||||||
mov pm hr
|
const 0x100
|
||||||
|
mov ar ir
|
||||||
const LIST_START_MODIFIED
|
reset gr
|
||||||
mov ar asr
|
reset grm
|
||||||
mov hr pm
|
const 0xB00
|
||||||
|
mov ar ir
|
||||||
|
reset gr
|
||||||
|
reset grm
|
||||||
|
|
||||||
// Initialize PC to point at list
|
// Initialize PC to point at list
|
||||||
//const LIST_START
|
const LIST_START
|
||||||
mov ar pc
|
mov ar pc
|
||||||
|
|
||||||
|
|
||||||
$BUCKET_SORT_START
|
$BUCKET_SORT_START
|
||||||
|
|
||||||
// AR = PM[PC]
|
// First value to be sorted
|
||||||
// HR = PM[PC + 1]
|
|
||||||
mov pc asr; incpc
|
mov pc asr; incpc
|
||||||
mov pm ir; call @JTABLE
|
mov pm ir; call @JTABLE
|
||||||
|
|
||||||
mov pc asr
|
mov pm pc; mov pm lc
|
||||||
mov pm hr
|
sub gr; incpc
|
||||||
|
|
||||||
// Hash HR, partially hash AR.
|
|
||||||
// Result of HR-hash is in AR
|
|
||||||
// Result of AR-hash is in HR
|
|
||||||
irl; mov ar gr // PM[0xFF] = AR
|
|
||||||
irl; reset asr
|
|
||||||
irl; mov gr pm
|
|
||||||
irl; mov pc asr // Set GR to pre-hash value of HR
|
|
||||||
irl; mov pm gr
|
|
||||||
irl
|
|
||||||
irl
|
|
||||||
and HASH_MASK
|
|
||||||
|
|
||||||
mov ar asr
|
|
||||||
mov pm lc; mov pm pc
|
|
||||||
incpc
|
|
||||||
mov pc pm
|
mov pc pm
|
||||||
mov ar pc
|
mov ar pc
|
||||||
|
|
||||||
$FIRST_INSERTION
|
$FIRST_INSERTION
|
||||||
|
mov pc asr; bls @FIRST_INSERTION_END_BIGGEST
|
||||||
incpc; bls @FIRST_INSERTION_END_BIGGEST
|
|
||||||
|
|
||||||
mov pc asr
|
|
||||||
mov pm ar
|
mov pm ar
|
||||||
sub gr
|
sub ir
|
||||||
adn gr; brn @FIRST_INSERTION_BOTTOM
|
adn ir; brn @FIRST_INSERTION_BOTTOM
|
||||||
|
|
||||||
mov gr pm; incpc
|
mov ir pm; incpc
|
||||||
|
|
||||||
$FIRST_INSERTION_SHIFT
|
$FIRST_INSERTION_SHIFT
|
||||||
mov pc asr; bls @FIRST_INSERTION_END_NOTBIGGEST
|
mov pc asr; bls @FIRST_INSERTION_END_NOTBIGGEST
|
||||||
mov pm gr
|
mov pm ir
|
||||||
mov ar pm
|
mov ar pm
|
||||||
mov gr ar; declc; incpc; bra @FIRST_INSERTION_SHIFT
|
mov ir ar; declc; incpc; bra @FIRST_INSERTION_SHIFT
|
||||||
|
|
||||||
$FIRST_INSERTION_BOTTOM
|
$FIRST_INSERTION_BOTTOM
|
||||||
declc; bra @FIRST_INSERTION
|
declc; incpc; bra @FIRST_INSERTION
|
||||||
|
|
||||||
$FIRST_INSERTION_END_BIGGEST
|
$FIRST_INSERTION_END_BIGGEST
|
||||||
mov pc asr
|
mov ir pm
|
||||||
mov gr pm
|
|
||||||
|
|
||||||
$FIRST_INSERTION_END_NOTBIGGEST
|
$FIRST_INSERTION_END_NOTBIGGEST
|
||||||
|
|
||||||
// Prepare second insertion (minimal bookkeeping)
|
|
||||||
mov hr ar
|
|
||||||
and HASH_MASK
|
// Second value to be sorted
|
||||||
mov ar asr
|
mov hr pc
|
||||||
|
mov pc asr; incpc
|
||||||
|
mov pm ir; call @JTABLE
|
||||||
mov pm pc; mov pm lc
|
mov pm pc; mov pm lc
|
||||||
incpc; reset asr
|
sub gr; incpc
|
||||||
mov pm gr
|
|
||||||
mov ar asr
|
|
||||||
mov pc pm
|
mov pc pm
|
||||||
mov ar pc
|
mov ar pc
|
||||||
|
|
||||||
|
|
||||||
$SECOND_INSERTION
|
$SECOND_INSERTION
|
||||||
|
mov pc asr; bls @SECOND_INSERTION_END_BIGGEST
|
||||||
incpc; bls @SECOND_INSERTION_END_BIGGEST
|
|
||||||
|
|
||||||
mov pc asr
|
|
||||||
mov pm ar
|
mov pm ar
|
||||||
sub gr
|
sub ir
|
||||||
adn gr; brn @SECOND_INSERTION_BOTTOM
|
adn ir; brn @SECOND_INSERTION_BOTTOM
|
||||||
|
|
||||||
mov gr pm; incpc
|
mov ir pm; incpc
|
||||||
|
|
||||||
$SECOND_INSERTION_SHIFT
|
$SECOND_INSERTION_SHIFT
|
||||||
mov pc asr; bls @SECOND_INSERTION_END_NOTBIGGEST
|
mov pc asr; bls @SECOND_INSERTION_END_NOTBIGGEST
|
||||||
mov pm gr
|
mov pm ir
|
||||||
mov ar pm
|
mov ar pm
|
||||||
mov gr ar; declc; incpc; bra @SECOND_INSERTION_SHIFT
|
mov ir ar; declc; incpc; bra @SECOND_INSERTION_SHIFT
|
||||||
|
|
||||||
$SECOND_INSERTION_BOTTOM
|
$SECOND_INSERTION_BOTTOM
|
||||||
declc; bra @SECOND_INSERTION
|
declc; incpc; bra @SECOND_INSERTION
|
||||||
|
|
||||||
$SECOND_INSERTION_END_BIGGEST
|
$SECOND_INSERTION_END_BIGGEST
|
||||||
mov pc asr
|
mov ir pm
|
||||||
mov gr pm
|
|
||||||
|
|
||||||
$SECOND_INSERTION_END_NOTBIGGEST
|
$SECOND_INSERTION_END_NOTBIGGEST
|
||||||
mov ir ar; mov ir pc
|
mov hr ar; mov hr pc
|
||||||
sub LIST_END_MODIFIED
|
sub LIST_END
|
||||||
brz @BUCKET_SORT_END; incpc
|
bnz @BUCKET_SORT_START
|
||||||
bra @BUCKET_SORT_START; incpc
|
|
||||||
|
|
||||||
$BUCKET_SORT_END
|
$BUCKET_SORT_END
|
||||||
|
|
||||||
|
const BUCKET_SIZE
|
||||||
//call @BREAK
|
|
||||||
|
|
||||||
|
|
||||||
// Merge negative values
|
|
||||||
const NEGATIVE_END
|
|
||||||
mov ar hr
|
mov ar hr
|
||||||
const NEGATIVE_START
|
const HIGHEST_BUCKET
|
||||||
mov ar pc
|
mov ar pc
|
||||||
const LIST_START
|
const LIST_START
|
||||||
|
|
||||||
$NEGATIVE_MERGE
|
|
||||||
mov pc ir
|
$MERGE
|
||||||
mov pc asr; incpc
|
mov pc asr; incpc
|
||||||
mov pm lc
|
mov pm lc
|
||||||
$NEGATIVE_MERGE_MOVE
|
|
||||||
mov pc asr; bls @NEGATIVE_MERGE_BOTTOM
|
$MERGE_MOVE
|
||||||
|
mov pc asr; bls @MERGE_BOTTOM
|
||||||
mov pm gr
|
mov pm gr
|
||||||
mov ar asr
|
mov ar asr
|
||||||
add 1
|
add 1
|
||||||
mov gr pm; declc; incpc; bra @NEGATIVE_MERGE_MOVE
|
mov gr pm; declc; incpc; bra @MERGE_MOVE
|
||||||
|
|
||||||
$NEGATIVE_MERGE_BOTTOM
|
$MERGE_BOTTOM
|
||||||
mov ar gr
|
|
||||||
mov ir ar
|
|
||||||
sub hr
|
|
||||||
adn hr; brz @POSITIVE_MERGE_INIT
|
|
||||||
add BUCKET_SIZE
|
|
||||||
mov ar pc
|
|
||||||
mov gr ar; bra @NEGATIVE_MERGE
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Merge positive values
|
|
||||||
$POSITIVE_MERGE_INIT
|
|
||||||
const POSITIVE_END
|
|
||||||
mov ar hr
|
|
||||||
const POSITIVE_START
|
|
||||||
mov ar pc
|
|
||||||
mov gr ar // Pop AR
|
|
||||||
|
|
||||||
$POSITIVE_MERGE
|
|
||||||
mov pc asr; incpc
|
|
||||||
mov pm lc
|
|
||||||
$POSITIVE_MERGE_MOVE
|
|
||||||
mov pc asr; bls @POSITIVE_MERGE_BOTTOM
|
|
||||||
mov pm gr
|
|
||||||
mov ar asr
|
|
||||||
add 1
|
|
||||||
mov gr pm; declc; incpc; bra @POSITIVE_MERGE_MOVE
|
|
||||||
|
|
||||||
$POSITIVE_MERGE_BOTTOM
|
|
||||||
mov ar gr
|
mov ar gr
|
||||||
mov pc ar
|
mov pc ar
|
||||||
and BUCKET_INDEX_TRACKER
|
and BUCKET_INDEX_TRACKER
|
||||||
sub hr
|
sub hr; brz @PROGRAM_END
|
||||||
adn hr; brz @PROGRAM_END
|
|
||||||
adn BUCKET_SIZE
|
|
||||||
mov ar pc
|
mov ar pc
|
||||||
mov gr ar; bra @POSITIVE_MERGE
|
mov gr ar; bra @MERGE
|
||||||
|
|
||||||
|
|
||||||
$PROGRAM_END
|
$PROGRAM_END
|
||||||
@ -239,70 +174,70 @@ halt
|
|||||||
|
|
||||||
// Jump-table subroutine
|
// Jump-table subroutine
|
||||||
$JTABLE
|
$JTABLE
|
||||||
mov pm hr; bop
|
mov pc hr; bop
|
||||||
|
|
||||||
|
|
||||||
// IR OP-field jump table
|
// IR OP-field jump table
|
||||||
$OT_0
|
$OT_0
|
||||||
const 0x70
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_1
|
|
||||||
const 0x7e
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_2
|
|
||||||
const 0x8c
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_3
|
|
||||||
const 0x9a
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_4
|
|
||||||
const 0xa8
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_5
|
|
||||||
const 0xb6
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_6
|
|
||||||
const 0xc4
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_7
|
|
||||||
const 0xd2
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_8
|
|
||||||
const 0x00
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_9
|
|
||||||
const 0x0e
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_A
|
|
||||||
const 0x1c
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_B
|
|
||||||
const 0x2a
|
|
||||||
mov ar asr; ret
|
|
||||||
|
|
||||||
$OT_C
|
|
||||||
const 0x38
|
const 0x38
|
||||||
mov ar asr; ret
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_1
|
||||||
|
const 0x30
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_2
|
||||||
|
const 0x28
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_3
|
||||||
|
const 0x20
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_4
|
||||||
|
const 0x18
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_5
|
||||||
|
const 0x10
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_6
|
||||||
|
const 0x08
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_7
|
||||||
|
const 0x00
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_8
|
||||||
|
const 0x78
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_9
|
||||||
|
const 0x70
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_A
|
||||||
|
const 0x68
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_B
|
||||||
|
const 0x60
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
|
$OT_C
|
||||||
|
const 0x58
|
||||||
|
mov ar asr; ret
|
||||||
|
|
||||||
$OT_D
|
$OT_D
|
||||||
const 0x46
|
const 0x50
|
||||||
mov ar asr; ret
|
mov ar asr; ret
|
||||||
|
|
||||||
$OT_E
|
$OT_E
|
||||||
const 0x54
|
const 0x48
|
||||||
mov ar asr; ret
|
mov ar asr; ret
|
||||||
|
|
||||||
$OT_F
|
$OT_F
|
||||||
const 0x62
|
const 0x40
|
||||||
mov ar asr; ret
|
mov ar asr; ret
|
||||||
|
Loading…
x
Reference in New Issue
Block a user