Skip to content

Commit

Permalink
Merge pull request #268 from obhalerao/main
Browse files Browse the repository at this point in the history
Add Euler's totient function benchmark
  • Loading branch information
sampsyo authored Aug 31, 2023
2 parents a17acfd + f5ca8c5 commit d170c79
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
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 @@ -55,6 +55,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.
* `vsmul`: Multiplies a constant scalar to each element of a large array. Tests the performance of vectorization optimizations.
* `reverse`: Compute number with reversed digits (e.g. 123 -> 321).
Expand Down Expand Up @@ -87,3 +88,4 @@ Credit for several of these benchmarks goes to Alexa VanHattum and Gregory Yaune
[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)
[totient]: https://en.wikipedia.org/wiki/Euler's_totient_function

0 comments on commit d170c79

Please sign in to comment.