Path: blob/master/3rdparty/openexr/Half/halfFunction.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////3334// Primary authors:35// Florian Kainz <[email protected]>36// Rod Bogart <[email protected]>373839//---------------------------------------------------------------------------40//41// halfFunction<T> -- a class for fast evaluation42// of half --> T functions43//44// The constructor for a halfFunction object,45//46// halfFunction (function,47// domainMin, domainMax,48// defaultValue,49// posInfValue, negInfValue,50// nanValue);51//52// evaluates the function for all finite half values in the interval53// [domainMin, domainMax], and stores the results in a lookup table.54// For finite half values that are not in [domainMin, domainMax], the55// constructor stores defaultValue in the table. For positive infinity,56// negative infinity and NANs, posInfValue, negInfValue and nanValue57// are stored in the table.58//59// The tabulated function can then be evaluated quickly for arbitrary60// half values by calling the the halfFunction object's operator()61// method.62//63// Example:64//65// #include <math.h>66// #include <halfFunction.h>67//68// halfFunction<half> hsin (sin);69//70// halfFunction<half> hsqrt (sqrt, // function71// 0, HALF_MAX, // domain72// half::qNan(), // sqrt(x) for x < 073// half::posInf(), // sqrt(+inf)74// half::qNan(), // sqrt(-inf)75// half::qNan()); // sqrt(nan)76//77// half x = hsin (1);78// half y = hsqrt (3.5);79//80//---------------------------------------------------------------------------8182#ifndef _HALF_FUNCTION_H_83#define _HALF_FUNCTION_H_8485#include "half.h"8687#include <IlmBaseConfig.h>88#ifndef ILMBASE_HAVE_LARGE_STACK89#include <string.h> // need this for memset90#else91#endif9293#include <float.h>949596template <class T>97class halfFunction98{99public:100101//------------102// Constructor103//------------104105template <class Function>106halfFunction (Function f,107half domainMin = -HALF_MAX,108half domainMax = HALF_MAX,109T defaultValue = 0,110T posInfValue = 0,111T negInfValue = 0,112T nanValue = 0);113114#ifndef ILMBASE_HAVE_LARGE_STACK115~halfFunction () { delete [] _lut; }116#endif117118//-----------119// Evaluation120//-----------121122T operator () (half x) const;123124private:125#ifdef ILMBASE_HAVE_LARGE_STACK126T _lut[1 << 16];127#else128T * _lut;129#endif130};131132133//---------------134// Implementation135//---------------136137template <class T>138template <class Function>139halfFunction<T>::halfFunction (Function f,140half domainMin,141half domainMax,142T defaultValue,143T posInfValue,144T negInfValue,145T nanValue)146{147#ifndef ILMBASE_HAVE_LARGE_STACK148_lut = new T[1<<16];149memset (_lut, 0 , (1<<16) * sizeof(T));150#endif151152for (int i = 0; i < (1 << 16); i++)153{154half x;155x.setBits (i);156157if (x.isNan())158_lut[i] = nanValue;159else if (x.isInfinity())160_lut[i] = x.isNegative()? negInfValue: posInfValue;161else if (x < domainMin || x > domainMax)162_lut[i] = defaultValue;163else164_lut[i] = f (x);165}166}167168169template <class T>170inline T171halfFunction<T>::operator () (half x) const172{173return _lut[x.bits()];174}175176177#endif178179180