Fixmath  1.4
Data Structures | Typedefs | Functions
fixmath.h File Reference

Fixed-point math library. More...

Go to the source code of this file.

Data Structures

struct  fx_rdiv_t
 

Typedefs

typedef int32_t fixed_t
 

Functions

int fx_clz (uint32_t word)
 
int fx_ctz (uint32_t word)
 
int fx_bitcount (uint32_t word)
 
fixed_t fx_itox (int32_t ival, unsigned frac)
 
fixed_t fx_ftox (float fval, unsigned frac)
 
fixed_t fx_dtox (double dval, unsigned frac)
 
fixed_t fx_xtox (fixed_t xval, unsigned frac1, unsigned frac2)
 
int32_t fx_xtoi (fixed_t xval, unsigned frac)
 
float fx_xtof (fixed_t xval, unsigned frac)
 
double fx_xtod (fixed_t xval, unsigned frac)
 
int32_t fx_roundx (fixed_t xval, unsigned frac)
 
int32_t fx_ceilx (fixed_t xval, unsigned frac)
 
int32_t fx_floorx (fixed_t xval, unsigned frac)
 
fixed_t fx_addx (fixed_t x1, fixed_t x2)
 
fixed_t fx_subx (fixed_t x1, fixed_t x2)
 
fixed_t fx_mulx (fixed_t x1, fixed_t x2, unsigned frac)
 
fixed_t fx_divx (fixed_t x1, fixed_t x2, unsigned frac)
 
fixed_t fx_rdivx (fixed_t xval, const fx_rdiv_t *rdiv)
 
int fx_itoa (char *str, int32_t val)
 
int fx_xtoa (char *str, fixed_t xval, unsigned frac, unsigned digits)
 
fixed_t fx_invx (fixed_t xval, unsigned frac, fx_rdiv_t *rdiv)
 
fixed_t fx_sqrtx (fixed_t xval, unsigned frac)
 
fixed_t fx_isqrtx (fixed_t xval, unsigned frac, fx_rdiv_t *rdiv)
 
fixed_t fx_expx (fixed_t xval, unsigned frac)
 
fixed_t fx_exp2x (fixed_t xval, unsigned frac)
 
fixed_t fx_exp10x (fixed_t xval, unsigned frac)
 
fixed_t fx_logx (fixed_t xval, unsigned frac)
 
fixed_t fx_log2x (fixed_t xval, unsigned frac)
 
fixed_t fx_log10x (fixed_t xval, unsigned frac)
 
fixed_t fx_powx (fixed_t xval, unsigned xfrac, fixed_t yval, unsigned yfrac)
 
fixed_t fx_sinx (fixed_t xval, unsigned frac)
 
fixed_t fx_cosx (fixed_t xval, unsigned frac)
 

Detailed Description

Fixed-point math library.

Typedef Documentation

typedef int32_t fixed_t

The fixed-point data type.

Function Documentation

int fx_clz ( uint32_t  word)

Count the number of leading zeros in a 32-bit word. Note that fx_clz(0) is undefined.

Parameters
wordThe source word.
Returns
The number of leading zeros.
int fx_ctz ( uint32_t  word)

Count the number of trailing zeros in a 32-bit word. Note that fx_ctz(0) is undefined.

Parameters
wordThe source word.
Returns
The number of trailing zeros.
int fx_bitcount ( uint32_t  word)

Count the number of bits set in a 32-bit word.

Parameters
wordThe source word.
Returns
The number of bits set.
fixed_t fx_itox ( int32_t  ival,
unsigned  frac 
)

Integer to fixed-point conversion.

Parameters
ivalInteger value.
fracThe number of fixed-point fractional bits desired.
Returns
The converted fixed-point number.
fixed_t fx_ftox ( float  fval,
unsigned  frac 
)

Single precision float to fixed-point conversion. The error is within 1 ulp.

Parameters
fvalSingle-precision float value.
fracThe number of fixed-point fractional bits desired.
Returns
The converted fixed-point number.
Note
The fval argument may be evaluated twice.
fixed_t fx_dtox ( double  dval,
unsigned  frac 
)

Double precision float to fixed-point conversion. The error is within 1/2 ulp.

Parameters
dvalDouble-precision float value.
fracThe number of fixed-point fractional bits desired.
Returns
The converted fixed-point number.
Note
The dval argument may be evaluated twice.
fixed_t fx_xtox ( fixed_t  xval,
unsigned  frac1,
unsigned  frac2 
)

Fixed-point format conversion. The error is within 1/2 ulp.

Parameters
xvalFixed-point value with frac1 fractional bits.
frac1The number of fractional in original representation.
frac2The number of fractional bits resired.
Returns
A fixed-point value with frac2 fractional bits.
Note
The frac1 and frac2 arguments may be evaluated twice.
int32_t fx_xtoi ( fixed_t  xval,
unsigned  frac 
)

Fixed-point to integer conversion.

This is equal to fx_floorx().

Parameters
xvalFixed-point value.
fracThe number of fractional bits.
Returns
The integer part of xval, rounded towards -infinity.
float fx_xtof ( fixed_t  xval,
unsigned  frac 
)

Fixed-point to single precision float conversion. The error is within 1/2 ulp.

Parameters
xvalFixed-point value.
fracThe number of fractional bits.
Returns
The converted floating-point number.
double fx_xtod ( fixed_t  xval,
unsigned  frac 
)

Fixed-point to double precision float conversion. The error is within 1/2 ulp.

Parameters
xvalFixed-point value.
fracThe number of fractional bits.
Returns
The converted double-precision floating-point number.
int32_t fx_roundx ( fixed_t  xval,
unsigned  frac 
)

Round a fixed-point number to the nearest integer.

Parameters
xvalFixed-point value.
fracThe number of fractional bits.
Returns
The value rounded to the nearest integer.
Note
The frac argument may be evaluated twice.
int32_t fx_ceilx ( fixed_t  xval,
unsigned  frac 
)

Round a fixed-point number up to the nearest integer.

Parameters
xvalFixed-point value.
fracThe number of fractional bits in xval.
Returns
The value rounded to up to the nearest integer. The return value is int32_t and therefore has zero fractional bits.
Note
The frac argument may evaluated twice.
int32_t fx_floorx ( fixed_t  xval,
unsigned  frac 
)

Round a fixed-point number down to the nearest integer.

Parameters
xvalFixed-point value.
fracThe number of fractional bits in xval.
Returns
The value rounded down to the nearest integer. The return value is int32_t and therefore has zero fractional bits.
fixed_t fx_addx ( fixed_t  x1,
fixed_t  x2 
)

Fixed-point addition. The terms must have the same number of fractional bits. This operation expands to a regular add operation.

Parameters
x1First fixed-point term.
x2Second fixed-point term.
Returns
The sum with the same number of fractional bits as the terms.
fixed_t fx_subx ( fixed_t  x1,
fixed_t  x2 
)

Fixed-point subtraction. The terms must have the same number of fractional bits. This operation expands to a regular add operation.

Parameters
x1First fixed-point term.
x2Second fixed-point term.
Returns
The difference with the same number of fractional bits as the terms.
fixed_t fx_mulx ( fixed_t  x1,
fixed_t  x2,
unsigned  frac 
)

Fixed-point multiply. The error is within 1/2 ulp. The number of fraction bits in the result is f1 + f2 - frac, where f1 and f2 are the number of fraction bits of x1 and x2, respectively. Thus, if both f1 and f2 are equal to frac, the number of fraction bits in the result will also be frac.

Parameters
x1First fixed-point factor.
x2Second fixed-point factor.
fracNumber of fraction bits, see explaination above.
Returns
The multiplied result.
Note
The frac argument may be evaluated twice.
fixed_t fx_divx ( fixed_t  x1,
fixed_t  x2,
unsigned  frac 
)

Fixed-point divide. The error is within 1/2 ulp. The number of fraction bits in the result is f1 - f2 + frac, where f1 and f2 are the number of fraction bits of x1 and x2, respectively. Thus, if both f1 and f2 are equal to frac, the number of fraction bits in the result will also be frac.

Parameters
x1Fixed-point numerator.
x2Fixed-point denominator.
fracNumber of fraction bits, see explaination above.
Returns
The quotient result.
fixed_t fx_rdivx ( fixed_t  xval,
const fx_rdiv_t rdiv 
)

Fixed-point divide by reciprocal multiplication.

Parameters
xvalFixed-point numerator.
rdivA reciprocal division factor, obtained from either fx_invx() or fx_isqrtx().
Returns
The quotient, with the same number of fractional bits as the numerator.
Note
The rdiv argument may be evaluated twice.
int fx_itoa ( char *  str,
int32_t  val 
)

Integer to ASCII string conversion.

Parameters
strThe string buffer to use. It must be long enough to hold the string value, i.e. at least 12 bytes in the worst case.
valThe integral number to convert.
Returns
The length of the converted string. If str is NULL, a negative value is returned and errno is set to EINVAL.
int fx_xtoa ( char *  str,
fixed_t  xval,
unsigned  frac,
unsigned  digits 
)

Fixed-point to ASCII string conversion. The number is converted to the format [-]d.ddd[e+00]. If the precision (digits) is zero, no decimal point appears. If the exponent is zero, no exponent appears.

Parameters
strThe string buffer to use. It must be long enough to hold the string value, i.e. at least 8 + digits bytes.
xvalThe fixed-point number to convert.
fracThe number of fractional bits.
digitsThe number of digits to the right of the decimal point.
Returns
The length of the converted string. If str is NULL or frac is greater than 31, a negative value is returned and errno is set to EINVAL.
fixed_t fx_invx ( fixed_t  xval,
unsigned  frac,
fx_rdiv_t rdiv 
)

Fixed-point inverse value. The error is within 4 ulp if frac is less than 31, 7 ulp otherwise. If the input is out-of-range (zero), then errno is set to EDOM.

Parameters
xvalA non-zero fixed-point value.
fracThe number of fractional bits.
rdivOptional reciprocal division multiplier.
Returns
The inverse value, i.e. 1/xval.
fixed_t fx_sqrtx ( fixed_t  xval,
unsigned  frac 
)

Fixed-point square root. The error is within 2 ulp if frac is less than 31, and within 4 ulp otherwise. If the input is out-of-range (negative), then errno is set to EDOM.

Parameters
xvalA non-zero fixed-point value.
fracThe number of fractional bits.
Returns
The square root value, i.e. sqrt(xval).
fixed_t fx_isqrtx ( fixed_t  xval,
unsigned  frac,
fx_rdiv_t rdiv 
)

Fixed-point inverse square root. The error is within 2 ulp if frac is less than 31, and within 3 ulp otherwise. If the input is out-of-range (negative or zero), then errno is set to EDOM.

Parameters
xvalA non-zero fixed-point value.
fracThe number of fractional bits.
rdivOptional reciprocal division multiplier.
Returns
The inverse square root value, i.e. 1/sqrt(xval).
fixed_t fx_expx ( fixed_t  xval,
unsigned  frac 
)

Fixed-point natural exponential. The error is within 2 ulp. If frac is greater than 31, then errno is set to EDOM.

Parameters
xvalA fixed-point value.
fracThe number of fractional bits.
Returns
The natural exponential of xval, i.e. exp(xval).
fixed_t fx_exp2x ( fixed_t  xval,
unsigned  frac 
)

Fixed-point base-2 exponential. The error is within 2 ulp. If frac is greater than 31, then errno is set to EDOM.

Parameters
xvalA fixed-point value.
fracThe number of fractional bits.
Returns
The base-2 exponential of xval, i.e. 2**(xval).
fixed_t fx_exp10x ( fixed_t  xval,
unsigned  frac 
)

Fixed-point base-10 exponential. The error is within 2 ulp. If frac is greater than 31, then errno is set to EDOM.

Parameters
xvalA fixed-point value.
fracThe number of fractional bits.
Returns
The base-10 exponential of xval, i.e. 10**(xval).
fixed_t fx_logx ( fixed_t  xval,
unsigned  frac 
)

Fixed-point natural logarithm. The error is within 2 ulp. If xval is negative or zero or frac is greater than 31, then errno is set to EDOM.

Parameters
xvalA fixed-point value.
fracThe number of fractional bits.
Returns
The natural logarithm of xval, i.e. log(xval).
fixed_t fx_log2x ( fixed_t  xval,
unsigned  frac 
)

Fixed-point base-2 logarithm. The error is within 2 ulp. If xval is negative or zero or frac is greater than 31, then errno is set to EDOM.

Parameters
xvalA fixed-point value.
fracThe number of fractional bits.
Returns
The base-2 logarithm of xval, i.e. log2(xval).
fixed_t fx_log10x ( fixed_t  xval,
unsigned  frac 
)

Fixed-point base-10 logarithm. The error is within 2 ulp. If xval is negative or zero or frac is greater than 31, then errno is set to EDOM.

Parameters
xvalA fixed-point value.
fracThe number of fractional bits.
Returns
The base-10 logarithm of xval, i.e. log10(xval).
fixed_t fx_powx ( fixed_t  xval,
unsigned  xfrac,
fixed_t  yval,
unsigned  yfrac 
)

Fixed-point power function. The error is bounded by C1 |yval| xval yval + C2, where C1 and C2 are approximately 1 and 5 ulp, respectively. If either xfrac or yfrac is greater than 31, or xval is negative, or xval is zero and yval is negative, then errno is set to EDOM.

Parameters
xvalThe base in fixed-point format.
xfracThe number of fractional bits for the base.
yvalThe exponent in fixed-point format.
yfracThe number of fractional bits for the exponent.
Returns
xval raised to the power of yval. The number of fractional bits is xfrac.
fixed_t fx_sinx ( fixed_t  xval,
unsigned  frac 
)

Fixed-point sine. The error is within 3 ulp if frac is less than 31, and within 4 ulp otherwise. If frac is greater than 31, then errno is set to EDOM.

Parameters
xvalA fixed-point value.
fracThe number of fractional bits.
Returns
The sine value of xval, i.e. sin(xval).
fixed_t fx_cosx ( fixed_t  xval,
unsigned  frac 
)

Fixed-point cosine. The error is within 3 ulp if frac is less than 31, and within 4 ulp otherwise. If frac is greater than 31, then errno is set to EDOM.

Parameters
xvalA fixed-point value.
fracThe number of fractional bits.
Returns
The cosine value of xval, i.e. cos(xval).