HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
halfFunction.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 //---------------------------------------------------------------------------
7 //
8 // halfFunction<T> -- a class for fast evaluation
9 // of half --> T functions
10 //
11 // The constructor for a halfFunction object,
12 //
13 // halfFunction (function,
14 // domainMin, domainMax,
15 // defaultValue,
16 // posInfValue, negInfValue,
17 // nanValue);
18 //
19 // evaluates the function for all finite half values in the interval
20 // [domainMin, domainMax], and stores the results in a lookup table.
21 // For finite half values that are not in [domainMin, domainMax], the
22 // constructor stores defaultValue in the table. For positive infinity,
23 // negative infinity and NANs, posInfValue, negInfValue and nanValue
24 // are stored in the table.
25 //
26 // The tabulated function can then be evaluated quickly for arbitrary
27 // half values by calling the the halfFunction object's operator()
28 // method.
29 //
30 // Example:
31 //
32 // #include <math.h>
33 // #include <halfFunction.h>
34 //
35 // halfFunction<half> hsin (sin);
36 //
37 // halfFunction<half> hsqrt (sqrt, // function
38 // 0, HALF_MAX, // domain
39 // half::qNan(), // sqrt(x) for x < 0
40 // half::posInf(), // sqrt(+inf)
41 // half::qNan(), // sqrt(-inf)
42 // half::qNan()); // sqrt(nan)
43 //
44 // half x = hsin (1);
45 // half y = hsqrt (3.5);
46 //
47 //---------------------------------------------------------------------------
48 
49 #ifndef _HALF_FUNCTION_H_
50 #define _HALF_FUNCTION_H_
51 
52 /// @cond Doxygen_Suppress
53 
54 #include "half.h"
55 
56 #include "ImathConfig.h"
57 #ifndef IMATH_HAVE_LARGE_STACK
58 # include <string.h> // need this for memset
59 #else
60 #endif
61 
62 #include <float.h>
63 
64 template <class T> class halfFunction
65 {
66 public:
67  //------------
68  // Constructor
69  //------------
70 
71  template <class Function>
72  halfFunction (
73  Function f,
74  half domainMin = -HALF_MAX,
75  half domainMax = HALF_MAX,
76  T defaultValue = 0,
77  T posInfValue = 0,
78  T negInfValue = 0,
79  T nanValue = 0);
80 
81 #ifndef IMATH_HAVE_LARGE_STACK
82  ~halfFunction () { delete[] _lut; }
83  halfFunction (const halfFunction&) = delete;
84  halfFunction& operator= (const halfFunction&) = delete;
85  halfFunction (halfFunction&&) = delete;
86  halfFunction& operator= (halfFunction&&) = delete;
87 #endif
88 
89  //-----------
90  // Evaluation
91  //-----------
92 
93  T operator() (half x) const;
94 
95 private:
96 #ifdef IMATH_HAVE_LARGE_STACK
97  T _lut[1 << 16];
98 #else
99  T* _lut;
100 #endif
101 };
102 
103 //---------------
104 // Implementation
105 //---------------
106 
107 template <class T>
108 template <class Function>
109 halfFunction<T>::halfFunction (
110  Function f,
111  half domainMin,
112  half domainMax,
113  T defaultValue,
114  T posInfValue,
115  T negInfValue,
116  T nanValue)
117 {
118 #ifndef IMATH_HAVE_LARGE_STACK
119  _lut = new T[1 << 16];
120 #endif
121 
122  for (int i = 0; i < (1 << 16); i++)
123  {
124  half x;
125  x.setBits (i);
126 
127  if (x.isNan ())
128  _lut[i] = nanValue;
129  else if (x.isInfinity ())
130  _lut[i] = x.isNegative () ? negInfValue : posInfValue;
131  else if (x < domainMin || x > domainMax)
132  _lut[i] = defaultValue;
133  else
134  _lut[i] = f (x);
135  }
136 }
137 
138 template <class T>
139 inline T
140 halfFunction<T>::operator() (half x) const
141 {
142  return _lut[x.bits ()];
143 }
144 
145 /// @endcond
146 
147 #endif
imath_half_bits_t half
if we're in a C-only context, alias the half bits type to half
Definition: half.h:268
#define HALF_MAX
Largest positive half.
Definition: half.h:225
GLfloat f
Definition: glcorearb.h:1926
GLint GLenum GLint x
Definition: glcorearb.h:409
LeafData & operator=(const LeafData &)=delete