Skip to content

Commit 716311a

Browse files
authored
api(fmath.h): apply OIIO_NODISCARD (integer helpers, float utilities) (AcademySoftwareFoundation#5221)
Part of AcademySoftwareFoundation#4781 No callers within OIIO ignore the return value. Signed-off-by: Luna Kim <177369799+luna-y-kim@users.noreply.github.com>
1 parent b49dddb commit 716311a

1 file changed

Lines changed: 37 additions & 32 deletions

File tree

src/include/OpenImageIO/fmath.h

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ OIIO_NAMESPACE_BEGIN
138138
/// Quick test for whether an integer is a power of 2.
139139
///
140140
template<typename T>
141-
inline OIIO_HOSTDEVICE constexpr bool
141+
OIIO_NODISCARD inline OIIO_HOSTDEVICE constexpr bool
142142
ispow2(T x) noexcept
143143
{
144144
// Numerous references for this bit trick are on the web. The
@@ -155,7 +155,7 @@ ispow2(T x) noexcept
155155

156156
/// Round up to next higher power of 2 (return x if it's already a power
157157
/// of 2).
158-
inline OIIO_HOSTDEVICE constexpr int
158+
OIIO_NODISCARD inline OIIO_HOSTDEVICE constexpr int
159159
ceil2(int x) noexcept
160160
{
161161
// Here's a version with no loops.
@@ -178,7 +178,7 @@ ceil2(int x) noexcept
178178

179179
/// Round down to next lower power of 2 (return x if it's already a power
180180
/// of 2).
181-
inline OIIO_HOSTDEVICE constexpr int
181+
OIIO_NODISCARD inline OIIO_HOSTDEVICE constexpr int
182182
floor2(int x) noexcept
183183
{
184184
// Make all bits past the first 1 also be 1, i.e. 0001xxxx -> 00011111
@@ -198,7 +198,7 @@ floor2(int x) noexcept
198198
/// `round_to_multiple(10,10) == 10`, `round_to_multiple(17,10) == 20`, and
199199
/// `round_to_multiple(-17,10) == -10`.
200200
template <typename V, typename M>
201-
inline OIIO_HOSTDEVICE V round_to_multiple (V value, M multiple)
201+
OIIO_NODISCARD inline OIIO_HOSTDEVICE V round_to_multiple (V value, M multiple)
202202
{
203203
OIIO_DASSERT(multiple > M(0));
204204
if (value >= 0)
@@ -213,7 +213,7 @@ inline OIIO_HOSTDEVICE V round_to_multiple (V value, M multiple)
213213
/// round_to_multiple). This is a template that should work for any
214214
/// integer type.
215215
template<typename T>
216-
inline OIIO_HOSTDEVICE T
216+
OIIO_NODISCARD inline OIIO_HOSTDEVICE T
217217
round_to_multiple_of_pow2(T x, T m)
218218
{
219219
OIIO_DASSERT(ispow2(m));
@@ -227,7 +227,8 @@ round_to_multiple_of_pow2(T x, T m)
227227
/// `round_down_to_multiple(17,10) == 10`, and
228228
/// `round_down_to_multiple(-17,10) == -20`.
229229
template <typename V, typename M>
230-
inline OIIO_HOSTDEVICE V round_down_to_multiple (V value, M multiple)
230+
OIIO_NODISCARD inline OIIO_HOSTDEVICE V
231+
round_down_to_multiple (V value, M multiple)
231232
{
232233
if (value < 0)
233234
value -= V(multiple - 1);
@@ -238,7 +239,7 @@ inline OIIO_HOSTDEVICE V round_down_to_multiple (V value, M multiple)
238239

239240
/// Multiply two unsigned 32-bit ints safely, carefully checking for
240241
/// overflow, and clamping to uint32_t's maximum value.
241-
inline OIIO_HOSTDEVICE uint32_t
242+
OIIO_NODISCARD inline OIIO_HOSTDEVICE uint32_t
242243
clamped_mult32(uint32_t a, uint32_t b)
243244
{
244245
const uint32_t Err = std::numeric_limits<uint32_t>::max();
@@ -250,7 +251,7 @@ clamped_mult32(uint32_t a, uint32_t b)
250251

251252
/// Multiply two unsigned 64-bit ints safely, carefully checking for
252253
/// overflow, and clamping to uint64_t's maximum value.
253-
inline OIIO_HOSTDEVICE uint64_t
254+
OIIO_NODISCARD inline OIIO_HOSTDEVICE uint64_t
254255
clamped_mult64(uint64_t a, uint64_t b)
255256
{
256257
uint64_t ab = a * b;
@@ -264,7 +265,7 @@ clamped_mult64(uint64_t a, uint64_t b)
264265

265266
// safe_mod is like integer a%b, but safely returns 0 when b==0.
266267
template <class T>
267-
OIIO_FORCEINLINE OIIO_HOSTDEVICE T
268+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE T
268269
safe_mod(T a, T b)
269270
{
270271
return b ? (a % b) : T(0);
@@ -286,7 +287,7 @@ safe_mod(T a, T b)
286287

287288
/// clamp a to bounds [low,high].
288289
template <class T>
289-
OIIO_FORCEINLINE OIIO_HOSTDEVICE T
290+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE T
290291
clamp (const T& a, const T& low, const T& high)
291292
{
292293
#if 1
@@ -312,38 +313,38 @@ clamp (const T& a, const T& low, const T& high)
312313

313314
#ifndef __CUDA_ARCH__
314315
// Specialization of clamp for vfloat4
315-
template<> OIIO_FORCEINLINE simd::vfloat4
316+
template<> OIIO_NODISCARD OIIO_FORCEINLINE simd::vfloat4
316317
clamp (const simd::vfloat4& a, const simd::vfloat4& low, const simd::vfloat4& high)
317318
{
318319
return simd::min (high, simd::max (low, a));
319320
}
320321

321-
template<> OIIO_FORCEINLINE simd::vfloat8
322+
template<> OIIO_NODISCARD OIIO_FORCEINLINE simd::vfloat8
322323
clamp (const simd::vfloat8& a, const simd::vfloat8& low, const simd::vfloat8& high)
323324
{
324325
return simd::min (high, simd::max (low, a));
325326
}
326327

327-
template<> OIIO_FORCEINLINE simd::vfloat16
328+
template<> OIIO_NODISCARD OIIO_FORCEINLINE simd::vfloat16
328329
clamp (const simd::vfloat16& a, const simd::vfloat16& low, const simd::vfloat16& high)
329330
{
330331
return simd::min (high, simd::max (low, a));
331332
}
332333

333334
// Specialization of clamp for vint4
334-
template<> OIIO_FORCEINLINE simd::vint4
335+
template<> OIIO_NODISCARD OIIO_FORCEINLINE simd::vint4
335336
clamp (const simd::vint4& a, const simd::vint4& low, const simd::vint4& high)
336337
{
337338
return simd::min (high, simd::max (low, a));
338339
}
339340

340-
template<> OIIO_FORCEINLINE simd::vint8
341+
template<> OIIO_NODISCARD OIIO_FORCEINLINE simd::vint8
341342
clamp (const simd::vint8& a, const simd::vint8& low, const simd::vint8& high)
342343
{
343344
return simd::min (high, simd::max (low, a));
344345
}
345346

346-
template<> OIIO_FORCEINLINE simd::vint16
347+
template<> OIIO_NODISCARD OIIO_FORCEINLINE simd::vint16
347348
clamp (const simd::vint16& a, const simd::vint16& low, const simd::vint16& high)
348349
{
349350
return simd::min (high, simd::max (low, a));
@@ -360,7 +361,8 @@ clamp (const simd::vint16& a, const simd::vint16& low, const simd::vint16& high)
360361
// least as much precision as a multiply followed by a separate add."
361362

362363
/// Fused multiply and add: (a*b + c)
363-
OIIO_FORCEINLINE OIIO_HOSTDEVICE float madd (float a, float b, float c) {
364+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE float
365+
madd (float a, float b, float c) {
364366
// NOTE: GCC/ICC will turn this (for float) into a FMA unless
365367
// explicitly asked not to, clang will do so if -ffp-contract=fast.
366368
OIIO_CLANG_PRAGMA(clang fp contract(fast))
@@ -369,23 +371,26 @@ OIIO_FORCEINLINE OIIO_HOSTDEVICE float madd (float a, float b, float c) {
369371

370372

371373
/// Fused multiply and subtract: (a*b - c)
372-
OIIO_FORCEINLINE OIIO_HOSTDEVICE float msub (float a, float b, float c) {
374+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE float
375+
msub (float a, float b, float c) {
373376
OIIO_CLANG_PRAGMA(clang fp contract(fast))
374377
return a * b - c;
375378
}
376379

377380

378381

379382
/// Fused negative multiply and add: -(a*b) + c
380-
OIIO_FORCEINLINE OIIO_HOSTDEVICE float nmadd (float a, float b, float c) {
383+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE float
384+
nmadd (float a, float b, float c) {
381385
OIIO_CLANG_PRAGMA(clang fp contract(fast))
382386
return c - (a * b);
383387
}
384388

385389

386390

387391
/// Negative fused multiply and subtract: -(a*b) - c
388-
OIIO_FORCEINLINE OIIO_HOSTDEVICE float nmsub (float a, float b, float c) {
392+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE float
393+
nmsub (float a, float b, float c) {
389394
OIIO_CLANG_PRAGMA(clang fp contract(fast))
390395
return -(a * b) - c;
391396
}
@@ -395,7 +400,7 @@ OIIO_FORCEINLINE OIIO_HOSTDEVICE float nmsub (float a, float b, float c) {
395400
/// Linearly interpolate values v0-v1 at x: v0*(1-x) + v1*x.
396401
/// This is a template, and so should work for any types.
397402
template <class T, class Q>
398-
OIIO_FORCEINLINE OIIO_HOSTDEVICE T
403+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE T
399404
lerp (const T& v0, const T& v1, const Q& x)
400405
{
401406
// NOTE: a*(1-x) + b*x is much more numerically stable than a+x*(b-a)
@@ -408,7 +413,7 @@ lerp (const T& v0, const T& v1, const Q& x)
408413
/// v2 lower left, v3 lower right) at coordinates (s,t) and return the
409414
/// result. This is a template, and so should work for any types.
410415
template <class T, class Q>
411-
OIIO_FORCEINLINE OIIO_HOSTDEVICE T
416+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE T
412417
bilerp(const T& v0, const T& v1, const T& v2, const T& v3, const Q& s, const Q& t)
413418
{
414419
// NOTE: a*(t-1) + b*t is much more numerically stable than a+t*(b-a)
@@ -460,7 +465,7 @@ bilerp_mad (const T *v0, const T *v1,
460465
/// upper right top, ...) at coordinates (s,t,r), and return the
461466
/// result. This is a template, and so should work for any types.
462467
template <class T, class Q>
463-
OIIO_FORCEINLINE OIIO_HOSTDEVICE T
468+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE T
464469
trilerp (T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, Q s, Q t, Q r)
465470
{
466471
// NOTE: a*(t-1) + b*t is much more numerically stable than a+t*(b-a)
@@ -564,7 +569,7 @@ bicubic_interp (const T **val, T s, T t, int n, T *result)
564569

565570

566571
/// Return floor(x) cast to an int.
567-
OIIO_FORCEINLINE OIIO_HOSTDEVICE int
572+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE int
568573
ifloor (float x)
569574
{
570575
return (int)floorf(x);
@@ -576,7 +581,7 @@ ifloor (float x)
576581
/// to the built-in modf, but returns a true int, always rounds down
577582
/// (compared to modf which rounds toward 0), and always returns
578583
/// frac >= 0 (compared to modf which can return <0 if x<0).
579-
inline OIIO_HOSTDEVICE float
584+
OIIO_NODISCARD inline OIIO_HOSTDEVICE float
580585
floorfrac (float x, int *xint)
581586
{
582587
#if 1
@@ -592,19 +597,19 @@ floorfrac (float x, int *xint)
592597

593598

594599
#ifndef __CUDA_ARCH__
595-
inline simd::vfloat4 floorfrac (const simd::vfloat4& x, simd::vint4 *xint) {
600+
OIIO_NODISCARD inline simd::vfloat4 floorfrac (const simd::vfloat4& x, simd::vint4 *xint) {
596601
simd::vfloat4 f = simd::floor(x);
597602
*xint = simd::vint4(f);
598603
return x - f;
599604
}
600605

601-
inline simd::vfloat8 floorfrac (const simd::vfloat8& x, simd::vint8 *xint) {
606+
OIIO_NODISCARD inline simd::vfloat8 floorfrac (const simd::vfloat8& x, simd::vint8 *xint) {
602607
simd::vfloat8 f = simd::floor(x);
603608
*xint = simd::vint8(f);
604609
return x - f;
605610
}
606611

607-
inline simd::vfloat16 floorfrac (const simd::vfloat16& x, simd::vint16 *xint) {
612+
OIIO_NODISCARD inline simd::vfloat16 floorfrac (const simd::vfloat16& x, simd::vint16 *xint) {
608613
simd::vfloat16 f = simd::floor(x);
609614
*xint = simd::vint16(f);
610615
return x - f;
@@ -616,13 +621,13 @@ inline simd::vfloat16 floorfrac (const simd::vfloat16& x, simd::vint16 *xint) {
616621

617622
/// Convert degrees to radians.
618623
template <typename T>
619-
OIIO_FORCEINLINE OIIO_HOSTDEVICE constexpr T radians (T deg) {
624+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE constexpr T radians (T deg) {
620625
return deg * T(M_PI / 180.0);
621626
}
622627

623628
/// Convert radians to degrees
624629
template <typename T>
625-
OIIO_FORCEINLINE OIIO_HOSTDEVICE constexpr T degrees (T rad) {
630+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE constexpr T degrees (T rad) {
626631
return rad * T(180.0 / M_PI);
627632
}
628633

@@ -646,7 +651,7 @@ OIIO_FORCEINLINE OIIO_HOSTDEVICE constexpr T degrees (T rad) {
646651
/// It's also safe (though pointless) to use fast_neg for integer types,
647652
/// where both `-x` and `0-x` are implemented as a `neg` instruction.
648653
template <typename T>
649-
OIIO_FORCEINLINE OIIO_HOSTDEVICE T
654+
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE T
650655
fast_neg(const T &x) {
651656
return T(0) - x;
652657
}
@@ -687,7 +692,7 @@ sincos (double x, double* sine, double* cosine)
687692
/// Return -1 if x<0, 0 if x==0, 1 if x>0. For floating point types, this is
688693
/// not friendly to NaN inputs!
689694
template<typename T>
690-
inline OIIO_HOSTDEVICE T sign(T x)
695+
OIIO_NODISCARD inline OIIO_HOSTDEVICE T sign(T x)
691696
{
692697
return x < T(0) ? T(-1) : (x == T(0) ? T(0) : T(1));
693698
}

0 commit comments

Comments
 (0)