1 |
2 |
ndave |
// The MIT License
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2006 Nirav Dave (ndave@csail.mit.edu)
|
4 |
|
|
//
|
5 |
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
|
|
// of this software and associated documentation files (the "Software"), to deal
|
7 |
|
|
// in the Software without restriction, including without limitation the rights
|
8 |
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
|
|
// copies of the Software, and to permit persons to whom the Software is
|
10 |
|
|
// furnished to do so, subject to the following conditions:
|
11 |
|
|
//
|
12 |
|
|
// The above copyright notice and this permission notice shall be included in
|
13 |
|
|
// all copies or substantial portions of the Software.
|
14 |
|
|
//
|
15 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21 |
|
|
// THE SOFTWARE.
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
typedef struct{
|
27 |
|
|
Bit#(n) i;
|
28 |
|
|
Bit#(n) q;
|
29 |
|
|
}
|
30 |
|
|
ComplexF#(numeric type n) deriving(Eq, Bits);
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
function Int#(n) toInt(Bit#(n) x)= unpack(x);
|
34 |
|
|
function Bit#(n) toBit(Int#(n) x)= pack(x);
|
35 |
|
|
|
36 |
|
|
instance Literal#(ComplexF#(n));
|
37 |
|
|
function ComplexF#(n) fromInteger(Integer x);
|
38 |
|
|
return error("Can't use Literal");
|
39 |
|
|
endfunction
|
40 |
|
|
endinstance
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
instance Bounded#(ComplexF#(n));
|
44 |
|
|
function ComplexF#(n) minBound();
|
45 |
|
|
Int#(n) mb = minBound;
|
46 |
|
|
return ComplexF{i: pack(mb),q: pack(mb)};
|
47 |
|
|
endfunction
|
48 |
|
|
function ComplexF#(n) maxBound();
|
49 |
|
|
Int#(n) mb = maxBound;
|
50 |
|
|
return ComplexF{i: pack(mb),q: pack(mb)};
|
51 |
|
|
endfunction
|
52 |
|
|
endinstance
|
53 |
|
|
|
54 |
|
|
instance BitExtend#(n,m, ComplexF) provisos(Add#(k,n,m));
|
55 |
|
|
function ComplexF#(m) zeroExtend(ComplexF#(n) x);
|
56 |
|
|
return ComplexF{
|
57 |
|
|
i: zeroExtend(x.i),
|
58 |
|
|
q: zeroExtend(x.q)
|
59 |
|
|
};
|
60 |
|
|
endfunction
|
61 |
|
|
function ComplexF#(m) signExtend(ComplexF#(n) x);
|
62 |
|
|
return ComplexF{
|
63 |
|
|
i: signExtend(x.i),
|
64 |
|
|
q: signExtend(x.q)
|
65 |
|
|
};
|
66 |
|
|
endfunction
|
67 |
|
|
function ComplexF#(n) truncate(ComplexF#(m) x);
|
68 |
|
|
Nat rmax = fromInteger(valueOf(m) -1);
|
69 |
|
|
Nat rmin = fromInteger(valueOf(m) - valueOf(n));
|
70 |
|
|
return ComplexF{
|
71 |
|
|
i: x.i[rmax:rmin],
|
72 |
|
|
q: x.q[rmax:rmin]
|
73 |
|
|
};
|
74 |
|
|
endfunction
|
75 |
|
|
endinstance
|
76 |
|
|
|
77 |
|
|
function Bit#(n) complex_add(Bit#(n) x, Bit#(n) y) provisos(Add#(1,k,n), Add#(1,n, TAdd#(1,n)));
|
78 |
|
|
Nat si = fromInteger(valueOf(n) - 1);
|
79 |
|
|
Nat si_p_1 = fromInteger(valueOf(n));
|
80 |
|
|
Bit#(1) sx = pack(x)[si];
|
81 |
|
|
Bit#(1) sy = pack(y)[si];
|
82 |
|
|
Int#(TAdd#(1,n)) ix = unpack({sx,x});
|
83 |
|
|
Int#(TAdd#(1,n)) iy = unpack({sy,y});
|
84 |
|
|
Int#(TAdd#(1,n)) ir = ix + iy + 1;
|
85 |
|
|
Bit#(n) res = (pack(ir))[si_p_1:1];
|
86 |
|
|
return res;
|
87 |
|
|
endfunction
|
88 |
|
|
|
89 |
|
|
function Bit#(n) complex_sub(Bit#(n) x, Bit#(n) y) provisos(Add#(1,k,n), Add#(1,n, TAdd#(1,n)));
|
90 |
|
|
Nat si = fromInteger(valueOf(n) - 1);
|
91 |
|
|
Nat si_p_1 = fromInteger(valueOf(n));
|
92 |
|
|
Bit#(1) sx = pack(x)[si];
|
93 |
|
|
Bit#(1) sy = pack(y)[si];
|
94 |
|
|
Int#(TAdd#(1,n)) ix = unpack({sx,x});
|
95 |
|
|
Int#(TAdd#(1,n)) iy = unpack({sy,y});
|
96 |
|
|
Int#(TAdd#(1,n)) ir = ix - iy + 1;
|
97 |
|
|
Bit#(n) res = (pack(ir))[si_p_1:1];
|
98 |
|
|
return res;
|
99 |
|
|
endfunction
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
function Bit#(n) complex_mult(Bit#(n) x, Bit#(n) y) provisos(Add#(k,n,TAdd#(n,n)));
|
103 |
|
|
Nat si = fromInteger(valueOf(n) - 1) ;
|
104 |
|
|
Nat si2 = fromInteger(2*(valueOf(n) - 1));
|
105 |
|
|
Nat si_1 = fromInteger(valueOf(n) - 2); // 14 for 16
|
106 |
|
|
Bit#(TAdd#(n,n)) half = 1 << (si_1);
|
107 |
|
|
|
108 |
|
|
Int#(TAdd#(n,n)) ix = unpack(signExtend(x));
|
109 |
|
|
Int#(TAdd#(n,n)) iy = unpack(signExtend(y));
|
110 |
|
|
|
111 |
|
|
Bit#(TAdd#(n,n)) t1 = pack(ix*iy);
|
112 |
|
|
Bit#(TAdd#(n,n)) t2 = t1 + half;
|
113 |
|
|
Bit#(n) t3 = t2[si2:si];
|
114 |
|
|
Int#(n) it3 = unpack(t3);
|
115 |
|
|
Bit#(n) res = pack((it3 == minBound) ? maxBound : it3);
|
116 |
|
|
|
117 |
|
|
return res;
|
118 |
|
|
endfunction
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
instance Arith#(ComplexF#(n)) provisos(Add#(1,k,n), Add#(k2,n,TAdd#(n,n)), Add#(1,n,TAdd#(1,n)));
|
122 |
|
|
|
123 |
|
|
function ComplexF#(n) \+ (ComplexF#(n) x, ComplexF#(n) y);
|
124 |
|
|
return ComplexF{
|
125 |
|
|
i: complex_add(x.i, y.i),
|
126 |
|
|
q: complex_add(x.q, y.q)
|
127 |
|
|
};
|
128 |
|
|
endfunction
|
129 |
|
|
|
130 |
|
|
function ComplexF#(n) \- (ComplexF#(n) x, ComplexF#(n) y);
|
131 |
|
|
return ComplexF{
|
132 |
|
|
i: complex_sub(x.i, y.i),
|
133 |
|
|
q: complex_sub(x.q, y.q)
|
134 |
|
|
};
|
135 |
|
|
endfunction
|
136 |
|
|
|
137 |
|
|
function ComplexF#(n) \* (ComplexF#(n) x, ComplexF#(n) y) provisos(Add#(k2,n,TAdd#(n,n)));
|
138 |
|
|
Bit#(n) ii = complex_mult(x.i, y.i);
|
139 |
|
|
Bit#(n) qq = complex_mult(x.q, y.q);
|
140 |
|
|
Bit#(n) iq = complex_mult(x.i, y.q);
|
141 |
|
|
Bit#(n) qi = complex_mult(x.q, y.i);
|
142 |
|
|
|
143 |
|
|
return ComplexF{
|
144 |
|
|
i: complex_add(ii, qq),
|
145 |
|
|
q: complex_sub(qi, iq)
|
146 |
|
|
};
|
147 |
|
|
endfunction
|
148 |
|
|
|
149 |
|
|
function ComplexF#(n) negate (ComplexF#(n) x);
|
150 |
|
|
return ComplexF{
|
151 |
|
|
i: negate(x.i),
|
152 |
|
|
q: negate(x.q)
|
153 |
|
|
};
|
154 |
|
|
endfunction
|
155 |
|
|
|
156 |
|
|
endinstance
|