/src/FreeImage/Source/OpenEXR/Half/halfFunction.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 159 lines · 51 code · 21 blank · 87 comment · 7 complexity · ca1486917528b45dbc7c3fa0c2d1ace0 MD5 · raw file

  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 <float.h>
  82. #include "half.h"
  83. template <class T>
  84. class halfFunction
  85. {
  86. public:
  87. //------------
  88. // Constructor
  89. //------------
  90. template <class Function>
  91. halfFunction (Function f,
  92. half domainMin = -HALF_MAX,
  93. half domainMax = HALF_MAX,
  94. T defaultValue = 0,
  95. T posInfValue = 0,
  96. T negInfValue = 0,
  97. T nanValue = 0);
  98. //-----------
  99. // Evaluation
  100. //-----------
  101. T operator () (half x) const;
  102. private:
  103. T _lut[1 << 16];
  104. };
  105. //---------------
  106. // Implementation
  107. //---------------
  108. template <class T>
  109. template <class Function>
  110. halfFunction<T>::halfFunction (Function f,
  111. half domainMin,
  112. half domainMax,
  113. T defaultValue,
  114. T posInfValue,
  115. T negInfValue,
  116. T nanValue)
  117. {
  118. for (int i = 0; i < (1 << 16); i++)
  119. {
  120. half x;
  121. x.setBits (i);
  122. if (x.isNan())
  123. _lut[i] = nanValue;
  124. else if (x.isInfinity())
  125. _lut[i] = x.isNegative()? negInfValue: posInfValue;
  126. else if (x < domainMin || x > domainMax)
  127. _lut[i] = defaultValue;
  128. else
  129. _lut[i] = f (x);
  130. }
  131. }
  132. template <class T>
  133. inline T
  134. halfFunction<T>::operator () (half x) const
  135. {
  136. return _lut[x.bits()];
  137. }
  138. #endif