上一篇文章分治法(Divide and Conquer)以 Leading Zero Count 为例解释了分治法带来的好处,本篇文章再举一个类似的例子。
Count Number of Ones,计算一个32-bit输入中 1 的个数。
For 循环
always_comb begin
count = 0;
for (int i=0; i<32; i++) begin
count = count + data_i[i];
end
end
分治法
第一级:6-3 compressor (需要三个LUT6)
第二级:ternary adder
第三级:ternary adder
logic [4:0][2:0] temp1;
logic [1:0] temp1_1;
always_comb
for (int i=0; i<5; i++) begin
case(data_i[i*6 +: 6])
6'b000000 : temp1[i] = 0;
6'b000001 : temp1[i] = 1;
6'b000010 : temp1[i] = 1;
6'b000011 : temp1[i] = 2;
...
6'b111111 : temp1[i] = 6;
endcase
end
end
assign temp1_1 = data_i[30] + data_i[31]:
logic [1:0][4:0] temp2;
always_comb begin
temp2[0] = temp1[0] + temp1[1] + temp[2];
temp2[1] = temp1[3] + temp1[4] + temp[5];
end
logic [5:0] count;
assign count = temp2[0] + temp2[1] + temp1_1;
综合结果对比
| WNS | Logic Levels | Num of LUTs | |
|---|---|---|---|
| For loop | 8.496 | 5 | 34 |
| Divide and Conquer | 8.718 | 4 | 29 |
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !