Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
he-andy authored Aug 30, 2023
2 parents f5a2209 + c8a6d32 commit d642bd5
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ __pycache__
.vscode

**/*.o
.DS_Store
.DS_Store

# vim swap files
*.swp
*.swo
27 changes: 27 additions & 0 deletions benchmarks/core/hanoi.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Tower of Hanoi puzzle.
#
# Input: Number of disks.
# Output: Each move in order, one on each line, where a move `src dst` indicates
# that the top disk from rod `src` should be moved to rod `dst`.

@hanoi (disks: int, src: int, dst: int, spare: int) {
zero: int = const 0;
pos: bool = gt disks zero;
br pos .then .else;
.then:
one: int = const 1;
above: int = sub disks one;
call @hanoi above src spare dst;
print src dst;
call @hanoi above spare dst src;
.else:
ret;
}

# ARGS: 3
@main (disks: int) {
src: int = const 0;
dst: int = const 2;
spare: int = const 1;
call @hanoi disks src dst spare;
}
7 changes: 7 additions & 0 deletions benchmarks/core/hanoi.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0 2
0 1
2 1
0 2
1 0
1 2
0 2
1 change: 1 addition & 0 deletions benchmarks/core/hanoi.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 99
30 changes: 30 additions & 0 deletions benchmarks/core/reverse.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ARGS: 123
@main (input: int){
n: int = id input;
v0: int = const 0;
v1: int = const 10;
result: int = id v0;
v2: bool = const true;
notdone: bool = id v2;
.for.cond.3:
v4: bool = id notdone;
br v4 .for.body.3 .for.end.3;
.for.body.3:
v5: int = id n;
a: int = div v5 v1;
floor: int = mul a v1;
remainder: int = sub v5 floor;
result: int = mul result v1;
result: int = add result remainder;
n: int = id a;
comp1: bool = eq n v0;
br comp1 .if.body .for.incre;
.if.body:
notdone: bool = const false;
jmp .for.cond.3;
.for.incre:
jmp .for.cond.3;
.for.end.3:
print result;
}

1 change: 1 addition & 0 deletions benchmarks/core/reverse.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
321
1 change: 1 addition & 0 deletions benchmarks/core/reverse.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 46
83 changes: 83 additions & 0 deletions benchmarks/float/euler.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Approximate Euler's number using the Taylor series

# ARGS: 18
@main(n: float) {
v0: float = id n;
e: float = call @taylor_series_euler v0;
e: float = id e;
v1: float = id e;
print v1;
v2: int = const 0;
}

@factorial(n: float): float {
v1: float = id n;
v2: float = const 1;
v3: bool = fle v1 v2;
br v3 .then.0 .else.0;
.then.0:
v4: float = const 1;
ret v4;
jmp .endif.0;
.else.0:
.endif.0:
v5: float = id n;
v6: float = const 1;
v7: float = fsub v5 v6;
v8: float = call @factorial v7;
v9: float = id n;
v10: float = fmul v8 v9;
ret v10;
}

@taylor_series_euler(n: float): float {
v0: float = const 0;
e: float = id v0;
v2: float = const 0;
i: float = id v2;
.for.cond.1:
v3: float = id i;
v4: float = id n;
v5: bool = flt v3 v4;
br v5 .for.body.1 .for.end.1;
.for.body.1:
v6: float = const 1;
v7: float = id i;
v8: float = call @factorial v7;
v9: float = fdiv v6 v8;
v10: float = id e;
v11: float = fadd v9 v10;
e: float = id v11;
v12: float = id i;
v13: float = const 1;
v14: float = fadd v12 v13;
i: float = id v14;
jmp .for.cond.1;
.for.end.1:
v15: float = id e;
ret v15;
}

# The Typescript file that generated the above code:
# ts2bril euler.ts | bril2txt > euler.bril

# function main(n: number) {
# var e: number = taylor_series_euler(n);
# console.log(e);
# }
#
# function factorial(n: number): number {
# if n <= 1 {
# return 1;
# }
# return factorial(n-1)*n;
# }
#
# function taylor_series_euler(n: number): number {
# var e: number = 0;
# var i: number;
# for(let i = 0; i < n; i=i+1) {
# e = 1 / factorial(i) + e;
# }
# return e;
# }
1 change: 1 addition & 0 deletions benchmarks/float/euler.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.71828182845904553
1 change: 1 addition & 0 deletions benchmarks/float/euler.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 1908
6 changes: 6 additions & 0 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ The current benchmarks are:
* `digial-root`: Computes the digital root of the input number.
* `eight-queens`: Counts the number of solutions for *n* queens problem, a generalization of [Eight queens puzzle][eight_queens].
* `euclid`: Calculates the greatest common divisor between two large numbers using the [Euclidean Algorithm][euclid] with a helper function for the modulo operator.
* `euler`: Approximates [Euler's number][euler] using the Taylor series.
* `fact`: Prints the factorial of *n*, computing it recursively.
* `factors`: Print the factors of the *n* using the [trial division][trialdivision] method.
* `fib`: Calculate the *n*th Fibonacci number by allocating and filling an [array](../lang/memory.md) of numbers up to that point.
* `fizz-buzz`: The infamous [programming test][fizzbuzz].
* `function_call`: For benchmarking the overhead of simple function calls.
* `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.
* `mandelbrot`: Generates a really low resolution, ascii, [mandelbrot set][mandelbrot].
* `mat-inv` : Calculates the inverse of a 3x3 matrix and prints it out.
Expand All @@ -53,6 +55,8 @@ The current benchmarks are:
* `sum-sq-diff`: Output the difference between the sum of the squares of the first *n* natural numbers and the square of their sum.
* `up-arrow`: Computes [Knuth's up arrow][uparrow] notation, with the first argument being the number, the second argument being the number of Knuth's up arrows, and the third argument being the number of repeats.
* `simd`: Multiplies a constant to each element of a large array. Tests the performance of vectorization optimizations.
* `reverse`: Compute number with reversed digits (e.g. 123 -> 321).


Credit for several of these benchmarks goes to Alexa VanHattum and Gregory Yauney, who implemented them for their [global value numbering project][gvnblog].

Expand All @@ -79,3 +83,5 @@ Credit for several of these benchmarks goes to Alexa VanHattum and Gregory Yaune
[uparrow]: https://en.wikipedia.org/wiki/Knuth%27s_up-arrow_notation
[riemann]: https://en.wikipedia.org/wiki/Riemann_sum
[mandelbrot]: https://en.wikipedia.org/wiki/Mandelbrot_set
[hanoi]: https://en.wikipedia.org/wiki/Tower_of_Hanoi
[euler]: https://en.wikipedia.org/wiki/E_(mathematical_constant)

0 comments on commit d642bd5

Please sign in to comment.