Debian Codegraph @

src/Imath/halfFunction.h

raw
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 (Function f,
73 half domainMin = -HALF_MAX,
74 half domainMax = HALF_MAX,
75 T defaultValue = 0,
76 T posInfValue = 0,
77 T negInfValue = 0,
78 T nanValue = 0);
79
80 #ifndef IMATH_HAVE_LARGE_STACK
81 ~halfFunction() { delete[] _lut; }
82 halfFunction (const halfFunction&) = delete;
83 halfFunction& operator= (const halfFunction&) = delete;
84 halfFunction (halfFunction&&) = delete;
85 halfFunction& operator= (halfFunction&&) = delete;
86 #endif
87
88 //-----------
89 // Evaluation
90 //-----------
91
92 T operator() (half x) const;
93
94 private:
95 #ifdef IMATH_HAVE_LARGE_STACK
96 T _lut[1 << 16];
97 #else
98 T* _lut;
99 #endif
100 };
101
102 //---------------
103 // Implementation
104 //---------------
105
106 template <class T>
107 template <class Function>
108 halfFunction<T>::halfFunction (Function f,
109 half domainMin,
110 half domainMax,
111 T defaultValue,
112 T posInfValue,
113 T negInfValue,
114 T nanValue)
115 {
116 #ifndef IMATH_HAVE_LARGE_STACK
117 _lut = new T[1 << 16];
118 #endif
119
120 for (int i = 0; i < (1 << 16); i++)
121 {
122 half x;
123 x.setBits (i);
124
125 if (x.isNan())
126 _lut[i] = nanValue;
127 else if (x.isInfinity())
128 _lut[i] = x.isNegative() ? negInfValue : posInfValue;
129 else if (x < domainMin || x > domainMax)
130 _lut[i] = defaultValue;
131 else
132 _lut[i] = f (x);
133 }
134 }
135
136 template <class T>
137 inline T
138 halfFunction<T>::operator() (half x) const
139 {
140 return _lut[x.bits()];
141 }
142
143
144 /// @endcond
145
146
147 #endif