forked from cdkersey/chdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shifter.h
79 lines (61 loc) · 1.96 KB
/
shifter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef __SHIFTER_H
#define __SHIFTER_H
#include "bvec-basic.h"
#include "mux.h"
#include "hierarchy.h"
namespace chdl {
// Fixed shift by B bits (negative for left, positive for right). A series of
// these could be used to construct a barrel shifter.
template <unsigned N>
bvec<N> ShifterStage(int B, bvec<N> in, node enable, node arith, node rot)
{
HIERARCHY_ENTER();
bvec<N> shifted;
for (int i = 0; i < N; ++i) {
if ((i+B)%N < 0) shifted[i] = And(in[(i+B)%N], rot);
if ((i+B)%N >= 0 && i+B < N) shifted[i] = in[i+B];
else shifted[i] = Or(And(in[N-1], arith),
And(in[(i+B)%N], rot));
}
bvec<N> r(Mux(enable, in, shifted));
HIERARCHY_EXIT();
return r;
}
// 2^M bit bidirectional barrel shifter.
template <unsigned M>
bvec<M> Shifter(bvec<M> in, bvec<CLOG2(M)> shamt,
node arith, node rot, node dir)
{
HIERARCHY_ENTER();
vec<CLOG2(M)+1, bvec<M>> vl, vr;
vl[0] = vr[0] = in;
for (unsigned i = 0; i < CLOG2(M); ++i) {
vl[i+1] = ShifterStage<M>(-(1<<i), vl[i], shamt[i], arith, rot);
vr[i+1] = ShifterStage<M>( (1<<i), vr[i], shamt[i], arith, rot);
}
bvec<M> r(Mux(dir, vl[CLOG2(M)], vr[CLOG2(M)]));
HIERARCHY_EXIT();
return r;
}
template <unsigned N>
bvec<N> operator<<(const bvec<N> &in, const bvec<CLOG2(N)> &shamt)
{
return Shifter(in, shamt, Lit(0), Lit(0), Lit(0));
}
template <unsigned N>
bvec<N> operator>>(const bvec<N> &in, const bvec<CLOG2(N)> &shamt)
{
return Shifter(in, shamt, Lit(0), Lit(0), Lit(1));
}
template <unsigned N>
bvec<N> RotL(const bvec<N> &in, const bvec<CLOG2(N)> &shamt)
{
return Shifter(in, shamt, Lit(0), Lit(1), Lit(0));
}
template <unsigned N>
bvec<N> RotR(const bvec<N> &in, const bvec<CLOG2(N)> &shamt)
{
return Shifter(in, shamt, Lit(0), Lit(1), Lit(1));
}
};
#endif