HDK
ImathFun.h
Go to the documentation of this file.
1 //
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 #ifndef INCLUDED_IMATHFUN_H
7 #define INCLUDED_IMATHFUN_H
8
9 //-----------------------------------------------------------------------------
10 //
11 // Miscellaneous utility functions
12 //
13 //-----------------------------------------------------------------------------
14
15 #include <limits>
16 #include <cstdint>
17
18 #include "ImathExport.h"
19 #include "ImathNamespace.h"
20 #include "ImathPlatform.h"
21
23
24 template <class T>
25 IMATH_HOSTDEVICE constexpr inline T
27 {
28  return (a > T (0)) ? a : -a;
29 }
30
31 template <class T>
32 IMATH_HOSTDEVICE constexpr inline int
34 {
35  return (a > T (0)) ? 1 : ((a < T (0)) ? -1 : 0);
36 }
37
38 template <class T, class Q>
39 IMATH_HOSTDEVICE constexpr inline T
40 lerp (T a, T b, Q t) IMATH_NOEXCEPT
41 {
42  return (T) (a * (1 - t) + b * t);
43 }
44
45 template <class T, class Q>
46 IMATH_HOSTDEVICE constexpr inline T
48 {
49  return (T) ((a > b) ? (a - (a - b) * t) : (a + (b - a) * t));
50 }
51
52 template <class T>
53 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
55 {
56  //
57  // Return how far m is between a and b, that is return t such that
58  // if:
59  // t = lerpfactor(m, a, b);
60  // then:
61  // m = lerp(a, b, t);
62  //
63  // If a==b, return 0.
64  //
65
66  T d = b - a;
67  T n = m - a;
68
69  if (abs (d) > T (1) || abs (n) < std::numeric_limits<T>::max() * abs (d))
70  return n / d;
71
72  return T (0);
73 }
74
75 template <class T>
76 IMATH_HOSTDEVICE constexpr inline T
77 clamp (T a, T l, T h) IMATH_NOEXCEPT
78 {
79  return (a < l) ? l : ((a > h) ? h : a);
80 }
81
82 template <class T>
83 IMATH_HOSTDEVICE constexpr inline int
85 {
87 }
88
89 template <class T>
90 IMATH_HOSTDEVICE constexpr inline int
91 cmpt (T a, T b, T t) IMATH_NOEXCEPT
92 {
93  return (IMATH_INTERNAL_NAMESPACE::abs (a - b) <= t) ? 0 : cmp (a, b);
94 }
95
96 template <class T>
97 IMATH_HOSTDEVICE constexpr inline bool
99 {
100  return (IMATH_INTERNAL_NAMESPACE::abs (a) <= t) ? 1 : 0;
101 }
102
103 template <class T1, class T2, class T3>
104 IMATH_HOSTDEVICE constexpr inline bool
105 equal (T1 a, T2 b, T3 t) IMATH_NOEXCEPT
106 {
107  return IMATH_INTERNAL_NAMESPACE::abs (a - b) <= t;
108 }
109
110 template <class T>
111 IMATH_HOSTDEVICE constexpr inline int
113 {
114  return (x >= 0) ? int (x) : -(int (-x) + (-x > int (-x)));
115 }
116
117 template <class T>
118 IMATH_HOSTDEVICE constexpr inline int
120 {
121  return -floor (-x);
122 }
123
124 template <class T>
125 IMATH_HOSTDEVICE constexpr inline int
127 {
128  return (x >= 0) ? int (x) : -int (-x);
129 }
130
131 //
132 // Integer division and remainder where the
133 // remainder of x/y has the same sign as x:
134 //
135 // divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y))
136 // mods(x,y) == x - y * divs(x,y)
137 //
138
139 IMATH_HOSTDEVICE constexpr inline int
140 divs (int x, int y) IMATH_NOEXCEPT
141 {
142  return (x >= 0) ? ((y >= 0) ? (x / y) : -(x / -y)) : ((y >= 0) ? -(-x / y) : (-x / -y));
143 }
144
145 IMATH_HOSTDEVICE constexpr inline int
146 mods (int x, int y) IMATH_NOEXCEPT
147 {
148  return (x >= 0) ? ((y >= 0) ? (x % y) : (x % -y)) : ((y >= 0) ? -(-x % y) : -(-x % -y));
149 }
150
151 //
152 // Integer division and remainder where the
153 // remainder of x/y is always positive:
154 //
155 // divp(x,y) == floor (double(x) / double (y))
156 // modp(x,y) == x - y * divp(x,y)
157 //
158
159 IMATH_HOSTDEVICE constexpr inline int
160 divp (int x, int y) IMATH_NOEXCEPT
161 {
162  return (x >= 0) ? ((y >= 0) ? (x / y) : -(x / -y))
163  : ((y >= 0) ? -((y - 1 - x) / y) : ((-y - 1 - x) / -y));
164 }
165
166 IMATH_HOSTDEVICE constexpr inline int
167 modp (int x, int y) IMATH_NOEXCEPT
168 {
169  return x - y * divp (x, y);
170 }
171
172 //----------------------------------------------------------
173 // Successor and predecessor for floating-point numbers:
174 //
175 // succf(f) returns float(f+e), where e is the smallest
176 // positive number such that float(f+e) != f.
177 //
178 // predf(f) returns float(f-e), where e is the smallest
179 // positive number such that float(f-e) != f.
180 //
181 // succd(d) returns double(d+e), where e is the smallest
182 // positive number such that double(d+e) != d.
183 //
184 // predd(d) returns double(d-e), where e is the smallest
185 // positive number such that double(d-e) != d.
186 //
187 // Exceptions: If the input value is an infinity or a nan,
188 // succf(), predf(), succd(), and predd() all
189 // return the input value without changing it.
190 //
191 //----------------------------------------------------------
192
193 IMATH_EXPORT float succf (float f) IMATH_NOEXCEPT;
194 IMATH_EXPORT float predf (float f) IMATH_NOEXCEPT;
195
196 IMATH_EXPORT double succd (double d) IMATH_NOEXCEPT;
197 IMATH_EXPORT double predd (double d) IMATH_NOEXCEPT;
198
199 //
200 // Return true if the number is not a NaN or Infinity.
201 //
202
203 IMATH_HOSTDEVICE inline bool
205 {
206  union
207  {
208  float f;
209  int i;
210  } u;
211  u.f = f;
212
213  return (u.i & 0x7f800000) != 0x7f800000;
214 }
215
216 IMATH_HOSTDEVICE inline bool
218 {
219  union
220  {
221  double d;
222  uint64_t i;
223  } u;
224  u.d = d;
225
226  return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;
227 }
228
230
231 #endif // INCLUDED_IMATHFUN_H
IMATH_HOSTDEVICE constexpr int modp(int x, int y) IMATH_NOEXCEPT
Definition: ImathFun.h:167
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:72
IMATH_HOSTDEVICE constexpr int floor(T x) IMATH_NOEXCEPT
Definition: ImathFun.h:112
IMATH_HOSTDEVICE constexpr int divp(int x, int y) IMATH_NOEXCEPT
Definition: ImathFun.h:160
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
GLint y
Definition: glcorearb.h:103
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T lerpfactor(T m, T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:54
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:102
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
IMATH_HOSTDEVICE constexpr T clamp(T a, T l, T h) IMATH_NOEXCEPT
Definition: ImathFun.h:77
IMATH_EXPORT float predf(float f) IMATH_NOEXCEPT
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
IMATH_HOSTDEVICE constexpr int trunc(T x) IMATH_NOEXCEPT
Definition: ImathFun.h:126
IMATH_EXPORT float succf(float f) IMATH_NOEXCEPT
IMATH_HOSTDEVICE bool finited(double d) IMATH_NOEXCEPT
Definition: ImathFun.h:217
IMATH_HOSTDEVICE constexpr int sign(T a) IMATH_NOEXCEPT
Definition: ImathFun.h:33
IMATH_EXPORT double predd(double d) IMATH_NOEXCEPT
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
#define IMATH_EXPORT
Definition: ImathExport.h:47
GLdouble t
IMATH_HOSTDEVICE constexpr int divs(int x, int y) IMATH_NOEXCEPT
Definition: ImathFun.h:140
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
IMATH_HOSTDEVICE constexpr int ceil(T x) IMATH_NOEXCEPT
Definition: ImathFun.h:119
IMATH_HOSTDEVICE constexpr bool iszero(T a, T t) IMATH_NOEXCEPT
Definition: ImathFun.h:98
IMATH_HOSTDEVICE constexpr T ulerp(T a, T b, Q t) IMATH_NOEXCEPT
Definition: ImathFun.h:47
IMATH_EXPORT double succd(double d) IMATH_NOEXCEPT
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
IMATH_HOSTDEVICE constexpr int cmpt(T a, T b, T t) IMATH_NOEXCEPT
Definition: ImathFun.h:91
IMATH_HOSTDEVICE constexpr T lerp(T a, T b, Q t) IMATH_NOEXCEPT
Definition: ImathFun.h:40
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_HOSTDEVICE constexpr T abs(T a) IMATH_NOEXCEPT
Definition: ImathFun.h:26
IMATH_HOSTDEVICE constexpr int mods(int x, int y) IMATH_NOEXCEPT
Definition: ImathFun.h:146
IMATH_HOSTDEVICE bool finitef(float f) IMATH_NOEXCEPT
Definition: ImathFun.h:204