IT++ Logo
fix.h
Go to the documentation of this file.
1 
29 #ifndef FIX_H
30 #define FIX_H
31 
32 #include <itpp/fixed/fix_base.h>
33 #include <itpp/fixed/fix_factory.h>
34 #include <itpp/itexports.h>
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 Fix : public Fix_Base
52 {
53  friend class CFix;
54  template<int, e_mode, o_mode, q_mode> friend class Fixed;
55  template<int, e_mode, o_mode, q_mode> friend class CFixed;
56 public:
58  Fix(double x = 0.0, int s = 0, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
59  : Fix_Base(s, w, e, o, q, ptr), re(scale_and_apply_modes(x)) {}
61  explicit Fix(const Fix_Factory &f)
62  : Fix_Base(0, f.wordlen, f.emode, f.omode, f.qmode, f.stat_ptr), re(0) {}
64  Fix(fixrep r, int s, int, int)
65  : Fix_Base(s), re(r) {}
67  Fix(const Fix &x, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
68  : Fix_Base(x.shift, w, e, o, q, ptr), re(x.re) {}
70  virtual ~Fix() {}
71 
73  Fix& operator=(const Fix &x);
75  Fix& operator=(const int x);
77  Fix& operator+=(const Fix &x);
79  Fix& operator+=(const int x);
81  Fix& operator-=(const Fix &x);
83  Fix& operator-=(const int x);
85  Fix& operator*=(const Fix &x);
87  Fix& operator*=(const int x);
89  Fix& operator/=(const Fix &x);
91  Fix& operator/=(const int x);
93  Fix operator-() const;
95  Fix& operator<<=(const int n);
97  Fix& operator>>=(const int n);
98 
100  void set(double x, int n);
102  void set(double x, int n, q_mode q);
104  void set_re(fixrep x) {re = apply_o_mode(x);}
105 
107  void lshift(int n);
109  void rshift(int n);
111  void rshift(int n, q_mode q);
112 
114  virtual void print() const;
116  fixrep get_re() const {return re;}
118  double unfix() const;
119 
120 #ifndef NO_IMPLICIT_FIX_CONVERSION
121  operator double() const {
123  it_assert_debug(shift>=-63 && shift <= 64, "Fix::operator double: Illegal shift!");
124  return double(re)*DOUBLE_POW2[64 - shift];
125  }
126 #endif
127 
129  friend ITPP_EXPORT int assert_shifts(const CFix &x, const Fix &y);
131  friend ITPP_EXPORT int assert_shifts(const Fix &x, const Fix &y);
133  friend ITPP_EXPORT int assert_shifts(const Fix &x, int y);
134 
135 protected:
138 };
139 
141 ITPP_EXPORT int assert_shifts(const Fix &x, const Fix &y);
143 ITPP_EXPORT int assert_shifts(const Fix &x, int y);
144 
146 ITPP_EXPORT std::istream &operator>>(std::istream &is, Fix &x);
148 ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const Fix &x);
149 
151 typedef Vec<Fix> fixvec;
153 typedef Mat<Fix> fixmat;
154 
155 // Specialization of template definition in vec.cpp
156 template<> void fixvec::set(const char *values);
157 // Specialization of template definition in mat.cpp
158 template<> void fixmat::set(const char *values);
159 
161 
162 } // namespace itpp
163 
164 #endif // #ifndef FIX_H
Templated fixed-point data type.
Definition: fixed.h:48
Templated complex fixed-point data type.
Definition: cfixed.h:50
Definitions of a base class for fixed-point data types.
Fix(double x=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: fix.h:58
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
Definitions of a class factory for fixed-point data types Fix and CFix.
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition: binary.cpp:36
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
fixrep re
Data representation.
Definition: fix.h:137
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
fixrep get_re() const
Get data representation (mainly for internal use since it reveals the representation type) ...
Definition: fix.h:116
virtual ~Fix()
Destructor.
Definition: fix.h:70
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
o_mode
Overflow modes (aligned with SystemC)
Definition: fix_base.h:948
itpp namespace
Definition: itmex.h:36
const int MAX_WORDLEN
Max word length.
Definition: fix_base.h:886
Fix(const Fix &x, int w=MAX_WORDLEN, e_mode e=TC, o_mode o=WRAP, q_mode q=TRN, Stat *ptr=0)
Copy constructor.
Definition: fix.h:67
Fixed-point data type.
Definition: fix.h:51
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
Vector Class (Templated)
Definition: factory.h:42
Fix(const Fix_Factory &f)
Constructor.
Definition: fix.h:61
int64_t fixrep
Representation for fixed-point data types.
Definition: fix_base.h:884
Mat< Fix > fixmat
Typedef for fixed-point matrix type.
Definition: fix.h:153
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
Fix(fixrep r, int s, int, int)
Constructor for internal use. No restrictions are applied. The dummies help to avoid ambiguities...
Definition: fix.h:64
q_mode
Quantization modes (aligned with SystemC)
Definition: fix_base.h:957
Vec< Fix > fixvec
Typedef for fixed-point vector type.
Definition: fix.h:151
void set_re(fixrep x)
Set data representation (mainly for internal use since it reveals the representation type) ...
Definition: fix.h:104

Generated on Thu Jun 21 2018 16:06:18 for IT++ by Doxygen 1.8.13