00001 //---------------------------------------------------------------------------- 00002 /** @file SgFastLog.cpp */ 00003 //---------------------------------------------------------------------------- 00004 00005 #include "SgSystem.h" 00006 #include "SgFastLog.h" 00007 00008 #include <limits> 00009 #include <cmath> 00010 #include <boost/static_assert.hpp> 00011 00012 using namespace std; 00013 00014 //---------------------------------------------------------------------------- 00015 00016 namespace { 00017 00018 // The fast log algorithm requires 4 byte integers 00019 BOOST_STATIC_ASSERT(sizeof(int) == 4); 00020 00021 // The fast log algorithm requires that floats use IEEE 754 format 00022 BOOST_STATIC_ASSERT(numeric_limits<float>::is_iec559); 00023 00024 } // namespace 00025 00026 //---------------------------------------------------------------------------- 00027 00028 SgFastLog::SgFastLog(int mantissaBits) 00029 : m_mantissaBitsDiff(MAX_MANTISSA_BITS - mantissaBits) 00030 { 00031 m_lookupTable = new float[1 << mantissaBits]; 00032 IntFloat x; 00033 x.m_int = 0x3F800000; 00034 int incr = (1 << m_mantissaBitsDiff); 00035 int p = static_cast<int>(pow(2.0f, mantissaBits)); 00036 float invLogTwo = 1.f / log(2.f); 00037 for (int i = 0; i < p; ++i) 00038 { 00039 m_lookupTable[i] = log(x.m_float) * invLogTwo; 00040 x.m_int += incr; 00041 } 00042 } 00043 00044 SgFastLog::~SgFastLog() 00045 { 00046 delete[] m_lookupTable; 00047 } 00048 00049 //----------------------------------------------------------------------------