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 1/2] 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 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 2/2] 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.