IT++ Logo
cfix.h
Go to the documentation of this file.
1 
29 #ifndef CFIX_H
30 #define CFIX_H
31 
32 #include <itpp/fixed/fix.h>
33 #include <itpp/itexports.h>
34 
35 
36 namespace itpp
37 {
38 
39 // Forward declarations
40 template<class Num_T> class Vec;
41 template<class Num_T> class Mat;
42 
45 
51 class ITPP_EXPORT CFix : public Fix_Base
52 {
53  template<int, e_mode, o_mode, q_mode> friend class CFixed;
54 public:
56  CFix(double r = 0.0, double i = 0.0, int s = 0, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
57  : Fix_Base(s, w, e, o, q, ptr), re(scale_and_apply_modes(r)), im(scale_and_apply_modes(i)) {}
59  CFix(std::complex<double> x, double, int s = 0, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
60  : Fix_Base(s, w, e, o, q, ptr), re(scale_and_apply_modes(std::real(x))), im(scale_and_apply_modes(std::imag(x))) {}
62  explicit CFix(const Fix_Factory &f)
63  : Fix_Base(0, f.wordlen, f.emode, f.omode, f.qmode, f.stat_ptr), re(0), im(0) {}
65  CFix(fixrep r, fixrep i, int s, int, int)
66  : Fix_Base(s), re(r), im(i) {}
68  CFix(const Fix &r, const Fix &i = 0.0, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
69  : Fix_Base(assert_shifts(r, i), w, e, o, q, ptr), re(r.re), im(i.re) {}
71  CFix(const CFix &x, double, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
72  : Fix_Base(x.shift, w, e, o, q, ptr), re(x.re), im(x.im) {}
74  virtual ~CFix() {}
75 
77  CFix& operator=(const CFix &x);
79  CFix& operator=(const Fix &x);
81  CFix& operator=(const std::complex<double> &x);
83  CFix& operator=(const int x);
85  CFix& operator+=(const CFix &x);
87  CFix& operator+=(const Fix &x);
89  CFix& operator+=(const int x);
91  CFix& operator-=(const CFix &x);
93  CFix& operator-=(const Fix &x);
95  CFix& operator-=(const int x);
97  CFix& operator*=(const CFix &x);
99  CFix& operator*=(const Fix &x);
101  CFix& operator*=(const int x);
103  CFix& operator/=(const CFix &x);
105  CFix& operator/=(const Fix &x);
107  CFix& operator/=(const int x);
109  CFix operator-() const;
111  CFix& operator<<=(const int n);
113  CFix& operator>>=(const int n);
114 
116  void set(double real, double imag, int n);
118  void set(double real, double imag, int n, q_mode q);
120  void set(const std::complex<double> &x, int n);
122  void set(const std::complex<double> &x, int n, q_mode q);
124  void set_re(fixrep x) {re = apply_o_mode(x);}
126  void set_im(fixrep x) {im = apply_o_mode(x);}
127 
129  void lshift(int n);
131  void rshift(int n);
133  void rshift(int n, q_mode q);
134 
136  virtual void print() const;
138  fixrep get_re() const {return re;}
140  fixrep get_im() const {return im;}
142  std::complex<double> unfix() const;
143 
144 #ifndef NO_IMPLICIT_FIX_CONVERSION
145  operator std::complex<double>() const {
147  it_assert_debug(shift >= -63 && shift <= 64, "CFix::operator complex<double>: Illegal shift!");
148  return std::complex<double>(double(re)*DOUBLE_POW2[64 - shift],
149  double(im)*DOUBLE_POW2[64 - shift]);
150  }
151 #endif
152 
154  friend ITPP_EXPORT int assert_shifts(const CFix &x, const CFix &y);
156  friend ITPP_EXPORT int assert_shifts(const CFix &x, const Fix &y);
158  friend ITPP_EXPORT int assert_shifts(const CFix &x, int y);
159 
160 protected:
163 };
164 
166 ITPP_EXPORT int assert_shifts(const CFix &x, const CFix &y);
168 ITPP_EXPORT int assert_shifts(const CFix &x, const Fix &y);
170 ITPP_EXPORT int assert_shifts(const CFix &x, int y);
171 
173 ITPP_EXPORT std::istream &operator>>(std::istream &is, CFix &x);
175 ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const CFix &x);
176 
181 
182 // Specialization of template definition in vec.cpp
183 template<> void cfixvec::set(const char *values);
184 // Specialization of template definition in mat.cpp
185 template<> void cfixmat::set(const char *values);
186 
188 
189 } // namespace itpp
190 
191 #endif // #ifndef CFIX_H
Templated complex fixed-point data type.
Definition: cfixed.h:50
CFix(const Fix_Factory &f)
Constructor.
Definition: cfix.h:62
CFix(fixrep r, fixrep i, int s, int, int)
Constructor for internal use. No restrictions are applied. The dummies help to avoid ambiguities...
Definition: cfix.h:65
vec imag(const cvec &data)
Imaginary part of complex values.
Definition: elem_math.cpp:180
Mat< CFix > cfixmat
Typedef for complex fixed-point matrix type.
Definition: cfix.h:180
void set(const char *str)
Set the vector equal to the values in the str string.
Definition: vec.h:814
Matrix Class (Templated)
Definition: factory.h:41
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition: binary.cpp:36
CFix(const Fix &r, const Fix &i=0.0, int w=MAX_WORDLEN, e_mode e=TC, o_mode o=WRAP, q_mode q=TRN, Stat *ptr=0)
Constructor.
Definition: cfix.h:68
STL namespace.
fixrep re
Real data part.
Definition: cfix.h:161
void set(const std::string &str)
Set matrix equal to values in the string str.
Definition: mat.h:762
Class factory for fixed-point data types Fix and CFix.
Definition: fix_factory.h:120
double unfix(const Fix &x)
Convert Fix to double by multiplying the bit representation with pow2(-shift)
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
const double DOUBLE_POW2[128]
Table for fast multiplication by 2^(n-64)
Definition: fix_base.h:906
Complex fixed-point data type.
Definition: cfix.h:51
Mat< Num_T > operator-(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Subtraction of two matrices.
Definition: mat.h:1382
e_mode
Sign encoding modes (aligned with SystemC)
Definition: fix_base.h:942
Truncation.
Definition: fix_base.h:964
CFix(const CFix &x, double, int w=MAX_WORDLEN, e_mode e=TC, o_mode o=WRAP, q_mode q=TRN, Stat *ptr=0)
Copy constructor.
Definition: cfix.h:71
o_mode
Overflow modes (aligned with SystemC)
Definition: fix_base.h:948
void set_im(fixrep x)
Set data representation for imaginary part (mainly for internal use since it reveals the representati...
Definition: cfix.h:126
itpp namespace
Definition: itmex.h:36
const int MAX_WORDLEN
Max word length.
Definition: fix_base.h:886
virtual ~CFix()
Destructor.
Definition: cfix.h:74
CFix(std::complex< double > x, double, int s=0, int w=MAX_WORDLEN, e_mode e=TC, o_mode o=WRAP, q_mode q=TRN, Stat *ptr=0)
Constructor.
Definition: cfix.h:59
void set_re(fixrep x)
Set data representation for real part (mainly for internal use since it reveals the representation ty...
Definition: cfix.h:124
fixrep im
Imaginary data part.
Definition: cfix.h:162
Fixed-point data type.
Definition: fix.h:51
fixrep get_im() const
Get data representation for imaginary part (mainly for internal use since it reveals the representati...
Definition: cfix.h:140
Base class for fixed-point data types.
Definition: fix_base.h:981
int assert_shifts(const CFix &x, const CFix &y)
Check that x.shift==y.shift OR x==0 OR y==0 and return the shift (for the non-zero argument) ...
Definition: cfix.cpp:249
Vec< CFix > cfixvec
Typedef for complex fixed-point vector type.
Definition: cfix.h:178
Definitions of a fixed-point data type Fix.
Vector Class (Templated)
Definition: factory.h:42
fixrep get_re() const
Get data representation for real part (mainly for internal use since it reveals the representation ty...
Definition: cfix.h:138
CFix(double r=0.0, double i=0.0, int s=0, int w=MAX_WORDLEN, e_mode e=TC, o_mode o=WRAP, q_mode q=TRN, Stat *ptr=0)
Default constructor.
Definition: cfix.h:56
int64_t fixrep
Representation for fixed-point data types.
Definition: fix_base.h:884
Wrap-around.
Definition: fix_base.h:952
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition: binary.cpp:42
Two&#39;s complement.
Definition: fix_base.h:943
A class for sampling a signal and calculating statistics.
Definition: misc_stat.h:48
vec real(const cvec &data)
Real part of complex values.
Definition: elem_math.cpp:157
q_mode
Quantization modes (aligned with SystemC)
Definition: fix_base.h:957
SourceForge Logo

Generated on Sun Apr 10 2022 12:00:00 for IT++ by Doxygen 1.8.14