unsigned int ones32(unsigned int x)
{
x -= ((x >> 1) & 0x55555555);
x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
x = (((x >> 4) + x) & 0x0f0f0f0f);
x += (x >> 8);
x += (x >> 16);
return(x & 0x0000003f);
}
module top (clk, in, ones);
input clk;
input [31:0] in;
reg [31:0] d;
output reg [5:0] ones;
always @ (posedge clk) begin
d <= in;
ones <= (((d[ 0] + d[ 1] + d[ 2] + d[ 3]) + (d[ 4] + d[ 5] + d[ 6] + d[ 7]))
+ ((d[ 8] + d[ 9] + d[10] + d[11]) + (d[12] + d[13] + d[14] + d[15])))
+ (((d[16] + d[17] + d[18] + d[19]) + (d[20] + d[21] + d[22] + d[23]))
+ ((d[24] + d[25] + d[26] + d[27]) + (d[28] + d[29] + d[30] + d[31])));
end
endmodule
module top (clk, in, ones);
input clk;
input [31:0] in;
reg [31:0] d;
reg [2:0] a[0:7];
reg [3:0] b[0:3];
reg [4:0] c[0:1];
output reg [5:0] ones;
always @ (posedge clk) begin
d <= in;
a[0] <= d[ 0] + d[ 1] + d[ 2] + d[ 3];
a[1] <= d[ 4] + d[ 5] + d[ 6] + d[ 7];
a[2] <= d[ 8] + d[ 9] + d[10] + d[11];
a[3] <= d[12] + d[13] + d[14] + d[15];
a[4] <= d[16] + d[17] + d[18] + d[19];
a[5] <= d[20] + d[21] + d[22] + d[23];
a[6] <= d[24] + d[25] + d[26] + d[27];
a[7] <= d[28] + d[29] + d[30] + d[31];
b[0] <= a[0] + a[1];
b[1] <= a[2] + a[3];
b[2] <= a[4] + a[5];
b[3] <= a[6] + a[7];
c[0] <= b[0] + b[1];
c[1] <= b[2] + b[3];
ones <= c[0] + c[1];
end
endmodule
unsigned int ones32(unsigned int num)
{
return num - (num<<1) - (num<<2) - (num<<3) - (num<<4)
- (num<<5) - (num<<6) - (num<<7) - (num<<8)
- (num<<9) - (num<<10) - (num<<11) - (num<<12)
- (num<<13) - (num<<14) - (num<<15) - (num<<16)
- (num<<17) - (num<<18) - (num<<19) - (num<<20)
- (num<<21) - (num<<22) - (num<<23) - (num<<24)
- (num<<25) - (num<<26) - (num<<27) - (num<<28)
- (num<<29) - (num<<30) - (num<<31);
}
smileysam said:can't we use a for loop and then use an if statement and increment the counter on finding 1??
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?