Module 11 : IQ – Math Library C28x
32-Bit-Digital Signal Controller
TMS320F2812
Texas Instruments Incorporated
European Customer Training Centre
University of Applied Sciences Zwickau (FH)
11 - 1
Floating Point, Integer and Fixed Point
Two basic categories of processors:
Floating Point
Integer/Fixed Point
What is the difference?
What are advantages / disadvantages ?
Real – Time Control : Fixed Point !
Discuss fixed-point math development
limitations
Compare/contrast floating-point and IQ
representation
Texas Instruments IQ-Math approach
11 - 2
Processor Types
Floating Point Processors
Internal Hardware Unit to support Floating Point
Operations
Examples : Intel’s Pentium Series , Texas
Instruments C 6000 DSP
High dynamic range for numeric calculation
Rather expensive
Integer / Fixed – Point Processors
Fixed Point Arithmetic Unit
Almost all embedded controllers are fixed point
machines
Examples: all microcontroller families, e.g.
Motorola HC68x, Infineon C166, Texas
Instruments TMS430, TMS320C5000, C2000
Lowest price per MIPS 11 - 3
IEEE Standard 754 Single Precision
Floating-Point
31 30 23 22 0
s eeeeeeee fffffffffffffffffffffff
1 bit sign 8 bit exponent 23 bit mantissa (fraction)
Case 1: if e = 255 and f =/ 0, then v = NaN
Case 2: if e = 255 and f = 0, then v = [(-1)s]*infinity
Case 3: if 0 < e < 255, then v = [(-1)s]*[2(e-127)]*(1.f)
Case 4: if e = 0 and f =/ 0, then v = [(-1)s]*[2(-126)]*(0.f)
Case 5: if e = 0 and f = 0, then v = [(-1)s]*0
Advantage Exponent gives large dynamic range
Disadvantage Precision of a number depends on its exponent
11 - 4
Floating - Point does not solve everything!
Example: x = 10.0 (0x41200000)
+ y = 0.000000238 (0x347F8CF1)
z = 10.000000238 WRONG!
RIGHT?
You cannot represent 10.000000238 with
single-precision floating point
0x412000000 = 10.000000000
10.000000238 can’t represent!
0x412000001 = 10.000000950
So z gets rounded down to 10.000000000
11 - 5
Integer Numbering System Basics
Binary Numbers
01102 = (0*8)+(1*4)+(1*2)+(0*1) = 610
111102 = (1*16)+(1*8)+(1*4)+(1*2)+(0*1) = 3010
Two’s Complement Numbers
01102 = (0*-8)+(1*4)+(1*2)+(0*1) = 610
111102 = (1*-16)+(1*8)+(1*4)+(1*2)+(0*1) = -2 10
11 - 6
Four-Bit Integer Multiplication
0100 4
x 1101 x -3
00000100
0000000
000100
11100
11110100 -12
Accumulator 11110100
11110100
Data Memory ?
Is there another (superior) numbering system? 11 - 7
Yes: Binary Fractions
11 00 11 11
-1
• 1/2 1/4 1/8
= -1 + 1/4 + 1/8 = -5/8
Fractions have the nice property that
fraction x fraction = fraction
11 - 8
Four-Bit Multiplication
0100
. 1/2
x 1101
. x - 3/8
00000100
0000000
000100
11100
11110100 -3/16
Accumulator 11110100
11110100
Data Memory 1110
. -1/4
11 - 9
Fractional Representation
31 0
S IIIIIIII fffffffffffffffffffffff
32 bit mantissa
.
-2I + 2I-1 + … + 21 + 20 2-1 + 2-2 + … + 2-Q
“IQ” – Format
“I” INTEGER – Fraction
“Q” QUOTIENT – Fraction
Advantage Precision same for all numbers in an IQ format
Disadvantage Limited dynamic range compared to floating point
11 - 10
IQ - Examples
I1Q3 – Format:
3 0
S fff
Most negative decimal number: -1.0 = 1.000 B
Most positive decimal number: + 0.875 = 0.111 B
Smallest negative decimal number: -1*2-3 (0.125) = 1.111 B
Smallest positive decimal number: 2-3 ( 0.125) = 0. 001 B
Range: -1.0 …. 0.875 ( + 1.0)
Resolution: 2-3
11 - 11
IQ - Examples
I3Q1 – Format:
3 0
SII f
Most negative decimal number: -4.0 = 100.0 B
Most positive decimal number: + 3.5 = 011.1 B
Smallest negative decimal number: -1 * 2-1 = 111.1 B
Smallest positive decimal number: 2-1 = 000.1 B
Range: -4.0 …. +3.5 ( + 4.0)
Resolution: 2-1
11 - 12
IQ - Examples
I1Q31 – Format:
31 0
S fff ffff ffff ffff ffff ffff ffff ffff
Most negative decimal number: -1.0
1.000 0000 0000 0000 0000 0000 0000 0000 B
Most positive decimal number: + 1.0
0.111 1111 1111 1111 1111 1111 1111 1111 B
Smallest negative decimal number: -1*2 -31
1.111 1111 1111 1111 1111 1111 1111 1111 B
Smallest positive decimal number: 2-31
0.000 0000 0000 0000 0000 0000 0000 0001 B
Range: -1.0 …. (+1.0)
Resolution: 2-31
11 - 13
IQ - Examples
I8Q24 – Format:
31 0
S III IIII ffff ffff ffff ffff ffff
Most negative decimal number: -128
1000 0000. 0000 0000 0000 0000 0000 0000 B
Most positive decimal number: + 128
0111 1111. 1111 1111 1111 1111 1111 1111 B
Smallest negative decimal number: -1*2 -24
1111 1111. 1111 1111 1111 1111 1111 1111 B
Smallest positive decimal number: 2-24
0000 0000. 0000 0000 0000 0000 0000 0001 B
Range: -128 …. (+128)
Resolution: 2-24
11 - 14
IQ-Math can do better!
I8Q24 Example: x = 10.0 (0x0A000000)
+ y = 0.000000238 (0x00000004)
z = 10.000000238 (0x0A000004)
Exact Result (this example)
11 - 15
What is Sign Extension?
When moving a value from a narrowed width location to a
wider width location, the sign bit is extended to fill the width
of the destination
Sign extension applies to signed numbers only
It keeps negative numbers negative!
Sign extension controlled by SXM bit in ST0 register; When
SXM = 1, sign extension happens automatically
4 bit Example: Load a memory value into the ACC
memory 1101 = -23 + 22 + 20 = -3
Load and sign extend
ACC 1111 1101 = -27 + 26 + 25 + 24 + 23 + 22 + 20
= -128 + 64 + 32 + 16 + 8 + 4 +
1
= -3 11 - 16
Correcting Redundant Sign Bit
Accumulator 11 11 11 11 00 11 00 00
Redundant
Sign Bit
Correcting Redundant Sign Bit
IQmath: automatically handled (next topic)
Q math in “C”, shift in software:
int x,y,z;
z = ((long)x * (long)y) >> 15;
11 - 17
How is a fraction coded?
~1 ~ 32K 7FFF
½ 16K 4000
0
0 0000
*32768
–½ –16K C000
–1 –32K 8000
Fractions Integers Hex
Example: represent the fraction number 0.707
void main(void) {
int coef = 32768 * 707 / 1000;
}
11 - 18
Fractional vs. Integer
Range
Integers have a maximum range determined
by the number of bits
Fractions have a maximum range of ±1
Precision
Integers have a maximum precision of 1
Fractional precision is determined by the
number of bits
11 - 19
So how do we really use all this fraction stuff?
The Fix-Point Development Dilemma
Natural development starts
Simulation
Platform with simulation in floating-
Takes many days/weeks (i.e. MatLab) point
to convert (one way
process)
Can be easily ported
Fixed-Point Floating-Point
Algorithm Algorithm
to floating-point
(ASM, C, C++) (C or C++) device
Floating-Point DSP
Fix-Point DSP
11 - 20
Traditional 16-bit “Q” Math Approach
y = mx + b
s Q15 M
ss Q30
s Q15 X
sssssssssssss Q15 s Q15 B
Align Binary
<< 15 Point For Add
ss Q30
sI Q30
Align Binary
>> 15 Point For Store
ssssssssssssI Q15 s Q15 Y
in C: Y = ((i32) M * (i32) X + (i32) B << Q) >> Q;
11 - 21
Traditional 32-bit “Q” Math Approach
y = mx + b
I8 Q24 M
I16 Q48
I8 Q24 X
ssssssssssssssssssI8 Q24 I8 Q24 B
Align Decimal
<< 24 Point for Add
ssssI8 Q48
I16 Q48
Align Decimal
>> 24 Point for Store
sssssssssssssssssI16 Q24 I8 Q24 Y
in C: Y = ((i64) M * (i64) X + (i64) B << Q) >> Q;
Note: Requires support for 64-bit integer data type in compiler 11 - 22
32-bit IQmath Approach
y = mx + b
I8 Q24 M
I16 Q48
I8 Q24 X
Align Decimal
Point Of Multiply >> 24
sssssssssssssssssI16 Q24
I8 Q24 B
I8 Q24 I8 Q24 Y
in C: Y = ((i64) M * (i64) X) >> Q + B;
11 - 23
IQmath Approach
Multiply Operation
Y = ((i64) M * (i64) X) >> Q + B;
Redefine the multiply operation as follows:
_IQmpy(M,X) == ((i64) M * (i64) X) >> Q
This simplifies the equation as follows:
Y = _IQmpy(M,X) + B;
C28x compiler supports “_IQmpy” intrinsic; assembly code generated:
MOVL XT,@M
IMPYL P,XT,@X ; P = low 32-bits of M*X
QMPYL ACC,XT,@X ; ACC = high 32-bits of M*X
LSL64 ACC:P,#(32-Q) ; ACC = ACC:P << 32-Q
; (same as P = ACC:P >> Q)
ADDL ACC,@B ; Add B
MOVL @Y,ACC ; Result = Y = _IQmpy(M*X) + B
; 7 Cycles
11 - 24
IQmath Approach
It looks like floating-point!
float Y, M, X, B;
Floating-Point
Y = M * X + B;
long Y, M, X, B;
Traditional
Fix-Point Q Y = ((i64) M * (i64) X + (i64) B << Q)) >> Q;
“IQmath” _iq Y, M, X, B;
In C Y = _IQmpy(M, X) + B;
“IQmath” iq Y, M, X, B;
In C++ Y = M * X + B;
Taking advantage of operator overloading feature in C++,
“IQmath” looks like floating-point math (looks natural!)
11 - 25
IQmath Approach
GLOBAL_Q simplification
User selects “Global Q” value for the whole application
GLOBAL_Q
based on the required dynamic range or resolution, for example:
GLOBAL_Q Max Val Min Val Resolution
28 7.999 999 996 -8.000 000 000 0.000 000 004
24 127.999 999 94 -128.000 000 00 0.000 000 06
20 2047.999 999 -2048.000 000 0.000 001
#define GLOBAL_Q 18 // set in “IQmathLib.h” file
_iq Y, M, X, B;
Y = _IQmpy(M,X) + B; // all values are in Q = 18
The user can also explicitly specify the Q value to use:
_iq20 Y, M, X, B;
Y = _IQ20mpy(M,X) + B; // all values are in Q = 20
11 - 26
IQmath Approach
Targeting Fixed-Point or Floating-Point device
Y = _IQmpy(M, X) + B;
User selects target math type
(in “IQmathLib.h” file)
#if MATH_TYPE == IQ_MATH #if MATH_TYPE == FLOAT_MATH
Y = (float)M * (float)X + (float)B;
Compile & Run Compile & Run
using “IQmath” on using floating-point math on
C28x C3x, C67x,C28x (RTS), PC,..
All “IQmath” operations have an equivalent floating-point operation
11 - 27
IQmath Library: math & trig functions (v1.4)
Operation Floating-Point “IQmath” in C “IQmath” in C++
type float A, B; _iq A, B; iq A, B;
constant A = 1.2345 A = _IQ(1.2345) A = IQ(1.2345)
multiply A*B _IQmpy(A , B) A*B
divide A/B _IQdiv (A , B) A/B
add A+B A+B A+B
substract A-B A-B A–B
boolean >, >=, <, <=, ==, |=, &&, || >, >=, <, <=, ==, |=, &&, || >, >=, <, <=, ==, |=, &&, ||
trig sin(A),cos(A) _IQsin(A), _IQcos(A) IQsin(A),IQcos(A)
functions sin(A*2pi),cos(A*2pi) _IQsinPU(A), _IQcosPU(A) IQsinPU(A),IQcosPU(A)
atan(A),atan2(A,B) _IQatan(A), _IQatan2(A,B) IQatan(A),IQatan2(A,B)
atan2(A,B)/2pi
_IQatan2PU(A,B) IQatan2PU(A,B)
sqrt(A),1/sqrt(A)
_IQsqrt(A), _IQisqrt(A) IQsqrt(A),IQisqrt(A)
sqrt(A*A + B*B)
_IQmag(A,B) IQmag(A,B)
saturation if(A > Pos) A = Pos _IQsat(A,Pos,Neg) IQsat(A,Pos,Neg)
if(A < Neg) A = Neg
Accuracy of functions/operations approx ~28 to ~31 bits
11 - 28
IQmath Library: Conversion Functions (v1.4)
Operation Floating-Point “IQmath” in C “IQmath” in C++
iq to iqN A _IQtoIQN(A) IQtoIQN(A)
iqN to iq A _IQNtoIQ(A) IQNtoIQ(A)
integer(iq) (long) A _IQint(A) IQint(A)
fraction(iq) A – (long) A _IQfrac(A) IQfrac(A)
iq = iq*long A * (float) B _IQmpyI32(A,B) IQmpyI32(A,B)
integer(iq*long) (long) (A * (float) B) _IQmpyI32int(A,B) IQmpyI32int(A,B)
fraction(iq*long) A - (long) (A * (float) B) _IQmpyI32frac(A,B) IQmpyI32frac(A,B)
qN to iq A _QNtoIQ(A) QNtoIQ(A)
iq to qN A _IQtoQN(A) IQtoQN(A)
string to iq atof(char) _atoIQ(char) atoIQ(char)
IQ to float A _IQtoF(A) IQtoF(A)
[Link] > contains library of math functions
IQmathLib.h > C header file
IQmathCPP.h > C++ header file
11 - 29
AC Induction Motor Example
One of the more complex motor control algorithms
Sensorless, ACI induction machine direct rotor flux control
Goal: motor speed estimation & alpha-axis stator current estimation
11 - 30
AC Induction Motor Example
Park Transform - floating-point C code
#include “math.h”
#define TWO_PI 6.28318530717959
void park_calc(PARK *v)
{
float cos_ang , sin_ang;
sin_ang = sin(TWO_PI * v->ang);
cos_ang = cos(TWO_PI * v->ang);
v->de = (v->ds * cos_ang) + (v->qs * sin_ang);
v->qe = (v->qs * cos_ang) - (v->ds * sin_ang);
}
11 - 31
AC Induction Motor Example
Park Transform - converting to “IQmath” C code
#include “math.h”
#include “IQmathLib.h”
#define TWO_PI _IQ(6.28318530717959)
6.28318530717959
void park_calc(PARK *v)
{
float
_iq cos_ang , sin_ang;
sin_ang = sin(TWO_PI * v->ang); , v->ang));
_IQsin(_IQmpy(TWO_PI
_IQcos(_IQmpy(TWO_PI
cos_ang = cos(TWO_PI * v->ang); , v->ang));
_IQmpy(v->ds
v->de = (v->ds , cos_ang)
* cos_ang) + _IQmpy(v->qs
+ (v->qs * sin_ang); , sin_ang);
_IQmpy(v->qs
v->qe = (v->qs , cos_ang)
* cos_ang) - _IQmpy(v->ds
- (v->ds * sin_ang); , sin_ang);
}
11 - 32
AC Induction Motor Example
Park Transform - converting to “IQmath” C++ code
#include “math.h”
extern “C” { #include “IQmathLib.h” }
#include “IQmathCPP.h”
#define TWO_PI IQ(6.28318530717959)
6.28318530717959
void park_calc(PARK *v)
{
float
iq cos_ang , sin_ang;
IQsin(TWO_PI
sin_ang = sin(TWO_PI * v->ang);
* v->ang);
cos_ang = cos(TWO_PI * v->ang);
IQcos(TWO_PI * v->ang);
v->de = (v->ds * cos_ang) + (v->qs * sin_ang);
v->qe = (v->qs * cos_ang) - (v->ds * sin_ang);
}
11 - 33
AC Induction Motor Example
Q stability range
Q range Stability Range
Q31 to Q27 Unstable
(not enough dynamic range)
Q26 to Q19 Stable
Q18 to Q0 Unstable
(not enough resolution, quantization problems)
The developer must pick the right GLOBAL_Q value!
11 - 34
Where Is IQmath Applicable?
Anywhere a large dynamic range is not required
Motor Control (PID, State Estimator, Kalman,...)
Servo Control
Modems
Audio (MP3, etc.)
Imaging (JPEG, etc.)
Any application using 16/32-bit fixed-point Q math
Where it is not applicable
Graphical applications (3D rotation, etc.)
When trying to squeeze every last cycle
11 - 35
IQmath Approach Summary
“IQmath” + fixed-point processor with 32-bit capabilities =
Seamless portability of code between fixed and floating-point
devices
User selects target math type in “IQmathLib.h” file
#if MATH_TYPE == IQ_MATH
#if MATH_TYPE == FLOAT_MATH
One source code set for simulation vs. target device
Numerical resolution adjustability based on application
requirement
Set in “IQmathLib.h” file
#define GLOBAL_Q 18
Explicitly specify Q value
_iq20 X, Y, Z;
Numerical accuracy without sacrificing time and cycles
Rapid conversion/porting and implementation of algorithms
IQmath library is freeware - available from TI DSP website
[Link] (follow C2000 DSP links) 11 - 36