If A Number is An Exact Power of 2?

This algorithm is also very simple. All bits of 32 bit number are summed together and checked if accumulated sum is '1' or not. This algorithm is implemented as series of 1-bit full adders in hardware. Following is the code snippet for this algorithm.

 

 Adding All 1s in Number

 



     always @(*) begin
       sum = 0;
       for (i = 0; i < 32; i = i+1) begin
         sum = sum + inb[i];
       end
     end
      
     always @(posedge clk or negedge rst_n) begin
       if (~rst_n)
          val <= 1'b0;
       else
          val <= sum == 1;
     end

 


If implemented in ANSI-C, loop will run 32 times. All bits are added together and result is checked if it is one or not. Number of cycles taken arek*32 (Where k is arbitrary constant). HW performance is

  • Code Size – 21 (Less the better)
  • Complexity – 9/10 on simplicity scale. (More the better – Little Subjective)
  • Basic Gate Count & Timing –  Area is 1813 micron2 (Less the better), Critical Path Delay 4.32ns(Less the better)
  • Optimizable – Area is 3885 micron2(Less the better), Critical Path Delay 2.62ns (Less the better)

 

Next: Design Implementation 3 – Nifty & Cute

Leave a Reply

Your email address will not be published. Required fields are marked *