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.

FPGA implementation of Log function

Status
Not open for further replies.

ammassk

Member level 2
Joined
Jul 19, 2012
Messages
43
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,606
dear all

How can I implement log(1+exp(x)/1-exp(x)) in FPGA?suggest me the best method for this implementation. If any sample code available, please give me.
 

use a LUT (ie a ROM, with X as the address).
 

In my project x is a huge matrix(100 columns and 56 rows). How can I write code for these much values to put in LUT?Also i need to take each value of these from the memory for the further computations. ROM I can't use, I think. Is there any method to implement this?
 

In my project x is a huge matrix(100 columns and 56 rows). How can I write code for these much values to put in LUT?
The look-up-table can generate output for different values sequentially, one per clock cycle.

If the x values are constants, the calculation can be performed at compile time.
 

100 by 56 matrix consists of 5600 real values. How can I store these real values into LUT?
 

I think, the question has been already addressed in your previous thread https://www.edaboard.com/threads/263091/

It's still not completely clear what you try to achieve, if you are talking about an array of constants or a RAM that can take different values during run time. The solution of the log function question also depends on it.

It has been previously mentioned that real numbers can't be synthesized to FPGA hardware. They can appear however in constants and intermediate values of compile time calculations. To represent real numbers in hardware, floating point or fixed point number formats are used.
 

It has been previously mentioned that real numbers can't be synthesized to FPGA hardware. They can appear however in constants and intermediate values of compile time calculations. To represent real numbers in hardware, floating point or fixed point number formats are used.

I didn't understand how to use the fixed point formats in this context.Can you please explain it?
 

If you have access to Xilinx System Generator, it has a reference block that uses Cordic to calculate natural log.

dear all

How can I implement log(1+exp(x)/1-exp(x)) in FPGA?suggest me the best method for this implementation. If any sample code available, please give me.
 

I didn't understand how to use the fixed point formats in this context.Can you please explain it?

Not sure what you don't understand about it. If it's in general, a random google for "fixed point floating point difference" finds some useful explanations.


Also ...

log(1+exp(x)/1-exp(x))

and

In my project x is a huge matrix(100 columns and 56 rows).

I'll assume that your function is not exactly what you are saying, but rather what you think you are saying. You probably mean you have 5600 matrix elements, and you perform the function log(1+exp(x)/1-exp(x)) for each of these elements on an indivual basis. If that is indeed the case then you do what's already been suggested. Iterate over the 5600 elements, and use a lookup table to perform the calculations. Assuming a lookup can be done in a single clock cycle that means 5600 cycles to handle the entire matrix.

And if you think to be doing log(1+exp(X)/1-exp(X)) where X really is a 100x56 matrix then I'm afraid you're in very serious, grave, deep trouble my friend. ;)
 
  • Like
Reactions: verylsi

    verylsi

    Points: 2
    Helpful Answer Positive Rating
I need to do what exactly meant u first( You probably mean you have 5600 matrix elements, and you perform the function log(1+exp(x)/1-exp(x)) for each of these elements on an indivual basis).is der any sample code available to calculate log function?
 

Is there any sample code available to calculate log function?

You can just google for some generic code with a lookup table. That'll have a function in it that fills some ram with values. Then all you have to do is plug in your favorite function. Or what you can do is write a matlab script that calculates your function, and spits out lines of verilog code. Then you include this verilog file. I think for this function that's what I'd do.

Or like Jim suggested, use a cordic core.
 
Last edited:

as still I am beginner in Verilog, I found some codes for the trigonometric functions as follows:


Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function real sin;
  input x;
  real x;
  real x1,y,y2,y3,y5,y7,sum,sign;
  begin
    sign = 1.0;
    x1 = x;
    if (x1<0)
      begin
        x1 = -x1;
        sign = -1.0;
      end
    while (x1 > 3.14159265/2.0)
      begin
        x1 = x1 - 3.14159265;
        sign = -1.0*sign;
      end  
    y = x1*2/3.14159265;
    y2 = y*y;
    y3 = y*y2;
    y5 = y3*y2;
    y7 = y5*y2;
    sum = 1.570794*y - 0.645962*y3 +
           0.079692*y5 - 0.004681712*y7;
    sin = sign*sum;
  end
endfunction
 
The cosine and tangent are computed from the sine:
 
function real cos;
  input x;
  real x;
  begin
    cos = sin(x + 3.14159265/2.0);
  end
endfunction
 
 
function real tan;
  input x;
  real x;
  begin
    tan = sin(x)/cos(x);
  end
endfunction
 
 
function real rootof2;
  input n;
  integer n; 
  real power;
  integer i;
  
  begin
    power = 0.82629586;
    power = power / 10000000.0;
    power = power + 1.0;
    i = -23;
    
    if (n >= 1)
      begin
        power = 2.0;
        i = 0;
      end
 
    for (i=i; i< n; i=i+1)
      begin
        power = power * power;
      end
    rootof2 = power;
  end
endfunction
 
 
function real exp;
  input x;
  real x;    
  real x1,power,prod;
  integer i;
  begin
    x1 = fabs(x)*1.44269504;
    if (x1 > 255.0)
      begin
        exp = 0.0;
        if (x>0.0) 
          begin
            $display("exp illegal argument:",x);
            $stop;            
          end
      end
    else
      begin
        prod = 1.0;
        power = 128.0;
        for (i=7; i>=-23; i=i-1)
          begin
            if (x1 > power)
              begin
                prod = prod * rootof2(i);
                x1 = x1 - power;
              end
            power = power / 2.0;
          end
        if (x < 0)
          exp = 1.0/prod;
        else
          exp = prod;
      end
  end
endfunction
 
 
function real log;
  input x;
  real x;
  real re,log2;
  integer i;
  begin
    if (x <= 0.0)
      begin
        $display("log illegal argument:",x);
        $stop;
        log = 0;
      end
    else
      begin  
        if (x<1.0)
          re = 1.0/x;
        else
          re = x;
        log2 = 0.0;
        for (i=7; i>=-23; i=i-1) 
          begin
            if (re > rootof2(i))
              begin
                re = re/rootof2(i);
                log2 = 2.0*log2 + 1.0;
              end
            else
              log2 = log2*2;
          end
        if (x < 1.0)
          log = -log2/12102203.16;
        else
          log = log2/12102203.16;
      end
    end
endfunction   
 
 
 
function real pow;
  input x,y;
  real x,y;
  begin
    if (x<0.0)
      begin
        $display("pow illegal argument:",x);
        $stop;            
      end    
    pow = exp(y*log(x));
  end
endfunction
 
 
function real sqrt;
  input x;
  real x;
  begin
    if (x<0.0)
      begin
        $display("sqrt illegal argument:",x);
        $stop;            
      end    
    sqrt = exp(0.5*log(x));
  end
endfunction
 
 
function real atan;
  input x;
  real x;
  real x1,x2,sign,bias;
  real d3,s3;
  begin
    sign = 1.0;
    bias = 0.0;
    x1 = x;
    if (x1 < 0.0)
      begin
    x1 = -x1;
    sign = -1.0;
      end
    if (x1 > 1.0)
      begin
    x1 = 1.0/x1;
    bias = sign*3.14159265/2.0;
    sign = -1.0*sign;
      end
    x2 = x1*x1;
    d3 = x2 + 1.44863154;
    d3 = 0.26476862 / d3; 
    s3 = x2 + 3.3163354;  
    d3 = s3 - d3;  
    d3 = 7.10676 / d3; 
    s3 = 6.762139 + x2;
    d3 = s3 - d3;  
    d3 = 3.7092563 / d3;  
    d3 = d3 + 0.17465544;  
    atan = sign*x1*d3+bias;
  end
endfunction



and you can find the original text in this paper: Verilog Transcendental Functions for Numerical Testbenches
 
Last edited by a moderator:

Cordic will give function arctanh,Where log(1+x/1-x)=2*arctanh.Using cordic how can I find out my function ln(1+exp(x)/1-exp(x))?

- - - Updated - - -
 

Through the process of calculation. Or, you know, by using a lookup table.
 
  • Like
Reactions: Aya2002

    Aya2002

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top