IT++ Logo
fix_base.h
Go to the documentation of this file.
1 
29 #ifndef FIX_BASE_H
30 #define FIX_BASE_H
31 
32 #include <itpp/base/ittypes.h>
33 #include <itpp/stat/misc_stat.h>
34 #include <itpp/itexports.h>
35 
36 
37 namespace itpp
38 {
39 
881 
884 typedef int64_t fixrep;
886 const int MAX_WORDLEN = 64;
887 
889 const uint64_t UINT64_POW2[64] = {
890  uint64_t(1), uint64_t(1) << 1, uint64_t(1) << 2, uint64_t(1) << 3, uint64_t(1) << 4,
891  uint64_t(1) << 5, uint64_t(1) << 6, uint64_t(1) << 7, uint64_t(1) << 8, uint64_t(1) << 9,
892  uint64_t(1) << 10, uint64_t(1) << 11, uint64_t(1) << 12, uint64_t(1) << 13, uint64_t(1) << 14,
893  uint64_t(1) << 15, uint64_t(1) << 16, uint64_t(1) << 17, uint64_t(1) << 18, uint64_t(1) << 19,
894  uint64_t(1) << 20, uint64_t(1) << 21, uint64_t(1) << 22, uint64_t(1) << 23, uint64_t(1) << 24,
895  uint64_t(1) << 25, uint64_t(1) << 26, uint64_t(1) << 27, uint64_t(1) << 28, uint64_t(1) << 29,
896  uint64_t(1) << 30, uint64_t(1) << 31, uint64_t(1) << 32, uint64_t(1) << 33, uint64_t(1) << 34,
897  uint64_t(1) << 35, uint64_t(1) << 36, uint64_t(1) << 37, uint64_t(1) << 38, uint64_t(1) << 39,
898  uint64_t(1) << 40, uint64_t(1) << 41, uint64_t(1) << 42, uint64_t(1) << 43, uint64_t(1) << 44,
899  uint64_t(1) << 45, uint64_t(1) << 46, uint64_t(1) << 47, uint64_t(1) << 48, uint64_t(1) << 49,
900  uint64_t(1) << 50, uint64_t(1) << 51, uint64_t(1) << 52, uint64_t(1) << 53, uint64_t(1) << 54,
901  uint64_t(1) << 55, uint64_t(1) << 56, uint64_t(1) << 57, uint64_t(1) << 58, uint64_t(1) << 59,
902  uint64_t(1) << 60, uint64_t(1) << 61, uint64_t(1) << 62, uint64_t(1) << 63
903 };
904 
906 const double DOUBLE_POW2[128] = {
907  0.5 / UINT64_POW2[63], 1.0 / UINT64_POW2[63], 1.0 / UINT64_POW2[62], 1.0 / UINT64_POW2[61],
908  1.0 / UINT64_POW2[60], 1.0 / UINT64_POW2[59], 1.0 / UINT64_POW2[58], 1.0 / UINT64_POW2[57],
909  1.0 / UINT64_POW2[56], 1.0 / UINT64_POW2[55], 1.0 / UINT64_POW2[54], 1.0 / UINT64_POW2[53],
910  1.0 / UINT64_POW2[52], 1.0 / UINT64_POW2[51], 1.0 / UINT64_POW2[50], 1.0 / UINT64_POW2[49],
911  1.0 / UINT64_POW2[48], 1.0 / UINT64_POW2[47], 1.0 / UINT64_POW2[46], 1.0 / UINT64_POW2[45],
912  1.0 / UINT64_POW2[44], 1.0 / UINT64_POW2[43], 1.0 / UINT64_POW2[42], 1.0 / UINT64_POW2[41],
913  1.0 / UINT64_POW2[40], 1.0 / UINT64_POW2[39], 1.0 / UINT64_POW2[38], 1.0 / UINT64_POW2[37],
914  1.0 / UINT64_POW2[36], 1.0 / UINT64_POW2[35], 1.0 / UINT64_POW2[34], 1.0 / UINT64_POW2[33],
915  1.0 / UINT64_POW2[32], 1.0 / UINT64_POW2[31], 1.0 / UINT64_POW2[30], 1.0 / UINT64_POW2[29],
916  1.0 / UINT64_POW2[28], 1.0 / UINT64_POW2[27], 1.0 / UINT64_POW2[26], 1.0 / UINT64_POW2[25],
917  1.0 / UINT64_POW2[24], 1.0 / UINT64_POW2[23], 1.0 / UINT64_POW2[22], 1.0 / UINT64_POW2[21],
918  1.0 / UINT64_POW2[20], 1.0 / UINT64_POW2[19], 1.0 / UINT64_POW2[18], 1.0 / UINT64_POW2[17],
919  1.0 / UINT64_POW2[16], 1.0 / UINT64_POW2[15], 1.0 / UINT64_POW2[14], 1.0 / UINT64_POW2[13],
920  1.0 / UINT64_POW2[12], 1.0 / UINT64_POW2[11], 1.0 / UINT64_POW2[10], 1.0 / UINT64_POW2[9],
921  1.0 / UINT64_POW2[8], 1.0 / UINT64_POW2[7], 1.0 / UINT64_POW2[6], 1.0 / UINT64_POW2[5],
922  1.0 / UINT64_POW2[4], 1.0 / UINT64_POW2[3], 1.0 / UINT64_POW2[2], 1.0 / UINT64_POW2[1],
923  1.0, 1.0*UINT64_POW2[1], 1.0*UINT64_POW2[2], 1.0*UINT64_POW2[3],
924  1.0*UINT64_POW2[4], 1.0*UINT64_POW2[5], 1.0*UINT64_POW2[6], 1.0*UINT64_POW2[7],
925  1.0*UINT64_POW2[8], 1.0*UINT64_POW2[9], 1.0*UINT64_POW2[10], 1.0*UINT64_POW2[11],
926  1.0*UINT64_POW2[12], 1.0*UINT64_POW2[13], 1.0*UINT64_POW2[14], 1.0*UINT64_POW2[15],
927  1.0*UINT64_POW2[16], 1.0*UINT64_POW2[17], 1.0*UINT64_POW2[18], 1.0*UINT64_POW2[19],
928  1.0*UINT64_POW2[20], 1.0*UINT64_POW2[21], 1.0*UINT64_POW2[22], 1.0*UINT64_POW2[23],
929  1.0*UINT64_POW2[24], 1.0*UINT64_POW2[25], 1.0*UINT64_POW2[26], 1.0*UINT64_POW2[27],
930  1.0*UINT64_POW2[28], 1.0*UINT64_POW2[29], 1.0*UINT64_POW2[30], 1.0*UINT64_POW2[31],
931  1.0*UINT64_POW2[32], 1.0*UINT64_POW2[33], 1.0*UINT64_POW2[34], 1.0*UINT64_POW2[35],
932  1.0*UINT64_POW2[36], 1.0*UINT64_POW2[37], 1.0*UINT64_POW2[38], 1.0*UINT64_POW2[39],
933  1.0*UINT64_POW2[40], 1.0*UINT64_POW2[41], 1.0*UINT64_POW2[42], 1.0*UINT64_POW2[43],
934  1.0*UINT64_POW2[44], 1.0*UINT64_POW2[45], 1.0*UINT64_POW2[46], 1.0*UINT64_POW2[47],
935  1.0*UINT64_POW2[48], 1.0*UINT64_POW2[49], 1.0*UINT64_POW2[50], 1.0*UINT64_POW2[51],
936  1.0*UINT64_POW2[52], 1.0*UINT64_POW2[53], 1.0*UINT64_POW2[54], 1.0*UINT64_POW2[55],
937  1.0*UINT64_POW2[56], 1.0*UINT64_POW2[57], 1.0*UINT64_POW2[58], 1.0*UINT64_POW2[59],
938  1.0*UINT64_POW2[60], 1.0*UINT64_POW2[61], 1.0*UINT64_POW2[62], 1.0*UINT64_POW2[63]
939 };
940 
942 enum e_mode {
943  TC,
944  US
945 };
946 
948 enum o_mode {
949  SAT,
954 };
955 
957 enum q_mode {
958  RND,
964  TRN,
966 };
967 
974 };
975 
981 class ITPP_EXPORT Fix_Base
982 {
983 public:
985  explicit Fix_Base(int s = 0, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
986  : shift(s), wordlen(w), emode(e), omode(o), qmode(q), stat_ptr(ptr) {init();}
988  Fix_Base(const Fix_Base &x)
989  : shift(x.shift), wordlen(MAX_WORDLEN), emode(TC), omode(WRAP), qmode(TRN), stat_ptr(0) {init();}
991  virtual ~Fix_Base() {}
992 
994  void set_shift(int s) {shift = s;}
996  static void set_output_mode(output_mode o) {outputmode = o;}
998  static void set_output_mode(std::string o);
999 
1001  int get_shift() const {return shift;}
1003  int get_wordlen() const {return wordlen;}
1005  e_mode get_e_mode() const {return emode;}
1007  o_mode get_o_mode() const {return omode;}
1009  q_mode get_q_mode() const {return qmode;}
1011  output_mode get_output_mode() const {return outputmode;}
1013  fixrep get_max() const {return max;}
1015  fixrep get_min() const {return min;}
1017  virtual void print() const;
1018 
1019 protected:
1021  int shift;
1023  int wordlen;
1033  fixrep min;
1035  fixrep max;
1038 
1040  void init();
1042  fixrep apply_o_mode(fixrep x) const;
1044  fixrep scale_and_apply_modes(double x) const {return scale_and_apply_modes(x, qmode);}
1046  fixrep scale_and_apply_modes(double x, q_mode q) const;
1048  fixrep rshift_and_apply_q_mode(fixrep x, int n) const {return rshift_and_apply_q_mode(x, n, qmode);}
1050  fixrep rshift_and_apply_q_mode(fixrep x, int n, q_mode q) const;
1051 
1052 private:
1054  static output_mode outputmode;
1055 };
1056 
1058 inline std::ostream &operator<<(std::ostream &os, const output_mode &o)
1059 {
1061  return os;
1062 }
1063 
1065 
1066 } // namespace itpp
1067 
1068 #endif // #ifndef FIX_BASE_H
output_mode get_output_mode() const
Get output mode.
Definition: fix_base.h:1011
int wordlen
Word length.
Definition: fix_base.h:1023
static void set_output_mode(output_mode o)
Set output mode to OUTPUT_FIX, OUTPUT_FIX_SHIFT, OUTPUT_FLOAT or OUTPUT_FLOAT_SHIFT. Static member function.
Definition: fix_base.h:996
Truncation to zero.
Definition: fix_base.h:965
fixrep scale_and_apply_modes(double x) const
Convert from double to fixrep using shift and quantization mode qmode, then call limit() ...
Definition: fix_base.h:1044
Rounding to infinity.
Definition: fix_base.h:961
Rounding to minus infinity.
Definition: fix_base.h:960
Saturation.
Definition: fix_base.h:949
q_mode get_q_mode() const
Get quantization mode.
Definition: fix_base.h:1009
Output floating-point value.
Definition: fix_base.h:972
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition: binary.cpp:36
const uint64_t UINT64_POW2[64]
Table for fast multiplication or division by 2^n.
Definition: fix_base.h:889
Unsigned.
Definition: fix_base.h:944
void set_shift(int s)
Set shift (without shifting)
Definition: fix_base.h:994
fixrep rshift_and_apply_q_mode(fixrep x, int n) const
Right shift n bits using quantization mode qmode and make call to statistics object (if any) ...
Definition: fix_base.h:1048
Stat * stat_ptr
Pointer to statistics object.
Definition: fix_base.h:1031
e_mode emode
Sign encoding mode.
Definition: fix_base.h:1025
e_mode get_e_mode() const
Get sign encoding mode.
Definition: fix_base.h:1005
virtual ~Fix_Base()
Destructor.
Definition: fix_base.h:991
fixrep get_max() const
Get maximum value of data representation.
Definition: fix_base.h:1013
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
const double DOUBLE_POW2[128]
Table for fast multiplication by 2^(n-64)
Definition: fix_base.h:906
e_mode
Sign encoding modes (aligned with SystemC)
Definition: fix_base.h:942
Output fixed-point representation only.
Definition: fix_base.h:970
Fix_Base(const Fix_Base &x)
Copy constructor.
Definition: fix_base.h:988
IT++ type definitions.
Truncation.
Definition: fix_base.h:964
Rounding to plus infinity.
Definition: fix_base.h:958
T max(const Vec< T > &v)
Maximum value of vector.
Definition: min_max.h:45
Rounding to zero.
Definition: fix_base.h:959
o_mode
Overflow modes (aligned with SystemC)
Definition: fix_base.h:948
Convergent rounding with half-way value rounded to odd value (not defined in SystemC) ...
Definition: fix_base.h:963
fixrep get_min() const
Get minimum value of data representation.
Definition: fix_base.h:1015
q_mode qmode
Quantization mode.
Definition: fix_base.h:1029
fixrep min
Minimum allowed value (help variable to speed up calculations)
Definition: fix_base.h:1033
int n_unused_bits
Number of unused (MSB) bits (help variable to speed up calculations)
Definition: fix_base.h:1037
o_mode omode
Overflow mode.
Definition: fix_base.h:1027
itpp namespace
Definition: itmex.h:36
output_mode
Output modes.
Definition: fix_base.h:969
const int MAX_WORDLEN
Max word length.
Definition: fix_base.h:886
Miscellaneous statistics functions and classes - header file.
int shift
Accumulated bitshift (positive means left-shifted, negative means right-shifted)
Definition: fix_base.h:1021
Output floating-point value followed by <<shift.
Definition: fix_base.h:973
Base class for fixed-point data types.
Definition: fix_base.h:981
Fix_Base(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_base.h:985
Symmetrical saturation (Not implemented)
Definition: fix_base.h:951
o_mode get_o_mode() const
Get overflow mode.
Definition: fix_base.h:1007
int get_wordlen() const
Get word length.
Definition: fix_base.h:1003
Sign magnitued wrap-around (Not implemented)
Definition: fix_base.h:953
int64_t fixrep
Representation for fixed-point data types.
Definition: fix_base.h:884
Output fixed-point representation followed by <shift> (default)
Definition: fix_base.h:971
Wrap-around.
Definition: fix_base.h:952
Two&#39;s complement.
Definition: fix_base.h:943
A class for sampling a signal and calculating statistics.
Definition: misc_stat.h:48
Convergent rounding with half-way value rounded to even value.
Definition: fix_base.h:962
Saturation to zero (Not implemented)
Definition: fix_base.h:950
q_mode
Quantization modes (aligned with SystemC)
Definition: fix_base.h:957
int get_shift() const
Get shift.
Definition: fix_base.h:1001
fixrep max
Maximum allowed value (help variable to speed up calculations)
Definition: fix_base.h:1035

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