IT++ Logo

fix_base.h

Go to the documentation of this file.
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 
00880 
00881 
00883   typedef int64_t fixrep;
00885   const int MAX_WORDLEN = 64;
00886 
00888   const uint64_t UINT64_POW2[64] = {
00889     uint64_t(1),     uint64_t(1)<<1,  uint64_t(1)<<2,  uint64_t(1)<<3,  uint64_t(1)<<4,
00890     uint64_t(1)<<5,  uint64_t(1)<<6,  uint64_t(1)<<7,  uint64_t(1)<<8,  uint64_t(1)<<9,
00891     uint64_t(1)<<10, uint64_t(1)<<11, uint64_t(1)<<12, uint64_t(1)<<13, uint64_t(1)<<14,
00892     uint64_t(1)<<15, uint64_t(1)<<16, uint64_t(1)<<17, uint64_t(1)<<18, uint64_t(1)<<19,
00893     uint64_t(1)<<20, uint64_t(1)<<21, uint64_t(1)<<22, uint64_t(1)<<23, uint64_t(1)<<24,
00894     uint64_t(1)<<25, uint64_t(1)<<26, uint64_t(1)<<27, uint64_t(1)<<28, uint64_t(1)<<29,
00895     uint64_t(1)<<30, uint64_t(1)<<31, uint64_t(1)<<32, uint64_t(1)<<33, uint64_t(1)<<34,
00896     uint64_t(1)<<35, uint64_t(1)<<36, uint64_t(1)<<37, uint64_t(1)<<38, uint64_t(1)<<39,
00897     uint64_t(1)<<40, uint64_t(1)<<41, uint64_t(1)<<42, uint64_t(1)<<43, uint64_t(1)<<44,
00898     uint64_t(1)<<45, uint64_t(1)<<46, uint64_t(1)<<47, uint64_t(1)<<48, uint64_t(1)<<49,
00899     uint64_t(1)<<50, uint64_t(1)<<51, uint64_t(1)<<52, uint64_t(1)<<53, uint64_t(1)<<54,
00900     uint64_t(1)<<55, uint64_t(1)<<56, uint64_t(1)<<57, uint64_t(1)<<58, uint64_t(1)<<59,
00901     uint64_t(1)<<60, uint64_t(1)<<61, uint64_t(1)<<62, uint64_t(1)<<63
00902   };
00903 
00905   const double DOUBLE_POW2[128] = {
00906     0.5/UINT64_POW2[63], 1.0/UINT64_POW2[63], 1.0/UINT64_POW2[62], 1.0/UINT64_POW2[61],
00907     1.0/UINT64_POW2[60], 1.0/UINT64_POW2[59], 1.0/UINT64_POW2[58], 1.0/UINT64_POW2[57],
00908     1.0/UINT64_POW2[56], 1.0/UINT64_POW2[55], 1.0/UINT64_POW2[54], 1.0/UINT64_POW2[53],
00909     1.0/UINT64_POW2[52], 1.0/UINT64_POW2[51], 1.0/UINT64_POW2[50], 1.0/UINT64_POW2[49],
00910     1.0/UINT64_POW2[48], 1.0/UINT64_POW2[47], 1.0/UINT64_POW2[46], 1.0/UINT64_POW2[45],
00911     1.0/UINT64_POW2[44], 1.0/UINT64_POW2[43], 1.0/UINT64_POW2[42], 1.0/UINT64_POW2[41],
00912     1.0/UINT64_POW2[40], 1.0/UINT64_POW2[39], 1.0/UINT64_POW2[38], 1.0/UINT64_POW2[37],
00913     1.0/UINT64_POW2[36], 1.0/UINT64_POW2[35], 1.0/UINT64_POW2[34], 1.0/UINT64_POW2[33],
00914     1.0/UINT64_POW2[32], 1.0/UINT64_POW2[31], 1.0/UINT64_POW2[30], 1.0/UINT64_POW2[29],
00915     1.0/UINT64_POW2[28], 1.0/UINT64_POW2[27], 1.0/UINT64_POW2[26], 1.0/UINT64_POW2[25],
00916     1.0/UINT64_POW2[24], 1.0/UINT64_POW2[23], 1.0/UINT64_POW2[22], 1.0/UINT64_POW2[21],
00917     1.0/UINT64_POW2[20], 1.0/UINT64_POW2[19], 1.0/UINT64_POW2[18], 1.0/UINT64_POW2[17],
00918     1.0/UINT64_POW2[16], 1.0/UINT64_POW2[15], 1.0/UINT64_POW2[14], 1.0/UINT64_POW2[13],
00919     1.0/UINT64_POW2[12], 1.0/UINT64_POW2[11], 1.0/UINT64_POW2[10], 1.0/UINT64_POW2[9],
00920     1.0/UINT64_POW2[8],  1.0/UINT64_POW2[7],  1.0/UINT64_POW2[6],  1.0/UINT64_POW2[5],
00921     1.0/UINT64_POW2[4],  1.0/UINT64_POW2[3],  1.0/UINT64_POW2[2],  1.0/UINT64_POW2[1],
00922     1.0,                 1.0*UINT64_POW2[1],  1.0*UINT64_POW2[2],  1.0*UINT64_POW2[3],
00923     1.0*UINT64_POW2[4],  1.0*UINT64_POW2[5],  1.0*UINT64_POW2[6],  1.0*UINT64_POW2[7],
00924     1.0*UINT64_POW2[8],  1.0*UINT64_POW2[9],  1.0*UINT64_POW2[10], 1.0*UINT64_POW2[11],
00925     1.0*UINT64_POW2[12], 1.0*UINT64_POW2[13], 1.0*UINT64_POW2[14], 1.0*UINT64_POW2[15],
00926     1.0*UINT64_POW2[16], 1.0*UINT64_POW2[17], 1.0*UINT64_POW2[18], 1.0*UINT64_POW2[19],
00927     1.0*UINT64_POW2[20], 1.0*UINT64_POW2[21], 1.0*UINT64_POW2[22], 1.0*UINT64_POW2[23],
00928     1.0*UINT64_POW2[24], 1.0*UINT64_POW2[25], 1.0*UINT64_POW2[26], 1.0*UINT64_POW2[27],
00929     1.0*UINT64_POW2[28], 1.0*UINT64_POW2[29], 1.0*UINT64_POW2[30], 1.0*UINT64_POW2[31],
00930     1.0*UINT64_POW2[32], 1.0*UINT64_POW2[33], 1.0*UINT64_POW2[34], 1.0*UINT64_POW2[35],
00931     1.0*UINT64_POW2[36], 1.0*UINT64_POW2[37], 1.0*UINT64_POW2[38], 1.0*UINT64_POW2[39],
00932     1.0*UINT64_POW2[40], 1.0*UINT64_POW2[41], 1.0*UINT64_POW2[42], 1.0*UINT64_POW2[43],
00933     1.0*UINT64_POW2[44], 1.0*UINT64_POW2[45], 1.0*UINT64_POW2[46], 1.0*UINT64_POW2[47],
00934     1.0*UINT64_POW2[48], 1.0*UINT64_POW2[49], 1.0*UINT64_POW2[50], 1.0*UINT64_POW2[51],
00935     1.0*UINT64_POW2[52], 1.0*UINT64_POW2[53], 1.0*UINT64_POW2[54], 1.0*UINT64_POW2[55],
00936     1.0*UINT64_POW2[56], 1.0*UINT64_POW2[57], 1.0*UINT64_POW2[58], 1.0*UINT64_POW2[59],
00937     1.0*UINT64_POW2[60], 1.0*UINT64_POW2[61], 1.0*UINT64_POW2[62], 1.0*UINT64_POW2[63]
00938   };
00939 
00941   enum e_mode {
00942     TC,                 
00943     US                  
00944   };
00945 
00947   enum o_mode {
00948     SAT,                
00949     SAT_ZERO,           
00950     SAT_SYM,            
00951     WRAP,               
00952     WRAP_SM             
00953   };
00954 
00956   enum q_mode {
00957     RND,                
00958     RND_ZERO,           
00959     RND_MIN_INF,        
00960     RND_INF,            
00961     RND_CONV,           
00962     RND_CONV_ODD,       
00963     TRN,                
00964     TRN_ZERO            
00965   };
00966 
00968   enum output_mode {
00969     OUTPUT_FIX,         
00970     OUTPUT_FIX_SHIFT,   
00971     OUTPUT_FLOAT,       
00972     OUTPUT_FLOAT_SHIFT  
00973   };
00974 
00980   class Fix_Base {
00981   public:
00983     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)
00984       : shift(s), wordlen(w), emode(e), omode(o), qmode(q), stat_ptr(ptr) {init();}
00986     Fix_Base(const Fix_Base &x)
00987       : shift(x.shift), wordlen(MAX_WORDLEN), emode(TC), omode(WRAP), qmode(TRN), stat_ptr(0) {init();}
00989     virtual ~Fix_Base() {}
00990 
00992     void set_shift(int s) {shift = s;}
00994     static void set_output_mode(output_mode o) {outputmode = o;}
00996     static void set_output_mode(std::string o);
00997 
00999     int get_shift() const {return shift;}
01001     int get_wordlen() const {return wordlen;}
01003     e_mode get_e_mode() const {return emode;}
01005     o_mode get_o_mode() const {return omode;}
01007     q_mode get_q_mode() const {return qmode;}
01009     output_mode get_output_mode() const {return outputmode;}
01011     fixrep get_max() const {return max;}
01013     fixrep get_min() const {return min;}
01015     virtual void print() const;
01016 
01017   protected:
01019     int shift;
01021     int wordlen;
01023     e_mode emode;
01025     o_mode omode;
01027     q_mode qmode;
01029     Stat *stat_ptr;
01031     fixrep min;
01033     fixrep max;
01035     int n_unused_bits;
01036 
01038     void init();
01040     fixrep apply_o_mode(fixrep x) const;
01042     fixrep scale_and_apply_modes(double x) const {return scale_and_apply_modes(x, qmode);}
01044     fixrep scale_and_apply_modes(double x, q_mode q) const;
01046     fixrep rshift_and_apply_q_mode(fixrep x, int n) const {return rshift_and_apply_q_mode(x, n, qmode);}
01048     fixrep rshift_and_apply_q_mode(fixrep x, int n, q_mode q) const;
01049 
01050   private:
01052     static output_mode outputmode;
01053   };
01054 
01056   inline std::ostream &operator<<(std::ostream &os, const output_mode &o)
01057   {
01058     Fix_Base::set_output_mode(o);
01059     return os;
01060   }
01061 
01063 
01064 } // namespace itpp
01065 
01066 #endif // #ifndef FIX_BASE_H
SourceForge Logo

Generated on Sat Apr 19 10:59:23 2008 for IT++ by Doxygen 1.5.5