2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
6 #ifndef INCLUDED_IMATHFUN_H
7 #define INCLUDED_IMATHFUN_H
9 //-----------------------------------------------------------------------------
11 // Miscellaneous utility functions
13 //-----------------------------------------------------------------------------
18 #include "ImathExport.h"
19 #include "ImathNamespace.h"
20 #include "ImathPlatform.h"
22 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
25 IMATH_HOSTDEVICE constexpr inline T
26 abs (T a) IMATH_NOEXCEPT
28 return (a > T (0)) ? a : -a;
32 IMATH_HOSTDEVICE constexpr inline int
33 sign (T a) IMATH_NOEXCEPT
35 return (a > T (0)) ? 1 : ((a < T (0)) ? -1 : 0);
38 template <class T, class Q>
39 IMATH_HOSTDEVICE constexpr inline T
40 lerp (T a, T b, Q t) IMATH_NOEXCEPT
42 return (T) (a * (1 - t) + b * t);
45 template <class T, class Q>
46 IMATH_HOSTDEVICE constexpr inline T
47 ulerp (T a, T b, Q t) IMATH_NOEXCEPT
49 return (T) ((a > b) ? (a - (a - b) * t) : (a + (b - a) * t));
53 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
54 lerpfactor (T m, T a, T b) IMATH_NOEXCEPT
57 // Return how far m is between a and b, that is return t such that
59 // t = lerpfactor(m, a, b);
69 if (abs (d) > T (1) || abs (n) < std::numeric_limits<T>::max() * abs (d))
76 IMATH_HOSTDEVICE constexpr inline T
77 clamp (T a, T l, T h) IMATH_NOEXCEPT
79 return (a < l) ? l : ((a > h) ? h : a);
83 IMATH_HOSTDEVICE constexpr inline int
84 cmp (T a, T b) IMATH_NOEXCEPT
86 return IMATH_INTERNAL_NAMESPACE::sign (a - b);
90 IMATH_HOSTDEVICE constexpr inline int
91 cmpt (T a, T b, T t) IMATH_NOEXCEPT
93 return (IMATH_INTERNAL_NAMESPACE::abs (a - b) <= t) ? 0 : cmp (a, b);
97 IMATH_HOSTDEVICE constexpr inline bool
98 iszero (T a, T t) IMATH_NOEXCEPT
100 return (IMATH_INTERNAL_NAMESPACE::abs (a) <= t) ? 1 : 0;
103 template <class T1, class T2, class T3>
104 IMATH_HOSTDEVICE constexpr inline bool
105 equal (T1 a, T2 b, T3 t) IMATH_NOEXCEPT
107 return IMATH_INTERNAL_NAMESPACE::abs (a - b) <= t;
111 IMATH_HOSTDEVICE constexpr inline int
112 floor (T x) IMATH_NOEXCEPT
114 return (x >= 0) ? int (x) : -(int (-x) + (-x > int (-x)));
118 IMATH_HOSTDEVICE constexpr inline int
119 ceil (T x) IMATH_NOEXCEPT
125 IMATH_HOSTDEVICE constexpr inline int
126 trunc (T x) IMATH_NOEXCEPT
128 return (x >= 0) ? int (x) : -int (-x);
132 // Integer division and remainder where the
133 // remainder of x/y has the same sign as x:
135 // divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y))
136 // mods(x,y) == x - y * divs(x,y)
139 IMATH_HOSTDEVICE constexpr inline int
140 divs (int x, int y) IMATH_NOEXCEPT
142 return (x >= 0) ? ((y >= 0) ? (x / y) : -(x / -y)) : ((y >= 0) ? -(-x / y) : (-x / -y));
145 IMATH_HOSTDEVICE constexpr inline int
146 mods (int x, int y) IMATH_NOEXCEPT
148 return (x >= 0) ? ((y >= 0) ? (x % y) : (x % -y)) : ((y >= 0) ? -(-x % y) : -(-x % -y));
152 // Integer division and remainder where the
153 // remainder of x/y is always positive:
155 // divp(x,y) == floor (double(x) / double (y))
156 // modp(x,y) == x - y * divp(x,y)
159 IMATH_HOSTDEVICE constexpr inline int
160 divp (int x, int y) IMATH_NOEXCEPT
162 return (x >= 0) ? ((y >= 0) ? (x / y) : -(x / -y))
163 : ((y >= 0) ? -((y - 1 - x) / y) : ((-y - 1 - x) / -y));
166 IMATH_HOSTDEVICE constexpr inline int
167 modp (int x, int y) IMATH_NOEXCEPT
169 return x - y * divp (x, y);
172 //----------------------------------------------------------
173 // Successor and predecessor for floating-point numbers:
175 // succf(f) returns float(f+e), where e is the smallest
176 // positive number such that float(f+e) != f.
178 // predf(f) returns float(f-e), where e is the smallest
179 // positive number such that float(f-e) != f.
181 // succd(d) returns double(d+e), where e is the smallest
182 // positive number such that double(d+e) != d.
184 // predd(d) returns double(d-e), where e is the smallest
185 // positive number such that double(d-e) != d.
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.
191 //----------------------------------------------------------
193 IMATH_EXPORT float succf (float f) IMATH_NOEXCEPT;
194 IMATH_EXPORT float predf (float f) IMATH_NOEXCEPT;
196 IMATH_EXPORT double succd (double d) IMATH_NOEXCEPT;
197 IMATH_EXPORT double predd (double d) IMATH_NOEXCEPT;
200 // Return true if the number is not a NaN or Infinity.
203 IMATH_HOSTDEVICE inline bool
204 finitef (float f) IMATH_NOEXCEPT
213 return (u.i & 0x7f800000) != 0x7f800000;
216 IMATH_HOSTDEVICE inline bool
217 finited (double d) IMATH_NOEXCEPT
226 return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;
229 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
231 #endif // INCLUDED_IMATHFUN_H