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