aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/ieee/math_real-body.vhdl
blob: 41e6a084dcd1333de28f068fb8dc3282f314bd4b (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
--------------------------------------------------------------- 
--
-- This source file may be used and distributed without restriction.
-- No declarations or definitions shall be added to this package.
-- This package cannot be sold or distributed for profit. 
--
--   ****************************************************************
--   *                                                              *
--   *                      W A R N I N G		 	    *
--   *								    *
--   *   This DRAFT version IS NOT endorsed or approved by IEEE     *
--   *							            *
--   ****************************************************************
--
-- Title:    PACKAGE BODY MATH_REAL
--
-- Library:  This package shall be compiled into a library 
--           symbolically named IEEE.
--
-- Purpose:  VHDL declarations for mathematical package MATH_REAL
--	     which contains common real constants, common real
--	     functions, and real trascendental functions.
--
-- Author:   IEEE VHDL Math Package Study Group 
--
-- Notes:
-- 	The package body shall be considered the formal definition of 
-- 	the semantics of this package. Tool developers may choose to implement 
-- 	the package body in the most efficient manner available to them.
--
--      Source code and algorithms for this package body comes from the 
--	following sources: 
--		IEEE VHDL Math Package Study Group participants,
--		U. of Mississippi, Mentor Graphics, Synopsys,
--		Viewlogic/Vantage, Communications of the ACM (June 1988, Vol
--		31, Number 6, pp. 747, Pierre L'Ecuyer, Efficient and Portable
--		Random Number Generators), Handbook of Mathematical Functions
--	        by Milton Abramowitz and Irene A. Stegun (Dover).
--
-- History:
-- 	Version 0.1	Jose A. Torres	4/23/93	First draft
-- 	Version 0.2	Jose A. Torres	5/28/93	Fixed potentially illegal code
--
-- GHDL history
--  2005-04-07  Initial version.
--  2005-12-23  I. Curtis : overhaul of log functions to bring in line
--                          with ieee standard
-------------------------------------------------------------
Library IEEE;

Package body MATH_REAL is
    --
    -- non-trascendental functions
    --
    function SIGN (X: real ) return real is
    	-- returns 1.0 if X > 0.0; 0.0 if X == 0.0; -1.0 if X < 0.0
    begin
        assert false severity failure;
    end SIGN; 

    function CEIL (X : real ) return real is
    begin
        assert false severity failure;
    end CEIL; 

    function FLOOR (X : real ) return real is
    begin
        assert false severity failure;
    end FLOOR;

    function ROUND (X : real ) return real is
    begin
        assert false severity failure;
    end ROUND;
    
    function FMAX (X, Y : real ) return real is
    begin
        assert false severity failure;
    end FMAX;

    function FMIN (X, Y : real ) return real is
    begin
        assert false severity failure;
    end FMIN;

    --
    -- Pseudo-random number generators
    --

    procedure UNIFORM(variable Seed1,Seed2:inout integer;variable X:out real) is
	-- returns a pseudo-random number with uniform distribution in the 
	-- interval (0.0, 1.0).
	-- Before the first call to UNIFORM, the seed values (Seed1, Seed2) must
	-- be initialized to values in the range [1, 2147483562] and 
	-- [1, 2147483398] respectively.  The seed values are modified after 
	-- each call to UNIFORM.
	-- This random number generator is portable for 32-bit computers, and
	-- it has period ~2.30584*(10**18) for each set of seed values.
	--
	-- For VHDL-1992, the seeds will be global variables, functions to 
	-- initialize their values (INIT_SEED) will be provided, and the UNIFORM
	-- procedure call will be modified accordingly.  
	
	variable z, k: integer;
	begin
        k := Seed1/53668;
        Seed1 := 40014 * (Seed1 - k * 53668) - k * 12211;
        
        if Seed1 < 0  then
            Seed1 := Seed1 + 2147483563;
        end if;


        k := Seed2/52774;
        Seed2 := 40692 * (Seed2 - k * 52774) - k * 3791;
        
        if Seed2 < 0  then
            Seed2 := Seed2 + 2147483399;
        end if;

        z := Seed1 - Seed2;
        if z < 1 then
            z := z + 2147483562;
        end if;

        X :=  REAL(Z)*4.656613e-10;
    end UNIFORM;


    function SRAND (seed: in integer ) return integer is
    begin
        assert false severity failure;
    end SRAND;

    function RAND return integer is
    begin
        assert false severity failure;
    end RAND;

    function GET_RAND_MAX  return integer is
        -- The value this function returns should be the same as
        -- RAND_MAX in /usr/include/stdlib.h
    begin
        assert false
            report "Be sure to update GET_RAND_MAX in mathpack.vhd"
            severity note;
        return 2147483647;  -- i386 linux
    end GET_RAND_MAX;

    --
    -- trascendental and trigonometric functions
    --
    function c_sqrt (x : real ) return real;
    attribute foreign of c_sqrt : function is "VHPIDIRECT sqrt";
    
    function c_sqrt (x : real ) return real is
    begin
        assert false severity failure;
    end c_sqrt;

    function SQRT (X : real ) return real is
    begin
        -- check validity of argument
        if ( X < 0.0 ) then
            assert false report "X < 0 in SQRT(X)" 
                severity ERROR;
            return (0.0);
        end if;
        return c_sqrt(X);
    end SQRT;

    function CBRT (X : real ) return real is
    begin
        assert false severity failure;
    end CBRT;

    function "**" (X : integer; Y : real) return real is
    	-- returns Y power of X ==>  X**Y;
    	-- error if X = 0 and Y <= 0.0
    	-- error if X < 0 and Y does not have an integer value
    begin
        -- check validity of argument
        if ( X = 0  ) and ( Y <= 0.0 ) then
            assert false report "X = 0 and Y <= 0.0 in X**Y" 
                severity ERROR;
            return (0.0);
        end if;

        if ( X < 0  ) and ( Y /= REAL(INTEGER(Y)) ) then
            assert false 
                report "X < 0 and Y \= integer in X**Y" 
                severity ERROR;
            return (0.0);
        end if;

        -- compute the result
        return EXP (Y * LOG (REAL(X)));
    end "**";

    function "**" (X : real; Y : real) return real is
    	-- returns Y power of X ==>  X**Y;
    	-- error if X = 0.0 and Y <= 0.0
    	-- error if X < 0.0 and Y does not have an integer value
    begin
        -- check validity of argument
        if ( X = 0.0  ) and ( Y <= 0.0 ) then
            assert false report "X = 0.0 and Y <= 0.0 in X**Y" 
                severity ERROR;
            return (0.0);
        end if;

        if ( X < 0.0  ) and ( Y /= REAL(INTEGER(Y)) ) then
            assert false report "X < 0.0 and Y \= integer in X**Y" 
                severity ERROR;
            return (0.0);
        end if;

        -- compute the result
        return EXP (Y * LOG (X));
    end "**";

    function EXP  (X : real ) return real is
    begin
        assert false severity failure;
    end EXP;

    function c_log (x : real ) return real;
    attribute foreign of c_log : function is "VHPIDIRECT log"; 

    function c_log (x : real ) return real is
    begin
        assert false severity failure;
    end c_log; 

    function LOG (X : real ) return real is
    	-- returns natural logarithm of X; X > 0
        --
        -- This function computes the exponential using the following series:
        --    log(x) = 2[ (x-1)/(x+1) + (((x-1)/(x+1))**3)/3.0 + ...] ; x > 0
        -- 
    begin
    	-- check validity of argument
    	if ( x <= 0.0 ) then
       	  assert false report "X <= 0 in LOG(X)" 
			severity ERROR;
            return(REAL'LOW);
    	end if;
    	return c_log(x); 
    end LOG;

    function LOG (X : in real; BASE: in real) return real is
      -- returns logarithm base BASE of X; X > 0
    begin
      -- check validity of argument
      if ( BASE <= 0.0 ) or ( x <= 0.0 ) then
        assert false report "BASE <= 0.0 or X <= 0.0 in LOG(BASE, X)" 
          severity ERROR;
        return(REAL'LOW);
      end if; 
      -- compute the value
      return (LOG(X)/LOG(BASE));
    end LOG;

    function LOG2 (X : in real) return real is
      -- returns logarithm BASE 2 of X; X > 0
    begin
      return LOG(X) / MATH_LOG_OF_2;
    end LOG2;

    function LOG10 (X : in real) return real is
      -- returns logarithm BASE 10 of X; X > 0
    begin
      return LOG(X) / MATH_LOG_OF_10;
    end LOG10;
    
    function  SIN (X : real ) return real is
    begin 
        assert false severity failure;
    end SIN;

   
    function COS (x : REAL) return REAL is 
    begin 
        assert false severity failure;
    end COS;
   
    function TAN (x : REAL) return REAL is 
    begin
        assert false severity failure;
    end TAN; 
    
    function c_asin (x : real ) return real;
    attribute foreign of c_asin : function is "VHPIDIRECT asin"; 

    function c_asin (x : real ) return real is
    begin
        assert false severity failure;
    end c_asin; 

    function ASIN (x : real ) return real is
        -- returns  -PI/2 < asin X < PI/2; | X | <= 1
    begin   
        if abs x > 1.0 then 
            assert false
                report "Out of range parameter passed to ASIN" 
                severity ERROR;
            return x;
        else
            return c_asin(x);
        end if; 
    end ASIN; 
   
    function c_acos (x : real ) return real;
    attribute foreign of c_acos : function is "VHPIDIRECT acos"; 

    function c_acos (x : real ) return real is
    begin
        assert false severity failure;
    end c_acos; 

    function ACOS (x : REAL) return REAL is
    	-- returns  0 < acos X < PI; | X | <= 1
    begin  
      if abs x > 1.0 then 
         assert false 
            report "Out of range parameter passed to ACOS" 
			severity ERROR; 
         return x;
      else
         return c_acos(x);
      end if;
    end ACOS; 
   
   function ATAN (x : REAL) return REAL is
    	-- returns  -PI/2 < atan X < PI/2
   begin
        assert false severity failure;
   end ATAN; 

    function c_atan2 (x : real; y : real) return real;
    attribute foreign of c_atan2 : function is "VHPIDIRECT atan2"; 

    function c_atan2 (x : real; y: real) return real is
    begin
        assert false severity failure;
    end c_atan2; 

    function ATAN2 (x : REAL; y : REAL) return REAL is 
        -- returns  atan (X/Y); -PI < atan2(X,Y) < PI; Y /= 0.0
    begin   
        if y = 0.0 and x = 0.0 then 
            assert false 
                report "atan2(0.0, 0.0) is undetermined, returned 0,0" 
                severity NOTE;
            return 0.0; 
        else
            return c_atan2(x,y);
        end if;     
    end ATAN2; 


    function SINH (X : real) return real is
    	-- hyperbolic sine; returns (e**X - e**(-X))/2
    begin
        assert false severity failure;
    end SINH;

    function  COSH (X : real) return real is
    	-- hyperbolic cosine; returns (e**X + e**(-X))/2
    begin
        assert false severity failure;
    end COSH;

    function  TANH (X : real) return real is
    	-- hyperbolic tangent; -- returns (e**X - e**(-X))/(e**X + e**(-X))
    begin
        assert false severity failure;
    end TANH;
    
    function ASINH (X : real) return real is
    	-- returns ln( X + sqrt( X**2 + 1))
    begin
        assert false severity failure;
    end ASINH;

    function c_acosh (x : real ) return real;
    attribute foreign of c_acosh : function is "VHPIDIRECT acosh"; 

    function c_acosh (x : real ) return real is
    begin
        assert false severity failure;
    end c_acosh;

    function ACOSH (X : real) return real is
    	-- returns ln( X + sqrt( X**2 - 1));   X >= 1
    begin
      	if abs x >= 1.0 then 
         	assert false report "Out of range parameter passed to ACOSH" 
			severity ERROR; 
         	return x;
      	end if; 
        return c_acosh(x);
    end ACOSH;

    function c_atanh (x : real ) return real;
    attribute foreign of c_atanh : function is "VHPIDIRECT atanh"; 

    function c_atanh (x : real ) return real is
    begin
        assert false severity failure;
    end c_atanh;

    function ATANH (X : real) return real is
    	-- returns (ln( (1 + X)/(1 - X)))/2 ; | X | < 1
    begin
      	if abs x < 1.0 then 
        	assert false report "Out of range parameter passed to ATANH" 
			severity ERROR; 
        	return x;
      	end if; 
        return c_atanh(x);
    end ATANH; 

end  MATH_REAL;