| 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 | |
| 22 | IMATH_INTERNAL_NAMESPACE_HEADER_ENTER |
| 23 | |
| 24 | template <class T> |
| 25 | IMATH_HOSTDEVICE constexpr inline T |
| 26 | abs (T a) IMATH_NOEXCEPT |
| 27 | { |
| 28 | return (a > T (0)) ? a : -a; |
| 29 | } |
| 30 | |
| 31 | template <class T> |
| 32 | IMATH_HOSTDEVICE constexpr inline int |
| 33 | sign (T a) IMATH_NOEXCEPT |
| 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 |
| 47 | ulerp (T a, T b, Q t) IMATH_NOEXCEPT |
| 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 |
| 54 | lerpfactor (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 | |
| 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 |
| 84 | cmp (T a, T b) IMATH_NOEXCEPT |
| 85 | { |
| 86 | return IMATH_INTERNAL_NAMESPACE::sign (a - b); |
| 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 |
| 98 | iszero (T a, T t) IMATH_NOEXCEPT |
| 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 |
| 112 | floor (T x) IMATH_NOEXCEPT |
| 113 | { |
| 114 | return (x >= 0) ? int (x) : -(int (-x) + (-x > int (-x))); |
| 115 | } |
| 116 | |
| 117 | template <class T> |
| 118 | IMATH_HOSTDEVICE constexpr inline int |
| 119 | ceil (T x) IMATH_NOEXCEPT |
| 120 | { |
| 121 | return -floor (-x); |
| 122 | } |
| 123 | |
| 124 | template <class T> |
| 125 | IMATH_HOSTDEVICE constexpr inline int |
| 126 | trunc (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 | |
| 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 |
| 204 | finitef (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 | |
| 216 | IMATH_HOSTDEVICE inline bool |
| 217 | finited (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 | |
| 229 | IMATH_INTERNAL_NAMESPACE_HEADER_EXIT |
| 230 | |
| 231 | #endif // INCLUDED_IMATHFUN_H |
| 232 | |