Lec 12: Fixed

advertisement
ELET 7404 Embedded & Real Time
Operating Systems
Fixed-Point Math
Chap. 9, Labrosse Book
Fall 2007
Fixed-Point Math
• Most low-end processors, such as embedded processors
Do not provide hardware-assisted floating-point math
• The PIC18F452 only supports a 8x8 hardware multiply
• Look at Table 1: Performance Comparison
‰ Without Hardware Math support
‰ With Hardware Math support
Table 1. Performance Comparison for Processors
With and Without Hardware Math Support
Fixed-Point Math
(cont.)
ƒ As an embedded system programmer confronted
with the task of writing code that is:
• Fast
• Small in bytes
The big question ?
ƒ Without floating-point math
ƒ How would you perform the following
12.34V + 987.654 =
3.1416 X 5.4
=
0.00456/98.7
=
‰ FIXED-POINT MATH
‰ Integer Math
Fixed-Point Math
(cont.)
‰ Fixed-Point Math is integer math that allows
fractions.
‰ The idea: Trick the computer into thinking
you are talking about an integer when in fact
you, as the programmer, know that you are
dealing with a number that has a fractional
component.
Fixed-Point Math
(cont.)
‰ The computer only thinks in bits, e.g.
0000000000010000
or 24 , or 16
‰ However, this assumes that the right most bit
represents 20
‰ Does it have to be ?
Fixed-Point Math
(cont.)
‰ Implied decimal point (radix)
– normally rightmost bit
‰ Why can’t I put it someplace else ? I can
‰ Programmer decides to place radix between the 5th
and 6th bit position (rightmost bit weight isÎ2-5)
0000000000010000
is no longer 16
now it is
0.5
In other words, 16 has been scaled by 2-5
2-5 = 1/25 = 1/32 = 0.03125
i.e. 16 * 0.03125 = 0.5
‰ Radix between the 5th and 6th bit position
‰ This means that the rightmost bit weight is: 2-5
Fixed-Point Math
(cont.)
ƒ So as far as the computer knows
0000000000010000
Its dealing with plain old 16
ƒ But the programmer (THAT’S YOU!)
Know otherwise
ƒ So, important point
• It’s the programmer’s job to keep track of radix
placement
Fixed-Point Math
(cont.)
‰ By manipulating the radix position
the programmer (YOU!) can scale integers into
fractional values.
‰ Location of the radix defines a convention for
how the program will interpret a 16-bit string
‰ As the radix point moves to the left
• The factional portion of string increases
• The fraction becomes more precise
• However, the overall range of numbers diminishes
• Because there are fewer whole-number places
Fixed-Point Math
(cont.)
‰ When the program performs arithmetic on fixed point
numbers, it actually manipulates integers
• Add
• Subtract
• Multiply
• Divide
‰ Microprocessors do not provide mechanisms to
represent fixed-point numbers
Fixed-Point Math
(cont.)
Fixed-point number :
<mantissa> S <exponent>
Where S means:
That the Mantissa needs to be Scaled by 2exponent
- Exponent is also called the scale factor
- Mantissa is always an integer number
Fixed-Point Number Notation
Fixed-point number : <mantissa> S <exponent>
S means Mantissa needs to be scaled by 2exponent
Example 1:
5S-3
Represents 0.6250 or 5 x 2-3 = 5/23 = 5/8
1 x 2-1 = 1 x 1/2 = 0.5
0 x 2-2 = 0 x 1/ 22 = 1/4 = 0
1 x 2-3 = 0 x 1/ 23 = 1/8 = 0.125
SUM = 0.5 + 0.125 = 0.625
- Mantissa is a number represented by an integer
- Exponent is maintained mentally by the programmer
Fixed-Point Number Notation
Fixed-point number : <mantissa> S <exponent>
S means Mantissa needs to be scaled by 2exponent
Example 2:
31S-8
Represents 0.1211 or 31 x 2-8 = 31/256
1 x 2-4 = 1 x 1/24 = 1/16 =0.0625
1 x 2-5 = 1 x 1/25 = 1/32 =0.03125
1 x 2-6 = 1 x 1/26 = 1/64 = 0.015625
1 x 2-7 = 1 x 1/27 = 1/128 = 0.0078125
1 x 2-8 = 1 x 1/28 = 1/256 = 0.00390625
SUM = 0.0625+0.03125+0.015625+0.0078125+
+ 0.00390625 = 0.1211
Fixed-Point Number Notation
Fixed-point number : <mantissa> S <exponent>
S means Mantissa needs to be scaled by 2exponent
Example 3:
-123S-16
Represents -0.001877 or -123 x 2-16 = -123/216
= -123/65536
- Mantissa is a number represented by an integer
- Exponent is maintained mentally by the programmer
Scaling
‰ Scaling is done to allow almost any number to be
represented using a 16-bit number.
‰ Various equations have been developed to make
this representation possible.
INT( ) means take the integer part of the result.
Results of INT must be truncated. Meaning, everything
after the decimal point is removed. No rounding occurs
Scaling
(cont.)
This truncation, INT( ), does cause us to lose a little
accuracy but it is worth the gain in processor resources.
Example 4:
To represent the number 1.2345 in fixed-point math
log(65535/1.2345) = log(53086.27) = 4.725
log(2) = 0.301
INT(4.725/0.301) = INT(15.697) = 15
Thus, the number 1.2345 is written as 40452 S-15
Scaling
(cont.)
To represent in fixed-point math, with 16 bits, numbers
greater than 216 = 65535.0, the formula is:
However, this will cause loss in resolution, and we
would need more than 16 bits to represent large
numbers without loss in resolution.
Scaling
(cont.)
Example 5:
To represent the number 107573 in fixed-point math
log(107573/65535) = log( 1.64146) = 0.21523
log(2) = 0.301
INT(0.21523/0.301) =INT(0.715) = 0
Thus, the number 107573 is written as 53786 S1
However, loss in resolution, we would need 17-bits to
represent this number
Scaling
(cont.)
• To represent any signed number between -32767.0
to +32767.0
• To represent a signed number that is less than
- 32767.0 and greater than +32767.0
Download