Skip to content

Commit

Permalink
Merge pull request #270 from BillXu2000/benchmark_inv
Browse files Browse the repository at this point in the history
Add modular multiplicative inverse benchmark
  • Loading branch information
sampsyo committed Aug 31, 2023
2 parents d170c79 + 8b03361 commit 05479b0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
90 changes: 90 additions & 0 deletions benchmarks/core/mod_inv.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# ARGS: 46, 10007
@main(n: int, p: int) {
v0: int = const 2;
two: int = id v0;
v1: int = id p;
v2: int = id two;
v3: int = sub v1 v2;
m: int = id v3;
v4: int = const 1;
ans: int = id v4;
v5: int = id n;
a: int = id v5;
v7: int = const 1;
i: int = id v7;
.for.cond.6:
v8: int = id m;
v9: int = const 0;
v10: bool = gt v8 v9;
br v10 .for.body.6 .for.end.6;
.for.body.6:
v12: int = id m;
v13: int = id m;
v14: int = id two;
v15: int = div v13 v14;
v16: int = id two;
v17: int = mul v15 v16;
v18: bool = eq v12 v17;
br v18 .then.11 .else.11;
.then.11:
jmp .endif.11;
.else.11:
v19: int = id ans;
v20: int = id a;
v21: int = mul v19 v20;
v22: int = id p;
v23: int = call @mod v21 v22;
ans: int = id v23;
.endif.11:
v24: int = id a;
v25: int = id a;
v26: int = mul v24 v25;
v27: int = id p;
v28: int = call @mod v26 v27;
a: int = id v28;
v29: int = id m;
v30: int = id two;
v31: int = div v29 v30;
m: int = id v31;
jmp .for.cond.6;
.for.end.6:
v32: int = id ans;
print v32;
v33: int = const 0;
}
@mod(n: int, p: int): int {
v0: int = id n;
v1: int = id n;
v2: int = id p;
v3: int = div v1 v2;
v4: int = id p;
v5: int = mul v3 v4;
v6: int = sub v0 v5;
ret v6;
}

# ts2bril inv.ts | bril2txt | tail +3 | sed -E 's/float/int/g' | sed -E 's/fgt/gt/g' > inv.bril
# generated by typescript code below:
#
# function main(n: bigint, p: bigint) {
# var two: bigint = 2;
# var m: bigint = p - two;
# var ans: bigint = 1;
# var a: bigint = n;
# for (let i = 1; m > 0; m = m / two) {
# if (m == m / two * two) {
# }
# else {
# ans = mod(ans * a, p);
# }
# a = mod(a * a, p);
# }
# console.log(ans);
# }
#
# function mod(n: bigint, p: bigint): bigint {
# return n - n / p * p;
# }
#


1 change: 1 addition & 0 deletions benchmarks/core/mod_inv.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2393
1 change: 1 addition & 0 deletions benchmarks/core/mod_inv.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 558
4 changes: 3 additions & 1 deletion docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The current benchmarks are:
* `mat-inv` : Calculates the inverse of a 3x3 matrix and prints it out.
* `mat-mul`: Multiplies two `nxn` matrices using the [naive][matmul] matrix multiplication algorithm. The matrices are randomly generated using a [linear congruential generator][rng].
* `max-subarray`: solution to the classic Maximum Subarray problem.
* `mod_inv`: Calculates the [modular inverse][modinv] of `n` under to a prime modulus p.
* `newton`: Calculate the square root of 99,999 using the [newton method][newton]
* `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 [<Zn,+>][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.
Expand Down Expand Up @@ -88,4 +89,5 @@ 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
[modinv]: https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
[totient]: https://en.wikipedia.org/wiki/Euler's_totient_function

0 comments on commit 05479b0

Please sign in to comment.