1-- ----------------------------------------------------------------------------
2--   Title      : NUMERIC_STD arithmetic package for synthesis
3--              : Rev. 1.7 (Nov. 23 1994)
4--              :
5--   Library    : This package shall be compiled into a library symbolically
6--              : named IEEE.
7--              :
8--   Developers : IEEE DASC Synthesis Working Group, PAR 1076.3
9--              :
10--   Purpose    : This package defines numeric types and arithmetic functions
11--              : for use with synthesis tools. Two numeric types are defined:
12--              :     --> UNSIGNED : represents UNSIGNED number in vector form
13--              :     --> SIGNED   : represents a SIGNED number in vector form
14--              : The base element type is type STD_LOGIC.
15--              : The leftmost bit is treated as the most significant bit.
16--              : Signed vectors are represented in two's complement form.
17--              : This package contains overloaded arithmetic operators on
18--              : the SIGNED and UNSIGNED types. The package also contains
19--              : useful type conversions functions.
20--              :
21--              : If any argument to a function is a null array, a null array is
22--              : returned (exceptions, if any, are noted individually).
23--              :
24--   Note       : No declarations or definitions shall be included in, or
25--              : excluded from, this package. The package declaration declares
26--              : the functions that can be used by a user. The package body
27--              : shall be considered the formal definition of the semantics of
28--              : this package. Tool developers may choose to implement the
29--              : package body in the most efficient manner available to them.
30--              :
31-- ----------------------------------------------------------------------------
32library ieee;
33use ieee.STD_LOGIC_1164.all;
34
35Package numeric_std is
36
37  --===========================================================================
38  -- Numeric array type definitions
39  --===========================================================================
40
41  type UNSIGNED is array ( NATURAL range <> ) of STD_LOGIC;
42  type SIGNED is array ( NATURAL range <> ) of STD_LOGIC;
43
44  --===========================================================================
45  -- Arithmetic Operators:
46  --===========================================================================
47
48     -- Id: A.1
49  function "abs" ( X : SIGNED) return SIGNED;
50     -- Result subtype: SIGNED(X'LENGTH-1 downto 0).
51     -- Result: Returns the absolute value of a SIGNED vector X.
52
53     -- Id: A.2
54  function "-" ( ARG: SIGNED) return SIGNED;
55     -- Result subtype: SIGNED(ARG'LENGTH-1 downto 0).
56     -- Result: Returns the value of the unary minus operation on a
57     --         SIGNED vector ARG.
58
59  --============================================================================
60
61     -- Id: A.3
62  function "+" (L,R: UNSIGNED ) return UNSIGNED;
63     -- Result subtype: UNSIGNED(MAX(L'LENGTH, R'LENGTH)-1 downto 0).
64     -- Result: Adds two UNSIGNED vectors that may be of different lengths.
65
66     -- Id: A.4
67  function "+" ( L,R: SIGNED) return SIGNED;
68     -- Result subtype: SIGNED(MAX(L'LENGTH, R'LENGTH)-1 downto 0).
69     -- Result: Adds two SIGNED vectors that may be of different lengths.
70
71     -- Id: A.5
72  function "+" ( L: UNSIGNED; R: NATURAL) return UNSIGNED;
73     -- Result subtype: UNSIGNED(L'LENGTH-1 downto 0).
74     -- Result: Adds an UNSIGNED vector, L, with a non-negative INTEGER, R.
75
76     -- Id: A.6
77  function "+" ( L: NATURAL; R: UNSIGNED) return UNSIGNED;
78     -- Result subtype: UNSIGNED(R'LENGTH-1 downto 0).
79     -- Result: Adds a non-negative INTEGER, L, with an UNSIGNED vector, R.
80
81     -- Id: A.7
82  function "+" ( L: INTEGER; R: SIGNED) return SIGNED;
83     -- Result subtype: SIGNED (R'LENGTH-1 downto 0).
84     -- Result: Adds an INTEGER, L (may be positive or negative), to a SIGNED
85     --         vector, R.
86
87     -- Id: A.8
88  function "+" ( L: SIGNED; R: INTEGER) return SIGNED;
89     -- Result subtype: SIGNED (L'LENGTH-1 downto 0).
90     -- Result: Adds a SIGNED vector, L, to an INTEGER, R.
91
92  --============================================================================
93
94     -- Id: A.9
95  function "-" (L,R: UNSIGNED ) return UNSIGNED;
96     -- Result subtype: UNSIGNED(MAX(L'LENGTH, R'LENGTH)-1 downto 0).
97     -- Result: Subtracts two UNSIGNED vectors that may be of different lengths.
98
99     -- Id: A.10
100  function "-" ( L,R: SIGNED) return SIGNED;
101     -- Result subtype: SIGNED(MAX(L'LENGTH, R'LENGTH)-1 downto 0).
102     -- Result: Subtracts a SIGNED vector, R, from another SIGNED vector, L,
103     --         that may possibly be of different lengths.
104
105     -- Id: A.11
106  function "-" ( L: UNSIGNED;R: NATURAL) return UNSIGNED;
107     -- Result subtype: UNSIGNED (L'LENGTH-1 downto 0).
108     -- Result: Subtracts a non-negative INTEGER, R, from an UNSIGNED vector, L.
109
110     -- Id: A.12
111  function "-" ( L: NATURAL; R: UNSIGNED) return UNSIGNED;
112     -- Result subtype: UNSIGNED(R'LENGTH-1 downto 0).
113     -- Result: Subtracts an UNSIGNED vector, R, from a non-negative INTEGER, L.
114
115     -- Id: A.13
116  function "-" ( L: SIGNED; R: INTEGER) return SIGNED;
117     -- Result subtype: SIGNED (L'LENGTH-1 downto 0).
118     -- Result: Subtracts an INTEGER, R, from a SIGNED vector, L.
119
120     -- Id: A.14
121  function "-" ( L: INTEGER; R: SIGNED) return SIGNED;
122     -- Result subtype: SIGNED(R'LENGTH-1 downto 0).
123     -- Result: Subtracts a SIGNED vector, R, from an INTEGER, L.
124
125  --============================================================================
126
127     -- Id: A.15
128  function "*" (L,R: UNSIGNED ) return UNSIGNED;
129     -- Result subtype: UNSIGNED((L'length+R'length-1) downto 0).
130     -- Result: Performs the multiplication operation on two UNSIGNED vectors
131     --         that may possibly be of different lengths.
132
133     -- Id: A.16
134  function "*" ( L,R: SIGNED) return SIGNED;
135     -- Result subtype: SIGNED((L'length+R'length-1) downto 0)
136     -- Result: Multiplies two SIGNED vectors that may possibly be of
137     --         different lengths.
138
139     -- Id: A.17
140  function "*" ( L: UNSIGNED; R: NATURAL) return UNSIGNED;
141     -- Result subtype: UNSIGNED((L'length+L'length-1) downto 0).
142     -- Result: Multiplies an UNSIGNED vector, L, with a non-negative
143     --         INTEGER, R. R is converted to an UNSIGNED vector of
144     --         SIZE L'length before multiplication.
145
146     -- Id: A.18
147  function "*" ( L: NATURAL; R: UNSIGNED) return UNSIGNED;
148     -- Result subtype: UNSIGNED((R'length+R'length-1) downto 0).
149     -- Result: Multiplies an UNSIGNED vector, R, with a non-negative
150     --         INTEGER, L. L is converted to an UNSIGNED vector of
151     --         SIZE R'length before multiplication.
152
153     -- Id: A.19
154  function "*" ( L: SIGNED; R: INTEGER) return SIGNED;
155     -- Result subtype: SIGNED((L'length+L'length-1) downto 0)
156     -- Result: Multiplies a SIGNED vector, L, with an INTEGER, R. R is
157     --         converted to a SIGNED vector of SIZE L'length before
158     --         multiplication.
159
160     -- Id: A.20
161  function "*" ( L: INTEGER; R: SIGNED) return SIGNED;
162     -- Result subtype: SIGNED((R'length+R'length-1) downto 0)
163     -- Result: Multiplies a SIGNED vector, R, with an INTEGER, L. L is
164     --         converted to a SIGNED vector of SIZE R'length before
165     --         multiplication.
166
167  --============================================================================
168  --
169  --  NOTE: If second argument is zero for "/" operator, a severity level
170  --        of ERROR is issued.
171
172     -- Id: A.21
173  function "/" (L,R: UNSIGNED ) return UNSIGNED;
174     -- Result subtype: UNSIGNED (L'LENGTH-1 downto 0)
175     -- Result: Divides an UNSIGNED vector, L, by another UNSIGNED vector, R.
176
177     -- Id: A.22
178  function "/" ( L,R: SIGNED) return SIGNED;
179     -- Result subtype: SIGNED (L'LENGTH-1 downto 0)
180     -- Result: Divides an SIGNED vector, L, by another SIGNED vector, R.
181
182     -- Id: A.23
183  function "/" ( L: UNSIGNED; R: NATURAL) return UNSIGNED;
184     -- Result subtype: UNSIGNED (L'LENGTH-1 downto 0)
185     -- Result: Divides an UNSIGNED vector, L, by a non-negative INTEGER, R.
186     --         If NO_OF_BITS(R) > L'LENGTH, then R is truncated to L'LENGTH.
187
188     -- Id: A.24
189  function "/" ( L: NATURAL; R: UNSIGNED) return UNSIGNED;
190     -- Result subtype: UNSIGNED (R'LENGTH-1 downto 0)
191     -- Result: Divides a non-negative INTEGER, L, by an UNSIGNED vector, R.
192     --         If NO_OF_BITS(L) > R'LENGTH, then L is truncated to R'LENGTH.
193
194     -- Id: A.25
195  function "/" ( L: SIGNED; R: INTEGER) return SIGNED;
196     -- Result subtype: SIGNED (L'LENGTH-1 downto 0)
197     -- Result: Divides a SIGNED vector, L, by an INTEGER, R.
198     --         If NO_OF_BITS(R) > L'LENGTH, then R is truncated to L'LENGTH.
199
200     -- Id: A.26
201  function "/" ( L: INTEGER; R: SIGNED) return SIGNED;
202     -- Result subtype: SIGNED (R'LENGTH-1 downto 0)
203     -- Result: Divides an INTEGER, L, by a SIGNED vector, R.
204     --         If NO_OF_BITS(L) > R'LENGTH, then L is truncated to R'LENGTH.
205
206  --============================================================================
207  --
208  --  NOTE: If second argument is zero for "rem" operator, a severity level
209  --        of ERROR is issued.
210
211     -- Id: A.27
212  function "rem" (L,R: UNSIGNED ) return UNSIGNED;
213     -- Result subtype: UNSIGNED(L'LENGTH-1 downto 0)
214     -- Result: Computes "L rem R" where L and R are UNSIGNED vectors.
215
216     -- Id: A.28
217  function "rem" ( L,R: SIGNED) return SIGNED;
218     -- Result subtype: SIGNED(L'LENGTH-1 downto 0)
219     -- Result: Computes "L rem R" where L and R are SIGNED vectors.
220
221     -- Id: A.29
222  function "rem" ( L: UNSIGNED; R: NATURAL) return UNSIGNED;
223     -- Result subtype: UNSIGNED(L'LENGTH-1 downto 0)
224     -- Result: Computes "L rem R" where L is an UNSIGNED vector and R is a
225     --         non-negative INTEGER.
226     --         If NO_OF_BITS(R) > L'LENGTH, then R is truncated to L'LENGTH.
227
228     -- Id: A.30
229  function "rem" ( L: NATURAL; R: UNSIGNED) return UNSIGNED;
230     -- Result subtype: UNSIGNED(R'LENGTH-1 downto 0)
231     -- Result: Computes "L rem R" where R is an UNSIGNED vector and L is a
232     --         non-negative INTEGER.
233     --         If NO_OF_BITS(L) > R'LENGTH, then L is truncated to R'LENGTH.
234
235     -- Id: A.31
236  function "rem" ( L: SIGNED; R: INTEGER) return SIGNED;
237     -- Result subtype: SIGNED(L'LENGTH-1 downto 0)
238     -- Result: Computes "L rem R" where L is SIGNED vector and R is an INTEGER.
239     --         If NO_OF_BITS(R) > L'LENGTH, then R is truncated to L'LENGTH.
240
241     -- Id: A.32
242  function "rem" ( L: INTEGER; R: SIGNED) return SIGNED;
243     -- Result subtype: SIGNED(R'LENGTH-1 downto 0)
244     -- Result: Computes "L rem R" where R is SIGNED vector and L is an INTEGER.
245     --         If NO_OF_BITS(L) > R'LENGTH, then L is truncated to R'LENGTH.
246
247  --============================================================================
248  --
249  --  NOTE: If second argument is zero for "mod" operator, a severity level
250  --        of ERROR is issued.
251
252     -- Id: A.33
253  function "mod" (L,R: UNSIGNED ) return UNSIGNED;
254     -- Result subtype: UNSIGNED(L'LENGTH-1 downto 0)
255     -- Result: Computes "L mod R" where L and R are UNSIGNED vectors.
256
257     -- Id: A.34
258  function "mod" ( L,R: SIGNED) return SIGNED;
259     -- Result subtype: SIGNED(L'LENGTH-1 downto 0)
260     -- Result: Computes "L mod R" where L and R are SIGNED vectors.
261
262     -- Id: A.35
263  function "mod" ( L: UNSIGNED; R: NATURAL) return UNSIGNED;
264     -- Result subtype: UNSIGNED(L'LENGTH-1 downto 0)
265     -- Result: Computes "L mod R" where L is an UNSIGNED vector and R
266     --         is a non-negative INTEGER.
267     --         If NO_OF_BITS(R) > L'LENGTH, then R is truncated to L'LENGTH.
268
269     -- Id: A.36
270  function "mod" ( L: NATURAL; R: UNSIGNED) return UNSIGNED;
271     -- Result subtype: UNSIGNED(R'LENGTH-1 downto 0)
272     -- Result: Computes "L mod R" where R is an UNSIGNED vector and L
273     --         is a non-negative INTEGER.
274     --         If NO_OF_BITS(L) > R'LENGTH, then L is truncated to R'LENGTH.
275
276     -- Id: A.37
277  function "mod" ( L: SIGNED; R: INTEGER) return SIGNED;
278     -- Result subtype: SIGNED(L'LENGTH-1 downto 0)
279     -- Result: Computes "L mod R" where L is a SIGNED vector and
280     --         R is an INTEGER.
281     --         If NO_OF_BITS(R) > L'LENGTH, then R is truncated to L'LENGTH.
282
283     -- Id: A.38
284  function "mod" ( L: INTEGER; R: SIGNED) return SIGNED;
285     -- Result subtype: SIGNED(R'LENGTH-1 downto 0)
286     -- Result: Computes "L mod R" where L is an INTEGER and
287     --         R is a SIGNED vector.
288     --         If NO_OF_BITS(L) > R'LENGTH, then L is truncated to R'LENGTH.
289
290  --============================================================================
291  -- Comparison Operators
292  --============================================================================
293
294     -- Id: C.1
295  function ">"  (L,R: UNSIGNED ) return BOOLEAN;
296     -- Result subtype: BOOLEAN
297     -- Result: Computes "L > R" where L and R are UNSIGNED vectors possibly
298     --         of different lengths.
299
300     -- Id: C.2
301  function ">"  ( L,R: SIGNED) return BOOLEAN;
302     -- Result subtype: BOOLEAN
303     -- Result: Computes "L > R" where L and R are SIGNED vectors possibly
304     --         of different lengths.
305
306     -- Id: C.3
307  function ">"  ( L: NATURAL; R: UNSIGNED) return BOOLEAN;
308     -- Result subtype: BOOLEAN
309     -- Result: Computes "L > R" where L is a non-negative INTEGER and
310     --         R is an UNSIGNED vector.
311
312     -- Id: C.4
313  function ">"  ( L: INTEGER; R: SIGNED) return BOOLEAN;
314     -- Result subtype: BOOLEAN
315     -- Result: Computes "L > R" where L is a INTEGER and
316     --         R is a SIGNED vector.
317
318     -- Id: C.5
319  function ">"  ( L: UNSIGNED; R: NATURAL) return BOOLEAN;
320     -- Result subtype: BOOLEAN
321     -- Result: Computes "L > R" where L is an UNSIGNED vector and
322     --         R is a non-negative INTEGER.
323
324     -- Id: C.6
325  function ">"  ( L: SIGNED; R: INTEGER) return BOOLEAN;
326     -- Result subtype: BOOLEAN
327     -- Result: Computes "L > R" where L is a SIGNED vector and
328     --         R is a INTEGER.
329
330  --============================================================================
331
332     -- Id: C.7
333  function "<"  (L,R: UNSIGNED ) return BOOLEAN;
334     -- Result subtype: BOOLEAN
335     -- Result: Computes "L < R" where L and R are UNSIGNED vectors possibly
336     --         of different lengths.
337
338     -- Id: C.8
339  function "<"  ( L,R: SIGNED) return BOOLEAN;
340     -- Result subtype: BOOLEAN
341     -- Result: Computes "L < R" where L and R are SIGNED vectors possibly
342     --         of different lengths.
343
344     -- Id: C.9
345  function "<"  ( L: NATURAL; R: UNSIGNED) return BOOLEAN;
346     -- Result subtype: BOOLEAN
347     -- Result: Computes "L < R" where L is a non-negative INTEGER and
348     --         R is an UNSIGNED vector.
349
350     -- Id: C.10
351  function "<"  ( L: INTEGER; R: SIGNED) return BOOLEAN;
352     -- Result subtype: BOOLEAN
353     -- Result: Computes "L < R" where L is an INTEGER and
354     --         R is a SIGNED vector.
355
356     -- Id: C.11
357  function "<"  ( L: UNSIGNED; R: NATURAL) return BOOLEAN;
358     -- Result subtype: BOOLEAN
359     -- Result: Computes "L < R" where L is an UNSIGNED vector and
360     --         R is a non-negative INTEGER.
361
362     -- Id: C.12
363  function "<"  ( L: SIGNED; R: INTEGER) return BOOLEAN;
364     -- Result subtype: BOOLEAN
365     -- Result: Computes "L < R" where L is a SIGNED vector and
366     --         R is an INTEGER.
367
368  --============================================================================
369
370     -- Id: C.13
371  function "<=" (L,R: UNSIGNED ) return BOOLEAN;
372     -- Result subtype: BOOLEAN
373     -- Result: Computes "L <= R" where L and R are UNSIGNED vectors possibly
374     --         of different lengths.
375
376     -- Id: C.14
377  function "<=" ( L,R: SIGNED) return BOOLEAN;
378     -- Result subtype: BOOLEAN
379     -- Result: Computes "L <= R" where L and R are SIGNED vectors possibly
380     --         of different lengths.
381
382     -- Id: C.15
383  function "<=" ( L: NATURAL; R: UNSIGNED) return BOOLEAN;
384     -- Result subtype: BOOLEAN
385     -- Result: Computes "L <= R" where L is a non-negative INTEGER and
386     --         R is an UNSIGNED vector.
387
388     -- Id: C.16
389  function "<=" ( L: INTEGER; R: SIGNED) return BOOLEAN;
390     -- Result subtype: BOOLEAN
391     -- Result: Computes "L <= R" where L is an INTEGER and
392     --         R is a SIGNED vector.
393
394     -- Id: C.17
395  function "<=" ( L: UNSIGNED; R: NATURAL) return BOOLEAN;
396     -- Result subtype: BOOLEAN
397     -- Result: Computes "L <= R" where L is an UNSIGNED vector and
398     --         R is a non-negative INTEGER.
399
400     -- Id: C.18
401  function "<=" ( L: SIGNED; R: INTEGER) return BOOLEAN;
402     -- Result subtype: BOOLEAN
403     -- Result: Computes "L <= R" where L is a SIGNED vector and
404     --         R is an INTEGER.
405
406  --============================================================================
407
408     -- Id: C.19
409  function ">=" (L,R: UNSIGNED ) return BOOLEAN;
410     -- Result subtype: BOOLEAN
411     -- Result: Computes "L >= R" where L and R are UNSIGNED vectors possibly
412     --         of different lengths.
413
414     -- Id: C.20
415  function ">=" ( L,R: SIGNED) return BOOLEAN;
416     -- Result subtype: BOOLEAN
417     -- Result: Computes "L >= R" where L and R are SIGNED vectors possibly
418     --         of different lengths.
419
420     -- Id: C.21
421  function ">=" ( L: NATURAL; R: UNSIGNED) return BOOLEAN;
422     -- Result subtype: BOOLEAN
423     -- Result: Computes "L >= R" where L is a non-negative INTEGER and
424     --         R is an UNSIGNED vector.
425
426     -- Id: C.22
427  function ">=" ( L: INTEGER; R: SIGNED) return BOOLEAN;
428     -- Result subtype: BOOLEAN
429     -- Result: Computes "L >= R" where L is an INTEGER and
430     --         R is a SIGNED vector.
431
432     -- Id: C.23
433  function ">=" ( L: UNSIGNED; R: NATURAL) return BOOLEAN;
434     -- Result subtype: BOOLEAN
435     -- Result: Computes "L >= R" where L is an UNSIGNED vector and
436     --         R is a non-negative INTEGER.
437
438     -- Id: C.24
439  function ">=" ( L: SIGNED; R: INTEGER) return BOOLEAN;
440     -- Result subtype: BOOLEAN
441     -- Result: Computes "L >= R" where L is a SIGNED vector and
442     --         R is an INTEGER.
443
444  --============================================================================
445
446     -- Id: C.25
447  function "=" (L,R: UNSIGNED ) return BOOLEAN;
448     -- Result subtype: BOOLEAN
449     -- Result: Computes "L = R" where L and R are UNSIGNED vectors possibly
450     --         of different lengths.
451
452     -- Id: C.26
453  function "=" ( L,R: SIGNED) return BOOLEAN;
454     -- Result subtype: BOOLEAN
455     -- Result: Computes "L = R" where L and R are SIGNED vectors possibly
456     --         of different lengths.
457
458     -- Id: C.27
459  function "=" ( L: NATURAL; R: UNSIGNED) return BOOLEAN;
460     -- Result subtype: BOOLEAN
461     -- Result: Computes "L = R" where L is a non-negative INTEGER and
462     --         R is an UNSIGNED vector.
463
464     -- Id: C.28
465  function "=" ( L: INTEGER; R: SIGNED) return BOOLEAN;
466     -- Result subtype: BOOLEAN
467     -- Result: Computes "L = R" where L is an INTEGER and
468     --         R is a SIGNED vector.
469
470     -- Id: C.29
471  function "=" ( L: UNSIGNED; R: NATURAL) return BOOLEAN;
472     -- Result subtype: BOOLEAN
473     -- Result: Computes "L = R" where L is an UNSIGNED vector and
474     --         R is a non-negative INTEGER.
475
476     -- Id: C.30
477  function "=" ( L: SIGNED; R: INTEGER) return BOOLEAN;
478     -- Result subtype: BOOLEAN
479     -- Result: Computes "L = R" where L is a SIGNED vector and
480     --         R is an INTEGER.
481
482  --============================================================================
483
484     -- Id: C.31
485  function "/=" (L,R: UNSIGNED ) return BOOLEAN;
486     -- Result subtype: BOOLEAN
487     -- Result: Computes "L /= R" where L and R are UNSIGNED vectors possibly
488     --         of different lengths.
489
490     -- Id: C.32
491  function "/=" ( L,R: SIGNED) return BOOLEAN;
492     -- Result subtype: BOOLEAN
493     -- Result: Computes "L /= R" where L and R are SIGNED vectors possibly
494     --         of different lengths.
495
496     -- Id: C.33
497  function "/=" ( L: NATURAL; R: UNSIGNED) return BOOLEAN;
498     -- Result subtype: BOOLEAN
499     -- Result: Computes "L /= R" where L is a non-negative INTEGER and
500     --         R is an UNSIGNED vector.
501
502     -- Id: C.34
503  function "/=" ( L: INTEGER; R: SIGNED) return BOOLEAN;
504     -- Result subtype: BOOLEAN
505     -- Result: Computes "L /= R" where L is an INTEGER and
506     --         R is a SIGNED vector.
507
508     -- Id: C.35
509  function "/=" ( L: UNSIGNED; R: NATURAL) return BOOLEAN;
510     -- Result subtype: BOOLEAN
511     -- Result: Computes "L /= R" where L is an UNSIGNED vector and
512     --         R is a non-negative INTEGER.
513
514     -- Id: C.36
515  function "/=" ( L: SIGNED; R: INTEGER) return BOOLEAN;
516     -- Result subtype: BOOLEAN
517     -- Result: Computes "L /= R" where L is a SIGNED vector and
518     --         R is an INTEGER.
519
520  --============================================================================
521  -- Shift and Rotate Functions
522  --============================================================================
523
524     -- Id: S.1
525  function shift_left  (  ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED;
526     -- Result subtype: UNSIGNED (ARG'LENGTH-1 downto 0)
527     -- Result: Performs a shift-left on an UNSIGNED vector COUNT times.
528     --         The vacated positions are filled with Bit '0'.
529     -- The COUNT leftmost bits are lost.
530
531     -- Id: S.2
532  function shift_right (  ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED;
533     -- Result subtype: UNSIGNED (ARG'LENGTH-1 downto 0)
534     -- Result: Performs a shift-right on an UNSIGNED vector COUNT times.
535     --         The vacated positions are filled with Bit '0'.
536     --         The COUNT rightmost bits are lost.
537
538     -- Id: S.3
539  function shift_left  (  ARG: SIGNED; COUNT: NATURAL) return SIGNED;
540     -- Result subtype: SIGNED (ARG'LENGTH-1 downto 0)
541     -- Result: Performs a shift-left on a SIGNED vector COUNT times.
542     --         All bits of ARG, except ARG'LEFT, are shifted left COUNT times.
543     --         The vacated positions are filled with Bit '0'.
544     --         The COUNT leftmost bits, except ARG'LEFT, are lost.
545
546     -- Id: S.4
547  function shift_right (  ARG: SIGNED; COUNT: NATURAL) return SIGNED;
548     -- Result subtype: SIGNED (ARG'LENGTH-1 downto 0)
549     -- Result: Performs a shift-right on a SIGNED vector COUNT times.
550     --         The vacated positions are filled with the leftmost bit,ARG'LEFT.
551     --         The COUNT rightmost bits are lost.
552
553  --============================================================================
554
555  --============================================================================
556
557     -- Id: S.5
558  function rotate_left  (  ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED;
559     -- Result subtype: UNSIGNED (ARG'LENGTH-1 downto 0)
560     -- Result: Performs a rotate_left of an UNSIGNED vector COUNT times.
561
562     -- Id: S.6
563  function rotate_right (  ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED;
564     -- Result subtype: UNSIGNED (ARG'LENGTH-1 downto 0)
565     -- Result: Performs a rotate_right of an UNSIGNED vector COUNT times.
566
567     -- Id: S.7
568  function rotate_left  (  ARG: SIGNED; COUNT: NATURAL) return SIGNED;
569     -- Result subtype: SIGNED (ARG'LENGTH-1 downto 0)
570     -- Result: Performs a logical rotate-left of a SIGNED
571     --         vector COUNT times.
572
573     -- Id: S.8
574  function rotate_right (  ARG: SIGNED; COUNT: NATURAL) return SIGNED;
575     -- Result subtype: SIGNED (ARG'LENGTH-1 downto 0)
576     -- Result: Performs a logical rotate-right of a SIGNED
577     --         vector COUNT times.
578
579  --============================================================================
580  --  RESIZE Functions
581  --============================================================================
582
583     -- Id: R.1
584  function RESIZE ( ARG: SIGNED; NEW_SIZE: NATURAL) return SIGNED;
585     -- Result subtype: SIGNED(ARG'LENGTH-1 downto 0)
586     -- Result: ReSIZEs the SIGNED vector ARG to the specified SIZE.
587     --         To create a larger vector, the new [leftmost] bit positions
588     --         are filled with the sign bit (ARG'LEFT). When truncating,
589     --         the sign bit is retained along with the rightmost part.
590
591     -- Id: R.2
592  function RESIZE ( ARG: UNSIGNED; NEW_SIZE: NATURAL) return UNSIGNED;
593     -- Result subtype: UNSIGNED(ARG'LENGTH-1 downto 0)
594     -- Result: ReSIZEs the SIGNED vector ARG to the specified SIZE.
595     --         To create a larger vector, the new [leftmost] bit positions
596     --         are filled with '0'. When truncating, the leftmost bits
597     --         are dropped.
598
599
600  --============================================================================
601  -- Conversion Functions
602  --============================================================================
603
604     -- Id: D.1
605  function TO_INTEGER ( ARG: UNSIGNED) return NATURAL;
606     -- Result subtype: NATURAL. Value cannot be negative since parameter is an
607     --                 UNSIGNED vector.
608     -- Result: Converts the UNSIGNED vector to an INTEGER.
609
610     -- Id: D.2
611  function TO_INTEGER ( ARG: SIGNED) return INTEGER;
612     -- Result subtype: INTEGER
613     -- Result: Converts a SIGNED vector to an INTEGER.
614
615     -- Id: D.3
616  function TO_UNSIGNED (  ARG,SIZE: NATURAL) return UNSIGNED;
617     -- Result subtype: UNSIGNED (SIZE-1 downto 0)
618     -- Result: Converts a non-negative INTEGER to an UNSIGNED vector with
619     --         the specified SIZE.
620
621     -- Id: D.4
622  function TO_SIGNED ( ARG: INTEGER; SIZE: NATURAL) return SIGNED;
623     -- Result subtype: SIGNED (SIZE-1 downto 0)
624     -- Result: Converts an INTEGER to a SIGNED vector of the specified SIZE.
625
626     -- Id: D.5
627  function TO_UNSIGNED ( ARG: STD_LOGIC_VECTOR) return UNSIGNED;
628     -- Result subtype: UNSIGNED, same range as input ARG
629     -- Result: Converts STD_LOGIC_VECTOR to UNSIGNED.
630
631     -- Id: D.6
632  function TO_SIGNED ( ARG: STD_LOGIC_VECTOR) return SIGNED;
633     -- Result subtype: SIGNED, same range as input ARG
634     -- Result: Converts STD_LOGIC_VECTOR to SIGNED.
635
636     -- Id: D.7
637  function TO_STDLOGICVECTOR ( ARG: UNSIGNED) return STD_LOGIC_VECTOR;
638     -- Result subtype: STD_LOGIC_VECTOR, same range as input ARG
639     -- Result: Converts UNSIGNED to STD_LOGIC_VECTOR.
640
641     -- Id: D.8
642  function TO_STDLOGICVECTOR ( ARG: SIGNED) return STD_LOGIC_VECTOR;
643     -- Result subtype: STD_LOGIC_VECTOR, same range as input ARG
644     -- Result: Converts SIGNED to STD_LOGIC_VECTOR.
645
646  --============================================================================
647  -- Logical Operators
648  --============================================================================
649
650     -- Id: L.1
651  function "not" ( L: UNSIGNED ) return UNSIGNED;
652     -- Result subtype: UNSIGNED, same range as input L
653     -- Result: Termwise inversion
654
655     -- Id: L.2
656  function "and" ( L,R: UNSIGNED ) return UNSIGNED;
657     -- Result subtype: UNSIGNED, same range as input L
658     -- Result: Vector AND operation
659
660     -- Id: L.3
661  function "or" ( L,R: UNSIGNED ) return UNSIGNED;
662     -- Result subtype: UNSIGNED, same range as input L
663     -- Result: Vector OR operation
664
665     -- Id: L.4
666  function "nand" ( L,R: UNSIGNED ) return UNSIGNED;
667     -- Result subtype: UNSIGNED, same range as input L
668     -- Result: Vector NAND operation
669
670     -- Id: L.5
671  function "nor" ( L,R: UNSIGNED ) return UNSIGNED;
672     -- Result subtype: UNSIGNED, same range as input L
673     -- Result: Vector NOR operation
674
675     -- Id: L.6
676  function "xor" ( L,R: UNSIGNED ) return UNSIGNED;
677     -- Result subtype: UNSIGNED, same range as input L
678     -- Result: Vector XOR operation
679
680--  -----------------------------------------------------------------------
681--  Note : The declaration and implementation of the "xnor" function is
682--  specifically commented until at which time the VHDL language has been
683--  officially adopted as containing such a function. At such a point,
684--  the following comments may be removed along with this notice without
685--  further "official" ballotting of this 1076.3 package. It is
686--  the intent of this effort to provide such a function once it becomes
687--  available in the VHDL standard.
688--  -----------------------------------------------------------------------
689     -- Id: L.7
690--  function "xnor" ( L,R: UNSIGNED ) return UNSIGNED;
691     -- Result subtype: UNSIGNED, same range as input L
692     -- Result: Vector XNOR operation
693
694     -- Id: L.8
695  function "not" ( L: SIGNED) return SIGNED;
696     -- Result subtype: SIGNED, same range as input L
697     -- Result: Termwise inversion
698
699     -- Id: L.9
700  function "and" ( L,R: SIGNED ) return SIGNED;
701     -- Result subtype: SIGNED, same range as input L
702     -- Result: Vector AND operation
703
704     -- Id: L.10
705  function "or" ( L,R: SIGNED ) return SIGNED;
706     -- Result subtype: SIGNED, same range as input L
707     -- Result: Vector OR operation
708
709     -- Id: L.11
710  function "nand" ( L,R: SIGNED ) return SIGNED;
711     -- Result subtype: SIGNED, same range as input L
712     -- Result: Vector NAND operation
713
714     -- Id: L.12
715  function "nor" ( L,R: SIGNED ) return SIGNED;
716     -- Result subtype: SIGNED, same range as input L
717     -- Result: Vector NOR operation
718
719     -- Id: L.13
720  function "xor" ( L,R: SIGNED ) return SIGNED;
721     -- Result subtype: SIGNED, same range as input L
722     -- Result: Vector XOR operation
723
724--  -----------------------------------------------------------------------
725--  Note : The declaration and implementation of the "xnor" function is
726--  specifically commented until at which time the VHDL language has been
727--  officially adopted as containing such a function. At such a point,
728--  the following comments may be removed along with this notice without
729--  further "official" ballotting of this 1076.3 package. It is
730--  the intent of this effort to provide such a function once it becomes
731--  available in the VHDL standard.
732--  -----------------------------------------------------------------------
733     -- Id: L.14
734--  function "xnor" ( L,R: SIGNED ) return SIGNED;
735     -- Result subtype: SIGNED, same range as input L
736     -- Result: Vector XNOR operation
737
738  --============================================================================
739  -- Match Functions
740  --============================================================================
741
742     -- Id: M.1
743  function STD_MATCH (L, R: STD_ULOGIC) return BOOLEAN;
744     -- Result: terms compared per STD_LOGIC_1164 intent
745
746     -- Id: M.2
747  function STD_MATCH (L, R: STD_LOGIC_VECTOR) return BOOLEAN;
748     -- Result: termwise comparison per STD_LOGIC_1164 intent
749
750  end numeric_std;
751
752--=============================================================================
753--=======================    Package    Body    ===============================
754--=============================================================================
755
756
757Package body numeric_std is
758
759
760   -- null range array constants
761
762constant NAU : UNSIGNED (0 downto 1) := (others => '0');
763constant NAS : SIGNED (0 downto 1) := (others => '0');
764
765   -- implementation controls
766
767constant NO_WARNING : boolean := TRUE; -- Edwin changed to true -- default to emit warnings
768
769--=========================Local Subprograms=================================
770
771function MAX(LEFT, RIGHT: INTEGER) return INTEGER is
772begin
773  if LEFT > RIGHT then return LEFT;
774  else return RIGHT;
775    end if;
776  end;
777
778function MIN(LEFT, RIGHT: INTEGER) return INTEGER is
779begin
780  if LEFT < RIGHT then return LEFT;
781  else return RIGHT;
782    end if;
783  end;
784
785function Signed_NUM_BITS ( ARG: INTEGER) return natural is
786variable nBits: natural;
787variable N: natural;
788begin
789  if ARG>=0 then
790    N:=ARG;
791  else
792    N:=-(ARG+1);
793    end if;
794  nBits:=1;
795  while N>0 loop
796    nBits:=nBits+1;
797    N:= N / 2;
798  end loop;
799  return nBits;
800  end;
801
802function UNSIGNED_NUM_BITS (ARG: natural) return natural is
803variable nBits: natural;
804variable N: natural;
805begin
806  N:=ARG;
807  nBits:=1;
808  while N>1 loop
809    nBits:=nBits+1;
810    N:= N / 2;
811    end loop;
812  return nBits;
813  end;
814
815
816------------------------------------------------------------------------
817
818 -- this internal function computes the addition of two UNSIGNED
819 -- with input CARRY
820 -- * the two arguments are of the same length
821
822function ADD_UNSIGNED ( L,R: UNSIGNED; C: STD_LOGIC ) return UNSIGNED  is
823constant L_left:INTEGER:= L'length-1;
824alias XL: UNSIGNED(L_left downto 0) is L;
825alias XR: UNSIGNED(L_left downto 0) is R;
826variable RESULT: UNSIGNED(L_left downto 0);
827variable CBIT : STD_LOGIC:= C;
828begin
829  for i in 0 to L_left loop
830    RESULT(i) := CBIT xor XL(i) xor XR(i);
831    CBIT := (CBIT and XL(i)) or (CBIT and XR(i)) or (XL(i) and XR(i));
832    end loop;
833  return RESULT;
834  end ADD_UNSIGNED;
835
836 -- this internal function computes the addition of two SIGNED
837 -- with input CARRY
838 -- * the two arguments are of the same length
839
840function ADD_SIGNED ( L,R: SIGNED; C: STD_LOGIC ) return SIGNED  is
841constant L_left:INTEGER:= L'length-1;
842alias XL: SIGNED(L_left downto 0) is L;
843alias XR: SIGNED(L_left downto 0) is R;
844variable RESULT: SIGNED(L_left downto 0);
845variable CBIT: STD_LOGIC:= C;
846begin
847  for i in 0 to L_left loop
848    RESULT(i) := CBIT xor XL(i) xor XR(i);
849    CBIT := (CBIT and XL(i)) or (CBIT and XR(i)) or (XL(i) and XR(i));
850    end loop;
851  return RESULT;
852  end ADD_SIGNED;
853
854------------------------------------------------------------------------
855
856    -- this internal procedure computes UNSIGNED division
857    -- giving the quotient and remainder.
858    procedure divMod (num, XDENOM: UNSIGNED;
859                      xquot, xremain: out UNSIGNED) is
860       variable TEMP: UNSIGNED(num'length-1 downto 0);
861       variable quot: UNSIGNED(MAX(num'length,XDENOM'length)-1 downto 0);
862       variable diff: UNSIGNED(XDENOM'length downto 0);
863       alias DENOM : UNSIGNED (XDENOM'length-1 downto 0) is XDENOM;
864       variable CARRY: STD_LOGIC;
865       variable TOPBIT: natural;
866       variable isZero: boolean;
867    begin
868       isZero:=TRUE;
869       for j in XDENOM'range loop
870          CARRY:=DENOM(j);
871          if CARRY/='0' then
872             isZero:=FALSE;
873          end if;
874       end loop;
875       assert not isZero
876         report "DIV,MOD,or REM by zero"
877         severity error;
878       TEMP:=num;
879       quot:= (others =>'0');
880       TOPBIT:=0;
881       for j in DENOM'range loop
882          if DENOM(j)='1' then
883             TOPBIT:=j;
884             exit;
885          end if;
886       end loop;
887
888       CARRY:='0';
889       for j in num'length-(TOPBIT+1) downto 0 loop
890          -- lexical ordering works okay for this comparison, no overloaded
891          -- function is needed.
892          if CARRY&TEMP(TOPBIT+j downto j) >= "0"&DENOM(TOPBIT downto 0) then
893             diff(TOPBIT+1 downto 0) := (CARRY&TEMP(TOPBIT+j downto j))
894                                       -("0"&DENOM(TOPBIT downto 0));
895
896             assert diff(TOPBIT+1)='0'
897                   report "internal error in the division algorithm"
898                   severity error;
899             CARRY:=diff(TOPBIT);
900             if TOPBIT+j+1<=TEMP'left then
901                TEMP(TOPBIT+j+1):='0';
902             end if;
903             TEMP(TOPBIT+j downto j):=diff(TOPBIT downto 0);
904             quot(j):='1';
905          else
906             assert CARRY='0'
907                   report "internal error in the division algorithm"
908                   severity error;
909             CARRY:=TEMP(TOPBIT+j);
910          end if;
911       end loop;
912       xquot:=quot(num'length-1 downto 0);
913       xremain:=TEMP(num'length-1 downto 0);
914    end divMod;
915
916-----------------Local Subprograms - shift/rotate ops-------------------------
917
918function XSLL(ARG: STD_LOGIC_VECTOR; COUNT: NATURAL) return STD_LOGIC_VECTOR is
919constant ARG_L:INTEGER:= ARG'length-1;
920alias XARG: STD_LOGIC_VECTOR(ARG_L downto 0) is ARG;
921variable RESULT: STD_LOGIC_VECTOR(ARG_L downto 0) := (others=>'0');
922begin
923  if COUNT <= ARG_L then
924    RESULT(ARG_L downto COUNT):=XARG(ARG_L-COUNT downto 0);
925    end if;
926  return RESULT;
927end;
928
929function XSRL(ARG: STD_LOGIC_VECTOR; COUNT: NATURAL) return STD_LOGIC_VECTOR is
930constant ARG_L:INTEGER:= ARG'length-1;
931alias XARG: STD_LOGIC_VECTOR(ARG_L downto 0) is ARG;
932variable RESULT: STD_LOGIC_VECTOR(ARG_L downto 0) := (others=>'0');
933begin
934  if COUNT <= ARG_L then
935    RESULT(ARG_L-COUNT downto 0):=XARG(ARG_L downto COUNT);
936    end if;
937  return RESULT;
938end;
939
940function XSRA(ARG: STD_LOGIC_VECTOR; COUNT: NATURAL) return STD_LOGIC_VECTOR is
941constant ARG_L:INTEGER:= ARG'length-1;
942alias XARG: STD_LOGIC_VECTOR(ARG_L downto 0) is ARG;
943variable RESULT: STD_LOGIC_VECTOR(ARG_L downto 0);
944variable XCOUNT: natural := COUNT;
945begin
946  if ((ARG'length <= 1) or (xCOUNT = 0)) then return ARG;
947  else
948    if (XCOUNT > ARG_L) then xCOUNT:= ARG_L; end if;
949    RESULT(ARG_L-XCOUNT downto 0):=XARG(ARG_L downto XCOUNT);
950    RESULT(ARG_L downto (ARG_L - XCOUNT + 1)) := (others=>XARG(ARG_L));
951    end if;
952  return RESULT;
953  end;
954
955function XROL(ARG: STD_LOGIC_VECTOR; COUNT: NATURAL) return STD_LOGIC_VECTOR is
956constant ARG_L:INTEGER:= ARG'length-1;
957alias XARG: STD_LOGIC_VECTOR(ARG_L downto 0) is ARG;
958variable RESULT: STD_LOGIC_VECTOR(ARG_L downto 0) := XARG;
959variable COUNTM: INTEGER;
960begin
961  COUNTM:= COUNT mod (ARG_L + 1);
962  if COUNTM /= 0 then
963    RESULT(ARG_L downto COUNTM):=XARG(ARG_L-COUNTM downto 0);
964    RESULT(COUNTM-1 downto 0):=XARG(ARG_L downto ARG_L-COUNTM+1);
965    end if;
966  return RESULT;
967  end;
968
969function XROR(ARG: STD_LOGIC_VECTOR; COUNT: NATURAL) return STD_LOGIC_VECTOR is
970constant ARG_L:INTEGER:= ARG'length-1;
971alias XARG: STD_LOGIC_VECTOR(ARG_L downto 0) is ARG;
972variable RESULT: STD_LOGIC_VECTOR(ARG_L downto 0) := XARG;
973variable COUNTM: INTEGER;
974begin
975  COUNTM:= COUNT mod (ARG_L + 1);
976  if COUNTM /= 0 then
977    RESULT(ARG_L-COUNTM downto 0):=XARG(ARG_L downto COUNTM);
978    RESULT(ARG_L downto ARG_L-COUNTM+1):=XARG(COUNTM-1 downto 0);
979    end if;
980  return RESULT;
981  end;
982
983
984-----------------Local Subprograms - Relational ops--------------------------
985
986--
987-- General "=" for UNSIGNED vectors, same length
988--
989function UNSIGNED_equal ( L,R: UNSIGNED) return BOOLEAN is
990begin
991  return STD_LOGIC_VECTOR (L) = STD_LOGIC_VECTOR (R) ;
992  end;
993
994--
995-- General "=" for SIGNED vectors, same length
996--
997function SIGNED_equal ( L,R: SIGNED) return BOOLEAN is
998begin
999  return STD_LOGIC_VECTOR (L) = STD_LOGIC_VECTOR (R) ;
1000  end;
1001
1002
1003
1004--
1005-- General "<" for UNSIGNED vectors, same length
1006--
1007function UNSIGNED_LESS ( L,R: UNSIGNED) return BOOLEAN is
1008begin
1009  return STD_LOGIC_VECTOR (L) < STD_LOGIC_VECTOR (R) ;
1010  end UNSIGNED_LESS ;
1011
1012--
1013-- General "<" function for SIGNED vectors, same length
1014--
1015function SIGNED_LESS ( L,R: SIGNED) return BOOLEAN is
1016variable intern_l : SIGNED (0 to L'LENGTH-1);
1017variable intern_r : SIGNED (0 to R'LENGTH-1);
1018begin
1019  intern_l:=l;
1020  intern_r:=r;
1021  intern_l(0):=not intern_l(0);
1022  intern_r(0):=not intern_r(0);
1023  return STD_LOGIC_VECTOR (intern_L) < STD_LOGIC_VECTOR (intern_R) ;
1024end;
1025
1026--
1027-- General "<=" function for UNSIGNED vectors, same length
1028--
1029function UNSIGNED_LESS_OR_EQUAL ( L,R: UNSIGNED) return BOOLEAN is
1030begin
1031  return STD_LOGIC_VECTOR (L) <= STD_LOGIC_VECTOR (R) ;
1032  end;
1033
1034--
1035-- General "<=" function for SIGNED vectors, same length
1036--
1037function SIGNED_LESS_OR_EQUAL ( L,R: SIGNED) return BOOLEAN is
1038  -- Need aliasses to assure index direction
1039variable intern_l : SIGNED (0 to L'LENGTH-1);
1040variable intern_r : SIGNED (0 to R'LENGTH-1);
1041begin
1042  intern_l:=l;
1043  intern_r:=r;
1044  intern_l(0):=not intern_l(0);
1045  intern_r(0):=not intern_r(0);
1046  return STD_LOGIC_VECTOR (intern_L) <= STD_LOGIC_VECTOR (intern_R) ;
1047  end;
1048
1049-- function TO_01 is used to convert vectors to the
1050--          correct form for exported functions,
1051--          and to report if there is an element which
1052--          is not in (0,1,h,l).
1053--    Assume the vector is normalized and non-null.
1054--    The function is duplicated for SIGNED and UNSIGNED types.
1055
1056function TO_01(S : SIGNED ; xmap : STD_LOGIC:= '0') return SIGNED is
1057variable RESULT: SIGNED(S'length-1 downto 0);
1058variable bad_element : boolean := FALSE;
1059alias xs : SIGNED(s'length-1 downto 0) is S;
1060begin
1061  for i in RESULT'range loop
1062    case xs(i) is
1063      when '0' | 'L' => RESULT(i):='0';
1064      when '1' | 'H' => RESULT(i):='1';
1065      when others => bad_element := TRUE;
1066      end case;
1067    end loop;
1068  if bad_element then
1069    assert NO_WARNING
1070      report "numeric_std.TO_01: Array Element not in {0,1,H,L}"
1071      severity warning;
1072    for i in RESULT'range loop
1073      RESULT(i) := xmap;        -- standard fixup
1074      end loop;
1075    end if;
1076  return RESULT;
1077  end TO_01;
1078
1079function TO_01(S : UNSIGNED ; xmap : STD_LOGIC:= '0') return UNSIGNED is
1080variable RESULT: UNSIGNED(S'length-1 downto 0);
1081variable bad_element : boolean := FALSE;
1082alias xs : UNSIGNED(S'length-1 downto 0) is S;
1083begin
1084  for i in RESULT'range loop
1085    case xs(i) is
1086      when '0' | 'L' => RESULT(i):='0';
1087      when '1' | 'H' => RESULT(i):='1';
1088      when others => bad_element := TRUE;
1089      end case;
1090    end loop;
1091  if bad_element then
1092    assert NO_WARNING
1093      report "numeric_std.TO_01: Array Element not in {0,1,H,L}"
1094      severity warning;
1095    for i in RESULT'range loop
1096      RESULT(i) := xmap;        -- standard fixup
1097      end loop;
1098    end if;
1099  return RESULT;
1100  end TO_01;
1101
1102--=========================Exported Functions=================================
1103
1104     -- Id: A.1
1105function "abs" ( X : SIGNED) return SIGNED is
1106constant ARG_LEFT:INTEGER:= X'length-1;
1107alias XX : SIGNED(ARG_LEFT downto 0) is X;
1108variable RESULT: SIGNED (ARG_LEFT downto 0);
1109begin
1110  if X'length<1 then return NAS; end if;
1111  RESULT:=TO_01(xx,'X');
1112  if (RESULT(RESULT'left)='X') then return RESULT; end if;
1113  if RESULT(RESULT'left) = '1' then
1114    RESULT:= -RESULT;
1115    end if;
1116  return RESULT;
1117  end; -- "abs"
1118
1119
1120     -- Id: A.2
1121function "-" ( ARG: SIGNED) return SIGNED is
1122constant ARG_LEFT:INTEGER:= ARG'length-1;
1123alias XARG: SIGNED(ARG_LEFT downto 0) is ARG;
1124variable RESULT,XARG01 : SIGNED(ARG_LEFT downto 0);
1125variable CBIT : STD_LOGIC:= '1';
1126begin
1127  if ARG'length<1 then return NAS; end if;
1128  XARG01 := TO_01(ARG,'X');
1129  if (XARG01(XARG01'left)='X') then return XARG01; end if;
1130  for i in 0 to RESULT'left loop
1131    RESULT(i) := not(XARG01(i)) xor CBIT;
1132    CBIT := CBIT and not(XARG01(i));
1133    end loop;
1134  return RESULT;
1135  end; -- "-"
1136
1137
1138--=============================================================================
1139
1140
1141     -- Id: A.3
1142function "+" ( L,R: UNSIGNED ) return UNSIGNED is
1143constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1144variable L01 : UNSIGNED(SIZE-1 downto 0);
1145variable R01 : UNSIGNED(SIZE-1 downto 0);
1146begin
1147  if ((L'length<1) or (R'length<1)) then return NAU; end if;
1148  L01 := TO_01(RESIZE(L,SIZE),'X');
1149  if (L01(L01'left)='X') then return L01; end if;
1150  R01 := TO_01(RESIZE(R,SIZE),'X');
1151  if (R01(R01'left)='X') then return R01; end if;
1152  return ADD_UNSIGNED (L01, R01, '0') ;
1153  end;
1154
1155     -- Id: A.4
1156function "+" ( L,R: SIGNED ) return SIGNED is
1157constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1158variable L01 : SIGNED(SIZE-1 downto 0);
1159variable R01 : SIGNED(SIZE-1 downto 0);
1160begin
1161  if ((L'length<1) or (R'length<1)) then return NAS; end if;
1162  L01 := TO_01(RESIZE(L,SIZE),'X');
1163  if (L01(L01'left)='X') then return L01; end if;
1164  R01 := TO_01(RESIZE(R,SIZE),'X');
1165  if (R01(R01'left)='X') then return R01; end if;
1166  return ADD_SIGNED (L01, R01, '0') ;
1167  end;
1168
1169     -- Id: A.5
1170function "+" ( L: UNSIGNED; R: NATURAL) return UNSIGNED is
1171begin
1172  return L + TO_UNSIGNED( R , L'length);
1173  end;
1174
1175     -- Id: A.6
1176function "+" ( L: NATURAL; R: UNSIGNED) return UNSIGNED is
1177begin
1178  return TO_UNSIGNED( L , R'length) + R;
1179  end;
1180
1181     -- Id: A.7
1182function "+" ( L: SIGNED; R: INTEGER) return SIGNED is
1183begin
1184  return L + TO_SIGNED( R , L'length);
1185  end;
1186
1187     -- Id: A.8
1188function "+" ( L: INTEGER; R: SIGNED) return SIGNED is
1189begin
1190  return TO_SIGNED( L , R'length) + R;
1191  end;
1192
1193--=============================================================================
1194
1195     -- Id: A.9
1196function "-" ( L,R: UNSIGNED) return UNSIGNED  is
1197constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1198variable L01 : UNSIGNED(SIZE-1 downto 0);
1199variable R01 : UNSIGNED(SIZE-1 downto 0);
1200begin
1201  if ((L'length<1) or (R'length<1)) then return NAU; end if;
1202  L01 := TO_01(RESIZE(L,SIZE),'X');
1203  if (L01(L01'left)='X') then return L01; end if;
1204  R01 := TO_01(RESIZE(R,SIZE),'X');
1205  if (R01(R01'left)='X') then return R01; end if;
1206  return ADD_UNSIGNED (L01,not(R01),'1');
1207  end;
1208
1209     -- Id: A.10
1210function "-" ( L,R: SIGNED) return SIGNED is
1211constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1212variable L01 : SIGNED(SIZE-1 downto 0);
1213variable R01 : SIGNED(SIZE-1 downto 0);
1214begin
1215  if ((L'length<1) or (R'length<1)) then return NAS; end if;
1216  L01 := TO_01(RESIZE(L,SIZE),'X');
1217  if (L01(L01'left)='X') then return L01; end if;
1218  R01 := TO_01(RESIZE(R,SIZE),'X');
1219  if (R01(R01'left)='X') then return R01; end if;
1220  return ADD_SIGNED (L01,not(R01),'1');
1221  end;
1222
1223
1224
1225     -- Id: A.11
1226function "-" ( L: UNSIGNED; R: NATURAL) return UNSIGNED is
1227begin
1228  return L -  TO_UNSIGNED( R , L'length);
1229  end;
1230
1231     -- Id: A.12
1232function "-" ( L: NATURAL; R: UNSIGNED) return UNSIGNED is
1233begin
1234  return TO_UNSIGNED( L , R'length) - R;
1235  end;
1236
1237     -- Id: A.13
1238function "-" ( L: SIGNED; R: INTEGER) return SIGNED is
1239begin
1240  return L - TO_SIGNED( R , L'length);
1241  end;
1242
1243     -- Id: A.14
1244function "-" ( L: INTEGER; R: SIGNED) return SIGNED is
1245begin
1246  return  TO_SIGNED( L , R'length) - R ;
1247  end;
1248
1249
1250--=============================================================================
1251
1252     -- Id: A.15
1253function "*" ( L,R: UNSIGNED) return UNSIGNED is
1254constant L_left:INTEGER:= L'length-1;
1255constant R_left:INTEGER:= R'length-1;
1256alias xxl : UNSIGNED(L_left downto 0) is L;
1257alias xxr : UNSIGNED(R_left downto 0) is R;
1258variable xl : UNSIGNED(L_left downto 0);
1259variable xr : UNSIGNED(R_left downto 0);
1260variable RESULT: UNSIGNED((L'length+R'length-1) downto 0) :=(others=>'0');
1261variable adval : UNSIGNED((L'length+R'length-1) downto 0);
1262begin
1263  if ((L'length<1) or (R'length<1)) then return NAU; end if;
1264  xl := TO_01(xxl,'X');
1265  xr := TO_01(xxr,'X');
1266  if ((xl(xl'left)='X') or (xr(xr'left)='X')) then
1267    RESULT:= (others=>'X');
1268    return RESULT;
1269    end if;
1270  adval := RESIZE(xr,RESULT'length);
1271  for i in 0 to L_left loop
1272    if xl(i)='1' then RESULT:= RESULT + adval;
1273    end if;
1274    adval := shift_left(adval,1);
1275    end loop;
1276  return RESULT;
1277end;
1278
1279     -- Id: A.16
1280function "*" ( L,R: SIGNED) return SIGNED is
1281constant L_left:INTEGER:= L'length-1;
1282constant R_left:INTEGER:= R'length-1;
1283alias xxl : SIGNED(L_left downto 0) is L;
1284alias xxr : SIGNED(R_left downto 0) is R;
1285variable xl : SIGNED(L_left downto 0);
1286variable xr : SIGNED(R_left downto 0);
1287variable RESULT: SIGNED((L'length+R'length-1) downto 0) :=(others=>'0');
1288variable adval : SIGNED((L'length+R'length-1) downto 0);
1289variable invt : STD_LOGIC:= '0';
1290begin
1291  if ((L'length<1) or (R'length<1)) then return NAS; end if;
1292  xl := TO_01(xxl,'X');
1293  xr := TO_01(xxr,'X');
1294  if ((xl(xl'left)='X') or (xr(xr'left)='X')) then
1295    RESULT:= (others=>'X');
1296    return RESULT;
1297    end if;
1298  adval := RESIZE(xr,RESULT'length);
1299  if xl(xl'left)='1' then
1300    adval := -(adval);
1301    invt := '1';
1302    end if;
1303  for i in 0 to L_left loop
1304    if (invt xor xl(i))='1' then RESULT:= RESULT + adval;
1305    end if;
1306    adval := shift_left(adval,1);
1307    end loop;
1308  return RESULT;
1309  end;
1310
1311
1312     -- Id: A.17
1313function "*" ( L: UNSIGNED; R: NATURAL) return UNSIGNED is
1314begin
1315  return L *  TO_UNSIGNED( R , L'length);
1316  end;
1317
1318     -- Id: A.18
1319function "*" ( L: NATURAL; R: UNSIGNED) return UNSIGNED is
1320begin
1321  return TO_UNSIGNED( L , R'length) * R;
1322  end;
1323
1324     -- Id: A.19
1325function "*" ( L: SIGNED; R: INTEGER) return SIGNED is
1326begin
1327  return L * TO_SIGNED( R , L'length);
1328  end;
1329
1330     -- Id: A.20
1331function "*" ( L: INTEGER; R: SIGNED) return SIGNED is
1332begin
1333  return  TO_SIGNED( L , R'length) * R ;
1334  end;
1335
1336--=============================================================================
1337
1338     -- Id: A.21
1339function "/" ( L,R: UNSIGNED) return UNSIGNED is
1340constant L_left:INTEGER:= L'length-1;
1341constant R_left:INTEGER:= R'length-1;
1342alias xxl : UNSIGNED(L_left downto 0) is L;
1343alias xxr : UNSIGNED(R_left downto 0) is R;
1344variable xl : UNSIGNED(L_left downto 0);
1345variable xr : UNSIGNED(R_left downto 0);
1346variable fquot,fremain : UNSIGNED(L'length-1 downto 0);
1347begin
1348  if ((L'length<1) or (R'length<1)) then return NAU; end if;
1349  xl := TO_01(xxl,'X');
1350  xr := TO_01(xxr,'X');
1351  if ((xl(xl'left)='X') or (xr(xr'left)='X')) then
1352    fquot := (others=>'X');
1353    return fquot;
1354    end if;
1355  divMod(xl,xr,fquot,fremain);
1356  return fquot;
1357  end;
1358
1359     -- Id: A.22
1360function "/" ( L,R: SIGNED) return SIGNED is
1361constant L_left:INTEGER:= L'length-1;
1362constant R_left:INTEGER:= R'length-1;
1363alias xxl : SIGNED(L_left downto 0) is L;
1364alias xxr : SIGNED(R_left downto 0) is R;
1365variable xl : SIGNED(L_left downto 0);
1366variable xr : SIGNED(R_left downto 0);
1367variable fquot,fremain : UNSIGNED(L'length-1 downto 0);
1368variable xnum: UNSIGNED(L'length-1 downto 0);
1369variable XDENOM: UNSIGNED(R'length-1 downto 0);
1370variable qneg: boolean := FALSE;
1371begin
1372  if ((L'length<1) or (R'length<1)) then return NAS; end if;
1373  xl := TO_01(xxl,'X');
1374  xr := TO_01(xxr,'X');
1375  if ((xl(xl'left)='X') or (xr(xr'left)='X')) then
1376    fquot := (others=>'X');
1377    return SIGNED(fquot);
1378    end if;
1379  if xl(xl'left)='1' then
1380    xnum:=UNSIGNED(-xl);
1381    qNeg:=TRUE;
1382  else
1383    xnum:=UNSIGNED(xl);
1384    end if;
1385  if xr(xr'left)='1' then
1386    xdenom:=UNSIGNED(-xr);
1387    qNeg:=not qNeg;
1388  else
1389    xdenom:=UNSIGNED(xr);
1390    end if;
1391  divMod(xnum,XDENOM,fquot,fremain);
1392  if qNeg then fquot:="0"-fquot;  end if;
1393  return SIGNED(fquot);
1394  end;
1395
1396     -- Id: A.23
1397function "/" ( L : UNSIGNED; R : NATURAL) return UNSIGNED is
1398  constant R_Length : Natural := max(L'Length, unsigned_num_bits(R));
1399  variable XR,Quot : unsigned (R_Length-1 downto 0);
1400begin
1401  XR := TO_UNSIGNED(R, R_Length);
1402  Quot := L / XR;
1403  if R_Length>L'Length and Quot(0)/='X'
1404      and Quot(R_Length-1 downto L'Length)/=(R_Length-1 downto L'Length => '0')
1405  then
1406    ASSERT no_warning report "Numeric_std.""/"": Quotient Truncated"
1407          severity warning;
1408  end if;
1409  return Quot(L'Length-1 downto 0);
1410  end;
1411
1412     -- Id: A.24
1413function "/" ( L : NATURAL; R : UNSIGNED) return UNSIGNED is
1414  constant L_Length : Natural := max(unsigned_num_bits(L), R'length);
1415  variable XL,Quot : UNSIGNED (L_Length-1 downto 0);
1416begin
1417  XL := TO_UNSIGNED(L,L_LENGTH);
1418  QUOT := XL / R;
1419  if L_LENGTH>R'LENGTH and Quot(0)/='X'
1420      and QUOT(L_LENGTH-1 downto R'Length)/=(L_LENGTH-1 downto R'Length => '0')
1421  then
1422    ASSERT no_warning report "Numeric_std.""/"": Quotient Truncated"
1423          severity warning;
1424  end if;
1425  return Quot(R'Length-1 downto 0);
1426  end;
1427
1428     -- Id: A.25
1429function "/" ( L : SIGNED; R : INTEGER ) return SIGNED is
1430  constant R_Length : Natural := max(L'Length, signed_num_bits(R));
1431  variable XR,Quot : signed (R_Length-1 downto 0);
1432begin
1433  XR := TO_SIGNED(R, R_Length);
1434  Quot := L / XR;
1435  if R_Length>L'Length and Quot(0)/='X'
1436        and Quot(R_Length-1 downto L'Length)
1437            /= (R_Length-1 downto L'Length => Quot(L'Length-1))
1438  then
1439    ASSERT no_warning report "Numeric_std.""/"": Quotient Truncated"
1440          severity warning;
1441  end if;
1442  return Quot(L'Length-1 downto 0);
1443  end;
1444
1445     -- Id: A.26
1446function "/" ( L : INTEGER; R : SIGNED) return SIGNED is
1447  constant L_Length : Natural := max(signed_num_bits(L), R'length);
1448  variable XL,Quot : SIGNED (L_Length-1 downto 0);
1449begin
1450  XL := TO_SIGNED(L,L_LENGTH);
1451  QUOT := XL / R;
1452  if L_LENGTH>R'LENGTH and Quot(0)/='X'
1453        and QUOT(L_LENGTH-1 downto R'Length)
1454           /= (L_LENGTH-1 downto R'Length => Quot(R'Length-1))
1455  then
1456    ASSERT no_warning report "Numeric_std.""/"": Quotient Truncated"
1457          severity warning;
1458  end if;
1459  return Quot(R'Length-1 downto 0);
1460  end;
1461--=============================================================================
1462
1463     -- Id: A.27
1464function "rem" ( L,R: UNSIGNED) return UNSIGNED is
1465constant L_left:INTEGER:= L'length-1;
1466constant R_left:INTEGER:= R'length-1;
1467alias xxl : UNSIGNED(L_left downto 0) is L;
1468alias xxr : UNSIGNED(R_left downto 0) is R;
1469variable xl : UNSIGNED(L_left downto 0);
1470variable xr : UNSIGNED(R_left downto 0);
1471variable fquot,fremain : UNSIGNED(l'length-1 downto 0);
1472begin
1473  if ((L'length<1) or (R'length<1)) then return NAU; end if;
1474  xl := TO_01(xxl,'X');
1475  xr := TO_01(xxr,'X');
1476  if ((xl(xl'left)='X') or (xr(xr'left)='X')) then
1477    fremain := (others=>'X');
1478    return fremain;
1479    end if;
1480  divMod(xl,xr,fquot,fremain);
1481  return fremain;
1482  end;
1483
1484     -- Id: A.28
1485function "rem" ( L,R: SIGNED) return SIGNED is
1486constant L_left:INTEGER:= L'length-1;
1487constant R_left:INTEGER:= R'length-1;
1488alias xxl : SIGNED(L_left downto 0) is L;
1489alias xxr : SIGNED(R_left downto 0) is R;
1490variable fquot,fremain : UNSIGNED(l'length-1 downto 0);
1491variable xnum: UNSIGNED(l'length-1 downto 0);
1492variable XDENOM: UNSIGNED(r'length-1 downto 0);
1493variable rneg: boolean := FALSE;
1494begin
1495  if ((L'length<1) or (R'length<1)) then return NAS; end if;
1496  xnum := UNSIGNED(TO_01(xxl,'X'));
1497  XDENOM := UNSIGNED(TO_01(xxr,'X'));
1498  if ((xnum(xnum'left)='X') or (xdenom(xnum'left)='X')) then
1499    fremain := (others=>'X');
1500    return SIGNED(fremain);
1501    end if;
1502  if xnum(xnum'left)='1' then
1503     xnum:=UNSIGNED(-SIGNED(xnum));
1504     rNeg:=TRUE;
1505  else
1506     xNum:=UNSIGNED(xnum);
1507  end if;
1508  if XDENOM(XDENOM'left)='1' then
1509     XDENOM:=UNSIGNED(-SIGNED(XDENOM));
1510  else
1511     XDENOM:=UNSIGNED(XDENOM);
1512  end if;
1513  divMod(xnum,XDENOM,fquot,fremain);
1514  if rNeg then
1515     fremain:="0"-fremain;
1516  end if;
1517  return SIGNED(fremain);
1518  end;
1519
1520     -- Id: A.29
1521function "rem" ( L : UNSIGNED; R : NATURAL) return UNSIGNED is
1522  constant R_Length : Natural := max(L'Length, unsigned_num_bits(R));
1523  variable XR,XRem : unsigned (R_Length-1 downto 0);
1524begin
1525  XR := TO_UNSIGNED(R, R_Length);
1526  XRem := L rem XR;
1527  if R_Length>L'Length and XRem(0)/='X'
1528         and XRem(R_Length-1 downto L'Length)
1529          /= (R_Length-1 downto L'Length => '0')
1530  then
1531    ASSERT no_warning report "Numeric_std.""rem"": Remainder Truncated"
1532          severity warning;
1533  end if;
1534  return XRem(L'Length-1 downto 0);
1535end;
1536
1537     -- Id: A.30
1538function "rem" ( L : NATURAL; R : UNSIGNED) return UNSIGNED is
1539  constant L_Length : Natural := max(unsigned_num_bits(L), R'Length);
1540  variable XL,XRem : unsigned (L_Length-1 downto 0);
1541begin
1542  XL := TO_UNSIGNED(L, L_Length);
1543  XRem := XL rem R;
1544  if L_Length>R'Length and XRem(0)/='X'
1545         and XRem(L_Length-1 downto R'Length)
1546          /= (L_Length-1 downto R'Length => '0')
1547  then
1548    ASSERT no_warning report "Numeric_std.""rem"": Remainder Truncated"
1549          severity warning;
1550  end if;
1551  return XRem(R'Length-1 downto 0);
1552end;
1553
1554     -- Id: A.31
1555function "rem" ( L : SIGNED; R : INTEGER ) return SIGNED is
1556  constant R_Length : Natural := max(L'Length, signed_num_bits(R));
1557  variable XR,XRem : signed (R_Length-1 downto 0);
1558begin
1559  XR := TO_SIGNED(R, R_Length);
1560  XRem := L rem XR;
1561  if R_Length>L'Length and XRem(0)/='X'
1562         and XRem(R_Length-1 downto L'Length)
1563          /= (R_Length-1 downto L'Length => XRem(L'Length-1))
1564  then
1565    ASSERT no_warning report "Numeric_std.""rem"": Remainder Truncated"
1566          severity warning;
1567  end if;
1568  return XRem(L'Length-1 downto 0);
1569end;
1570
1571     -- Id: A.32
1572function "rem" ( L : INTEGER; R : SIGNED) return SIGNED is
1573  constant L_Length : Natural := max(signed_num_bits(L), R'Length);
1574  variable XL,XRem : signed (L_Length-1 downto 0);
1575begin
1576  XL := TO_SIGNED(L, L_Length);
1577  XRem := XL rem R;
1578  if L_Length>R'Length and XRem(0)/='X'
1579         and XRem(L_Length-1 downto R'Length)
1580          /= (L_Length-1 downto R'Length => XRem(R'Length-1))
1581  then
1582    ASSERT no_warning report "Numeric_std.""rem"": Remainder Truncated"
1583          severity warning;
1584  end if;
1585  return XRem(R'Length-1 downto 0);
1586end;
1587
1588--=============================================================================
1589
1590     -- Id: A.33
1591function "mod" ( L,R: UNSIGNED) return UNSIGNED is
1592constant L_left:INTEGER:= L'length-1;
1593constant R_left:INTEGER:= R'length-1;
1594alias xxl : UNSIGNED(L_left downto 0) is L;
1595alias xxr : UNSIGNED(R_left downto 0) is R;
1596variable xl : UNSIGNED(L_left downto 0);
1597variable xr : UNSIGNED(R_left downto 0);
1598variable fquot,fremain : UNSIGNED(l'length-1 downto 0);
1599begin
1600  if ((L'length<1) or (R'length<1)) then return NAU; end if;
1601  xl := TO_01(xxl,'X');
1602  xr := TO_01(xxr,'X');
1603  if ((xl(xl'left)='X') or (xr(xr'left)='X')) then
1604    fremain := (others=>'X');
1605    return fremain;
1606    end if;
1607  divMod(xl,xr,fquot,fremain);
1608  return fremain;
1609  end;
1610
1611     -- Id: A.34
1612function "mod" ( L,R: SIGNED) return SIGNED is
1613constant L_left:INTEGER:= L'length-1;
1614constant R_left:INTEGER:= R'length-1;
1615alias xxl : SIGNED(L_left downto 0) is L;
1616alias xxr : SIGNED(R_left downto 0) is R;
1617variable xl : SIGNED(L_left downto 0);
1618variable xr : SIGNED(R_left downto 0);
1619variable fquot,fremain : UNSIGNED(l'length-1 downto 0);
1620variable xnum: UNSIGNED(l'length-1 downto 0);
1621variable XDENOM: UNSIGNED(r'length-1 downto 0);
1622variable rneg: boolean := FALSE;
1623begin
1624  if ((L'length<1) or (R'length<1)) then return NAS; end if;
1625  xl := TO_01(xxl,'X');
1626  xr := TO_01(xxr,'X');
1627  if ((xl(xl'left)='X') or (xr(xr'left)='X')) then
1628    fremain := (others=>'X');
1629    return SIGNED(fremain);
1630    end if;
1631  if xl(xl'left)='1' then
1632     xnum:=UNSIGNED(-xl);
1633  else
1634     xNum:=UNSIGNED(xl);
1635  end if;
1636  if xr(xr'left)='1' then
1637     XDENOM:=UNSIGNED(-xr);
1638     rNeg:=TRUE;
1639  else
1640     XDENOM:=UNSIGNED(xr);
1641  end if;
1642  divMod(xnum,XDENOM,fquot,fremain);
1643  if rNeg and l(l'left)='1' then
1644     fremain:="0"-fremain;
1645  elsif rNeg then
1646     fremain:=fremain-XDENOM;
1647  elsif l(l'left)='1' then
1648     fremain:=XDENOM-fremain;
1649  end if;
1650  return SIGNED(fremain);
1651  end;
1652
1653     -- Id: A.35
1654function "mod" ( L : UNSIGNED; R : NATURAL) return UNSIGNED is
1655  constant R_Length : Natural := max(L'Length, unsigned_num_bits(R));
1656  variable XR,XRem : unsigned (R_Length-1 downto 0);
1657begin
1658  XR := TO_UNSIGNED(R, R_Length);
1659  XRem := L mod XR;
1660  if R_Length>L'Length and XRem(0)/='X'
1661        and XRem(R_Length-1 downto L'Length)
1662          /= (R_Length-1 downto L'Length => '0')
1663  then
1664    ASSERT no_warning report "Numeric_std.""mod"": Modulus Truncated"
1665          severity warning;
1666  end if;
1667  return XRem(L'Length-1 downto 0);
1668end;
1669
1670     -- Id: A.36
1671function "mod" ( L : NATURAL; R : UNSIGNED) return UNSIGNED is
1672  constant L_Length : Natural := max(unsigned_num_bits(L), R'Length);
1673  variable XL,XRem : unsigned (L_Length-1 downto 0);
1674begin
1675  XL := TO_UNSIGNED(L, L_Length);
1676  XRem := XL mod R;
1677  if L_Length>R'Length and XRem(0)/='X'
1678        and XRem(L_Length-1 downto R'Length)
1679          /= (L_Length-1 downto R'Length => '0')
1680  then
1681    ASSERT no_warning report "Numeric_std.""mod"": Modulus Truncated"
1682          severity warning;
1683  end if;
1684  return XRem(R'Length-1 downto 0);
1685end;
1686
1687     -- Id: A.37
1688function "mod" ( L : SIGNED; R : INTEGER ) return SIGNED is
1689  constant R_Length : Natural := max(L'Length, signed_num_bits(R));
1690  variable XR,XRem : signed (R_Length-1 downto 0);
1691begin
1692  XR := TO_SIGNED(R, R_Length);
1693  XRem := L mod XR;
1694  if R_Length>L'Length and XRem(0)/='X'
1695        and XRem(R_Length-1 downto L'Length)
1696          /= (R_Length-1 downto L'Length => XRem(L'Length-1))
1697  then
1698    ASSERT no_warning report "Numeric_std.""mod"": Modulus Truncated"
1699          severity warning;
1700  end if;
1701  return XRem(L'Length-1 downto 0);
1702end;
1703
1704     -- Id: A.38
1705function "mod" ( L : INTEGER; R : SIGNED) return SIGNED is
1706  constant L_Length : Natural := max(signed_num_bits(L), R'Length);
1707  variable XL,XRem : signed (L_Length-1 downto 0);
1708begin
1709  XL := TO_SIGNED(L, L_Length);
1710  XRem := XL mod R;
1711  if L_Length>R'Length and XRem(0)/='X'
1712        and XRem(L_Length-1 downto R'Length)
1713          /= (L_Length-1 downto R'Length => XRem(R'Length-1))
1714  then
1715    ASSERT no_warning report "Numeric_std.""mod"": Modulus Truncated"
1716          severity warning;
1717  end if;
1718  return XRem(R'Length-1 downto 0);
1719end;
1720
1721--=============================================================================
1722
1723     -- Id: C.1
1724function ">"  ( L,R: UNSIGNED) return BOOLEAN is
1725constant L_left:INTEGER:= L'length-1;
1726constant R_left:INTEGER:= R'length-1;
1727alias XL: UNSIGNED(L_left downto 0) is L;
1728alias XR: UNSIGNED(R_left downto 0) is R;
1729constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1730variable L01 : UNSIGNED(L_left downto 0);
1731variable R01 : UNSIGNED(R_left downto 0);
1732begin
1733  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
1734  L01 := TO_01(XL,'X');
1735  R01 := TO_01(XR,'X');
1736  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
1737  return not UNSIGNED_LESS_OR_EQUAL (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
1738  end ">" ;
1739
1740     -- Id: C.2
1741function ">"  ( L,R: SIGNED) return BOOLEAN is
1742constant L_left:INTEGER:= L'length-1;
1743constant R_left:INTEGER:= R'length-1;
1744alias XL: SIGNED(L_left downto 0) is L;
1745alias XR: SIGNED(R_left downto 0) is R;
1746constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1747variable L01 : SIGNED(L_left downto 0);
1748variable R01 : SIGNED(R_left downto 0);
1749begin
1750  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
1751  L01 := TO_01(XL,'X');
1752  R01 := TO_01(XR,'X');
1753  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
1754  return not SIGNED_LESS_OR_EQUAL (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
1755  end ">" ;
1756
1757     -- Id: C.3
1758function ">"  ( L: NATURAL; R: UNSIGNED) return BOOLEAN is
1759constant R_left:INTEGER:= R'length-1;
1760alias XR: UNSIGNED(R_left downto 0) is R;
1761variable R01 : UNSIGNED(R_left downto 0);
1762begin
1763  if (R'length<1) then return FALSE; end if;
1764  R01 := TO_01(XR,'X');
1765  if (R01(R01'left)='X') then return FALSE; end if;
1766  if UNSIGNED_NUM_BITS(l)>R'length then return TRUE; end if;
1767  return not UNSIGNED_LESS_OR_EQUAL (TO_UNSIGNED (L,R01'LENGTH), R01) ;
1768  end ">" ;
1769
1770     -- Id: C.4
1771function ">"  ( L: INTEGER; R: SIGNED) return BOOLEAN is
1772constant R_left:INTEGER:= R'length-1;
1773alias XR: SIGNED(R_left downto 0) is R;
1774variable R01 : SIGNED(R_left downto 0);
1775begin
1776  if (R'length<1) then return FALSE; end if;
1777  R01 := TO_01(XR,'X');
1778  if (R01(R01'left)='X') then return FALSE; end if;
1779  if Signed_NUM_BITS(l)>R'length then return L>0; end if;
1780  return not SIGNED_LESS_OR_EQUAL (TO_SIGNED(L,R01'LENGTH), R01) ;
1781  end ">" ;
1782
1783     -- Id: C.5
1784function ">"  ( L: UNSIGNED; R: NATURAL) return BOOLEAN is
1785constant L_left:INTEGER:= L'length-1;
1786alias XL: UNSIGNED(L_left downto 0) is L;
1787variable L01 : UNSIGNED(L_left downto 0);
1788begin
1789  if (L'length<1) then return FALSE; end if;
1790  L01 := TO_01(XL,'X');
1791  if (L01(L01'left)='X') then return FALSE; end if;
1792  if UNSIGNED_NUM_BITS(R)>L'length then return FALSE; end if;
1793  return not UNSIGNED_LESS_OR_EQUAL (L01, TO_UNSIGNED (R,L01'LENGTH)) ;
1794  end ">" ;
1795
1796     -- Id: C.6
1797function ">"  ( L: SIGNED; R: INTEGER) return BOOLEAN is
1798constant L_left:INTEGER:= L'length-1;
1799alias XL: SIGNED(L_left downto 0) is L;
1800variable L01 : SIGNED(L_left downto 0);
1801begin
1802  if (L'length<1) then return FALSE; end if;
1803  L01 := TO_01(XL,'X');
1804  if (L01(L01'left)='X') then return FALSE; end if;
1805  if Signed_NUM_BITS(R)>L'length then return 0>R; end if;
1806  return not SIGNED_LESS_OR_EQUAL (L01, TO_SIGNED(R,L01'LENGTH)) ;
1807  end ">" ;
1808
1809--=============================================================================
1810
1811     -- Id: C.7
1812function "<"  ( L,R: UNSIGNED) return BOOLEAN is
1813constant L_left:INTEGER:= L'length-1;
1814constant R_left:INTEGER:= R'length-1;
1815alias XL: UNSIGNED(L_left downto 0) is L;
1816alias XR: UNSIGNED(R_left downto 0) is R;
1817constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1818variable L01 : UNSIGNED(L_left downto 0);
1819variable R01 : UNSIGNED(R_left downto 0);
1820begin
1821  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
1822  L01 := TO_01(XL,'X');
1823  R01 := TO_01(XR,'X');
1824  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
1825  return UNSIGNED_LESS (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
1826  end "<" ;
1827
1828     -- Id: C.8
1829function "<"  ( L,R: SIGNED) return BOOLEAN is
1830constant L_left:INTEGER:= L'length-1;
1831constant R_left:INTEGER:= R'length-1;
1832alias XL: SIGNED(L_left downto 0) is L;
1833alias XR: SIGNED(R_left downto 0) is R;
1834constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1835variable L01 : SIGNED(L_left downto 0);
1836variable R01 : SIGNED(R_left downto 0);
1837begin
1838  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
1839  L01 := TO_01(XL,'X');
1840  R01 := TO_01(XR,'X');
1841  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
1842  return SIGNED_LESS (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
1843  end "<" ;
1844
1845     -- Id: C.9
1846function "<"  ( L: NATURAL; R: UNSIGNED) return BOOLEAN is
1847constant R_left:INTEGER:= R'length-1;
1848alias XR: UNSIGNED(R_left downto 0) is R;
1849variable R01 : UNSIGNED(R_left downto 0);
1850begin
1851  if (R'length<1) then return FALSE; end if;
1852  R01 := TO_01(XR,'X');
1853  if (R01(R01'left)='X') then return FALSE; end if;
1854  if UNSIGNED_NUM_BITS(L)>R'length then return L<0; end if;
1855  return UNSIGNED_LESS (TO_UNSIGNED(L,R01'LENGTH), R01) ;
1856  end "<" ;
1857
1858     -- Id: C.10
1859function "<"  ( L: INTEGER; R: SIGNED) return BOOLEAN is
1860constant R_left:INTEGER:= R'length-1;
1861alias XR: SIGNED(R_left downto 0) is R;
1862variable R01 : SIGNED(R_left downto 0);
1863begin
1864  if (R'length<1) then return FALSE; end if;
1865  R01 := TO_01(XR,'X');
1866  if (R01(R01'left)='X') then return FALSE; end if;
1867  if Signed_NUM_BITS(L)>R'length then return L<0; end if;
1868  return SIGNED_LESS (TO_SIGNED(L,R01'LENGTH), R01) ;
1869  end "<" ;
1870
1871     -- Id: C.11
1872function "<"  ( L: UNSIGNED; R: NATURAL) return BOOLEAN is
1873constant L_left:INTEGER:= L'length-1;
1874alias XL: UNSIGNED(L_left downto 0) is L;
1875variable L01 : UNSIGNED(L_left downto 0);
1876begin
1877  if (L'length<1) then return FALSE; end if;
1878  L01 := TO_01(XL,'X');
1879  if (L01(L01'left)='X') then return FALSE; end if;
1880  if UNSIGNED_NUM_BITS(R)>L'length then return 0<R; end if;
1881  return UNSIGNED_LESS (L01, TO_UNSIGNED (R,L01'LENGTH)) ;
1882  end "<" ;
1883
1884     -- Id: C.12
1885function "<"  ( L: SIGNED; R: INTEGER) return BOOLEAN is
1886constant L_left:INTEGER:= L'length-1;
1887alias XL: SIGNED(L_left downto 0) is L;
1888variable L01 : SIGNED(L_left downto 0);
1889begin
1890  if (L'length<1) then return FALSE; end if;
1891  L01 := TO_01(XL,'X');
1892  if (L01(L01'left)='X') then return FALSE; end if;
1893  if Signed_NUM_BITS(R)>L'length then return 0<R; end if;
1894  return SIGNED_LESS (L01, TO_SIGNED (R,L01'LENGTH)) ;
1895  end "<" ;
1896
1897--=============================================================================
1898
1899     -- Id: C.13
1900function "<=" ( L,R: UNSIGNED) return BOOLEAN is
1901constant L_left:INTEGER:= L'length-1;
1902constant R_left:INTEGER:= R'length-1;
1903alias XL: UNSIGNED(L_left downto 0) is L;
1904alias XR: UNSIGNED(R_left downto 0) is R;
1905constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1906variable L01 : UNSIGNED(L_left downto 0);
1907variable R01 : UNSIGNED(R_left downto 0);
1908begin
1909  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
1910  L01 := TO_01(XL,'X');
1911  R01 := TO_01(XR,'X');
1912  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
1913  return UNSIGNED_LESS_OR_EQUAL (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
1914  end "<=" ;
1915
1916     -- Id: C.14
1917function "<=" ( L,R: SIGNED) return BOOLEAN is
1918constant L_left:INTEGER:= L'length-1;
1919constant R_left:INTEGER:= R'length-1;
1920alias XL: SIGNED(L_left downto 0) is L;
1921alias XR: SIGNED(R_left downto 0) is R;
1922constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1923variable L01 : SIGNED(L_left downto 0);
1924variable R01 : SIGNED(R_left downto 0);
1925begin
1926  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
1927  L01 := TO_01(XL,'X');
1928  R01 := TO_01(XR,'X');
1929  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
1930  return SIGNED_LESS_OR_EQUAL (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
1931  end "<=" ;
1932
1933     -- Id: C.15
1934function "<=" ( L: NATURAL; R: UNSIGNED) return BOOLEAN is
1935constant R_left:INTEGER:= R'length-1;
1936alias XR: UNSIGNED(R_left downto 0) is R;
1937variable R01 : UNSIGNED(R_left downto 0);
1938begin
1939  if (R'length<1) then return FALSE; end if;
1940  R01 := TO_01(XR,'X');
1941  if (R01(R01'left)='X') then return FALSE; end if;
1942  if UNSIGNED_NUM_BITS(L)>R'length then return L<0; end if;
1943  return UNSIGNED_LESS_OR_EQUAL (TO_UNSIGNED(L,R01'LENGTH), R01) ;
1944  end "<=" ;
1945
1946     -- Id: C.16
1947function "<=" ( L: INTEGER; R: SIGNED) return BOOLEAN is
1948constant R_left:INTEGER:= R'length-1;
1949alias XR: SIGNED(R_left downto 0) is R;
1950variable R01 : SIGNED(R_left downto 0);
1951begin
1952  if (R'length<1) then return FALSE; end if;
1953  R01 := TO_01(XR,'X');
1954  if (R01(R01'left)='X') then return FALSE; end if;
1955  if Signed_NUM_BITS(L)>R'length then return L<0; end if;
1956  return SIGNED_LESS_OR_EQUAL (TO_SIGNED(L,R01'LENGTH), R01) ;
1957  end "<=" ;
1958
1959     -- Id: C.17
1960function "<=" ( L: UNSIGNED; R: NATURAL) return BOOLEAN is
1961constant L_left:INTEGER:= L'length-1;
1962alias XL: UNSIGNED(L_left downto 0) is L;
1963variable L01 : UNSIGNED(L_left downto 0);
1964begin
1965  if (L_left<0) then return FALSE; end if;
1966  L01 := TO_01(XL,'X');
1967  if (L01(L01'left)='X') then return FALSE; end if;
1968  if UNSIGNED_NUM_BITS(R)>L'length then return 0<R; end if;
1969  return UNSIGNED_LESS_OR_EQUAL (L01, TO_UNSIGNED(R,L01'LENGTH)) ;
1970  end "<=" ;
1971
1972     -- Id: C.18
1973function "<=" ( L: SIGNED; R: INTEGER) return BOOLEAN is
1974constant L_left:INTEGER:= L'length-1;
1975alias XL: SIGNED(L_left downto 0) is L;
1976variable L01 : SIGNED(L_left downto 0);
1977begin
1978  if (L_left<0) then return FALSE; end if;
1979  L01 := TO_01(XL,'X');
1980  if (L01(L01'left)='X') then return FALSE; end if;
1981  if Signed_NUM_BITS(R)>L'length then return 0<R; end if;
1982  return SIGNED_LESS_OR_EQUAL (L01, TO_SIGNED(R,L01'LENGTH)) ;
1983  end "<=" ;
1984
1985--=============================================================================
1986
1987     -- Id: C.19
1988function ">=" ( L,R: UNSIGNED) return BOOLEAN is
1989constant L_left:INTEGER:= L'length-1;
1990constant R_left:INTEGER:= R'length-1;
1991alias XL: UNSIGNED(L_left downto 0) is L;
1992alias XR: UNSIGNED(R_left downto 0) is R;
1993constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
1994variable L01 : UNSIGNED(L_left downto 0);
1995variable R01 : UNSIGNED(R_left downto 0);
1996begin
1997  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
1998  L01 := TO_01(XL,'X');
1999  R01 := TO_01(XR,'X');
2000  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
2001  return not UNSIGNED_LESS (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
2002  end ">=" ;
2003
2004     -- Id: C.20
2005function ">=" ( L,R: SIGNED) return BOOLEAN is
2006constant L_left:INTEGER:= L'length-1;
2007constant R_left:INTEGER:= R'length-1;
2008alias XL: SIGNED(L_left downto 0) is L;
2009alias XR: SIGNED(R_left downto 0) is R;
2010constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
2011variable L01 : SIGNED(L_left downto 0);
2012variable R01 : SIGNED(R_left downto 0);
2013begin
2014  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
2015  L01 := TO_01(XL,'X');
2016  R01 := TO_01(XR,'X');
2017  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
2018  return not SIGNED_LESS (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
2019  end ">=" ;
2020
2021     -- Id: C.21
2022function ">=" ( L: NATURAL; R: UNSIGNED) return BOOLEAN is
2023constant R_left:INTEGER:= R'length-1;
2024alias XR: UNSIGNED(R_left downto 0) is R;
2025variable R01 : UNSIGNED(R_left downto 0);
2026begin
2027  if (R'length<1) then return FALSE; end if;
2028  R01 := TO_01(XR,'X');
2029  if (R01(R01'left)='X') then return FALSE; end if;
2030  if UNSIGNED_NUM_BITS(L)>R'length then return L>0; end if;
2031  return not UNSIGNED_LESS (TO_UNSIGNED(L,R01'LENGTH), R01) ;
2032  end ">=" ;
2033
2034     -- Id: C.22
2035function ">=" ( L: INTEGER; R: SIGNED) return BOOLEAN is
2036constant R_left:INTEGER:= R'length-1;
2037alias XR: SIGNED(R_left downto 0) is R;
2038variable R01 : SIGNED(R_left downto 0);
2039begin
2040  if (R'length<1) then return FALSE; end if;
2041  R01 := TO_01(XR,'X');
2042  if (R01(R01'left)='X') then return FALSE; end if;
2043  if Signed_NUM_BITS(L)>R'length then return L>0; end if;
2044  return not SIGNED_LESS (TO_SIGNED (L,R01'LENGTH), R01) ;
2045  end ">=" ;
2046
2047     -- Id: C.23
2048function ">=" ( L: UNSIGNED; R: NATURAL) return BOOLEAN is
2049constant L_left:INTEGER:= L'length-1;
2050alias XL: UNSIGNED(L_left downto 0) is L;
2051variable L01 : UNSIGNED(L_left downto 0);
2052begin
2053  if (L'length<1) then return FALSE; end if;
2054  L01 := TO_01(XL,'X');
2055  if (L01(L01'left)='X') then return FALSE; end if;
2056  if UNSIGNED_NUM_BITS(R)>L'length then return 0>R; end if;
2057  return not UNSIGNED_LESS(L01, TO_UNSIGNED(R,L01'LENGTH)) ;
2058  end ">=" ;
2059
2060     -- Id: C.24
2061function ">=" ( L: SIGNED; R: INTEGER) return BOOLEAN is
2062constant L_left:INTEGER:= L'length-1;
2063alias XL: SIGNED(L_left downto 0) is L;
2064variable L01 : SIGNED(L_left downto 0);
2065begin
2066  if (L'length<1) then return FALSE; end if;
2067  L01 := TO_01(XL,'X');
2068  if (L01(L01'left)='X') then return FALSE; end if;
2069  if Signed_NUM_BITS(R)>L'length then return 0>R; end if;
2070  return not SIGNED_LESS (L01, TO_SIGNED(R,L01'LENGTH)) ;
2071  end ">=" ;
2072
2073--=============================================================================
2074
2075     -- Id: C.25
2076function "="  ( L,R: UNSIGNED) return BOOLEAN is
2077constant L_left:INTEGER:= L'length-1;
2078constant R_left:INTEGER:= R'length-1;
2079alias XL: UNSIGNED(L_left downto 0) is L;
2080alias XR: UNSIGNED(R_left downto 0) is R;
2081constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
2082variable L01 : UNSIGNED(L_left downto 0);
2083variable R01 : UNSIGNED(R_left downto 0);
2084begin
2085  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
2086  L01 := TO_01(XL,'X');
2087  R01 := TO_01(XR,'X');
2088  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
2089  return UNSIGNED_equal (RESIZE(L01,SIZE),RESIZE(R01,SIZE)) ;
2090  end "=" ;
2091
2092     -- Id: C.26
2093function "="  ( L,R: SIGNED) return BOOLEAN is
2094constant L_left:INTEGER:= L'length-1;
2095constant R_left:INTEGER:= R'length-1;
2096alias XL: SIGNED(L_left downto 0) is L;
2097alias XR: SIGNED(R_left downto 0) is R;
2098constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
2099variable L01 : SIGNED(L_left downto 0);
2100variable R01 : SIGNED(R_left downto 0);
2101begin
2102  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
2103  L01 := TO_01(XL,'X');
2104  R01 := TO_01(XR,'X');
2105  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return FALSE; end if;
2106  return SIGNED_equal (RESIZE(L01,SIZE), RESIZE(R01,SIZE)) ;
2107  end "=" ;
2108
2109     -- Id: C.27
2110function "="  ( L: NATURAL; R: UNSIGNED) return BOOLEAN is
2111constant R_left:INTEGER:= R'length-1;
2112alias XR: UNSIGNED(R_left downto 0) is R;
2113variable R01 : UNSIGNED(R_left downto 0);
2114begin
2115  if (R'length<1) then return FALSE; end if;
2116  R01 := TO_01(XR,'X');
2117  if (R01(R01'left)='X') then return FALSE; end if;
2118  if UNSIGNED_NUM_BITS(L)>R'Length then return FALSE; end if;
2119  return UNSIGNED_equal ( TO_UNSIGNED(L,R01'LENGTH), R01);
2120  end "=" ;
2121
2122     -- Id: C.28
2123function "="  ( L: INTEGER; R: SIGNED) return BOOLEAN is
2124constant R_left:INTEGER:= R'length-1;
2125alias XR: SIGNED(R_left downto 0) is R;
2126variable R01 : SIGNED(R_left downto 0);
2127begin
2128  if (R'length<1) then return FALSE; end if;
2129  R01 := TO_01(XR,'X');
2130  if (R01(R01'left)='X') then return FALSE; end if;
2131  if UNSIGNED_NUM_BITS(L)>R'Length then return FALSE; end if;
2132  return SIGNED_equal ( TO_SIGNED(L,R01'LENGTH), R01) ;
2133  end "=" ;
2134
2135     -- Id: C.29
2136function "="  ( L: UNSIGNED; R: NATURAL) return BOOLEAN is
2137constant L_left:INTEGER:= L'length-1;
2138alias XL: UNSIGNED(L_left downto 0) is L;
2139variable L01 : UNSIGNED(L_left downto 0);
2140begin
2141  if (L'length<1) then return FALSE; end if;
2142  L01 := TO_01(XL,'X');
2143  if (L01(L01'left)='X') then return FALSE; end if;
2144  if UNSIGNED_NUM_BITS(R)>L'length then return FALSE; end if;
2145  return UNSIGNED_equal (L01, TO_UNSIGNED (R,L01'LENGTH)) ;
2146  end "=" ;
2147
2148     -- Id: C.30
2149function "="  ( L: SIGNED; R: INTEGER) return BOOLEAN is
2150constant L_left:INTEGER:= L'length-1;
2151alias XL: SIGNED(L_left downto 0) is L;
2152variable L01 : SIGNED(L_left downto 0);
2153begin
2154  if (L'length<1) then return FALSE; end if;
2155  L01 := TO_01(XL,'X');
2156  if (L01(L01'left)='X') then return FALSE; end if;
2157  if UNSIGNED_NUM_BITS(R)>L'length then return FALSE; end if;
2158  return SIGNED_equal (L01, TO_SIGNED (R,L01'LENGTH)) ;
2159  end "=" ;
2160
2161
2162--=============================================================================
2163
2164     -- Id: C.31
2165function "/="  ( L,R: UNSIGNED) return BOOLEAN is
2166constant L_left:INTEGER:= L'length-1;
2167constant R_left:INTEGER:= R'length-1;
2168alias XL: UNSIGNED(L_left downto 0) is L;
2169alias XR: UNSIGNED(R_left downto 0) is R;
2170constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
2171variable L01 : UNSIGNED(L_left downto 0);
2172variable R01 : UNSIGNED(R_left downto 0);
2173begin
2174  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
2175  L01 := TO_01(XL);
2176  R01 := TO_01(XR);
2177  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return TRUE; end if;
2178  return not(UNSIGNED_equal (RESIZE(L01,SIZE),RESIZE(R01,SIZE))) ;
2179  end "/=" ;
2180
2181     -- Id: C.32
2182function "/="  ( L,R: SIGNED) return BOOLEAN is
2183constant L_left:INTEGER:= L'length-1;
2184constant R_left:INTEGER:= R'length-1;
2185alias XL: SIGNED(L_left downto 0) is L;
2186alias XR: SIGNED(R_left downto 0) is R;
2187constant SIZE: NATURAL:= MAX (L'LENGTH, R'LENGTH) ;
2188variable L01 : SIGNED(L_left downto 0);
2189variable R01 : SIGNED(R_left downto 0);
2190begin
2191  if ((L'length<1) or (R'length<1)) then return FALSE; end if;
2192  L01 := TO_01(XL);
2193  R01 := TO_01(XR);
2194  if ((L01(L01'left)='X') or (R01(R01'left)='X')) then return TRUE; end if;
2195  return not(SIGNED_equal (RESIZE(L01,SIZE), RESIZE(R01,SIZE))) ;
2196  end "/=" ;
2197
2198     -- Id: C.33
2199function "/="  ( L: NATURAL; R: UNSIGNED) return BOOLEAN is
2200constant R_left:INTEGER:= R'length-1;
2201alias XR: UNSIGNED(R_left downto 0) is R;
2202variable R01 : UNSIGNED(R_left downto 0);
2203begin
2204  if (R'length<1) then return FALSE; end if;
2205  R01 := TO_01(XR);
2206  if (R01(R01'left)='X') then return TRUE; end if;
2207  if UNSIGNED_NUM_BITS(L)>R'Length then return TRUE; end if;
2208  return not(UNSIGNED_equal ( TO_UNSIGNED(L,R01'LENGTH), R01));
2209  end "/=" ;
2210
2211     -- Id: C.34
2212function "/="  ( L: INTEGER; R: SIGNED) return BOOLEAN is
2213constant R_left:INTEGER:= R'length-1;
2214alias XR: SIGNED(R_left downto 0) is R;
2215variable R01 : SIGNED(R_left downto 0);
2216begin
2217  if (R'length<1) then return FALSE; end if;
2218  R01 := TO_01(XR);
2219  if (R01(R01'left)='X') then return TRUE; end if;
2220  if Signed_NUM_BITS(L)>R'Length then return TRUE; end if;
2221  return not(SIGNED_equal ( TO_SIGNED(L,R01'LENGTH), R01)) ;
2222  end "/=" ;
2223
2224     -- Id: C.35
2225function "/="  ( L: UNSIGNED; R: NATURAL) return BOOLEAN is
2226constant L_left:INTEGER:= L'length-1;
2227alias XL: UNSIGNED(L_left downto 0) is L;
2228variable L01 : UNSIGNED(L_left downto 0);
2229begin
2230  if (L'length<1) then return FALSE; end if;
2231  L01 := TO_01(XL);
2232  if (L01(L01'left)='X') then return TRUE; end if;
2233  if UNSIGNED_NUM_BITS(R)>L'Length then return TRUE; end if;
2234  return not(UNSIGNED_equal (L01, TO_UNSIGNED (R,L01'LENGTH))) ;
2235  end "/=" ;
2236
2237     -- Id: C.36
2238function "/="  ( L: SIGNED; R: INTEGER) return BOOLEAN is
2239constant L_left:INTEGER:= L'length-1;
2240alias XL: SIGNED(L_left downto 0) is L;
2241variable L01 : SIGNED(L_left downto 0);
2242begin
2243  if (L'length<1) then return FALSE; end if;
2244  L01 := TO_01(XL);
2245  if (L01(L01'left)='X') then return TRUE; end if;
2246  if Signed_NUM_BITS(R)>L'Length then return TRUE; end if;
2247  return not(SIGNED_equal (L01, TO_SIGNED (R,L01'LENGTH))) ;
2248  end "/=" ;
2249
2250
2251--=============================================================================
2252
2253     -- Id: S.1
2254function shift_left(ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED is
2255begin
2256  if (ARG'length<1) then return NAU; end if;
2257  return UNSIGNED(XSLL(STD_LOGIC_VECTOR(ARG),COUNT));
2258  end;
2259
2260     -- Id: S.2
2261function shift_right(ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED is
2262begin
2263  if (ARG'length<1) then return NAU; end if;
2264  return UNSIGNED(XSRL(STD_LOGIC_VECTOR(ARG),COUNT));
2265  end;
2266
2267     -- Id: S.3
2268function shift_left(ARG: SIGNED; COUNT: NATURAL) return SIGNED is
2269begin
2270  if (ARG'length<1) then return NAS; end if;
2271  return SIGNED(XSLL(STD_LOGIC_VECTOR(ARG),COUNT));
2272  end;
2273
2274     -- Id: S.4
2275function shift_right(ARG: SIGNED; COUNT: NATURAL) return SIGNED is
2276begin
2277  if (ARG'length<1) then return NAS; end if;
2278  return SIGNED(XSRA(STD_LOGIC_VECTOR(ARG),COUNT));
2279  end;
2280
2281--=============================================================================
2282
2283     -- Id: S.5
2284function rotate_left(ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED is
2285begin
2286  if (ARG'length<1) then return NAU; end if;
2287  return UNSIGNED(XROL(STD_LOGIC_VECTOR(ARG),COUNT));
2288  end;
2289
2290     -- Id: S.6
2291function rotate_right(ARG: UNSIGNED; COUNT: NATURAL) return UNSIGNED is
2292begin
2293  if (ARG'length<1) then return NAU; end if;
2294  return UNSIGNED(XROR(STD_LOGIC_VECTOR(ARG),COUNT));
2295  end;
2296
2297
2298     -- Id: S.7
2299function rotate_left(ARG: SIGNED; COUNT: NATURAL) return SIGNED is
2300begin
2301  if (ARG'length<1) then return NAS; end if;
2302  return SIGNED(XROL(STD_LOGIC_VECTOR(ARG),COUNT));
2303  end;
2304
2305     -- Id: S.8
2306function rotate_right(ARG: SIGNED; COUNT: NATURAL) return SIGNED is
2307begin
2308  if (ARG'length<1) then return NAS; end if;
2309  return SIGNED(XROR(STD_LOGIC_VECTOR(ARG),COUNT));
2310  end;
2311
2312--=============================================================================
2313
2314
2315     -- Id: D.1
2316function TO_INTEGER(ARG: UNSIGNED) return NATURAL is
2317constant ARG_LEFT:INTEGER:= ARG'length-1;
2318alias XXARG:UNSIGNED(ARG_LEFT downto 0) is ARG;
2319variable XARG:UNSIGNED(ARG_LEFT downto 0);
2320variable RESULT: NATURAL:= 0;
2321variable w : INTEGER:= 1;  -- weight factor
2322begin
2323  if (ARG'length<1) then
2324    assert NO_WARNING
2325    report "numeric_std.TO_INTEGER: null arg"
2326    severity warning;
2327    return 0;
2328    end if;
2329  XARG:= TO_01(XXARG);
2330  if (XARG(XARG'left)='X') then
2331    assert NO_WARNING
2332    report "numeric_std.TO_INTEGER: metavalue arg set to 0"
2333    severity warning;
2334    return 0;
2335    end if;
2336  for i in XARG'reverse_range loop
2337    if XARG (i) = '1' then
2338      RESULT:= RESULT + w;
2339      end if;
2340    if (i /= XARG'left) then w := w + w;
2341      end if;
2342    end loop;
2343  return RESULT;
2344  end TO_INTEGER;
2345
2346     -- Id: D.2
2347function TO_INTEGER(ARG: SIGNED) return INTEGER is
2348begin
2349  if ARG(ARG'left) = '0' then
2350    return TO_INTEGER( UNSIGNED (ARG)) ;
2351  else
2352    return (- (TO_INTEGER( UNSIGNED ( - (ARG + 1)))) -1);
2353    end if;
2354  end TO_INTEGER;
2355
2356     -- Id: D.3
2357function TO_UNSIGNED(ARG,SIZE: NATURAL) return UNSIGNED is
2358  variable RESULT: UNSIGNED (SIZE-1 downto 0) ;
2359  variable i_val:natural := ARG;
2360  begin
2361  if (SIZE < 1) then return NAU; end if;
2362  for i in 0 to RESULT'left loop
2363    if (i_val MOD 2) = 0 then
2364       RESULT(i) := '0';
2365    else RESULT(i) := '1' ;
2366      end if;
2367    i_val := i_val/2 ;
2368    end loop;
2369  if not(i_val=0) then
2370    assert NO_WARNING
2371    report "numeric_std.TO_UNSIGNED : vector truncated"
2372    severity WARNING ;
2373    end if;
2374  return RESULT ;
2375  end TO_UNSIGNED;
2376
2377     -- Id: D.4
2378function TO_SIGNED(ARG: INTEGER; SIZE: NATURAL) return SIGNED is
2379  variable RESULT: SIGNED (SIZE-1 downto 0) ;
2380  variable b_val : STD_LOGIC:= '0' ;
2381  variable i_val : INTEGER:= ARG ;
2382  begin
2383  if (SIZE < 1) then return NAS; end if;
2384  if (ARG<0) then
2385    b_val := '1' ;
2386    i_val := -(ARG+1) ;
2387    end if ;
2388  for i in 0 to RESULT'left loop
2389    if (i_val MOD 2) = 0 then
2390      RESULT(i) := b_val;
2391    else
2392      RESULT(i) := not b_val ;
2393      end if;
2394    i_val := i_val/2 ;
2395    end loop;
2396  if ((i_val/=0) or (b_val/=RESULT(RESULT'left))) then
2397    assert NO_WARNING
2398    report "numeric_std.TO_SIGNED : vector truncated"
2399    severity WARNING ;
2400    end if;
2401  return RESULT;
2402  end TO_SIGNED;
2403
2404     -- Id: D.5
2405function TO_UNSIGNED(ARG: STD_LOGIC_VECTOR) return UNSIGNED is
2406  begin
2407  return UNSIGNED(ARG);
2408  end TO_UNSIGNED;
2409
2410     -- Id: D.6
2411function TO_SIGNED(ARG: STD_LOGIC_VECTOR) return SIGNED is
2412  begin
2413  return SIGNED(ARG);
2414  end TO_SIGNED;
2415
2416     -- Id: D.7
2417function TO_STDLOGICVECTOR(ARG: UNSIGNED) return STD_LOGIC_VECTOR is
2418  begin
2419  return STD_LOGIC_VECTOR(ARG);
2420  end TO_STDLOGICVECTOR;
2421
2422     -- Id: D.8
2423function TO_STDLOGICVECTOR(ARG: SIGNED) return STD_LOGIC_VECTOR is
2424  begin
2425  return STD_LOGIC_VECTOR(ARG);
2426  end TO_STDLOGICVECTOR;
2427
2428--=============================================================================
2429
2430
2431     -- Id: R.1
2432function RESIZE (ARG: SIGNED; NEW_SIZE: NATURAL) return SIGNED is
2433alias invec : SIGNED (ARG'length-1 downto 0) is ARG ;
2434variable RESULT: SIGNED (NEW_SIZE-1 downto 0) ;
2435constant bound : NATURAL:= MIN(ARG'length,RESULT'length)-2 ;
2436begin
2437  if (NEW_SIZE<1) then return NAS; end if;
2438  RESULT:= (others=>ARG(ARG'left)) ;
2439  if bound >= 0 then
2440    RESULT(bound downto 0) := invec(bound downto 0) ;
2441    end if;
2442  return RESULT;
2443  end RESIZE ;
2444
2445
2446     -- Id: R.2
2447function RESIZE ( ARG: UNSIGNED; NEW_SIZE: NATURAL) return UNSIGNED is
2448constant ARG_LEFT:INTEGER:= ARG'length-1;
2449alias XARG: UNSIGNED(ARG_LEFT downto 0) is ARG;
2450variable RESULT: UNSIGNED(NEW_SIZE-1 downto 0) := (others=>'0');
2451begin
2452  if (NEW_SIZE<1) then return NAU; end if;
2453  if XARG'length=0 then return RESULT;
2454    end if;
2455  if (RESULT'length < ARG'length) then
2456    RESULT(RESULT'left downto 0) := XARG(RESULT'left downto 0);
2457  else
2458    RESULT(RESULT'left downto XARG'left+1) := (others => '0');
2459    RESULT(XARG'left downto 0) := XARG;
2460    end if;
2461  return RESULT;
2462  end RESIZE;
2463
2464
2465  --============================================================================
2466
2467     -- Id: L.1
2468function "not" ( L: UNSIGNED) return UNSIGNED is
2469variable RESULT: UNSIGNED (L'range);
2470begin
2471  RESULT:= UNSIGNED(not(STD_LOGIC_VECTOR(L)));
2472  return RESULT;
2473  end "not";
2474
2475     -- Id: L.2
2476function "and" ( L,R: UNSIGNED ) return UNSIGNED is
2477variable RESULT: UNSIGNED (L'range);
2478begin
2479  RESULT:= UNSIGNED(STD_LOGIC_VECTOR(L) and STD_LOGIC_VECTOR(R));
2480  return RESULT;
2481  end "and";
2482
2483     -- Id: L.3
2484function "or" ( L,R: UNSIGNED ) return UNSIGNED is
2485variable RESULT: UNSIGNED (L'range);
2486begin
2487  RESULT:= UNSIGNED(STD_LOGIC_VECTOR(L) or STD_LOGIC_VECTOR(R));
2488  return RESULT;
2489  end "or";
2490
2491     -- Id: L.4
2492function "nand" ( L,R: UNSIGNED ) return UNSIGNED is
2493variable RESULT: UNSIGNED (L'range);
2494begin
2495  RESULT:= UNSIGNED(STD_LOGIC_VECTOR(L) nand STD_LOGIC_VECTOR(R));
2496  return RESULT;
2497  end "nand";
2498
2499     -- Id: L.5
2500function "nor" ( L,R: UNSIGNED ) return UNSIGNED is
2501variable RESULT: UNSIGNED (L'range);
2502begin
2503  RESULT:= UNSIGNED(STD_LOGIC_VECTOR(L) nor STD_LOGIC_VECTOR(R));
2504  return RESULT;
2505  end "nor";
2506
2507     -- Id: L.6
2508function "xor" ( L,R: UNSIGNED ) return UNSIGNED is
2509variable RESULT: UNSIGNED (L'range);
2510begin
2511  RESULT:= UNSIGNED(STD_LOGIC_VECTOR(L) xor STD_LOGIC_VECTOR(R));
2512  return RESULT;
2513  end "xor";
2514
2515--  -----------------------------------------------------------------------
2516--  Note : The declaration and implementation of the "xnor" function is
2517--  specifically commented until at which time the VHDL language has been
2518--  officially adopted as containing such a function. At such a point,
2519--  the following comments may be removed along with this notice without
2520--  further "official" ballotting of this 1076.3 package. It is
2521--  the intent of this effort to provide such a function once it becomes
2522--  available in the VHDL standard.
2523--  -----------------------------------------------------------------------
2524     -- Id: L.7
2525--function "xnor" ( L,R: UNSIGNED ) return UNSIGNED is
2526--variable RESULT: UNSIGNED (L'range);
2527--begin
2528--  RESULT:= UNSIGNED(STD_LOGIC_VECTOR(L) xnor STD_LOGIC_VECTOR(R));
2529--  return RESULT;
2530--  end "xnor";
2531
2532      -- Id: L.8
2533function "not" ( L: SIGNED) return SIGNED is
2534variable RESULT: SIGNED (L'range);
2535begin
2536  RESULT:= SIGNED(not(STD_LOGIC_VECTOR(L)));
2537  return RESULT;
2538  end "not";
2539
2540     -- Id: L.9
2541function "and" ( L,R: SIGNED ) return SIGNED is
2542variable RESULT: SIGNED (L'range);
2543begin
2544  RESULT:= SIGNED(STD_LOGIC_VECTOR(L) and STD_LOGIC_VECTOR(R));
2545  return RESULT;
2546  end "and";
2547
2548     -- Id: L.10
2549function "or" ( L,R: SIGNED ) return SIGNED is
2550variable RESULT: SIGNED (L'range);
2551begin
2552  RESULT:= SIGNED(STD_LOGIC_VECTOR(L) or STD_LOGIC_VECTOR(R));
2553  return RESULT;
2554  end "or";
2555
2556     -- Id: L.11
2557function "nand" ( L,R: SIGNED ) return SIGNED is
2558variable RESULT: SIGNED (L'range);
2559begin
2560  RESULT:= SIGNED(STD_LOGIC_VECTOR(L) nand STD_LOGIC_VECTOR(R));
2561  return RESULT;
2562  end "nand";
2563
2564     -- Id: L.12
2565function "nor" ( L,R: SIGNED ) return SIGNED is
2566variable RESULT: SIGNED (L'range);
2567begin
2568  RESULT:= SIGNED(STD_LOGIC_VECTOR(L) nor STD_LOGIC_VECTOR(R));
2569  return RESULT;
2570  end "nor";
2571
2572     -- Id: L.13
2573function "xor" ( L,R: SIGNED ) return SIGNED is
2574variable RESULT: SIGNED (L'range);
2575begin
2576  RESULT:= SIGNED(STD_LOGIC_VECTOR(L) xor STD_LOGIC_VECTOR(R));
2577  return RESULT;
2578  end "xor";
2579
2580--  -----------------------------------------------------------------------
2581--  Note : The declaration and implementation of the "xnor" function is
2582--  specifically commented until at which time the VHDL language has been
2583--  officially adopted as containing such a function. At such a point,
2584--  the following comments may be removed along with this notice without
2585--  further "official" ballotting of this 1076.3 package. It is
2586--  the intent of this effort to provide such a function once it becomes
2587--  available in the VHDL standard.
2588--  -----------------------------------------------------------------------
2589     -- Id: L.14
2590--function "xnor" ( L,R: SIGNED ) return SIGNED is
2591--variable RESULT: SIGNED (L'range);
2592--begin
2593--  RESULT:= SIGNED(STD_LOGIC_VECTOR(L) xnor STD_LOGIC_VECTOR(R));
2594--  return RESULT;
2595--  end "xnor";
2596
2597--=============================================================================
2598
2599-- support constants for STD_MATCH:
2600
2601 type STDULOGIC_TABLE is array(STD_ULOGIC, STD_ULOGIC) of STD_ULOGIC;
2602
2603 -- truth table for "and" function
2604 constant AND_TABLE : STDULOGIC_TABLE := (
2605 -- ----------------------------------------------------
2606 -- | U X 0 1 Z W L H -  | |
2607 -- ----------------------------------------------------
2608         ( 'U', 'U', '0', 'U', 'U', 'U', '0', 'U', 'U' ), -- | U |
2609         ( 'U', 'X', '0', 'X', 'X', 'X', '0', 'X', 'X' ), -- | X |
2610         ( '0', '0', '0', '0', '0', '0', '0', '0', '0' ), -- | 0 |
2611         ( 'U', 'X', '0', '1', 'X', 'X', '0', '1', 'X' ), -- | 1 |
2612         ( 'U', 'X', '0', 'X', 'X', 'X', '0', 'X', 'X' ), -- | Z |
2613         ( 'U', 'X', '0', 'X', 'X', 'X', '0', 'X', 'X' ), -- | W |
2614         ( '0', '0', '0', '0', '0', '0', '0', '0', '0' ), -- | L |
2615         ( 'U', 'X', '0', '1', 'X', 'X', '0', '1', 'X' ), -- | H |
2616         ( 'U', 'X', '0', 'X', 'X', 'X', '0', 'X', 'X' )  -- | - |
2617 );
2618
2619 -- truth table for STD_MATCH function
2620 constant MATCH_TABLE : STDULOGIC_TABLE := (
2621 -- ----------------------------------------------------
2622 -- | U X 0 1 Z W L H -  | |
2623 -- ----------------------------------------------------
2624         ( 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', '1' ), -- | U |
2625         ( 'U', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '1' ), -- | X |
2626         ( 'U', 'X', '1', '0', 'X', 'X', '1', '0', '1' ), -- | 0 |
2627         ( 'U', 'X', '0', '1', 'X', 'X', '0', '1', '1' ), -- | 1 |
2628         ( 'U', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '1' ), -- | Z |
2629         ( 'U', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '1' ), -- | W |
2630         ( 'U', 'X', '1', '0', 'X', 'X', '1', '0', '1' ), -- | L |
2631         ( 'U', 'X', '0', '1', 'X', 'X', '0', '1', '1' ), -- | H |
2632         ( '1', '1', '1', '1', '1', '1', '1', '1', '1' )  -- | - |
2633 );
2634
2635     -- Id: M.1
2636function STD_MATCH (L, R: STD_ULOGIC) return BOOLEAN is
2637  variable VALUE : STD_ULOGIC;
2638  begin
2639    VALUE := MATCH_TABLE(L, R);
2640    return VALUE = '1';
2641  end STD_MATCH;
2642
2643
2644     -- Id: M.2
2645function STD_MATCH (L, R: STD_LOGIC_VECTOR) return BOOLEAN is
2646  alias LV: STD_LOGIC_VECTOR ( 1 to L'LENGTH ) is L;
2647  alias RV: STD_LOGIC_VECTOR ( 1 to R'LENGTH ) is R;
2648  variable VALUE: STD_ULOGIC:= '1';
2649  begin
2650    -- Check that both input vectors are the same length.
2651    if LV'LENGTH /= RV'LENGTH then
2652      assert NO_WARNING
2653      report "STD_MATCH input arguments are not of equal length"
2654      severity warning;
2655      return FALSE;
2656    else
2657      for i in LV'LOW to LV'HIGH loop
2658        VALUE := AND_TABLE(MATCH_TABLE(LV(i), RV(i)), VALUE);
2659        end loop;
2660      return VALUE = '1';
2661      end if;
2662  end STD_MATCH;
2663
2664
2665
2666--=============================================================================
2667end numeric_std;
2668
2669
2670
2671
2672