Coverage Report

Created: 2022-08-24 06:20

/work/_deps/imath-src/src/Imath/ImathFun.h
Line
Count
Source (jump to first uncovered line)
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
26
abs (T a) IMATH_NOEXCEPT
27
0
{
28
0
    return (a > T (0)) ? a : -a;
29
0
}
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
0
{
142
0
    return (x >= 0) ? ((y >= 0) ? (x / y) : -(x / -y))
143
0
                    : ((y >= 0) ? -(-x / y) : (-x / -y));
144
0
}
145
146
IMATH_HOSTDEVICE constexpr inline int
147
mods (int x, int y) IMATH_NOEXCEPT
148
0
{
149
0
    return (x >= 0) ? ((y >= 0) ? (x % y) : (x % -y))
150
0
                    : ((y >= 0) ? -(-x % y) : -(-x % -y));
151
0
}
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
4.86M
{
164
4.86M
    return (x >= 0) ? ((y >= 0) ? (x / y) : -(x / -y))
165
4.86M
                    : ((y >= 0) ? -((y - 1 - x) / y) : ((-y - 1 - x) / -y));
166
4.86M
}
167
168
IMATH_HOSTDEVICE constexpr inline int
169
modp (int x, int y) IMATH_NOEXCEPT
170
3.58M
{
171
3.58M
    return x - y * divp (x, y);
172
3.58M
}
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
206
finitef (float f) IMATH_NOEXCEPT
207
0
{
208
0
    union
209
0
    {
210
0
        float f;
211
0
        int   i;
212
0
    } u;
213
0
    u.f = f;
214
0
215
0
    return (u.i & 0x7f800000) != 0x7f800000;
216
0
}
217
218
IMATH_HOSTDEVICE inline bool
219
finited (double d) IMATH_NOEXCEPT
220
0
{
221
0
    union
222
0
    {
223
0
        double   d;
224
0
        uint64_t i;
225
0
    } u;
226
0
    u.d = d;
227
0
228
0
    return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;
229
0
}
230
231
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
232
233
#endif // INCLUDED_IMATHFUN_H