v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
Classes | Macros | Typedefs | Functions
Math

Floating point and integer math, matrices, vectors. More...

Classes

struct  Sifteo::AffineMatrix
 An augmented 3x2 matrix, for doing 2D affine transforms. More...
 
struct  Sifteo::Random
 Pseudo-random number generator. More...
 
struct  Sifteo::Vector2< T >
 Generalized two-element cartesian coordinate vector. More...
 
struct  Sifteo::Vector3< T >
 Generalized three-element cartesian coordinate vector. More...
 

Macros

#define M_1_PI   0.318309886183790671537767526745028724
 1/pi
 
#define M_2_PI   0.636619772367581343075535053490057448
 2/pi
 
#define M_2_SQRTPI   1.12837916709551257389615890312154517
 2/sqrt(pi)
 
#define M_E   2.71828182845904523536028747135266250
 e
 
#define M_LN10   2.30258509299404568401799145468436421
 log e10
 
#define M_LN2   0.693147180559945309417232121458176568
 log e2
 
#define M_LOG10E   0.434294481903251827651128918916605082
 log 10e
 
#define M_LOG2E   1.44269504088896340735992468100189214
 log 2e
 
#define M_PI   3.14159265358979323846264338327950288
 pi
 
#define M_PI_2   1.57079632679489661923132169163975144
 pi/2
 
#define M_PI_4   0.785398163397448309615660845819875721
 pi/4
 
#define M_SQRT1_2   0.707106781186547524400844362104849039
 1/sqrt(2)
 
#define M_SQRT2   1.41421356237309504880168872420969808
 sqrt(2)
 
#define M_TAU   6.2831853071795862
 2 * pi
 
#define MAXFLOAT   ((float)3.40282346638528860e+38)
 Largest single-precision float value.
 
#define NAN   __builtin_nanf("0x7fc00000")
 Not a Number constant.
 

Typedefs

typedef Vector2< int8_t > Sifteo::Byte2
 Typedef for a 2-vector of bytes.
 
typedef Vector3< int8_t > Sifteo::Byte3
 Typedef for a 2-vector of bytes.
 
typedef Vector2< double > Sifteo::Double2
 Typedef for a 2-vector of double-precision floats.
 
typedef Vector3< double > Sifteo::Double3
 Typedef for a 2-vector of double-precision floats.
 
typedef Vector2< float > Sifteo::Float2
 Typedef for a 2-vector of floats.
 
typedef Vector3< float > Sifteo::Float3
 Typedef for a 2-vector of floats.
 
typedef Vector2< int > Sifteo::Int2
 Typedef for a 2-vector of ints.
 
typedef Vector3< int > Sifteo::Int3
 Typedef for a 2-vector of ints.
 
typedef Vector2< short > Sifteo::Short2
 Typedef for a 2-vector of shorts.
 
typedef Vector3< short > Sifteo::Short3
 Typedef for a 2-vector of shorts.
 
typedef Vector2< uint8_t > Sifteo::UByte2
 Typedef for a 2-vector of unsigned bytes.
 
typedef Vector3< uint8_t > Sifteo::UByte3
 Typedef for a 2-vector of unsigned bytes.
 
typedef Vector2< unsigned > Sifteo::UInt2
 Typedef for a 2-vector of unsigned ints.
 
typedef Vector3< unsigned > Sifteo::UInt3
 Typedef for a 2-vector of unsigned ints.
 
typedef Vector2< unsigned short > Sifteo::UShort2
 Typedef for a 2-vector of unsigned shorts.
 
typedef Vector3< unsigned short > Sifteo::UShort3
 Typedef for a 2-vector of unsigned shorts.
 

Functions

template<typename T >
Sifteo::abs (const T &value)
 For any type, return the absolute value. More...
 
template<typename T >
bool Sifteo::almostEqual (const T a, const T b, const T epsilon)
 Returns true when 'a' and 'b' are within 'epsilon' of each other.
 
float Sifteo::atan (float x)
 Single-precision arc tangent function, of one variable.
 
float Sifteo::atan2 (float a, float b)
 Single-precision arc tangent function, of two variables. More...
 
template<typename T >
Sifteo::bitRange (int begin, int end)
 Return a value of type T which has bits set in the half-open interval [begin, end). More...
 
template<typename T >
long Sifteo::ceil (const T value)
 Returns the next integer value closer to positive infinity from 'value'. More...
 
template<typename T >
Sifteo::ceildiv (T numerator, T denominator)
 Ceiling division. Divide, rounding up instead of down.
 
template<typename T >
Sifteo::clamp (const T &value, const T &low, const T &high)
 For any type, clamp a value to the extremes 'low' and 'high'. More...
 
unsigned Sifteo::clz (uint32_t word)
 Count leading zeroes in a 32-bit word. More...
 
float Sifteo::cos (float x)
 Calculate the cosine of a specified angle, in radians. Single-precision. More...
 
template<typename T >
Sifteo::cross (T u, T v)
 Vector cross-product.
 
template<typename T >
Sifteo::dot (Vector2< T > u, Vector2< T > v)
 Vector dot-product.
 
template<typename T >
Sifteo::dot (Vector3< T > u, Vector3< T > v)
 Vector dot-product.
 
unsigned Sifteo::ffs (uint32_t word)
 Find first bit set in a 32-bit word. More...
 
template<typename T >
long Sifteo::floor (const T value)
 Returns the next integer value closer to negative infinity from 'value'. More...
 
float Sifteo::fmod (float a, float b)
 Compute the remainder (modulo) operation for two floating point numbers. Single-precision.
 
double Sifteo::fmod (double a, double b)
 Compute the remainder (modulo) operation for two floating point numbers. Double-precision.
 
bool Sifteo::isunordered (float a, float b=0.f)
 Unordered comparison. More...
 
bool Sifteo::isunordered (double a, double b=0.f)
 Unordered comparison. More...
 
float Sifteo::log (float a)
 Compute the natural log of a floating point number. Single-precision.
 
double Sifteo::log (double a)
 Compute the natural log of a floating point number. Double-precision.
 
template<typename T >
Sifteo::lslc (const T &value, int bits)
 Logical shift left with clamping. More...
 
template<typename T >
Sifteo::lsrc (const T &value, int bits)
 Logical shift right with clamping. More...
 
template<typename T >
Vector2< T > Sifteo::polar (T angle, T magnitude)
 Convert polar to cartesian.
 
float Sifteo::pow (float a, float b)
 Compute 'x' raised to the power 'y'. Single-precision.
 
double Sifteo::pow (double a, double b)
 Compute 'x' raised to the power 'y'. Double-precision.
 
template<typename T >
long Sifteo::round (const T value)
 Rounds 'value' to the nearest whole value. More...
 
template<typename T >
Sifteo::roundup (T numerator, T denominator)
 Round 'numerator' up to the nearest multiple of 'denominator'. Only for integer types.
 
float Sifteo::sin (float x)
 Calculate the sine of a specified angle, in radians. Single-precision. More...
 
void Sifteo::sincos (float x, float *s, float *c)
 Simultaneously compute the sine and cosine of a specified angle, in radians. More...
 
float Sifteo::sqrt (float a)
 Compute the square root of a floating point number. Single-precision.
 
double Sifteo::sqrt (double a)
 Compute the square root of a floating point number. Double-precision.
 
float Sifteo::tan (float x)
 Calculate the tangent of a specified angle, in radians. Single-precision.
 
float Sifteo::tcos (float x)
 Table-driven drop-in replacement for cos() More...
 
int Sifteo::tcosi (int x)
 Integer cosine table lookup. More...
 
float Sifteo::tsin (float x)
 Table-driven drop-in replacement for sin() More...
 
int Sifteo::tsini (int x)
 Integer sine table lookup. More...
 
unsigned Sifteo::umod (int a, int b)
 Compute the unsigned remainder from dividing two signed integers.
 
template<typename T >
Vector2< T > Sifteo::vec (T x, T y)
 Create a Vector2, from a set of (x,y) coordinates. More...
 
template<typename T >
Vector3< T > Sifteo::vec (T x, T y, T z)
 Create a Vector3, from a set of (x, y, z) coordinates. More...
 

Detailed Description

Floating point and integer math, matrices, vectors.

Function Documentation

template<typename T >
T Sifteo::abs ( const T &  value)
inline

For any type, return the absolute value.

If the value is less than zero, we return -value. Otherwise, we return the unmodified value.

float Sifteo::atan2 ( float  a,
float  b 
)
inline

Single-precision arc tangent function, of two variables.

This computes the arc tangent of y/x, using the sign of each argument to determine which quadrant the result is in.

template<typename T >
T Sifteo::bitRange ( int  begin,
int  end 
)
inline

Return a value of type T which has bits set in the half-open interval [begin, end).

The range may include negative values and/or values greater than the width of the type.

template<typename T >
long Sifteo::ceil ( const T  value)
inline

Returns the next integer value closer to positive infinity from 'value'.

E.g.:

ceil(1.0) => 1.0
ceil(1.1) => 2.0
ceil(-1.0) => -1.0
ceil(-1.1) => -1.0
ceil(-1.9) => -1.0
template<typename T >
T Sifteo::clamp ( const T &  value,
const T &  low,
const T &  high 
)
inline

For any type, clamp a value to the extremes 'low' and 'high'.

If the value is less than 'low' we return 'low', and if it's greater than 'high' we return 'high'. Otherwise we return the value unmodified.

unsigned Sifteo::clz ( uint32_t  word)
inline

Count leading zeroes in a 32-bit word.

If the parameter is nonzero, returns a number between 0 and 31 indicating how many leading zeroes are present in the parameter, starting at the most significant bit. If the parameter is zero, returns 32.

This function is very fast: it is implemented as a single CPU instruction.

float Sifteo::cos ( float  x)
inline

Calculate the cosine of a specified angle, in radians. Single-precision.

Trigonometry functions are very slow. If you're computing more than a few sine or cosine values per frame, consider using the table-driven alternative tsin().

unsigned Sifteo::ffs ( uint32_t  word)
inline

Find first bit set in a 32-bit word.

Finds the first bit set in a word, starting with the least significant bit. If no bits are set (the argument was zero), returns zero. Otherwise, returns a number between 1 (LSB) and 32 (MSB).

This function is implemented using clz().

template<typename T >
long Sifteo::floor ( const T  value)
inline

Returns the next integer value closer to negative infinity from 'value'.

E.g.:

floor(1.0) => 1.0
floor(1.9) => 1.0
floor(-1.0) => -1.0
floor(-1.1) => -2.0
floor(-1.9) => -2.0
bool Sifteo::isunordered ( float  a,
float  b = 0.f 
)
inline

Unordered comparison.

Given one or two single-precision floating point numbers, is there no defined sort order between them?

Returns true if either or both arguments are NaN.

bool Sifteo::isunordered ( double  a,
double  b = 0.f 
)
inline

Unordered comparison.

Given one or two double-precision floating point numbers, is there no defined sort order between them?

Returns true if either or both arguments are NaN.

template<typename T >
T Sifteo::lslc ( const T &  value,
int  bits 
)
inline

Logical shift left with clamping.

If the shift amount is negative, it is treated as zero. Shift amounts greater than or equal to the word width will always return zero.

template<typename T >
T Sifteo::lsrc ( const T &  value,
int  bits 
)
inline

Logical shift right with clamping.

If the shift amount is negative, it is treated as zero. Shift amounts greater than or equal to the word width will always return zero.

template<typename T >
long Sifteo::round ( const T  value)
inline

Rounds 'value' to the nearest whole value.

E.g.:

round(1.4) => 1.0
round(1.5) => 2.0

round(-1.4) => -1.0
round(-1.5) => -1.0
round(-1.51) => -2.0
float Sifteo::sin ( float  x)
inline

Calculate the sine of a specified angle, in radians. Single-precision.

Trigonometry functions are very slow. If you're computing more than a few sine or cosine values per frame, consider using the table-driven alternative tsin().

void Sifteo::sincos ( float  x,
float *  s,
float *  c 
)
inline

Simultaneously compute the sine and cosine of a specified angle, in radians.

This yields two single-precision floating point results, returned via the pointers 's' and 'c'.

float Sifteo::tcos ( float  x)
inline

Table-driven drop-in replacement for cos()

This calculates the cosine of a specified angle, in radians. Usage is identical to cos(), but the implementation is based on the same fast table lookup used by tsini() and tcosi().

int Sifteo::tcosi ( int  x)
inline

Integer cosine table lookup.

This is an all-integer table driven alternative to cos(). It is very fast. The lookup table is stored in very fast internal flash memory.

The input angle is specified in units of 360/8192 degrees. A full circle is exactly 8192 units, meaning that a 90 degree arc is 2048 units. The result is in fixed-point, with 16 bits to the right of the binary point. A value of 1.0 is represented by exactly 65536.

Only the low 13 bits of the input angle are used. This function is implemented as a thin wrapper around tsini().

float Sifteo::tsin ( float  x)
inline

Table-driven drop-in replacement for sin()

This calculates the sine of a specified angle, in radians. Usage is identical to sin(), but the implementation is based on the same fast table lookup used by tsini() and tcosi().

int Sifteo::tsini ( int  x)
inline

Integer sine table lookup.

This is an all-integer table driven alternative to sin(). It is very fast. The lookup table is stored in very fast internal flash memory.

The input angle is specified in units of 360/8192 degrees. A full circle is exactly 8192 units, meaning that a 90 degree arc is 2048 units. The result is in fixed-point, with 16 bits to the right of the binary point. A value of 1.0 is represented by exactly 65536.

Only the low 13 bits of the input angle are used.

template<typename T >
Vector2<T> Sifteo::vec ( x,
y 
)
inline

Create a Vector2, from a set of (x,y) coordinates.

This is a standalone function, instead of a constructor, so that Vector2 can remain a POD type, and it can be used in unions.

template<typename T >
Vector3<T> Sifteo::vec ( x,
y,
z 
)
inline

Create a Vector3, from a set of (x, y, z) coordinates.

This is a standalone function, instead of a constructor, so that Vector3 can remain a POD type, and it can be used in unions.