Iqmath
Iqmath
Iqmath
IMPORTANT NOTICE
Texas Instruments and its subsidiaries (TI) reserve the right to make changes to their products or to discontinue any product or service without notice, and advise customers to obtain the latest version of relevant information to verify, before placing orders, that information being relied on is current and complete. All products are sold subject to the terms and conditions of sale supplied at the time of order acknowledgement, including those pertaining to warranty, patent infringement, and limitation of liability. TI warrants performance of its semiconductor products to the specifications applicable at the time of sale in accordance with TIs standard warranty. Testing and other quality control techniques are utilized to the extent TI deems necessary to support this warranty. Specific testing of all parameters of each device is not necessarily performed, except those mandated by government requirements. Customers are responsible for their applications using TI components. In order to minimize risks associated with the customers applications, adequate design and operating safeguards must be provided by the customer to minimize inherent or procedural hazards. TI assumes no liability for applications assistance or customer product design. TI does not warrant or represent that any license, either express or implied, is granted under any patent right, copyright, mask work right, or other intellectual property right of TI covering or relating to any combination, machine, or process in which such products or services might be or are used. TIs publication of information regarding any third partys products or services does not constitute TIs approval, license, warranty or endorsement thereof. Reproduction of information in TI data books or data sheets is permissible only if reproduction is without alteration and is accompanied by all associated warranties, conditions, limitations and notices. Representation or reproduction of this information with alteration voids all warranties provided for an associated TI product or service, is an unfair and deceptive business practice, and TI is not responsible or liable for any such use. Resale of TIs products or services with statements different from or beyond the parameters stated by TI for that products or service voids all express and any implied warranties for the associated TI product or service, is an unfair and deceptive business practice, and TI is not responsible nor liable for any such use. Also see: Standard Terms and Conditions of Sale for Semiconductor Products. www.ti.com/sc/docs/stdterms.htm Mailing Address: Texas Instruments Post Office Box 655303 Dallas, Texas 75265
Trademarks
TMS320 is the trademark of Texas Instruments Incorporated. All other trademarks mentioned herein are property of their respective companies
Acronyms
C28x+FPU: Refers to devices with the C28x plus floating-point-unit. IQmath: High Accuracy Mathematical Functions (32-bit implementation). QMATH: Fixed Point Mathematical computation
Chapter 1.
1.1.
Introduction................................................................................................................. 6
Chapter 2.
2.1. 2.2.
IQmath Package Contents .......................................................................................... 7 How to Install the IQmath Library ............................................................................ 8
Chapter 3.
3.1. 3.2. 3.3. 3.4. 3.5. 3.6. 3.7. 3.8. 3.9. 3.10. 3.11.
IQmath Arguments and Data Types........................................................................... 9 IQmath Data type: Range & Resolution................................................................... 10 Calling an IQmath Function from C ........................................................................ 11 Calling an IQmath function from C++ ..................................................................... 11 IQmath Section and Lookup Tables ......................................................................... 12 Accessing IQmath Functions in the Boot ROM ....................................................... 16 IQmath Naming Conventions ................................................................................... 19 Selecting the GLOBAL_Q format ............................................................................ 20 Using the IQmath GEL file for Debugging ............................................................... 21 Converting an IQmath Application to Floating-Point .......................................... 23 The IQmath C-Calling Convention ....................................................................... 23
Chapter 4. Chapter 5.
5.1. 5.2.
5.3.
Chapter 6.
6.1.
6.2.
6.3.
6.4.
Trigonometric Functions........................................................................................... 55
IQNasin ........................................................................................................................ 55 IQNsin .......................................................................................................................... 56 IQNsinPU ..................................................................................................................... 58 IQNacos ....................................................................................................................... 60 IQNcos ......................................................................................................................... 61 IQNcosPU .................................................................................................................... 63 IQNatan2 ...................................................................................................................... 65 IQNatan2PU ................................................................................................................. 67 IQNatan ........................................................................................................................ 69
6.5.
6.6.
Chapter 7.
Chapter 1. Introduction
1.1. Introduction
Texas Instruments TMS320C28x IQmath Library is collection of highly optimized and high precision mathematical functions for C/C++ programmers to seamlessly port a floating-point algorithm into fixed point code on TMS320C28x devices. These routines are typically used in computationally intensive real-time applications where optimal execution speed and high accuracy is critical. By using these routines you can achieve execution speeds considerable faster than equivalent code written in standard ANSI C language. In addition, by providing readyto-use high precision functions, TI IQmath library can shorten significantly your DSP application development time.
<base>\lib <base>\gel
The following example project run on CCS V4: <base>\examples_ccsv4\C <base>\examples_ccsv4\C\<device> <base>\examples_ccsv4\C\source <base>\examples_ccsv4\Cpp C example: Refer to ReadMe_SampleC.txt Each device family has its own project folder Shared source code for the C example C++ code example: Refer to ReadMe_SampleCpp.txt
<base>\examples_ccsv4\Cpp\source Shared source code for the C++ example <base>\examples_ccsv4\Cpp\<device> Each device family has its own project folder <base>\examples\cmd Linker command files used by the examples
The following example project runs on CCS V3.3: <base>\examples\C <base>\examples\C\projects <base>\examples\C\source <base>\examples\Cpp <base>\examples\Cpp\projects <base>\examples\Cpp\source <base>\examples\cmd C example: Refer to ReadMe_SampleC.txt CCS projects for the C example Source code for the C example C++ code example: Refer to ReadMe_SampleCpp.txt CCS projects for the C++ example Source code for the C++ example Linker command files used by the examples
10
*/
11
Call the functions using the iq and iqN classes along with the C++ code function definitions. In C++ the functions are called without the leading underscore and the math operations are overloaded For example: C Code _iq, _iqN _IQ(A), _IQN(A) _IQdiv(A,B) C++ Code iq, iqN IQ(A), IQN(A) A/B Note IQ Data Types Convert float to IQ Division
The following example code contains a call to the IQ25sin routines in IQmath Library using C++ code: extern "C" { #include "IQmathLib.h" } #include "IQmathCPP.h" #define PI 3.14159 iq input, sin_out; void main(void ) { /* 0.25 x PI radians represented in Q29 format input = IQ29(0.25*PI); sin_out = IQ29sin(input); }
*/
12
Assembly Section Name (i.e. .sect) IQmathTables IQmathTables IQmathTables IQmathTables IQmathTablesRam (IQNexpTable.obj only) IQmathTables IQmathTablesRam (IQNexpTable.obj) IQmathTablesRam (IQNasinTable.obj)
Boot ROM Location 0x3FF000 0x3FF000 0x3FF000 0x3FE000 0x3FEB50 0x3FE000 0x3FEB50 0x3FEBDC
If your device is not listed, refer to the boot ROM reference guide and the boot ROM source code for your device to get more information. If a table is not included in the boot ROM of the device, then it must be loaded into the appropriate memory in the linker command file. NOTE: IQmathTablesRam The section name IQmathTablesRam may be a bit misleading. This table contains initialized data tables for the IQexp, IQasin, and IQacos functions. During debug, these tables can be loaded directly into SARAM as shown in the linker file below. During stand-alone operation, if these functions are used, then the table should be loaded into non-volatile memory (for example flash). If you want to access them in SARAM then copy them from flash to SARAM during initialization.
IQdivTable QdivTableEnd IQdivRoundSatTable IQmathTables (IQmathTables.obj) IQsqrtTable IQsqrtTableEnd IQisqrtTable IQisqrtTableEnd IQsqrtRoundSatTable IQisqrtRoundSatTable IQexpTable IQexpTableMinMax IQexpTableMinMaxEnd IQexpTableCoeff IQexpTableCoeffEnd IQasinTable
13
The following is an example of a linker file for the 281x devices. In this case only the IQmathTables section is in the boot ROM. If used, the IQmathTablesRam section must be loaded into the device along with the application itself. IQmath Linker Command File Example: 281x Devices MEMORY { PAGE 0: PRAMH0 (RW) : origin = 0x3f8000, length = 0x001000 PAGE 1: IQTABLES (R) : origin = 0x3FF000, length = 0x000b50 DRAMH0 (RW) : origin = 0x3f9000, length = 0x001000 } SECTIONS { IQmathTables : load = IQTABLES, type = NOLOAD, PAGE = 1 IQmathTablesRam : load = DRAMH0, PAGE = 1 IQmath : load = PRAMH0, PAGE = 0 }
The 2833x and 2823x boot ROM includes the IQNexpTable used by the IQNexp() and IQexp() functions. In this case the IQNexpTable can be placed into its own section and identified as type NOLOAD as shown in the next example. The remainder of the IQmathTablesRam section is allocated to SARAM.
IQmath Linker Command File Example: 2823x Device MEMORY { PAGE 0: PRAML0 (RW) : origin = 0x008000, length = 0x001000 PAGE 1: IQTABLES (R) : origin = 0x3FE000, length = 0x000b50 IQTABLES2 (R) : origin = 0x3FEB50, length = 0x00008c DRAML1 (RW) : origin = 0x009000, length = 0x001000 } SECTIONS { IQmathTables : load = IQTABLES, type = NOLOAD, PAGE = 1 IQmathTables2 > IQTABLES2, type = NOLOAD, PAGE = 1 { IQmath.lib<IQNexpTable.obj> (IQmathTablesRam) } IQmathTablesRam : load = DRAML1, PAGE = 1 IQmath : load = PRAML0, PAGE = 0 }
14
Some devices, such as the 2802x and 2803x, include both IQNexpTable used by the IQNexp() and IQexp() functions as well as the IQasinTable used by the IQasin() and IQNasin() functions. In this case the IQNexpTable and IQasinTable can be placed into their own section and identified as type NOLOAD as shown in the next example.
IQmath Linker Command File Example: 2803x Device MEMORY { PAGE 0: PRAML0 (RW) : origin = 0x008000, length = 0x001000 PAGE 1: IQTABLES (R) : origin = 0x3FE000, length = 0x000b50 IQTABLES2 (R) : origin = 0x3FEB50, length = 0x00008c IQTABLES3 (R) : origin = 0x3FEBDC, length = 0x0000AA DRAML1 (RW) : origin = 0x009000, length = 0x001000 } SECTIONS { IQmathTables : load = IQTABLES, type = NOLOAD, PAGE = 1 IQmathTables2 > IQTABLES2, type = NOLOAD, PAGE = 1 { IQmath.lib<IQNexpTable.obj> (IQmathTablesRam) } IQmathTables3 > IQTABLES3, type = NOLOAD, PAGE = 1 { IQmath.lib<IQNasinTable.obj> (IQmathTablesRam) } IQmath : load = PRAML0, PAGE = 0 }
NOTE: When including IQNexpTable.obj and/or IQNasinTable.obj in the linker file SECTIONS: It should be noted that if the IQexp() function is not called in the application, then the allocation for IQNexpTable.obj will result in a linker warning. Likewise if IQasin() or IQacos() are not called, then IQNasinTable.obj will result in a linker warning. This is because the table will not be included if it is not used. This warning will not cause any ill effects to the normal operation of the application.
15
To easily use the boot ROM copies of the functions in place of the existing functions, an IQmath boot ROM symbol library has been provided with the boot ROM source code. This source code is available for download from the TI website with the boot ROM reference guide. You will link the IQmath Boot ROM symbol library before the normal IQmath library. The boot ROM symbol library replaces only the subset of functions in the boot ROM. Therefore, the standard IQmath library should be linked after the boot ROM symbol library. The following pages give an overview of how to set the link order for the library in CCS V3.3 and CCS V4. To confirm that the boot ROM copies of the functions are being used, check the generated .map file. Look for the symbols of the functions. For example, the address of the IQ24div, IQ24exp and IQ24sin symbols shown below are from the boot ROM area of the 2803x device. The remaining symbols are in RAM. Address 0000821a 003ff083 000082a2 003fef35 000082df 003ff3c2 Symbol __IQ24atan2PU __IQ24div __IQ24exp __IQ24mag __IQ24mpyI32int __IQ24sin
16
The following instructions apply to Code Composer Studio V4 1. In the C/C++ perspective, right click on the project and add both the boot ROM Symbol Library (for example: 2802x_IQmath_BootROMSymbols.lib) and the IQmath library files to the project. 2. Right click on the project and select properties. 3. Select the CCS Build Options and the link order tab. 4. Add both the boot ROM symbol library and the IQmath library to the link order. Adjust the order if required such that the symbol library is linked in first as shown below. 5. Under the C/C++ Build options, go to the Tools Setting Tab -> C2000 Linker options -> File Search Path: 6. Check the options Search libraries in priority order (-priority) Reread libraries; resolve backward references (-x) 7. Click ok.
17
The following instructions apply to Code Composer Studio V3.3 1. Add the boot ROM symbol library (for example: 2802x_IQmath_BootROMSymbols.lib) and the standard IQmath.lib library to the project using Project->Add Files to Project Once the libraries are added to the project they will appear in the link order tab of the build options. 2. Open the build options dialog box under Project->Build Options 3. Under the Linker->Advanced tab, select the priority linker switch. This will force the linker to resolve symbols to the first library linked. 4. Under the Link Order tab, select the two libraries and add them to the link order. Use the up/down arrows to arrange them in the proper order. The first library listed will be linked first. 5. Under the Linker->Libraries dialog add the path to the 2802x_IQmath_BootROMSymbols.lib (or 2803x_IQmath_BootROMSymbols.lib) and the IQmath library in the search path. Do not include either libraries in the Incl. Libraries box. Doing so can cause problems when changing the link order since Code Composer uses both this field and the link order tab to determine which object files are linked first. 6. Save the project. (Project->Save).
18
C++ Code Examples: IQsin(A) IQcos(A) IQrmpy(A,B) A*B /* High Precision SIN /* High Precision COS /* IQ multiply with rounding /* IQ multiply */ */ */ */
Q-format specific functions to cater to Q1 to Q30 data format. C-Code Examples: _IQ29sin(A) _IQ28sin(A) _IQ27sin(A) _IQ26sin(A) _IQ25sin(A) _IQ24sin(A) /* High Precision SIN: input/output are in Q29 /* High Precision SIN: input/output are in Q28 /* High Precision SIN: input/output are in Q27 /* High Precision SIN: input/output are in Q26 /* High Precision SIN: input/output are in Q25 /* High Precision SIN: input/output are in Q24 */ */ */ */ */ */
C++ Code Examples: IQ29sin(A) IQ28sin(A) IQ27sin(A) IQ26sin(A) IQ25sin(A) IQ24sin(A) /* High Precision SIN: input/output are in Q29 /* High Precision SIN: input/output are in Q28 /* High Precision SIN: input/output are in Q27 /* High Precision SIN: input/output are in Q26 /* High Precision SIN: input/output are in Q25 /* High Precision SIN: input/output are in Q24 */ */ */ */ */ */
19
_IQNxxx( ), Where xxx is the Function Name & N is the Q format of input/output
24
/* Q1 to Q29
*/
CASE II : A complete system consists of various modules. Some modules may require different precision, then the rest of the system. In such situation, we need to over-ride the GLOBAL_Q defined in the IQmathLib.h file and use the local Q format. This can be easily done by defining the GLOBAL_Q constant in the source file of the module before the include statement.
20
21
Step 4: Modifying IQmath variable The watch window does not allow the modification of variables that are not of native type. To facilitate this, the following GEL operations can be found under the GEL toolbar: IQ C Support SetIQvalue Set2IQvalues Set3IQvalues SetIQNvalue Set2IQNvalues Set3IQNvalues ; GLOBAL_Q format ; IQN format
22
Invoking one of the above GEL operations will bring up a dialogue box window, which the user can enter the variable name and the floating-point value to set. The function will convert the float value to the appropriate IQ value.
23
The IQmath.lib library has been built for use on standard C28x fixed-point devices. This library can be used with both C and C++ code. IQmath_fpu32.lib
The IQmath_fpu32.lib library is useful for users who want to use both IQmath functions as well as native C28x floating-point code within their project. This library can be used with both C and C++ code. The IQmath_fpu32.lib library has been built with the following compiler flags: -v28 --float_support=fpu32 IQmath_fpu32.lib still contains the same IQmath functions as IQmath.lib. It does not contain native floating-point functions. The float_support option is available in the C28x compiler version 5.0.0 and later.
24
_iqN
iq iqN A, B F S P N
25
Float ASCII string to IQ IQ to ASCII string extract integer portion of IQ extract fractional portion of IQ Convert IQ number to IQN number (32bit) Convert IQN (32-bit) number to IQ number Convert IQ number to QN number (16bit) Convert QN (16-bit) number to IQ number
Q=GLOBAL_Q Q=1:30 Q=GLOBAL_Q Q=1:30 Q=GLOBAL_Q Q=1:30 Q=GLOBAL_Q Q=1:30 Q=GLOBAL_Q Q=GLOBAL_Q Q=GLOBAL_Q Q=GLOBAL_Q
_iqN _atoIQN( char *S) int _IQtoa(char *S, const * format, long x) int _IQNtoa(char *S, const * format, long x)
_iq _IQfrac( _iq A) _iqN _IQNfrac( _iqN A) _iqN _IQtoIQN( _iq A) _iq _IQNtoIQ( _iqN A) int _IQtoQN( _iq A)
_iq _QNtoIQ( int A)
26
Arithmetic Operations
Functions _iq _IQmpy( _iq A, _iq B) Description IQ Multiplication IQ format Q=GLOBAL_Q Q=1:30 IQ Multiplication with rounding Q=GLOBAL_Q Q=1:30 IQ multiplication with rounding & saturation Q=GLOBAL_Q Q=1:30
Multiply IQ with "long" integer Multiply IQ with "long", return integer part Multiply IQ with "long", return fraction part Multiply two 2-different IQ number
_iqN _IQNmpyI32( _iqN A, long B) long _IQmpyI32int( _iq A, long B) long _IQNmpyI32int( _iqN A, long B) long _IQmpyI32frac( _iq A, long B) long _IQNmpyI32frac( _iqN A, long B) _iq _IQmpyIQX( _iqN1 A, N1, _iqN2 B, N2 )
_iqN _IQmpyIQX( _iqN1 A, N1, _iqN2 B, N2 ) _iq _IQdiv( _iq A, _iq B) Fixed point division
Q=GLOBAL_Q Q=1:30
Trigonometric Functions:
Functions _iq _IQasin( _iq A) Description High precision ASIN (output in radians) IQ format Q=GLOBAL_Q Q=1:29 High precision SIN (input in radians) Q=GLOBAL_Q Q=1:29 High precision SIN (input in per-unit) Q=GLOBAL_Q Q=1:30 High precision ACOS (output in radians) Q=GLOBAL_Q Q=1:30 High precision COS (Input in radians) Q=GLOBAL_Q Q=1:29 High precision COS (input in per-unit) Q=GLOBAL_Q Q=1:30 4-quadrant ATAN (output in radians) Q=GLOBAL_Q Q=1:29 4-quadrant ATAN (output in per-unit) Q=GLOBAL_Q Q=1:29
_iqN _IQNasin( _iqN A) _iq _IQsin( _iq A) _iqN _IQNsin( _iqN A) _iq _IQsinPU( _iq A)
Arctangent
Q=GLOBAL_Q Q=1:29
27
Mathematical Functions:
Functions _iq _IQexp( _iq A) _iqN _IQNexp( _iqN A) _iq _IQsqrt( _iq A) _iqN _IQNsqrt( _iqN A) _iq _IQisqrt( _iq A) _iqN _IQNisqrt( _iqN A) _iq _IQmag( _iq A, _iq B) _iqN _IQNmag( _iqN A, _iqN B) High precision inverse square root High precision square root Description High precision e raised to the A power IQ format Q=GLOBAL_Q Q=1:29 Q=GLOBAL_Q Q=1:30 Q=GLOBAL_Q Q=1:30
Q=GLOBAL_Q Q=1:30
Miscellaneous
Functions _iq _IQsat( _iq A, long P, long N) _iq _IQabs( _iq A) Description Saturate the IQ number Absolute value of IQ number Q format Q=GLOBAL_Q Q=GLOBAL_Q
28
Trigonometric Functions IQNasin IQNsin IQNsinPU IQNacos IQNcos IQNcosPU IQNatan2 IQNatan2PU IQatan IQNexp IQNsqrt IQNisqrt IQNmag IQNmpy IQNrmpy IQNrsmpy IQNmpyI32 IQNmpyI32int IQNmpyI32frac IQNmpyIQX IQNdiv IQN IQNtoF IQNtoa atoIQN IQNint IQNfrac IQtoIQN IQNtoIQ IQtoQN QNtoIQ IQsat IQNabs 1-30 1-30 1-30 1-30 1-30 1-30 1-30 1-30 1-30 1-15 1-15 1-30 1-30 1-29 1-29 1-30 1-29 1-29 1-30 1-29 1-29 1-29 1-29 1-30 1-30 1-30 1-30 1-30 1-30 1-30 1-30 1-30 154 46 40 170 44 38 109 117 109 190 63 64 86 ~6 17 21 ~4 22 24 ~7 63 NA 22 N/A N/A 14 17 ~4 ~4 ~4 ~4 ~7 ~2 IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN*IQN IQN*IQN IQN*IQN IQN*long IQN*long IQN*long IQN*IQN IQN/IQN Float IQN IQN char * IQN IQN GLOBAL_Q IQN GLOBAL_Q QN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN IQN long IQN IQN IQN IQN Float string IQN long IQN IQN GLOBAL_Q QN GLOBAL_Q IQN IQN C-MACRO C-MACRO C-MACRO C-MACRO INTRINSIC INTRINSIC C-MACRO INTRINSIC C-MACRO INTRINSIC Note A Note A Note A
Miscellaneous
29
Notes: A. IQNexp, IQNasin and IQNacos use look-up tables in the IQmathTablesRam section. Refer to Section 3.3. B. Execution cycles & Program memory usage mentioned in the Table assumes IQ24 format. Execution cycles may vary by few cycles for some other IQ format. Program memory may vary by few words for some other IQ format. C. Execution Cycles mentioned in the table includes the CALL and RETURN (LCR + LRETR) and it assumes that the IQmath table is loaded in internal memory. D. Accuracy should always be verified and tested within the end application.
30
This C-macro converts a floating-point constant or variable to the equivalent IQ value. Global IQ Macro (IQ format = GLOBAL_Q) C _iq _IQ(float F) C++ iq IQ(float F) Q format specific IQ Macro (IQ format = IQ1 to IQ29) C _iqN _IQN(float F) C++ iq IQN(float F)
Input Output
Floating point variable or constant Global IQ Macro (IQ format = GLOBAL_Q) Fixed point equivalent of floating-point input in GLOBAL_Q format Q format specific IQ Macro (IQ format = IQ1 to IQ29) Fixed point equivalent of floating-point input in IQN format
Usage
This operation is typically used to convert a floating-point constant or variable to the equivalent IQ value. Example 1: Implement an equation in IQmath // Floating-point equation Y = M*1.26 + 2.345 // IQmath equation using the GLOBAL_Q value Y =_IQmpy(M, _IQ(1.26)) + _IQ(2.345) // IQmath equation specifying the Q value Y =_IQ23mpy(M, _IQ23(1.26)) +_IQ23(2.345) Example 2: Convert a floating-point variable to an IQ data type. #include IQmathLib.h float x=3.343; _iq y1; _iq23 y2 y1=_IQ(x) y2=_IQ23(x) // Uses the GLOBAL_Q value // Specifies the Q value
31
Example 3:
#include IQmathLib.h // IQmath using GLOBAL_Q _iq Array[4] = {_IQ(1.0), _IQ(2.5) _IQ(-0.2345), _IQ(0.0) } // IQmath specifying the Q value _iq23 Array[4] = {_IQ23(1.0), _IQ23(2.5) _IQ23(-0.2345), _IQ23(0.0) }
32
IQNtoF IQNtoF
Description Declaration
This function converts an IQ number to equivalent floating-point value in IEEE 754 format. Global IQ function (IQ format = GLOBAL_Q) C float _IQtoF(_iq A) C++ float IQtoF(const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ30) C float _IQNtoF(_iqN A) C++ float IQNtoF(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Fixed point IQ number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Fixed point IQ number in IQN format.
Output Usage
Floating point equivalent of fixed-point input. This operation is typically used in cases where the user may wish to perform some operations in floating-point format or convert data back to floating-point for display purposes. Example: Convert an array of IQ numbers to their equivalent floating-point values _iq DataIQ[N]; float DataF[N]; for(i = 0; i < N; i++) { DataF[i] = _IQtoF(DataIQ[i]); }
33
atoIQN atoIQN
Description Declaration This function converts a string to an IQ number. Global IQ function (IQ format = GLOBAL_Q) C float _atoIQ(char *S) C++ float atoIQ(char *S) Q format specific IQ function (IQ format = IQ1 to IQ30) C float _atoIQN(char *S) C++ float atoIQN(char *S) Input
String to IQN
This function recognizes (in order) an optional sign, a string of digits optionally containing a radix character. Valid Input strings: 12.23456, -12.23456, 0.2345, 0.0 , 0 , 127 , -89
Output
The first unrecognized character ends the string and returns zero. If the input string converts to a number greater then the max/min values for the given Q value, then the returned value will be limited to the min/max values Global IQ function (IQ format = GLOBAL_Q) Fixed point equivalent of input string in GLOBAL_Q format Q format specific IQ function (IQ format = IQ1 to IQ29) Fixed point equivalent of input string in IQN format
Usage
This is useful for programs that need to process user input or ASCII strings. Example: The following code prompts the user to enter the value X: char buffer[N]; _iq X; printf("Enter value X = "); gets(buffer); X = _atoIQ(buffer);
// IQ value (GLOBAL_Q)
34
IQNtoa IQNtoa
Description Declaration This function converts an IQ number to a string.
IQN to String
Global IQ function (IQ format = GLOBAL_Q) C int _IQtoa(char *string, const char *format, _iq x) C++ int IQtoa(char *string, const char *format, const iq &x) Q format specific IQ function (IQ format = IQ1 to IQ30) C int _IQNtoa(char *string, const char *format, _iqN x) C++ int IQNtoa(char *string, const char *format, const iqN &x)
Input
string: output string format: conversion format. Must be of the form "%xx.yyf" with xx and yy at most 2 characters in length. For Example: "%10.12f", "%2.4f", "%11.6f" The maximum supported integer field width (xx) is 11 (including any negative sign). This captures the full integer range for I2Q30 to I31Q1 numbers. x: Global IQ function: input value in IQ format Q format specific function: input value in IQN format
Output
The output string is returned in the location pointed to by the string argument. If you are using MATH_TYPE set to IQ_MATH, then the return integer value is an error code with the following possible values: 0 = no error 1 = width too small to hold integer characters 2 = illegal format specified If you are using MATH_TYPE set to FLOAT_MATH, then sprintf() is called and the return integer value is the number of characters written.
Usage Any leading zeros are not printed for the integer part. Hence, the format specifies the maximum width of the integer field. The field may be smaller. The output string is terminated with the null character. The integer width in "format" includes the negative sign for negative numbers, e.g. -12.3456 is "%3.5f" The decimal width in "format" includes the decimal point. For example: 12.3456 is "%3.5f" "string" must be large enough to hold the output (including the negative sign, and the terminating null character). The program does not check for overrun. Memory corruption will occur if "string" is too small. A non-zero return value indicates that the output string is invalid.
35
Example: char buffer[30]; _iq x1 = _iq1 x2 = _iq14 x3 = _iq30 x4 = int error; _IQ(1.125); _IQ1(-6789546.3); _IQ14(-432.6778); _IQ30(1.127860L);
// Global_Q error = _IQtoa(buffer, "%10.10f", x1); // IQ1 error = _IQ1toa(buffer, "%8.2f", x2); // IQ14 error = _IQ14toa(buffer, "%6.6f", x3); // IQ30 error = _IQ30toa(buffer, "%11.12f", x4);
36
IQNint IQNint
Description Declaration
This function returns the integer portion of IQ number. Global IQ function (IQ format = GLOBAL_Q) C long _IQint(_iq A) C++ long IQint(const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ30) C long _IQNint(_iqN A) C++ long IQNint(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Fixed point IQ number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Fixed point IQ number in IQN format.
Output Usage
Integer part of the IQ number Example 1: Extract the integer and fractional part of an IQ number. _iq Y0 = 2.3456; _iq Y1 = -2.3456 long Y0int, Y1int; _iq Y0frac, Y1frac; Y0int Y1int Y0frac Y1frac = = = = _IQint(Y0); _IQint(Y1); _IQfrac(Y0); _IQfrac(Y1); // // // // Y0int = 2 Y1int = -2 Y0frac = 0.3456 Y1frac = -0.3456
Example 2: Build an IQ number from an integer and fractional part _iq Y; long Yint; _iq Yfrac; Y = _IQmpyI32(_IQ(1.0), Yint) + Yfrac;
37
IQNfrac IQNfrac
Description Declaration
This function returns the fractional portion of IQ number. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQfrac(_iq A) C++ iq IQfrac(const iq &A) Q format specific IQ function (IQ format = N = 1 to 30) C _iqN _IQNfrac(_iqN A) C++ iqN IQNfrac(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Fixed point IQ number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Fixed point IQ number in IQN format.
Output Usage
Fractional part of the IQ number Example 1: Extract the integer and fractional part of an IQ number _iq Y0 = _IQ(2.3456); _iq Y1 = _IQ(-2.3456); long Y0int, Y1int; _iq Y0frac, Y1frac; Y0int Y1int Y0frac Y1frac = = = = _IQint(Y0); _IQint(Y1); _IQfrac(Y0); _IQfrac(Y1); // // // // Y0int = 2 Y1int = -2 Y0frac = 0.3456 Y1frac = -0.3456
Example 2: Build an IQ number from an integer and fractional part. _iq Y; long Yint; _iq Yfrac; Y = _IQmpyI32(_IQ(1.0), Yint) + Yfrac;
38
IQtoIQN IQtoIQN
Description
This Macro converts an IQ number in GLOBAL_Q format to the specified IQ format. C C++ _iqN _IQtoIQN(_iq A) iqN IQtoIQN(const iq &A)
IQ number in GLOBAL_Q format Equivalent value of input in IQN format This macro may be used in cases where a calculation may temporarily overflow the IQ value resolution and hence require a different IQ value to be used for the intermediate operations. Example: The Following example calculates the magnitude of complex number (X+jY) in Q26 format: Z = sqrt(X^2 + Y^2) The values Z, X, Y are given as GLOBAL_Q = 26, but the equation itself may generate an overflow. To guard against this, the intermediate calculations will be performed using Q = 23 and the value converted back at the end as shown below: #include IQmathLib.h _iq Z, Y, X; _iq23 temp; // GLOBAL_Q = 26
39
IQNtoIQ IQNtoIQ
Description Declaration Input Output Usage
This Macro converts an IQ number in IQN format to the GLOBAL_Q format. C C++ _iq _IQNtoIQ(_iqN A) iq IQNtoIQ(const iqN &A)
IQ number in IQN format Equivalent value of input in GLOBAL_Q format This macro may be used in cases where the result of the calculation performed in different IQ resolution to be converted to GLOBAL_Q format. Example: Following example calculates the magnitude of complex number (X+jY) in Q26 format: Z = sqrt(X^2 + Y^2) The values Z, X, Y are given as GLOBAL_Q = 26, but the equation itself may generate an overflow. To guard against this, the intermediate calculations will be performed using Q = 23 and the value converted back at the end as shown below: #include IQmathLib.h _iq Z, Y, X; _iq23 temp; // GLOBAL_Q = 26
40
IQtoQN IQtoQN
Description
GLOBAL_Q number to QN
This Macro converts a 32-bit number in GLOBAL_Q format to 16-bit number in QN format. C C++ int _IQtoQN(_iq A) int IQtoQN(const iq &A)
IQ number in GLOBAL_Q format Equivalent value of input in QN format (16-bit fixed point number) This macro may be used in cases where the input and output data is 16-bits, but the intermediate operations are operated using IQ data types. Example: Sum of product computation using the input sequence that is not in GLOBAL_Q format: Y = X0*C0 + X1*C1 + X2*C2 // X0, X1, X2 in Q15 format //C0,C1, C2 in GLOBAL_Q format
We can convert the Q15 values to IQ, perform the intermediate sums using IQ and then store the result back as Q15: short X0, X1, X2; iq C0, C1, C2; short Y; _iq sum sum sum sum Y = // // // // Q15 short GLOBAL_Q Q15 IQ (GLOBAL_Q)
41
QNtoIQ QNtoIQ
Description
QN number to GLOBAL_Q
This Macro converts a 16-bit number in QN format to 32-bit number in GLOBAL_Q format. C C++ _iq _QNtoIQ(int A) iq QNtoIQ(int A)
16-bit fixed point number in QN format Equivalent value of input in GLOBAL_Q format This macro may be used in cases where the input and output data is 16-bits, but the intermediate operations are operated using IQ data types. Example: Sum of product computation using the input sequence that is not in GLOBAL_Q format: Y = X0*C0 + X1*C1 + X2*C2 // X0, X1, X2 in Q15 format // C0, C1, C2 in GLOBAL_Q format
We can convert the Q15 values to IQ, perform the intermediate sums using IQ and then store the result back as Q15: short X0, X1, X2; iq C0, C1, C2; short Y; _iq sum sum sum sum Y = // // // // Q15 short GLOBAL_Q Q15 IQ (GLOBAL_Q)
42
This #define macro in the IQmathLib.h can be used to perform a left shift in order to multipy a number by a power of 2. Macros are provided for multiply by 2, 4, 8 16, 32 and 64. When using FLOAT_MATH the corresponding multiply will be applied.
Input A is an IQ number. Output is an IQ number. Example 1; Compute Y = 2*X = X << 1 _iq Y; Y = _IQmpy2(X); Compute Y = 16*X = X << 4 _iq Y; Y = _IQmpy16(X);
43
This #define macro in the IQmathLib.h can be used to perform a right shift in order to multipy a number by a power of 2. Macros are provided for divide by 2, 4, 8 16, 32 and 64. When using FLOAT_MATH the corresponding multiply (i.e. .5, .25 etc) will be applied.
Input A is an IQ number. Output is an IQ number. Example 1; Compute Y = 2*X = X << 1 _iq Y; Y = _IQdiv2(X); Compute Y = 16*X = X << 4 _iq Y; Y = _IQdiv16(X);
44
This C compiler intrinsic multiplies two IQ number. It does not perform saturation and rounding. In most cases, the multiplication of two IQ variables will not exceed the range of the IQ variable. This operation takes the least amount of cycles and code size and should be used most often. Global IQ intrinsic (IQ format = GLOBAL_Q) C _iq _IQmpy(_iq A, _iq B) C++ iq operator * (const iq &A, const iq &B) iq &iq :: operator *= (const iq &A) Q format specific IQ intrinsic (IQ format = IQ1 to IQ30) C _iqN _IQNmpy(_iqN A, _iqN B) C++ iqN operator * (const iqN &A, const iqN &B) iqN &iqN :: operator *= (const iqN &A)
Declaration
Input
Global IQ intrinsic (IQ format = GLOBAL_Q) Inputs A and B are IQ numbers in GLOBAL_Q format Q format specific IQ intrinsic (IQ format = IQ1 to IQ30) Inputs A and B are IQ numbers in IQN format
Output
Global IQ intrinsic (IQ format = GLOBAL_Q) Result of multiplication in GLOBAL_Q format Q format specific IQ intrinsic (IQ format = IQ1 to IQ30) Result of multiplication in IQN format.
Usage
Example 1; Compute Y = M*X + B in GLOBAL_Q format with no rounding or saturation. _iq Y, M, X, B; Y = _IQmpy(M,X) + B; Example 2: Compute Y = M*X + B in IQ10 format with no rounding or saturation, assuming M, X, B are represented in IQ10 format. _iq10 Y, M, X, B; Y = _IQ10mpy(M,X) + B;
45
IQNrmpy IQNrmpy
Description
This function multiplies two IQ number and rounds the result. In cases where absolute accuracy is necessary, this operation performs the IQ multiply and rounds the result before storing back as an IQ number. This gives an additional 1/2 LSBit of accuracy. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQrmpy(_iq A, _iq B) C++ iq IQrmpy(const iq &A, const iq &B) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNrmpy(_iqN A, _iqN B) C++ iqN IQNrmpy(const iqN &A, const iqN &B)
Declaration
Input
Global IQ function (IQ format = GLOBAL_Q) Input A & B are IQ number in GLOBAL_Q format Q format specific IQ function (IQ format = IQ1 to IQ30) Input A & B are IQ number in IQN format
Output
Global IQ function (IQ format = GLOBAL_Q) Result of multiplication in GLOBAL_Q format Q format specific IQ function (IQ format = IQ1 to IQ30) Result of multiplication in IQN format.
Usage
Example 1: Compute Y = M*X + B in GLOBAL_Q format with rounding but without saturation. _iq Y, M, X, B; Y = _IQrmpy(M,X) + B; Example 2: Compute Y = M*X + B in IQ10 format with rounding but without saturation. _iq10 Y, M, X, B; Y = _IQ10rmpy(M,X) + B;
46
IQNrsmpy IQNrsmpy
Description
This function multiplies two IQ number with rounding and saturation. In cases where the calculation may possibly exceed the range of the IQ variable, then this operation will round and then saturate the result to the maximum IQ value range before storing. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQrsmpy(_iq A, _iq B) C++ iq IQrsmpy(iq &A, iq &B) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNrsmpy(_iqN A, _iqN B) C++ iqN IQNrsmpy(const iqN &A, const iqN &B)
Declaration
Input
Global IQ function (IQ format = GLOBAL_Q) Input A & B are IQ number in GLOBAL_Q format Q format specific IQ function (IQ format = IQ1 to IQ30) Input A & B are IQ number in IQN format
Output
Global IQ function (IQ format = GLOBAL_Q) Result of multiplication in GLOBAL_Q format Q format specific IQ function (IQ format = IQ1 to IQ30) Result of multiplication in IQN format.
Usage
Let us assume that we use IQ26 are GLOBAL_Q format. This means that the range of the numbers is approximately [-32.0, 32.0] (Refer section 3.2). If two IQ variables are multiplied together, then the maximum range of the result is [-1024, 1024]. This operation would make sure that the result is saturated to +/- 32 in cases where the result exceeds this. Example 1: Compute Y = M*X in the GLOBAL_Q format with rounding and saturation. Assume GLOBAL_Q=IQ26 in the IQmath header file. _iq Y, M, X; M=_IQ(10.9); // M=10.9 X=_IQ(4.5); // X=4.5 Y = _IQrmpy(M,X); // Y= ~32.0, output is Saturated to MAX Example 2: Compute Y = M*X in IQ26 format with rounding and saturation. _iq26 Y, M, X; M=_IQ26(-10.9); X=_IQ26(4.5); Y = _IQ26rmpy(M,X); // M=-10.9 // X=4.5 // Y= -32.0, saturated to MIN
47
IQNmpyI32 IQNmpyI32
Description Declaration
Multiplication (IQN*LONG)
This macro multiplies an IQ number with a long integer. Global IQ Macro (IQ format = GLOBAL_Q) C _iq _IQmpyI32(_iq A, long B) C++ iq IQmpyI32(const iq &A, long B) Q format specific IQ Macro (IQ format = IQ1 to IQ30) C _iqN _IQNmpyI32( _iqN A, long B) C++ iqN IQNmpyI32(const iqN &A, long B)
Input
Global IQ Macro (IQ format = GLOBAL_Q) Operand A is an IQ number in GLOBAL_Q format and B is the long integer. Q format specific IQ Macro (IQ format = IQ1 to IQ30) Operand A is an IQ number in IQN format and B is the long integer.
Output
Global IQ Macro (IQ format = GLOBAL_Q) Result of multiplication in GLOBAL_Q format Q format specific IQ Macro (IQ format = IQ1 to IQ30) Result of multiplication in IQN format.
Usage
Example 1: Compute Y = 5*X in the GLOBAL_Q format. Assume GLOBAL_Q =IQ26 in the IQmath header file. _iq Y, X; // X=5.1 in GLOBAL_Q format // Y= 25.5 in GLOBAL_Q format
Compute Y = 5*X in IQ26 format. _iq26 Y, X; long M; M=5; X = _IQ26(5.1); Y = _IQ26mpyI32(X,M); // M=5 // X=5.1 in IQ29 format // Y=25.5 in IQ29 format
48
IQNmpyI32int IQNmpyI32int
Description Declaration
This function multiplies an IQ number with a long integer and returns the integer part of the result. Global IQ function (IQ format = GLOBAL_Q) C long _IQmpyI32int(_iq A, long B) C++ long IQmpyI32int(const iq &A, long B) Q format specific IQ function (IQ format = IQ1 to IQ30) C long _IQNmpyI32int(_iqN A, long B) C++ long IQNmpyI32int(const iqN &A, long B)
Input
Global IQ function (IQ format = GLOBAL_Q) Operand A is an IQ number in GLOBAL_Q format and B is the long integer. Q format specific IQ function (IQ format = IQ1 to IQ30) Operand A is an IQ number in IQN format and B is the long integer.
Output
Global IQ function (IQ format = GLOBAL_Q) Integer part of the result (32-bit) Q format specific IQ function (IQ format = IQ1 to IQ30) Integer part of the result (32-bit)
Usage
Example 1 Convert an IQ value in the range [- 1.0, +1.0] to a DAC value with the range [ 0 to 1023]: _iq Output; long temp; short OutputDAC; // value converted to +/- 512 temp = _IQmpyI32int(Output, 512); // value scaled to 0 to 1023 temp += 512; // saturate within range of DAC if( temp > 1023 )temp = 1023; if( temp < 0 ) temp = 0; // output to DAC value OutputDAC = (int )temp;
Note: The integer operation performs the multiply and calculates the integer portion from the resulting 64-bit calculation. Hence it avoids any overflow conditions.
49
IQNmpyI32frac IQNmpyI32frac
Description Declaration
This function multiplies an IQ number with a long integer and returns the fractional part of the result. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQmpyI32frac(_iq A, long B) C++ iq IQmpyI32frac(const iq &y, long x) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNmpyI32frac( _iqN A, long B) C++ iqN IQNmpyI32frac(const iqN &A, long B)
Input
Global IQ function (IQ format = GLOBAL_Q) Operand A is an IQ number in GLOBAL_Q format and B is a long integer. Q format specific IQ function (IQ format = IQ1 to IQ30) Operand A is an IQ number in IQN format and B is the long integer.
Output
Global IQ function (IQ format = GLOBAL_Q) Fractional part of the result (32-bit) Q format specific IQ function (IQ format = IQ1 to IQ30) Fractional part of the result (32-bit)
Usage Example: The following example extracts the fractional part of result after multiplication (Assuming GLOBAL_Q=IQ26) _iq X1= _IQ(2.5); _iq X2= _IQ26(-1.1); _iq Y1frac, Y2frac; long M1=5, M2=9; // Y1frac Y1frac // Y2frac Y2frac = = = = 0.5 in GLOBAL_Q IQmpyI32frac(X1, M1); -0.9 in GLOBAL_Q IQ26mpyI32frac(X2, M2);
50
IQNmpyIQX IQNmpyIQX
Description Declaration
Multiplication (GLOBAL_Q=IQN1*IQN2)
This C compiler intrinsic multiplies two IQ number that are represented in different IQ format Global IQ Intrinsic (IQ format = GLOBAL_Q) C _iq _IQmpyIQX(_iqN1 A, int N1, _iqN2 B, int N2) C++ iq IQmpyIQX(iqN1 A, int N1, iqN2 B, int N2) Q format specific IQ Intrinsic (IQ format = IQ1 to IQ30) C _iqN _IQNmpyIQX(_iqN1 A, int N1, _iqN2 B, int N2) C++ iqN IQNmpyIQX(iqN1 A, int N1, iqN2 B, int N2)
Input Output
Operand A is an IQ number in IQN1 format and operand B is in IQN2 format. Global IQ Intrinsic (IQ format = GLOBAL_Q) Result of the multiplication in GLOBAL_Q format Q format specific IQ Intrinsic (IQ format = IQ1 to IQ30) Result of the multiplication in IQN format
Usage
This operation is useful when we wish to multiply values of different IQ. Example: Calculate the following equation: Y = X0*C0 + X1*C1 + X2*C2 Where, X0, X1, X2 values are in IQ30 format (Range -2 to +2) C0, C1, C2 values are in IQ28 format (Range 8 to +8) The maximum range of Y will be -48 to +48. Therefore, we will store the result in an IQ format that is less then IQ25. Case 1: GLOBAL_Q=IQ25 _iq30 X0, X1, X2; _iq28 C0, C1, C2; _iq Y; // All values IQ30 // All values IQ28 // Result GLOBAL_Q = IQ25
Y = _IQmpyIQX(X0, 30, C0, 28); Y += _IQmpyIQX(X1, 30, C1, 28); Y += _IQmpyIQX(X2, 30, C2, 28); Case 2: IQ Specific computation _iq30 X0, X1, X2; _iq28 C0, C1, C2; _iq25 Y; // All values IQ30 // All values IQ28 // Result GLOBAL_Q = IQ25
Y = _IQ25mpyIQX(X0, 30, C0, 28); Y += _IQ25mpyIQX(X1, 30, C1, 28); Y += _IQ25mpyIQX(X2, 30, C2, 28);
51
IQNdiv IQNdiv
Description Declaration
Fixed-point division
This module divides two IQN number and provide 32-bit quotient (IQN format) using Newton-Raphson technique Global IQ function (IQ format = GLOBAL_Q) C _iq _IQdiv(_iq A, _iq B) C++ iq operator / (const iq &A, const iq &B) iq &iq :: operator /= (const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNdiv(_iqN A, iq B) C++ iqN operator / (const iqN &A, const iqN &B) iqN &iqN :: operator /= (const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Input A & B are fixed-point number represented in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Input A & B are fixed-point number in IQN format (N=1:30)
Output
Global IQ function (IQ format = GLOBAL_Q) Output in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Output in IQN format (N=1:30)
Accuracy Usage
= 20 log 2 2 Example:
( ) 20 log (7 ) = 28 bits
31 2
The following example obtains 15 =0.666 assuming that GLOBAL_Q is set to Q28 format in the IQmath header file. #include IQmathLib.h _iq in1 out1; _iq28 in2 out2; void main(void ) { in1 = _IQ(1.5); out1 = _IQdiv(_IQ(1.0), in1); in2 = _IQ28(1.5); out2 = _IQ28div(_IQ28(1.0), in2); }
52
Fixed Point vs. Floating Point Analysis Fixed Point division vs. C Float division
53
Fixed Point vs. Floating Point Analysis Fixed Point division vs. C Float division
54
55
IQNsin IQNsin
Description Declaration
This module computes the sine value of the input (in radians) using table look-up and Taylor series expansion between the look-up table entries. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQsin(_iq A) C++ iq IQsin(const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ29) C _iqN _IQNsin(_iqN A) C++ iqN IQNsin(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Input argument is in radians and represented as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) Input argument is in radians and represented as fixed-point number in IQN format (N=1:29).
Output
Global IQ function (IQ format = GLOBAL_Q) This function returns the sine of the input argument as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) This function returns the sine of the input argument as fixed-point number in IQN format (N=1:29)
Accuracy Example
= 20 log 2
The following example obtains sin (0.25 x pi) = 0.707 assuming that GLOBAL_Q is set to Q29 format in the IQmath header file. #include IQmathLib.h #define PI 3.14156 _iq in1, out1; _iq28 in2, out2; void main(void ){ // in1 = 0.25 x pi x 2^29 = 0x1921FB54 // out1 = sin(0.25 x pi)x 2^29 = 0x16A09E66 // in2 = 0x25 x pi x 2^29 = 0x1921FB54 // out2 = sin(0.25 x pi)x 2^29 = 0x16A09E66 in1=_IQ(0.25*PI); out1=_IQsin(in1) in2=_IQ29(0.25*PI) out2=_IQ29sin(in2); }
56
Fixed Point vs. Floating Point Analysis IQNsin Function vs. C Float SIN: Input varies from to
57
IQNsinPU IQNsinPU
Description Declaration
Fixed point SIN (radians in per unit) This module computes the sine value of the input (in per-unit radians) using table look-up and Taylor series expansion between the look-up table entries. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQsinPU(_iq A) C++ iq IQsinPU(const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNsinPU(_iqN A) C iqN IQNsinPU(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Input argument is in per-unit radians and represented as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Input argument is in per-unit radians and represented as fixed-point number in IQN format (N=1:30).
Output
Global IQ function (IQ format = GLOBAL_Q) This function returns the sine of the input argument as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) This function returns the sine of the input argument as fixed-point number in IQN format (N=1:30)
= 20 log 2 1 2
30
The following example obtains the sin (0.25 ) =0.707 assuming that GLOBAL_Q is set to Q30 format in the IQmath header file. #include IQmathLib.h #define PI 3.14156 _iq in1, out1; _iq30 in2, out2; void main(void ){ // in1 = in2 = (0.25 x PI)/(2PI) x 2^30 // = (.25/2) x 2^30 = 0x08000000 or .125 // out1 = out2 = sinPU(0.25/2) x 2^30 = 0x2D413CCC or .707 in1 out1 in2 out2 } =_IQ(0.25L/2.0L); =_IQsinPU(in1) =_IQ30(0.25*PI/PI); =_IQ30sinPU(in2);
58
Fixed Point vs. Floating Point Analysis IQNsinPU Function vs. C Float SIN: Input varies from 0 to 2 in per unit representation
59
IQNacos IQNacos
Description Declaration
Fixed point ACOS (radians) This module computes the inverse cosine of the input and returns the result in radians. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQacos(_iq A) C++ iq IQacos( const iq & A) Q format specific IQ function (IQ format = N = 1 to 29) C _iqN _IQNacos(_iqN A) C++ iq N IQNacos(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Input argument is in radians and represented as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) Input argument is in radians and represented as fixed-point number in IQN format (N=1:29).
Output
Global IQ function (IQ format = GLOBAL_Q) This function returns the inverse cosine of the input argument as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) This function returns the inverse cosine of the input argument as fixed-point number in IQN format (N=1:29)
Example
The following example obtains the result of asin (0.70710678) = (0.25 ) assuming that GLOBAL_Q is set to Q29 format in the IQmath header file. #include IQmathLib.h _iq in1, out1; _iq29 in2, out2; void main(void ) { // in1 = 0x70710678 x 2^29 // out1 = acos(0.70710678L) // in2 = 0x70710678 x 2^29 // out2 = acos(0.70710678L) in1 out1 in2 out2 } =_IQ(0.70710678L); =_IQacos(in1); =_IQ29(0.70710678L) =_IQ29acos(in2);
= x = x
60
IQNcos IQNcos
Description Declaration
Fixed point COS (radians) This module computes the cosine value of the input (in radians) using table lookup and Taylor series expansion between the look up table entries. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQcos(_iq A) C++ iq IQcos(const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ29) C _iqN _IQNcos(_iqN A) C++ iqN IQNcos(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Input argument is in radians and represented as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) Input argument is in radians and represented as fixed-point number in IQN format (N=1:29).
Output
Global IQ function (IQ format = GLOBAL_Q) This function returns the cosine of the input argument as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) This function returns the cosine of the input argument as fixed-point number in IQN format (N=1:29)
Accuracy Example
= 20 log 2
The following example obtains the cos(0.25 ) =0.707 assuming that GLOBAL_Q is set to Q29 format in the IQmath header file. #include IQmathLib.h #define PI 3.14156 _iq in1, out1; _iq29 in2 out2; void main(void ){ // in = 0.25 x PI // out1 = cos(.25 x // in = 0.25 x PI // out1 = cos(.25 x in1 out1 in2 out2 }
x 2^29 = 0x1921FB54 PI) x 2^29 = 0x16A09E66 x 2^29 = 0x1921FB54 PI) x 2^29 = 0x16A09E66
61
Fixed Point vs. Floating Point Analysis Fixed Point COS Function vs. C Float COS: Input varies from to
62
IQNcosPU IQNcosPU
Description Declaration
Fixed point COS (radians in per unit) This module computes the cosine value of the input (in per-unit radians) using table look-up and Taylor series expansion between the look up table entries. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQcosPU(_iq A) C++ iq IQcosPU(const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNcosPU(_iqN A) C++ iqN IQNcosPU(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Input argument is in per-unit radians and represented as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Input argument is in per-unit radians and represented as fixed-point number in IQN format (N=1:30).
Output
Global IQ function (IQ format = GLOBAL_Q) This function returns the sine of the input argument as fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) This function returns the sine of the input argument as fixed-point number in IQN format (N=1:30)
Accuracy Usage
= 20 log 2 1 2
30
) 20 log (2)
2
= 29 bits
Example: The following sample code obtains the cos(0.25 ) =0.707 assuming that GLOBAL_Q is set to Q30 format in the IQmath header file. #include IQmathLib.h #define PI 3.14156 _iq in1, out1; _iq30 in2, out2 void main(void // in1 = in2 // // out1 = out2 ){ = (0.25 x PI)/(2PI) x 2^30 = (.025/2.0) x 2^30 = 0x08000000 or .125 = cosPU(0.25/2.0) x 2^30 = 0x2D413CCC or .707
63
Fixed Point vs. Floating Point Analysis Fixed Point COS Function vs. C Float COS: Input varies from 0 to 2 in per unit
64
IQNatan2
IQNatan2
Description
Fixed point 4-quadrant ATAN (in radians) This module computes 4-quadrant arctangent. Output of this module is in radians that varies from to Global IQ function (IQ format = GLOBAL_Q) C _iq _IQatan2(_iq A, _iq B) C++ iq IQatan2(const iq &A, const iq &B) Q format specific IQ function (IQ format = IQ1 to IQ29) C _iqN _IQNatan2(_iqN A, _iqN B) C++ iqN IQNatan2(const iqN &A, const iqN &B)
Declaration
Input
Global IQ function (IQ format = GLOBAL_Q) Inputs A and B are fixed-point numbers represented in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) Inputs A and B are fixed-point numbers in IQN format (N=1:29)
Output
Global IQ function (IQ format = GLOBAL_Q) This function returns the inverse tangent of the input argument as fixed-point number in GLOBAL_Q format. The output contains the angle in radians between [ ,+ ] Q format specific IQ function (IQ format = IQ1 to IQ29) This function returns the inverse tangent of the input argument as fixed-point number in IQN format (N=1:29). The output contains the angle in radians between [ ,+ ]
Accuracy Usage
= 20 log 2
The following example obtains tan (sin ( 5 ), cos( 5 )) = 5 , assuming that the GLOBAL_Q is set to Q29 format in the IQmath header file. #include IQmathLib.h #define PI 3.14156L _iq xin1, yin1, out1; _iq29 xin2, yin2, out2; void main(void ){ // xin1 = xin2 = cos(PI/5) x 2^29 = 0x19E37FA8 // yin1 = yin2 = sin(PI/5) x 2^29 = 0x12CF17EF // out1 = out2 = PI/5 x 2^29 = 0x141B21C3 xin1 yin1 out1 xin2 yin2 out2 } = _IQcos(_IQ(PI/5.0L)); = _IQsin(_IQ(PI/5.0L)); = _IQatan2(yin1,xin1); = _IQ29cos(_IQ29(PI/5.0L)); = _IQ29sin(_IQ29(PI/5.0L)); = _IQ29atan2(yin1,xin1);
65
Fixed Point vs. Floating Point Analysis Fixed Point ARCTAN Function vs. C Float ARCTAN
66
IQNatan2PU IQNatan2PU
Description
This module computes 4-quadrant arctangent. Output of this module is in per unit radians that varies from 0 (0 radians) to 1 ( 2 radians). Global IQ function (IQ format = GLOBAL_Q) C _iq _IQatan2PU(_iq A, _iq B) C++ iq IQatan2PU(const iq &A, const iq &B) Q format specific IQ function (IQ format = IQ1 to IQ29) C _iqN _IQNatan2PU(_iqN A, _iqN B) C++ iqN IQNatan2PU(const iqN &A, const iqN &B)
Declaration
Input
Global IQ function (IQ format = GLOBAL_Q) Inputs A and B are fixed-point number represented in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) Input A and B are fixed-point number in IQN format (N=1:29)
Output
Global IQ function (IQ format = GLOBAL_Q) This function returns the inverse tangent of the input argument as fixed-point number in GLOBAL_Q format. The output contains the angle in per unit radians that varies from 0 (0 radians) to 1 ( 2 radians). Q format specific IQ function (IQ format = IQ1 to IQ29) This function returns the inverse tangent of the input argument as fixed-point number in IQN format (N=1:29). The output contains the angle in per unit radians that varies from 0 (0 radians) to 1 ( 2 radians).
Accuracy Usage
= 20 log 2 1 2
29
The following sample code obtains tan (sin ( 5 ), cos( 5 )) = GLOBAL_Q is set to Q29 format in the IQmath header file.
, assuming that
#include IQmathLib.h #define PI 3.14156L _iq xin1, yin1, out1; _iq29 xin2, yin2, out2; void main(void ){ // xin1 = xin2 = cos(PI/5) x 2^29 = 0x19E37FA8 // yin1 = yin2 = sin(PI/5) x 2^29 = 0x12CF17EF // out1 = out2 = (PI/5)/(2PI) x 2^29 = 0x03333104 xin1 = _IQcos(_IQ(PI/5.0L)); yin1 = _IQsin(_IQ(PI/5.0L)); out1 = _IQatan2PU(yin1,xin1); xin2 = _IQ29cos(_IQ29(PI/5.0L)); yin2 = _IQ29sin(_IQ29(PI/5.0L)); out2 = _IQ29atan2PU(yin1,xin1); }
67
Fixed Point vs. Floating Point Analysis Fixed Point ARCTAN Function vs. C Float ARCTAN
68
Fixed point ATAN (in radians) This module computes arctangent. Output of this module is in radians that vary from 2 to 2 . Global IQ Macro (IQ format = GLOBAL_Q) C _iq _IQatan(_iq A) C++ iq IQatan(const iq &A) Q format specific IQ Macro (IQ format = IQ1 to IQ29) C _iqN _IQNatan(_iqN A) C++ iqN IQNatan(const iqN &A)
Declaration
Input
Global IQ function (IQ format = GLOBAL_Q) Input argument is a fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ29) Input argument is a fixed-point number in IQN format (N=1:30)
Output
Global IQ function (IQ format = GLOBAL_Q) This function returns the inverse tangent of the input argument as fixed-point number in GLOBAL_Q format. The output contains the angle in radians between
[ 2 ,+ 2 ]
Q format specific IQ function (IQ format = IQ1 to IQ29) This function returns the inverse tangent of the input argument as fixed-point number in IQN format (N=1:29). The output contains the angle in radians between 2 ,+ 2
Accuracy
= 20 log 2
2 29 20 log 2 (2 ) = 25 bits 2
1
Usage
The following example obtains tan (1) = 4 , assuming that GLOBAL_Q is set to Q29 format in the IQmath header file. #include IQmathLib.h _iq in1, out1; _iq29 in2, out2; void main(void ) { in1 = _IQ(1.0L); out1 =_IQatan(in1); in2 =_IQ29(1.0L); out2 =_IQ29atan(in2) }
69
70
IQNsqrt
IQNsqrt Description Declaration
Fixed point Square-root This module computes the square root of the input using table lookup and Newton-Raphson approximation. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQsqrt(_iq A) C++ iq IQsqrt(const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNsqrt(_iqN A) C++ iqN IQNsqrt(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Input argument is a fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Input argument is a fixed-point number in IQN format (N=1:30)
Output
Global IQ function (IQ format = GLOBAL_Q) Square root of input in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Square root of input in IQN format (N=1:30)
Accuracy Usage
= 20 log 2 2
Calculate 1.8 = 1.34164, assuming that GLOBAL_Q is set to Q30 format in IQmath header file. #include IQmathLib.h _iq in1, out1; _iq30 in2, out2; void main(void ) { // in1 = in2 = 1.8 x 2^30 = 0x73333333 // out1 = out2 = sqrt(1.8) x 2^30 = 0x55DD7151 in1 = _IQ(1.8); out1 = _IQsqrt(x); in2 = _IQ30(1.8); out2 = _IQ30sqrt(x); }
71
Fixed Point vs. Floating Point Analysis Fixed Point SQRT Function vs. C Float SQRT
72
IQNisqrt
IQNisqrt Description Declaration
Fixed point Inverse Square-root This module computes the inverse square root of the input using table lookup and Newton-Raphson approximation. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQisqrt(_iq A) C++ iq IQisqrt(const iq &A) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNisqrt(_iqN A) C++ iqN IQNisqrt(const iqN &A)
Input
Global IQ function (IQ format = GLOBAL_Q) Input argument is a fixed-point number in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Input argument is a fixed-point number in IQN format (N=1:30)
Output
Global IQ function (IQ format = GLOBAL_Q) Inverse square-root of the input expressed in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Inverse square root of input expressed in IQN format (N=1:30)
Accuracy Usage
= 20 log 2 2
1
Calculate 1.8 =0.74535 assuming that GLOBAL_Q is set to Q30 format in the IQmath header file. #include IQmathLib.h _iq in1, out1; _iq30 in2, out2; void main(void ) { // in1 = in2 = 1.8 x 2^30 = 0x73333333 // out1 = out2 = 1/sqrt(1.8) x 2^30 = 0x2FB3E99E in1 =_IQ(1.8); out1 =_IQisqrt(in1); in2 =_IQ30(1.8); out2 =_IQ30isqrt(in2); }
73
Fixed Point vs. Floating Point Analysis Fixed Point inverse SQRT Function vs. C Float inverse SQRT
74
Fixed Point vs. Floating Point Analysis Fixed Point inverse SQRT Function vs. C Float inverse SQRT
75
IQNmag
IQNmag Description
Magnitude of IQ complex number This function calculates the magnitude of two orthogonal vectors as follows: Mag = sqrt(A^2 + B^2). This operation achieves better accuracy and avoids overflow A problems that may be encountered by using the "_IQsqrt" function. Global IQ function (IQ format = GLOBAL_Q) C _iq _IQmag(_iq A, _iq B) C++ iq IQmag(const iq &A, const iq &B) Q format specific IQ function (IQ format = IQ1 to IQ30) C _iqN _IQNmag(_iqN A, _iqN B) C++ iqN IQNmag(const iqN &A, const iqN &B)
Declaration
Input
Global IQ function (IQ format = GLOBAL_Q) Inputs A and B are IQ numbers represented in GLOBAL_Q format. Q format specific IQ function (IQ format = IQ1 to IQ30) Inputs A and B are IQ numbers represented in IQN format
Output
Global IQ function (IQ format = GLOBAL_Q) Magnitude of the input vector in GLOBAL_Q format Q format specific IQ function (IQ format = IQ1 to IQ30) Magnitude of the input vector in IQN format
Accuracy Usage
29-bits (Same as SQRT function) Example: The following sample code obtains the magnitude of the complex number (Assuming GLOBAL_Q=IQ28). #include IQmathLib.h // Complex number = real1 + j*imag1 // Complex number = real2 + j*imag2 _iq real1, imag1, mag1; _iq28 real2, imag2, mag2; void main(void ) { // mag1 real1 imag1 mag1 // mag2 real2 imag2 mag2 } = = = = 5.6568 in IQ28 format _IQ(4.0); _IQ(4.0); _IQmag(real1, imag1);
76
77
IQsat IQsat
Description Declaration Input
This intrinsic saturates an IQ value to the given Positive and Negative limits. This operation is useful in areas where there is potential for overflow in a calculation. _iq _IQsat(_iq A, long P, long N) Global IQ function (IQ format = GLOBAL_Q) IQ number in GLOBAL_Q format Output Format Global IQ function (IQ format = GLOBAL_Q) Saturated output in GLOBAL_Q format
Usage
Example: Calculate the linear equation "Y = M*X + B", with saturation. All variables are GLOBAL_Q = 26. However, there is a possibility that the variable ranges may cause overflow, so we must perform the calculation and saturate the result. To do this, we perform the intermediate operations using IQ = 20 and then saturate before converting the result back to the appropriate GLOBAL_Q value: #include IQmathLib.h void main(void) { _iq Y, M, X, B; _iq20 temp;
78
FLOAT_MATH: Corrected the #defines for conversion from IQ to Q15 o Removed the L from the constant so the compiler will not call the 64-bit floating point routine in the RTS library. o Removed the cast of the float to long before the multiply. FLOAT_MATH: Modified IQdiv and IQNdiv macros such that the arguments are cast to float before the division. This will allow the macros to be used with integer types, and not just _iq types, properly. Examples: Fixed some issues with the projects being portable under CCS 4. Fixed the 28335 project so it links in the fpu32 versions of the rts library and IQmath. Fixed typos in this document.
Minor release to add examples and build information for Code Composer Studio V4. Changed the install directory to fit with ControlSuite.
79
V1.5a
June 1, 2009
Rebuilt the IQmath.lib with large model enabled for the function IQNtoa. No other functions were changed. Updated documentation to include 2802x and 2803x devices.
V1.5
July 8, 2008
Added examples for Piccolo 28027 and 28035. Added IQNtoa. Header IQmathLib.h and IQmathCPP.h files were updated to fix typos and missing information. Refer to the header files themselves for more information. Added a version of the library built with the compiler switch --float_support=fpu32. This enables mixing the IQmath with the native floating support capabilities of C28x+FPU. Added information describing how to convert between IQmath and float math. Added the IQexp, IQasin, IQacos, and IQNtoa function information to this document. Added C++ information to this document. Previously this information was only in a readme.txt file. Added information regarding locations of tables stored in the boot ROM of different devices. Noted the IQmath .gel file is most useful when using a legacy debugger that does not support IQmath. Added examples for 2808, 28335 and 28235. Updated the example flow to better match the header file and peripheral examples also provided by TI. General documentation cleanup and improvements. Install is now under a version specific directory. Changed the directory structure to reduce the number of duplicated files. Fixed bug in IQexp. Added IQexp, IQasin, IQacos. Previous Web Release
80