HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fmath.h File Reference
#include <algorithm>
#include <cmath>
#include <cstring>
#include <limits>
#include <typeinfo>
#include <type_traits>
#include <OpenImageIO/Imath.h>
#include <OpenImageIO/span.h>
#include <OpenImageIO/dassert.h>
#include <OpenImageIO/oiioversion.h>
#include <OpenImageIO/platform.h>
#include <OpenImageIO/simd.h>
+ Include dependency graph for fmath.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  big_enough_float< T >
 
struct  big_enough_float< int >
 
struct  big_enough_float< unsigned int >
 
struct  big_enough_float< int64_t >
 
struct  big_enough_float< uint64_t >
 
struct  big_enough_float< double >
 
struct  DataProxy< I, E >
 
struct  ConstDataProxy< I, E >
 
struct  DataArrayProxy< I, E >
 
struct  ConstDataArrayProxy< I, E >
 
class  EightBitConverter< T >
 

Macros

#define OIIO_FMATH_H   1
 
#define OIIO_FMATH_SIMD_FRIENDLY   0
 
#define M_PI   3.14159265358979323846264338327950288
 
#define M_PI_2   1.57079632679489661923132169163975144
 
#define M_PI_4   0.785398163397448309615660845819875721
 
#define M_TWO_PI   (M_PI * 2.0)
 
#define M_1_PI   0.318309886183790671537767526745028724
 
#define M_2_PI   0.636619772367581343075535053490057448
 
#define M_SQRT2   1.41421356237309504880168872420969808
 
#define M_SQRT1_2   0.707106781186547524400844362104849039
 
#define M_LN2   0.69314718055994530941723212145817656
 
#define M_LN10   2.30258509299404568401799145468436421
 
#define M_E   2.71828182845904523536028747135266250
 
#define M_LOG2E   1.44269504088896340735992468100189214
 
#define OIIO_FMATH_HAS_SAFE_FMOD   1
 

Functions

template<typename T >
OIIO_HOSTDEVICE
OIIO_CONSTEXPR14 bool 
ispow2 (T x) noexcept
 
OIIO_HOSTDEVICE
OIIO_CONSTEXPR14 int 
ceil2 (int x) noexcept
 
OIIO_HOSTDEVICE
OIIO_CONSTEXPR14 int 
floor2 (int x) noexcept
 
OIIO_HOSTDEVICE int pow2roundup (int x)
 
OIIO_HOSTDEVICE int pow2rounddown (int x)
 
template<typename V , typename M >
OIIO_HOSTDEVICEround_to_multiple (V value, M multiple)
 
template<typename T >
OIIO_HOSTDEVICEround_to_multiple_of_pow2 (T x, T m)
 
OIIO_HOSTDEVICE uint32_t clamped_mult32 (uint32_t a, uint32_t b)
 
OIIO_HOSTDEVICE uint64_t clamped_mult64 (uint64_t a, uint64_t b)
 
template<class T >
OIIO_NODISCARD
OIIO_FORCEINLINE
OIIO_HOSTDEVICE
rotl (T x, int s) noexcept
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE uint32_t 
rotl32 (uint32_t x, int k)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE uint64_t 
rotl64 (uint64_t x, int k)
 
template<class T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_mod (T a, T b)
 
template<class T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEclamp (const T &a, const T &low, const T &high)
 clamp a to bounds [low,high]. More...
 
template<>
OIIO_FORCEINLINE simd::vfloat4 clamp (const simd::vfloat4 &a, const simd::vfloat4 &low, const simd::vfloat4 &high)
 
template<>
OIIO_FORCEINLINE simd::vfloat8 clamp (const simd::vfloat8 &a, const simd::vfloat8 &low, const simd::vfloat8 &high)
 
template<>
OIIO_FORCEINLINE simd::vfloat16 clamp (const simd::vfloat16 &a, const simd::vfloat16 &low, const simd::vfloat16 &high)
 
template<>
OIIO_FORCEINLINE simd::vint4 clamp (const simd::vint4 &a, const simd::vint4 &low, const simd::vint4 &high)
 
template<>
OIIO_FORCEINLINE simd::vint8 clamp (const simd::vint8 &a, const simd::vint8 &low, const simd::vint8 &high)
 
template<>
OIIO_FORCEINLINE simd::vint16 clamp (const simd::vint16 &a, const simd::vint16 &low, const simd::vint16 &high)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
madd (float a, float b, float c)
 Fused multiply and add: (a*b + c) More...
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
msub (float a, float b, float c)
 Fused multiply and subtract: (a*b - c) More...
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
nmadd (float a, float b, float c)
 Fused negative multiply and add: -(a*b) + c. More...
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
nmsub (float a, float b, float c)
 Negative fused multiply and subtract: -(a*b) - c. More...
 
template<class T , class Q >
OIIO_FORCEINLINE OIIO_HOSTDEVICElerp (const T &v0, const T &v1, const Q &x)
 
template<class T , class Q >
OIIO_FORCEINLINE OIIO_HOSTDEVICEbilerp (const T &v0, const T &v1, const T &v2, const T &v3, const Q &s, const Q &t)
 
template<class T , class Q >
OIIO_HOSTDEVICE void bilerp (const T *v0, const T *v1, const T *v2, const T *v3, Q s, Q t, int n, T *result)
 
template<class T , class Q >
OIIO_HOSTDEVICE void bilerp_mad (const T *v0, const T *v1, const T *v2, const T *v3, Q s, Q t, Q scale, int n, T *result)
 
template<class T , class Q >
OIIO_FORCEINLINE OIIO_HOSTDEVICEtrilerp (T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, Q s, Q t, Q r)
 
template<class T , class Q >
OIIO_HOSTDEVICE void trilerp (const T *v0, const T *v1, const T *v2, const T *v3, const T *v4, const T *v5, const T *v6, const T *v7, Q s, Q t, Q r, int n, T *result)
 
template<class T , class Q >
OIIO_HOSTDEVICE void trilerp_mad (const T *v0, const T *v1, const T *v2, const T *v3, const T *v4, const T *v5, const T *v6, const T *v7, Q s, Q t, Q r, Q scale, int n, T *result)
 
template<typename T >
OIIO_HOSTDEVICE void evalBSplineWeights (T w[4], T fraction)
 
template<typename T >
OIIO_HOSTDEVICE void evalBSplineWeightDerivs (T dw[4], T fraction)
 
template<class T >
OIIO_HOSTDEVICE void bicubic_interp (const T **val, T s, T t, int n, T *result)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE int 
ifloor (float x)
 Return floor(x) cast to an int. More...
 
OIIO_HOSTDEVICE float floorfrac (float x, int *xint)
 
simd::vfloat4 floorfrac (const simd::vfloat4 &x, simd::vint4 *xint)
 
simd::vfloat8 floorfrac (const simd::vfloat8 &x, simd::vint8 *xint)
 
simd::vfloat16 floorfrac (const simd::vfloat16 &x, simd::vint16 *xint)
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEradians (T deg)
 Convert degrees to radians. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEdegrees (T rad)
 Convert radians to degrees. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEfast_neg (const T &x)
 
OIIO_HOSTDEVICE void sincos (float x, float *sine, float *cosine)
 
OIIO_HOSTDEVICE void sincos (double x, double *sine, double *cosine)
 
OIIO_HOSTDEVICE float sign (float x)
 
template<typename IN_TYPE , typename OUT_TYPE >
OIIO_FORCEINLINE
OIIO_HOSTDEVICE OUT_TYPE 
bit_cast (const IN_TYPE &in)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE int 
bitcast_to_int (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
bitcast_to_float (int x)
 
template<class T >
OIIO_HOSTDEVICE void swap_endian (T *f, int len=1)
 
template<typename S , typename D , typename F >
OIIO_HOSTDEVICEscaled_conversion (const S &src, F scale, F min, F max)
 
template<typename S , typename D >
void convert_type (const S *src, D *dst, size_t n, D _min, D _max)
 
template<>
void convert_type< uint8_t, float > (const uint8_t *src, float *dst, size_t n, float, float)
 
template<>
void convert_type< uint16_t, float > (const uint16_t *src, float *dst, size_t n, float, float)
 
template<>
void convert_type< float, uint16_t > (const float *src, uint16_t *dst, size_t n, uint16_t, uint16_t)
 
template<>
void convert_type< float, uint8_t > (const float *src, uint8_t *dst, size_t n, uint8_t, uint8_t)
 
template<typename S , typename D >
void convert_type (const S *src, D *dst, size_t n)
 
template<typename S , typename D >
convert_type (const S &src)
 
template<unsigned int FROM_BITS, unsigned int TO_BITS>
OIIO_HOSTDEVICE unsigned int bit_range_convert (unsigned int in)
 
OIIO_HOSTDEVICE unsigned int bit_range_convert (unsigned int in, unsigned int FROM_BITS, unsigned int TO_BITS)
 
template<typename T >
void bitstring_add_n_bits (T *&out, int &filled, uint32_t val, int n)
 
template<typename T >
void bit_pack (cspan< T > data, void *out, int outbits)
 
template<typename T >
void bit_unpack (int n, const unsigned char *in, int inbits, T *out)
 
OIIO_HOSTDEVICE void float_to_rational (float f, unsigned int &num, unsigned int &den)
 
OIIO_HOSTDEVICE void float_to_rational (float f, int &num, int &den)
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_sqrt (T x)
 Safe (clamping) sqrt: safe_sqrt(x<0) returns 0, not NaN. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_inversesqrt (T x)
 Safe (clamping) inverse sqrt: safe_inversesqrt(x<=0) returns 0. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_asin (T x)
 Safe (clamping) arcsine: clamp to the valid domain. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_acos (T x)
 Safe (clamping) arccosine: clamp to the valid domain. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_log2 (T x)
 Safe log2: clamp to valid domain. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_log (T x)
 Safe log: clamp to valid domain. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_log10 (T x)
 Safe log10: clamp to valid domain. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_logb (T x)
 Safe logb: clamp to valid domain. More...
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEsafe_pow (T x, T y)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
safe_fmod (float a, float b)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE int 
fast_rint (float x)
 
OIIO_FORCEINLINE simd::vint4 fast_rint (const simd::vfloat4 &x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_sin (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_cos (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE void 
fast_sincos (float x, float *sine, float *cosine)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_tan (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_sinpi (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_cospi (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_acos (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_asin (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_atan (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_atan2 (float y, float x)
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEfast_log2 (const T &xval)
 
template<>
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_log2 (const float &xval)
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEfast_log (const T &x)
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEfast_log10 (const T &x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_logb (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_log1p (float x)
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEfast_exp2 (const T &xval)
 
template<>
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_exp2 (const float &xval)
 
template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICEfast_exp (const T &x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_correct_exp (float x)
 Faster float exp than is in libm, but still 100% accurate. More...
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_exp10 (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_expm1 (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_sinh (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_cosh (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_tanh (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_safe_pow (float x, float y)
 
template<typename T , typename U >
OIIO_FORCEINLINE OIIO_HOSTDEVICEfast_pow_pos (const T &x, const U &y)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_cbrt (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_erf (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_erfc (float x)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE float 
fast_ierf (float x)
 
template<class T , class Func >
OIIO_HOSTDEVICEinvert (Func &func, T y, T xmin=0.0, T xmax=1.0, int maxiters=32, T eps=1.0e-6, bool *brack=0)
 
OIIO_HOSTDEVICE float interpolate_linear (float x, span_strided< const float > y)
 

Detailed Description

A variety of floating-point math helper routines (and, slight misnomer, some int stuff as well).

Definition in file fmath.h.

Macro Definition Documentation

#define M_1_PI   0.318309886183790671537767526745028724

Definition at line 102 of file fmath.h.

#define M_2_PI   0.636619772367581343075535053490057448

Definition at line 105 of file fmath.h.

#define M_E   2.71828182845904523536028747135266250

Definition at line 120 of file fmath.h.

#define M_LN10   2.30258509299404568401799145468436421

Definition at line 117 of file fmath.h.

#define M_LN2   0.69314718055994530941723212145817656

Definition at line 114 of file fmath.h.

#define M_LOG2E   1.44269504088896340735992468100189214

Definition at line 123 of file fmath.h.

#define M_PI   3.14159265358979323846264338327950288

Definition at line 90 of file fmath.h.

#define M_PI_2   1.57079632679489661923132169163975144

Definition at line 93 of file fmath.h.

#define M_PI_4   0.785398163397448309615660845819875721

Definition at line 96 of file fmath.h.

#define M_SQRT1_2   0.707106781186547524400844362104849039
Examples:
RAY/RAY_DemoSprite.C.

Definition at line 111 of file fmath.h.

#define M_SQRT2   1.41421356237309504880168872420969808

Definition at line 108 of file fmath.h.

#define M_TWO_PI   (M_PI * 2.0)

Definition at line 99 of file fmath.h.

#define OIIO_FMATH_H   1

Definition at line 31 of file fmath.h.

#define OIIO_FMATH_HAS_SAFE_FMOD   1

Definition at line 1493 of file fmath.h.

#define OIIO_FMATH_SIMD_FRIENDLY   0

Occasionally there may be a tradeoff where the best/fastest implementation of a math function in an ordinary scalar context does things that prevent autovectorization (or OMP pragma vectorization) of a loop containing that operation, and the only way to make it SIMD friendly slows it down as a scalar op.

By default, we always prefer doing it as correctly and quickly as possible in scalar mode, but if OIIO_FMATH_SIMD_FRIENDLY is defined to be nonzero, we prefer ensuring that the SIMD loops are as efficient as possible, even at the expense of scalar performance.

Because everything here consists of inline functions, and it doesn't really matter for OIIO internals themselves, this is primarily intended for the sake of 3rd party apps that happen to have OIIO as a dependency and use the fmath.h functions. Such a downstream dependency just needs to ensure that OIIO_FMATH_SIMD_FRIENDLY is defined to 1 prior to including fmath.h.

Definition at line 69 of file fmath.h.

Function Documentation

template<class T >
OIIO_HOSTDEVICE void bicubic_interp ( const T **  val,
s,
t,
int  n,
T *  result 
)
inline

Bicubically interoplate arrays of pointers arranged in a 4x4 pattern with val[0] pointing to the data in the upper left corner, val[15] pointing to the lower right) at coordinates (s,t), storing the results in 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t interpolants).

Definition at line 582 of file fmath.h.

template<class T , class Q >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T bilerp ( const T &  v0,
const T &  v1,
const T &  v2,
const T &  v3,
const Q &  s,
const Q &  t 
)

Bilinearly interoplate values v0-v3 (v0 upper left, v1 upper right, v2 lower left, v3 lower right) at coordinates (s,t) and return the result. This is a template, and so should work for any types.

Definition at line 445 of file fmath.h.

template<class T , class Q >
OIIO_HOSTDEVICE void bilerp ( const T *  v0,
const T *  v1,
const T *  v2,
const T *  v3,
s,
t,
int  n,
T *  result 
)
inline

Bilinearly interoplate arrays of values v0-v3 (v0 upper left, v1 upper right, v2 lower left, v3 lower right) at coordinates (s,t), storing the results in 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t interpolants).

Definition at line 460 of file fmath.h.

template<class T , class Q >
OIIO_HOSTDEVICE void bilerp_mad ( const T *  v0,
const T *  v1,
const T *  v2,
const T *  v3,
s,
t,
scale,
int  n,
T *  result 
)
inline

Bilinearly interoplate arrays of values v0-v3 (v0 upper left, v1 upper right, v2 lower left, v3 lower right) at coordinates (s,t), SCALING the interpolated value by 'scale' and then ADDING to 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t interpolants).

Definition at line 479 of file fmath.h.

template<typename IN_TYPE , typename OUT_TYPE >
OIIO_FORCEINLINE OIIO_HOSTDEVICE OUT_TYPE bit_cast ( const IN_TYPE &  in)

Definition at line 737 of file fmath.h.

template<typename T >
void bit_pack ( cspan< T >  data,
void out,
int  outbits 
)
inline

Pack values from T in[0..n-1] (where T is expected to be a uint8, uint16, or uint32, into successive raw outbits-bit pieces of out[], where outbits is expected to be less than the number of bits in a T.

Definition at line 1180 of file fmath.h.

template<unsigned int FROM_BITS, unsigned int TO_BITS>
OIIO_HOSTDEVICE unsigned int bit_range_convert ( unsigned int  in)
inline

Helper function to convert channel values between different bit depths. Roughly equivalent to:

out = round (in * (pow (2, TO_BITS) - 1) / (pow (2, FROM_BITS) - 1));

but utilizes an integer math trick for speed. It can be proven that the absolute error of this method is less or equal to 1, with an average error (with respect to the entire domain) below 0.2.

It is assumed that the original value is a valid FROM_BITS integer, i.e. shifted fully to the right.

Definition at line 1101 of file fmath.h.

OIIO_HOSTDEVICE unsigned int bit_range_convert ( unsigned int  in,
unsigned int  FROM_BITS,
unsigned int  TO_BITS 
)
inline

Definition at line 1114 of file fmath.h.

template<typename T >
void bit_unpack ( int  n,
const unsigned char *  in,
int  inbits,
T *  out 
)
inline

Decode n packed inbits-bits values from in[...] into normal uint8, uint16, or uint32 representation of T out[0..n-1]. In other words, each successive inbits of in (allowing spanning of byte boundaries) will be stored in a successive out[i].

Definition at line 1200 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float bitcast_to_float ( int  x)

Definition at line 784 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE int bitcast_to_int ( float  x)

Definition at line 781 of file fmath.h.

template<typename T >
void bitstring_add_n_bits ( T *&  out,
int filled,
uint32_t  val,
int  n 
)
inline

Append the n LSB bits of val into a bit sting T out[], where the filled MSB bits of *out are already filled in. Increment out and adjust filled as required. Type T should be uint8_t, uint16_t, or uint32_t.

Definition at line 1132 of file fmath.h.

OIIO_HOSTDEVICE OIIO_CONSTEXPR14 int ceil2 ( int  x)
inlinenoexcept

Round up to next higher power of 2 (return x if it's already a power of 2).

Definition at line 153 of file fmath.h.

template<class T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T clamp ( const T &  a,
const T &  low,
const T &  high 
)

clamp a to bounds [low,high].

Definition at line 323 of file fmath.h.

Definition at line 349 of file fmath.h.

Definition at line 355 of file fmath.h.

Definition at line 361 of file fmath.h.

Definition at line 368 of file fmath.h.

Definition at line 374 of file fmath.h.

Definition at line 380 of file fmath.h.

OIIO_HOSTDEVICE uint32_t clamped_mult32 ( uint32_t  a,
uint32_t  b 
)
inline

Multiply two unsigned 32-bit ints safely, carefully checking for overflow, and clamping to uint32_t's maximum value.

Definition at line 223 of file fmath.h.

OIIO_HOSTDEVICE uint64_t clamped_mult64 ( uint64_t  a,
uint64_t  b 
)
inline

Multiply two unsigned 64-bit ints safely, carefully checking for overflow, and clamping to uint64_t's maximum value.

Definition at line 235 of file fmath.h.

template<typename S , typename D >
void convert_type ( const S *  src,
D *  dst,
size_t  n,
_min,
_max 
)

Convert n consecutive values from the type of S to the type of D. The conversion is not a simple cast, but correctly remaps the 0.0->1.0 range from and to the full positive range of integral types. Take a memcpy shortcut if both types are the same and no conversion is necessary. Optional arguments can give nonstandard quantizations.

Definition at line 852 of file fmath.h.

template<typename S , typename D >
void convert_type ( const S *  src,
D *  dst,
size_t  n 
)
inline

Definition at line 1048 of file fmath.h.

template<typename S , typename D >
D convert_type ( const S &  src)
inline

Convert a single value from the type of S to the type of D. The conversion is not a simple cast, but correctly remaps the 0.0->1.0 range from and to the full positive range of integral types. Take a copy shortcut if both types are the same and no conversion is necessary.

Definition at line 1065 of file fmath.h.

template<>
void convert_type< float, uint16_t > ( const float *  src,
uint16_t *  dst,
size_t  n,
uint16_t  ,
uint16_t   
)
inline

Definition at line 980 of file fmath.h.

template<>
void convert_type< float, uint8_t > ( const float *  src,
uint8_t *  dst,
size_t  n,
uint8_t  ,
uint8_t   
)
inline

Definition at line 1001 of file fmath.h.

template<>
void convert_type< uint16_t, float > ( const uint16_t *  src,
float *  dst,
size_t  n,
float  ,
float   
)
inline

Definition at line 936 of file fmath.h.

template<>
void convert_type< uint8_t, float > ( const uint8_t *  src,
float *  dst,
size_t  n,
float  ,
float   
)
inline

Definition at line 918 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T degrees ( rad)

Convert radians to degrees.

Definition at line 656 of file fmath.h.

template<typename T >
OIIO_HOSTDEVICE void evalBSplineWeightDerivs ( dw[4],
fraction 
)
inline

Evaluate B-spline derivative weights in w[0..3] for the given fraction. This is an important component of performing a cubic interpolation with derivatives.

Definition at line 564 of file fmath.h.

template<typename T >
OIIO_HOSTDEVICE void evalBSplineWeights ( w[4],
fraction 
)
inline

Evaluate B-spline weights in w[0..3] for the given fraction. This is an important component of performing a cubic interpolation.

Definition at line 550 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_acos ( float  x)

Definition at line 1727 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_asin ( float  x)

Definition at line 1742 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_atan ( float  x)

Definition at line 1755 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_atan2 ( float  y,
float  x 
)

Definition at line 1774 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_cbrt ( float  x)

Definition at line 2128 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_correct_exp ( float  x)

Faster float exp than is in libm, but still 100% accurate.

Definition at line 2007 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_cos ( float  x)

Definition at line 1587 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_cosh ( float  x)

Definition at line 2063 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_cospi ( float  x)

Fast approximate cos(x*M_PI) with ~0.1% absolute error. Note that this is MUCH faster, but much less accurate than fast_cos.

Definition at line 1718 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_erf ( float  x)

Definition at line 2145 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_erfc ( float  x)

Definition at line 2169 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T fast_exp ( const T &  x)

Definition at line 1992 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_exp10 ( float  x)

Definition at line 2019 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T fast_exp2 ( const T &  xval)

Definition at line 1921 of file fmath.h.

template<>
OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_exp2 ( const float &  xval)

Definition at line 1954 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_expm1 ( float  x)

Definition at line 2028 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_ierf ( float  x)

Definition at line 2181 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T fast_log ( const T &  x)

Definition at line 1864 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T fast_log10 ( const T &  x)

Definition at line 1879 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_log1p ( float  x)

Definition at line 1905 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T fast_log2 ( const T &  xval)

Definition at line 1813 of file fmath.h.

template<>
OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_log2 ( const float &  xval)

Definition at line 1834 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_logb ( float  x)

Definition at line 1892 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T fast_neg ( const T &  x)

Faster floating point negation, in cases where you aren't too picky about the difference between +0 and -0. (Courtesy of Alex Wells, Intel, via code in OSL.)

Beware: fast_neg(0.0f) returns 0.0f, NOT -0.0f. All other values, including -0.0 and +/- Inf, work identically to -x. For many use cases, that's fine. (When was the last time you wanted a -0.0f anyway?)

The reason it's faster is that -x (for float x) is implemented by compilers by xor'ing x against a bitmask that flips the sign bit, and that bitmask had to come from somewhere – memory! – which can be expensive, depending on whether/where it is in cache. But 0 - x generates a zero in a register (with xor, no memory access needed) and then subtracts, so that's sometimes faster because there is no memory read. This works for SIMD types as well!

It's also safe (though pointless) to use fast_neg for integer types, where both -x and 0-x are implemented as a neg instruction.

Definition at line 679 of file fmath.h.

template<typename T , typename U >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T fast_pow_pos ( const T &  x,
const U &  y 
)

Definition at line 2122 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE int fast_rint ( float  x)

Round to nearest integer, returning as an int. Note that this differs from std::rint, which returns a float; it's more akin to std::lrint, which returns a long (int). Sorry for the naming confusion.

Definition at line 1537 of file fmath.h.

Definition at line 1549 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_safe_pow ( float  x,
float  y 
)

Definition at line 2084 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_sin ( float  x)

Definition at line 1555 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE void fast_sincos ( float  x,
float *  sine,
float *  cosine 
)

Definition at line 1614 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_sinh ( float  x)

Definition at line 2040 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_sinpi ( float  x)

Fast, approximate sin(x*M_PI) with maximum absolute error of 0.000918954611. Adapted from http://devmaster.net/posts/9648/fast-and-accurate-sine-cosine#comment-76773 Note that this is MUCH faster, but much less accurate than fast_sin.

Definition at line 1680 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_tan ( float  x)

Definition at line 1648 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float fast_tanh ( float  x)

Definition at line 2073 of file fmath.h.

OIIO_HOSTDEVICE void float_to_rational ( float  f,
unsigned int num,
unsigned int den 
)
inline

Simple conversion of a (presumably non-negative) float into a rational. This does not attempt to find the simplest fraction that approximates the float, for example 52.83 will simply return 5283/100. This does not attempt to gracefully handle floats that are out of range that could be easily int/int.

Definition at line 1339 of file fmath.h.

OIIO_HOSTDEVICE void float_to_rational ( float  f,
int num,
int den 
)
inline

Simple conversion of a float into a rational. This does not attempt to find the simplest fraction that approximates the float, for example 52.83 will simply return 5283/100. This does not attempt to gracefully handle floats that are out of range that could be easily int/int.

Definition at line 1366 of file fmath.h.

OIIO_HOSTDEVICE OIIO_CONSTEXPR14 int floor2 ( int  x)
inlinenoexcept

Round down to next lower power of 2 (return x if it's already a power of 2).

Definition at line 176 of file fmath.h.

OIIO_HOSTDEVICE float floorfrac ( float  x,
int xint 
)
inline

Return (x-floor(x)) and put (int)floor(x) in *xint. This is similar to the built-in modf, but returns a true int, always rounds down (compared to modf which rounds toward 0), and always returns frac >= 0 (comapred to modf which can return <0 if x<0).

Definition at line 613 of file fmath.h.

simd::vfloat4 floorfrac ( const simd::vfloat4 x,
simd::vint4 xint 
)
inline

Definition at line 628 of file fmath.h.

simd::vfloat8 floorfrac ( const simd::vfloat8 x,
simd::vint8 xint 
)
inline

Definition at line 634 of file fmath.h.

simd::vfloat16 floorfrac ( const simd::vfloat16 x,
simd::vint16 xint 
)
inline

Definition at line 640 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE int ifloor ( float  x)

Return floor(x) cast to an int.

Definition at line 601 of file fmath.h.

OIIO_HOSTDEVICE float interpolate_linear ( float  x,
span_strided< const float >  y 
)
inline

Linearly interpolate a list of evenly-spaced knots y[0..len-1] with y[0] corresponding to the value at x==0.0 and y[len-1] corresponding to x==1.0.

Definition at line 2288 of file fmath.h.

template<class T , class Func >
OIIO_HOSTDEVICE T invert ( Func &  func,
y,
xmin = 0.0,
xmax = 1.0,
int  maxiters = 32,
eps = 1.0e-6,
bool *  brack = 0 
)

Solve for the x for which func(x) == y on the interval [xmin,xmax]. Use a maximum of maxiter iterations, and stop any time the remaining search interval or the function evaluations <= eps. If brack is non-NULL, set it to true if y is in [f(xmin), f(xmax)], otherwise false (in which case the caller should know that the results may be unreliable. Results are undefined if the function is not monotonic on that interval or if there are multiple roots in the interval (it may not converge, or may converge to any of the roots without telling you that there are more than one).

Definition at line 2236 of file fmath.h.

template<typename T >
OIIO_HOSTDEVICE OIIO_CONSTEXPR14 bool ispow2 ( x)
inlinenoexcept

Quick test for whether an integer is a power of 2.

Definition at line 140 of file fmath.h.

template<class T , class Q >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T lerp ( const T &  v0,
const T &  v1,
const Q &  x 
)

Linearly interpolate values v0-v1 at x: v0*(1-x) + v1*x. This is a template, and so should work for any types.

Definition at line 432 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float madd ( float  a,
float  b,
float  c 
)

Fused multiply and add: (a*b + c)

Definition at line 396 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float msub ( float  a,
float  b,
float  c 
)

Fused multiply and subtract: (a*b - c)

Definition at line 405 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float nmadd ( float  a,
float  b,
float  c 
)

Fused negative multiply and add: -(a*b) + c.

Definition at line 413 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float nmsub ( float  a,
float  b,
float  c 
)

Negative fused multiply and subtract: -(a*b) - c.

Definition at line 421 of file fmath.h.

OIIO_HOSTDEVICE int pow2rounddown ( int  x)
inline

Definition at line 192 of file fmath.h.

OIIO_HOSTDEVICE int pow2roundup ( int  x)
inline

Definition at line 191 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T radians ( deg)

Convert degrees to radians.

Definition at line 652 of file fmath.h.

template<class T >
OIIO_NODISCARD OIIO_FORCEINLINE OIIO_HOSTDEVICE T rotl ( x,
int  s 
)
noexcept

Bitwise circular rotation left by s bits (for any unsigned integer type). For info on the C++20 std::rotl(), see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0553r4.html

Definition at line 256 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE uint32_t rotl32 ( uint32_t  x,
int  k 
)

Definition at line 281 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE uint64_t rotl64 ( uint64_t  x,
int  k 
)

Definition at line 291 of file fmath.h.

template<typename V , typename M >
OIIO_HOSTDEVICE V round_to_multiple ( value,
multiple 
)
inline

Round value up to the next whole multiple. For example, round_to_multiple(7,10) returns 10.

Definition at line 199 of file fmath.h.

template<typename T >
OIIO_HOSTDEVICE T round_to_multiple_of_pow2 ( x,
m 
)
inline

Round up to the next whole multiple of m, for the special case where m is definitely a power of 2 (somewhat simpler than the more general round_to_multiple). This is a template that should work for any integer type.

Definition at line 212 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_acos ( x)

Safe (clamping) arccosine: clamp to the valid domain.

Definition at line 1416 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_asin ( x)

Safe (clamping) arcsine: clamp to the valid domain.

Definition at line 1408 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE float safe_fmod ( float  a,
float  b 
)

Safe fmod: guard against b==0.0 (in which case, return 0.0). Also, it seems that this is much faster than std::fmod!

Definition at line 1474 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_inversesqrt ( x)

Safe (clamping) inverse sqrt: safe_inversesqrt(x<=0) returns 0.

Definition at line 1401 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_log ( x)

Safe log: clamp to valid domain.

Definition at line 1434 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_log10 ( x)

Safe log10: clamp to valid domain.

Definition at line 1443 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_log2 ( x)

Safe log2: clamp to valid domain.

Definition at line 1425 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_logb ( x)

Safe logb: clamp to valid domain.

Definition at line 1452 of file fmath.h.

template<class T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_mod ( a,
b 
)

Definition at line 301 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_pow ( x,
y 
)

Safe pow: clamp the domain so it never returns Inf or NaN or has divide by zero error.

Definition at line 1459 of file fmath.h.

template<typename T >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T safe_sqrt ( x)

Safe (clamping) sqrt: safe_sqrt(x<0) returns 0, not NaN.

Definition at line 1395 of file fmath.h.

template<typename S , typename D , typename F >
OIIO_HOSTDEVICE D scaled_conversion ( const S &  src,
scale,
min,
max 
)
inline

Multiply src by scale, clamp to [min,max], and round to the nearest D (presumed to be integer). This is just a helper for the convert_type templates, it probably has no other use.

Definition at line 829 of file fmath.h.

OIIO_HOSTDEVICE float sign ( float  x)
inline

Definition at line 715 of file fmath.h.

OIIO_HOSTDEVICE void sincos ( float  x,
float *  sine,
float *  cosine 
)
inline

Definition at line 686 of file fmath.h.

OIIO_HOSTDEVICE void sincos ( double  x,
double *  sine,
double *  cosine 
)
inline

Definition at line 700 of file fmath.h.

template<class T >
OIIO_HOSTDEVICE void swap_endian ( T *  f,
int  len = 1 
)
inline

Change endian-ness of one or more data items that are each 2, 4, or 8 bytes. This should work for any of short, unsigned short, int, unsigned int, float, long long, pointers.

Definition at line 795 of file fmath.h.

template<class T , class Q >
OIIO_FORCEINLINE OIIO_HOSTDEVICE T trilerp ( v0,
v1,
v2,
v3,
v4,
v5,
v6,
v7,
s,
t,
r 
)

Trilinearly interoplate arrays of values v0-v7 (v0 upper left top, v1 upper right top, ...) at coordinates (s,t,r), and return the result. This is a template, and so should work for any types.

Definition at line 497 of file fmath.h.

template<class T , class Q >
OIIO_HOSTDEVICE void trilerp ( const T *  v0,
const T *  v1,
const T *  v2,
const T *  v3,
const T *  v4,
const T *  v5,
const T *  v6,
const T *  v7,
s,
t,
r,
int  n,
T *  result 
)
inline

Trilinearly interoplate arrays of values v0-v7 (v0 upper left top, v1 upper right top, ...) at coordinates (s,t,r), storing the results in 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t,r interpolants).

Definition at line 515 of file fmath.h.

template<class T , class Q >
OIIO_HOSTDEVICE void trilerp_mad ( const T *  v0,
const T *  v1,
const T *  v2,
const T *  v3,
const T *  v4,
const T *  v5,
const T *  v6,
const T *  v7,
s,
t,
r,
scale,
int  n,
T *  result 
)
inline

Trilinearly interoplate arrays of values v0-v7 (v0 upper left top, v1 upper right top, ...) at coordinates (s,t,r), SCALING the interpolated value by 'scale' and then ADDING to 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t,r interpolants).

Definition at line 536 of file fmath.h.