Ones Counter

P

Paulo Valentim

Hello!

David Bishop wrote in this newsgroup a function which implemented a
recursive xor function. Please check out
http://groups.google.com/groups?hl=pt&lr=&safe=off&[email protected]

This worked beautifully in Synplify Pro 7.7. I tried extending the
idea to do a "Ones counter" using a generic binary adder structure.
The synthesis tool implements the function as if it was done in a FOR
loop and not recursively. I'm just curious as if somebody can
successfully use this function (get a binary adder tree) in another
synthesis tool or if there is a better way of doing this.
Thanks a lot. BTW, here is the code I adapted from Bishop's code.

function ones_count (arg : std_logic_vector )
return integer is
variable Upper, Lower : integer range 0 to VEC_SIZE;
variable Half : integer range 0 to VEC_SIZE;
variable BUS_int : std_logic_vector ( arg'length - 1 downto 0 );
variable Result : integer range 0 to VEC_SIZE;

begin
if (arg'LENGTH < 1) then -- In the case of a NULL
range
Result := 0;
else
BUS_int := to_ux01 (arg);
if ( BUS_int'length = 1 ) then
Result := conv_integer(BUS_int ( BUS_int'left ));
elsif ( BUS_int'length = 2 ) then
Result := conv_integer(BUS_int ( BUS_int'right )) +
conv_integer(BUS_int ( BUS_int'left));
else
Half := ( BUS_int'length + 1 ) / 2 + BUS_int'right;
Upper := ones_count ( BUS_int ( BUS_int'left downto Half ));
Lower := ones_count ( BUS_int ( Half - 1 downto
BUS_int'right));
Result := Upper + Lower;
end if;
end if;
return Result;
end;

Any ideas to make this work better? Thanks!

- Paulo Valentim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top