Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Euler's totient function benchmark #268

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions benchmarks/core/totient.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# ARGS: 2023
@main (n: int) {
print n;
tot: int = call @totient n;
print tot;
}

@totient (n: int): int {
result: int = id n;
p: int = const 2;
one: int = const 1;
zero: int = const 0;

.for.set.cond:
pp: int = mul p p;
cond: bool = le pp n;
br cond .for.set.body .for.set.end;

.for.set.body:

npmod: int = call @mod n p;
if_cond: bool = eq npmod zero;
br if_cond .if_lbl .else_lbl;
.if_lbl:

.while.set.cond:

npmod: int = call @mod n p;
while_cond: bool = eq npmod zero;
br while_cond .while.body .while.end;

.while.body:
npdiv: int = div n p;
n: int = id npdiv;
jmp .while.set.cond;

.while.end:

resdiv: int = div result p;
result: int = sub result resdiv;

.else_lbl:

p: int = add p one;
jmp .for.set.cond;

.for.set.end:

final_if_cond: bool = gt n one;
br final_if_cond .final_if_label .final_else_label;

.final_if_label:
resdiv: int = div result n;
result: int = sub result resdiv;

.final_else_label:

ret result;
}

@mod (a: int, b: int): int {
ad: int = div a b;
mad: int = mul b ad;
ans: int = sub a mad;
ret ans;
}
2 changes: 2 additions & 0 deletions benchmarks/core/totient.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2023
1632
1 change: 1 addition & 0 deletions benchmarks/core/totient.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 253
2 changes: 2 additions & 0 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The current benchmarks are:
* `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.
* `totient`: Computes [Euler's totient function][totient] on an input integer *n*.
* `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).

Expand Down Expand Up @@ -84,3 +85,4 @@ Credit for several of these benchmarks goes to Alexa VanHattum and Gregory Yaune
[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)
[totient]: https://en.wikipedia.org/wiki/Euler's_totient_function