Module 11: IQ - Math Library C28x
Module 11: IQ - Math Library C28x
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)
11 - 4
Floating - Point does not solve everything!
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
Binary Numbers
01102 = (0*8)+(1*4)+(1*2)+(0*1) = 610
111102 = (1*16)+(1*8)+(1*4)+(1*2)+(0*1) = 3010
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
11 - 8
Four-Bit Multiplication
0100
. 1/2
x 1101
. x - 3/8
00000100
0000000
000100
11100
11110100 -3/16
Accumulator 11110100
11110100
.
-2I + 2I-1 + … + 21 + 20 2-1 + 2-2 + … + 2-Q
“IQ” – Format
“I” INTEGER – Fraction
“Q” QUOTIENT – Fraction
11 - 10
IQ - Examples
I1Q3 – Format:
3 0
S fff
Most negative decimal number: -1.0 = 1.000 B
11 - 11
IQ - Examples
I3Q1 – Format:
3 0
SII f
Most negative decimal number: -4.0 = 100.0 B
11 - 12
IQ - Examples
I1Q31 – Format:
31 0
11 - 13
IQ - Examples
I8Q24 – Format:
31 0
11 - 14
IQ-Math can do better!
z = 10.000000238 (0x0A000004)
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
Accumulator 11 11 11 11 00 11 00 00
Redundant
Sign Bit
11 - 17
How is a fraction coded?
~1 ~ 32K 7FFF
½ 16K 4000
0
0 0000
*32768
–½ –16K C000
–1 –32K 8000
Fractions Integers Hex
11 - 19
So how do we really use all this fraction stuff?
The Fix-Point Development Dilemma
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
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
I8 Q24 M
I16 Q48
I8 Q24 X
Align Decimal
Point Of Multiply >> 24
sssssssssssssssssI16 Q24
I8 Q24 B
I8 Q24 I8 Q24 Y
11 - 23
IQmath Approach
Multiply Operation
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;
Y = _IQmpy(M, X) + B;
User selects target math type
(in “IQmathLib.h” file)
#if MATH_TYPE == IQ_MATH #if MATH_TYPE == FLOAT_MATH
#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”
11 - 33
AC Induction Motor Example
Q stability range
Q18 to Q0 Unstable
(not enough resolution, quantization problems)
11 - 34
Where Is IQmath Applicable?
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