Skip to content

Commit

Permalink
Merge pull request #286 from Arthur-Chang016/main
Browse files Browse the repository at this point in the history
Adding sum-check benchmark
  • Loading branch information
sampsyo committed Sep 7, 2023
2 parents 0301e69 + 09753c9 commit 43895fc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
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 @@ -64,6 +64,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

0 comments on commit 43895fc

Please sign in to comment.