Microchip® Advanced Software Framework

fixed_t.c File Reference

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 "fixed_t.h"

Macros

#define clzll(n)   __builtin_clzll(n)
 

Functions

int fixed_range (double x_min, double x_max)
 signed fixed-point range More...
 
int fixed_resolution (double epsilon)
 signed fixed-point resolution More...
 
fixed_t fixed_sqrt (fixed_t f, int Q)
 Calculate the square root of an integer. More...
 

#define clzll (   n)    __builtin_clzll(n)

Referenced by fixed_sqrt().

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.

Parameters
x_minThe smallest signed integer portion of a real type.
x_maxThe largest signed integer portion of a real type.
Returns
The required number of integer bits required for a fixed-point implementation.
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.

Parameters
epsilonThe required resolution of the fixed-point number.
Returns
The required number of fractional bits required for a fixed-point implementation.
fixed_t fixed_sqrt ( fixed_t  f,
int  Q 
)

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 \).

Parameters
fA fixed-point value. Negative values will be computed using the unsigned magnitude without reporting a domain error.
QThe number of fractional bits in parameter f.
Returns
The integer square root.

References clzll, and temp.