From d02c3b35b2f627fd3c06a1f6b3844c3abbc6c338 Mon Sep 17 00:00:00 2001 From: Jiahan Xie <765130715@qq.com> Date: Thu, 31 Aug 2023 18:46:24 -0400 Subject: [PATCH 1/2] bril program for finding the majority element --- benchmarks/mem/major_elm.bril | 73 +++++++++++++++++++++++++++++++++++ benchmarks/mem/major_elm.out | 1 + benchmarks/mem/major_elm.prof | 1 + 3 files changed, 75 insertions(+) create mode 100644 benchmarks/mem/major_elm.bril create mode 100644 benchmarks/mem/major_elm.out create mode 100644 benchmarks/mem/major_elm.prof diff --git a/benchmarks/mem/major_elm.bril b/benchmarks/mem/major_elm.bril new file mode 100644 index 000000000..53ca810ab --- /dev/null +++ b/benchmarks/mem/major_elm.bril @@ -0,0 +1,73 @@ +# Return the majority element (appears more than floor(n/2) times) of an array, assuming that the majority element is guaranteed to exist. +# Inputs: an array of size 3 (fixed); +# Output: the majority element of the array. +# Not a big example in terms of the array size and the total dynamic instructions; but I found Boyer-Moore voting algorithm very interesting! +# Acknowledgement: brought idea from the pack function in Pat-Lafon's binary-search.bril to my create_arr. +@main() { + arr_size: int = const 3; + e1: int = const 3; + e2: int = const 2; + e3: int = const 3; + nums: ptr = call @create_arr arr_size e1 e2 e3; + + zero: int = const 0; + one: int = const 1; + first_elm_ptr: ptr = ptradd nums zero; + major_elm: int = load first_elm_ptr; + count: int = const 1; + + i: int = const 1; + .check_bound: + end_cond: bool = ge i arr_size; + br end_cond .end .body; + + .body: + cur_ptr: ptr = ptradd nums i; + cur_val: int = load cur_ptr; + cur_major_cond: bool = eq cur_val major_elm; + br cur_major_cond .incr_count .body.else; + + .incr_count: + count: int = add count one; + i: int = add i one; + jmp .check_bound; + + .body.else: + cnt_eq_0: bool = eq count zero; + br cnt_eq_0 .eq_zero_if .eq_zero_else; + + .eq_zero_if: + cur_ptr: ptr = ptradd nums i; + major_elm: int = load cur_ptr; + count: int = add count one; + i: int = add i one; + jmp .check_bound; + + .eq_zero_else: + count: int = sub count one; + i: int = add i one; + jmp .check_bound; + + .end: + free nums; + print major_elm; +} + +@create_arr(size: int, e1: int, e2: int, e3: int): ptr { + one: int = const 1; + i: int = const 0; + array: ptr = alloc size; + + loc: ptr = ptradd array i; + store loc e1; + + i: int = add i one; + loc: ptr = ptradd array i; + store loc e2; + + i: int = add i one; + loc: ptr = ptradd array i; + store loc e3; + + ret array; +} diff --git a/benchmarks/mem/major_elm.out b/benchmarks/mem/major_elm.out new file mode 100644 index 000000000..00750edc0 --- /dev/null +++ b/benchmarks/mem/major_elm.out @@ -0,0 +1 @@ +3 diff --git a/benchmarks/mem/major_elm.prof b/benchmarks/mem/major_elm.prof new file mode 100644 index 000000000..3ccbf1820 --- /dev/null +++ b/benchmarks/mem/major_elm.prof @@ -0,0 +1 @@ +total_dyn_inst: 47 From 262ea101adf94691d76c9a2a34fd07b0fff13af5 Mon Sep 17 00:00:00 2001 From: Jiahan Xie <765130715@qq.com> Date: Thu, 31 Aug 2023 18:55:18 -0400 Subject: [PATCH 2/2] replace underscore with dash for my bril program's name, to be coherent with others; mention major-elm in the docs --- benchmarks/mem/{major_elm.bril => major-elm.bril} | 0 benchmarks/mem/{major_elm.out => major-elm.out} | 0 benchmarks/mem/{major_elm.prof => major-elm.prof} | 0 docs/tools/bench.md | 1 + 4 files changed, 1 insertion(+) rename benchmarks/mem/{major_elm.bril => major-elm.bril} (100%) rename benchmarks/mem/{major_elm.out => major-elm.out} (100%) rename benchmarks/mem/{major_elm.prof => major-elm.prof} (100%) diff --git a/benchmarks/mem/major_elm.bril b/benchmarks/mem/major-elm.bril similarity index 100% rename from benchmarks/mem/major_elm.bril rename to benchmarks/mem/major-elm.bril diff --git a/benchmarks/mem/major_elm.out b/benchmarks/mem/major-elm.out similarity index 100% rename from benchmarks/mem/major_elm.out rename to benchmarks/mem/major-elm.out diff --git a/benchmarks/mem/major_elm.prof b/benchmarks/mem/major-elm.prof similarity index 100% rename from benchmarks/mem/major_elm.prof rename to benchmarks/mem/major-elm.prof diff --git a/docs/tools/bench.md b/docs/tools/bench.md index bbce89d1f..73fb1927e 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -34,6 +34,7 @@ The current benchmarks are: * `gcd`: Calculate Greatest Common Divisor (GCD) of two input positive integer using [Euclidean algorithm][euclidean_into]. * `hanoi`: Print the solution to the *n*-disk [Tower of Hanoi][hanoi] puzzle. * `loopfact`: Compute *n!* imperatively using a loop. +* `major-elm`: Find the majority element in an array using [a linear time voting algorithm](https://www.cs.utexas.edu/~moore/best-ideas/mjrty/). * `mandelbrot`: Generates a really low resolution, ascii, [mandelbrot set][mandelbrot]. * `mat-inv` : Calculates the inverse of a 3x3 matrix and prints it out. * `mat-mul`: Multiplies two `nxn` matrices using the [naive][matmul] matrix multiplication algorithm. The matrices are randomly generated using a [linear congruential generator][rng].