00001 00030 #ifndef FIX_BASE_H 00031 #define FIX_BASE_H 00032 00033 #include <itpp/base/ittypes.h> 00034 #include <itpp/stat/misc_stat.h> 00035 00036 00037 namespace itpp 00038 { 00039 00881 00882 00884 typedef int64_t fixrep; 00886 const int MAX_WORDLEN = 64; 00887 00889 const uint64_t UINT64_POW2[64] = { 00890 uint64_t(1), uint64_t(1) << 1, uint64_t(1) << 2, uint64_t(1) << 3, uint64_t(1) << 4, 00891 uint64_t(1) << 5, uint64_t(1) << 6, uint64_t(1) << 7, uint64_t(1) << 8, uint64_t(1) << 9, 00892 uint64_t(1) << 10, uint64_t(1) << 11, uint64_t(1) << 12, uint64_t(1) << 13, uint64_t(1) << 14, 00893 uint64_t(1) << 15, uint64_t(1) << 16, uint64_t(1) << 17, uint64_t(1) << 18, uint64_t(1) << 19, 00894 uint64_t(1) << 20, uint64_t(1) << 21, uint64_t(1) << 22, uint64_t(1) << 23, uint64_t(1) << 24, 00895 uint64_t(1) << 25, uint64_t(1) << 26, uint64_t(1) << 27, uint64_t(1) << 28, uint64_t(1) << 29, 00896 uint64_t(1) << 30, uint64_t(1) << 31, uint64_t(1) << 32, uint64_t(1) << 33, uint64_t(1) << 34, 00897 uint64_t(1) << 35, uint64_t(1) << 36, uint64_t(1) << 37, uint64_t(1) << 38, uint64_t(1) << 39, 00898 uint64_t(1) << 40, uint64_t(1) << 41, uint64_t(1) << 42, uint64_t(1) << 43, uint64_t(1) << 44, 00899 uint64_t(1) << 45, uint64_t(1) << 46, uint64_t(1) << 47, uint64_t(1) << 48, uint64_t(1) << 49, 00900 uint64_t(1) << 50, uint64_t(1) << 51, uint64_t(1) << 52, uint64_t(1) << 53, uint64_t(1) << 54, 00901 uint64_t(1) << 55, uint64_t(1) << 56, uint64_t(1) << 57, uint64_t(1) << 58, uint64_t(1) << 59, 00902 uint64_t(1) << 60, uint64_t(1) << 61, uint64_t(1) << 62, uint64_t(1) << 63 00903 }; 00904 00906 const double DOUBLE_POW2[128] = { 00907 0.5 / UINT64_POW2[63], 1.0 / UINT64_POW2[63], 1.0 / UINT64_POW2[62], 1.0 / UINT64_POW2[61], 00908 1.0 / UINT64_POW2[60], 1.0 / UINT64_POW2[59], 1.0 / UINT64_POW2[58], 1.0 / UINT64_POW2[57], 00909 1.0 / UINT64_POW2[56], 1.0 / UINT64_POW2[55], 1.0 / UINT64_POW2[54], 1.0 / UINT64_POW2[53], 00910 1.0 / UINT64_POW2[52], 1.0 / UINT64_POW2[51], 1.0 / UINT64_POW2[50], 1.0 / UINT64_POW2[49], 00911 1.0 / UINT64_POW2[48], 1.0 / UINT64_POW2[47], 1.0 / UINT64_POW2[46], 1.0 / UINT64_POW2[45], 00912 1.0 / UINT64_POW2[44], 1.0 / UINT64_POW2[43], 1.0 / UINT64_POW2[42], 1.0 / UINT64_POW2[41], 00913 1.0 / UINT64_POW2[40], 1.0 / UINT64_POW2[39], 1.0 / UINT64_POW2[38], 1.0 / UINT64_POW2[37], 00914 1.0 / UINT64_POW2[36], 1.0 / UINT64_POW2[35], 1.0 / UINT64_POW2[34], 1.0 / UINT64_POW2[33], 00915 1.0 / UINT64_POW2[32], 1.0 / UINT64_POW2[31], 1.0 / UINT64_POW2[30], 1.0 / UINT64_POW2[29], 00916 1.0 / UINT64_POW2[28], 1.0 / UINT64_POW2[27], 1.0 / UINT64_POW2[26], 1.0 / UINT64_POW2[25], 00917 1.0 / UINT64_POW2[24], 1.0 / UINT64_POW2[23], 1.0 / UINT64_POW2[22], 1.0 / UINT64_POW2[21], 00918 1.0 / UINT64_POW2[20], 1.0 / UINT64_POW2[19], 1.0 / UINT64_POW2[18], 1.0 / UINT64_POW2[17], 00919 1.0 / UINT64_POW2[16], 1.0 / UINT64_POW2[15], 1.0 / UINT64_POW2[14], 1.0 / UINT64_POW2[13], 00920 1.0 / UINT64_POW2[12], 1.0 / UINT64_POW2[11], 1.0 / UINT64_POW2[10], 1.0 / UINT64_POW2[9], 00921 1.0 / UINT64_POW2[8], 1.0 / UINT64_POW2[7], 1.0 / UINT64_POW2[6], 1.0 / UINT64_POW2[5], 00922 1.0 / UINT64_POW2[4], 1.0 / UINT64_POW2[3], 1.0 / UINT64_POW2[2], 1.0 / UINT64_POW2[1], 00923 1.0, 1.0*UINT64_POW2[1], 1.0*UINT64_POW2[2], 1.0*UINT64_POW2[3], 00924 1.0*UINT64_POW2[4], 1.0*UINT64_POW2[5], 1.0*UINT64_POW2[6], 1.0*UINT64_POW2[7], 00925 1.0*UINT64_POW2[8], 1.0*UINT64_POW2[9], 1.0*UINT64_POW2[10], 1.0*UINT64_POW2[11], 00926 1.0*UINT64_POW2[12], 1.0*UINT64_POW2[13], 1.0*UINT64_POW2[14], 1.0*UINT64_POW2[15], 00927 1.0*UINT64_POW2[16], 1.0*UINT64_POW2[17], 1.0*UINT64_POW2[18], 1.0*UINT64_POW2[19], 00928 1.0*UINT64_POW2[20], 1.0*UINT64_POW2[21], 1.0*UINT64_POW2[22], 1.0*UINT64_POW2[23], 00929 1.0*UINT64_POW2[24], 1.0*UINT64_POW2[25], 1.0*UINT64_POW2[26], 1.0*UINT64_POW2[27], 00930 1.0*UINT64_POW2[28], 1.0*UINT64_POW2[29], 1.0*UINT64_POW2[30], 1.0*UINT64_POW2[31], 00931 1.0*UINT64_POW2[32], 1.0*UINT64_POW2[33], 1.0*UINT64_POW2[34], 1.0*UINT64_POW2[35], 00932 1.0*UINT64_POW2[36], 1.0*UINT64_POW2[37], 1.0*UINT64_POW2[38], 1.0*UINT64_POW2[39], 00933 1.0*UINT64_POW2[40], 1.0*UINT64_POW2[41], 1.0*UINT64_POW2[42], 1.0*UINT64_POW2[43], 00934 1.0*UINT64_POW2[44], 1.0*UINT64_POW2[45], 1.0*UINT64_POW2[46], 1.0*UINT64_POW2[47], 00935 1.0*UINT64_POW2[48], 1.0*UINT64_POW2[49], 1.0*UINT64_POW2[50], 1.0*UINT64_POW2[51], 00936 1.0*UINT64_POW2[52], 1.0*UINT64_POW2[53], 1.0*UINT64_POW2[54], 1.0*UINT64_POW2[55], 00937 1.0*UINT64_POW2[56], 1.0*UINT64_POW2[57], 1.0*UINT64_POW2[58], 1.0*UINT64_POW2[59], 00938 1.0*UINT64_POW2[60], 1.0*UINT64_POW2[61], 1.0*UINT64_POW2[62], 1.0*UINT64_POW2[63] 00939 }; 00940 00942 enum e_mode { 00943 TC, 00944 US 00945 }; 00946 00948 enum o_mode { 00949 SAT, 00950 SAT_ZERO, 00951 SAT_SYM, 00952 WRAP, 00953 WRAP_SM 00954 }; 00955 00957 enum q_mode { 00958 RND, 00959 RND_ZERO, 00960 RND_MIN_INF, 00961 RND_INF, 00962 RND_CONV, 00963 RND_CONV_ODD, 00964 TRN, 00965 TRN_ZERO 00966 }; 00967 00969 enum output_mode { 00970 OUTPUT_FIX, 00971 OUTPUT_FIX_SHIFT, 00972 OUTPUT_FLOAT, 00973 OUTPUT_FLOAT_SHIFT 00974 }; 00975 00981 class Fix_Base 00982 { 00983 public: 00985 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) 00986 : shift(s), wordlen(w), emode(e), omode(o), qmode(q), stat_ptr(ptr) {init();} 00988 Fix_Base(const Fix_Base &x) 00989 : shift(x.shift), wordlen(MAX_WORDLEN), emode(TC), omode(WRAP), qmode(TRN), stat_ptr(0) {init();} 00991 virtual ~Fix_Base() {} 00992 00994 void set_shift(int s) {shift = s;} 00996 static void set_output_mode(output_mode o) {outputmode = o;} 00998 static void set_output_mode(std::string o); 00999 01001 int get_shift() const {return shift;} 01003 int get_wordlen() const {return wordlen;} 01005 e_mode get_e_mode() const {return emode;} 01007 o_mode get_o_mode() const {return omode;} 01009 q_mode get_q_mode() const {return qmode;} 01011 output_mode get_output_mode() const {return outputmode;} 01013 fixrep get_max() const {return max;} 01015 fixrep get_min() const {return min;} 01017 virtual void print() const; 01018 01019 protected: 01021 int shift; 01023 int wordlen; 01025 e_mode emode; 01027 o_mode omode; 01029 q_mode qmode; 01031 Stat *stat_ptr; 01033 fixrep min; 01035 fixrep max; 01037 int n_unused_bits; 01038 01040 void init(); 01042 fixrep apply_o_mode(fixrep x) const; 01044 fixrep scale_and_apply_modes(double x) const {return scale_and_apply_modes(x, qmode);} 01046 fixrep scale_and_apply_modes(double x, q_mode q) const; 01048 fixrep rshift_and_apply_q_mode(fixrep x, int n) const {return rshift_and_apply_q_mode(x, n, qmode);} 01050 fixrep rshift_and_apply_q_mode(fixrep x, int n, q_mode q) const; 01051 01052 private: 01054 static output_mode outputmode; 01055 }; 01056 01058 inline std::ostream &operator<<(std::ostream &os, const output_mode &o) 01059 { 01060 Fix_Base::set_output_mode(o); 01061 return os; 01062 } 01063 01065 01066 } // namespace itpp 01067 01068 #endif // #ifndef FIX_BASE_H
Generated on Wed Dec 7 2011 03:38:48 for IT++ by Doxygen 1.7.4