<<

module_lib_fixed_8_24 API

The funcionality is exported in the include file mathf8_24.h. THis file should be included in all XC and C files that use this library.

Type definition

f8_24

This type describes the fixed point numbers used in this library.

Values are stored in 32 bits comprising a sign bit (bit 31), 7 mantissa bits (bits 30..24), and 24 fractional bits (bits 23..0). The value of a fixed point number is defined as the the signed integer value multiplied by 2^-24. There are no representations for not-a-number or infinite.

Constants

MINF8_24

This constant defines the smallest number that is defined in the fixed point range, which is -128.

MAXF8_24

This constant defines the biggest number that is defined in the fixed point range, which is 127.999999940395355224609375.

HALF

This constant is the fixed point representation of the number 0.5.

ONE

This constant is the fixed point representation of the number 1.0.

PI2

This constant is the closest fixed point representation of 2 PI.

PIHALF

This constant is the fixed point representation of PI/2.

Basic operations

The addition and subtraction operators + and - can be used on fixed point numbers as normal. Make sure that constants are shifted up 24 places, ie, for a fixed point number f, the statement f++ adds 0.000000059604644775390625 not one.

f8_24 mulf8_24(f8_24 x, f8_24 y)

This function returns the product of two fixed point numbers.

If the product does not fit in a fixed point number, a modulo answer is returned.

Parameters:
  • x – First number to be multiplied
  • y – Second number to be multiplied
Returns:

x*y

f8_24 divf8_24(f8_24 x, f8_24 y)

This function returns the division of two fixed point numbers.

If the product does not fit in a fixed point number, higher bits will be lost.

Parameters:
  • x – the divisor
  • y – the dividend
Returns:

x / y

f8_24 ldexpf8_24(f8_24 mant, int exp)

This function is the fixed point equivalent of ldexp(): given a mantissa and an exponent it computes the fixed point number with the value mant * 2 ^ exp.

The reverse operation is implemented by frexp8_24().

Parameters:
  • mant – mantissa input value represented as a fixed point number
  • exp – exponent input value, represented as a signed integer
Returns:

math * 2 ^ exp

{f8_24,int} f frexpf8_24(f8_24 d)

This functions splits a fixed point number into a mantissa and exponent.

The mantissa is either zero or in the range [0.5..1). The exponent is in the range [-32..32]. and an exponent of a fixed point number. The reverse operation is implemented by ldexp8_24().

Parameters:
  • d – input fixed point number
Returns:

a mantissa as an fixed point number

an exponent as an integer.

f8_24 reducef8_24(int h, unsigned l)

This function clips and rounds a 16.48 fixed point number into an 8.24 fixed point number.

It is used for finalising the answer out of a series of multiply accumulates. If an overflow has happened, the result is saturated to MINF8_24 or MAXF8_24. The number is rounded towards minus infinity; if rounding is required half should be added to the number, typically before the multiply accumulate

Parameters:
  • h – high input word (two’s complement 16.16 number)
  • l – low input word to be concatenated with h to form 16.48 number
Returns:

saturated (h:l)[bits 8..40]

f8_24 f8_24toint(f8_24 x)

This function converts a fixed point number to an integer, discarding the fractional part.

Parameters:
  • x – fixed point input value
Returns:

integer equivalent

f8_24 inttof8_24(f8_24 x)

This function converts an integer to a fixed point number.

The input to int2f8_24() has to be in the range [-128..127].

Parameters:
  • x – integer input value
Returns:

fixed point equivalent

f8_24 fabsf8_24(f8_24 x)

This function returns the absolute value of a fixed point number.

The input value has to be in the range [-MAXF8_24..MAXF8_24]; the absolute value of MINF8_24 cannot be represented as a fixed point number.

Parameters:
  • x – input value
Returns:

|x|

f8_24 froundf8_24(f8_24 x)

This function rounds a fixed point number to its nearest integral number and returns a fixed point number.

The input to froundf8_24() should be less than 127.5.

Parameters:
  • x – input value
Returns:

nearest integer.

Mathematical functions

f8_24 sinf8_24(f8_24 x)

This function returns the sine of a fixed point number in radians.

The input number has to be in the range -MAXF8_24 + PI and MAXF8_24 - PI.

Parameters:
  • x – input value in radians
Returns:

sine(x)

f8_24 cosf8_24(f8_24 x)

This function returns the cosine of a fixed point number in radians.

The input number has to be in the range -MAXF8_24 + PI and MAXF8_24 - PI.

Parameters:
  • x – input value in radians
Returns:

cosine(x)

f8_24 sinhf8_24(f8_24 x)

This function returns the hyperbolic sine (sinh) of a fixed point number.

The input number has to be in the range [-5.5..5.5] in order to avoid overflow, and for values outside the [-4..4] range there are relatively large errors in the result.

Parameters:
  • x – input value
Returns:

sinh(x)

f8_24 coshf8_24(f8_24 x)

This function returns the hyperbolic cosine (cosh) of a fixed point number.

The input number has to be in the range [-5.5..5.5] in order to avoid overflow, and for values outside the [-4..4] range there are relatively large errors in the result.

Parameters:
  • x – input value
Returns:

sinh(x)

f8_24 sqrtf8_24(f8_24 x)

This function returns the square root of a fixed point number.

The input number has to be positive.

Parameters:
  • x – input value in radians
Returns:

sqrt(x)

f8_24 expf8_24(f8_24 x)

This function returns the natural exponent of a fixed point number.

The input number has to be less than 4.8, otherwise the answer cannot be represented as a fixed point number. For input values larger than 3 there is a relatively large error in the last three bits of the answer.

Parameters:
  • x – input value
Returns:

e^x

f8_24 logf8_24(f8_24 x)

This function returns the natural logarithm (ln) of a fixed point number.

The input number has to be positive.

Parameters:
  • x – input value
Returns:

ln(x).

f8_24 log10f8_24(f8_24 x)

This function returns the logarithm base 10 of a fixed point number.

The input number has to be positive.

Parameters:
  • x – input value
Returns:

ln(x)/ln(10).

Input/Output functions

void printf8_24(f8_24 x)

This function prints a number using a decimal point.

Parameters:
  • x – value to be printed
void printf8_24ln(f8_24 x)

This function prints a number using a decimal point and adds a newline.

Parameters:
  • x – value to be printed

Example program

Below is an example function that prints out the values of a sine wave:

#include "mathf8_24.h"

void printsine(void) {
    for(f8_24 rad = 0; rad <= PI2; rad += PI2/20) {
        printstr("sin ");
        printf8_24(rad);
        printstr(" = ");
        printf8_24ln(cosf8_24(rad));
    }
}