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

Add integer-based std_pow #399

Merged
merged 15 commits into from
Feb 20, 2021
52 changes: 51 additions & 1 deletion primitives/bitnum/math.futil
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
extern "math.sv" {
primitive std_sqrt(in: 32, go: 1, clk: 1) -> (out: 32, done: 1);
}
}

// Computes the unsigned value b^e, where
// b is the `base` and e is the `exp`.
component std_pow(base: 32, exp: 32) -> (out: 32) {
cells {
pow = std_reg(32);
count = std_reg(32);
mul = std_mult(32);
lt = std_lt(32);
incr = std_add(32);
}
wires {
group init {
pow.in = 32'd1;
pow.write_en = 1'd1;
count.in = 32'd0;
count.write_en = 1'd1;
init[done] = pow.done & count.done ? 1'd1;
}
group do_mul {
mul.left = base;
mul.right = pow.out;
pow.in = mul.out;
pow.write_en = 1'd1;
do_mul[done] = pow.done;
}
group incr_count {
incr.left = 32'd1;
incr.right = count.out;
count.in = incr.out;
count.write_en = 1'd1;
incr_count[done] = count.done;
}
group cond {
lt.right = exp;
lt.left = count.out;
cond[done] = 1'd1;
}

out = pow.out;
}
control {
seq {
init;
while lt.out with cond {
par { do_mul; incr_count; }
}
}
}
}
20 changes: 0 additions & 20 deletions primitives/bitnum/unsigned.sv
Original file line number Diff line number Diff line change
Expand Up @@ -274,23 +274,3 @@ module std_mod #(
);
assign out = left % right;
endmodule

module std_exp (
input logic [31:0] exponent,
input logic go,
input logic clk,
output logic [31:0] out,
output logic done
);
always_ff @(posedge clk) begin
if (go) begin
// XXX: This is a hilariously bad approximation
/* verilator lint_off REALCVT */
out <= /* 2.718281 */ 3 ** exponent;
done <= 1;
end else begin
out <= 0;
done <= 0;
end
end
endmodule
11 changes: 11 additions & 0 deletions tests/correctness/pow.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"base": [
2
],
"exp": [
10
],
"out": [
1024
]
}
36 changes: 36 additions & 0 deletions tests/correctness/pow.futil
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import "primitives/std.lib";
import "primitives/bitnum/math.futil";
component main() -> () {
cells {
out = std_mem_d1(32, 1, 1);
base = std_mem_d1(32, 1, 1);
exp = std_mem_d1(32, 1, 1);
base_reg = std_reg(32);
exp_reg = std_reg(32);
std_pow0 = std_pow();
}
wires {
group init {
base.addr0 = 1'd0;
exp.addr0 = 1'd0;
base_reg.in = base.read_data;
exp_reg.in = exp.read_data;
base_reg.write_en = 1'd1;
exp_reg.write_en = 1'd1;
init[done] = base_reg.done & exp_reg.done ? 1'd1;
}
group fill_memory {
out.write_data = std_pow0.out;
out.addr0 = 1'd0;
out.write_en = 1'd1;
fill_memory[done] = out.done;
}
}
control {
seq {
init;
invoke std_pow0(base=base_reg.out, exp=exp_reg.out)();
fill_memory;
}
}
}
20 changes: 20 additions & 0 deletions tests/correctness/pow.futil.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"out": {
"data": [
0
],
"bitwidth": 32
},
"base": {
"data": [
2
],
"bitwidth": 32
},
"exp": {
"data": [
10
],
"bitwidth": 32
}
}