simple method of forcing single precision floating point operations

16 visualizaciones (últimos 30 días)
Jonah Caplan
Jonah Caplan el 10 de Ag. de 2015
Respondida: leong el 2 de Jul. de 2019
There are some frustrating things that occur when trying to generate single precision math using embedded coder. I only have single precision hardware available so it's important to use single precision operations and variables.
In a simple demonstration function where the inputs and outputs have been set to single in the model explorer:
function y = fcn(u)
y = u;
for i = single(0:1000000-1)
y = i*2+i^2;
end
the bounds of the loop must be specifically set to single or else an error is raised. There are lots of little problems like this that come up that make editing preexisting code/simulink projects pretty frustrating to debug.
Also when using trig and square root functions, the generated c code uses sin and cos instead of sinf and cosf etc. despite the fact that the arguments are single precision. The generated code needs manual editing from what I can tell to get consistent single precision math throughout.
Is there a better way?
  1 comentario
dpb
dpb el 10 de Ag. de 2015
I'd guess the "better way" unless TMW fixes such issues which I wouldn't expect any time soon if ever for the latter problem would be to use #define to do the translation of the library function names.
I unfortunately don't have any suggestions for the other issue; I agree that lacking a declaration facility with Matlab makes dealing with such issues a pit(proverbial)a[ppendage]

Iniciar sesión para comentar.

Respuestas (3)

Adam Barber
Adam Barber el 12 de Ag. de 2015
Hey Jonah,
I'm not sure if you tried this already, but you should be able to specify single precision by following the steps here:
with an example here:
If you don't explicitly set data types anywhere, then Simulink should only generate code for single precision. If you do find inconsistencies I would definitely bring those up with Technical Support.
Hope that helps,
-Adam
  3 comentarios
Jonah Caplan
Jonah Caplan el 12 de Ag. de 2015
also, things get really ugly if the loop bounds are variable because you can't cast the type anymore, which means each individual instance of i in the code needs to have a single() around it.
function y = fcn(u,loopLimit)
y = u;
for i = single(0:loopLimit)
y = i*2+i^2;
end
gives the error:
FOR loop index expressions of unknown size are only supported if they are of the form A:B or A:B:C.
Function 'ForLoop' (#24.37.85), line 3, column 1:
"for i = single(0:loopLimit)"
Launch diagnostic report.
so you need to do:
function y = fcn(u,loopLimit)
y = u;
for i = 0:loopLimit
y = i*single(2)+single(i)^2;
end
which is getting a bit cumbersome
Jonah Caplan
Jonah Caplan el 12 de Ag. de 2015
Better yet, the single() can be eliminated by combining the specify single precision option and ensuring all constants are set to Inherit: Inherit via back propagation rather than Inherit: Inherit from 'Constant value'
phew!

Iniciar sesión para comentar.


Amish Ghadiya
Amish Ghadiya el 22 de Ag. de 2016
Editada: dpb el 23 de Ag. de 2016
How matlab Embedded function support fixed point data type Help me if any one knows the solution.
*Below mentioned Embedded matlab function code*
function [Out1, Out2] = CheckCount(int1,int2)
%#eml
persistent var1 CNT;
if isempty(var1 ); var1 =[0 0 0 0 0]; end
if isempty(CNT); CNT=0 ; end
Out1 = 0;
Out2 = 1;
var1= [ int1, var1 (1:4) ];
if sum(var1) > 20
CNT = CNT + 1 ;
if CNT >= 5 ; CNT=5 ; end
Out2 = 1 - (CNT*0.2);
Out1 = 1;
end
if int2< 7.2
Out2 = 0;
end
input and output data type are same
ex: fixdt(0,16,0.1,0)
When i try to compile below mentioned error is displayed
*This assignment writes a 'double' value into a 'uint8' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches.
Function 'SCA__USSDIST_Embedded MATLAB ' (#1474.182.186), line 8, column 1: "Out1" Launch diagnostic report.*

leong
leong el 2 de Jul. de 2019
the code replacement library is intended for such use.. although it might not be as easy..

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by