+ Post New Thread
Results 1 to 19 of 19

11th June 2018, 07:29 #1
 Join Date
 Jun 2018
 Posts
 8
 Helped
 0 / 0
 Points
 64
 Level
 1
How to calculate log10(n) in Verilog
Hiii
can someone please help me to calculate log base 10 in Verilog. What i need to do is to simply calculate the log base 10 of a variable n. Please help with testbench also.

Advertisment

11th June 2018, 08:07 #2
 Join Date
 Jun 2010
 Posts
 6,702
 Helped
 1954 / 1954
 Points
 36,694
 Level
 46
Re: How to calculate log10(n) in Verilog
Have you even started? For a log function the easiest method is a look up table.

11th June 2018, 08:15 #3
 Join Date
 Jun 2018
 Posts
 8
 Helped
 0 / 0
 Points
 64
 Level
 1
Re: How to calculate log10(n) in Verilog
Sorry, I am a newbie in verilog programming. So can you please explain with example.?

11th June 2018, 10:17 #4
 Join Date
 Nov 2012
 Posts
 2,717
 Helped
 637 / 637
 Points
 14,713
 Level
 29
Re: How to calculate log10(n) in Verilog
For a log function the easiest method is a look up table...
from the log base e to log base 10 it is simple matter of scaling.

11th June 2018, 14:20 #5
 Join Date
 Nov 2006
 Posts
 61
 Helped
 7 / 7
 Points
 2,406
 Level
 11
Re: How to calculate log10(n) in Verilog
As "c_mitra" replied, complex math functions are calculated using numerical methods. This question is not about verilog or digital design. You can search on how to calculate the log or ln or whatever using some additions/subtractions/multiplications/division then you use HDL to implement this.

12th June 2018, 08:49 #6
 Join Date
 Nov 2012
 Posts
 2,717
 Helped
 637 / 637
 Points
 14,713
 Level
 29
Re: How to calculate log10(n) in Verilog
I am a newbie in verilog programming. So can you please explain with example.?
I do not have the reference right now, but the floating point computations on the intel CPU for the log takes the largest number of cycles to complete...
Equally complex is the computation of x^y where both x and y are floats. Calculating in double precision is the slowest (most time consuming)

12th June 2018, 11:36 #7
 Join Date
 Apr 2018
 Location
 Gdańsk, Poland
 Posts
 82
 Helped
 14 / 14
 Points
 525
 Level
 4
 Blog Entries
 3
Re: How to calculate log10(n) in Verilog
What is desired type of variable n?
If it is only positive and integer number (1, 2, 3, etc.) then the best method is to use Look Up Table. Use Excel or something to calculate log10(n) table and write it to some memory, e.g. Block ROM.
If it is float number then like c_mitra and sherif123 said you need to use numerical methods to make an approximation of the more complex function.

12th June 2018, 12:02 #8
 Join Date
 Jan 2008
 Location
 Bochum, Germany
 Posts
 43,141
 Helped
 13126 / 13126
 Points
 248,053
 Level
 100
Re: How to calculate log10(n) in Verilog
Different implementation methods for log function have been suggested, it's usually decided based on resolution and range requirements. ROM table with piecewise linear interpolation would be my preferred method in case of doubt.

Advertisment

14th June 2018, 07:36 #9
 Join Date
 Jun 2018
 Posts
 8
 Helped
 0 / 0
 Points
 64
 Level
 1
Re: How to calculate log10(n) in Verilog
Thank you for replies. I need to calculate log for a particular given input value. So I guess lookup table method is not need. Yeah I got the solution some how but the thing is I want to write code for it in verilog. also i need for unsigned integer.
I can write it in C programming but i dont know how to do with verilog? Can any one help e to write the below C code to write in verilog?
FYI, I want to give n and r value manually.Last edited by bassa; 14th June 2018 at 08:00. Reason: add c syntax

Advertisment

14th June 2018, 08:43 #10
 Join Date
 Jan 2008
 Location
 Germay
 Posts
 1,140
 Helped
 248 / 248
 Points
 7,695
 Level
 20
 Blog Entries
 1
Re: How to calculate log10(n) in Verilog
I can write it in C programming but i dont know how to do with verilog? Can any one help e to write the below C code to write in verilog?.....yes, I do this for fun!

14th June 2018, 09:01 #11
 Join Date
 Jan 2008
 Location
 Bochum, Germany
 Posts
 43,141
 Helped
 13126 / 13126
 Points
 248,053
 Level
 100
Re: How to calculate log10(n) in Verilog
The recursive calculation scheme can't be used as hardware logic description, otherwise the number of recursions must be known at synthesis time.
You can implement the iterative solution as a state machine, driven by a clock. Or less advisable, define a maximum iteration count and enroll the loop into combinational logic.

14th June 2018, 09:07 #12
 Join Date
 Apr 2018
 Location
 Gdańsk, Poland
 Posts
 82
 Helped
 14 / 14
 Points
 525
 Level
 4
 Blog Entries
 3
Re: How to calculate log10(n) in Verilog
If you say so, then I guess that you don't understand what look up table method means.
Make a table in Block ROM with log10(n) results for positive and integer n.
Then input 'n' is the index, where you are looking up a value to get from that table.
The output is the value from this table under index 'n'.
That's the definition for the look up table and I think it suits perfectly yours need.

14th June 2018, 15:03 #13
 Join Date
 Nov 2012
 Posts
 2,717
 Helped
 637 / 637
 Points
 14,713
 Level
 29
Re: How to calculate log10(n) in Verilog
Your program works only for some values that give integer results: for every n that is not a power of r, you will get only the integer part. What you are trying to do?
you can declare the base r as an integer but n and the log must be declared float (or double).
You wanted to calculate log10(n), right? you can have n=1, 10, 100 and so on...
Any other value of n will return a wrong result.
You need to begin at the beginning: start from the log...

18th June 2018, 09:03 #14
 Join Date
 Jun 2018
 Posts
 8
 Helped
 0 / 0
 Points
 64
 Level
 1
Re: How to calculate log10(n) in Verilog

25th June 2018, 03:02 #15
 Join Date
 Feb 2015
 Posts
 920
 Helped
 263 / 263
 Points
 5,550
 Level
 17
Re: How to calculate log10(n) in Verilog
I think my choice would be based on performance requirements and input size. for example, unsigned 32b integer has about 10 values, depending on if the lg(0) case is also detected. This can be done with a handful of comparators and a small population count adder tree.

3rd July 2018, 08:20 #16
 Join Date
 Jun 2018
 Posts
 8
 Helped
 0 / 0
 Points
 64
 Level
 1
Re: How to calculate log10(n) in Verilog
Thank you for the lookup table clarification. But in my application I don't wish to store the values previously, because I am working on a security oriented algorithm. So what I need is a synthesisable verilog code to find log value for which I have to give 'n' as input and need direct logarithm output of n base 10 (or) some other base(say m).

Advertisment

3rd July 2018, 09:17 #17
 Join Date
 Jan 2008
 Location
 Bochum, Germany
 Posts
 43,141
 Helped
 13126 / 13126
 Points
 248,053
 Level
 100
Re: How to calculate log10(n) in Verilog
Don't see how security conflicts with using lookup tables. However you can use any numerical method of your choice, e.g. the recursive algorithm sketched in post #9 in a synthesizable implementation. Number format has to be tailored for intended range and resolution, in case of doubt use appropriate fixed point format.

4th July 2018, 02:45 #18
 Join Date
 Feb 2015
 Posts
 920
 Helped
 263 / 263
 Points
 5,550
 Level
 17
Re: How to calculate log10(n) in Verilog
For a LUT, a concern is if there are multiple memory elements. By providing a known input in a loop, the location information about the LUTs become known from RF/Temperature measurements. Then an unknown input can be put into a loop. Information about the key or plaintext might become known.
The recursive implementation is not ideal  it can complete early and use less energy. Time/power differences are the more basic attacks on crypto.
HW Crypto is difficult. A goal is to make a circuit where nothing correlates to the key or plaintext. This means avoiding time differences, power differences, location differences, and even any failure mode that might reveal any information about the key. Basically, overclocking the device should not create a failure that gives information about the key.

4th July 2018, 04:56 #19
 Join Date
 Feb 2014
 Posts
 478
 Helped
 174 / 174
 Points
 3,749
 Level
 14
Re: How to calculate log10(n) in Verilog
C development for FPGA’s is more realistic these days. Take a look at vivado HLS or its Altera counterpart.
I spent a day using HLS for the first time recently and got an algorithm up and running pretty easily. It’s promising though ultimately I decided to stick with verilog.
Though if you’re comfortable with C and not verlilog it may make a lot of sense.
+ Post New Thread
Please login