ASF Sensor API fixed point math operations.
This header defines types and operations that support basic fixed-point arithmetic operations. The fixed_t
type is a signed 32-bit value that is stored and operated on using integer operations. The number of fractional bits operated on will be context-specific.
The conversion functions and some of the arithmetic functions require a Q parameter specifying the number of fractional bits in the operands. Arithmetic functions that do not require the Q parameter assume that both operands have the same Q format (the same number of fractional bits).
Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
Typedefs | |
typedef int32_t | fixed_t |
Signed fixed-point format storage type. More... | |
Functions | |
static fixed_t | fixed_conv (fixed_t a, int q1, int q2) |
fixed point Q format conversion More... | |
Type Conversion Primitives | |
static fixed_t | long_to_fixed (long n, int Q) |
static fixed_t | float_to_fixed (float x, int Q) |
static fixed_t | double_to_fixed (double x, int Q) |
static long | fixed_to_long (fixed_t f, int Q) |
static float | fixed_to_float (fixed_t f, int Q) |
static double | fixed_to_double (fixed_t f, int Q) |
Addition and Subtraction Primitives | |
static fixed_t | fixed_add (fixed_t a, fixed_t b) |
static fixed_t | fixed_addl (fixed_t a, long b, int Q) |
static fixed_t | fixed_sub (fixed_t a, fixed_t b) |
static fixed_t | fixed_subl (fixed_t a, long b, int Q) |
Multiplication and Division Primitives | |
static fixed_t | fixed_mul (fixed_t a, fixed_t b, int Q) |
static fixed_t | fixed_mull (fixed_t a, long b) |
static fixed_t | fixed_mul_rounded (fixed_t a, fixed_t b, int Q) |
static fixed_t | fixed_div (fixed_t a, fixed_t b, int Q) |
static fixed_t | fixed_divl (fixed_t a, long b) |
static fixed_t | fixed_div_rounded (fixed_t a, fixed_t b, int Q) |
Algebraic and Transcendental Functions | |
fixed_t | fixed_sqrt (fixed_t f, int Q) |
Calculate the square root of an integer. More... | |
fixed-point resolution and range utilities | |
The fixed_range() and fixed_resolution() utilities can be used to define, verify, test, and characterize fixed-point formats for applications that are being converted from floating-point or implemented in fixed-point. | |
int | fixed_range (double x_min, double x_max) |
signed fixed-point range More... | |
int | fixed_resolution (double epsilon) |
signed fixed-point resolution More... | |
typedef int32_t fixed_t |
Signed fixed-point format storage type.
|
inlinestatic |
References long_to_fixed().
fixed point Q format conversion
This routine performs a change of exponent operation where fixed-point value a in q1 format is converted to q2 format. The change of binary exponent is derived from the following relation:
\[ n2^{-p} = k2^{-r} \Longrightarrow k = n2^{r - p} \]
Thus, the converted value k is obtained by dividing n by \( 2^{p - r} \) when \( (p > r) \) or by multiplying by \( 2^{r - p} \) when \( (r \ge p) \).
int fixed_range | ( | double | x_min, |
double | x_max | ||
) |
signed fixed-point range
Given a signed fixed-point format Qm.f
, where f represents the number of fractional bits and m the number of integer bits, this routine calculates the the number of integer bits m required to represent real data types in the interval [x_min
, x_max
].
The x_min
and x_max
interval endpoints are the smallest and largest integer portions, respectively, of the numbers that must be represented in fixed-point.
x_min | The smallest signed integer portion of a real type. |
x_max | The largest signed integer portion of a real type. |
int fixed_resolution | ( | double | epsilon | ) |
signed fixed-point resolution
Given a signed fixed-point format Qm.f
, where f represents the number of fractional bits and m the number of integer bits, this routine calculates the the number of fractional bits f required to represent real data types with resolution \( \epsilon = \frac{1}{2^{f}} \). For example, a signed variable A = 3.141 requires <= 0.001.
epsilon | The required resolution of the fixed-point number. |
Given a signed fixed-point format Qm.f
, where f represents the number of fractional bits and m the number of integer bits, this routine calculates the the number of fractional bits f required to represent real data types with resolution
\[ \epsilon = \frac{1}{2^{f}} \]
. For example, a signed variable A = 3.141 requires <= 0.001.
epsilon | The required resolution of the fixed-point number. |
Calculate the square root of an integer.
This routine calculates the integer square root, isqrt(), of a positive integer argument n:
\[ isqrt(n) = \lfloor n^{1/2} \rfloor \]
The computed value is the largest positive integer m with a square that is less than or equal to the given integer n such that \( m^{2} \le n \).
f | A fixed-point value. Negative values will be computed using the unsigned magnitude without reporting a domain error. |
Q | The number of fractional bits in parameter f. |
References long_to_fixed().
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Referenced by fixed_addl(), and fixed_subl().