- Keep MSB drops low‑order bits and shortens the fraction length so that all necessary integer bits fit; no overflow occurs.
- Keep LSB keeps the binary‑point position, preserving the fractional resolution, and simply lets any excess high‑order bits fall off.
keep msb vs keep lsb maths(fixed point toolbox)
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
what is the difference between keep msb maths and keep lsb maths in matlab
0 comentarios
Respuestas (2)
TED MOSBY
el 9 de Mayo de 2025
Hi,
In MATLAB’s Fixed‑point designer the terms Keep MSB (keep most‑significant bits) and Keep LSB (keep least‑significant bits) appear in the fimath object settings for SumMode and ProductMode.
They tell MATLAB how to squeeze a full‑precision result back into a shorter word‑length when you don’t choose full‑precision arithmetic.
Here's the link for the function fimath:
Hope this helps!
0 comentarios
Andy Bartlett
el 13 de Mayo de 2025
Keep-Most-Significant-Bits vs Keep-Least-Significant Bits vs Full-Precision
Let's consider three different fimath settings.
- One configured to Keep the 32 Most Significant Bits (MSBs) of Products and Sum
- One configured to Keep the 32 Least Significant Bits (LSBs) of Products and Sum
- The default which returns Full Precision Products and Sums
fmMS32Bits = fimath('RoundingMethod', 'Floor', ...
'OverflowAction', 'Wrap', ...
'ProductMode', 'KeepMSB', ...
'ProductWordLength', 32, ...
'SumMode', 'KeepMSB', ...
'SumWordLength', 32, ...
'CastBeforeSum', true);
fmLS32Bits = fimath('RoundingMethod', 'Floor', ...
'OverflowAction', 'Wrap', ...
'ProductMode', 'KeepLSB', ...
'ProductWordLength', 32, ...
'SumMode', 'KeepLSB', ...
'SumWordLength', 32, ...
'CastBeforeSum', true);
fmDefault = fimath
32 Bit Full Precision Example
Now let's apply this to multiplying two 16 bit fixed-point values.
- When multiplying two 16 bit values, the full precision answer is 32 bits.
- Keeping the 32 MSBs of a 32 bit full precision result just keeps the whole thing
- Keeping the 32 LSBs of a 32 bit full precision result just keeps the whole thing
So the results are identical in all three cases. As in this example, where the first two rows are the multiplicands and the last three rows are the FullPrecision, Keep32MSB, and Keep32LSB products, respectively.
% Type Real World Notation: Binary Point
% Value
% numerictype(0,16,2) 13672.25 = 11010101101000.01
% numerictype(0,16,10) 54.5986328125 = 110110.1001100101
% numerictype(0,32,12) 746486.15747070312 = 10110110001111110110.001010000101
% numerictype(0,32,12) 746486.15747070312 = 10110110001111110110.001010000101
% numerictype(0,32,12) 746486.15747070312 = 10110110001111110110.001010000101
48 Bit Full Precision Example
More interesting is a case of multiplying a 32 bit value by a 16 bit value.
- When multiplying 32 bits by 16 bit values, the full precision answer is 48 bits.
- Keeping the 32 MSBs of a 48 bit full precision result drops the 16 LSBs
- Keeping the 32 LSBs of a 48 bit full precision result drops the 16 MSBs
When KeepMSB requires dropping LSBs the Rounding Method matters, for the case of Floor as configured above the LSBs are just thrown away and the remaining bits are not affected. If a different rounding method was selected, then one may be added to the remaining stored integer bits if rounding up is called for.
When KeepLSB requires dropping MSBs the Overflow Action matters, for the case of Wrap as configured above the MSBs are just thrown away and the remaining bits are not affected. If Saturation was used instead, then in the case of an overflow like this example the output would be very different.
% Type Real World Notation: Binary Point
% Value
% numerictype(0,32,18) 13672.211227416992 = 11010101101000.001101100001001100
% numerictype(0,16,10) 54.5986328125 = 110110.1001100101
% numerictype(0,48,28) 746484.04054068029 = 10110110001111110100.0000101001100000110111111100
% numerictype(0,32,12) 746484.04052734375 = 10110110001111110100.000010100110
% numerictype(0,32,28) 4.0405406802892685 = 0100.0000101001100000110111111100
Common Practices
- A key reason not to use Full-Precision is to reduce the costs on the embedded system of big word lengths
- KeepMSB is generally better when handling approximations of the real world values of engineering signals, like the examples above.
- KeepLSB is generally better when you are just doing math on indices and you are confident the indicies will not overflow the KeepLSB bit limit you've specified.
- You'll often have good engineering reasons to go beyond the simple rules of FullPrecision, KeepMSB, and KeepLSB. Using fimath and other techniques, you can control exactly what range bits are dropped and what precision bits are dropped to match the needs of your design.
Example MATLAB Code
wordLengthA = 32;
wordLengthB = 16;
format compact
format long
a = fi(17*pi*2^8,0,wordLengthA)
b = fi(exp(4),0,wordLengthB)
prodFullPrecision = a * b
prodMS32Bits = mulWithFiMath(a,b,fmMS32Bits)
prodLS32Bits = mulWithFiMath(a,b,fmLS32Bits)
function y = mulWithFiMath(a,b,fm)
temp = setfimath(a,fm) * setfimath(b,fm);
y = removefimath(temp);
end
0 comentarios
Ver también
Categorías
Más información sobre Fixed-Point Designer en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!