How can I set 'CastBeforeSum' to be False in Simulink?

2 visualizaciones (últimos 30 días)
I'm getting an error while trying to set the following specifications using "fimath" within a MATLAB function block in Simulink. The specifications are as follows:
fimath('CastBeforeSum' , false , ...
'OverflowAction' , 'Saturate' , ...
'RoundingMethod' , 'Round' ,...
'ProductMode' , 'SpecifyPrecision' , ...
'ProductWordLength' , 15, ...
'ProductFractionLength', 5, ...
'SumMode' , 'SpecifyPrecision' , ...
'SumWordLength' , 15, ...
'SumFractionLength' , 5);
The specific error message is: "fi math operations require CastBeforeSum to be true when SumMode is not FullPrecision." 
This error does not appear while setting the same specifications in MATLAB. Why does doing so in Simulink throw an error? If this can't be changed, is there a workaround to set "CastBeforeSum" to be false in Simulink? I want these specific rulesets to apply as I am dealing with HDL code generation. 

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 18 de Sept. de 2023
By design, all sum operations in Simulink do a cast into sum (or accumulator) type before adding the operands. Therefore you need either "CastBeforeSum" to be true or the "SumMode" to be "FullPrecision" to avoid the error in Simulink. 
There are a few possible workarounds for this. One such workaround is to first perform the sum in full precision and then cast it back into the desired precision as follows:
function c = mysum_no_cast_before_sum(a,b)
% Compute a + b (or a * b) in full precision (the default),
% then cast to the sum type with saturation and round.
T = mysum_types;
c = cast(a + b, 'like',T.c);
end
function T = mysum_types
% Specify the type of the sum or product
T.c = fi([],1,15,5,...
'OverflowAction' , 'Saturate' , ...
'RoundingMethod' , 'Round');
end
This can be used as a MATLAB function block. You can verify this with the generated HDL code to see that the sum was computed in full precision and then cast back to the specified sum type. 
Another possible way is to first define the output and then assign into it. This can be done as follows: 
function c = mysum_no_cast_before_sum(a,b)
% Define the result as the type and math you want.
c = fi(0,1,15,5,...
'OverflowAction' , 'Saturate' , ...
'RoundingMethod' , 'Round');
% Then do the sum in full precision (which is the default), then assign
% in to the result. The assign in c(:)=... will cast the full
% precision sum into the result type.
c(:) = a + b;
end
The result is the same while doing either of the methods. 

Más respuestas (0)

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by