UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Visitor aio85
Visitor
17,746 Views
Registered: ‎03-27-2009

Logarithms in VHDL

I need to use a logarithm operator in vhdl, but if I use that of math_real package I must use real numbers, and then I can't make synthesize. The algorithm I want to implement use the logarithm and rounds the result to obtain an integer, then if there is any function which makes the logarithm and save the result in an integer, I can use it. Is there any way to make this or any similar package?
0 Kudos
4 Replies
Instructor
Instructor
17,743 Views
Registered: ‎08-14-2007

Re: Logarithms in VHDL

If you can scale the input so that you take the logarithm base 2, then you only need to find the first '1' bit.  That's

nothing more than a priority encoder.

-- Gabor
0 Kudos
Highlighted
Visitor aio85
Visitor
17,709 Views
Registered: ‎03-27-2009

Re: Logarithms in VHDL

Thanks for replying, but, this method could be used to calculate neperian logarithm? Is there any web site where it's explained? I have looked for it, but I've not found anyone and I don't understand completely this method. Thanks
0 Kudos
Instructor
Instructor
17,684 Views
Registered: ‎08-14-2007

Re: Logarithms in VHDL

Actually the relationship is only simple after you take the logarithm:

 

log2 (X) = ln (X) / ln (2)

 

So:

 

ln (X) = log2 (X) * ln (2)

 

The problem here is that if you want ln (X) to the nearest integer, having log2 (X)

will only help if you have some precision beyond the decimal point.  You can

achieve this by first finding the highest order (leftmost) '1' bit in X to get the integer

part of log2 (X) and then using a table lookup on the rest of X after normalizing it

to be a number between 0.5 and 1.

 

Regards,

Gabor

-- Gabor
0 Kudos
Visitor aio85
Visitor
17,675 Views
Registered: ‎03-27-2009

Re: Logarithms in VHDL

 

 

I've done what you have told me, first in matlab and then in vhdl. In matlab I've seen that I don't need the decimal aproximationvto the device I want to design, so I've make the next function in vhdl:

 

function logarithm (a : integer) return integer is
    variable temp, lo, lo_aux : integer:=0;
   begin
        temp := a/2;
        while temp /= 0 loop
            temp := temp/2;
            lo := lo + 1;
        end loop;
        lo := lo*6931;
        if lo < 10000 then
            lo_aux := 0;
        else
            lo_aux := lo/10000;
        end if;
        return lo_aux;
     end function logarithm;

 

After synthetizing it gives me the next error in lo_aux := lo/10000:

 

Operator <DIVIDE> must have constant operands or first operand must be power of 2 

 

Thanks again for replying. 

0 Kudos