HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathFun.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 #ifndef INCLUDED_IMATHFUN_H
7 #define INCLUDED_IMATHFUN_H
8 
9 //-----------------------------------------------------------------------------
10 //
11 // Miscellaneous utility functions
12 //
13 //-----------------------------------------------------------------------------
14 
15 #include <cstdint>
16 #include <limits>
17 
18 #include "ImathExport.h"
19 #include "ImathNamespace.h"
20 #include "ImathPlatform.h"
21 
22 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
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))
143  : ((y >= 0) ? -(-x / y) : (-x / -y));
144 }
145 
146 IMATH_HOSTDEVICE constexpr inline int
147 mods (int x, int y) IMATH_NOEXCEPT
148 {
149  return (x >= 0) ? ((y >= 0) ? (x % y) : (x % -y))
150  : ((y >= 0) ? -(-x % y) : -(-x % -y));
151 }
152 
153 //
154 // Integer division and remainder where the
155 // remainder of x/y is always positive:
156 //
157 // divp(x,y) == floor (double(x) / double (y))
158 // modp(x,y) == x - y * divp(x,y)
159 //
160 
161 IMATH_HOSTDEVICE constexpr inline int
162 divp (int x, int y) IMATH_NOEXCEPT
163 {
164  return (x >= 0) ? ((y >= 0) ? (x / y) : -(x / -y))
165  : ((y >= 0) ? -((y - 1 - x) / y) : ((-y - 1 - x) / -y));
166 }
167 
168 IMATH_HOSTDEVICE constexpr inline int
169 modp (int x, int y) IMATH_NOEXCEPT
170 {
171  return x - y * divp (x, y);
172 }
173 
174 //----------------------------------------------------------
175 // Successor and predecessor for floating-point numbers:
176 //
177 // succf(f) returns float(f+e), where e is the smallest
178 // positive number such that float(f+e) != f.
179 //
180 // predf(f) returns float(f-e), where e is the smallest
181 // positive number such that float(f-e) != f.
182 //
183 // succd(d) returns double(d+e), where e is the smallest
184 // positive number such that double(d+e) != d.
185 //
186 // predd(d) returns double(d-e), where e is the smallest
187 // positive number such that double(d-e) != d.
188 //
189 // Exceptions: If the input value is an infinity or a nan,
190 // succf(), predf(), succd(), and predd() all
191 // return the input value without changing it.
192 //
193 //----------------------------------------------------------
194 
195 IMATH_EXPORT float succf (float f) IMATH_NOEXCEPT;
196 IMATH_EXPORT float predf (float f) IMATH_NOEXCEPT;
197 
198 IMATH_EXPORT double succd (double d) IMATH_NOEXCEPT;
199 IMATH_EXPORT double predd (double d) IMATH_NOEXCEPT;
200 
201 //
202 // Return true if the number is not a NaN or Infinity.
203 //
204 
205 IMATH_HOSTDEVICE inline bool
207 {
208  union
209  {
210  float f;
211  int i;
212  } u;
213  u.f = f;
214 
215  return (u.i & 0x7f800000) != 0x7f800000;
216 }
217 
218 IMATH_HOSTDEVICE inline bool
220 {
221  union
222  {
223  double d;
224  uint64_t i;
225  } u;
226  u.d = d;
227 
228  return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;
229 }
230 
231 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
232 
233 #endif // INCLUDED_IMATHFUN_H
IMATH_HOSTDEVICE constexpr int modp(int x, int y) IMATH_NOEXCEPT
Definition: ImathFun.h:169
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
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:162
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:108
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:219
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
Definition: glad.h:2397
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:147
IMATH_HOSTDEVICE bool finitef(float f) IMATH_NOEXCEPT
Definition: ImathFun.h:206