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.
If the standard math library 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
.
x = -pi:0.01:pi; codegen -singleC mysine -args {x} -config:lib -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 MATLAB code tab. Under Highlight,
select the Double-precision operations check
box. Under Functions, click mysine
.
In the code pane, the report highlights the sin
call.
To address this warning, specify use of the C99 (ISO) standard math library.
At the command line:
cfg = coder.config('lib'); cfg.TargetLangStandard = 'C99 (ISO)'
In the app, in the project build settings, on the Custom
Code tab, set Standard math library to C99
(ISO)
.
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 forgeterf
.
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 MATLAB code tab. Under Highlight,
select the Double-precision operations check
box. Under Functions, click geterf
.
In the code pane, the report highlights the erf
call.
To address this warning, rewrite your code so that it does not use the function that is implemented in 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 MATLAB code tab. Under Highlight,
select the Double-precision operations check
box. Under Functions, click mysum
.
In the code pane, the report highlights the sum
call.
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.