Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Can someone explain how clogb2 function works ?

Status
Not open for further replies.

mr_vasanth

Member level 5
Member level 5
Joined
Mar 12, 2007
Messages
86
Helped
5
Reputation
10
Reaction score
7
Trophy points
1,288
Location
Bangalore, India, India
Visit site
Activity points
1,906

Code Verilog - [expand]
1
2
3
4
5
function integer clogb2;
input[31:0] value;
for(clogb2=0; value>0; clogb2=clogb2+1)
value = value>>1;
endfunction



Assume the input value is 1.
I just could not understand how the recursive function call works and when clogb2=clogb2+1 gets executed.

Can someone explain this ?
 
Last edited by a moderator:

This is not a recursive function call. In Verilog, the name of the function is used as variable representing the return value. There was no return statement in Verilog. It has been added by SystemVerilog.

Also, Verilog-2005 added $clog2, so there is no need to write this function yourself.
 

Thanks dave.

I need some more info. What will be the value of clogb2 when the loop exits ?
Assume value = 1 is passed as an input.

1. First clogb2 will be initialized to 0
2. Checks the condition value > 0, condition passed
3. Right shift value by 1, value becomes 0 post shift
4. Increment clogb2 = clogb2 + 1
5. Check the condition value > 0, condition failed and loop exits.

When value 1 is passed as an input, logb2 value should be 0 to my knowledge. But if I go by the steps listed above the value we get is 1.
Please correct me if the above steps listed by me has any flaw.
 

20.8.1 Integer math functions (1800-2012)
The system function $clog2 shall return the ceiling of the log base 2 of the argument (the log rounded up to an integer value). The argument can be an integer or an arbitrary sized vector value. The argument shall be treated as an unsigned value, and an argument value of 0 shall produce a result of 0.
This system function can be used to compute the minimum address width necessary to address a memory of a given size or the minimum vector width necessary to represent a given number of states.
 

dave,

Thank you so much for your time and explanation. I am aware of what you have written in your second post.

My only confusion is how this for loop works ? how does it return a value of 0 when the input argument is 1 ?
for(clogb2=0; value>0; clogb2=clogb2+1)
value = value>>1;
endfunction

Would be great if you could explain only how does this for loop works ?
 

That makes sense. But this triggers another question.

We need at least one bit to represent the value 1. I agree to that.
In the same way, Don't we need at least one bit to represent the value 0 ?

I believe this function clogb2 cannot be used (or invalid) when the input value is 0.
 
Last edited:

From a synthesis perspective, you don't need any bits to represent the a variable whose only value is zero; it becomes a constant. And constant 0 can always be optimized away.
 

There is an error in my code Dave. The corrected one you can find below.
I have earlier missed the statement value = value-1; That was causing all my confusion.
Now the function produces exact logb2 value for all the value, except 0.

Code:
function integer clogb2;
  input integer value;
  begin
    [B]value = value-1;[/B]
    for (clogb2=0; value>0; clogb2=clogb2+1)
      value = value>>1;
  end
endfunction
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top