yarf 0.1
Yet Another RepRap Firmware
Defines | Typedefs | Functions
src/util/fixed_point.h File Reference

Fixed point number representation and basic math routines. The fixed point numbers are unsigned and occupy 32 bits, of which 16 for the whole part and 16 for the fractional part. More...

#include <stdint.h>

Go to the source code of this file.

Defines

#define NB_FRACTIONAL_BITS   16
 The number of fractional bits in the fixed point representation.
#define FRACTIONAL_MULTIPLIER   65536.0f
 2 ^ (NB_FRACTIONAL_BITS)
#define FXP_16U16_MAX   UINT32_MAX
 The largest representable fixed point 16u16 number.
#define FXP_16U16_ZERO   0
 The number 0 represented as a fixed point 16u16.
#define FXP_16U16_ONE   ((fxp16u16_t)1 << NB_FRACTIONAL_BITS)
 The number 1 represented as a fixed point 16u16.
#define FXP_16U16_ONE_HALF   (FXP_16U16_ONE >> 1)
 The number 0.5 represented as a fixed point 16u16.

Typedefs

typedef uint32_t fxp16u16_t
 Fixed point 16u16 are internally represented as 32-bit unsigned integers.

Functions

uint16_t fxp16u16_to_uint16 (fxp16u16_t x)
 Converts a fixed point 16u16 number into an unsigned 16-bit integer, by rounding to the nearest integer.
float fxp16u16_to_float (fxp16u16_t x)
 Converts a fixed point 16u16 number into a float.
fxp16u16_t fxp16u16_from_uint16 (uint16_t x)
 Converts an unsigned 16-bit integer into a fixed point 16u16 number.
fxp16u16_t fxp16u16_from_float (float x)
 Converts a float into a fixed point 16u16 number.
fxp16u16_t fxp16u16_from_float_s (float x)
 Converts a float into a fixed point 16u16 number, using saturated arithmetic.
fxp16u16_t fxp16u16_mul (fxp16u16_t x, fxp16u16_t y)
 Returns the product of two fixed point 16u16 numbers, if that product is within the range of representable fixed point 16u16 numbers.
fxp16u16_t fxp16u16_div (fxp16u16_t x, fxp16u16_t y)
 Returns the quotient of two fixed point 16u16 numbers.
fxp16u16_t fxp16u16_div_uint16 (fxp16u16_t x, uint16_t y)
 Returns the quotient of a fixed point 16u16 number divided by an unsigned 16-bit integer.
fxp16u16_t fxp16u16_square (fxp16u16_t x)
 Returns the square of a fixed point 16u16 number.

Detailed Description

Fixed point number representation and basic math routines. The fixed point numbers are unsigned and occupy 32 bits, of which 16 for the whole part and 16 for the fractional part.

Author:
Pieter Agten (pieter.agten@gmail.com)
Date:
16 nov 2011

Definition in file fixed_point.h.


Function Documentation

fxp16u16_t fxp16u16_div ( fxp16u16_t  x,
fxp16u16_t  y 
) [inline]

Returns the quotient of two fixed point 16u16 numbers.

Parameters:
xthe dividend
ythe divisor
Returns:
x / y

Definition at line 176 of file fixed_point.h.

fxp16u16_t fxp16u16_div_uint16 ( fxp16u16_t  x,
uint16_t  y 
) [inline]

Returns the quotient of a fixed point 16u16 number divided by an unsigned 16-bit integer.

Parameters:
xthe dividend
ythe divisor
Returns:
x / y

Definition at line 192 of file fixed_point.h.

fxp16u16_t fxp16u16_from_float ( float  x) [inline]

Converts a float into a fixed point 16u16 number.

Parameters:
xthe float to convert
Returns:
The fixed point 16u16 number closest to the given float, if the float is within the representable range of the fixed point 16u16 datatype (0 to (65536 - eps)). The result is undefined otherwise.

Definition at line 121 of file fixed_point.h.

fxp16u16_t fxp16u16_from_float_s ( float  x) [inline]

Converts a float into a fixed point 16u16 number, using saturated arithmetic.

Parameters:
xthe float to convert
Returns:
The fixed point 16u16 number closest to the given float, if the float is within the representable range of the fixed point 16u16 datatype (0 to (65536 - eps)). If the float is less than 0, the fixed point 16u16 representation of 0 is returned. If the float is larger than the maximum representable fixed point 16u16 value, FXP_16U16_MAX is returned.

Definition at line 139 of file fixed_point.h.

fxp16u16_t fxp16u16_from_uint16 ( uint16_t  x) [inline]

Converts an unsigned 16-bit integer into a fixed point 16u16 number.

Parameters:
xthe 16-bit integer to convert
Returns:
The fixed point 16u16 number of which the whole part is equal to x and of which the fractional part is 0.

Definition at line 107 of file fixed_point.h.

fxp16u16_t fxp16u16_mul ( fxp16u16_t  x,
fxp16u16_t  y 
) [inline]

Returns the product of two fixed point 16u16 numbers, if that product is within the range of representable fixed point 16u16 numbers.

Parameters:
xthe first factor of the product
ythe second factor of the product
Returns:
(x * y) if that is within the range of representable fixed point 16u16 numbers, or an undefined value otherwise.

Definition at line 161 of file fixed_point.h.

fxp16u16_t fxp16u16_square ( fxp16u16_t  x) [inline]

Returns the square of a fixed point 16u16 number.

Parameters:
xthe number of which to return the square
Returns:
x * x

Definition at line 206 of file fixed_point.h.

float fxp16u16_to_float ( fxp16u16_t  x) [inline]

Converts a fixed point 16u16 number into a float.

Parameters:
xthe fixed point number to convert
Returns:
The float closest to the number represented by x.

Definition at line 94 of file fixed_point.h.

uint16_t fxp16u16_to_uint16 ( fxp16u16_t  x) [inline]

Converts a fixed point 16u16 number into an unsigned 16-bit integer, by rounding to the nearest integer.

Parameters:
xthe fixed point number to convert
Returns:
The integer closest to the number represented by x, in the form of a 16-bit unsigned integer. If the fractional part of x is equal to 0.5, it is rounded up.

Definition at line 82 of file fixed_point.h.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines