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 <limits>
16#include <cstdint>
17
18#include "ImathExport.h"
19#include "ImathNamespace.h"
20#include "ImathPlatform.h"
21
22IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
23
24template <class T>
25IMATH_HOSTDEVICE constexpr inline T
26abs (T a) IMATH_NOEXCEPT
27{
28 return (a > T (0)) ? a : -a;
29}
30
31template <class T>
32IMATH_HOSTDEVICE constexpr inline int
33sign (T a) IMATH_NOEXCEPT
34{
35 return (a > T (0)) ? 1 : ((a < T (0)) ? -1 : 0);
36}
37
38template <class T, class Q>
39IMATH_HOSTDEVICE constexpr inline T
40lerp (T a, T b, Q t) IMATH_NOEXCEPT
41{
42 return (T) (a * (1 - t) + b * t);
43}
44
45template <class T, class Q>
46IMATH_HOSTDEVICE constexpr inline T
47ulerp (T a, T b, Q t) IMATH_NOEXCEPT
48{
49 return (T) ((a > b) ? (a - (a - b) * t) : (a + (b - a) * t));
50}
51
52template <class T>
53IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
54lerpfactor (T m, T a, T b) IMATH_NOEXCEPT
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
75template <class T>
76IMATH_HOSTDEVICE constexpr inline T
77clamp (T a, T l, T h) IMATH_NOEXCEPT
78{
79 return (a < l) ? l : ((a > h) ? h : a);
80}
81
82template <class T>
83IMATH_HOSTDEVICE constexpr inline int
84cmp (T a, T b) IMATH_NOEXCEPT
85{
86 return IMATH_INTERNAL_NAMESPACE::sign (a - b);
87}
88
89template <class T>
90IMATH_HOSTDEVICE constexpr inline int
91cmpt (T a, T b, T t) IMATH_NOEXCEPT
92{
93 return (IMATH_INTERNAL_NAMESPACE::abs (a - b) <= t) ? 0 : cmp (a, b);
94}
95
96template <class T>
97IMATH_HOSTDEVICE constexpr inline bool
98iszero (T a, T t) IMATH_NOEXCEPT
99{
100 return (IMATH_INTERNAL_NAMESPACE::abs (a) <= t) ? 1 : 0;
101}
102
103template <class T1, class T2, class T3>
104IMATH_HOSTDEVICE constexpr inline bool
105equal (T1 a, T2 b, T3 t) IMATH_NOEXCEPT
106{
107 return IMATH_INTERNAL_NAMESPACE::abs (a - b) <= t;
108}
109
110template <class T>
111IMATH_HOSTDEVICE constexpr inline int
112floor (T x) IMATH_NOEXCEPT
113{
114 return (x >= 0) ? int (x) : -(int (-x) + (-x > int (-x)));
115}
116
117template <class T>
118IMATH_HOSTDEVICE constexpr inline int
119ceil (T x) IMATH_NOEXCEPT
120{
121 return -floor (-x);
122}
123
124template <class T>
125IMATH_HOSTDEVICE constexpr inline int
126trunc (T x) IMATH_NOEXCEPT
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
139IMATH_HOSTDEVICE constexpr inline int
140divs (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
145IMATH_HOSTDEVICE constexpr inline int
146mods (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
159IMATH_HOSTDEVICE constexpr inline int
160divp (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
166IMATH_HOSTDEVICE constexpr inline int
167modp (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
193IMATH_EXPORT float succf (float f) IMATH_NOEXCEPT;
194IMATH_EXPORT float predf (float f) IMATH_NOEXCEPT;
195
196IMATH_EXPORT double succd (double d) IMATH_NOEXCEPT;
197IMATH_EXPORT double predd (double d) IMATH_NOEXCEPT;
198
199//
200// Return true if the number is not a NaN or Infinity.
201//
202
203IMATH_HOSTDEVICE inline bool
204finitef (float f) IMATH_NOEXCEPT
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
216IMATH_HOSTDEVICE inline bool
217finited (double d) IMATH_NOEXCEPT
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
229IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
230
231#endif // INCLUDED_IMATHFUN_H
232

source code of include/Imath/ImathFun.h