Skip to content

Commit

Permalink
Merge pull request #267 from alifarahbakhsh/palindrome
Browse files Browse the repository at this point in the history
Added palindrome benchmark
  • Loading branch information
sampsyo committed Aug 31, 2023
2 parents 38c867f + f6bf765 commit a17acfd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
80 changes: 80 additions & 0 deletions benchmarks/core/palindrome.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# ARGS: 12321
@main(in: int) {
#in: int = const 2343553432;
ten: int = const 10;
zero: int = const 0;
one: int = const 1;
index: int = const 1;
not_finished: bool = const true;
.for.cond:
br not_finished .for.body .for.end;
.for.body:
power: int = call @pow ten index;
d: int = div in power;
check: bool = eq d zero;
br check .if.true .if.false;
.if.true:
not_finished: bool = const false;
jmp .for.cond;
.if.false:
index: int = add index one;
jmp .for.cond;
.for.end:
exp: int = sub index one;
is_palindrome: bool = call @palindrome in exp;
print is_palindrome;
}

@pow(base: int, exp: int): int {
res: int = const 1;
zero: int = const 0;
one: int = const 1;
not_finished: bool = const true;
.for.cond.pow:
br not_finished .for.body.pow .for.end.pow;
.for.body.pow:
finished: bool = eq exp zero;
br finished .if.true.pow .if.false.pow;
.if.true.pow:
not_finished: bool = const false;
jmp .for.cond.pow;
.if.false.pow:
res: int = mul res base;
exp: int = sub exp one;
jmp .for.cond.pow;
.for.end.pow:
ret res;
}

@palindrome(in: int, len: int): bool {
is_palindrome: bool = const false;
zero: int = const 0;
two: int = const 2;
ten: int = const 10;
check: bool = le len zero;
br check .if.true.palindrome .if.false.palindrome;
.if.true.palindrome:
is_palindrome: bool = const true;
jmp .if.end.palindrome;
.if.false.palindrome:
power: int = call @pow ten len;
left: int = div in power;
v1: int = div in ten;
v2: int = mul v1 ten;
right: int = sub in v2;
is_equal: bool = eq left right;
br is_equal .if.true.mirror .if.false.mirror;
.if.true.mirror:
temp: int = mul power left;
temp: int = sub in temp;
temp: int = sub temp right;
next_in: int = div temp ten;
next_len: int = sub len two;
is_palindrome: bool = call @palindrome next_in next_len;
jmp .if.end.palindrome;
.if.false.mirror:
is_palindrome: bool = const false;
jmp .if.end.palindrome;
.if.end.palindrome:
ret is_palindrome;
}
1 change: 1 addition & 0 deletions benchmarks/core/palindrome.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions benchmarks/core/palindrome.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 298
2 changes: 2 additions & 0 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The current benchmarks are:
* `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 [<Zn,+>][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.
* `palindrome`: Outputs a 0-1 value indicating whether the input is a [palindrome][palindrome] number.
* `perfect`: Check if input argument is a perfect number. Returns output as Unix style return code.
* `pow`: Computes the n^<sup>th</sup> power of a given (float) number.
* `primes-between`: Print the primes in the interval `[a, b]`.
Expand Down Expand Up @@ -83,5 +84,6 @@ 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
[palindrome]: https://en.wikipedia.org/wiki/Palindrome
[hanoi]: https://en.wikipedia.org/wiki/Tower_of_Hanoi
[euler]: https://en.wikipedia.org/wiki/E_(mathematical_constant)

0 comments on commit a17acfd

Please sign in to comment.