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

Adding sum-check benchmark #286

Merged
merged 2 commits into from
Sep 7, 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
38 changes: 38 additions & 0 deletions benchmarks/core/sum-check.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# compute the sum of [1, n] by both loop and formula
# and compare them to see if the result is the same

# ARGS: 1000
@main(n: int) {
first: int = call @sum_by_loop n;
second: int = call @sum_by_formula n;
isSame: bool = eq first second;
print first;
print second;
print isSame;
}

@sum_by_loop(n: int): int {
one: int = const 1;
sum: int = const 0;
i: int = const 1;

.for_start:
con: bool = le i n;
br con .for .end;
.for:
sum: int = add sum i;
i: int = add i one;
jmp .for_start;
.end:
ret sum;
}

# sum = (1 + n) * n / 2
@sum_by_formula(n: int): int {
one: int = const 1;
two: int = const 2;
n_1: int = add one n;
multi: int = mul n_1 n;
sum: int = div multi two;
ret sum;
}
3 changes: 3 additions & 0 deletions benchmarks/core/sum-check.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
500500
500500
true
1 change: 1 addition & 0 deletions benchmarks/core/sum-check.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 5018
1 change: 1 addition & 0 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The current benchmarks are:
* `sieve`: Print all prime numbers up to *n* using the [Sieve of Eratosthenes][sievee].
* `sqrt`: Implements the [Newton–Raphson Method][newton] of approximating the square root of a number to arbitrary precision
* `sum-bit`: Print the number of 1-bits in the binary representation of the input integer.
* `sum-check`: Compute the sum of [1, n] by both loop and formula, and check if the result is the same.
* `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*.
Expand Down