src/include/fmath.h File Reference

#include <cmath>
#include <limits>
#include <typeinfo>
#include <algorithm>
#include <stdint.h>

Go to the source code of this file.

Classes

class  EightBitConverter< T >
struct  is_same< T, U >
struct  is_same< T, T >
struct  DataProxy< I, E >
struct  ConstDataProxy< I, E >
struct  DataArrayProxy< I, E >
struct  ConstDataArrayProxy< I, E >

Defines

#define M_PI   3.1415926535897932
#define M_PI_2   1.5707963267948966
#define M_TWO_PI   (M_PI * 2.0)
#define M_SQRT2   1.414135623730950
#define M_SQRT1_2   0.7071067811865475
#define M_LN2   0.6931471805599453
#define M_LN10   2.3025850929940457
#define HUGE_FLOAT   ((float)1.0e38)
#define UNINITIALIZED_FLOAT   (- std::numeric_limits<float>::max())

Functions

bool huge (float f)
bool ispow2 (int x)
bool ispow2 (unsigned int x)
int pow2roundup (int x)
int pow2rounddown (int x)
bool littleendian (void)
bool bigendian (void)
template<class T >
void swap_endian (T *f, int len=1)
template<class T >
clamp (T a, T l, T h)
uint32_t clamped_mult32 (uint32_t a, uint32_t b)
uint64_t clamped_mult64 (uint64_t a, uint64_t b)
template<typename S , typename D >
void convert_type (const S *src, D *dst, size_t n, D _zero=0, D _one=1, D _min=std::numeric_limits< D >::min(), D _max=std::numeric_limits< D >::max())
template<typename S , typename D >
convert_type (const S &src)
template<class T , class Q >
bilerp (T v0, T v1, T v2, T v3, Q s, Q t)
template<class T , class Q >
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 >
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)
int RoundToInt (double val)
int RoundToInt (float val)
int FloorToInt (double val)
int FloorToInt (float val)
int CeilToInt (double val)
int CeilToInt (float val)
int FloatToInt (double val)
int FloatToInt (float val)
float floorfrac (float x, int *xint)
float radians (float deg)
float degrees (float rad)
void float_to_rational (float f, unsigned int &num, unsigned int &den)
void float_to_rational (float f, int &num, int &den)

Detailed Description

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


Define Documentation

#define HUGE_FLOAT   ((float)1.0e38)

Large constant that we use to indicate a really large float

#define M_LN10   2.3025850929940457

ln(10)

#define M_LN2   0.6931471805599453

ln(2)

#define M_PI   3.1415926535897932

PI

#define M_PI_2   1.5707963267948966

PI / 2

#define M_SQRT1_2   0.7071067811865475

1/sqrt(2)

#define M_SQRT2   1.414135623730950

sqrt(2)

#define M_TWO_PI   (M_PI * 2.0)

PI * 2

#define UNINITIALIZED_FLOAT   (- std::numeric_limits<float>::max())

Special value we can use for an uninitialized float.


Function Documentation

bool bigendian ( void   )  [inline]

Return true if the architecture we are running on is big endian

template<class T , class Q >
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).

template<class T , class Q >
T bilerp ( v0,
v1,
v2,
v3,
s,
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.

template<class T , class Q >
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).

int CeilToInt ( double  val  )  [inline]

Fast (int)ceil(val) See Michael Herf's "Know Your FPU" page: http://www.stereopsis.com/sree/fpu2006.html

template<class T >
T clamp ( a,
l,
h 
) [inline]

clamp a to bounds [l,h].

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.

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.

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.

template<typename S , typename D >
void convert_type ( const S *  src,
D *  dst,
size_t  n,
_zero = 0,
_one = 1,
_min = std::numeric_limits<D>::min(),
_max = std::numeric_limits<D>::max() 
) [inline]

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.

float degrees ( float  rad  )  [inline]

Convert radians to degrees

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.

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.

int FloatToInt ( double  val  )  [inline]

Fast (int)val See Michael Herf's "Know Your FPU" page: http://www.stereopsis.com/sree/fpu2006.html

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).

int FloorToInt ( double  val  )  [inline]

Fast (int)floor(val) See Michael Herf's "Know Your FPU" page: http://www.stereopsis.com/sree/fpu2006.html

bool huge ( float  f  )  [inline]

Test a float for whether it's huge. To account for awful fp roundoff, consider it large if within a factor of 2 of HUGE_FLOAT.

bool ispow2 ( unsigned int  x  )  [inline]

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

bool ispow2 ( int  x  )  [inline]

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

bool littleendian ( void   )  [inline]

Return true if the architecture we are running on is little endian

int pow2rounddown ( int  x  )  [inline]

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

int pow2roundup ( int  x  )  [inline]

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

float radians ( float  deg  )  [inline]

Convert degrees to radians.

int RoundToInt ( double  val  )  [inline]

Fast rounding to nearest integer. See Michael Herf's "Know Your FPU" page: http://www.stereopsis.com/sree/fpu2006.html

template<class T >
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.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 27 06:30:40 2009 for OpenImageIO by  doxygen 1.6.1