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 <OpenImageIO/span.h>
#include <OpenImageIO/dassert.h>
#include <OpenImageIO/missing_math.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  is_same< T, U >
 Helper template to let us tell if two types are the same. More...
 
struct  is_same< T, T >
 
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
 

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)
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE uint32_t 
rotl32 (uint32_t x, int k)
 Bitwise circular rotation left by k bits (for 32 bit unsigned integers) More...
 
OIIO_FORCEINLINE
OIIO_HOSTDEVICE uint64_t 
rotl64 (uint64_t x, int k)
 Bitwise circular rotation left by k bits (for 64 bit unsigned integers) More...
 
template<class T >
OIIO_HOSTDEVICEsafe_mod (T a, T b)
 
template<class T >
OIIO_HOSTDEVICEclamp (const T &a, const T &low, const T &high)
 clamp a to bounds [low,high]. More...
 
template<>
simd::vfloat4 clamp (const simd::vfloat4 &a, const simd::vfloat4 &low, const simd::vfloat4 &high)
 
template<>
simd::vfloat8 clamp (const simd::vfloat8 &a, const simd::vfloat8 &low, const simd::vfloat8 &high)
 
OIIO_HOSTDEVICE float madd (float a, float b, float c)
 Fused multiply and add: (a*b + c) More...
 
OIIO_HOSTDEVICE float msub (float a, float b, float c)
 Fused multiply and subtract: -(a*b - c) More...
 
OIIO_HOSTDEVICE float nmadd (float a, float b, float c)
 Fused negative multiply and add: -(a*b) + c. More...
 
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_HOSTDEVICElerp (const T &v0, const T &v1, const Q &x)
 
template<class T , class Q >
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_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_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_HOSTDEVICEradians (T deg)
 Convert degrees to radians. More...
 
template<typename T >
OIIO_HOSTDEVICEdegrees (T rad)
 Convert radians to degrees. More...
 
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_HOSTDEVICE OUT_TYPE bit_cast (const IN_TYPE in)
 
OIIO_HOSTDEVICE int bitcast_to_int (float x)
 
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 _min, float _max)
 
template<>
void convert_type< uint16_t, float > (const uint16_t *src, float *dst, size_t n, float _min, float _max)
 
template<>
void convert_type< float, uint16_t > (const float *src, uint16_t *dst, size_t n, uint16_t _min, uint16_t _max)
 
template<>
void convert_type< float, uint8_t > (const float *src, uint8_t *dst, size_t n, uint8_t _min, uint8_t _max)
 
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)
 
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_HOSTDEVICEsafe_sqrt (T x)
 Safe (clamping) sqrt: safe_sqrt(x<0) returns 0, not NaN. More...
 
template<typename T >
OIIO_HOSTDEVICEsafe_inversesqrt (T x)
 Safe (clamping) inverse sqrt: safe_inversesqrt(x<=0) returns 0. More...
 
template<typename T >
OIIO_HOSTDEVICEsafe_asin (T x)
 Safe (clamping) arcsine: clamp to the valid domain. More...
 
template<typename T >
OIIO_HOSTDEVICEsafe_acos (T x)
 Safe (clamping) arccosine: clamp to the valid domain. More...
 
template<typename T >
OIIO_HOSTDEVICEsafe_log2 (T x)
 Safe log2: clamp to valid domain. More...
 
template<typename T >
OIIO_HOSTDEVICEsafe_log (T x)
 Safe log: clamp to valid domain. More...
 
template<typename T >
OIIO_HOSTDEVICEsafe_log10 (T x)
 Safe log10: clamp to valid domain. More...
 
template<typename T >
OIIO_HOSTDEVICEsafe_logb (T x)
 Safe logb: clamp to valid domain. More...
 
template<typename T >
OIIO_HOSTDEVICEsafe_pow (T x, T y)
 
OIIO_HOSTDEVICE int fast_rint (float x)
 Round to nearest integer, returning as an int. More...
 
simd::vint4 fast_rint (const simd::vfloat4 &x)
 
OIIO_HOSTDEVICE float fast_sin (float x)
 
OIIO_HOSTDEVICE float fast_cos (float x)
 
OIIO_HOSTDEVICE void fast_sincos (float x, float *sine, float *cosine)
 
OIIO_HOSTDEVICE float fast_tan (float x)
 
OIIO_HOSTDEVICE float fast_sinpi (float x)
 
OIIO_HOSTDEVICE float fast_cospi (float x)
 
OIIO_HOSTDEVICE float fast_acos (float x)
 
OIIO_HOSTDEVICE float fast_asin (float x)
 
OIIO_HOSTDEVICE float fast_atan (float x)
 
OIIO_HOSTDEVICE float fast_atan2 (float y, float x)
 
template<typename T >
OIIO_HOSTDEVICEfast_log2 (const T &xval)
 
template<>
OIIO_HOSTDEVICE float fast_log2 (const float &xval)
 
template<typename T >
OIIO_HOSTDEVICEfast_log (const T &x)
 
template<typename T >
OIIO_HOSTDEVICEfast_log10 (const T &x)
 
OIIO_HOSTDEVICE float fast_logb (float x)
 
OIIO_HOSTDEVICE float fast_log1p (float x)
 
template<typename T >
OIIO_HOSTDEVICEfast_exp2 (const T &xval)
 
template<>
OIIO_HOSTDEVICE float fast_exp2 (const float &xval)
 
template<typename T >
OIIO_HOSTDEVICEfast_exp (const T &x)
 
OIIO_HOSTDEVICE float fast_correct_exp (float x)
 Faster float exp than is in libm, but still 100% accurate. More...
 
OIIO_HOSTDEVICE float fast_exp10 (float x)
 
OIIO_HOSTDEVICE float fast_expm1 (float x)
 
OIIO_HOSTDEVICE float fast_sinh (float x)
 
OIIO_HOSTDEVICE float fast_cosh (float x)
 
OIIO_HOSTDEVICE float fast_tanh (float x)
 
OIIO_HOSTDEVICE float fast_safe_pow (float x, float y)
 
template<typename T , typename U >
OIIO_HOSTDEVICEfast_pow_pos (const T &x, const U &y)
 
OIIO_HOSTDEVICE float fast_cbrt (float x)
 
OIIO_HOSTDEVICE float fast_erf (float x)
 
OIIO_HOSTDEVICE float fast_erfc (float x)
 
OIIO_HOSTDEVICE float fast_ierf (float x)
 
template<class T , class Func >
invert (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 OIIO_FMATH_H   1

Definition at line 54 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 455 of file fmath.h.

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

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 318 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 333 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 352 of file fmath.h.

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

Definition at line 583 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 908 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 921 of file fmath.h.

OIIO_HOSTDEVICE float bitcast_to_float ( int  x)
inline

Definition at line 593 of file fmath.h.

OIIO_HOSTDEVICE int bitcast_to_int ( float  x)
inline

Definition at line 592 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 105 of file fmath.h.

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

clamp a to bounds [low,high].

Definition at line 243 of file fmath.h.

template<>
simd::vfloat4 clamp ( const simd::vfloat4 a,
const simd::vfloat4 low,
const simd::vfloat4 high 
)
inline

Definition at line 252 of file fmath.h.

template<>
simd::vfloat8 clamp ( const simd::vfloat8 a,
const simd::vfloat8 low,
const simd::vfloat8 high 
)
inline

Definition at line 258 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 175 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 187 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 659 of file fmath.h.

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

Definition at line 855 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 872 of file fmath.h.

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

Definition at line 787 of file fmath.h.

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

Definition at line 808 of file fmath.h.

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

Definition at line 743 of file fmath.h.

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

Definition at line 725 of file fmath.h.

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

Convert radians to degrees.

Definition at line 529 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 437 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 423 of file fmath.h.

OIIO_HOSTDEVICE float fast_acos ( float  x)
inline

Definition at line 1383 of file fmath.h.

OIIO_HOSTDEVICE float fast_asin ( float  x)
inline

Definition at line 1398 of file fmath.h.

OIIO_HOSTDEVICE float fast_atan ( float  x)
inline

Definition at line 1411 of file fmath.h.

OIIO_HOSTDEVICE float fast_atan2 ( float  y,
float  x 
)
inline

Definition at line 1430 of file fmath.h.

OIIO_HOSTDEVICE float fast_cbrt ( float  x)
inline

Definition at line 1762 of file fmath.h.

OIIO_HOSTDEVICE float fast_correct_exp ( float  x)
inline

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

Definition at line 1641 of file fmath.h.

OIIO_HOSTDEVICE float fast_cos ( float  x)
inline

Definition at line 1241 of file fmath.h.

OIIO_HOSTDEVICE float fast_cosh ( float  x)
inline

Definition at line 1697 of file fmath.h.

OIIO_HOSTDEVICE float fast_cospi ( float  x)
inline

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 1374 of file fmath.h.

OIIO_HOSTDEVICE float fast_erf ( float  x)
inline

Definition at line 1779 of file fmath.h.

OIIO_HOSTDEVICE float fast_erfc ( float  x)
inline

Definition at line 1803 of file fmath.h.

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

Definition at line 1626 of file fmath.h.

OIIO_HOSTDEVICE float fast_exp10 ( float  x)
inline

Definition at line 1653 of file fmath.h.

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

Definition at line 1563 of file fmath.h.

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

Definition at line 1596 of file fmath.h.

OIIO_HOSTDEVICE float fast_expm1 ( float  x)
inline

Definition at line 1662 of file fmath.h.

OIIO_HOSTDEVICE float fast_ierf ( float  x)
inline

Definition at line 1815 of file fmath.h.

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

Definition at line 1506 of file fmath.h.

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

Definition at line 1521 of file fmath.h.

OIIO_HOSTDEVICE float fast_log1p ( float  x)
inline

Definition at line 1547 of file fmath.h.

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

Definition at line 1455 of file fmath.h.

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

Definition at line 1476 of file fmath.h.

OIIO_HOSTDEVICE float fast_logb ( float  x)
inline

Definition at line 1534 of file fmath.h.

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

Definition at line 1756 of file fmath.h.

OIIO_HOSTDEVICE int fast_rint ( float  x)
inline

Round to nearest integer, returning as an int.

Definition at line 1190 of file fmath.h.

simd::vint4 fast_rint ( const simd::vfloat4 x)
inline

Definition at line 1202 of file fmath.h.

OIIO_HOSTDEVICE float fast_safe_pow ( float  x,
float  y 
)
inline

Definition at line 1718 of file fmath.h.

OIIO_HOSTDEVICE float fast_sin ( float  x)
inline

Definition at line 1208 of file fmath.h.

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

Definition at line 1269 of file fmath.h.

OIIO_HOSTDEVICE float fast_sinh ( float  x)
inline

Definition at line 1674 of file fmath.h.

OIIO_HOSTDEVICE float fast_sinpi ( float  x)
inline

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 1336 of file fmath.h.

OIIO_HOSTDEVICE float fast_tan ( float  x)
inline

Definition at line 1305 of file fmath.h.

OIIO_HOSTDEVICE float fast_tanh ( float  x)
inline

Definition at line 1707 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 1026 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 1053 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 128 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 486 of file fmath.h.

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

Definition at line 501 of file fmath.h.

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

Definition at line 507 of file fmath.h.

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

Definition at line 513 of file fmath.h.

OIIO_HOSTDEVICE int ifloor ( float  x)
inline

Return floor(x) cast to an int.

Definition at line 474 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 1922 of file fmath.h.

template<class T , class Func >
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 1870 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 92 of file fmath.h.

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

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 305 of file fmath.h.

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

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

Definition at line 267 of file fmath.h.

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

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

Definition at line 281 of file fmath.h.

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

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

Definition at line 288 of file fmath.h.

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

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

Definition at line 295 of file fmath.h.

OIIO_HOSTDEVICE int pow2rounddown ( int  x)
inline

Definition at line 144 of file fmath.h.

OIIO_HOSTDEVICE int pow2roundup ( int  x)
inline

Definition at line 143 of file fmath.h.

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

Convert degrees to radians.

Definition at line 525 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE uint32_t rotl32 ( uint32_t  x,
int  k 
)

Bitwise circular rotation left by k bits (for 32 bit unsigned integers)

Definition at line 200 of file fmath.h.

OIIO_FORCEINLINE OIIO_HOSTDEVICE uint64_t rotl64 ( uint64_t  x,
int  k 
)

Bitwise circular rotation left by k bits (for 64 bit unsigned integers)

Definition at line 211 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 151 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

Definition at line 164 of file fmath.h.

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

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

Definition at line 1103 of file fmath.h.

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

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

Definition at line 1095 of file fmath.h.

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

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

Definition at line 1088 of file fmath.h.

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

Safe log: clamp to valid domain.

Definition at line 1121 of file fmath.h.

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

Safe log10: clamp to valid domain.

Definition at line 1130 of file fmath.h.

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

Safe log2: clamp to valid domain.

Definition at line 1112 of file fmath.h.

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

Safe logb: clamp to valid domain.

Definition at line 1139 of file fmath.h.

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

Definition at line 221 of file fmath.h.

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

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

Definition at line 1146 of file fmath.h.

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

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

Definition at line 1082 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 636 of file fmath.h.

OIIO_HOSTDEVICE float sign ( float  x)
inline

Definition at line 563 of file fmath.h.

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

Definition at line 534 of file fmath.h.

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

Definition at line 548 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 602 of file fmath.h.

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

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 370 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 388 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 409 of file fmath.h.