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 |
|
|
import Vector::*;
|
27 |
|
|
|
28 |
|
|
function Bit#(1) getParity(Bit#(n) v) provisos(Add#(1, k, n));
|
29 |
|
|
function Bit#(1) _xor(Bit#(1) x, Bit#(1) y);
|
30 |
|
|
return (x ^ y);
|
31 |
|
|
endfunction
|
32 |
|
|
|
33 |
|
|
Vector#(n,Bit#(1)) vv = unpack(v);
|
34 |
|
|
Bit#(1) parity = Vector::fold(_xor, vv);
|
35 |
|
|
return(parity);
|
36 |
|
|
endfunction
|
37 |
|
|
|
38 |
|
|
function Bit#(n) reverseBits(Bit#(n) x);
|
39 |
|
|
Vector#(n, Bit#(1)) vx = unpack(x);
|
40 |
|
|
Vector#(n, Bit#(1)) rvx = Vector::reverse(vx);
|
41 |
|
|
Bit#(n) prvx = pack(rvx);
|
42 |
|
|
return(prvx);
|
43 |
|
|
endfunction
|
44 |
|
|
|
45 |
|
|
function Bit#(1) signBit(Bit#(n) x) provisos(Add#(1,k,n));
|
46 |
|
|
match {.signbit,.rest} = split(x);
|
47 |
|
|
return(signbit);
|
48 |
|
|
endfunction
|
49 |
|
|
|
50 |
|
|
function Vector#(sz, any_e) zipWith4(function any_e f(any_a a, any_b b, any_c c, any_d d),
|
51 |
|
|
Vector#(sz, any_a) va, Vector#(sz, any_b) vb,
|
52 |
|
|
Vector#(sz, any_c) vc, Vector#(sz, any_d) vd);
|
53 |
|
|
|
54 |
|
|
Vector#(sz, any_e) ve = replicate(?);
|
55 |
|
|
|
56 |
|
|
for(Integer i = 0; i < valueOf(sz); i = i + 1)
|
57 |
|
|
ve[i] = f(va[i],vb[i],vc[i],vd[i]);
|
58 |
|
|
|
59 |
|
|
return(ve);
|
60 |
|
|
|
61 |
|
|
endfunction
|
62 |
|
|
|
63 |
|
|
function Vector#(n, Bit#(m)) bitBreak(Bit#(nm) x) provisos(Mul#(n,m,nm));
|
64 |
|
|
return unpack(x);
|
65 |
|
|
endfunction
|
66 |
|
|
|
67 |
|
|
function Bit#(nm) bitMerge(Vector#(n, Bit#(m)) x) provisos(Mul#(n,m,nm));
|
68 |
|
|
return pack(x);
|
69 |
|
|
endfunction
|
70 |
|
|
|
71 |
|
|
function Vector#(n, alpha) v_truncate(Vector#(m, alpha) in) provisos(Add#(n,k,m));
|
72 |
|
|
Vector#(n, alpha) retval = newVector();
|
73 |
|
|
for(Integer i = valueOf(n) - 1, Integer j = valueOf(m) - 1; i >= 0; i = i - 1, j = j - 1)
|
74 |
|
|
begin
|
75 |
|
|
retval[i] = in[j];
|
76 |
|
|
end
|
77 |
|
|
|
78 |
|
|
return (retval);
|
79 |
|
|
endfunction
|
80 |
|
|
|
81 |
|
|
function Vector#(n, alpha) v_rtruncate(Vector#(m, alpha) in) provisos(Add#(n,k,m));
|
82 |
|
|
Vector#(n, alpha) retval = newVector();
|
83 |
|
|
for(Integer i = 0; i < valueOf(n); i = i + 1)
|
84 |
|
|
begin
|
85 |
|
|
retval[i] = in[i];
|
86 |
|
|
end
|
87 |
|
|
return (retval);
|
88 |
|
|
endfunction
|