m7 Fixed Point
m7 Fixed Point
Carsten Gremzow
Fixed Point Arithmetic
M7 ESLD 2/19
Fixed Point Arithmetic
Floating Point
M7 ESLD 3/19
Fixed Point Arithmetic
Floating Point
M7 ESLD 4/19
Fixed Point Arithmetic
Floating Point
Add - typically 4 clocks:
compare, shift, add, normalize
Multiply - typically 8 clocks:
add, fixed point multiply, normalize, add
Divide - typically 20-40 clocks
M7 ESLD 5/19
Fixed Point Arithmetic
Typical System Level Design Flow
M7 ESLD 6/19
Fixed Point Arithmetic
M7 ESLD 7/19
Fixed Point Arithmetic
M7 ESLD 8/19
Fixed Point Arithmetic
M7 ESLD 9/19
Fixed Point Arithmetic
Qn.m Positve Numbers
the MSB is the sign bit
for a positive fixed-point-number, MSB is ’0’:
for negative numbers, MSB has neative weight and the equivalent
value is
M7 ESLD 10/19
Fixed Point Arithmetic
Conversion to Qn.m
1. define total number of bits to reresent a Qn.m number
§ assume ten bits in the example
2. fix location of the decimal based on the value of the number
§ assume two bits for the integer part
§ the decimal point is implied
M7 ESLD 11/19
Fixed Point Arithmetic
Example
two bits for the integer and remaining eight bit keeps fractional part
a ten bit Q2.8 signed number covers -2 to +1.9922
increasing the fractional bits increases the precision
M7 ESLD 12/19
Fixed Point Arithmetic
M7 ESLD 13/19
Fixed Point Arithmetic
The Software Side
using 16, 32 and 64 Bit Integer Types for fixed point arithmetic
§ Ñ short, int and long int in C
Converting a floating point number to fixed point:
§ Multiply the float by a power of 2 represented by a floating point
value, and cast the result to an integer:
fp_pi = (int)(3.141593f * 65536.0f); // 16 bits
fractional
§ After calculations, cast the result to int by discarding the fractional
bits. E.g.:
int result = fp_pi » 16; // divide by 65536
§ Or, get the original float back by casting to float and dividing by
2fractionalbits :
float result = (float)fp_pi / 65536.0f;
§ Note that this last option has significant overhead, which should be
outweighed by the gains.
M7 ESLD 14/19
Fixed Point Arithmetic
M7 ESLD 15/19
Fixed Point Arithmetic
M7 ESLD 16/19
Fixed Point Arithmetic
M7 ESLD 17/19
Fixed Point Arithmetic
The Hardware Side
as in software addition and subtraction operation remain the same
§ it’s the sofware’s task to perform operand conversion / scaling
§ in VHDL:
s_sum <= s_a + s_b; – beware of the carry bit..
multiplication is harder yet simpler at the same time
§ you will need the full resulting width of the multiplication operation
§ perform shifting and truncation of leading bits of the result in a
separate assignment
§ cannot be performed in a single statement
§ concurrent example in VHDL
signal s_a16, s_b16, s_prod16 : std_logic_vector(15 downto 0); -- 16 bit signed
signal s_prod32 : std_logic_vector(31 downto 0);
M7 ESLD 18/19
Fixed Point Arithmetic
M7 ESLD 19/19