+ Post New Thread
Results 1 to 13 of 13
  1. #1
    Newbie level 3
    Points: 26, Level: 1

    Join Date
    Jun 2018
    Posts
    3
    Helped
    0 / 0
    Points
    26
    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.

    •   Alt11th June 2018, 07:29

      advertising

        
       

  2. #2
    Advanced Member level 5
    Points: 36,062, Level: 46
    Achievements:
    7 years registered

    Join Date
    Jun 2010
    Posts
    6,589
    Helped
    1924 / 1924
    Points
    36,062
    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.



  3. #3
    Newbie level 3
    Points: 26, Level: 1

    Join Date
    Jun 2018
    Posts
    3
    Helped
    0 / 0
    Points
    26
    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.?



  4. #4
    Advanced Member level 5
    Points: 13,767, Level: 28

    Join Date
    Nov 2012
    Posts
    2,565
    Helped
    596 / 596
    Points
    13,767
    Level
    28

    Re: How to calculate log10(n) in Verilog

    For a log function the easiest method is a look up table...
    ln(1+x) can be expressed as a series (infinite) but you cannot add upto infinity you use a finite approximation using a polynomial (Pade approximations). That is the method most calculators and system subroutines use. It is relatively straight forward.

    from the log base e to log base 10 it is simple matter of scaling.



    •   Alt11th June 2018, 10:17

      advertising

        
       

  5. #5
    Member level 3
    Points: 2,406, Level: 11

    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.



  6. #6
    Advanced Member level 5
    Points: 13,767, Level: 28

    Join Date
    Nov 2012
    Posts
    2,565
    Helped
    596 / 596
    Points
    13,767
    Level
    28

    Re: How to calculate log10(n) in Verilog

    I am a newbie in verilog programming. So can you please explain with example.?
    You can perhaps look up this paper which is rather simple for a mathematics paper: http://elib.mi.sanu.ac.rs/files/jour.../22/tm1212.pdf

    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)



    •   Alt12th June 2018, 08:49

      advertising

        
       

  7. #7
    Member level 1
    Points: 217, Level: 2
    Achievements:
    Created Blog entry

    Join Date
    Apr 2018
    Location
    Gdańsk, Poland
    Posts
    33
    Helped
    4 / 4
    Points
    217
    Level
    2
    Blog Entries
    3

    Re: How to calculate log10(n) in Verilog

    Quote Originally Posted by Poomagal View Post
    What i need to do is to simply calculate the log base 10 of a variable n.
    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.



  8. #8
    Super Moderator
    Points: 244,657, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    42,495
    Helped
    12948 / 12948
    Points
    244,657
    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.



  9. #9
    Newbie level 3
    Points: 26, Level: 1

    Join Date
    Jun 2018
    Posts
    3
    Helped
    0 / 0
    Points
    26
    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?

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    unsigned int Logn(unsigned int n, unsigned int r)
    {
       return (n > r-1)? 1 + Logn(n/r, r): 0;
    }
     
    int main()
    {
      unsigned int n = 256;
      unsigned int r = 4;
      printf("%u", Logn(n, r));
      getchar();
    }

    FYI, I want to give n and r value manually.
    Last edited by bassa; 14th June 2018 at 08:00. Reason: add c syntax



  10. #10
    Advanced Member level 4
    Points: 7,451, Level: 20
    Achievements:
    7 years registered Created Blog entry
    dpaul's Avatar
    Join Date
    Jan 2008
    Location
    Germay
    Posts
    1,094
    Helped
    244 / 244
    Points
    7,451
    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?
    Then you must learn Verilog first. Why would someone do the conversion for you, it is your task!
    .....yes, I do this for fun!



  11. #11
    Super Moderator
    Points: 244,657, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    42,495
    Helped
    12948 / 12948
    Points
    244,657
    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.



  12. #12
    Member level 1
    Points: 217, Level: 2
    Achievements:
    Created Blog entry

    Join Date
    Apr 2018
    Location
    Gdańsk, Poland
    Posts
    33
    Helped
    4 / 4
    Points
    217
    Level
    2
    Blog Entries
    3

    Re: How to calculate log10(n) in Verilog

    Quote Originally Posted by Poomagal View Post
    I need to calculate log for a particular given input value. So I guess lookup table method is not need.
    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.



    •   Alt14th June 2018, 09:07

      advertising

        
       

  13. #13
    Advanced Member level 5
    Points: 13,767, Level: 28

    Join Date
    Nov 2012
    Posts
    2,565
    Helped
    596 / 596
    Points
    13,767
    Level
    28

    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...



--[[ ]]--