/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas // Digital Ltd. LLC // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Industrial Light & Magic nor the names of // its contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////// // Primary authors: // Florian Kainz // Rod Bogart //--------------------------------------------------------------------------- // // s10e5Function -- a class for fast evaluation // of s10e5 --> T functions // // The constructor for a s10e5Function object, // // s10e5Function (function, // domainMin, domainMax, // defaultValue, // posInfValue, negInfValue, // nanValue); // // evaluates the function for all finite s10e5 values in the interval // [domainMin, domainMax], and stores the results in a lookup table. // For finite s10e5 values that are not in [domainMin, domainMax], the // constructor stores defaultValue in the table. For positive infinity, // negative infinity and NANs, posInfValue, negInfValue and nanValue // are stored in the table. // // The tabulated function can then be evaluated quickly for arbitrary // s10e5 values by calling the the s10e5Function object's operator() // method. // // Example: // // #include // #include // // s10e5Function hsin (sin); // // s10e5Function hsqrt (sqrt, // function // 0, S10E5_MAX, // domain // s10e5::qNan(), // sqrt(x) for x < 0 // s10e5::posInf(), // sqrt(+inf) // s10e5::qNan(), // sqrt(-inf) // s10e5::qNan()); // sqrt(nan) // // s10e5 x = hsin (1); // s10e5 y = hsqrt (3.5); // //--------------------------------------------------------------------------- #ifndef _S10E5_FUNCTION_H_ #define _S10E5_FUNCTION_H_ #include #include template class s10e5Function { public: //------------ // Constructor //------------ template s10e5Function (Function f, s10e5 domainMin = -S10E5_MAX, s10e5 domainMax = S10E5_MAX, T defaultValue = 0, T posInfValue = 0, T negInfValue = 0, T nanValue = 0); //----------- // Evaluation //----------- T operator () (s10e5 x) const; private: T _lut[1 << 16]; }; //--------------- // Implementation //--------------- template template s10e5Function::s10e5Function (Function f, s10e5 domainMin, s10e5 domainMax, T defaultValue, T posInfValue, T negInfValue, T nanValue) { for (int i = 0; i < (1 << 16); i++) { s10e5 x; x.setBits (i); if (x.isNan()) _lut[i] = nanValue; else if (x.isInfinity()) _lut[i] = x.isNegative()? negInfValue: posInfValue; else if (x < domainMin || x > domainMax) _lut[i] = defaultValue; else _lut[i] = f (x); } } template inline T s10e5Function::operator () (s10e5 x) const { return _lut[x.bits()]; } #endif