00001 //---------------------------------------------------------------------------- 00002 /** @file SgFastLog.h 00003 Fast logarithm. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #ifndef SG_FASTLOG_H 00008 #define SG_FASTLOG_H 00009 00010 //---------------------------------------------------------------------------- 00011 00012 /** Fast logarithm. 00013 Computes a fast single precision logarithm based on a lookup table. 00014 00015 O. Vinyals, G. Friedland, N. Mirghafori: Revisiting a basic function on 00016 current CPUs: A fast logarithm implementation with adjustable accuracy. 00017 http://www.icsi.berkeley.edu/pubs/techreports/TR-07-002.pdf 00018 00019 The implementation assumes that the platform uses IEEE 754 floats. 00020 */ 00021 class SgFastLog 00022 { 00023 public: 00024 SgFastLog(int mantissaBits); 00025 00026 ~SgFastLog(); 00027 00028 /** Get natural logarithm. */ 00029 float Log(float val) const; 00030 00031 private: 00032 union IntFloat 00033 { 00034 int m_int; 00035 00036 float m_float; 00037 }; 00038 00039 static const int MAX_MANTISSA_BITS = 23; 00040 00041 const int m_mantissaBitsDiff; 00042 00043 float* m_lookupTable; 00044 00045 /** Not implemented. */ 00046 SgFastLog(const SgFastLog&); 00047 00048 /** Not implemented. */ 00049 SgFastLog& operator=(const SgFastLog&); 00050 }; 00051 00052 inline float SgFastLog::Log(float val) const 00053 { 00054 IntFloat x; 00055 x.m_float = val; 00056 int logTwo = ((x.m_int >> MAX_MANTISSA_BITS) & 255) - 127; 00057 x.m_int &= 0x7FFFFF; 00058 x.m_int >>= m_mantissaBitsDiff; 00059 return ((m_lookupTable[x.m_int] + logTwo) * 0.69314718f); 00060 } 00061 00062 //---------------------------------------------------------------------------- 00063 00064 #endif // SG_FASTLOG_H