Skip to content

Commit

Permalink
fnv-hash benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
neel-patel-1 committed Jan 28, 2025
1 parent 2a149c9 commit 6c14ce3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
108 changes: 108 additions & 0 deletions benchmarks/mem/fnv1-hash.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
@main {
size: int = const 512;
arr: ptr<int> = alloc size;
call @fill_array arr size;
hash: int = call @fnv1a_hash arr size;
print hash;
free arr;
}

# FNV-1a hash function
@fnv1a_hash(data: ptr<int>, size: int): int {
FNV_prime: int = const 16777619;
FNV_offset_basis: int = const 2166136261;
hash: int = id FNV_offset_basis;
one: int = const 1;
i: int = const 0;
loc: ptr<int> = id data;
continue: bool = lt i size;
br continue .loop .exit;
.loop:
byte: int = load loc;
hash: int = call @XOR hash byte;
hash: int = mul hash FNV_prime;
loc: ptr<int> = ptradd loc one;
i: int = add i one;
continue: bool = lt i size;
br continue .loop .exit;
.exit:
ret hash;
}

# Fills the array with values 0, 1. 2, ... (from bril/benchmarks/mem/adler32.bril)
@fill_array(arr: ptr<int>, size:int) {
one: int = const 1;
curr: int = const 0;
loc: ptr<int> = id arr;
.loop:
store loc curr;
loc: ptr<int> = ptradd loc one;
curr: int = add curr one;
continue: bool = lt curr size;
br continue .loop .exit;
.exit:
nop;
}

# bitwise XOR (from bril/benchmarks/core/bitwise-ops.bril)
@XOR(a: int, b:int): int{
and_val: int = call @AND a b;
or_val: int = call @OR a b;
ans: int = sub or_val and_val;
ret ans;
}

@OR(a: int, b: int): int{
oper: bool = const true;
v1: int = call @loop_subroutine a b oper;
ret v1;
}

@AND(a: int, b: int): int{
oper: bool = const false;
v1: int = call @loop_subroutine a b oper;
ret v1;
}

@loop_subroutine(a: int, b: int, c: bool): int{
i: int = const 0;
n: int = const 63;
one: int = const 1;
two: int = const 2;
ans: int = const 0;
to_add: int = const 1;
.loop:
cond: bool = le i n;
br cond .here .end;
.here:
mod2a: bool = call @mod2 a;
mod2b: bool = call @mod2 b;
cond_add: bool = and mod2a mod2b;
br c .doOr .stay;
.doOr:
cond_add: bool = or mod2a mod2b;
.stay:
br cond_add .add .end_loop;
.add:
ans: int = add ans to_add;
.end_loop:
a: int = div a two;
b: int = div b two;
to_add: int = mul to_add two;
i: int = add i one;
jmp .loop;
.end:
ret ans;
}

@mod2(a: int): bool{ # if residue is 1 return true, else return false.
two: int = const 2;

tmp: int = div a two;
tmp2: int = mul tmp two;
tmp3: int = sub a tmp2;

one: int = const 1;
ans: bool = eq one tmp3;
ret ans;
}
1 change: 1 addition & 0 deletions benchmarks/mem/fnv1-hash.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8573363309
1 change: 1 addition & 0 deletions benchmarks/mem/fnv1-hash.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 1768440
1 change: 1 addition & 0 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The current benchmarks are:
* `factors`: Print the factors of the *n* using the [trial division][trialdivision] method.
* `fib`: Calculate the *n*th Fibonacci number by allocating and filling an [array](../lang/memory.md) of numbers up to that point.
* `fizz-buzz`: The infamous [programming test][fizzbuzz].
* `fnv1-hash`: Compute the [Fowler-Noll-Vo hash function](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) of an integer array.
* `function_call`: For benchmarking the overhead of simple function calls.
* `gcd`: Calculate Greatest Common Divisor (GCD) of two input positive integer using [Euclidean algorithm][euclidean_into].
* `hanoi`: Print the solution to the *n*-disk [Tower of Hanoi][hanoi] puzzle.
Expand Down

0 comments on commit 6c14ce3

Please sign in to comment.