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

Subversion Repositories ratpack

[/] [ratpack/] [trunk/] [rtl/] [vhdl/] [ratpack.vhd] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 kavi
--------------------------------------------------------------------------------
2
-- Filename: ratpack.vhd
3
-- Purpose : Rational arithmetic package
4
-- Author  : Nikolaos Kavvadias <nikolaos.kavvadias@gmail.com>
5
-- Date    : 21-Feb-2014
6
-- Version : 0.1.0
7
-- Revision: 0.0.0 (2009/07/25)
8
--           Initial version. Added the definition of a rational number, and the
9
--           implementation of the "+", "-", "*", "/" operators on reduced
10
--           rational numbers (i.e. relative primes), the gcd(x,y) and the
11
--           mediant operator.
12
--           0.0.1 (2009/07/28)
13
--           Added: numerator, denominator, abs, relational operators (>, <, >=,
14
--           <=, =, /=, changed the int2rat to to_rational, new version of
15
--           int2rat (casting an integer to a rational).
16
--           0.0.2 (2010/11/17)
17
--           Added an iterative version for the gcd computation (gcditer).
18
--           0.0.3 (2012/02/11)
19
--           Added max, min.
20
--           0.1.0 (2014/02/21)
21
--           Upgraded to version 0.1.0.
22
-- License : Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 by Nikolaos Kavvadias
23
--           This program is free software. You can redistribute it and/or 
24
--           modify it under the terms of the GNU Lesser General Public License, 
25
--           either version 3 of the License, or (at your option) any later 
26
--           version. See COPYING.
27
--
28
--------------------------------------------------------------------------------
29
 
30
package ratpack is
31
 
32
  -- A rational number is defined by the pair (numerator, denominator) where 
33
  -- both numbers. -- are in the range [-16384, 16383].
34
  constant numer : INTEGER := 0; -- numerator
35
  constant denom : INTEGER := 1; -- denominator
36
  type rational is array (natural range numer to denom) of
37
    integer; -- range -16384 to 16383;
38
  constant RAT_ZERO : rational := (0, 1);
39
  constant RAT_ONE  : rational := (1, 1);
40
 
41
  function to_rational (a, b : integer) return rational;
42
  function int2rat (a : integer) return rational;
43
  function numerator (a : rational) return integer;
44
  function denominator (a : rational) return integer;
45
  function "+" (a, b : rational) return rational;
46
  function "-" (a, b : rational) return rational;
47
  function "*" (a, b : rational) return rational;
48
  function "/" (a, b : rational) return rational;
49
  function "abs" (a : rational)  return rational;
50
  function max (a, b : rational) return rational;
51
  function min (a, b : rational) return rational;
52
  function ">" (a, b : rational) return boolean;
53
  function "<" (a, b : rational) return boolean;
54
  function ">=" (a, b : rational) return boolean;
55
  function "<=" (a, b : rational) return boolean;
56
  function "=" (a, b : rational) return boolean;
57
  function "/=" (a, b : rational) return boolean;
58
  function gcd (a, b : integer)  return integer;
59
  function gcditer (a, b : integer)  return integer;
60
  function mediant (a, b : rational) return rational;
61
 
62
end ratpack;
63
 
64
 
65
package body ratpack is
66
 
67
  function to_rational (a, b : integer) return rational is
68
    variable r : rational;
69
  begin
70
    r(numer) := a;
71
    r(denom) := b;
72
    return r;
73
  end to_rational;
74
 
75
  function int2rat (a : integer) return rational is
76
    variable r : rational;
77
  begin
78
    r(numer) := a;
79
    r(denom) := 1;
80
    return r;
81
  end int2rat;
82
 
83
  function numerator (a : rational) return integer is
84
    variable n : integer;
85
  begin
86
    n := a(numer);
87
    return n;
88
  end numerator;
89
 
90
  function denominator (a : rational) return integer is
91
    variable d : integer;
92
  begin
93
    d := a(denom);
94
    return d;
95
  end denominator;
96
 
97
  function "+" (a, b : rational) return rational is
98
    variable r : rational;
99
    variable tn, td : integer;
100
  begin
101
    tn := a(numer)*b(denom) + a(denom)*b(numer);
102
    td := a(denom) * b(denom);
103
    if ( gcd(abs(tn), abs(td)) > 0 ) then
104
      r(numer) := tn / gcd(abs(tn), abs(td));
105
      r(denom) := td / gcd(abs(tn), abs(td));
106
    else
107
      r(numer) := tn;
108
      r(denom) := td;
109
    end if;
110
    return r;
111
  end "+";
112
 
113
  function "-" (a, b : rational) return rational is
114
    variable r : rational;
115
    variable tn, td : integer;
116
  begin
117
    tn := a(numer)*b(denom) - a(denom)*b(numer);
118
    td := a(denom) * b(denom);
119
    if ( gcd(abs(tn), abs(td)) > 0 ) then
120
      r(numer) := tn / gcd(abs(tn), abs(td));
121
      r(denom) := td / gcd(abs(tn), abs(td));
122
    else
123
      r(numer) := tn;
124
      r(denom) := td;
125
    end if;
126
    return r;
127
  end "-";
128
 
129
  function "*" (a, b : rational) return rational is
130
    variable r : rational;
131
    variable tn, td : integer;
132
  begin
133
    tn := a(numer) * b(numer);
134
    td := a(denom) * b(denom);
135
    if ( gcd(abs(tn), abs(td)) > 0 ) then
136
      r(numer) := tn / gcd(abs(tn), abs(td));
137
      r(denom) := td / gcd(abs(tn), abs(td));
138
    else
139
      r(numer) := tn;
140
      r(denom) := td;
141
    end if;
142
    return r;
143
  end "*";
144
 
145
  function "/" (a, b : rational) return rational is
146
    variable r : rational;
147
    variable tn, td : integer;
148
  begin
149
    tn := a(numer) * b(denom);
150
    td := a(denom) * b(numer);
151
    if ( gcd(abs(tn), abs(td)) > 0 ) then
152
      r(numer) := tn / gcd(abs(tn), abs(td));
153
      r(denom) := td / gcd(abs(tn), abs(td));
154
    else
155
      r(numer) := tn;
156
      r(denom) := td;
157
    end if;
158
    return r;
159
  end "/";
160
 
161
  function "abs" (a : rational) return rational is
162
    variable ta, tb, y : integer;
163
    variable r : rational;
164
  begin
165
    ta := a(numer);
166
    tb := a(denom);
167
    if (ta < 0) then
168
      ta := - a(numer);
169
    end if;
170
    if (tb < 0) then
171
      tb := - a(denom);
172
    end if;
173
    r := to_rational(ta, tb);
174
    return r;
175
  end "abs";
176
 
177
  function max (a, b : rational) return rational is
178
    variable w, x, y, z : integer;
179
    variable t1, t2 : integer;
180
    variable maxv : rational;
181
  begin
182
    w := numerator(a);
183
    x := denominator(a);
184
    y := numerator(b);
185
    z := denominator(b);
186
    t1 := w*z;
187
    t2 := x*y;
188
    if (t1 > t2) then
189
      maxv := a;
190
    else
191
      maxv := b;
192
    end if;
193
    return maxv;
194
  end max;
195
 
196
  function min (a, b : rational) return rational is
197
    variable w, x, y, z : integer;
198
    variable t1, t2 : integer;
199
    variable minv : rational;
200
  begin
201
    w := numerator(a);
202
    x := denominator(a);
203
    y := numerator(b);
204
    z := denominator(b);
205
    t1 := w*z;
206
    t2 := x*y;
207
    if (t1 < t2) then
208
      minv := a;
209
    else
210
      minv := b;
211
    end if;
212
    return minv;
213
  end min;
214
 
215
  function ">" (a, b : rational) return boolean is
216
    variable diff : rational := a - b;
217
  begin
218
    return (((diff(numer) > 0) and (diff(denom) > 0)) or
219
            ((diff(numer) < 0) and (diff(denom) < 0)));
220
  end ">";
221
 
222
  function "<" (a, b : rational) return boolean is
223
    variable diff : rational := a - b;
224
  begin
225
    return (((diff(numer) > 0) and (diff(denom) < 0)) or
226
            ((diff(numer) < 0) and (diff(denom) > 0)));
227
  end "<";
228
 
229
  function ">=" (a, b : rational) return boolean is
230
    variable diff : rational := a - b;
231
  begin
232
    return (((diff(numer) >= 0) and (diff(denom) > 0)) or
233
            ((diff(numer) <= 0) and (diff(denom) < 0)));
234
  end ">=";
235
 
236
  function "<=" (a, b : rational) return boolean is
237
    variable diff : rational := a - b;
238
  begin
239
    return (((diff(numer) >= 0) and (diff(denom) < 0)) or
240
            ((diff(numer) <= 0) and (diff(denom) > 0)));
241
  end "<=";
242
 
243
  function "=" (a, b : rational) return boolean is
244
    variable diff : rational := a - b;
245
  begin
246
    return (diff(numer) = 0);
247
  end "=";
248
 
249
  function "/=" (a, b : rational) return boolean is
250
    variable diff : rational := a - b;
251
  begin
252
    return (diff(numer) /= 0);
253
  end "/=";
254
 
255
  function gcd (a, b : integer) return integer is
256
  begin
257
    if a = 0 then
258
      return b;
259
    end if;
260
    if b = 0 then
261
      return a;
262
    end if;
263
    if (a > b) then
264
      return gcd(b, a mod b);
265
    else
266
      return gcd(a, b mod a);
267
    end if;
268
  end gcd;
269
 
270
  function gcditer (a, b : integer) return integer is
271
    variable x, y : integer;
272
  begin
273
    x := a;
274
    y := b;
275
    if ((x = 0) and (y = 0)) then
276
      return 0;
277
    end if;
278
    while (x /= y) loop
279
      if (x >= y) then
280
        x := x - y;
281
      else
282
        y := y - x;
283
      end if;
284
    end loop;
285
    return x;
286
  end gcditer;
287
 
288
  function mediant (a, b : rational) return rational is
289
    variable r : rational;
290
    variable tn, td : integer;
291
  begin
292
    tn := a(numer) + b(numer);
293
    td := a(denom) + b(denom);
294
    if ( gcditer(abs(tn), abs(td)) > 0 ) then
295
      r(numer) := tn / gcditer(abs(tn), abs(td));
296
      r(denom) := td / gcditer(abs(tn), abs(td));
297
    else
298
      r(numer) := tn;
299
      r(denom) := td;
300
    end if;
301
    return r;
302
  end mediant;
303
 
304
end ratpack;

powered by: WebSVN 2.1.0

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