FixMath
Functions
MozziPrivate Namespace Reference

Functions

template<typename T >
constexpr T shiftR (T x, int8_t bits)
 
constexpr int8_t sBitsToBytes (int8_t N)
 
constexpr int8_t uBitsToBytes (int8_t N)
 
template<typename T >
constexpr T max (T N1, T N2)
 
template<typename T >
constexpr T min (T N1, T N2)
 
constexpr uint64_t sFullRange (int8_t N)
 
constexpr uint64_t uFullRange (int8_t N)
 
constexpr uint64_t rangeAdd (byte NF, byte _NF, uint64_t RANGE, uint64_t _RANGE)
 
constexpr int8_t neededNIExtra (int8_t NI, int8_t NF, uint64_t RANGE)
 
constexpr int8_t neededSNIExtra (int8_t NI, int8_t NF, uint64_t RANGE)
 
constexpr uint64_t rangeShift (int8_t N, int8_t SH, uint64_t RANGE)
 

Detailed Description

This file implements two fixed point number classes. These numbers can have a fractional part but are actually standard integers under the hood which makes calculations with them efficient on platforms which do not have a FPU like most micro-controllers. These numbers can be signed (SFix) or unsigned (UFix).

A fixed point number has its range defined by the number of bits encoding the integer part (NI in the following) and its precision by the number of bits encoding the fractional part (NF). For UFix types, the integral part can hold values in [0,2^NI-1], for SFix types, the integral part can hold values in [-2^NI,2^NI-1]. The number of bits encoding the fractional can be considered as the precision of the number: given NF, the number of possible values in the [0,1[ range will 2^NF. Hence, given NF, the resolution will be 1/(2^NF).

Under the hood, these types will keep track of the maximum possible value they might hold (this is the RANGE template parameter), and, if only SAFE operations (see below) are used, will automatically adjust there NI and NF to accomodate the result of a operation. It will also try not to promote there internal type when possible, assuming that you use the complete range of a given type.

The operations possible with these types can be divided into two categories:

Like standard C(++) types, the fixed point numbers defined here are following some rules:

More specifically on the returned types of the operations between fixed point math types:

Note on division: The division is not implemented. This is a deliberate choice made for two reasons:

division are usually very slow operations on MCU, hence there usage is discouraged. The ideal way of doing it is to compute the inverse whenever needed and only when needed. In the context of Mozzi for instance, a good way to do it would be to compute needed inverses in updateControl(), and use them in updateAudio().