Optimize bucketsort for unrolling by converting it to a zero-loss subroutine

This commit is contained in:
Gabriel Tofvesson 2019-04-27 02:08:44 +02:00
parent 40d1031392
commit ac78950784

View File

@ -66,106 +66,105 @@ reset grm
const LIST_START const LIST_START
mov ar pc mov ar pc
$BUCKET_SORT_START $BUCKET_SORT_START
// First value to be sorted // First value to be sorted
mov pc asr; incpc mov pc asr; incpc
mov pm ir; call @JTABLE mov pm ir; call @JTABLE
mov pm pc; mov pm lc
sub gr; incpc
mov pc pm
mov ar pc
$FIRST_INSERTION
mov pc asr; bls @FIRST_INSERTION_END_BIGGEST
mov pm ar
sub ir
adn ir; brn @FIRST_INSERTION_BOTTOM
mov ir pm; incpc
$FIRST_INSERTION_SHIFT
mov pc asr; bls @FIRST_INSERTION_END_NOTBIGGEST
mov pm ir
mov ar pm
mov ir ar; declc; incpc; bra @FIRST_INSERTION_SHIFT
$FIRST_INSERTION_BOTTOM
declc; incpc; bra @FIRST_INSERTION
$FIRST_INSERTION_END_BIGGEST
mov ir pm
$FIRST_INSERTION_END_NOTBIGGEST
// Second value to be sorted // Second value to be sorted
mov hr pc
mov pc asr; incpc mov pc asr; incpc
mov pm ir; call @JTABLE mov pm ir; call @JTABLE
mov pm pc; mov pm lc
sub gr; incpc
mov pc pm
mov ar pc
$SECOND_INSERTION // Third value to be sorted
mov pc asr; bls @SECOND_INSERTION_END_BIGGEST mov pc asr; incpc
mov pm ar mov pm ir; call @JTABLE
sub ir
adn ir; brn @SECOND_INSERTION_BOTTOM
mov ir pm; incpc // Fourth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
$SECOND_INSERTION_SHIFT // Fifth value to be sorted
mov pc asr; bls @SECOND_INSERTION_END_NOTBIGGEST mov pc asr; incpc
mov pm ir mov pm ir; call @JTABLE
mov ar pm
mov ir ar; declc; incpc; bra @SECOND_INSERTION_SHIFT
$SECOND_INSERTION_BOTTOM // Sixth value to be sorted
declc; incpc; bra @SECOND_INSERTION mov pc asr; incpc
mov pm ir; call @JTABLE
$SECOND_INSERTION_END_BIGGEST // Seventh value to be sorted
mov ir pm mov pc asr; incpc
mov pm ir; call @JTABLE
// Eighth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
// Ninth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
// Tenth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
// Eleventh value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
// Twelfth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
// Thirteenth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
// Foureenth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
// Fifteenth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
// Sixteenth value to be sorted
mov pc asr; incpc
mov pm ir; call @JTABLE
$SECOND_INSERTION_END_NOTBIGGEST
mov hr ar; mov hr pc
sub LIST_END sub LIST_END
bnz @BUCKET_SORT_START bnz @BUCKET_SORT_START
$BUCKET_SORT_END
// Merge
const BUCKET_SIZE const BUCKET_SIZE
mov ar hr mov ar hr
const HIGHEST_BUCKET
mov ar pc
const LIST_START const LIST_START
mov ar pc
const HIGHEST_BUCKET
$MERGE $MERGE
mov pc asr; incpc // Load bucket size
mov ar asr
mov pm lc mov pm lc
// Copy elements to list
$MERGE_MOVE $MERGE_MOVE
mov pc asr; bls @MERGE_BOTTOM sub gr; bls @MERGE_BOTTOM
mov pm gr
mov ar asr mov ar asr
add 1 mov pm ir
mov gr pm; declc; incpc; bra @MERGE_MOVE mov pc asr; incpc
mov ir pm; declc; bra @MERGE_MOVE
// Check if we just copied the last bucket
$MERGE_BOTTOM $MERGE_BOTTOM
mov ar gr
mov pc ar
and BUCKET_INDEX_TRACKER and BUCKET_INDEX_TRACKER
sub hr; brz @PROGRAM_END sub hr; bnz @MERGE
mov ar pc
mov gr ar; bra @MERGE
$PROGRAM_END
$BREAK $BREAK
halt halt
@ -176,68 +175,99 @@ halt
$JTABLE $JTABLE
mov pc hr; bop mov pc hr; bop
// IR OP-field jump table // IR OP-field jump table
$OT_0 $OT_0
const 0x38 const 0x38
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_1 $OT_1
const 0x30 const 0x30
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_2 $OT_2
const 0x28 const 0x28
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_3 $OT_3
const 0x20 const 0x20
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_4 $OT_4
const 0x18 const 0x18
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_5 $OT_5
const 0x10 const 0x10
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_6 $OT_6
const 0x08 const 0x08
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_7 $OT_7
const 0x00 const 0x00
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_8 $OT_8
const 0x78 const 0x78
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_9 $OT_9
const 0x70 const 0x70
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_A $OT_A
const 0x68 const 0x68
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_B $OT_B
const 0x60 const 0x60
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_C $OT_C
const 0x58 const 0x58
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_D $OT_D
const 0x50 const 0x50
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_E $OT_E
const 0x48 const 0x48
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$OT_F $OT_F
const 0x40 const 0x40
mov ar asr; ret mov ar asr; bra @PREPARE_SORT
$PREPARE_SORT
mov pm pc; mov pm lc
sub gr; incpc
mov pc pm
mov ar pc
$INSERTION
mov pc asr; bls @INSERTION_END_BIGGEST
mov pm ar
sub ir
adn ir; brn @INSERTION_BOTTOM
mov ir pm; incpc
$INSERTION_SHIFT
mov pc asr; bls @INSERTION_END_NOTBIGGEST
mov pm ir
mov ar pm
mov ir ar; declc; incpc; bra @INSERTION_SHIFT
$INSERTION_BOTTOM
declc; incpc; bra @INSERTION
$INSERTION_END_BIGGEST
mov ir pm
$INSERTION_END_NOTBIGGEST
mov hr ar; mov hr pc; ret