各位大侠,帮看看这个有什么好的算法
有个2bit的hash值,现在要用这个hash值确定1的位置,比如说hash值为1,就是找出7bit中从低位开始看,第二个1的位置,哪位大侠有什么好的方法呀,硬件要比较容易实现,谢谢!
1:7位每位和1与操作,结果进入移位寄存器
2:每个时钟周期移出一位,并进行累加
3:累加结果同hash值相等时,对应的时钟周期数(周期计数)是对应1的位置
这样行不行:
举个例子:
1. 输入,0101110,hash=4
2. 计算每位前出现1的个数,得到0112344(共14bit)
3. 前面结果的每一个(2bit)与hash数做异或,如hash=4,则得000011
4. 优先decoder,的到位置=6
你只用看7bit数的低4位,然后用这低4位做输入,搞个4到2的查找表,就成了。
我贴一个我的方案,看看能不能对你有帮助:
module exam(
x,
hash,
y
);
input [6:0] x;
input [1:0] hash;
output [2:0] y;
reg [2:0] y;
wire [6:0] a;
assign a = x;
wire [2:0] y0;
assign y0 = get(a);
wire [6:0] p;
assign p = 1'b1<<y0;
wire [6:0] b;
assign b = a & ~p;
wire [2:0] y1;
assign y1 = get(b);
wire [6:0] q;
assign q = 1'b1<<y1;
wire [6:0] c;
assign c = b & ~q;
wire [2:0] y2;
assign y2 = get(c);
wire [6:0] r;
assign r = 1'b1<<y2;
wire [6:0] d;
assign d = c & ~r;
wire [2:0] y3;
assign y3 = get(d);
always @ ( hash or y0 or y1 or y2 or y3 )
case(hash)
2'h0 : y = y0;
2'h1 : y = y1;
2'h2 : y = y2;
2'h3 : y = y3;
endcase
function [2:0] get;
input [6:0] t;
begin
if ( t[0] )
get = 3'd0;
else if ( t[1] )
get = 3'd1;
else if ( t[2] )
get = 3'd2;
else if ( t[3] )
get = 3'd3;
else if ( t[4] )
get = 3'd4;
else if ( t[5] )
get = 3'd5;
else
get = 3'd6;
end
endfunction
endmodule
还有一个更优的方案:
module exam(
x,
hash,
y
);
input [6:0] x;
input [1:0] hash;
output [2:0] y;
reg [2:0] y;
wire [6:0] a;
assign a = x;
wire [2:0] y0;
assign y0 = get_lsb(a);
wire [6:0] p;
assign p = 1'b1<<y0;
wire [6:0] b;
assign b = a & ~p;
wire [2:0] y1;
assign y1 = get_lsb(b);
wire [6:0] d;
assign d = x;
wire [2:0] y3;
assign y3 = get_msb(d);
wire [6:0] q;
assign q = 1'b1<<y3;
wire [6:0] c;
assign c = d & ~q;
wire [2:0] y2;
assign y2 = get_msb(c);
always @ ( hash or y0 or y1 or y2 or y3 )
case(hash)
2'h0 : y = y0;
2'h1 : y = y1;
2'h2 : y = y2;
2'h3 : y = y3;
endcase
function [2:0] get_lsb;
input [6:0] t;
begin
if ( t[0] )
get_lsb = 3'd0;
else if ( t[1] )
get_lsb = 3'd1;
else if ( t[2] )
get_lsb = 3'd2;
else if ( t[3] )
get_lsb = 3'd3;
else if ( t[4] )
get_lsb = 3'd4;
else if ( t[5] )
get_lsb = 3'd5;
else
get_lsb = 3'd6;
end
endfunction
function [2:0] get_msb;
input [6:0] t;
begin
if ( t[6] )
get_msb = 3'd6;
else if ( t[5] )
get_msb = 3'd5;
else if ( t[4] )
get_msb = 3'd4;
else if ( t[3] )
get_msb = 3'd3;
else if ( t[2] )
get_msb = 3'd2;
else if ( t[1] )
get_msb = 3'd1;
else
get_msb = 3'd0;
end
endfunction
endmodule