OpenCores
URL https://opencores.org/ocsvn/rng_lib/rng_lib/trunk

Subversion Repositories rng_lib

[/] [rng_lib/] [trunk/] [bench/] [vhdl/] [math_lib.vhd] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 gedra
----------------------------------------------------------------------
2
----                                                              ----
3
---- Math function library.                                       ----
4
----                                                              ----
5
---- This file is part of the Random Number Generator project     ----
6
---- http://www.opencores.org/cores/rng_lib/                      ----
7
----                                                              ----
8
---- Description                                                  ----
9
---- These math function are copied from the draft version of the ----
10
---- IEEE MATH_REAL package.                                      ----
11
----                                                              ----
12
---- To Do:                                                       ----
13
---- -                                                            ----
14
----                                                              ----
15
---- Author(s):                                                   ----
16
---- - Geir Drange, gedra@opencores.org                           ----
17
----                                                              ----
18
----------------------------------------------------------------------
19
----                                                              ----
20
---- Copyright (C) 2004 Authors and OPENCORES.ORG                 ----
21
----                                                              ----
22
---- This source file may be used and distributed without         ----
23
---- restriction provided that this copyright statement is not    ----
24
---- removed from the file and that any derivative work contains  ----
25
---- the original copyright notice and the associated disclaimer. ----
26
----                                                              ----
27
---- This source file is free software; you can redistribute it   ----
28
---- and/or modify it under the terms of the GNU General          ----
29
---- Public License as published by the Free Software Foundation; ----
30
---- either version 2.0 of the License, or (at your option) any   ----
31
---- later version.                                               ----
32
----                                                              ----
33
---- This source is distributed in the hope that it will be       ----
34
---- useful, but WITHOUT ANY WARRANTY; without even the implied   ----
35
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ----
36
---- PURPOSE. See the GNU General Public License for more details.----
37
----                                                              ----
38
---- You should have received a copy of the GNU General           ----
39
---- Public License along with this source; if not, download it   ----
40
---- from http://www.gnu.org/licenses/gpl.txt                     ----
41
----                                                              ----
42
----------------------------------------------------------------------
43
--
44
-- CVS Revision History
45
--
46
-- $Log: not supported by cvs2svn $
47 7 gedra
-- Revision 1.1  2004/09/28 15:02:56  gedra
48
-- Math functions library.
49 3 gedra
--
50
--
51 7 gedra
--
52
 
53 3 gedra
library ieee;
54
use ieee.std_logic_1164.all;
55
use ieee.numeric_std.all;
56
 
57
package math_lib is
58
 
59 7 gedra
   function sqrt(x : real) return real;  -- returns square root
60
   function ln(x   : real) return real;  -- natural logarithm
61
   function log(x  : real) return real;  -- base 10 logarithm
62
   function exp(x  : real) return real;  -- exponential function
63
 
64
   -- Some mathematical constants
65
   constant MATH_E : real := 2.71828_18284_59045_23536;
66
 
67 3 gedra
end math_lib;
68
 
69
package body math_lib is
70
 
71
-- Square root calculation
72 7 gedra
   function sqrt (x : real) return real is
73
      -- returns square root of X;  X >= 0
74
      --
75
      -- Computes square root using the Newton-Raphson approximation:
76
      -- F(n+1) = 0.5*[F(n) + x/F(n)];
77
      --
78
 
79
      constant inival       : real := 1.5;
80
      constant eps          : real := 0.000001;
81
      constant relative_err : real := eps*X;
82 3 gedra
 
83 7 gedra
      variable oldval : real;
84
      variable newval : real;
85
 
86
   begin
87
      -- check validity of argument
88
      if x < 0.0 then
89
         report "x < 0 in sqrt(x)"
90
            severity failure;
91
         return (0.0);
92 3 gedra
      end if;
93 7 gedra
 
94
      -- get the square root for special cases
95
      if x = 0.0 then
96
         return 0.0;
97
      else
98
         if x = 1.0 then
99
            return 1.0;                 -- return exact value
100
         end if;
101
      end if;
102
 
103
      -- get the square root for general cases
104
      oldval := inival;
105 3 gedra
      newval := (X/oldval + oldval)/2.0;
106
 
107 7 gedra
      while (abs(newval -oldval) > relative_err) loop
108
         oldval := newval;
109
         newval := (X/oldval + oldval)/2.0;
110
      end loop;
111
 
112
      return newval;
113
   end sqrt;
114
 
115 3 gedra
-- Natural logarithm calculation
116 7 gedra
   function ln (x : real) return real is
117
      -- returns natural logarithm of X; X > 0
118
      --
119
      -- This function computes the exponential using the following series:
120
      --    log(x) = 2[ (x-1)/(x+1) + (((x-1)/(x+1))**3)/3.0 + ...] ; x > 0
121
      --
122
 
123
      constant eps : real := 0.000001;  -- precision criteria
124 3 gedra
 
125 7 gedra
      variable xlocal    : real;        -- following variables are
126
      variable oldval    : real;        -- used to evaluate the series
127
      variable xlocalsqr : real;
128
      variable factor    : real;
129
      variable count     : integer;
130
      variable newval    : real;
131
 
132
   begin
133
      -- check validity of argument
134
      if x <= 0.0 then
135
         report "x <= 0 in ln(x)"
136
            severity failure;
137
         return(real'low);
138
      end if;
139 3 gedra
 
140 7 gedra
      -- compute value for special cases
141
      if x = 1.0 then
142
         return 0.0;
143
      else
144
         if x = MATH_E then
145
            return 1.0;
146
         end if;
147 3 gedra
      end if;
148
 
149 7 gedra
      -- compute value for general cases
150
      xlocal    := (x - 1.0)/(x + 1.0);
151
      oldval    := xlocal;
152
      xlocalsqr := xlocal*xlocal;
153
      factor    := xlocal*xlocalsqr;
154
      count     := 3;
155
      newval    := oldval + (factor/real(count));
156 3 gedra
 
157 7 gedra
      while (abs(newval - oldval) > eps) loop
158
         oldval := newval;
159
         count  := count +2;
160
         factor := factor * xlocalsqr;
161
         newval := oldval + factor/real(count);
162
      end loop;
163
 
164
      newval := newval * 2.0;
165
      return newval;
166
   end ln;
167
 
168 3 gedra
-- Base 10 logarithm calculation
169 7 gedra
   function log (x : real) return real is
170
      -- returns logarithm base 10 of x; x > 0
171
   begin
172
      -- check validity of argument
173
      if x <= 0.0 then
174
         assert false report "x <= 0.0 in log(x)"
175
            severity error;
176
         return(real'low);
177
      end if;
178 3 gedra
 
179 7 gedra
      -- compute the value
180
      return (ln(x)/2.30258509299);
181
   end log;
182 3 gedra
 
183
-- Calculate e**x
184 7 gedra
   function exp (x : real) return real is
185
      -- returns e**X; where e = MATH_E
186
      --
187
      -- This function computes the exponential using the following series:
188
      --    exp(x) = 1 + x + x**2/2! + x**3/3! + ... ; x > 0
189
      --
190
      constant eps : real := 0.000001;  -- precision criteria
191
 
192
      variable reciprocal : boolean := x < 0.0;  -- check sign of argument
193
      variable xlocal     : real    := abs(x);   -- use positive value
194
      variable oldval     : real;                -- following variables are
195
      variable num        : real;                -- used for series evaluation
196
      variable count      : integer;
197
      variable denom      : real;
198
      variable newval     : real;
199
 
200
   begin
201
      -- compute value for special cases
202
      if x = 0.0 then
203
         return 1.0;
204
      else
205
         if x = 1.0 then
206
            return MATH_E;
207
         end if;
208 3 gedra
      end if;
209
 
210 7 gedra
      -- compute value for general cases
211
      oldval := 1.0;
212
      num    := xlocal;
213
      count  := 1;
214
      denom  := 1.0;
215 3 gedra
      newval := oldval + num/denom;
216
 
217 7 gedra
      while (abs(newval - oldval) > eps) loop
218
         oldval := newval;
219
         num    := num*xlocal;
220
         count  := count +1;
221
         denom  := denom*(real(count));
222
         newval := oldval + num/denom;
223
      end loop;
224
 
225
      if reciprocal then
226
         newval := 1.0/newval;
227
      end if;
228
 
229
      return newval;
230
   end exp;
231
 
232 3 gedra
end math_lib;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.