halfFunction.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. // Primary authors:
  35. // Florian Kainz <kainz@ilm.com>
  36. // Rod Bogart <rgb@ilm.com>
  37. //---------------------------------------------------------------------------
  38. //
  39. // halfFunction<T> -- a class for fast evaluation
  40. // of half --> T functions
  41. //
  42. // The constructor for a halfFunction object,
  43. //
  44. // halfFunction (function,
  45. // domainMin, domainMax,
  46. // defaultValue,
  47. // posInfValue, negInfValue,
  48. // nanValue);
  49. //
  50. // evaluates the function for all finite half values in the interval
  51. // [domainMin, domainMax], and stores the results in a lookup table.
  52. // For finite half values that are not in [domainMin, domainMax], the
  53. // constructor stores defaultValue in the table. For positive infinity,
  54. // negative infinity and NANs, posInfValue, negInfValue and nanValue
  55. // are stored in the table.
  56. //
  57. // The tabulated function can then be evaluated quickly for arbitrary
  58. // half values by calling the the halfFunction object's operator()
  59. // method.
  60. //
  61. // Example:
  62. //
  63. // #include <math.h>
  64. // #include <halfFunction.h>
  65. //
  66. // halfFunction<half> hsin (sin);
  67. //
  68. // halfFunction<half> hsqrt (sqrt, // function
  69. // 0, HALF_MAX, // domain
  70. // half::qNan(), // sqrt(x) for x < 0
  71. // half::posInf(), // sqrt(+inf)
  72. // half::qNan(), // sqrt(-inf)
  73. // half::qNan()); // sqrt(nan)
  74. //
  75. // half x = hsin (1);
  76. // half y = hsqrt (3.5);
  77. //
  78. //---------------------------------------------------------------------------
  79. #ifndef _HALF_FUNCTION_H_
  80. #define _HALF_FUNCTION_H_
  81. #include "half.h"
  82. #include "IlmBaseConfig.h"
  83. #ifndef ILMBASE_HAVE_LARGE_STACK
  84. #include <string.h> // need this for memset
  85. #else
  86. #endif
  87. #include <float.h>
  88. template <class T>
  89. class halfFunction
  90. {
  91. public:
  92. //------------
  93. // Constructor
  94. //------------
  95. template <class Function>
  96. halfFunction (Function f,
  97. half domainMin = -HALF_MAX,
  98. half domainMax = HALF_MAX,
  99. T defaultValue = 0,
  100. T posInfValue = 0,
  101. T negInfValue = 0,
  102. T nanValue = 0);
  103. #ifndef ILMBASE_HAVE_LARGE_STACK
  104. ~halfFunction () { delete [] _lut; }
  105. #endif
  106. //-----------
  107. // Evaluation
  108. //-----------
  109. T operator () (half x) const;
  110. private:
  111. #ifdef ILMBASE_HAVE_LARGE_STACK
  112. T _lut[1 << 16];
  113. #else
  114. T * _lut;
  115. #endif
  116. };
  117. //---------------
  118. // Implementation
  119. //---------------
  120. template <class T>
  121. template <class Function>
  122. halfFunction<T>::halfFunction (Function f,
  123. half domainMin,
  124. half domainMax,
  125. T defaultValue,
  126. T posInfValue,
  127. T negInfValue,
  128. T nanValue)
  129. {
  130. #ifndef ILMBASE_HAVE_LARGE_STACK
  131. _lut = new T[1<<16];
  132. memset (_lut, 0 , (1<<16) * sizeof(T));
  133. #endif
  134. for (int i = 0; i < (1 << 16); i++)
  135. {
  136. half x;
  137. x.setBits (i);
  138. if (x.isNan())
  139. _lut[i] = nanValue;
  140. else if (x.isInfinity())
  141. _lut[i] = x.isNegative()? negInfValue: posInfValue;
  142. else if (x < domainMin || x > domainMax)
  143. _lut[i] = defaultValue;
  144. else
  145. _lut[i] = f (x);
  146. }
  147. }
  148. template <class T>
  149. inline T
  150. halfFunction<T>::operator () (half x) const
  151. {
  152. return _lut[x.bits()];
  153. }
  154. #endif