From 70af3d045fab131429c78ab94f5c4b5113b2d010 Mon Sep 17 00:00:00 2001 From: Chenxin Fang Date: Tue, 29 Aug 2023 19:10:46 -0400 Subject: [PATCH 1/9] reverse_int --- benchmarks/core/reverse.bril | 31 +++++++++++++++++++++++++++++++ benchmarks/core/reverse.out | 1 + benchmarks/core/reverse.prof | 1 + 3 files changed, 33 insertions(+) create mode 100644 benchmarks/core/reverse.bril create mode 100644 benchmarks/core/reverse.out create mode 100644 benchmarks/core/reverse.prof diff --git a/benchmarks/core/reverse.bril b/benchmarks/core/reverse.bril new file mode 100644 index 000000000..2a9c5ee62 --- /dev/null +++ b/benchmarks/core/reverse.bril @@ -0,0 +1,31 @@ + +@main{ + input: int = const 123; + 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; +} + diff --git a/benchmarks/core/reverse.out b/benchmarks/core/reverse.out new file mode 100644 index 000000000..3ae0b938f --- /dev/null +++ b/benchmarks/core/reverse.out @@ -0,0 +1 @@ +321 diff --git a/benchmarks/core/reverse.prof b/benchmarks/core/reverse.prof new file mode 100644 index 000000000..3ccbf1820 --- /dev/null +++ b/benchmarks/core/reverse.prof @@ -0,0 +1 @@ +total_dyn_inst: 47 From 0d2f423cec902068b76fa6d0e4905a13c75963bc Mon Sep 17 00:00:00 2001 From: bcarlet <8906114+bcarlet@users.noreply.github.com> Date: Tue, 29 Aug 2023 23:08:24 -0400 Subject: [PATCH 2/9] Add Hanoi benchmark --- benchmarks/core/hanoi.bril | 26 ++++++++++++++++++++++++++ benchmarks/core/hanoi.out | 7 +++++++ benchmarks/core/hanoi.prof | 1 + docs/tools/bench.md | 2 ++ 4 files changed, 36 insertions(+) create mode 100644 benchmarks/core/hanoi.bril create mode 100644 benchmarks/core/hanoi.out create mode 100644 benchmarks/core/hanoi.prof diff --git a/benchmarks/core/hanoi.bril b/benchmarks/core/hanoi.bril new file mode 100644 index 000000000..c32df92d5 --- /dev/null +++ b/benchmarks/core/hanoi.bril @@ -0,0 +1,26 @@ +# Three-disk Tower of Hanoi puzzle. +# +# Moves are printed 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 (disk: int, src: int, dst: int, spare: int) { + zero: int = const 0; + nonneg: bool = ge disk zero; + br nonneg .then .else; +.then: + one: int = const 1; + above: int = sub disk one; + call @hanoi above src spare dst; + print src dst; + call @hanoi above spare dst src; +.else: + ret; +} + +@main { + disk: int = const 2; + src: int = const 0; + dst: int = const 2; + spare: int = const 1; + call @hanoi disk src dst spare; +} diff --git a/benchmarks/core/hanoi.out b/benchmarks/core/hanoi.out new file mode 100644 index 000000000..57c7c1bc4 --- /dev/null +++ b/benchmarks/core/hanoi.out @@ -0,0 +1,7 @@ +0 2 +0 1 +2 1 +0 2 +1 0 +1 2 +0 2 diff --git a/benchmarks/core/hanoi.prof b/benchmarks/core/hanoi.prof new file mode 100644 index 000000000..a02ef5fe2 --- /dev/null +++ b/benchmarks/core/hanoi.prof @@ -0,0 +1 @@ +total_dyn_inst: 100 diff --git a/docs/tools/bench.md b/docs/tools/bench.md index 4cbcf3906..bc1602c58 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -28,6 +28,7 @@ The current benchmarks are: * `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 3-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. @@ -78,3 +79,4 @@ 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 From 0fc724720f727fa570315a3ee8ce52bcd656314a Mon Sep 17 00:00:00 2001 From: Chenxin Fang Date: Wed, 30 Aug 2023 10:11:03 -0400 Subject: [PATCH 3/9] use args --- benchmarks/core/reverse.bril | 5 ++--- benchmarks/core/reverse.prof | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/benchmarks/core/reverse.bril b/benchmarks/core/reverse.bril index 2a9c5ee62..14543250f 100644 --- a/benchmarks/core/reverse.bril +++ b/benchmarks/core/reverse.bril @@ -1,6 +1,5 @@ - -@main{ - input: int = const 123; +# ARGS: 123 +@main (input: int){ n: int = id input; v0: int = const 0; v1: int = const 10; diff --git a/benchmarks/core/reverse.prof b/benchmarks/core/reverse.prof index 3ccbf1820..b8bbeb828 100644 --- a/benchmarks/core/reverse.prof +++ b/benchmarks/core/reverse.prof @@ -1 +1 @@ -total_dyn_inst: 47 +total_dyn_inst: 46 From 69f8e57afce4f28b836e7e04fdcd281b3782c14a Mon Sep 17 00:00:00 2001 From: Chenxin Fang Date: Wed, 30 Aug 2023 10:15:39 -0400 Subject: [PATCH 4/9] edit doc --- docs/tools/bench.md | 96 ++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/docs/tools/bench.md b/docs/tools/bench.md index 4cbcf3906..213ad5a66 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -1,57 +1,57 @@ -Benchmarks -========== +# Benchmarks The `bench` directory in the Bril repository contains a fledgling suite of microbenchmarks that you can use to measure the impact of your optimizations. (Benchmarks are different from tests because they are meant to actually calculate something instead of just exercising a language feature.) The current benchmarks are: -* `ackermann`: Print the value of `Ack(m, n)`, the [two-argument Ackermann–Péter function][ackermann]. -* `adj2csr`: Convert a graph in [adjacency matrix][adj] format (dense representation) to [Compressed Sparse Row (CSR)][csr] format (sparse representation). The random graph is generated using the same [linear congruential generator][rng]. -* `adler32`: Computes the [Adler-32 Checksum][adler32] of an integer array. -* `armstrong`: Determines if the input is an [Armstrong number][armstrong], a number that is the sum of its own digits each raised to the power of the number of digits. -* `binary-fmt`: Print the binary format for the given positive integer. -* `binary-search`: Search a target integer within an integer array, outputs the index of target. -* `bitwise-ops`: Computes the OR, AND, or XOR between two 64-bit integers. (Three modes: 0 = AND, 1 = OR, 2 = XOR) -* `bubblesort`: Sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. -* `catalan`: Print the *n*th term in the [Catalan][catalan] sequence, compute using recursive function calls. -* `check-primes`: Check the first *n* natural numbers for primality, printing out a 1 if the number is prime and a 0 if it's not. -* `cholesky`: Perform Cholesky decomposition of a Hermitian and positive definite matrix. The result is validated by comparing with Python's `scipy.linalg.cholesky`. -* `collatz`: Print the [Collatz][collatz] sequence starting at *n*. Note: it is not known whether this will terminate for all *n*. -* `cordic`: Print an approximation of sine(radians) using 8 iterations of the [CORDIC algorithm](https://en.wikipedia.org/wiki/CORDIC). -* `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. -* `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]. -* `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. -* `mat-mul`: Multiplies two `nxn` matrices using the [naive][matmul] matrix multiplication algorithm. The matrices are randomly generated using a [linear congruential generator][rng]. -* `max-subarray`: solution to the classic Maximum Subarray problem. -* `newton`: Calculate the square root of 99,999 using the [newton method][newton] -* `n_root`: Calculate nth root of a float using newton's method. -* `orders`: Compute the order ord(u) for each u in a cyclic group [][cgroup] of integers modulo *n* under the group operation + (modulo *n*). Set the second argument *is_lcm* to true if you would like to compute the orders using the lowest common multiple and otherwise the program will use the greatest common divisor. -* `pascals-row`: Computes a row in Pascal's Triangle. -* `perfect`: Check if input argument is a perfect number. Returns output as Unix style return code. -* `pow`: Computes the n^th power of a given (float) number. -* `primes-between`: Print the primes in the interval `[a, b]`. -* `pythagorean_triple`: Prints all Pythagorean triples with the given c, if such triples exist. An intentionally very naive implementation. -* `quadratic`: The [quadratic formula][qf], including a hand-rolled implementation of square root. -* `recfact`: Compute *n!* using recursive function calls. -* `rectangles-area-difference`: Output the difference between the areas of rectangles (as a positive value) given their respective side lengths. -* `relative-primes`: Print all numbers relatively prime to *n* using [Euclidean algorithm][euclidean_into]. -* `riemann`: Prints the left, midpoint, and right [Riemann][riemann] Sums for a specified function, which is the square function in this benchmark. -* `sieve`: Print all prime numbers up to *n* using the [Sieve of Eratosthenes][sievee]. -* `sqrt`: Implements the [Newton–Raphson Method][newton] of approximating the square root of a number to arbitrary precision -* `sum-bit`: Print the number of 1-bits in the binary representation of the input integer. -* `sum-divisors`: Prints the positive integer divisors of the input integer, followed by the sum of the divisors. -* `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. +- `ackermann`: Print the value of `Ack(m, n)`, the [two-argument Ackermann–Péter function][ackermann]. +- `adj2csr`: Convert a graph in [adjacency matrix][adj] format (dense representation) to [Compressed Sparse Row (CSR)][csr] format (sparse representation). The random graph is generated using the same [linear congruential generator][rng]. +- `adler32`: Computes the [Adler-32 Checksum][adler32] of an integer array. +- `armstrong`: Determines if the input is an [Armstrong number][armstrong], a number that is the sum of its own digits each raised to the power of the number of digits. +- `binary-fmt`: Print the binary format for the given positive integer. +- `binary-search`: Search a target integer within an integer array, outputs the index of target. +- `bitwise-ops`: Computes the OR, AND, or XOR between two 64-bit integers. (Three modes: 0 = AND, 1 = OR, 2 = XOR) +- `bubblesort`: Sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. +- `catalan`: Print the *n*th term in the [Catalan][catalan] sequence, compute using recursive function calls. +- `check-primes`: Check the first _n_ natural numbers for primality, printing out a 1 if the number is prime and a 0 if it's not. +- `cholesky`: Perform Cholesky decomposition of a Hermitian and positive definite matrix. The result is validated by comparing with Python's `scipy.linalg.cholesky`. +- `collatz`: Print the [Collatz][collatz] sequence starting at _n_. Note: it is not known whether this will terminate for all _n_. +- `cordic`: Print an approximation of sine(radians) using 8 iterations of the [CORDIC algorithm](https://en.wikipedia.org/wiki/CORDIC). +- `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. +- `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]. +- `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. +- `mat-mul`: Multiplies two `nxn` matrices using the [naive][matmul] matrix multiplication algorithm. The matrices are randomly generated using a [linear congruential generator][rng]. +- `max-subarray`: solution to the classic Maximum Subarray problem. +- `newton`: Calculate the square root of 99,999 using the [newton method][newton] +- `n_root`: Calculate nth root of a float using newton's method. +- `orders`: Compute the order ord(u) for each u in a cyclic group [][cgroup] of integers modulo _n_ under the group operation + (modulo _n_). Set the second argument _is_lcm_ to true if you would like to compute the orders using the lowest common multiple and otherwise the program will use the greatest common divisor. +- `pascals-row`: Computes a row in Pascal's Triangle. +- `perfect`: Check if input argument is a perfect number. Returns output as Unix style return code. +- `pow`: Computes the n^th power of a given (float) number. +- `primes-between`: Print the primes in the interval `[a, b]`. +- `pythagorean_triple`: Prints all Pythagorean triples with the given c, if such triples exist. An intentionally very naive implementation. +- `quadratic`: The [quadratic formula][qf], including a hand-rolled implementation of square root. +- `recfact`: Compute _n!_ using recursive function calls. +- `rectangles-area-difference`: Output the difference between the areas of rectangles (as a positive value) given their respective side lengths. +- `relative-primes`: Print all numbers relatively prime to _n_ using [Euclidean algorithm][euclidean_into]. +- `riemann`: Prints the left, midpoint, and right [Riemann][riemann] Sums for a specified function, which is the square function in this benchmark. +- `sieve`: Print all prime numbers up to _n_ using the [Sieve of Eratosthenes][sievee]. +- `sqrt`: Implements the [Newton–Raphson Method][newton] of approximating the square root of a number to arbitrary precision +- `sum-bit`: Print the number of 1-bits in the binary representation of the input integer. +- `sum-divisors`: Prints the positive integer divisors of the input integer, followed by the sum of the divisors. +- `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. +- `reverse`: Print 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]. From cf2495fcec5605de23b37cefe041b16c58839a47 Mon Sep 17 00:00:00 2001 From: Kei Imada <> Date: Tue, 29 Aug 2023 20:27:46 -0400 Subject: [PATCH 5/9] add vim swap files to gitignore --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 256cccf67..3587e3dac 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,8 @@ __pycache__ .vscode **/*.o -.DS_Store \ No newline at end of file +.DS_Store + +# vim swap files +*.swp +*.swo From a5870627b3aa27abc65fabe77a1e336d7887c7f7 Mon Sep 17 00:00:00 2001 From: Kei Imada <> Date: Tue, 29 Aug 2023 20:38:16 -0400 Subject: [PATCH 6/9] euler benchmark Approximates Euler's number using the Taylor series --- benchmarks/float/euler.bril | 83 +++++++++++++++++++++++++++++++++++++ benchmarks/float/euler.out | 1 + benchmarks/float/euler.prof | 1 + 3 files changed, 85 insertions(+) create mode 100644 benchmarks/float/euler.bril create mode 100644 benchmarks/float/euler.out create mode 100644 benchmarks/float/euler.prof diff --git a/benchmarks/float/euler.bril b/benchmarks/float/euler.bril new file mode 100644 index 000000000..e50ea264a --- /dev/null +++ b/benchmarks/float/euler.bril @@ -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; +# } diff --git a/benchmarks/float/euler.out b/benchmarks/float/euler.out new file mode 100644 index 000000000..fe0cc23e6 --- /dev/null +++ b/benchmarks/float/euler.out @@ -0,0 +1 @@ +2.71828182845904553 diff --git a/benchmarks/float/euler.prof b/benchmarks/float/euler.prof new file mode 100644 index 000000000..569421c3a --- /dev/null +++ b/benchmarks/float/euler.prof @@ -0,0 +1 @@ +total_dyn_inst: 1908 From 34d851f41c14be9254983024d5476aa5dab447cd Mon Sep 17 00:00:00 2001 From: Kei Imada <> Date: Tue, 29 Aug 2023 20:46:39 -0400 Subject: [PATCH 7/9] euler benchmark documentation --- docs/tools/bench.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/tools/bench.md b/docs/tools/bench.md index 95445b416..764d76568 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -21,6 +21,7 @@ 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. @@ -77,3 +78,4 @@ 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 +[euler]: https://en.wikipedia.org/wiki/E_(mathematical_constant) From 24f2cc5d82fd220aeda7464c49b8af28fca037cc Mon Sep 17 00:00:00 2001 From: bcarlet <8906114+bcarlet@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:52:59 -0400 Subject: [PATCH 8/9] Generalize to n disks --- benchmarks/core/hanoi.bril | 21 +++++++++++---------- benchmarks/core/hanoi.prof | 2 +- docs/tools/bench.md | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/benchmarks/core/hanoi.bril b/benchmarks/core/hanoi.bril index c32df92d5..f60e5389b 100644 --- a/benchmarks/core/hanoi.bril +++ b/benchmarks/core/hanoi.bril @@ -1,15 +1,16 @@ -# Three-disk Tower of Hanoi puzzle. +# Tower of Hanoi puzzle. # -# Moves are printed 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`. +# 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 (disk: int, src: int, dst: int, spare: int) { +@hanoi (disks: int, src: int, dst: int, spare: int) { zero: int = const 0; - nonneg: bool = ge disk zero; - br nonneg .then .else; + pos: bool = gt disks zero; + br pos .then .else; .then: one: int = const 1; - above: int = sub disk one; + above: int = sub disks one; call @hanoi above src spare dst; print src dst; call @hanoi above spare dst src; @@ -17,10 +18,10 @@ ret; } -@main { - disk: int = const 2; +# ARGS: 3 +@main (disks: int) { src: int = const 0; dst: int = const 2; spare: int = const 1; - call @hanoi disk src dst spare; + call @hanoi disks src dst spare; } diff --git a/benchmarks/core/hanoi.prof b/benchmarks/core/hanoi.prof index a02ef5fe2..b50ba70bd 100644 --- a/benchmarks/core/hanoi.prof +++ b/benchmarks/core/hanoi.prof @@ -1 +1 @@ -total_dyn_inst: 100 +total_dyn_inst: 99 diff --git a/docs/tools/bench.md b/docs/tools/bench.md index bc1602c58..cac28a6d6 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -28,7 +28,7 @@ The current benchmarks are: * `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 3-disk [Tower of Hanoi][hanoi] puzzle. +* `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. From 13549351d93280cd8a7746c984db31a654b7bca2 Mon Sep 17 00:00:00 2001 From: Chenxin Fang Date: Wed, 30 Aug 2023 12:20:43 -0400 Subject: [PATCH 9/9] fix format --- docs/tools/bench.md | 97 +++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/docs/tools/bench.md b/docs/tools/bench.md index 213ad5a66..a7e090176 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -1,57 +1,58 @@ -# Benchmarks +Benchmarks +========== The `bench` directory in the Bril repository contains a fledgling suite of microbenchmarks that you can use to measure the impact of your optimizations. (Benchmarks are different from tests because they are meant to actually calculate something instead of just exercising a language feature.) The current benchmarks are: -- `ackermann`: Print the value of `Ack(m, n)`, the [two-argument Ackermann–Péter function][ackermann]. -- `adj2csr`: Convert a graph in [adjacency matrix][adj] format (dense representation) to [Compressed Sparse Row (CSR)][csr] format (sparse representation). The random graph is generated using the same [linear congruential generator][rng]. -- `adler32`: Computes the [Adler-32 Checksum][adler32] of an integer array. -- `armstrong`: Determines if the input is an [Armstrong number][armstrong], a number that is the sum of its own digits each raised to the power of the number of digits. -- `binary-fmt`: Print the binary format for the given positive integer. -- `binary-search`: Search a target integer within an integer array, outputs the index of target. -- `bitwise-ops`: Computes the OR, AND, or XOR between two 64-bit integers. (Three modes: 0 = AND, 1 = OR, 2 = XOR) -- `bubblesort`: Sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. -- `catalan`: Print the *n*th term in the [Catalan][catalan] sequence, compute using recursive function calls. -- `check-primes`: Check the first _n_ natural numbers for primality, printing out a 1 if the number is prime and a 0 if it's not. -- `cholesky`: Perform Cholesky decomposition of a Hermitian and positive definite matrix. The result is validated by comparing with Python's `scipy.linalg.cholesky`. -- `collatz`: Print the [Collatz][collatz] sequence starting at _n_. Note: it is not known whether this will terminate for all _n_. -- `cordic`: Print an approximation of sine(radians) using 8 iterations of the [CORDIC algorithm](https://en.wikipedia.org/wiki/CORDIC). -- `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. -- `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]. -- `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. -- `mat-mul`: Multiplies two `nxn` matrices using the [naive][matmul] matrix multiplication algorithm. The matrices are randomly generated using a [linear congruential generator][rng]. -- `max-subarray`: solution to the classic Maximum Subarray problem. -- `newton`: Calculate the square root of 99,999 using the [newton method][newton] -- `n_root`: Calculate nth root of a float using newton's method. -- `orders`: Compute the order ord(u) for each u in a cyclic group [][cgroup] of integers modulo _n_ under the group operation + (modulo _n_). Set the second argument _is_lcm_ to true if you would like to compute the orders using the lowest common multiple and otherwise the program will use the greatest common divisor. -- `pascals-row`: Computes a row in Pascal's Triangle. -- `perfect`: Check if input argument is a perfect number. Returns output as Unix style return code. -- `pow`: Computes the n^th power of a given (float) number. -- `primes-between`: Print the primes in the interval `[a, b]`. -- `pythagorean_triple`: Prints all Pythagorean triples with the given c, if such triples exist. An intentionally very naive implementation. -- `quadratic`: The [quadratic formula][qf], including a hand-rolled implementation of square root. -- `recfact`: Compute _n!_ using recursive function calls. -- `rectangles-area-difference`: Output the difference between the areas of rectangles (as a positive value) given their respective side lengths. -- `relative-primes`: Print all numbers relatively prime to _n_ using [Euclidean algorithm][euclidean_into]. -- `riemann`: Prints the left, midpoint, and right [Riemann][riemann] Sums for a specified function, which is the square function in this benchmark. -- `sieve`: Print all prime numbers up to _n_ using the [Sieve of Eratosthenes][sievee]. -- `sqrt`: Implements the [Newton–Raphson Method][newton] of approximating the square root of a number to arbitrary precision -- `sum-bit`: Print the number of 1-bits in the binary representation of the input integer. -- `sum-divisors`: Prints the positive integer divisors of the input integer, followed by the sum of the divisors. -- `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. -- `reverse`: Print number with reversed digits (e.g. 123 -> 321). +* `ackermann`: Print the value of `Ack(m, n)`, the [two-argument Ackermann–Péter function][ackermann]. +* `adj2csr`: Convert a graph in [adjacency matrix][adj] format (dense representation) to [Compressed Sparse Row (CSR)][csr] format (sparse representation). The random graph is generated using the same [linear congruential generator][rng]. +* `adler32`: Computes the [Adler-32 Checksum][adler32] of an integer array. +* `armstrong`: Determines if the input is an [Armstrong number][armstrong], a number that is the sum of its own digits each raised to the power of the number of digits. +* `binary-fmt`: Print the binary format for the given positive integer. +* `binary-search`: Search a target integer within an integer array, outputs the index of target. +* `bitwise-ops`: Computes the OR, AND, or XOR between two 64-bit integers. (Three modes: 0 = AND, 1 = OR, 2 = XOR) +* `bubblesort`: Sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. +* `catalan`: Print the *n*th term in the [Catalan][catalan] sequence, compute using recursive function calls. +* `check-primes`: Check the first *n* natural numbers for primality, printing out a 1 if the number is prime and a 0 if it's not. +* `cholesky`: Perform Cholesky decomposition of a Hermitian and positive definite matrix. The result is validated by comparing with Python's `scipy.linalg.cholesky`. +* `collatz`: Print the [Collatz][collatz] sequence starting at *n*. Note: it is not known whether this will terminate for all *n*. +* `cordic`: Print an approximation of sine(radians) using 8 iterations of the [CORDIC algorithm](https://en.wikipedia.org/wiki/CORDIC). +* `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. +* `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]. +* `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. +* `mat-mul`: Multiplies two `nxn` matrices using the [naive][matmul] matrix multiplication algorithm. The matrices are randomly generated using a [linear congruential generator][rng]. +* `max-subarray`: solution to the classic Maximum Subarray problem. +* `newton`: Calculate the square root of 99,999 using the [newton method][newton] +* `n_root`: Calculate nth root of a float using newton's method. +* `orders`: Compute the order ord(u) for each u in a cyclic group [][cgroup] of integers modulo *n* under the group operation + (modulo *n*). Set the second argument *is_lcm* to true if you would like to compute the orders using the lowest common multiple and otherwise the program will use the greatest common divisor. +* `pascals-row`: Computes a row in Pascal's Triangle. +* `perfect`: Check if input argument is a perfect number. Returns output as Unix style return code. +* `pow`: Computes the n^th power of a given (float) number. +* `primes-between`: Print the primes in the interval `[a, b]`. +* `pythagorean_triple`: Prints all Pythagorean triples with the given c, if such triples exist. An intentionally very naive implementation. +* `quadratic`: The [quadratic formula][qf], including a hand-rolled implementation of square root. +* `recfact`: Compute *n!* using recursive function calls. +* `rectangles-area-difference`: Output the difference between the areas of rectangles (as a positive value) given their respective side lengths. +* `relative-primes`: Print all numbers relatively prime to *n* using [Euclidean algorithm][euclidean_into]. +* `riemann`: Prints the left, midpoint, and right [Riemann][riemann] Sums for a specified function, which is the square function in this benchmark. +* `sieve`: Print all prime numbers up to *n* using the [Sieve of Eratosthenes][sievee]. +* `sqrt`: Implements the [Newton–Raphson Method][newton] of approximating the square root of a number to arbitrary precision +* `sum-bit`: Print the number of 1-bits in the binary representation of the input integer. +* `sum-divisors`: Prints the positive integer divisors of the input integer, followed by the sum of the divisors. +* `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. +* `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].