IT++ Logo Newcom Logo

fix_base.h

Go to the documentation of this file.
00001 
00033 #ifndef FIX_BASE_H
00034 #define FIX_BASE_H
00035 
00036 #include <itpp/base/stat.h>
00037 
00038 
00039 namespace itpp {
00040 
00881 
00882 
00883 #ifdef _MSC_VER
00885   typedef __int64 int64;
00887   typedef unsigned __int64 uint64;
00888 #else
00890   typedef long long int64;
00892   typedef unsigned long long uint64;
00893 #endif // _MSC_VER ifdef
00894 
00896   typedef int64 fixrep;
00898   const int MAX_WORDLEN = 64;
00899 
00901   const uint64 UINT64_POW2[64] = {
00902     uint64(1),     uint64(1)<<1,  uint64(1)<<2,  uint64(1)<<3,  uint64(1)<<4,
00903     uint64(1)<<5,  uint64(1)<<6,  uint64(1)<<7,  uint64(1)<<8,  uint64(1)<<9,
00904     uint64(1)<<10, uint64(1)<<11, uint64(1)<<12, uint64(1)<<13, uint64(1)<<14,
00905     uint64(1)<<15, uint64(1)<<16, uint64(1)<<17, uint64(1)<<18, uint64(1)<<19,
00906     uint64(1)<<20, uint64(1)<<21, uint64(1)<<22, uint64(1)<<23, uint64(1)<<24,
00907     uint64(1)<<25, uint64(1)<<26, uint64(1)<<27, uint64(1)<<28, uint64(1)<<29,
00908     uint64(1)<<30, uint64(1)<<31, uint64(1)<<32, uint64(1)<<33, uint64(1)<<34,
00909     uint64(1)<<35, uint64(1)<<36, uint64(1)<<37, uint64(1)<<38, uint64(1)<<39,
00910     uint64(1)<<40, uint64(1)<<41, uint64(1)<<42, uint64(1)<<43, uint64(1)<<44,
00911     uint64(1)<<45, uint64(1)<<46, uint64(1)<<47, uint64(1)<<48, uint64(1)<<49,
00912     uint64(1)<<50, uint64(1)<<51, uint64(1)<<52, uint64(1)<<53, uint64(1)<<54,
00913     uint64(1)<<55, uint64(1)<<56, uint64(1)<<57, uint64(1)<<58, uint64(1)<<59,
00914     uint64(1)<<60, uint64(1)<<61, uint64(1)<<62, uint64(1)<<63
00915   };
00916 
00918   const double DOUBLE_POW2[128] = {
00919     0.5/UINT64_POW2[63], 1.0/UINT64_POW2[63], 1.0/UINT64_POW2[62], 1.0/UINT64_POW2[61],
00920     1.0/UINT64_POW2[60], 1.0/UINT64_POW2[59], 1.0/UINT64_POW2[58], 1.0/UINT64_POW2[57],
00921     1.0/UINT64_POW2[56], 1.0/UINT64_POW2[55], 1.0/UINT64_POW2[54], 1.0/UINT64_POW2[53],
00922     1.0/UINT64_POW2[52], 1.0/UINT64_POW2[51], 1.0/UINT64_POW2[50], 1.0/UINT64_POW2[49],
00923     1.0/UINT64_POW2[48], 1.0/UINT64_POW2[47], 1.0/UINT64_POW2[46], 1.0/UINT64_POW2[45],
00924     1.0/UINT64_POW2[44], 1.0/UINT64_POW2[43], 1.0/UINT64_POW2[42], 1.0/UINT64_POW2[41],
00925     1.0/UINT64_POW2[40], 1.0/UINT64_POW2[39], 1.0/UINT64_POW2[38], 1.0/UINT64_POW2[37],
00926     1.0/UINT64_POW2[36], 1.0/UINT64_POW2[35], 1.0/UINT64_POW2[34], 1.0/UINT64_POW2[33],
00927     1.0/UINT64_POW2[32], 1.0/UINT64_POW2[31], 1.0/UINT64_POW2[30], 1.0/UINT64_POW2[29],
00928     1.0/UINT64_POW2[28], 1.0/UINT64_POW2[27], 1.0/UINT64_POW2[26], 1.0/UINT64_POW2[25],
00929     1.0/UINT64_POW2[24], 1.0/UINT64_POW2[23], 1.0/UINT64_POW2[22], 1.0/UINT64_POW2[21],
00930     1.0/UINT64_POW2[20], 1.0/UINT64_POW2[19], 1.0/UINT64_POW2[18], 1.0/UINT64_POW2[17],
00931     1.0/UINT64_POW2[16], 1.0/UINT64_POW2[15], 1.0/UINT64_POW2[14], 1.0/UINT64_POW2[13],
00932     1.0/UINT64_POW2[12], 1.0/UINT64_POW2[11], 1.0/UINT64_POW2[10], 1.0/UINT64_POW2[9],
00933     1.0/UINT64_POW2[8],  1.0/UINT64_POW2[7],  1.0/UINT64_POW2[6],  1.0/UINT64_POW2[5],
00934     1.0/UINT64_POW2[4],  1.0/UINT64_POW2[3],  1.0/UINT64_POW2[2],  1.0/UINT64_POW2[1],
00935     1.0,                 1.0*UINT64_POW2[1],  1.0*UINT64_POW2[2],  1.0*UINT64_POW2[3],
00936     1.0*UINT64_POW2[4],  1.0*UINT64_POW2[5],  1.0*UINT64_POW2[6],  1.0*UINT64_POW2[7],
00937     1.0*UINT64_POW2[8],  1.0*UINT64_POW2[9],  1.0*UINT64_POW2[10], 1.0*UINT64_POW2[11],
00938     1.0*UINT64_POW2[12], 1.0*UINT64_POW2[13], 1.0*UINT64_POW2[14], 1.0*UINT64_POW2[15],
00939     1.0*UINT64_POW2[16], 1.0*UINT64_POW2[17], 1.0*UINT64_POW2[18], 1.0*UINT64_POW2[19],
00940     1.0*UINT64_POW2[20], 1.0*UINT64_POW2[21], 1.0*UINT64_POW2[22], 1.0*UINT64_POW2[23],
00941     1.0*UINT64_POW2[24], 1.0*UINT64_POW2[25], 1.0*UINT64_POW2[26], 1.0*UINT64_POW2[27],
00942     1.0*UINT64_POW2[28], 1.0*UINT64_POW2[29], 1.0*UINT64_POW2[30], 1.0*UINT64_POW2[31],
00943     1.0*UINT64_POW2[32], 1.0*UINT64_POW2[33], 1.0*UINT64_POW2[34], 1.0*UINT64_POW2[35],
00944     1.0*UINT64_POW2[36], 1.0*UINT64_POW2[37], 1.0*UINT64_POW2[38], 1.0*UINT64_POW2[39],
00945     1.0*UINT64_POW2[40], 1.0*UINT64_POW2[41], 1.0*UINT64_POW2[42], 1.0*UINT64_POW2[43],
00946     1.0*UINT64_POW2[44], 1.0*UINT64_POW2[45], 1.0*UINT64_POW2[46], 1.0*UINT64_POW2[47],
00947     1.0*UINT64_POW2[48], 1.0*UINT64_POW2[49], 1.0*UINT64_POW2[50], 1.0*UINT64_POW2[51],
00948     1.0*UINT64_POW2[52], 1.0*UINT64_POW2[53], 1.0*UINT64_POW2[54], 1.0*UINT64_POW2[55],
00949     1.0*UINT64_POW2[56], 1.0*UINT64_POW2[57], 1.0*UINT64_POW2[58], 1.0*UINT64_POW2[59],
00950     1.0*UINT64_POW2[60], 1.0*UINT64_POW2[61], 1.0*UINT64_POW2[62], 1.0*UINT64_POW2[63]
00951   };
00952 
00954   enum e_mode {
00955     TC,                 
00956     US                  
00957   };
00958 
00960   enum o_mode {
00961     SAT,                
00962     SAT_ZERO,           
00963     SAT_SYM,            
00964     WRAP,               
00965     WRAP_SM             
00966   };
00967 
00969   enum q_mode {
00970     RND,                
00971     RND_ZERO,           
00972     RND_MIN_INF,        
00973     RND_INF,            
00974     RND_CONV,           
00975     RND_CONV_ODD,       
00976     TRN,                
00977     TRN_ZERO            
00978   };
00979 
00981   enum output_mode {
00982     OUTPUT_FIX,         
00983     OUTPUT_FIX_SHIFT,   
00984     OUTPUT_FLOAT,       
00985     OUTPUT_FLOAT_SHIFT  
00986   };
00987 
00993   class Fix_Base {
00994   public:
00996     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)
00997       : shift(s), wordlen(w), emode(e), omode(o), qmode(q), stat_ptr(ptr) {init();}
00999     Fix_Base(const Fix_Base &x)
01000       : shift(x.shift), wordlen(MAX_WORDLEN), emode(TC), omode(WRAP), qmode(TRN), stat_ptr(0) {init();}
01002     virtual ~Fix_Base() {}
01003 
01005     void set_shift(int s) {shift = s;}
01007     static void set_output_mode(output_mode o) {outputmode = o;}
01009     static void set_output_mode(std::string o);
01010 
01012     int get_shift() const {return shift;}
01014     int get_wordlen() const {return wordlen;}
01016     e_mode get_e_mode() const {return emode;}
01018     o_mode get_o_mode() const {return omode;}
01020     q_mode get_q_mode() const {return qmode;}
01022     output_mode get_output_mode() const {return outputmode;}
01024     fixrep get_max() const {return max;}
01026     fixrep get_min() const {return min;}
01028     virtual void print() const;
01029 
01030   protected:
01032     int shift;
01034     int wordlen;
01036     e_mode emode;
01038     o_mode omode;
01040     q_mode qmode;
01042     Stat *stat_ptr;
01044     fixrep min;
01046     fixrep max;
01048     int n_unused_bits;
01049 
01051     void init();
01053     fixrep apply_o_mode(fixrep x) const;
01055     fixrep scale_and_apply_modes(double x) const {return scale_and_apply_modes(x, qmode);}
01057     fixrep scale_and_apply_modes(double x, q_mode q) const;
01059     fixrep rshift_and_apply_q_mode(fixrep x, int n) const {return rshift_and_apply_q_mode(x, n, qmode);}
01061     fixrep rshift_and_apply_q_mode(fixrep x, int n, q_mode q) const;
01062 
01063   private:
01065     static output_mode outputmode;
01066   };
01067 
01069   inline std::ostream &operator<<(std::ostream &os, const output_mode &o)
01070   {
01071     Fix_Base::set_output_mode(o);
01072     return os;
01073   }
01074 
01076 
01077 } // namespace itpp
01078 
01079 #endif // #ifndef FIX_BASE_H
SourceForge Logo

Generated on Wed Apr 18 11:23:33 2007 for IT++ by Doxygen 1.5.2