17 #ifndef LEANSDR_DISCRMATH_H 18 #define LEANSDR_DISCRMATH_H 32 template <
typename T,
int N>
35 static const size_t WSIZE =
sizeof(T) * 8;
36 static const size_t NW = (N + WSIZE - 1) / WSIZE;
43 for (
int i = 1;
i <
NW; ++
i)
51 int nw = (a.
NW <
NW) ? a.
NW : NW;
52 for (
int i = 0;
i < nw; ++
i)
55 for (
int i = a.
NW;
i < NW; ++
i)
64 return v[i /
WSIZE] & ((T)1 << (i & (WSIZE - 1)));
72 int nw = (a.
NW <
NW) ? a.
NW : NW;
73 for (
int i = 0;
i < nw; ++
i)
85 fail(
"shift exceeds word width");
86 T mask = ~(((T)-1) << n);
87 for (
int i = NW - 1;
i > 0; --
i)
88 v[
i] = (v[
i] << n) | ((v[
i - 1] >> (WSIZE - n)) & mask);
99 v[NW - 1] &= ((T)-1) >> (NW * WSIZE - N);
110 template <
typename T,
int N,
typename Tm>
114 const Tm bitmask = (Tm)1 << (
sizeof(Tm) * 8 - 1);
118 for (
int bit =
sizeof(Tm) * 8; bit--; mi <<= 1)
121 bool resN = res[N - 1];
137 template <
typename T,
int N,
typename Tm>
142 for (
int i = 0;
i < res.
NW; ++
i)
144 const Tm bitmask = (Tm)1 << (
sizeof(Tm) * 8 - 1);
148 for (
int bit =
sizeof(Tm) * 8; bit--; mi <<= 1)
151 bool resN = res[N - 1];
154 resN ^= (bool)(mi & bitmask);
163 template <
typename T,
int N>
166 for (
int i = 0;
i < a.
NW; ++
i)
167 if (a.
v[
i] != b.
v[
i])
174 template <
typename T,
int N>
178 for (
int i = 0;
i < a.
NW; ++
i)
179 res.
v[
i] = a.
v[
i] ^ b.
v[
i];
185 template <
typename T,
int N,
int NB>
189 for (
int i = 0;
i < NB; ++
i, a <<= 1)
217 template <
typename Te,
int N, Te ALPHA, Te TRUNCP>
221 static const Te alpha = ALPHA;
225 fail(
"alpha!=2 not implemented");
228 for (
int i = 0;
i < (1 << N); ++
i)
230 lut_exp[
i] = alpha_i;
231 lut_exp[((1 << N) - 1) +
i] = alpha_i;
232 lut_log[alpha_i] =
i;
233 bool overflow = alpha_i & (1 << (N - 1));
235 alpha_i &= ~((~(Te)0) << N);
240 inline Te
add(Te x, Te y) {
return x ^ y; }
241 inline Te
sub(Te x, Te y) {
return x ^ y; }
246 return lut_exp[lut_log[x] + lut_log[y]];
252 return lut_exp[lut_log[x] + ((1 << N) - 1) - lut_log[y]];
256 return lut_exp[((1 << N) - 1) - lut_log[x]];
258 inline Te
exp(Te x) {
return lut_exp[x]; }
259 inline Te
log(Te x) {
return lut_log[x]; }
262 Te lut_exp[(1 << N) * 2];
268 #endif // LEANSDR_DISCRMATH_H
bitvect< T, N > operator*(bitvect< T, N > a, const bitvect< T, NB > &b)
bitvect< T, N > & operator<<=(unsigned int n)
static const size_t WSIZE
bool operator[](unsigned int i) const
bitvect< T, N > & copy(const bitvect< T, M > &a)
bitvect< T, N > divmod(const Tm *m, size_t nm, const bitvect< T, N > &p)
bool operator==(const bitvect< T, N > &a, const bitvect< T, N > &b)
bitvect< T, N > & operator+=(const bitvect< T, M > &a)
bitvect< T, N > operator+(const bitvect< T, N > &a, const bitvect< T, N > &b)
bitvect< T, N > shiftdivmod(const Tm *m, size_t nm, const bitvect< T, N > &p, T init=0)