Warnings from Conversion to Single-Precision C/C++ Code
When you generate single-precision C/C++ code by using the MATLAB®
Coder™ app
or codegen
with the -singleC
option,
you can receive the following warnings.
Function Uses Double-Precision in the C89/C90 Standard
If the language standard is C89/C90, the conversion process warns you when a function uses double-precision code in the C89/C90 standard.
Consider the function mysine
.
function c = mysine(a) c = sin(a); end
Generate single-precision code for mysine
using the C89/C90
standard.
x = -pi:0.01:pi; cfg = coder.config('lib'); cfg.TargetLangStandard = 'C89/C90 (ANSI)'; codegen -singleC -config cfg mysine -args {x} -report
codegen
warns that sin
uses
double-precision in the C89/C90 (ANSI) standard.
Warning: The function sin uses double-precision in the C89/C90 (ANSI) standard. For single-precision code, consider using the C99 (ISO) standard or use your own function.
To open the code generation report, click the View Report link.
To see that double-precision operations remain in the converted code, click the Code
Insights tab. Expand Potential data type issues
and then expand Double-precision operations. The report
indicates that mysine
has a double-precision operation at line
2 c = sin(a)
.
To address this warning, use the default language standard, C99 (ISO).
At the command line:
cfg.TargetLangStandard = 'C99 (ISO)';
In the app, in the project build settings, on the Custom Code tab, set Language standard to
C99 (ISO)
.
Built-In Function Is Implemented in Double-Precision
Some built-in MATLAB functions are implemented using double-precision operations. The conversion process warns that the code generated for these functions contains double-precision operations.
Consider the function geterf
that calls
the built-in function erf
.
function y = geterf(x) y = erf(x); end
Generate single-precision code for
geterf
.
codegen -singleC -config:lib -args {1} geterf -report
codegen
warns that erf
is
implemented in double precision.
Warning: The builtin function erf is implemented in double-precision. Code generated for this function will contain doubles.
To open the code generation report, click the View Report link.
To see that double-precision operations remain in the converted code, click the Code
Insights tab. Expand Potential data type issues
and then expand Double-precision operations. The report
indicates that geterf
has a double-precision operation at line
2 y = erf(x)
.
To address this warning, rewrite your code so that it does not use the function that is implemented in double precision.
Built-In Function Returns Double-Precision
If a built-in MATLAB function returns a double-precision output, the conversion process generates a warning.
Consider the function mysum
that calls
the built-in function sum
.
function y = mysum(x) y = sum(int32(x)); end
Generate single-precision code formysum
.
A = 1:10; codegen -singleC -config:lib -args {A} mysum -report
codegen
warns that mysum
is
implemented in double precision.
Warning: The output of builtin function sum is double-precision and has been cast to single-precision. The code generated for the builtin function may still contain doubles.
To open the code generation report, click the View Report link.
To see that double-precision operations remain in the converted code, click the Code
Insights tab. Expand Potential data type issues
and then expand Double-precision operations. The report
indicates that mysum
has a double-precision operation at line 2
y = sum(int32(x))
.
To address this warning, specify that you want the function
to return the 'native'
class.
(sum(int32(1), 'native')
Using this option causes the function to return the same type as the input.