Count ones without for loop

Without using the $countones option or foreach, is there a way to count the number of 1s in a given binary number using only gates?

Thanks

In reply to UVM_learner6:

Without using the $countones option or foreach, is there a way to count the number of 1s in a given binary number using only gates?

An option is to use adders. Those adders can be a single thread (slow though) like

 
(((a[0]+a[1]) +a[2]) +a[3]) ...  
Or with more parallelism 
   
(a[0]+a[1])  (a[2]+a[3])  (a[4]+a[5])  (a[4]+a[5])     ...
 _____|____________|_____  _____|___________|______
      +            +            +           +
    ---------|--------         --------|-------
             +                         +             

A synthesis tool should be able to do that for you automatically (e.g., use of the for loop. The style in writing the code may impact the speed, for example adding more parallelism). Pipeline regs will also speed things too.

Maybe there is a clever way to achieve this without clocking; for no this is my attmpted solution. I welcome ideas.
Ben Cohen
http://www.systemverilog.us/ ben@systemverilog.us
For training, consulting, services: contact Home - My cvcblr


  1. SVA Alternative for Complex Assertions
    https://verificationacademy.com/news/verification-horizons-march-2018-issue
  2. SVA: Package for dynamic and range delays and repeats - SystemVerilog - Verification Academy
  3. SVA in a UVM Class-based Environment
    https://verificationacademy.com/verification-horizons/february-2013-volume-9-issue-1/SVA-in-a-UVM-Class-based-Environment