1.4.4 LibQ Fixed-Point "C" Math Library

Introduction

This topic describes the LibQ Fixed-Point C Math Library.

Description

The LibQ Fixed-Point C Math Library is written in C and is available for all processors supported by Harmony 3.

The libq_c Fixed-Point Math Library provides fixed-point math functions written in C for portability between core processors. The library uses signed fixed point types (fractional Q types specified by Qn.m) which are specifed as follows in the library names:

Qndm where:

• n is the number of data bits to the left of the radix point

• m is the number of data bits to the right of the radix point

• a signed bit is implied (unless stated otherwise)

For convenience, short names are also defined for arbitrary scaled fractional types:

• q15 is signed fractional 16 bit value

• q31 is signed fractional 32 bit value

• i16 is signed integer, i.e. Q16d0

In addition, A pseudo floating point 32 bit format (FxQFloat32) is defined that consists of 16 mantissa and a 16 bit exponent (base 2). Functions in the library are prefixed with the type of the return value and followed by argument types (in order):

libq_<result><name><args>

For example, libq_q1d15_Sin_q10d6 returns a Q1.15 value equal to the to the sine of an angle specified as a Q10.6 value (in degrees between 0 and 360).

For arbitrary scaled types (q15, q16, q31, and q32) the scaling of the result will depend on the function and the scaling of the arguments. For instance, libq_q15_Add_q15_q15(a,b) will return a scaled value type that is the two input types (which must have equivalent scaled value type).

Configuring the Library using MHC

Select the Audio/Math/Math Libraries component to load the libq_c, libq/dsp libraries, as shown below.

The project configuration should now contain the Math Libraries block.

The configuration window (when the block is selected) looks like the following:

Remarks

The libq_c functions do not correspond to the libq fixed-point library optimized for the microaptive core processor and written in asm.

Library Overview

LIBQ_C math functions:

• Sine: libq_q1d15_Sin_q10d6, libq_q20d12_Sin_q20d12

• Abs: libq_q15_Abs_q15, libq_q31_Abs_q31

• Negate: libq_q15_Negate_q15, libq_q31_Negate_q31

• Round: libq_q15_RoundL_q31

• Deposit: libq_q31_DepositH_q15, libq_q31_DepositL_q15

• Extract: libq_q15_ExtractH_q31, libq_q15_ExtractL_q31

• Add: libq_q15_Add_q15_q15, libq_q31_Add_q31_q31

• Subtract: libq_q15_Sub_q15_q15, libq_q31_Sub_q31_q31

• Shift(Scale): libq_q15_ShiftLeft_q15_q15, libq_q31_ShiftLeft_q31_q15, libq_q15_ShiftRight_q15_q15, libq_q31_ShiftRight_q31_q15, libq_q15_ShiftRightRound_q15_q15, libq_q31_ShiftRightRound_q31_q15

• Multiply: libq_q15_Mult_q15_q15, libq_q15_MultipyR2_q15_q15, libq_q31_Multi_q15_q31

• Divide: libq_q15_DivisionWithSaturation_q15_q15

• Multiply-Accumulate: libq_q31_Mac_q31_q15_q15, libq_q15_MacR_q31_q15_q15

• Multiply-Subtract: libq_q31_Msu_q31_q15_q15, libq_q15_MsuR_q31_q15_q15

• Exponential-Averaging: libq_q15_ExpAvg_q15_q15_q1d15

LIBQ_C conversion functions:

• Normalize Q value: Fx16Norm, Fx32Norm

• Float-to-Q value: Fl2Fract16, Fl2Fract32, Fl2FxPnt16, Fl2FxPnt32, Fl2FxPnt

• Float-To-Integer: Fl2Int16, Fl2Int32

Library Interface

Functions

NameDescription
CosInDegsCalculates fixed point Cos(theta) (Q0.15 fixed point), where theta is in degrees (Q8.6 fixed point).
Fract_10toX_Q15bfpCalculates pow(2,x) with fixed point math.
Fract_10toX_Q15bfpFromQ15d16Calculates pow(2,x) with fixed point math.
Fract_10toX_Q15d16Calculates pow(2,x) with fixed point math.
Fract_2toMinusX_Q15Calculates pow(2,-x) with fixed point math.
Fract_2toX_Q15bfpCalculates pow(2,x) with fixed point math.
Fract_2toX_Q15bfpFromQ15d16Calculates pow(2,x) with fixed point math.
Fract_2toX_Q15d16Calculates pow(2,x) with fixed point math.
Fract_Convert_Q15bfpToQ15d16Convert Q15 block floating point to Q15.16 fixed point.
Fract_Convert_Q15bfpToQ31Convert Q15 block floating point to Q31 fixed point.
Fract_Convert_Q15d16ToQ15bfpConvert from Q15.16 fixed point to Q15 Block Floating Point.
Fract_Convert_Q15ToQ15bfpConvert from Q15 fixed point to Q15 Block Floating Point.
Fract_InversePowerComputes pow(nBase,-nExponent).
Fract_log10fixCalculates fixed point base 10 logarithm using number of fractional bits specified by precision argument.
Fract_log2fixCalculates fixed point base 2 logarithm using number of fractional bits specified by precision argument.
Fract_PowerComputes pow(nBase,nExponent) using for loop.
Fract_sqrtQ15Fixed point Q15 squareroot.
Fract_XminusY_Q15bfpSubtract two Q15 block floating point numbers.
Fract_XoverY_Q15bfpDivision x/y as Q15 block floating point.
Fract_XoverY_Q15bfpFromQ15Calculate ratio of X/Y as Q15s but return result as Q15 block floating point.
Fract_XoverY_Q15d16FromQ15Calculates ratio X/Y for two Q15 fixed points.
Fract_XplusY_Q15bfpAdd two Q15 block floating point numbers.
Fract_XtimesY_Q15Calculates product of X*Y for Q15 fixed point.
Fract_XtimesY_Q15bfpMultiply X times Y, for Q15 block floating point arguments.
Fract_XtimesY_Q15d16Calculates product of X*Y for Q15.16 fixed point.
Fract_XtimesY_Q31Calculates product of X*Y for Q31 fixed point.
Fx16NormNormalize the 16-bit fractional value.
Fx32NormNormalize the 32-bit number.
libq_q15_Abs_q15Saturated Absolute value.
libq_q15_Add_q15_q15Add two 16-bit 2s-complement fractional values.
libq_q15_DivisionWithSaturation_q15_q15Fractional division with saturation.
libq_q15_ExpAvg_q15_q15_q1d15Exponential averaging.
libq_q15_ExtractH_q31Extracts upper 16 bits of input 32-bit fractional value.
libq_q15_ExtractL_q31Extracts lower 16-bits of input 32-bit fractional value.
libq_q15_MacR_q31_q15_q15Multiply accumulate with rounding.
libq_q15_MsuR_q31_q15_q15Multiply-Subtraction with rounding.
libq_q15_MultiplyR2_q15_q15Fractional multiplication of two 16-bit fractional values giving a 16 bit rounded result.
libq_q15_Negate_q15Negate 16-bit 2s-complement fractional value with saturation.
libq_q15_RoundL_q31Rounds the lower 16-bits of the 32-bit fractional input.
libq_q15_ShiftLeft_q15_i16Arithmetic Shift of the 16-bit input argument.
libq_q15_ShiftRight_q15_i16Arithmetic RIGHT Shift on a 16-bit value.
libq_q15_ShiftRightR_q15_i16Performs an Arithmetic RIGHT Shift on a 16-bit input.
libq_q15_Sub_q15_q15Subtract two 16-bit 2s-complement fractional values.
libq_q1d15_Sin_q10d6Approximates the sine of an angle.
libq_q20d12_Sin_q20d123rd order Polynomial apprx. of a sine function.
libq_q31_Abs_q31Saturated Absolute value.
libq_q31_Add_q31_q31Add two 32-bit 2s-complement fractional values.
libq_q31_DepositH_q15Place 16 bits in the upper half of 32 bit word.
libq_q31_DepositL_q15Place 16 bits in the lower half of 32 bit word.
libq_q31_Mac_q31_q15_q15Multiply-Accumulate function WITH saturation.
libq_q31_Msu_q31_q15_q15L_msu(a,b,c)
libq_q31_Mult2_q15_q15Fractional multiplication of two 16-bit fractional values.
libq_q31_Multi_q15_q31Implement 16 bit by 32 bit multiply.
libq_q31_Negate_q31Negate 32-bit 2s-complement fractional value with saturation.
libq_q31_ShiftLeft_q31_i16Arithmetic Shift of the 32-bit value.
libq_q31_ShiftRight_q31_i16Arithmetic RIGHT Shift on a 32-bit value.
libq_q31_ShiftRightR_q31_i16Arithmetic RIGHT Shift on a 32-bit value
libq_q31_Sub_q31_q31Subtract two 32-bit 2s-complement fractional values
LUTwInterpolation_Q15Calculates calculates y = f(x) based on a lookup table with linear interpolation between table entries.
SinInDegsCalculates fixed point Sin(theta) (Q0.15 fixed point), where theta is in degrees (Q8.6 fixed point)