simple method of forcing single precision floating point operations

조회 수: 15 (최근 30일)
Jonah Caplan
Jonah Caplan 2015년 8월 10일
답변: leong 2019년 7월 2일
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
dpb
dpb 2015년 8월 10일
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]

댓글을 달려면 로그인하십시오.

답변 (3개)

Adam Barber
Adam Barber 2015년 8월 12일
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
Jonah Caplan
Jonah Caplan 2015년 8월 12일
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 2015년 8월 12일
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!

댓글을 달려면 로그인하십시오.


Amish Ghadiya
Amish Ghadiya 2016년 8월 22일
편집: dpb 2016년 8월 23일
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 2019년 7월 2일
the code replacement library is intended for such use.. although it might not be as easy..

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by