forked from sampsyo/bril
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sampsyo#276 from AliceSzzze/main
Added quicksort as a benchmark
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# An implementation of Quicksort using the Lomuto partition scheme, adapted from the pseudocode on Wikipedia | ||
# Input: an array of length 6 | ||
# Output: the input array sorted in ascending order | ||
# Adopted two helper functions, pack and print_array, from Jiajie Li's bubble sort benchmark | ||
|
||
# ARGS: 94 21 5 6 82 46 | ||
@main(n1: int, n2: int, n3: int, n4: int, n5: int, n6:int) { | ||
size: int = const 6; | ||
array: ptr<int> = call @pack size n1 n2 n3 n4 n5 n6; | ||
one: int = const 1; | ||
size_minus_one: int = sub size one; | ||
|
||
zero: int = const 0; | ||
call @qsort array zero size_minus_one; | ||
|
||
# Print array | ||
call @print_array array size; | ||
|
||
free array; | ||
} | ||
|
||
@qsort(array : ptr<int>, l: int, r:int) { | ||
# if l >= r or r < 0, return | ||
l_ge_r: bool = ge l r; | ||
zero: int = const 0; | ||
neg_r: bool = lt r zero; | ||
ret_cond: bool = or l_ge_r neg_r; | ||
|
||
br ret_cond .done .continue; | ||
|
||
.continue: | ||
p: int = call @partition array l r; | ||
one: int = const 1; | ||
p_minus_one: int = sub p one; | ||
p_plus_one: int = add p one; | ||
|
||
call @qsort array l p_minus_one; | ||
call @qsort array p_plus_one r; | ||
|
||
.done: | ||
ret; | ||
} | ||
|
||
@partition(array : ptr<int>, l: int, r:int) : int { | ||
# choose the last element as the pivot | ||
pivot_loc: ptr<int> = ptradd array r; | ||
pivot: int = load pivot_loc; | ||
one: int = const 1; | ||
i:int = sub l one; | ||
j:int = id i; | ||
|
||
.loop.init: | ||
j:int = add j one; | ||
cond: bool = lt j r; | ||
br cond .body .post.loop; | ||
|
||
.body: | ||
j_loc: ptr<int> = ptradd array j; | ||
a_j: int = load j_loc; | ||
swap_cond: bool = le a_j pivot; | ||
br swap_cond .swap .loop.init; | ||
|
||
.swap: | ||
# increment i by 1 | ||
i: int = add i one; | ||
|
||
# swap the current element with the element at the temporary pivot index i | ||
i_loc: ptr<int> = ptradd array i; | ||
a_i: int = load i_loc; | ||
store j_loc a_i; | ||
store i_loc a_j; | ||
jmp .loop.init; | ||
|
||
.post.loop: | ||
i:int = add i one; | ||
i_loc: ptr<int> = ptradd array i; | ||
a_i: int = load i_loc; | ||
store i_loc pivot; | ||
store pivot_loc a_i; | ||
ret i; | ||
} | ||
|
||
# allocate an array of the given size and pack the input values into the array | ||
@pack(size: int, n1: int, n2: int, n3: int, n4: int, n5: int, n6: int) : ptr<int> { | ||
one: int = const 1; | ||
i: int = const 0; | ||
array: ptr<int> = alloc size; | ||
# Pack data into array manually. Cannot use loop because of the different var name. | ||
loc: ptr<int> = ptradd array i; | ||
store loc n1; | ||
i: int = add i one; | ||
loc: ptr<int> = ptradd array i; | ||
store loc n2; | ||
i: int = add i one; | ||
loc: ptr<int> = ptradd array i; | ||
store loc n3; | ||
i: int = add i one; | ||
loc: ptr<int> = ptradd array i; | ||
store loc n4; | ||
i: int = add i one; | ||
loc: ptr<int> = ptradd array i; | ||
store loc n5; | ||
i: int = add i one; | ||
loc: ptr<int> = ptradd array i; | ||
store loc n6; | ||
|
||
ret array; | ||
} | ||
|
||
@print_array(array: ptr<int>, size: int) { | ||
i: int = const 0; | ||
one: int = const 1; | ||
.loop: | ||
cond: bool = lt i size; | ||
br cond .body .done; | ||
.body: | ||
loc: ptr<int> = ptradd array i; | ||
val: int = load loc; | ||
print val; | ||
.loop_end: | ||
i: int = add i one; | ||
jmp .loop; | ||
.done: | ||
ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
5 | ||
6 | ||
21 | ||
46 | ||
82 | ||
94 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
total_dyn_inst: 264 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters