diff --git a/benchmarks/core/palindrome.bril b/benchmarks/core/palindrome.bril new file mode 100644 index 000000000..bbfcbdcb3 --- /dev/null +++ b/benchmarks/core/palindrome.bril @@ -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; +} \ No newline at end of file diff --git a/benchmarks/core/palindrome.out b/benchmarks/core/palindrome.out new file mode 100644 index 000000000..27ba77dda --- /dev/null +++ b/benchmarks/core/palindrome.out @@ -0,0 +1 @@ +true diff --git a/benchmarks/core/palindrome.prof b/benchmarks/core/palindrome.prof new file mode 100644 index 000000000..29ad635b8 --- /dev/null +++ b/benchmarks/core/palindrome.prof @@ -0,0 +1 @@ +total_dyn_inst: 298 diff --git a/docs/tools/bench.md b/docs/tools/bench.md index 949923543..db5b959f9 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -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 [][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^th power of a given (float) number. * `primes-between`: Print the primes in the interval `[a, b]`. @@ -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)